Browse Source

优化代码

master
袁磊 6 months ago
parent
commit
f6485a3bb0
  1. 4
      App.vue
  2. 44
      pages/home/user.vue
  3. 18
      pages/setting/setting.vue
  4. 9
      pages/user/login.vue
  5. 5
      store/modules/user.js
  6. 4
      uni_modules/vrapile-im/pages/home/chatHome.vue
  7. 10
      uni_modules/vrapile-im/readme.md
  8. 45
      uni_modules/vrapile-im/store/modules/chat.js
  9. 10
      uni_modules/vrapile-im/store/modules/socket.js
  10. 1
      utils/request.js

4
App.vue

@ -59,8 +59,8 @@
}, },
initWebSocket(){ initWebSocket(){
if(getToken()){ if(getToken()){
store.dispatch('GetChatList').then(res => {
store.dispatch('ConnSocket');
socketStore.dispatch('GetChatList').then(res => {
socketStore.dispatch('ConnSocket');
}); });
} }
} }

44
pages/home/user.vue

@ -49,49 +49,6 @@
</view> </view>
</view> </view>
<view v-if="userInfo.userId" class="nine-nav-001-item mt15">
<view class="nine-nav-001-item-left">
<image class="image35" mode="scaleToFill" src="/static/image/user/c5.png"></image>
<text class="nine-nav-001-item-left-text">我的钱包</text>
</view>
<view class="nine-nav-001-item-right">
<image class="nine-nav-001-item-right-image" mode="scaleToFill" src="/static/image/user/cc.png">
</image>
</view>
</view>
<view v-if="userInfo.userId" class="nine-nav-001-item mt15">
<view class="nine-nav-001-item-left">
<image class="image35" mode="scaleToFill" src="/static/image/user/c1.png"></image>
<text class="nine-nav-001-item-left-text">我的订单</text>
</view>
<view class="nine-nav-001-item-right">
<image class="nine-nav-001-item-right-image" mode="scaleToFill" src="/static/image/user/cc.png">
</image>
</view>
</view>
<view v-if="userInfo.userId" class="nine-nav-001-item">
<view class="nine-nav-001-item-left">
<image class="image35" mode="scaleToFill" src="/static/image/user/c8.png"></image>
<text class="nine-nav-001-item-left-text">我的卡券</text>
</view>
<view class="nine-nav-001-item-right">
<image class="nine-nav-001-item-right-image" mode="scaleToFill" src="/static/image/user/cc.png">
</image>
</view>
</view>
<view class="nine-nav-001-item">
<view class="nine-nav-001-item-left">
<image class="image35" mode="scaleToFill" src="/static/image/user/c7.png"></image>
<text class="nine-nav-001-item-left-text">购物车</text>
</view>
<view class="nine-nav-001-item-right">
<image class="nine-nav-001-item-right-image" mode="scaleToFill" src="/static/image/user/cc.png">
</image>
</view>
</view>
<!--#ifndef MP-ALIPAY-->
<view class="nine-nav-001-item mt15"> <view class="nine-nav-001-item mt15">
<view class="nine-nav-001-item-left"> <view class="nine-nav-001-item-left">
<image class="image35" mode="scaleToFill" src="/static/image/user/c6.png"></image> <image class="image35" mode="scaleToFill" src="/static/image/user/c6.png"></image>
@ -102,7 +59,6 @@
</image> </image>
</view> </view>
</view> </view>
<!--#endif-->
<view class="nine-nav-001-item mt15" @click="openUrl('/pages/setting/setting')"> <view class="nine-nav-001-item mt15" @click="openUrl('/pages/setting/setting')">
<view class="nine-nav-001-item-left"> <view class="nine-nav-001-item-left">

18
pages/setting/setting.vue

@ -11,15 +11,6 @@
</image> </image>
</view> </view>
</view> </view>
<view v-if="userInfo.userId" class="nine-nav-001-item mt10">
<view class="nine-nav-001-item-left">
<text class="nine-nav-001-item-left-text">修改密码</text>
</view>
<view class="nine-nav-001-item-right">
<image class="nine-nav-001-item-right-image" mode="scaleToFill" src="/static/image/user/cc.png">
</image>
</view>
</view>
<view class="nine-nav-001-item mt10" @click="openUrl('/pages/setting/contact')"> <view class="nine-nav-001-item mt10" @click="openUrl('/pages/setting/contact')">
<view class="nine-nav-001-item-left"> <view class="nine-nav-001-item-left">
<text class="nine-nav-001-item-left-text">联系我们</text> <text class="nine-nav-001-item-left-text">联系我们</text>
@ -29,15 +20,6 @@
</image> </image>
</view> </view>
</view> </view>
<view class="nine-nav-001-item">
<view class="nine-nav-001-item-left">
<text class="nine-nav-001-item-left-text">清空缓存</text>
</view>
<view class="nine-nav-001-item-right">
<image class="nine-nav-001-item-right-image" mode="scaleToFill" src="/static/image/user/cc.png">
</image>
</view>
</view>
<view class="nine-nav-001-item" @click="openUrl('/pages/setting/about')"> <view class="nine-nav-001-item" @click="openUrl('/pages/setting/about')">
<view class="nine-nav-001-item-left"> <view class="nine-nav-001-item-left">
<text class="nine-nav-001-item-left-text">关于</text> <text class="nine-nav-001-item-left-text">关于</text>

9
pages/user/login.vue

@ -144,7 +144,6 @@
this.verifyfocus(); this.verifyfocus();
return; return;
} }
this.loading("正在登录...");
store.dispatch('Login', { store.dispatch('Login', {
loginType: this.loginType, loginType: this.loginType,
userName: this.userName.trim(), userName: this.userName.trim(),
@ -153,19 +152,11 @@
registerFlag: "Y" registerFlag: "Y"
}).then(() => { }).then(() => {
this.loginSuccess() this.loginSuccess()
}).catch(() => {
this.closeLoading()
}) })
}, },
loginSuccess() { loginSuccess() {
store.dispatch('GetInfo').then(res => { store.dispatch('GetInfo').then(res => {
socketStore.dispatch('GetChatList', {userId: store.state.user.userInfo.userId}).then(res=>{
socketStore.dispatch('ConnSocket', { url: import.meta.env.VITE_APP_SOCKET_URL, token: getToken() });
});
this.reLaunch('/pages/home/user') this.reLaunch('/pages/home/user')
this.closeLoading()
}).catch(()=>{
this.closeLoading()
}) })
}, },
registerClick() { registerClick() {

5
store/modules/user.js

@ -1,4 +1,5 @@
import store from '@/store' import store from '@/store'
import socketStore from '@/uni_modules/vrapile-im/store'
import storage from '@/utils/storage' import storage from '@/utils/storage'
import constant from '@/utils/constant' import constant from '@/utils/constant'
import { login, logout, getInfo } from '@/api/login' import { login, logout, getInfo } from '@/api/login'
@ -66,6 +67,10 @@ const user = {
commit('SET_USER_INFO', user) commit('SET_USER_INFO', user)
commit('SET_NAME', userName) commit('SET_NAME', userName)
commit('SET_AVATAR', avatar) commit('SET_AVATAR', avatar)
socketStore.dispatch('GetChatList').then(res => {
socketStore.dispatch('ConnSocket');
});
resolve(res) resolve(res)
}).catch(error => { }).catch(error => {
reject(error) reject(error)

4
uni_modules/vrapile-im/pages/home/chatHome.vue

@ -138,8 +138,8 @@
// socket // socket
noListDo(){ noListDo(){
if(storeOut.state.user.userInfo.userId && (!this.getChatList || this.getChatList.length == 0)){ if(storeOut.state.user.userInfo.userId && (!this.getChatList || this.getChatList.length == 0)){
store.dispatch('GetChatList', {userId: storeOut.state.user.userInfo.userId}).then(res => {
store.dispatch('ConnSocket', { url: import.meta.env.VITE_APP_SOCKET_URL, token: getToken() });
store.dispatch('GetChatList').then(res => {
store.dispatch('ConnSocket');
}); });
} }
}, },

10
uni_modules/vrapile-im/readme.md

@ -40,18 +40,14 @@
"tabBar": { "tabBar": {
"selectedColor": "#0000ff", "selectedColor": "#0000ff",
"list": [ "list": [
...
{ {
"pagePath": "uni_modules/vrapile-im/pages/home/chatHome", "pagePath": "uni_modules/vrapile-im/pages/home/chatHome",
"iconPath": "static/image/tabbar/chat.png", "iconPath": "static/image/tabbar/chat.png",
"selectedIconPath": "static/image/tabbar/chat-fill.png", "selectedIconPath": "static/image/tabbar/chat-fill.png",
"text": "消息" "text": "消息"
}, },
{
"pagePath": "pages/home/user",
"iconPath": "static/image/tabbar/user.png",
"selectedIconPath": "static/image/tabbar/user-fill.png",
"text": "我的"
}
...
] ]
}, },
``` ```
@ -64,7 +60,7 @@
<br><br> <br><br>
## 演示说明 ## 演示说明
<font color="gray">*以下所有工程共用一个后端,账号互通
<font color="gray">*以下系统共用一个后端,账号互通
( 账号/密码:visitor/visitor )*</font> ( 账号/密码:visitor/visitor )*</font>
<br><br> <br><br>

45
uni_modules/vrapile-im/store/modules/chat.js

@ -89,7 +89,7 @@ const chat = {
delete itemFriend["updateBy"] delete itemFriend["updateBy"]
delete itemFriend["createTime"] delete itemFriend["createTime"]
delete itemFriend["updateTime"] delete itemFriend["updateTime"]
listFriend.push({type: 0, ...itemFriend, userId: params.userId})
listFriend.push({type: 0, ...itemFriend})
commit('SET_USER_OBJECT', {...itemFriend, userId: itemFriend.friendId}) commit('SET_USER_OBJECT', {...itemFriend, userId: itemFriend.friendId})
} }
commit('SET_CHAT_FRIEND_LIST', listFriend) commit('SET_CHAT_FRIEND_LIST', listFriend)
@ -98,36 +98,37 @@ const chat = {
getUserAllGroup().then(res1 => { getUserAllGroup().then(res1 => {
let listGroup = [] let listGroup = []
for(let itemGroup of res1.data){ for(let itemGroup of res1.data){
delete itemGroup["createBy"]
delete itemGroup["updateBy"]
delete itemGroup["createTime"]
delete itemGroup["updateTime"]
listGroup.push({type: 1, ...itemGroup, userId: params.userId})
delete itemGroup["createBy"];
delete itemGroup["updateBy"];
delete itemGroup["createTime"];
delete itemGroup["updateTime"];
listGroup.push({type: 1, ...itemGroup});
// 查询群组人员 // 查询群组人员
getGroupUser(itemGroup.id).then(res2 => { getGroupUser(itemGroup.id).then(res2 => {
for(let itemGroupUser of res2.data){ for(let itemGroupUser of res2.data){
delete itemGroupUser["createBy"]
delete itemGroupUser["updateBy"]
delete itemGroupUser["createTime"]
delete itemGroupUser["updateTime"]
delete itemGroupUser["groupId"]
delete itemGroupUser["groupName"]
commit('SET_USER_OBJECT', itemGroupUser)
delete itemGroupUser["createBy"];
delete itemGroupUser["updateBy"];
delete itemGroupUser["createTime"];
delete itemGroupUser["updateTime"];
delete itemGroupUser["groupId"];
delete itemGroupUser["groupName"];
commit('SET_USER_OBJECT', itemGroupUser);
} }
resolve(res2)
}); });
delete itemGroup["createBy"]
delete itemGroup["updateBy"]
delete itemGroup["createTime"]
delete itemGroup["updateTime"]
commit('SET_GROUP_OBJECT', itemGroup)
delete itemGroup["createBy"];
delete itemGroup["updateBy"];
delete itemGroup["createTime"];
delete itemGroup["updateTime"];
commit('SET_GROUP_OBJECT', itemGroup);
} }
commit('SET_CHAT_GROUP_LIST', listGroup)
commit('SET_CHAT_GROUP_LIST', listGroup);
}).catch(error => { }).catch(error => {
reject(error)
reject(error);
}) })
}).catch(error => { }).catch(error => {
reject(error)
reject(error);
}).finally(() => {
resolve();
}) })
}) })
} }

10
uni_modules/vrapile-im/store/modules/socket.js

@ -3,6 +3,8 @@ import storage from '@/uni_modules/vrapile-im/utils/storage'
import constant from '@/uni_modules/vrapile-im/utils/constant' import constant from '@/uni_modules/vrapile-im/utils/constant'
import { formatDate } from '@/uni_modules/vrapile-im/utils/nineTool'; import { formatDate } from '@/uni_modules/vrapile-im/utils/nineTool';
import { TioSocket, getChatKey } from '@/uni_modules/vrapile-im/utils/tiosocket'; import { TioSocket, getChatKey } from '@/uni_modules/vrapile-im/utils/tiosocket';
// 外部需提供获取token函数并可以获取token
import { getToken } from '@/utils/token'
const socket = { const socket = {
state: { state: {
@ -76,7 +78,7 @@ const socket = {
try{ try{
if(unReadNum > 0){ if(unReadNum > 0){
uni.setTabBarBadge({ uni.setTabBarBadge({
index: getApp().globalData.msgTabBarIndex,
index: getApp().globalData.chatIndex,
text: String(unReadNum), text: String(unReadNum),
fail: (e) => { fail: (e) => {
// console.log(e) // console.log(e)
@ -84,7 +86,7 @@ const socket = {
}) })
}else{ }else{
uni.removeTabBarBadge({ uni.removeTabBarBadge({
index: getApp().globalData.msgTabBarIndex,
index: getApp().globalData.chatIndex,
fail: (e) => { fail: (e) => {
// console.log(e) // console.log(e)
} }
@ -105,10 +107,10 @@ const socket = {
// 连接websocket // 连接websocket
ConnSocket({ commit, state }, params) { ConnSocket({ commit, state }, params) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let url = params.url;
let url = import.meta.env.VITE_APP_SOCKET_URL;
let heartbeatTimeout = 50000; // 心跳超时时间,单位:毫秒 let heartbeatTimeout = 50000; // 心跳超时时间,单位:毫秒
let reconnInterval = 5000; // 重连间隔时间,单位:毫秒 let reconnInterval = 5000; // 重连间隔时间,单位:毫秒
let paramStr = "app=udemo-im&token=" + params.token
let paramStr = "app="+import.meta.env.VITE_APP_FLAG+"&token=" + getToken();
let socket = new TioSocket(url, paramStr, heartbeatTimeout, reconnInterval); let socket = new TioSocket(url, paramStr, heartbeatTimeout, reconnInterval);
socket.connect(false); socket.connect(false);
}) })

1
utils/request.js

@ -18,6 +18,7 @@ const request = config => {
config.header['Authorization'] = 'Vrapile ' + getToken() config.header['Authorization'] = 'Vrapile ' + getToken()
} }
config.header["Content-Language"] = "zh_CN";
config.header["platform"] = getApp().globalData.appId; config.header["platform"] = getApp().globalData.appId;
config.header["terminal"] = getApp().globalData.terminal; config.header["terminal"] = getApp().globalData.terminal;
config.header["nodeId"] = getNodeId(); config.header["nodeId"] = getNodeId();

Loading…
Cancel
Save