chat.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template>
  2. <view class="container">
  3. <scroll-view
  4. class="chat-scroll"
  5. scroll-y
  6. :scroll-top="scrollTop"
  7. scroll-with-animation
  8. @scrolltoupper="loadHistory"
  9. >
  10. <view class="msg-list">
  11. <block v-for="(item, index) in msgList" :key="index">
  12. <!-- 时间戳 -->
  13. <view class="time-stamp" v-if="showTime(index)">{{ item.time }}</view>
  14. <view :class="['msg-row', item.isMe ? 'row-right' : 'row-left']">
  15. <image class="avatar" :src="item.avatar" mode="aspectFill" />
  16. <view class="content">
  17. <!-- 文本消息 -->
  18. <view v-if="item.type === 'text'" class="bubble">{{ item.content }}</view>
  19. <!-- 岗位卡片 -->
  20. <view v-else-if="item.type === 'job_card'" class="card job-card" @tap="toJobDetail(item.payload.id)">
  21. <view class="job-header">
  22. <text class="job-name">{{ item.payload.title }}</text>
  23. <text class="salary">{{ item.payload.salary }}</text>
  24. </view>
  25. <view class="tags">
  26. <text v-for="tag in item.payload.tags" :key="tag" class="tag">{{ tag }}</text>
  27. </view>
  28. <view class="company">
  29. <image src="/static/icons/company.png" class="c-logo" />
  30. <text>{{ item.payload.company }}</text>
  31. <text class="loc">{{ item.payload.location }}</text>
  32. </view>
  33. </view>
  34. <!-- 订单卡片 -->
  35. <view v-else-if="item.type === 'order_card'" class="card order-card">
  36. <view class="order-title">您有一笔待付款项 <text class="status">已失效</text></view>
  37. <view class="order-body">
  38. <view class="item"><text class="label">项目名称:</text>{{ item.payload.name }}</view>
  39. <view class="item"><text class="label">支付金额:</text><text class="price">¥{{ item.payload.price }}</text></view>
  40. </view>
  41. <button class="btn disabled" disabled>已失效</button>
  42. </view>
  43. </view>
  44. </view>
  45. </block>
  46. </view>
  47. </scroll-view>
  48. <!-- 底部输入栏 -->
  49. <view class="input-panel">
  50. <image src="/static/icons/voice.png" class="icon" />
  51. <input
  52. class="input"
  53. v-model="inputVal"
  54. placeholder="请输入内容..."
  55. confirm-type="send"
  56. @confirm="sendMsg"
  57. />
  58. <image src="/static/icons/emoji.png" class="icon" />
  59. <image src="/static/icons/plus.png" class="icon" />
  60. </view>
  61. </view>
  62. </template>
  63. <script>
  64. export default {
  65. data() {
  66. return {
  67. inputVal: '',
  68. scrollTop: 0,
  69. msgList: [
  70. {
  71. isMe: false,
  72. type: 'job_card',
  73. avatar: '/static/kf-avatar.png',
  74. time: '12-28 13:15:56',
  75. payload: {
  76. id: 'j001',
  77. title: '审计员',
  78. salary: '13K-23K',
  79. tags: ['实习', '五险一金', '985'],
  80. company: '华财仁合',
  81. location: '上海市·黄浦区'
  82. }
  83. },
  84. {
  85. isMe: false,
  86. type: 'text',
  87. avatar: '/static/kf-avatar.png',
  88. content: '您好!我是您的专属客服,请问有什么可以帮助您',
  89. time: '12-28 13:16:10'
  90. },
  91. {
  92. isMe: false,
  93. type: 'order_card',
  94. avatar: '/static/kf-avatar.png',
  95. payload: {
  96. name: '审计师一极入职定金',
  97. price: '29.9'
  98. },
  99. time: '12-28 13:17:00'
  100. }
  101. ]
  102. }
  103. },
  104. methods: {
  105. sendMsg() {
  106. if(!this.inputVal) return;
  107. this.msgList.push({
  108. isMe: true,
  109. type: 'text',
  110. content: this.inputVal,
  111. avatar: '/static/user-avatar.png',
  112. time: new Date().toLocaleTimeString()
  113. });
  114. this.inputVal = '';
  115. this.scrollToBottom();
  116. // 这里调用 WebSocket 接口发送消息
  117. },
  118. scrollToBottom() {
  119. this.$nextTick(() => {
  120. this.scrollTop += 1000;
  121. })
  122. },
  123. showTime(index) {
  124. return index === 0; // 简化逻辑
  125. }
  126. }
  127. }
  128. </script>
  129. <style lang="scss">
  130. .container {
  131. display: flex;
  132. flex-direction: column;
  133. height: 100vh;
  134. background-color: #f7f7f7;
  135. }
  136. .chat-scroll {
  137. flex: 1;
  138. .msg-list {
  139. padding: 20rpx;
  140. .time-stamp {
  141. text-align: center;
  142. font-size: 22rpx;
  143. color: #999;
  144. margin: 30rpx 0;
  145. background: #e9e9e9;
  146. display: inline-block;
  147. padding: 4rpx 16rpx;
  148. border-radius: 8rpx;
  149. left: 50%;
  150. transform: translateX(-50%);
  151. position: relative;
  152. }
  153. .msg-row {
  154. display: flex;
  155. margin-bottom: 40rpx;
  156. .avatar {
  157. width: 80rpx;
  158. height: 80rpx;
  159. border-radius: 50%;
  160. }
  161. .content {
  162. max-width: 500rpx;
  163. margin: 0 20rpx;
  164. .bubble {
  165. padding: 20rpx;
  166. border-radius: 12rpx;
  167. font-size: 28rpx;
  168. line-height: 1.4;
  169. background-color: #fff;
  170. position: relative;
  171. }
  172. }
  173. &.row-left {
  174. .bubble {
  175. background-color: #fff;
  176. &::after {
  177. content: ''; position: absolute; left: -10rpx; top: 20rpx;
  178. border: 10rpx solid transparent; border-right-color: #fff;
  179. }
  180. }
  181. }
  182. &.row-right {
  183. flex-direction: row-reverse;
  184. .bubble {
  185. background-color: #95ec69;
  186. &::after {
  187. content: ''; position: absolute; right: -10rpx; top: 20rpx;
  188. border: 10rpx solid transparent; border-left-color: #95ec69;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. /* 卡片样式 */
  196. .card {
  197. background: #fff;
  198. border-radius: 20rpx;
  199. padding: 30rpx;
  200. box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.05);
  201. margin-top: 10rpx;
  202. &.job-card {
  203. .job-header {
  204. display: flex; justify-content: space-between; margin-bottom: 20rpx;
  205. .job-name { font-size: 32rpx; font-weight: bold; }
  206. .salary { color: #007aff; font-weight: bold; }
  207. }
  208. .tags {
  209. margin-bottom: 20rpx;
  210. .tag { font-size: 22rpx; color: #666; background: #f0f0f0; padding: 2rpx 12rpx; margin-right: 10rpx; border-radius: 4rpx; }
  211. }
  212. .company {
  213. display: flex; align-items: center; font-size: 24rpx; color: #333;
  214. .c-logo { width: 32rpx; height: 32rpx; margin-right: 10rpx; }
  215. .loc { margin-left: auto; color: #999; }
  216. }
  217. }
  218. &.order-card {
  219. .order-title {
  220. font-size: 28rpx; font-weight: bold; margin-bottom: 20rpx;
  221. .status { float: right; font-weight: normal; color: #999; font-size: 24rpx; }
  222. }
  223. .order-body {
  224. color: #666; font-size: 26rpx;
  225. .item { margin-bottom: 10rpx; }
  226. .price { color: #ff4d4f; font-weight: bold; font-size: 32rpx; }
  227. }
  228. .btn {
  229. margin-top: 30rpx; height: 70rpx; line-height: 70rpx; border-radius: 35rpx;
  230. font-size: 28rpx; background: #ffbc00; color: #fff;
  231. &.disabled { background: #e0e0e0; color: #999; }
  232. }
  233. }
  234. }
  235. .input-panel {
  236. display: flex; align-items: center; padding: 20rpx 30rpx;
  237. background: #f9f9f9; border-top: 1px solid #eee;
  238. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  239. .input { flex: 1; height: 72rpx; background: #fff; border-radius: 8rpx; margin: 0 20rpx; padding: 0 20rpx; font-size: 28rpx; }
  240. .icon { width: 56rpx; height: 56rpx; }
  241. }
  242. </style>