Files
ZhHealth/store/modules/im.js
2022-01-20 16:15:24 +08:00

128 lines
4.2 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: {}
},
getters: {
newMessage(state) {
return state.newMsg
},
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
}
},
actions: {
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) {
console.log('哪里更新的 ', 1);
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) {
console.log('哪里更新的 ', 2);
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);
}
})
}