uni-list-chat.vue 12 KB

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