chat.nvue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <template>
  2. <view>
  3. <text class="example-info">此示例展示了聊天列表的使用场景</text>
  4. <uni-section title="圆头像且不显示分割线" type="line"></uni-section>
  5. <uni-list :border="false">
  6. <uni-list-chat v-for="item in listData" :avatar-circle="true" :key="item.id" :title="item.author_name" :avatar="item.cover" :note="item.title" :time="item.published_at" :clickable="false"></uni-list-chat>
  7. </uni-list>
  8. <uni-section title="带圆点" type="line"></uni-section>
  9. <uni-list>
  10. <uni-list-chat v-for="item in listData" :key="item.id" :title="item.author_name" :avatar="item.cover" :note="item.title" :time="item.published_at" :badge-text="item.text" :clickable="false" badge-positon="left" badge-text="dot"></uni-list-chat>
  11. </uni-list>
  12. <uni-section title="自定义右侧内容" type="line"></uni-section>
  13. <uni-list>
  14. <uni-list-chat v-for="item in listData" :key="item.id" :title="item.author_name" :avatar="item.cover" :note="item.title" badge-positon="left" :badge-text="item.text">
  15. <view class="chat-custom-right">
  16. <text class="chat-custom-text">刚刚</text>
  17. <uni-icons type="star-filled" color="#999" size="18"></uni-icons>
  18. </view>
  19. </uni-list-chat>
  20. </uni-list>
  21. <uni-section title="带通知角标的单头像聊天列表" type="line"></uni-section>
  22. <uni-list>
  23. <uni-list-chat v-for="item in listData" :key="item.id" :title="item.author_name" :avatar="item.cover" :note="item.title" :time="item.published_at" :clickable="true" :badge-text="item.text" @click="onClick"></uni-list-chat>
  24. </uni-list>
  25. <uni-section title="带通知角标的多头像聊天列表" type="line"></uni-section>
  26. <uni-list>
  27. <uni-list-chat v-for="(item,index) in listData" :key="item.id" :title="item.author_name" :avatar="item.cover" :note="item.title" :time="item.published_at" :clickable="true" :avatarList="avatar(index+1)" :badge-text="item.text" @click="onClick"></uni-list-chat>
  28. </uni-list>
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. components: {},
  34. data() {
  35. return {
  36. UNITS: {
  37. '年': 31557600000,
  38. '月': 2629800000,
  39. '天': 86400000,
  40. '小时': 3600000,
  41. '分钟': 60000,
  42. '秒': 1000
  43. },
  44. listData: [],
  45. avatarList: [{
  46. url: '/static/logo.png'
  47. }, {
  48. url: '/static/logo.png'
  49. }, {
  50. url: '/static/logo.png'
  51. }]
  52. }
  53. },
  54. onLoad() {
  55. this.getList()
  56. },
  57. methods: {
  58. onClick() {
  59. uni.showToast({
  60. title: '列表被点击'
  61. })
  62. },
  63. avatar(count) {
  64. let arr = []
  65. this.avatarList.forEach((item, index) => {
  66. if (index < count) {
  67. arr.push(item)
  68. }
  69. })
  70. return arr
  71. },
  72. getList() {
  73. var data = {
  74. column: 'id,post_id,title,author_name,cover,published_at' //需要的字段名
  75. };
  76. uni.request({
  77. url: 'https://unidemo.dcloud.net.cn/api/news',
  78. data: data,
  79. success: data => {
  80. if (data.statusCode == 200) {
  81. let list = this.setTime(data.data);
  82. list = this.reload ? list : this.listData.concat(list);
  83. list.map(item => {
  84. item.text = Math.floor(Math.random() * (1 - 20) + 20)
  85. return item
  86. })
  87. this.listData = this.getRandomArrayElements(list, 3)
  88. }
  89. },
  90. fail: (data, code) => {
  91. console.log('fail' + JSON.stringify(data));
  92. }
  93. });
  94. },
  95. getRandomArrayElements(arr, count) {
  96. var shuffled = arr.slice(0),
  97. i = arr.length,
  98. min = i - count,
  99. temp, index;
  100. while (i-- > min) {
  101. index = Math.floor((i + 1) * Math.random());
  102. temp = shuffled[index];
  103. shuffled[index] = shuffled[i];
  104. shuffled[i] = temp;
  105. }
  106. return shuffled.slice(min);
  107. },
  108. setTime(items) {
  109. var newItems = [];
  110. items.forEach(e => {
  111. newItems.push({
  112. author_name: e.author_name,
  113. cover: e.cover,
  114. id: e.id,
  115. post_id: e.post_id,
  116. published_at: this.format(e.published_at),
  117. title: e.title
  118. });
  119. });
  120. return newItems;
  121. },
  122. format(dateStr) {
  123. var date = this.parse(dateStr)
  124. var diff = Date.now() - date.getTime();
  125. if (diff < this.UNITS['天']) {
  126. return this.humanize(diff);
  127. }
  128. var _format = function(number) {
  129. return (number < 10 ? ('0' + number) : number);
  130. };
  131. return date.getFullYear() + '-' + _format(date.getMonth() + 1) + '-' + _format(date.getDate()) + ' ' +
  132. _format(date.getHours()) + ':' + _format(date.getMinutes());
  133. },
  134. parse(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
  135. var a = str.split(/[^0-9]/);
  136. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  137. },
  138. }
  139. }
  140. </script>
  141. <style>
  142. @charset "UTF-8";
  143. /* 头条小程序组件内不能引入字体 */
  144. /* #ifdef MP-TOUTIAO */
  145. @font-face {
  146. font-family: uniicons;
  147. font-weight: normal;
  148. font-style: normal;
  149. src: url("~@/static/uni.ttf") format("truetype");
  150. }
  151. /* #endif */
  152. /* #ifndef APP-NVUE */
  153. page {
  154. display: flex;
  155. flex-direction: column;
  156. box-sizing: border-box;
  157. background-color: #efeff4;
  158. min-height: 100%;
  159. height: auto;
  160. }
  161. view {
  162. font-size: 14px;
  163. line-height: inherit;
  164. }
  165. .example {
  166. padding: 0 15px 15px;
  167. }
  168. .example-info {
  169. padding: 15px;
  170. color: #3b4144;
  171. background: #ffffff;
  172. }
  173. .example-body {
  174. /* #ifndef APP-NVUE */
  175. display: flex;
  176. /* #endif */
  177. flex-direction: row;
  178. flex-wrap: wrap;
  179. justify-content: center;
  180. padding: 0;
  181. font-size: 14px;
  182. background-color: #ffffff;
  183. }
  184. /* #endif */
  185. .example {
  186. padding: 0 15px;
  187. }
  188. .example-info {
  189. /* #ifndef APP-NVUE */
  190. display: block;
  191. /* #endif */
  192. padding: 15px;
  193. color: #3b4144;
  194. background-color: #ffffff;
  195. font-size: 14px;
  196. line-height: 20px;
  197. }
  198. .example-info-text {
  199. font-size: 14px;
  200. line-height: 20px;
  201. color: #3b4144;
  202. }
  203. .example-body {
  204. flex-direction: column;
  205. padding: 15px;
  206. background-color: #ffffff;
  207. }
  208. .word-btn-white {
  209. font-size: 18px;
  210. color: #FFFFFF;
  211. }
  212. .word-btn {
  213. /* #ifndef APP-NVUE */
  214. display: flex;
  215. /* #endif */
  216. flex-direction: row;
  217. align-items: center;
  218. justify-content: center;
  219. border-radius: 6px;
  220. height: 48px;
  221. margin: 15px;
  222. background-color: #007AFF;
  223. }
  224. .word-btn--hover {
  225. background-color: #4ca2ff;
  226. }
  227. .chat-custom-right {
  228. flex: 1;
  229. /* #ifndef APP-NVUE */
  230. display: flex;
  231. /* #endif */
  232. flex-direction: column;
  233. justify-content: space-between;
  234. align-items: flex-end;
  235. }
  236. .chat-custom-text {
  237. font-size: 12px;
  238. color: #999;
  239. }
  240. </style>