index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <template>
  2. <view>
  3. <view class="container" :style="pageHeight">
  4. <view class="box-1">
  5. <scroll-view scroll-y refresher-background="transparent" style="height: 100%;"
  6. @refresherrefresh="refresherrefresh" :refresher-enabled="true" :scroll-with-animation="false"
  7. :refresher-triggered="scrollView.refresherTriggered" :scroll-into-view="scrollView.intoView">
  8. <view class="talk-list">
  9. <view v-for="(item, index) in talkList" :key="item.msgSendTime" :id="'msg-' + item.msgSendTime">
  10. <view class="item flex-col" :class="item.senderId == this.userId ? 'push' : 'pull'">
  11. <!-- <image :src="item.pic" mode="aspectFill" class="pic"></image> -->
  12. <img v-if="item.senderId !== this.userId" :src="orderInfo.conversationAvatar || '/static/serverImg/mine/user.png' " alt=""
  13. class="pic" mode="aspectFill" />
  14. <img src="/static/serverImg/mine/user.png" alt="" class="pic" mode="aspectFill"
  15. v-else />
  16. <view class="content">
  17. <template v-if="item.msgType === '2'">
  18. <image :src="item.msgContent" mode="widthFix" style="width: 400rpx;"></image>
  19. </template>
  20. <template v-else>
  21. {{ item.msgContent }}
  22. </template>
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. </scroll-view>
  28. </view>
  29. <view class="box-2">
  30. <view class="flex-col">
  31. <view style="margin-right: 20rpx;">
  32. <uni-icons type="image" size="24" color="#42b983" @tap="handleImageClick"></uni-icons>
  33. </view>
  34. <view class="flex-grow">
  35. <input type="text" class="content" v-model="content" placeholder="请输入聊天内容"
  36. placeholder-style="color:#DDD;" :cursor-spacing="6">
  37. </view>
  38. <button class="send" @tap="handleSendClick">发送</button>
  39. </view>
  40. </view>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. import { wxUploadFile, wxMakePhoneCall } from '@/utils/wxRequest.js'
  46. import { sendMsg, getOrderConversationInfo, getListConversationInfo, setRead,getHistoryMsg } from '@/api/conversation.js';
  47. import { onMounted } from "vue";
  48. import store from "@/store"
  49. export default {
  50. data() {
  51. return {
  52. // 滚动容器
  53. scrollView: {
  54. refresherTriggered: false,
  55. intoView: "",
  56. safeAreaHeight: 0
  57. },
  58. // 聊天列表数据
  59. talkList: [],
  60. // 请求参数
  61. ajax: {
  62. rows: 20, //每页数量
  63. page: 1, //页码
  64. flag: true, // 请求开关
  65. },
  66. // 发送内容
  67. content: '',
  68. orderId: '',
  69. orderInfo: {},//订单信息
  70. userType: uni.getStorageSync('userType'), //读取本地存储
  71. system: '2',
  72. userId: uni.getStorageSync('userId'), //读取本地存储
  73. conversationRecordId: null,//会话id
  74. storeUser: store.state.user,
  75. store:store,
  76. }
  77. },
  78. computed: {
  79. // 页面高度
  80. pageHeight() {
  81. const safeAreaHeight = this.scrollView.safeAreaHeight;
  82. console.log('safeAreaHeight', safeAreaHeight);
  83. if (safeAreaHeight > 0) {
  84. return `height: calc(${safeAreaHeight}px - var(--window-top));`
  85. }
  86. return "";
  87. }
  88. },
  89. onLoad(options) {
  90. console.log("TCL: onLoad -> options", options)
  91. if (options.orderId) {
  92. this.orderId = options.orderId;
  93. }
  94. if (options.conversationRecordId) {
  95. this.conversationRecordId = options.conversationRecordId;
  96. }
  97. //1用户2志愿者3后台
  98. this.system = this.userType === 1 ? '1' : '2';
  99. // #ifdef H5
  100. this.scrollView.safeAreaHeight = uni.getSystemInfoSync().safeArea.height;
  101. // #endif
  102. this.getMessage();
  103. },
  104. onMounted() {
  105. // uni.onSocketMessage(function (parmas) {
  106. // const res = JSON.parse(parmas.data);
  107. // console.log("TCL: get -> res", res)
  108. // if (res.type === 'msgNew') {
  109. // this.talkList.unshift({ ...res.data, code: 200 });
  110. // }
  111. // console.log(' onMounted-收到服务器消息:', res, this.talkList);
  112. // });
  113. },
  114. methods: {
  115. // 下拉刷新
  116. refresherrefresh(e) {
  117. console.log("TCL: 下拉刷新 -> e", e)
  118. this.scrollView.refresherTriggered = true;
  119. // this.getMessage();
  120. const conversationMsgRecordId = this.talkList[this.talkList.length-1].conversationMsgRecordId;
  121. console.log("TCL: refresherrefresh -> conversationMsgRecordId", conversationMsgRecordId,this.talkList[this.talkList.length-1])
  122. const msgSendTime = this.talkList[this.talkList.length-1].msgSendTime
  123. getHistoryMsg({conversationMsgRecordId,conversationRecordId:this.conversationRecordId}).then(res => {
  124. console.log("TCL: refresherrefresh -> res", res)
  125. if(res.code === 200){
  126. if(res.data.length === 0) {
  127. uni.showToast({
  128. title: '没有更多消息了',
  129. icon: 'none'
  130. })
  131. this.scrollView.refresherTriggered = false;
  132. return
  133. }
  134. this.talkList = [...this.talkList,...res.data];
  135. this.scrollView.refresherTriggered = false;
  136. this.scrollView.intoView = `msg-${msgSendTime}`;
  137. }
  138. })
  139. },
  140. listsMessage(){
  141. console.log('-----listsMessage---->', this.storeUser,this.storeUser.wsManager);
  142. this.storeUser.wsManager.onMessage(res => {
  143. console.log('--------->', res)
  144. // 处理消息逻辑
  145. if (res.type === 'msgNew') {
  146. this.talkList.unshift({ ...res.data, code: 200 });
  147. }
  148. });
  149. },
  150. // 获取历史消息
  151. getMessage() {
  152. if (!this.ajax.flag) {
  153. return;
  154. }
  155. console.log('--------->', this.storeUser);
  156. if (!this.storeUser.wsManager) {
  157. store.dispatch('GetInfo').then((res) => {
  158. this.listsMessage();
  159. });
  160. }else{
  161. this.listsMessage();
  162. }
  163. // 此处用到 ES7 的 async/await 知识,为使代码更加优美。不懂的请自行学习。
  164. let get = async () => {
  165. this.ajax.flag = false;
  166. const parmas = {
  167. system: this.system
  168. }
  169. const apiFun = this.orderId ? getOrderConversationInfo : getListConversationInfo
  170. if (this.orderId) {
  171. parmas.mainOrderId = this.orderId
  172. }
  173. if (this.conversationRecordId) {
  174. parmas.conversationRecordId = this.conversationRecordId
  175. }
  176. await setRead(parmas); //设置已读
  177. const res = await apiFun(parmas);
  178. console.log("TCL: getOrder -> res", res)
  179. if (res.code === 200) {
  180. const data = res.data.vos;
  181. // 获取待滚动元素选择器,解决插入数据后,滚动条定位时使用。取当前消息数据的第一条信息元素
  182. const selector = `msg-${data?.[0]?.msgSendTime}`;
  183. // uni.setNavigationBarTitle({
  184. // title: userType === 1? res.data. : // 根据业务逻辑调整
  185. // });
  186. // 将获取到的消息数据合并到消息数组中
  187. this.talkList = data;
  188. console.log("TCL: getOrder -> talkList", this.talkList)
  189. this.orderInfo = res.data;
  190. // 数据挂载后执行,不懂的请自行阅读 Vue.js 文档对 Vue.nextTick 函数说明。
  191. this.$nextTick(() => {
  192. // 设置当前滚动的位置
  193. this.scrollView.intoView = selector;
  194. if (data.length < this.ajax.rows) {
  195. // 当前消息数据条数小于请求要求条数时,则无更多消息,不再允许请求。
  196. // 可在此处编写无更多消息数据时的逻辑
  197. } else {
  198. this.ajax.flag = true;
  199. this.ajax.page++;
  200. }
  201. })
  202. }
  203. }
  204. get();
  205. },
  206. // 发送信息
  207. handleSendClick() {
  208. if (!this.content) {
  209. uni.showToast({
  210. title: '请输入有效的内容',
  211. icon: 'none'
  212. })
  213. return;
  214. }
  215. this.sendMessage(this.content, 'text');
  216. // 清空内容框中的内容
  217. this.content = '';
  218. },
  219. // 处理图片点击
  220. async handleImageClick() {
  221. const res = await wxUploadFile('img');
  222. this.sendMessage(res[0].url, 'image');
  223. console.log("TCL: handleImageClick -> res", res)
  224. },
  225. async sendMessage(content, contentType = 'text') {
  226. // 将当前发送信息 添加到消息列表。
  227. let data = {
  228. conversationRecordId: "3",
  229. msgContent: content,
  230. msgSendTime: new Date().getTime(),
  231. senderId: this.userId,
  232. msgType: contentType =='text'?'1':'2',//消息类型 1文字消息 2图片消息
  233. }
  234. const res = await sendMsg({
  235. ...this.orderInfo,
  236. msgContent: content,
  237. system: this.system,
  238. msgType: data.msgType
  239. });
  240. this.talkList.unshift({ ...data, code: res.code });
  241. this.scrollView.intoView = `msg-${data.msgSendTime}`;
  242. },
  243. },
  244. }
  245. </script>
  246. <style lang="scss">
  247. @import "../../lib/global.scss";
  248. page {
  249. background-color: #F3F3F3;
  250. font-size: 28rpx;
  251. }
  252. .container {
  253. height: calc(100vh - var(--window-top));
  254. display: flex;
  255. flex-direction: column;
  256. flex-wrap: nowrap;
  257. align-content: center;
  258. justify-content: space-between;
  259. align-items: stretch;
  260. }
  261. /* 加载数据提示 */
  262. .tips {
  263. position: fixed;
  264. left: 0;
  265. top: var(--window-top);
  266. width: 100%;
  267. z-index: 9;
  268. background-color: rgba(0, 0, 0, 0.15);
  269. height: 72rpx;
  270. line-height: 72rpx;
  271. transform: translateY(-80rpx);
  272. transition: transform 0.3s ease-in-out 0s;
  273. &.show {
  274. transform: translateY(0);
  275. }
  276. }
  277. .box-1 {
  278. width: 100%;
  279. height: 0;
  280. flex: 1 0 auto;
  281. box-sizing: content-box;
  282. }
  283. .box-2 {
  284. height: auto;
  285. z-index: 2;
  286. border-top: #e5e5e5 solid 1px;
  287. box-sizing: content-box;
  288. background-color: #F3F3F3;
  289. /* 兼容iPhoneX */
  290. padding-bottom: 0;
  291. padding-bottom: constant(safe-area-inset-bottom);
  292. padding-bottom: env(safe-area-inset-bottom);
  293. >view {
  294. padding: 0 20rpx;
  295. height: 100rpx;
  296. }
  297. .content {
  298. background-color: #fff;
  299. height: 64rpx;
  300. padding: 0 20rpx;
  301. border-radius: 32rpx;
  302. font-size: 28rpx;
  303. }
  304. .send {
  305. background-color: #42b983;
  306. color: #fff;
  307. height: 64rpx;
  308. margin-left: 20rpx;
  309. border-radius: 32rpx;
  310. padding: 0;
  311. width: 120rpx;
  312. line-height: 62rpx;
  313. &:active {
  314. background-color: #5fc496;
  315. }
  316. }
  317. }
  318. .talk-list {
  319. padding-bottom: 20rpx;
  320. display: flex;
  321. flex-direction: column-reverse;
  322. flex-wrap: nowrap;
  323. align-content: flex-start;
  324. justify-content: flex-end;
  325. align-items: stretch;
  326. // 添加弹性容器,让内容自动在顶部
  327. &::before {
  328. content: '.';
  329. display: inline;
  330. visibility: hidden;
  331. line-height: 0;
  332. font-size: 0;
  333. flex: 1 0 auto;
  334. height: 1px;
  335. }
  336. /* 消息项,基础类 */
  337. .item {
  338. padding: 20rpx 20rpx 0 20rpx;
  339. align-items: flex-start;
  340. align-content: flex-start;
  341. color: #333;
  342. .pic {
  343. width: 92rpx;
  344. height: 92rpx;
  345. border-radius: 50%;
  346. border: #fff solid 1px;
  347. }
  348. .content {
  349. padding: 20rpx;
  350. border-radius: 4px;
  351. max-width: 500rpx;
  352. word-break: break-all;
  353. line-height: 52rpx;
  354. position: relative;
  355. }
  356. /* 收到的消息 */
  357. &.pull {
  358. .content {
  359. margin-left: 32rpx;
  360. background-color: #fff;
  361. &::after {
  362. content: '';
  363. display: block;
  364. width: 0;
  365. height: 0;
  366. border-top: 16rpx solid transparent;
  367. border-bottom: 16rpx solid transparent;
  368. border-right: 20rpx solid #fff;
  369. position: absolute;
  370. top: 30rpx;
  371. left: -18rpx;
  372. }
  373. }
  374. }
  375. /* 发出的消息 */
  376. &.push {
  377. /* 主轴为水平方向,起点在右端。使不修改DOM结构,也能改变元素排列顺序 */
  378. flex-direction: row-reverse;
  379. .content {
  380. margin-right: 32rpx;
  381. background-color: #a0e959;
  382. &::after {
  383. content: '';
  384. display: block;
  385. width: 0;
  386. height: 0;
  387. border-top: 16rpx solid transparent;
  388. border-bottom: 16rpx solid transparent;
  389. border-left: 20rpx solid #a0e959;
  390. position: absolute;
  391. top: 30rpx;
  392. right: -18rpx;
  393. }
  394. }
  395. }
  396. }
  397. }
  398. </style>