Files
ZhHealth/store/modules/im.js

137 lines
4.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import im from "@/utils/im/index.js"
export default {
state: {
newMsg: {},
friends: {},
sender: {},
// IM连接状态0是正常
connection: 0
},
getters: {
newMessage(state) {
return state.newMsg
},
connection(state) {
return state.connection
},
friends(state) {
return state.friends
},
userInfo: (state) => (targetId) => {
if (state.friends[targetId]) {
return state.friends[targetId]
} else {
console.log('没找到,死循环了,得处理 todo', targetId);
// im.syncUserInfo(targetId)
return {
name: '',
address: '',
hash: '',
portraitUrl: ''
}
}
},
hasUser: (state) => (targetId) => {
return Boolean(state.friends[targetId])
},
sender(state) {
return state.sender
}
},
mutations: {
newMessage(state, msg) {
Vue.set(state, 'newMsg', msg)
},
updateFriends(state, userInfo) {
Vue.set(state.friends, userInfo.userId, userInfo)
},
SET_state_sender(state, userInfo) {
state.sender = userInfo
},
SET_connection_status(state, status) {
state.connection = status
}
},
actions: {
updateConnectionStatus({commit}, status) {
commit('SET_connection_status', status)
},
newMessage({
commit
}, msg) {
commit('newMessage', msg)
},
setSenderInfo({
commit
}, userInfo) {
commit('SET_state_sender', userInfo)
},
updateFriends({
commit
}, userInfo) {
commit('updateFriends', userInfo)
const model = uni.model.friendModel
model.find('userId=' + userInfo.userId, (err, user) => {
if (!err && user.length == 0) {
saveAvatar(userInfo, (savedFilePath) => {
model.insert({
userId: userInfo.userId,
name: userInfo.name,
hash: userInfo.hash,
address: userInfo.address,
portraitUrl: savedFilePath,
}, (err, result) => {})
userInfo.portraitUrl = savedFilePath
commit('updateFriends', userInfo)
})
} else if (!err && user[0].hash != userInfo.hash) {
saveAvatar(userInfo, (savedFilePath) => {
model.update('userId=' + userInfo.userId, {
name: userInfo.name,
hash: userInfo.hash,
portraitUrl: savedFilePath,
}, (err, result) => {})
userInfo.portraitUrl = savedFilePath
commit('updateFriends', userInfo)
})
} else if (!err && user[0].portraitUrl.length > 50) {
saveAvatar(userInfo, (savedFilePath) => {
model.update('userId=' + userInfo.userId, {
name: userInfo.name,
hash: userInfo.hash,
portraitUrl: savedFilePath,
}, (err, result) => {})
userInfo.portraitUrl = savedFilePath
commit('updateFriends', userInfo)
})
} else {
console.log('不用操作', user[0]);
}
})
}
}
}
const saveAvatar = (userInfo, callback) => {
uni.downloadFile({
url: userInfo.portraitUrl,
success: ({
tempFilePath
}) => {
uni.saveFile({
tempFilePath: tempFilePath,
success: ({
savedFilePath
}) => {
callback(savedFilePath)
}
})
},
fail: (err) => {
console.log('头像保存失败', err);
}
})
}