uni-list-chat.vue 13 KB

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