150 lines
5.0 KiB
JavaScript
150 lines
5.0 KiB
JavaScript
import im from "@/utils/im/index.js"
|
||
|
||
export default {
|
||
state: {
|
||
friends: {},
|
||
myInfo: {}
|
||
},
|
||
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.myInfo
|
||
}
|
||
},
|
||
mutations: {
|
||
updateFriendInfo(state, userInfo) {
|
||
Vue.set(state.friends, userInfo.userId, userInfo)
|
||
},
|
||
SET_state_sender(state, userInfo) {
|
||
state.myInfo = {
|
||
userId: userInfo.userId,
|
||
name: userInfo.name,
|
||
portraitUrl: userInfo.portraitUrl
|
||
}
|
||
}
|
||
},
|
||
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)
|
||
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) => {
|
||
})
|
||
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) => {
|
||
})
|
||
}
|
||
} else {
|
||
}
|
||
})
|
||
},
|
||
// 初始化好友信息
|
||
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) => {
|
||
})
|
||
// 保存头像后,更新信息
|
||
commit('updateFriendInfo', info)
|
||
})
|
||
} else {
|
||
// 直接将信息,写入数据库
|
||
const info = {
|
||
userId: userInfo.userId,
|
||
name: userInfo.name,
|
||
hash: userInfo.hash,
|
||
portraitUrl: userInfo.portraitUrl,
|
||
localAvatar: ''
|
||
}
|
||
model.insert(info, (err, res) => {
|
||
})
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
const saveAvatar = (userInfo, callback) => {
|
||
uni.downloadFile({
|
||
url: userInfo.portraitUrl,
|
||
success: ({
|
||
tempFilePath
|
||
}) => {
|
||
uni.saveFile({
|
||
tempFilePath: tempFilePath,
|
||
success: ({
|
||
savedFilePath
|
||
}) => {
|
||
callback(savedFilePath)
|
||
}
|
||
})
|
||
},
|
||
fail: (err) => {
|
||
}
|
||
})
|
||
}
|