chat-box.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <template>
  2. <view class=" page chat-box">
  3. <view class="header">
  4. <text class="title">{{title}}</text>
  5. <uni-icons class="btn-side" type="more-filled" size="30"></uni-icons>
  6. </view>
  7. <view class="chat-msg" @click="switchChatTabBox('none')">
  8. <scroll-view class="scroll-box" scroll-y="true" :scroll-into-view="'chat-item-'+scrollMsgIdx">
  9. <view v-for="(msgInfo,idx) in chat.messages" :key="idx">
  10. <chat-message-item :headImage="headImage(msgInfo)" :showName="showName(msgInfo)"
  11. :id="'chat-item-'+idx" :msgInfo="msgInfo">
  12. </chat-message-item>
  13. </view>
  14. </scroll-view>
  15. </view>
  16. <view class="send-bar">
  17. <view class="iconfont icon-voice-circle"></view>
  18. <view class="send-text">
  19. <textarea class="send-text-area" v-model="sendText" auto-height :show-confirm-bar="false" :focus="sendTextFocus" @blur="onSendTextBlur()"
  20. @focus="onSendTextFoucs()" cursor-spacing="20" @keydown.enter="sendTextMessage()" @click="switchChatTabBox('none')"></textarea>
  21. </view>
  22. <view class="iconfont icon-icon_emoji" @click="switchChatTabBox('emo')"></view>
  23. <view v-show="sendText==''" class="iconfont icon-add-circle" @click="switchChatTabBox('tools')"></view>
  24. <button v-show="sendText!=''" class="btn-send" type="primary" @click="sendTextMessage()"
  25. size="mini">发送</button>
  26. </view>
  27. <view class="chat-tab-bar" v-show="chatTabBox!='none'">
  28. <view v-if="chatTabBox == 'tools'" class="chat-tools">
  29. <view class="chat-tools-item">
  30. <image-upload :onBefore="onUploadImageBefore" :onSuccess="onUploadImageSuccess"
  31. :onError="onUploadImageFail">
  32. <view class="tool-icon iconfont icon-picture"></view>
  33. </image-upload>
  34. <view class="tool-name">相册</view>
  35. </view>
  36. <view class="chat-tools-item" v-for="(tool, idx) in tools" @click="onClickTool(tool)">
  37. <view class="tool-icon iconfont" :class="tool.icon"></view>
  38. <view class="tool-name">{{ tool.name }}</view>
  39. </view>
  40. </view>
  41. <scroll-view v-if="chatTabBox==='emo'" class="chat-emotion" scroll-y="true">
  42. <view class="emotion-item-list">
  43. <image class="emotion-item" :src="$emo.textToPath(emoText)" v-for="(emoText, i) in $emo.emoTextList"
  44. :key="i" @click="selectEmoji(emoText)" mode="aspectFit" lazy-load="true"></image>
  45. </view>
  46. </scroll-view>
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. export default {
  52. data() {
  53. return {
  54. chat: {},
  55. friend: {},
  56. group: {},
  57. groupMembers: [],
  58. sendText: "",
  59. showVoice: false, // 是否显示语音录制弹窗
  60. scrollMsgIdx: 0, // 滚动条定位为到哪条消息
  61. chatTabBox: 'none',
  62. sendTextFocus: false,
  63. tools: [{
  64. name: "拍摄",
  65. icon: "icon-camera"
  66. },
  67. {
  68. name: "语音输入",
  69. icon: "icon-microphone"
  70. },
  71. {
  72. name: "文件",
  73. icon: "icon-folder"
  74. },
  75. {
  76. name: "呼叫",
  77. icon: "icon-call"
  78. }
  79. ]
  80. }
  81. },
  82. methods: {
  83. headImage(msgInfo) {
  84. if (this.chat.type == 'GROUP') {
  85. let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
  86. return member ? member.headImage : "";
  87. } else {
  88. return msgInfo.selfSend ? this.mine.headImageThumb : this.chat.headImage
  89. }
  90. },
  91. showName(msgInfo) {
  92. if (this.chat.type == 'GROUP') {
  93. let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
  94. return member ? member.aliasName : "";
  95. } else {
  96. return msgInfo.selfSend ? this.mine.nickName : this.chat.showName
  97. }
  98. },
  99. sendTextMessage() {
  100. if (!this.sendText.trim()) {
  101. return uni.showToast({
  102. title: "不能发送空白信息",
  103. icon: "none"
  104. });
  105. }
  106. let msgInfo = {
  107. content: this.sendText,
  108. type: 0
  109. }
  110. // 填充对方id
  111. this.fillTargetId(msgInfo, this.chat.targetId);
  112. this.sendText = "";
  113. this.$http({
  114. url: this.messageAction,
  115. method: 'POST',
  116. data: msgInfo
  117. }).then((id) => {
  118. msgInfo.id = id;
  119. msgInfo.sendTime = new Date().getTime();
  120. msgInfo.sendId = this.$store.state.userStore.userInfo.id;
  121. msgInfo.selfSend = true;
  122. this.$store.commit("insertMessage", msgInfo);
  123. this.sendText = "";
  124. }).finally(() => {
  125. // 滚动到底部
  126. this.scrollToBottom();
  127. // 重新获得输入焦点
  128. this.sendTextFocus = true;
  129. });
  130. },
  131. fillTargetId(msgInfo, targetId) {
  132. if (this.chat.type == "GROUP") {
  133. msgInfo.groupId = targetId;
  134. } else {
  135. msgInfo.recvId = targetId;
  136. }
  137. },
  138. scrollToBottom() {
  139. let size = this.chat.messages.length;
  140. if(size>0){
  141. this.scrollToMsgIdx(size-1);
  142. }
  143. },
  144. scrollToMsgIdx(idx){
  145. // 踩坑:如果scrollMsgIdx值没变化,滚动条不会移动
  146. if(idx == this.scrollMsgIdx && idx>0){
  147. this.$nextTick(() => {
  148. // 先滚动到上一条
  149. this.scrollMsgIdx = idx-1;
  150. // 再滚动目标位置
  151. this.scrollToMsgIdx(idx);
  152. });
  153. return;
  154. }
  155. this.$nextTick(() => {
  156. this.scrollMsgIdx = idx;
  157. });
  158. },
  159. switchChatTabBox(v) {
  160. this.chatTabBox = v;
  161. this.scrollToBottom();
  162. },
  163. selectEmoji(emoText) {
  164. this.sendText += `#${emoText};`;
  165. },
  166. onSendTextBlur(){
  167. this.sendTextFocus=false;
  168. },
  169. onSendTextFoucs(){
  170. console.log("onSendTextFoucs")
  171. this.scrollToBottom();
  172. },
  173. onUploadImageBefore(file) {
  174. let data = {
  175. originUrl: file.path,
  176. thumbUrl: file.path
  177. }
  178. let msgInfo = {
  179. id: 0,
  180. fileId: file.uid,
  181. sendId: this.mine.id,
  182. content: JSON.stringify(data),
  183. sendTime: new Date().getTime(),
  184. selfSend: true,
  185. type: 1,
  186. loadStatus: "loading"
  187. }
  188. // 填充对方id
  189. this.fillTargetId(msgInfo, this.chat.targetId);
  190. // 插入消息
  191. this.$store.commit("insertMessage", msgInfo);
  192. // 借助file对象保存
  193. file.msgInfo = msgInfo;
  194. // 滚到最低部
  195. this.scrollToBottom();
  196. return true;
  197. },
  198. onUploadImageSuccess(file, res) {
  199. let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
  200. msgInfo.content = JSON.stringify(res.data);
  201. this.$http({
  202. url: this.messageAction,
  203. method: 'POST',
  204. data: msgInfo
  205. }).then((id) => {
  206. msgInfo.loadStatus = 'ok';
  207. msgInfo.id = id;
  208. this.$store.commit("insertMessage", msgInfo);
  209. })
  210. },
  211. onUploadImageFail(file, err) {
  212. let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
  213. msgInfo.loadStatus = 'fail';
  214. this.$store.commit("insertMessage", msgInfo);
  215. },
  216. onClickTool(tool) {
  217. switch (tool.name) {
  218. case "相册":
  219. break;
  220. }
  221. },
  222. loadGroup(groupId) {
  223. this.$http({
  224. url: `/group/find/${groupId}`,
  225. method: 'GET'
  226. }).then((group) => {
  227. this.group = group;
  228. this.$store.commit("updateChatFromGroup", group);
  229. this.$store.commit("updateGroup", group);
  230. });
  231. this.$http({
  232. url: `/group/members/${groupId}`,
  233. method: 'get'
  234. }).then((groupMembers) => {
  235. this.groupMembers = groupMembers;
  236. });
  237. },
  238. loadFriend(friendId) {
  239. // 获取对方最新信息
  240. this.$http({
  241. url: `/user/find/${friendId}`,
  242. method: 'GET'
  243. }).then((friend) => {
  244. this.friend = friend;
  245. this.$store.commit("updateChatFromFriend", friend);
  246. this.$store.commit("updateFriend", friend);
  247. })
  248. },
  249. },
  250. computed: {
  251. mine() {
  252. return this.$store.state.userStore.userInfo;
  253. },
  254. title() {
  255. if (!this.chat) {
  256. return "";
  257. }
  258. let title = this.chat.showName;
  259. if (this.chat.type == "GROUP") {
  260. let size = this.groupMembers.filter(m => !m.quit).length;
  261. title += `(${size})`;
  262. }
  263. return title;
  264. },
  265. messageAction() {
  266. return `/message/${this.chat.type.toLowerCase()}/send`;
  267. }
  268. },
  269. onLoad(options) {
  270. // 聊天数据
  271. this.chat = this.$store.state.chatStore.chats[options.chatIdx];
  272. // 激活当前会话
  273. this.$store.commit("activeChat", options.chatIdx);
  274. // 页面滚到底部
  275. this.scrollToBottom();
  276. // 加载好友或群聊信息
  277. if (this.chat.type == "GROUP") {
  278. this.loadGroup(this.chat.targetId);
  279. } else {
  280. this.loadFriend(this.chat.targetId);
  281. }
  282. },
  283. onUnload() {
  284. console.log("onShow")
  285. this.$store.commit("activeChat", -1);
  286. }
  287. }
  288. </script>
  289. <style lang="scss" scoped>
  290. .chat-box {
  291. position: relative;
  292. border: #dddddd solid 1px;
  293. display: flex;
  294. flex-direction: column;
  295. .header {
  296. display: flex;
  297. justify-content: center;
  298. align-items: center;
  299. height: 60rpx;
  300. padding: 5px;
  301. background-color: white;
  302. line-height: 50px;
  303. font-size: 40rpx;
  304. font-weight: 600;
  305. border: #dddddd solid 1px;
  306. .btn-side {
  307. position: absolute;
  308. right: 30rpx;
  309. line-height: 60rpx;
  310. font-size: 28rpx;
  311. cursor: pointer;
  312. }
  313. }
  314. .chat-msg {
  315. flex: 1;
  316. padding: 0;
  317. border: #dddddd solid 1px;
  318. overflow: hidden;
  319. position: relative;
  320. background-color: white;
  321. .scroll-box {
  322. height: 100%;
  323. }
  324. }
  325. .send-bar {
  326. display: flex;
  327. align-items: center;
  328. padding: 10rpx;
  329. margin-bottom: 10rpx;
  330. border: #dddddd solid 1px;
  331. background-color: white;
  332. .iconfont {
  333. font-size: 70rpx;
  334. margin: 3rpx;
  335. }
  336. .send-text {
  337. flex: 1;
  338. background-color: #f8f8f8 !important;
  339. overflow: auto;
  340. padding: 20rpx;
  341. background-color: #fff;
  342. border-radius: 20rpx;
  343. max-height: 300rpx;
  344. min-height: 85rpx;
  345. font-size: 30rpx;
  346. box-sizing: border-box;
  347. .send-text-area {
  348. width: 100%;
  349. }
  350. }
  351. .btn-send {
  352. margin: 5rpx;
  353. }
  354. }
  355. .chat-tab-bar {
  356. height: 500rpx;
  357. padding: 20rpx;
  358. background-color: whitesmoke;
  359. .chat-tools {
  360. display: flex;
  361. flex-wrap: wrap;
  362. justify-content: space-between;
  363. .chat-tools-item {
  364. width: 140rpx;
  365. padding: 15rpx;
  366. display: flex;
  367. flex-direction: column;
  368. align-items: center;
  369. .tool-icon {
  370. padding: 15rpx;
  371. font-size: 80rpx;
  372. background-color: white;
  373. border-radius: 20%;
  374. }
  375. .tool-name {
  376. height: 60rpx;
  377. line-height: 60rpx;
  378. font-size: 25rpx;
  379. }
  380. }
  381. }
  382. .chat-emotion {
  383. height: 100%;
  384. .emotion-item-list {
  385. display: flex;
  386. flex-wrap: wrap;
  387. .emotion-item {
  388. width: 60rpx;
  389. height: 60rpx;
  390. text-align: center;
  391. cursor: pointer;
  392. padding: 15rpx;
  393. }
  394. }
  395. }
  396. }
  397. }
  398. </style>