| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- <template>
- <view class=" page chat-box">
- <view class="header">
- <text class="title">{{title}}</text>
- <uni-icons class="btn-side" type="more-filled" size="30"></uni-icons>
- </view>
- <view class="chat-msg" @click="switchChatTabBox('none')">
- <scroll-view class="scroll-box" scroll-y="true" :scroll-into-view="'chat-item-'+scrollMsgIdx">
- <view v-for="(msgInfo,idx) in chat.messages" :key="idx">
- <chat-message-item :headImage="headImage(msgInfo)" :showName="showName(msgInfo)"
- :id="'chat-item-'+idx" :msgInfo="msgInfo">
- </chat-message-item>
- </view>
- </scroll-view>
- </view>
- <view class="send-bar">
- <view class="iconfont icon-voice-circle"></view>
- <view class="send-text">
- <textarea class="send-text-area" v-model="sendText" auto-height :show-confirm-bar="false" :focus="sendTextFocus" @blur="onSendTextBlur()"
- @focus="onSendTextFoucs()" cursor-spacing="20" @keydown.enter="sendTextMessage()" @click="switchChatTabBox('none')"></textarea>
- </view>
- <view class="iconfont icon-icon_emoji" @click="switchChatTabBox('emo')"></view>
- <view v-show="sendText==''" class="iconfont icon-add-circle" @click="switchChatTabBox('tools')"></view>
- <button v-show="sendText!=''" class="btn-send" type="primary" @click="sendTextMessage()"
- size="mini">发送</button>
- </view>
- <view class="chat-tab-bar" v-show="chatTabBox!='none'">
- <view v-if="chatTabBox == 'tools'" class="chat-tools">
- <view class="chat-tools-item">
- <image-upload :onBefore="onUploadImageBefore" :onSuccess="onUploadImageSuccess"
- :onError="onUploadImageFail">
- <view class="tool-icon iconfont icon-picture"></view>
- </image-upload>
- <view class="tool-name">相册</view>
- </view>
- <view class="chat-tools-item" v-for="(tool, idx) in tools" @click="onClickTool(tool)">
- <view class="tool-icon iconfont" :class="tool.icon"></view>
- <view class="tool-name">{{ tool.name }}</view>
- </view>
- </view>
- <scroll-view v-if="chatTabBox==='emo'" class="chat-emotion" scroll-y="true">
- <view class="emotion-item-list">
- <image class="emotion-item" :src="$emo.textToPath(emoText)" v-for="(emoText, i) in $emo.emoTextList"
- :key="i" @click="selectEmoji(emoText)" mode="aspectFit" lazy-load="true"></image>
- </view>
- </scroll-view>
- </view>
- </view>
- </template>
- <script>
- export default {
- data() {
- return {
- chat: {},
- friend: {},
- group: {},
- groupMembers: [],
- sendText: "",
- showVoice: false, // 是否显示语音录制弹窗
- scrollMsgIdx: 0, // 滚动条定位为到哪条消息
- chatTabBox: 'none',
- sendTextFocus: false,
- tools: [{
- name: "拍摄",
- icon: "icon-camera"
- },
- {
- name: "语音输入",
- icon: "icon-microphone"
- },
- {
- name: "文件",
- icon: "icon-folder"
- },
- {
- name: "呼叫",
- icon: "icon-call"
- }
- ]
- }
- },
- methods: {
- headImage(msgInfo) {
- if (this.chat.type == 'GROUP') {
- let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
- return member ? member.headImage : "";
- } else {
- return msgInfo.selfSend ? this.mine.headImageThumb : this.chat.headImage
- }
- },
- showName(msgInfo) {
- if (this.chat.type == 'GROUP') {
- let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
- return member ? member.aliasName : "";
- } else {
- return msgInfo.selfSend ? this.mine.nickName : this.chat.showName
- }
- },
- sendTextMessage() {
- if (!this.sendText.trim()) {
- return uni.showToast({
- title: "不能发送空白信息",
- icon: "none"
- });
- }
- let msgInfo = {
- content: this.sendText,
- type: 0
- }
- // 填充对方id
- this.fillTargetId(msgInfo, this.chat.targetId);
- this.sendText = "";
- this.$http({
- url: this.messageAction,
- method: 'POST',
- data: msgInfo
- }).then((id) => {
- msgInfo.id = id;
- msgInfo.sendTime = new Date().getTime();
- msgInfo.sendId = this.$store.state.userStore.userInfo.id;
- msgInfo.selfSend = true;
- this.$store.commit("insertMessage", msgInfo);
- this.sendText = "";
- }).finally(() => {
- // 滚动到底部
- this.scrollToBottom();
- // 重新获得输入焦点
- this.sendTextFocus = true;
- });
- },
- fillTargetId(msgInfo, targetId) {
- if (this.chat.type == "GROUP") {
- msgInfo.groupId = targetId;
- } else {
- msgInfo.recvId = targetId;
- }
- },
- scrollToBottom() {
- let size = this.chat.messages.length;
- if(size>0){
- this.scrollToMsgIdx(size-1);
- }
- },
- scrollToMsgIdx(idx){
- // 踩坑:如果scrollMsgIdx值没变化,滚动条不会移动
- if(idx == this.scrollMsgIdx && idx>0){
- this.$nextTick(() => {
- // 先滚动到上一条
- this.scrollMsgIdx = idx-1;
- // 再滚动目标位置
- this.scrollToMsgIdx(idx);
- });
- return;
- }
- this.$nextTick(() => {
- this.scrollMsgIdx = idx;
- });
-
- },
- switchChatTabBox(v) {
- this.chatTabBox = v;
- this.scrollToBottom();
- },
- selectEmoji(emoText) {
- this.sendText += `#${emoText};`;
- },
- onSendTextBlur(){
- this.sendTextFocus=false;
- },
- onSendTextFoucs(){
- console.log("onSendTextFoucs")
- this.scrollToBottom();
- },
- onUploadImageBefore(file) {
- let data = {
- originUrl: file.path,
- thumbUrl: file.path
- }
- let msgInfo = {
- id: 0,
- fileId: file.uid,
- sendId: this.mine.id,
- content: JSON.stringify(data),
- sendTime: new Date().getTime(),
- selfSend: true,
- type: 1,
- loadStatus: "loading"
- }
- // 填充对方id
- this.fillTargetId(msgInfo, this.chat.targetId);
- // 插入消息
- this.$store.commit("insertMessage", msgInfo);
- // 借助file对象保存
- file.msgInfo = msgInfo;
- // 滚到最低部
- this.scrollToBottom();
- return true;
- },
- onUploadImageSuccess(file, res) {
- let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
- msgInfo.content = JSON.stringify(res.data);
- this.$http({
- url: this.messageAction,
- method: 'POST',
- data: msgInfo
- }).then((id) => {
- msgInfo.loadStatus = 'ok';
- msgInfo.id = id;
- this.$store.commit("insertMessage", msgInfo);
- })
- },
- onUploadImageFail(file, err) {
- let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
- msgInfo.loadStatus = 'fail';
- this.$store.commit("insertMessage", msgInfo);
- },
- onClickTool(tool) {
- switch (tool.name) {
- case "相册":
- break;
- }
-
- },
- loadGroup(groupId) {
- this.$http({
- url: `/group/find/${groupId}`,
- method: 'GET'
- }).then((group) => {
- this.group = group;
- this.$store.commit("updateChatFromGroup", group);
- this.$store.commit("updateGroup", group);
-
- });
-
- this.$http({
- url: `/group/members/${groupId}`,
- method: 'get'
- }).then((groupMembers) => {
- this.groupMembers = groupMembers;
- });
- },
- loadFriend(friendId) {
- // 获取对方最新信息
- this.$http({
- url: `/user/find/${friendId}`,
- method: 'GET'
- }).then((friend) => {
- this.friend = friend;
- this.$store.commit("updateChatFromFriend", friend);
- this.$store.commit("updateFriend", friend);
- })
- },
- },
- computed: {
- mine() {
- return this.$store.state.userStore.userInfo;
- },
- title() {
- if (!this.chat) {
- return "";
- }
- let title = this.chat.showName;
- if (this.chat.type == "GROUP") {
- let size = this.groupMembers.filter(m => !m.quit).length;
- title += `(${size})`;
- }
- return title;
- },
- messageAction() {
- return `/message/${this.chat.type.toLowerCase()}/send`;
- }
- },
- onLoad(options) {
- // 聊天数据
- this.chat = this.$store.state.chatStore.chats[options.chatIdx];
- // 激活当前会话
- this.$store.commit("activeChat", options.chatIdx);
- // 页面滚到底部
- this.scrollToBottom();
- // 加载好友或群聊信息
- if (this.chat.type == "GROUP") {
- this.loadGroup(this.chat.targetId);
- } else {
- this.loadFriend(this.chat.targetId);
- }
- },
- onUnload() {
- console.log("onShow")
- this.$store.commit("activeChat", -1);
- }
- }
- </script>
- <style lang="scss" scoped>
- .chat-box {
- position: relative;
- border: #dddddd solid 1px;
- display: flex;
- flex-direction: column;
- .header {
- display: flex;
- justify-content: center;
- align-items: center;
- height: 60rpx;
- padding: 5px;
- background-color: white;
- line-height: 50px;
- font-size: 40rpx;
- font-weight: 600;
- border: #dddddd solid 1px;
- .btn-side {
- position: absolute;
- right: 30rpx;
- line-height: 60rpx;
- font-size: 28rpx;
- cursor: pointer;
- }
- }
- .chat-msg {
- flex: 1;
- padding: 0;
- border: #dddddd solid 1px;
- overflow: hidden;
- position: relative;
- background-color: white;
- .scroll-box {
- height: 100%;
- }
- }
- .send-bar {
- display: flex;
- align-items: center;
- padding: 10rpx;
- margin-bottom: 10rpx;
- border: #dddddd solid 1px;
- background-color: white;
- .iconfont {
- font-size: 70rpx;
- margin: 3rpx;
- }
- .send-text {
- flex: 1;
- background-color: #f8f8f8 !important;
- overflow: auto;
- padding: 20rpx;
- background-color: #fff;
- border-radius: 20rpx;
- max-height: 300rpx;
- min-height: 85rpx;
- font-size: 30rpx;
- box-sizing: border-box;
-
- .send-text-area {
- width: 100%;
- }
- }
- .btn-send {
- margin: 5rpx;
- }
- }
- .chat-tab-bar {
- height: 500rpx;
- padding: 20rpx;
- background-color: whitesmoke;
- .chat-tools {
- display: flex;
- flex-wrap: wrap;
- justify-content: space-between;
- .chat-tools-item {
- width: 140rpx;
- padding: 15rpx;
- display: flex;
- flex-direction: column;
- align-items: center;
- .tool-icon {
- padding: 15rpx;
- font-size: 80rpx;
- background-color: white;
- border-radius: 20%;
- }
- .tool-name {
- height: 60rpx;
- line-height: 60rpx;
- font-size: 25rpx;
- }
- }
- }
- .chat-emotion {
- height: 100%;
- .emotion-item-list {
- display: flex;
- flex-wrap: wrap;
- .emotion-item {
- width: 60rpx;
- height: 60rpx;
- text-align: center;
- cursor: pointer;
- padding: 15rpx;
- }
- }
- }
- }
- }
- </style>
|