Browse Source

uniapp 体验优化

master
xie.bx 3 years ago
parent
commit
50bb28e1b4
  1. 85
      im-uniapp/App.vue
  2. 5
      im-uniapp/common/request.js
  3. 2
      im-uniapp/main.js
  4. 43
      im-uniapp/pages/chat/chat-box.vue
  5. 4
      im-uniapp/pages/group/group-edit.vue
  6. 69
      im-uniapp/pages/group/group-info.vue
  7. 2
      im-uniapp/pages/login/login.vue
  8. 3
      im-uniapp/pages/mine/mine-edit.vue
  9. 3
      im-uniapp/pages/mine/mine-password.vue
  10. 2
      im-uniapp/pages/mine/mine.vue
  11. 6
      im-uniapp/store/friendStore.js
  12. 5
      im-uniapp/store/groupStore.js
  13. 8
      im-uniapp/store/userStore.js

85
im-uniapp/App.vue

@ -1,8 +1,11 @@
<script>
import store from './store'
import store from './store';
import http from './common/request';
import * as enums from './common/enums';
import * as wsApi from './common/wssocket';
export default {
data(){
data() {
return {
audioTip: null
}
@ -10,7 +13,6 @@
methods: {
init(loginInfo) {
//
console.log(this)
store.dispatch("load").then(() => {
// websocket
this.initWebSocket(loginInfo);
@ -18,14 +20,15 @@
console.log(e);
this.exit();
})
},
initWebSocket(loginInfo) {
let userId = this.$store.state.userStore.userInfo.id;
this.$wsApi.createWebSocket(process.env.WS_URL, loginInfo.accessToken);
this.$wsApi.onopen(() => {
let userId = store.state.userStore.userInfo.id;
wsApi.createWebSocket(process.env.WS_URL, loginInfo.accessToken);
wsApi.onopen(() => {
this.pullUnreadMessage();
});
this.$wsApi.onmessage((cmd, msgInfo) => {
wsApi.onmessage((cmd, msgInfo) => {
if (cmd == 2) {
// 线
uni.showModal({
@ -48,26 +51,26 @@
},
pullUnreadMessage() {
//
this.$http({
http({
url: "/message/private/pullUnreadMessage",
method: 'POST'
});
//
this.$http({
http({
url: "/message/group/pullUnreadMessage",
method: 'POST'
});
},
handlePrivateMessage(msg) {
let friendId = msg.selfSend ? msg.recvId : msg.sendId;
let friend = this.$store.state.friendStore.friends.find((f) => f.id == friendId);
let friendId = msg.selfSend ? msg.recvId : msg.sendId;
let friend = store.state.friendStore.friends.find((f) => f.id == friendId);
if (!friend) {
this.$http({
http({
url: `/friend/find/${msg.sendId}`,
method: 'GET'
}).then((friend) => {
this.insertPrivateMessage(friend, msg);
this.$store.commit("addFriend", friend);
store.commit("addFriend", friend);
})
} else {
//
@ -77,20 +80,8 @@
},
insertPrivateMessage(friend, msg) {
// webrtc
if (msg.type >= this.$enums.MESSAGE_TYPE.RTC_CALL &&
msg.type <= this.$enums.MESSAGE_TYPE.RTC_CANDIDATE) {
// //
// if (msg.type == this.$enums.MESSAGE_TYPE.RTC_CALL ||
// msg.type == this.$enums.MESSAGE_TYPE.RTC_CANCEL) {
// this.$store.commit("showVideoAcceptorBox", friend);
// this.$refs.videoAcceptor.handleMessage(msg)
// } else {
// this.$refs.videoAcceptor.close()
// this.$refs.privateVideo.handleMessage(msg)
// }
// return;
}
if (msg.type >= enums.MESSAGE_TYPE.RTC_CALL &&
msg.type <= enums.MESSAGE_TYPE.RTC_CANDIDATE) {}
let chatInfo = {
type: 'PRIVATE',
@ -99,22 +90,22 @@
headImage: friend.headImage
};
//
this.$store.commit("openChat", chatInfo);
store.commit("openChat", chatInfo);
//
this.$store.commit("insertMessage", msg);
store.commit("insertMessage", msg);
//
!msg.selfSend && this.playAudioTip();
},
handleGroupMessage(msg) {
let group = this.$store.state.groupStore.groups.find((g) => g.id == msg.groupId);
let group = store.state.groupStore.groups.find((g) => g.id == msg.groupId);
if (!group) {
this.$http({
http({
url: `/group/find/${msg.groupId}`,
method: 'get'
}).then((group) => {
this.insertGroupMessage(group, msg);
this.$store.commit("addGroup", group);
store.commit("addGroup", group);
})
} else {
//
@ -130,14 +121,15 @@
headImage: group.headImageThumb
};
//
this.$store.commit("openChat", chatInfo);
store.commit("openChat", chatInfo);
//
this.$store.commit("insertMessage", msg);
store.commit("insertMessage", msg);
//
!msg.selfSend && this.playAudioTip();
},
exit() {
this.$wsApi.closeWebSocket();
console.log("exit");
wsApi.closeWebSocket();
uni.removeStorageSync("loginInfo");
uni.navigateTo({
url: "/pages/login/login"
@ -149,7 +141,6 @@
// this.audioTip.src = "/static/audio/tip.wav";
// this.audioTip.play();
}
},
onLaunch() {
//
@ -157,10 +148,10 @@
if (loginInfo) {
//
this.init(loginInfo)
}else{
} else {
//
uni.navigateTo({
url:"/pages/login/login"
url: "/pages/login/login"
})
}
}
@ -169,23 +160,23 @@
<style lang="scss">
@import url('./static/icon/iconfont.css');
.tab-page{
// #ifdef H5
.tab-page {
// #ifdef H5
height: calc(100vh - 46px - 50px); // h5100vh
// #endif
// #ifndef H5
height: calc(100vh);
height: calc(100vh);
// #endif
background-color: #f8f8f8;
}
.page{
// #ifdef H5
.page {
// #ifdef H5
height: calc(100vh - 45px); // h5100vh
// #endif
// #ifndef H5
height: calc(100vh);
height: calc(100vh);
// #endif
background-color: #f8f8f8;
}

5
im-uniapp/common/request.js

@ -93,9 +93,6 @@ const navToLogin = () => {
title: "登录过期,请需要重新登录",
duration: 1500
})
uni.removeStorageSync("loginInfo");
uni.navigateTo({
url: '/pages/login/login'
});
getApp().exit();
}
export default request;

2
im-uniapp/main.js

@ -14,8 +14,6 @@ export function createApp() {
app.config.globalProperties.$wsApi = socketApi;
app.config.globalProperties.$emo = emotion;
app.config.globalProperties.$enums = enums;
app.config.globalProperties.$init = (data)=>{getApp().init(data)};
app.config.globalProperties.$exit = (data)=>{getApp().exit()};
return {
app
}

43
im-uniapp/pages/chat/chat-box.vue

@ -16,15 +16,14 @@
<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" :adjust-position="false"
@focus="onSendTextFoucs" @confirm="sendTextMessage()"
@keyboardheightchange="onKeyboardheightchange"
confirm-type="send" @blur="onSendTextBlur"
<textarea class="send-text-area" v-model="sendText" auto-height :show-confirm-bar="false"
:adjust-position="false" @focus="onSendTextFoucs" @confirm="sendTextMessage()"
@keyboardheightchange="onKeyboardheightchange" confirm-type="send" @blur="onSendTextBlur"
confirm-hold :hold-keyboard="true"></textarea>
</view>
<view class="iconfont icon-icon_emoji" @touchend.prevent="switchChatTabBox('emo',true)"></view>
<view v-show="sendText==''" class="iconfont icon-add-circle" @touchend.prevent="switchChatTabBox('tools',true)">
<view v-show="sendText==''" class="iconfont icon-add-circle"
@touchend.prevent="switchChatTabBox('tools',true)">
</view>
<button v-show="sendText!=''" class="btn-send" type="primary" @touchend.prevent="sendTextMessage()"
size="mini">发送</button>
@ -53,7 +52,7 @@
</scroll-view>
<view v-if="showKeyBoard"></view>
</view>
</view>
</template>
@ -69,7 +68,7 @@
showVoice: false, //
scrollMsgIdx: 0, //
chatTabBox: 'none',
showKeyBoard:false,
showKeyBoard: false,
keyboardHeight: 322,
tools: [{
name: "拍摄",
@ -167,12 +166,12 @@
});
},
switchChatTabBox(chatTabBox,hideKeyBoard) {
switchChatTabBox(chatTabBox, hideKeyBoard) {
console.log("switchChatTabBox")
this.chatTabBox = chatTabBox;
this.scrollToBottom();
if(hideKeyBoard){
uni.hideKeyboard();
if (hideKeyBoard) {
uni.hideKeyboard();
}
},
selectEmoji(emoText) {
@ -186,13 +185,13 @@
// console.log(this.keyboardHeight)
// }
},
onKeyboardheightchange(e){
onKeyboardheightchange(e) {
console.log(e);
if(e.detail.height >0){
if (e.detail.height > 0) {
this.showKeyBoard = true;
this.switchChatTabBox('none',false)
this.switchChatTabBox('none', false)
this.keyboardHeight = this.rpxTopx(e.detail.height);
}else{
} else {
this.showKeyBoard = false;
}
},
@ -303,6 +302,20 @@
},
messageAction() {
return `/message/${this.chat.type.toLowerCase()}/send`;
},
messageSize() {
if (!this.chat || !this.chat.messages) {
return 0;
}
return this.chat.messages.length;
}
},
watch: {
messageSize: function(newSize, oldSize) {
//
if (newSize > oldSize) {
this.scrollToBottom();
}
}
},
onLoad(options) {

4
im-uniapp/pages/group/group-edit.vue

@ -81,13 +81,13 @@
uni.showToast({
title: `群聊创建成功,快邀请小伙伴进群吧`,
icon: 'none',
duration: 2000
duration: 1500
});
setTimeout(()=>{
uni.navigateTo({
url: "/pages/group/group-info?id="+group.id
});
},2000)
},1500)
})
},

69
im-uniapp/pages/group/group-info.vue

@ -21,7 +21,7 @@
<view class="group-detail">
<uni-section title="群聊名称:" titleFontSize="30rpx">
<template v-slot:right>
<text>{{group.name}}</text>
<text>{{groupInfo.name}}</text>
</template>
</uni-section>
<uni-section title="群主:" titleFontSize="30rpx">
@ -32,16 +32,16 @@
<uni-section title="群聊备注:" titleFontSize="30rpx">
<template v-slot:right>
<text> {{group.remark}}</text>
<text> {{groupInfo.remark}}</text>
</template>
</uni-section>
<uni-section title="我在本群的昵称:" titleFontSize="30rpx">
<template v-slot:right>
<text> {{group.aliasName}}</text>
<text> {{groupInfo.aliasName}}</text>
</template>
</uni-section>
<uni-section title="群公告:" titleFontSize="30rpx">
<uni-notice-bar :text="group.notice" />
<uni-notice-bar :text="groupInfo.notice" />
</uni-section>
<view class="group-edit" @click="onEditGroup()">修改群聊资料 > </view>
</view>
@ -57,15 +57,15 @@
export default {
data() {
return {
group: {},
groupId: null,
groupMembers: []
}
},
methods: {
onFocusSearch() {},
onInviteMember(){
onInviteMember() {
uni.navigateTo({
url: `/pages/group/group-invite?id=${this.group.id}`
url: `/pages/group/group-invite?id=${this.groupId}`
})
},
onShowUserInfo(userId) {
@ -73,22 +73,22 @@
url: "/pages/common/user-info?id=" + userId
})
},
onShowMoreMmeber(){
onShowMoreMmeber() {
uni.navigateTo({
url: `/pages/group/group-member?id=${this.group.id}`
url: `/pages/group/group-member?id=${this.groupId}`
})
},
onEditGroup() {
uni.navigateTo({
url: `/pages/group/group-edit?id=${this.group.id}`
url: `/pages/group/group-edit?id=${this.groupId}`
})
},
onSendMessage() {
let chat = {
type: 'GROUP',
targetId: this.group.id,
showName: this.group.remark,
headImage: this.group.headImage,
targetId: this.groupInfo.id,
showName: this.groupInfo.remark,
headImage: this.groupInfo.headImage,
};
this.$store.commit("openChat", chat);
uni.navigateTo({
@ -100,19 +100,19 @@
title: '确认退出?',
content: `退出群聊后将不再接受群里的消息,确认退出吗?`,
success: () => {
if(res.cancel)
if (res.cancel)
return;
this.$http({
url: `/group/quit/${this.group.id}`,
url: `/group/quit/${this.groupId}`,
method: 'DELETE'
}).then(() => {
this.$store.commit("removeGroup", this.group.id);
this.$store.commit("removeGroupChat", this.group.id);
this.$store.commit("removeGroup", this.groupId);
this.$store.commit("removeGroupChat", this.groupId);
uni.showModal({
title: `退出成功`,
content: `您已退出群聊'${this.group.name}'`,
content: `您已退出群聊'${this.groupInfo.name}'`,
showCancel: false,
success:()=>{
success: () => {
uni.navigateBack();
}
})
@ -123,35 +123,34 @@
onDissolveGroup() {
uni.showModal({
title: '确认解散?',
content: `确认要解散群聊'${this.group.name}'吗?`,
success:(res)=>{
if(res.cancel)
content: `确认要解散群聊'${this.groupInfo.name}'吗?`,
success: (res) => {
if (res.cancel)
return;
this.$http({
url: `/group/delete/${this.group.id}`,
url: `/group/delete/${this.groupId}`,
method: 'delete'
}).then(() => {
this.$store.commit("removeGroup", this.group.id);
this.$store.commit("removeGroupChat", this.group.id);
this.$store.commit("removeGroup", this.groupId);
this.$store.commit("removeGroupChat", this.groupId);
uni.showModal({
title: `解散成功`,
content: `群聊'${this.group.name}'已解散`,
content: `群聊'${this.groupInfo.name}'已解散`,
showCancel: false,
success:()=>{
success: () => {
uni.navigateBack();
}
})
});
}
});
},
loadGroupInfo(id) {
this.$http({
url: `/group/find/${id}`,
method: 'GET'
}).then((group) => {
this.group = group;
//
this.$store.commit("updateChatFromGroup", group);
//
@ -169,20 +168,26 @@
}
},
computed: {
groupInfo() {
let groups = this.$store.state.groupStore.groups;
return groups.find(g => g.id == this.groupId)
},
ownerName() {
let member = this.groupMembers.find((m) => m.userId == this.group.ownerId);
let member = this.groupMembers.find((m) => m.userId == this.groupInfo.ownerId);
return member && member.aliasName;
},
isOwner() {
return this.group.ownerId == this.$store.state.userStore.userInfo.id;
},
return this.groupInfo.ownerId == this.$store.state.userStore.userInfo.id;
}
},
onLoad(options) {
this.groupId = options.id;
//
this.loadGroupInfo(options.id);
//
this.loadGroupMembers(options.id)
}
}
</script>

2
im-uniapp/pages/login/login.vue

@ -49,7 +49,7 @@
console.log("登录成功,自动跳转到聊天页面...")
uni.setStorageSync("loginInfo", data);
// App.vue
this.$init(data);
getApp().init(data)
//
uni.switchTab({
url: "/pages/chat/chat"

3
im-uniapp/pages/mine/mine-edit.vue

@ -53,6 +53,9 @@
title:"修改成功",
icon: 'none'
});
setTimeout(()=>{
uni.navigateBack();
},1000);
})
}
},

3
im-uniapp/pages/mine/mine-password.vue

@ -82,6 +82,9 @@
title: "修改密码成功",
icon: 'none'
})
setTimeout(()=>{
uni.navigateBack();
},1000);
})
}).catch(err => {
console.log('表单错误信息:', err);

2
im-uniapp/pages/mine/mine.vue

@ -52,7 +52,7 @@
title: '确认退出?',
success: (res) => {
if (res.confirm) {
this.$exit();
getApp().exit()
}
}
});

6
im-uniapp/store/friendStore.js

@ -1,4 +1,4 @@
import request from '../common/request';
import http from '../common/request';
export default {
@ -50,7 +50,7 @@ export default {
actions: {
loadFriend(context) {
return new Promise((resolve, reject) => {
request({
http({
url: '/friend/list',
method: 'GET'
}).then((friends) => {
@ -68,7 +68,7 @@ export default {
context.state.friends.forEach((f) => {
userIds.push(f.id)
});
request({
http({
url: '/user/online?userIds='+ userIds.join(','),
method: 'GET'
}).then((onlineIds) => {

5
im-uniapp/store/groupStore.js

@ -1,7 +1,6 @@
import request from '@/common/request';
import http from '@/common/request';
export default {
state: {
groups: [],
activeIndex: -1,
@ -39,7 +38,7 @@ export default {
actions: {
loadGroup(context) {
return new Promise((resolve, reject) => {
request({
http({
url: '/group/list',
method: 'GET'
}).then((groups) => {

8
im-uniapp/store/userStore.js

@ -1,9 +1,8 @@
import {USER_STATE} from "../common/enums"
import request from '../common/request'
import http from '../common/request'
export default {
state: {
userInfo: {},
state: USER_STATE.FREE
@ -11,7 +10,8 @@ export default {
mutations: {
setUserInfo(state, userInfo) {
state.userInfo = userInfo;
// 使用深拷贝方式,否则小程序页面不刷新
Object.assign(state.userInfo, userInfo);
},
setUserState(state, userState) {
state.state = userState;
@ -20,7 +20,7 @@ export default {
actions:{
loadUser(context){
return new Promise((resolve, reject) => {
request({
http({
url: '/user/self',
method: 'GET'
}).then((userInfo) => {

Loading…
Cancel
Save