Files
ZhHealth/store/modules/im.js

153 lines
5.3 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: {
friends: {},
sender: {},
},
getters: {
friends(state) {
return state.friends
},
userInfo: (state) => (targetId) => {
if (state.friends[targetId]) {
const info = state.friends[targetId]
return {
userId: info.userId,
name: info.name,
portraitUrl: info.localAvatar ? info.localAvatar : require('@/static/user/cover.png')
}
} else {
return {
userId: '',
name: '',
portraitUrl: ''
}
}
},
sender(state) {
return state.sender
}
},
mutations: {
updateFriendInfo(state, userInfo) {
Vue.set(state.friends, userInfo.userId, userInfo)
},
SET_state_sender(state, userInfo) {
state.sender = userInfo
}
},
actions: {
setSenderInfo({
commit
}, userInfo) {
commit('SET_state_sender', userInfo)
},
// 载入好友信息
launchFriend({
commit
}, data) {
commit('updateFriendInfo', data)
},
// 更新好友信息这个时候要校验hash值了
updateFriend({
commit
}, userInfo) {
const model = uni.model.friendModel
model.find('userId=' + userInfo.userId, (err, result) => {
if (userInfo.hash != result[0].hash) {
commit('updateFriendInfo', userInfo)
console.log(userInfo);
if (userInfo.portraitUrl && userInfo.portraitUrl != result[0].portraitUrl) {
saveAvatar(userInfo, (savedFilePath) => {
const info = {
userId: userInfo.userId,
name: userInfo.name,
hash: userInfo.hash,
portraitUrl: userInfo.portraitUrl,
localAvatar: savedFilePath
}
model.update('userId=' + userInfo.userId, info, (err, res) => {
console.log('保存结果', err, res);
})
commit('updateFriendInfo', info)
})
} else {
const info = {
userId: userInfo.userId,
name: userInfo.name,
hash: userInfo.hash,
portraitUrl: userInfo.portraitUrl,
localAvatar: result[0].localAvatar
}
model.update('userId=' + userInfo.userId, info, (err, res) => {
console.log('保存结果', err, res);
})
}
} else {
console.log('不用更新的用户', userInfo.userId, userInfo.name);
}
})
},
// 初始化好友信息
initFriend({
commit
}, userInfo) {
// 将好友信息保存到vuex的内存中方便立即使用
commit('updateFriendInfo', userInfo)
const model = uni.model.friendModel
// 用户头像,是否需要下载到本地
if (userInfo.portraitUrl) {
saveAvatar(userInfo, (savedFilePath) => {
const info = {
userId: userInfo.userId,
name: userInfo.name,
hash: userInfo.hash,
portraitUrl: userInfo.portraitUrl,
localAvatar: savedFilePath
}
model.insert(info, (err, res) => {
console.log('保存结果', err, res);
})
// 保存头像后,更新信息
commit('updateFriendInfo', info)
})
} else {
// 直接将信息,写入数据库
const info = {
userId: userInfo.userId,
name: userInfo.name,
hash: userInfo.hash,
portraitUrl: userInfo.portraitUrl,
localAvatar: ''
}
model.insert(info, (err, res) => {
console.log('保存结果', err, res);
})
}
}
}
}
const saveAvatar = (userInfo, callback) => {
uni.downloadFile({
url: userInfo.portraitUrl,
success: ({
tempFilePath
}) => {
uni.saveFile({
tempFilePath: tempFilePath,
success: ({
savedFilePath
}) => {
callback(savedFilePath)
}
})
},
fail: (err) => {
console.log('头像保存失败', err);
}
})
}