uni-list-chat.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. <template>
  2. <!-- #ifdef APP-NVUE -->
  3. <cell>
  4. <!-- #endif -->
  5. <view :hover-class="!clickable && !link ? '' : 'uni-list-chat--hover'" class="uni-list-chat" @click.stop="onClick">
  6. <view :class="{ 'uni-list--border': border, 'uni-list-chat--first': isFirstChild }"></view>
  7. <view class="uni-list-chat__container">
  8. <view class="uni-list-chat__header-warp">
  9. <view v-if="avatarCircle || avatarList.length === 0" class="uni-list-chat__header" :class="{ 'header--circle': avatarCircle }">
  10. <image class="uni-list-chat__header-image" :class="{ 'header--circle': avatarCircle }" :src="avatarUrl" mode="aspectFill"></image>
  11. </view>
  12. <!-- 头像组 -->
  13. <view v-else class="uni-list-chat__header">
  14. <view v-for="(item, index) in avatarList" :key="index" class="uni-list-chat__header-box" :class="computedAvatar"
  15. :style="{ width: imageWidth + 'px', height: imageWidth + 'px' }">
  16. <image class="uni-list-chat__header-image" :style="{ width: imageWidth + 'px', height: imageWidth + 'px' }" :src="item.url"
  17. mode="aspectFill"></image>
  18. </view>
  19. </view>
  20. </view>
  21. <view v-if="badgeText && badgePositon === 'left'" class="uni-list-chat__badge uni-list-chat__badge-pos" :class="[isSingle]">
  22. <text class="uni-list-chat__badge-text">{{ badgeText === 'dot' ? '' : badgeText }}</text>
  23. </view>
  24. <view class="uni-list-chat__content">
  25. <view class="uni-list-chat__content-main">
  26. <text class="uni-list-chat__content-title uni-ellipsis">{{ title }}</text>
  27. <view style="flex-direction: row;">
  28. <text class="draft" v-if="isDraft">[草稿]</text>
  29. <text class="uni-list-chat__content-note uni-ellipsis">{{isDraft?note.slice(14):note}}</text>
  30. </view>
  31. </view>
  32. <view class="uni-list-chat__content-extra">
  33. <slot>
  34. <text class="uni-list-chat__content-extra-text">{{ time }}</text>
  35. <view v-if="badgeText && badgePositon === 'right'" class="uni-list-chat__badge" :class="[isSingle, badgePositon === 'right' ? 'uni-list-chat--right' : '']">
  36. <text class="uni-list-chat__badge-text">{{ badgeText === 'dot' ? '' : badgeText }}</text>
  37. </view>
  38. </slot>
  39. </view>
  40. </view>
  41. </view>
  42. </view>
  43. <!-- #ifdef APP-NVUE -->
  44. </cell>
  45. <!-- #endif -->
  46. </template>
  47. <script>
  48. // 头像大小
  49. const avatarWidth = 45;
  50. /**
  51. * ListChat 聊天列表
  52. * @description 聊天列表,用于创建聊天类列表
  53. * @tutorial https://ext.dcloud.net.cn/plugin?id=24
  54. * @property {String} title 标题
  55. * @property {String} note 描述
  56. * @property {Boolean} clickable = [true|false] 是否开启点击反馈,默认为false
  57. * @property {String} badgeText 数字角标内容
  58. * @property {String} badgePositon = [left|right] 角标位置,默认为 right
  59. * @property {String} link = [false|navigateTo|redirectTo|reLaunch|switchTab] 是否展示右侧箭头并开启点击反馈,默认为false
  60. * @value false 不开启
  61. * @value navigateTo 同 uni.navigateTo()
  62. * @value redirectTo 同 uni.redirectTo()
  63. * @value reLaunch 同 uni.reLaunch()
  64. * @value switchTab 同 uni.switchTab()
  65. * @property {String | PageURIString} to 跳转目标页面
  66. * @property {String} time 右侧时间显示
  67. * @property {Boolean} avatarCircle = [true|false] 是否显示圆形头像,默认为false
  68. * @property {String} avatar 头像地址,avatarCircle 不填时生效
  69. * @property {Array} avatarList 头像组,格式为 [{url:''}]
  70. * @event {Function} click 点击 uniListChat 触发事件
  71. */
  72. export default {
  73. name: 'UniListChat',
  74. emits:['click'],
  75. props: {
  76. title: {
  77. type: String,
  78. default: ''
  79. },
  80. note: {
  81. type: String,
  82. default: ''
  83. },
  84. clickable: {
  85. type: Boolean,
  86. default: false
  87. },
  88. link: {
  89. type: [Boolean, String],
  90. default: false
  91. },
  92. to: {
  93. type: String,
  94. default: ''
  95. },
  96. badgeText: {
  97. type: [String, Number],
  98. default: ''
  99. },
  100. badgePositon: {
  101. type: String,
  102. default: 'right'
  103. },
  104. time: {
  105. type: String,
  106. default: ''
  107. },
  108. avatarCircle: {
  109. type: Boolean,
  110. default: false
  111. },
  112. avatar: {
  113. type: String,
  114. default: ''
  115. },
  116. avatarList: {
  117. type: Array,
  118. default () {
  119. return [];
  120. }
  121. }
  122. },
  123. // inject: ['list'],
  124. computed: {
  125. isDraft(){
  126. return this.note.slice(0,14) == '[uni-im-draft]'
  127. },
  128. isSingle() {
  129. if (this.badgeText === 'dot') {
  130. return 'uni-badge--dot';
  131. } else {
  132. const badgeText = this.badgeText.toString();
  133. if (badgeText.length > 1) {
  134. return 'uni-badge--complex';
  135. } else {
  136. return 'uni-badge--single';
  137. }
  138. }
  139. },
  140. computedAvatar() {
  141. if (this.avatarList.length > 4) {
  142. this.imageWidth = avatarWidth * 0.31;
  143. return 'avatarItem--3';
  144. } else if (this.avatarList.length > 1) {
  145. this.imageWidth = avatarWidth * 0.47;
  146. return 'avatarItem--2';
  147. } else {
  148. this.imageWidth = avatarWidth;
  149. return 'avatarItem--1';
  150. }
  151. }
  152. },
  153. watch: {
  154. avatar:{
  155. handler(avatar) {
  156. if(avatar.substr(0,8) == 'cloud://'){
  157. uniCloud.getTempFileURL({
  158. fileList: [avatar]
  159. }).then(res=>{
  160. // console.log(res);
  161. // 兼容uniCloud私有化部署
  162. let fileList = res.fileList || res.result.fileList
  163. this.avatarUrl = fileList[0].tempFileURL
  164. })
  165. }else{
  166. this.avatarUrl = avatar
  167. }
  168. },
  169. immediate: true
  170. }
  171. },
  172. data() {
  173. return {
  174. isFirstChild: false,
  175. border: true,
  176. // avatarList: 3,
  177. imageWidth: 50,
  178. avatarUrl:''
  179. };
  180. },
  181. mounted() {
  182. this.list = this.getForm()
  183. if (this.list) {
  184. if (!this.list.firstChildAppend) {
  185. this.list.firstChildAppend = true;
  186. this.isFirstChild = true;
  187. }
  188. this.border = this.list.border;
  189. }
  190. },
  191. methods: {
  192. /**
  193. * 获取父元素实例
  194. */
  195. getForm(name = 'uniList') {
  196. let parent = this.$parent;
  197. let parentName = parent.$options.name;
  198. while (parentName !== name) {
  199. parent = parent.$parent;
  200. if (!parent) return false
  201. parentName = parent.$options.name;
  202. }
  203. return parent;
  204. },
  205. onClick() {
  206. if (this.to !== '') {
  207. this.openPage();
  208. return;
  209. }
  210. if (this.clickable || this.link) {
  211. this.$emit('click', {
  212. data: {}
  213. });
  214. }
  215. },
  216. openPage() {
  217. if (['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'].indexOf(this.link) !== -1) {
  218. this.pageApi(this.link);
  219. } else {
  220. this.pageApi('navigateTo');
  221. }
  222. },
  223. pageApi(api) {
  224. uni[api]({
  225. url: this.to,
  226. success: res => {
  227. this.$emit('click', {
  228. data: res
  229. });
  230. },
  231. fail: err => {
  232. this.$emit('click', {
  233. data: err
  234. });
  235. console.error(err.errMsg);
  236. }
  237. });
  238. }
  239. }
  240. };
  241. </script>
  242. <style lang="scss" >
  243. $uni-font-size-lg:16px;
  244. $uni-spacing-row-sm: 5px;
  245. $uni-spacing-row-base: 10px;
  246. $uni-spacing-row-lg: 15px;
  247. $background-color: #fff;
  248. $divide-line-color: #e5e5e5;
  249. $avatar-width: 45px;
  250. $avatar-border-radius: 5px;
  251. $avatar-border-color: #eee;
  252. $avatar-border-width: 1px;
  253. $title-size: 16px;
  254. $title-color: #3b4144;
  255. $title-weight: normal;
  256. $note-size: 12px;
  257. $note-color: #999;
  258. $note-weight: normal;
  259. $right-text-size: 12px;
  260. $right-text-color: #999;
  261. $right-text-weight: normal;
  262. $badge-left: 0px;
  263. $badge-top: 0px;
  264. $dot-width: 10px;
  265. $dot-height: 10px;
  266. $badge-size: 18px;
  267. $badge-font: 12px;
  268. $badge-color: #fff;
  269. $badge-background-color: #ff5a5f;
  270. $badge-space: 6px;
  271. $hover: #f5f5f5;
  272. .uni-list-chat {
  273. font-size: $uni-font-size-lg;
  274. position: relative;
  275. flex-direction: column;
  276. justify-content: space-between;
  277. background-color: $background-color;
  278. }
  279. // .uni-list-chat--disabled {
  280. // opacity: 0.3;
  281. // }
  282. .uni-list-chat--hover {
  283. background-color: $hover;
  284. }
  285. .uni-list--border {
  286. position: relative;
  287. margin-left: $uni-spacing-row-lg;
  288. /* #ifdef APP-PLUS */
  289. border-top-color: $divide-line-color;
  290. border-top-style: solid;
  291. border-top-width: 0.5px;
  292. /* #endif */
  293. }
  294. /* #ifndef APP-NVUE */
  295. .uni-list--border:after {
  296. position: absolute;
  297. top: 0;
  298. right: 0;
  299. left: 0;
  300. height: 1px;
  301. content: '';
  302. -webkit-transform: scaleY(0.5);
  303. transform: scaleY(0.5);
  304. background-color: $divide-line-color;
  305. }
  306. .uni-list-item--first:after {
  307. height: 0px;
  308. }
  309. /* #endif */
  310. .uni-list-chat--first {
  311. border-top-width: 0px;
  312. }
  313. .uni-ellipsis {
  314. /* #ifndef APP-NVUE */
  315. overflow: hidden;
  316. white-space: nowrap;
  317. text-overflow: ellipsis;
  318. /* #endif */
  319. /* #ifdef APP-NVUE */
  320. lines: 1;
  321. /* #endif */
  322. }
  323. .uni-ellipsis-2 {
  324. /* #ifndef APP-NVUE */
  325. overflow: hidden;
  326. text-overflow: ellipsis;
  327. display: -webkit-box;
  328. -webkit-line-clamp: 2;
  329. -webkit-box-orient: vertical;
  330. /* #endif */
  331. /* #ifdef APP-NVUE */
  332. lines: 2;
  333. /* #endif */
  334. }
  335. .uni-list-chat__container {
  336. position: relative;
  337. /* #ifndef APP-NVUE */
  338. display: flex;
  339. /* #endif */
  340. flex-direction: row;
  341. flex: 1;
  342. padding: $uni-spacing-row-base $uni-spacing-row-lg;
  343. position: relative;
  344. overflow: hidden;
  345. }
  346. .uni-list-chat__header-warp {
  347. position: relative;
  348. }
  349. .uni-list-chat__header {
  350. /* #ifndef APP-NVUE */
  351. display: flex;
  352. align-content: center;
  353. /* #endif */
  354. flex-direction: row;
  355. justify-content: center;
  356. align-items: center;
  357. flex-wrap: wrap-reverse;
  358. /* #ifdef APP-NVUE */
  359. width: 50px;
  360. height: 50px;
  361. /* #endif */
  362. /* #ifndef APP-NVUE */
  363. width: $avatar-width;
  364. height: $avatar-width;
  365. /* #endif */
  366. border-radius: $avatar-border-radius;
  367. border-color: $avatar-border-color;
  368. border-width: $avatar-border-width;
  369. border-style: solid;
  370. overflow: hidden;
  371. }
  372. .uni-list-chat__header-box {
  373. /* #ifndef APP-PLUS */
  374. box-sizing: border-box;
  375. display: flex;
  376. width: $avatar-width;
  377. height: $avatar-width;
  378. /* #endif */
  379. /* #ifdef APP-NVUE */
  380. width: 50px;
  381. height: 50px;
  382. /* #endif */
  383. overflow: hidden;
  384. border-radius: 2px;
  385. }
  386. .uni-list-chat__header-image {
  387. margin: 1px;
  388. /* #ifdef APP-NVUE */
  389. width: 50px;
  390. height: 50px;
  391. /* #endif */
  392. /* #ifndef APP-NVUE */
  393. width: $avatar-width;
  394. height: $avatar-width;
  395. /* #endif */
  396. }
  397. /* #ifndef APP-NVUE */
  398. .uni-list-chat__header-image {
  399. display: block;
  400. width: 100%;
  401. height: 100%;
  402. }
  403. .avatarItem--1 {
  404. width: 100%;
  405. height: 100%;
  406. }
  407. .avatarItem--2 {
  408. width: 47%;
  409. height: 47%;
  410. }
  411. .avatarItem--3 {
  412. width: 32%;
  413. height: 32%;
  414. }
  415. /* #endif */
  416. .header--circle {
  417. border-radius: 50%;
  418. }
  419. .uni-list-chat__content {
  420. /* #ifndef APP-NVUE */
  421. display: flex;
  422. /* #endif */
  423. flex-direction: row;
  424. flex: 1;
  425. overflow: hidden;
  426. padding: 2px 0;
  427. }
  428. .uni-list-chat__content-main {
  429. /* #ifndef APP-NVUE */
  430. display: flex;
  431. /* #endif */
  432. flex-direction: column;
  433. justify-content: space-between;
  434. padding-left: $uni-spacing-row-base;
  435. flex: 1;
  436. overflow: hidden;
  437. }
  438. .uni-list-chat__content-title {
  439. font-size: $title-size;
  440. color: $title-color;
  441. font-weight: $title-weight;
  442. overflow: hidden;
  443. }
  444. .draft ,.uni-list-chat__content-note {
  445. margin-top: 3px;
  446. color: $note-color;
  447. font-size: $note-size;
  448. font-weight: $title-weight;
  449. overflow: hidden;
  450. }
  451. .draft{
  452. color: #eb3a41;
  453. /* #ifndef APP-NVUE */
  454. flex-shrink: 0;
  455. /* #endif */
  456. padding-right: 3px;
  457. }
  458. .uni-list-chat__content-extra {
  459. /* #ifndef APP-NVUE */
  460. flex-shrink: 0;
  461. display: flex;
  462. /* #endif */
  463. flex-direction: column;
  464. justify-content: space-between;
  465. align-items: flex-end;
  466. margin-left: 5px;
  467. }
  468. .uni-list-chat__content-extra-text {
  469. color: $right-text-color;
  470. font-size: $right-text-size;
  471. font-weight: $right-text-weight;
  472. overflow: hidden;
  473. }
  474. .uni-list-chat__badge-pos {
  475. position: absolute;
  476. /* #ifdef APP-NVUE */
  477. left: 55px;
  478. top: 3px;
  479. /* #endif */
  480. /* #ifndef APP-NVUE */
  481. left: calc(#{$avatar-width} + 10px - #{$badge-space} + #{$badge-left});
  482. top: calc(#{$uni-spacing-row-base}/ 2 + 1px + #{$badge-top});
  483. /* #endif */
  484. }
  485. .uni-list-chat__badge {
  486. /* #ifndef APP-NVUE */
  487. display: flex;
  488. /* #endif */
  489. justify-content: center;
  490. align-items: center;
  491. border-radius: 100px;
  492. background-color: $badge-background-color;
  493. }
  494. .uni-list-chat__badge-text {
  495. color: $badge-color;
  496. font-size: $badge-font;
  497. }
  498. .uni-badge--single {
  499. /* #ifndef APP-NVUE */
  500. // left: calc(#{$avatar-width} + 7px + #{$badge-left});
  501. /* #endif */
  502. width: $badge-size;
  503. height: $badge-size;
  504. }
  505. .uni-badge--complex {
  506. /* #ifdef APP-NVUE */
  507. left: 50px;
  508. /* #endif */
  509. /* #ifndef APP-NVUE */
  510. width: auto;
  511. /* #endif */
  512. height: $badge-size;
  513. padding: 0 $badge-space;
  514. }
  515. .uni-badge--dot {
  516. /* #ifdef APP-NVUE */
  517. left: 60px;
  518. top: 6px;
  519. /* #endif */
  520. /* #ifndef APP-NVUE */
  521. left: calc(#{$avatar-width} + 15px - #{$dot-width}/ 2 + 1px + #{$badge-left});
  522. /* #endif */
  523. width: $dot-width;
  524. height: $dot-height;
  525. padding: 0;
  526. }
  527. .uni-list-chat--right {
  528. /* #ifdef APP-NVUE */
  529. left: 0;
  530. /* #endif */
  531. }
  532. </style>