uni-list-chat.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. let callback = {
  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. }
  236. }
  237. switch (api) {
  238. case 'navigateTo':
  239. uni.navigateTo(callback)
  240. break
  241. case 'redirectTo':
  242. uni.redirectTo(callback)
  243. break
  244. case 'reLaunch':
  245. uni.reLaunch(callback)
  246. break
  247. case 'switchTab':
  248. uni.switchTab(callback)
  249. break
  250. default:
  251. uni.navigateTo(callback)
  252. }
  253. }
  254. }
  255. };
  256. </script>
  257. <style lang="scss" >
  258. $uni-font-size-lg:16px;
  259. $uni-spacing-row-sm: 5px;
  260. $uni-spacing-row-base: 10px;
  261. $uni-spacing-row-lg: 15px;
  262. $background-color: #fff;
  263. $divide-line-color: #e5e5e5;
  264. $avatar-width: 45px;
  265. $avatar-border-radius: 5px;
  266. $avatar-border-color: #eee;
  267. $avatar-border-width: 1px;
  268. $title-size: 16px;
  269. $title-color: #3b4144;
  270. $title-weight: normal;
  271. $note-size: 12px;
  272. $note-color: #999;
  273. $note-weight: normal;
  274. $right-text-size: 12px;
  275. $right-text-color: #999;
  276. $right-text-weight: normal;
  277. $badge-left: 0px;
  278. $badge-top: 0px;
  279. $dot-width: 10px;
  280. $dot-height: 10px;
  281. $badge-size: 18px;
  282. $badge-font: 12px;
  283. $badge-color: #fff;
  284. $badge-background-color: #ff5a5f;
  285. $badge-space: 6px;
  286. $hover: #f5f5f5;
  287. .uni-list-chat {
  288. font-size: $uni-font-size-lg;
  289. position: relative;
  290. flex-direction: column;
  291. justify-content: space-between;
  292. background-color: $background-color;
  293. }
  294. // .uni-list-chat--disabled {
  295. // opacity: 0.3;
  296. // }
  297. .uni-list-chat--hover {
  298. background-color: $hover;
  299. }
  300. .uni-list--border {
  301. position: relative;
  302. margin-left: $uni-spacing-row-lg;
  303. /* #ifdef APP-PLUS */
  304. border-top-color: $divide-line-color;
  305. border-top-style: solid;
  306. border-top-width: 0.5px;
  307. /* #endif */
  308. }
  309. /* #ifndef APP-NVUE */
  310. .uni-list--border:after {
  311. position: absolute;
  312. top: 0;
  313. right: 0;
  314. left: 0;
  315. height: 1px;
  316. content: '';
  317. -webkit-transform: scaleY(0.5);
  318. transform: scaleY(0.5);
  319. background-color: $divide-line-color;
  320. }
  321. .uni-list-item--first:after {
  322. height: 0px;
  323. }
  324. /* #endif */
  325. .uni-list-chat--first {
  326. border-top-width: 0px;
  327. }
  328. .uni-ellipsis {
  329. /* #ifndef APP-NVUE */
  330. overflow: hidden;
  331. white-space: nowrap;
  332. text-overflow: ellipsis;
  333. /* #endif */
  334. /* #ifdef APP-NVUE */
  335. lines: 1;
  336. /* #endif */
  337. }
  338. .uni-ellipsis-2 {
  339. /* #ifndef APP-NVUE */
  340. overflow: hidden;
  341. text-overflow: ellipsis;
  342. display: -webkit-box;
  343. -webkit-line-clamp: 2;
  344. -webkit-box-orient: vertical;
  345. /* #endif */
  346. /* #ifdef APP-NVUE */
  347. lines: 2;
  348. /* #endif */
  349. }
  350. .uni-list-chat__container {
  351. position: relative;
  352. /* #ifndef APP-NVUE */
  353. display: flex;
  354. /* #endif */
  355. flex-direction: row;
  356. flex: 1;
  357. padding: $uni-spacing-row-base $uni-spacing-row-lg;
  358. position: relative;
  359. overflow: hidden;
  360. }
  361. .uni-list-chat__header-warp {
  362. position: relative;
  363. }
  364. .uni-list-chat__header {
  365. /* #ifndef APP-NVUE */
  366. display: flex;
  367. align-content: center;
  368. /* #endif */
  369. flex-direction: row;
  370. justify-content: center;
  371. align-items: center;
  372. flex-wrap: wrap-reverse;
  373. /* #ifdef APP-NVUE */
  374. width: 50px;
  375. height: 50px;
  376. /* #endif */
  377. /* #ifndef APP-NVUE */
  378. width: $avatar-width;
  379. height: $avatar-width;
  380. /* #endif */
  381. border-radius: $avatar-border-radius;
  382. border-color: $avatar-border-color;
  383. border-width: $avatar-border-width;
  384. border-style: solid;
  385. overflow: hidden;
  386. }
  387. .uni-list-chat__header-box {
  388. /* #ifndef APP-PLUS */
  389. box-sizing: border-box;
  390. display: flex;
  391. width: $avatar-width;
  392. height: $avatar-width;
  393. /* #endif */
  394. /* #ifdef APP-NVUE */
  395. width: 50px;
  396. height: 50px;
  397. /* #endif */
  398. overflow: hidden;
  399. border-radius: 2px;
  400. }
  401. .uni-list-chat__header-image {
  402. margin: 1px;
  403. /* #ifdef APP-NVUE */
  404. width: 50px;
  405. height: 50px;
  406. /* #endif */
  407. /* #ifndef APP-NVUE */
  408. width: $avatar-width;
  409. height: $avatar-width;
  410. /* #endif */
  411. }
  412. /* #ifndef APP-NVUE */
  413. .uni-list-chat__header-image {
  414. display: block;
  415. width: 100%;
  416. height: 100%;
  417. }
  418. .avatarItem--1 {
  419. width: 100%;
  420. height: 100%;
  421. }
  422. .avatarItem--2 {
  423. width: 47%;
  424. height: 47%;
  425. }
  426. .avatarItem--3 {
  427. width: 32%;
  428. height: 32%;
  429. }
  430. /* #endif */
  431. .header--circle {
  432. border-radius: 50%;
  433. }
  434. .uni-list-chat__content {
  435. /* #ifndef APP-NVUE */
  436. display: flex;
  437. /* #endif */
  438. flex-direction: row;
  439. flex: 1;
  440. overflow: hidden;
  441. padding: 2px 0;
  442. }
  443. .uni-list-chat__content-main {
  444. /* #ifndef APP-NVUE */
  445. display: flex;
  446. /* #endif */
  447. flex-direction: column;
  448. justify-content: space-between;
  449. padding-left: $uni-spacing-row-base;
  450. flex: 1;
  451. overflow: hidden;
  452. }
  453. .uni-list-chat__content-title {
  454. font-size: $title-size;
  455. color: $title-color;
  456. font-weight: $title-weight;
  457. overflow: hidden;
  458. }
  459. .draft ,.uni-list-chat__content-note {
  460. margin-top: 3px;
  461. color: $note-color;
  462. font-size: $note-size;
  463. font-weight: $title-weight;
  464. overflow: hidden;
  465. }
  466. .draft{
  467. color: #eb3a41;
  468. /* #ifndef APP-NVUE */
  469. flex-shrink: 0;
  470. /* #endif */
  471. padding-right: 3px;
  472. }
  473. .uni-list-chat__content-extra {
  474. /* #ifndef APP-NVUE */
  475. flex-shrink: 0;
  476. display: flex;
  477. /* #endif */
  478. flex-direction: column;
  479. justify-content: space-between;
  480. align-items: flex-end;
  481. margin-left: 5px;
  482. }
  483. .uni-list-chat__content-extra-text {
  484. color: $right-text-color;
  485. font-size: $right-text-size;
  486. font-weight: $right-text-weight;
  487. overflow: hidden;
  488. }
  489. .uni-list-chat__badge-pos {
  490. position: absolute;
  491. /* #ifdef APP-NVUE */
  492. left: 55px;
  493. top: 3px;
  494. /* #endif */
  495. /* #ifndef APP-NVUE */
  496. left: calc(#{$avatar-width} + 10px - #{$badge-space} + #{$badge-left});
  497. top: calc(#{$uni-spacing-row-base}/ 2 + 1px + #{$badge-top});
  498. /* #endif */
  499. }
  500. .uni-list-chat__badge {
  501. /* #ifndef APP-NVUE */
  502. display: flex;
  503. /* #endif */
  504. justify-content: center;
  505. align-items: center;
  506. border-radius: 100px;
  507. background-color: $badge-background-color;
  508. }
  509. .uni-list-chat__badge-text {
  510. color: $badge-color;
  511. font-size: $badge-font;
  512. }
  513. .uni-badge--single {
  514. /* #ifndef APP-NVUE */
  515. // left: calc(#{$avatar-width} + 7px + #{$badge-left});
  516. /* #endif */
  517. width: $badge-size;
  518. height: $badge-size;
  519. }
  520. .uni-badge--complex {
  521. /* #ifdef APP-NVUE */
  522. left: 50px;
  523. /* #endif */
  524. /* #ifndef APP-NVUE */
  525. width: auto;
  526. /* #endif */
  527. height: $badge-size;
  528. padding: 0 $badge-space;
  529. }
  530. .uni-badge--dot {
  531. /* #ifdef APP-NVUE */
  532. left: 60px;
  533. top: 6px;
  534. /* #endif */
  535. /* #ifndef APP-NVUE */
  536. left: calc(#{$avatar-width} + 15px - #{$dot-width}/ 2 + 1px + #{$badge-left});
  537. /* #endif */
  538. width: $dot-width;
  539. height: $dot-height;
  540. padding: 0;
  541. }
  542. .uni-list-chat--right {
  543. /* #ifdef APP-NVUE */
  544. left: 0;
  545. /* #endif */
  546. }
  547. </style>