代码优化,变量名称调整
This commit is contained in:
@@ -2,135 +2,134 @@ import im from "@/utils/im/index.js"
|
||||
|
||||
export default {
|
||||
state: {
|
||||
friends: {},
|
||||
contacts: {},
|
||||
myInfo: {}
|
||||
},
|
||||
getters: {
|
||||
friends(state) {
|
||||
return state.friends
|
||||
contacts(state) {
|
||||
return state.contacts
|
||||
},
|
||||
userInfo: (state) => (targetId) => {
|
||||
if (state.friends[targetId]) {
|
||||
const info = state.friends[targetId]
|
||||
contactInfo: (state) => (targetId) => {
|
||||
if (state.contacts[targetId]) {
|
||||
const info = state.contacts[targetId]
|
||||
|
||||
return {
|
||||
userId: info.userId,
|
||||
name: info.name,
|
||||
name: info.name,
|
||||
hash: info.hash,
|
||||
portraitUrl: info.localAvatar ? info.localAvatar : require('@/static/user/cover.png')
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
userId: '',
|
||||
name: '',
|
||||
name: '',
|
||||
hash: '',
|
||||
portraitUrl: ''
|
||||
}
|
||||
}
|
||||
},
|
||||
// 联系人是否存在
|
||||
contactIsExist: (state) => (targetId) => {
|
||||
return Boolean(state.contacts[targetId])
|
||||
},
|
||||
sender(state) {
|
||||
return state.myInfo
|
||||
}
|
||||
},
|
||||
mutations: {
|
||||
updateFriendInfo(state, userInfo) {
|
||||
Vue.set(state.friends, userInfo.userId, userInfo)
|
||||
updateContactInfo(state, contactInfo) {
|
||||
Vue.set(state.contacts, contactInfo.targetId, contactInfo)
|
||||
},
|
||||
SET_state_sender(state, userInfo) {
|
||||
setSenderInfo(state, contactInfo) {
|
||||
state.myInfo = {
|
||||
userId: userInfo.userId,
|
||||
name: userInfo.name,
|
||||
portraitUrl: userInfo.portraitUrl
|
||||
userId: contactInfo.targetId,
|
||||
name: contactInfo.name,
|
||||
portraitUrl: contactInfo.portraitUrl
|
||||
}
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
setSenderInfo({
|
||||
commit
|
||||
}, userInfo) {
|
||||
commit('SET_state_sender', userInfo)
|
||||
}, contactInfo) {
|
||||
commit('setSenderInfo', contactInfo)
|
||||
},
|
||||
// 载入好友信息
|
||||
launchFriend({
|
||||
launchContact({
|
||||
commit
|
||||
}, data) {
|
||||
commit('updateFriendInfo', data)
|
||||
commit('updateContactInfo', data)
|
||||
},
|
||||
// 更新好友信息,这个时候要校验hash值了
|
||||
updateFriend({
|
||||
updateContact({
|
||||
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) => {
|
||||
}, contactInfo) {
|
||||
const model = uni.model.contactModel
|
||||
model.find('targetId=' + contactInfo.targetId, (err, result) => {
|
||||
if (contactInfo.hash != result[0].hash) {
|
||||
commit('updateContactInfo', contactInfo)
|
||||
if (contactInfo.portraitUrl && contactInfo.portraitUrl != result[0].portraitUrl) {
|
||||
saveAvatar(contactInfo, (savedFilePath) => {
|
||||
const info = {
|
||||
userId: userInfo.userId,
|
||||
name: userInfo.name,
|
||||
hash: userInfo.hash,
|
||||
portraitUrl: userInfo.portraitUrl,
|
||||
targetId: contactInfo.targetId,
|
||||
name: contactInfo.name,
|
||||
hash: contactInfo.hash,
|
||||
portraitUrl: contactInfo.portraitUrl,
|
||||
localAvatar: savedFilePath
|
||||
}
|
||||
model.update('userId=' + userInfo.userId, info, (err, res) => {
|
||||
})
|
||||
commit('updateFriendInfo', info)
|
||||
model.update('targetId=' + contactInfo.targetId, info, (err, res) => {})
|
||||
commit('updateContactInfo', info)
|
||||
})
|
||||
} else {
|
||||
const info = {
|
||||
userId: userInfo.userId,
|
||||
name: userInfo.name,
|
||||
hash: userInfo.hash,
|
||||
portraitUrl: userInfo.portraitUrl,
|
||||
targetId: contactInfo.targetId,
|
||||
name: contactInfo.name,
|
||||
hash: contactInfo.hash,
|
||||
portraitUrl: contactInfo.portraitUrl,
|
||||
localAvatar: result[0].localAvatar
|
||||
}
|
||||
model.update('userId=' + userInfo.userId, info, (err, res) => {
|
||||
})
|
||||
model.update('targetId=' + contactInfo.targetId, info, (err, res) => {})
|
||||
}
|
||||
} else {
|
||||
}
|
||||
} else {}
|
||||
})
|
||||
},
|
||||
// 初始化好友信息
|
||||
initFriend({
|
||||
initContact({
|
||||
commit
|
||||
}, userInfo) {
|
||||
}, contactInfo) {
|
||||
// 将好友信息保存到vuex的内存中,方便立即使用
|
||||
commit('updateFriendInfo', userInfo)
|
||||
const model = uni.model.friendModel
|
||||
commit('updateContactInfo', contactInfo)
|
||||
const model = uni.model.contactModel
|
||||
// 用户头像,是否需要下载到本地
|
||||
if (userInfo.portraitUrl) {
|
||||
saveAvatar(userInfo, (savedFilePath) => {
|
||||
if (contactInfo.portraitUrl) {
|
||||
saveAvatar(contactInfo, (savedFilePath) => {
|
||||
const info = {
|
||||
userId: userInfo.userId,
|
||||
name: userInfo.name,
|
||||
hash: userInfo.hash,
|
||||
portraitUrl: userInfo.portraitUrl,
|
||||
targetId: contactInfo.targetId,
|
||||
name: contactInfo.name,
|
||||
hash: contactInfo.hash,
|
||||
portraitUrl: contactInfo.portraitUrl,
|
||||
localAvatar: savedFilePath
|
||||
}
|
||||
model.insert(info, (err, res) => {
|
||||
})
|
||||
model.insert(info, (err, res) => {})
|
||||
// 保存头像后,更新信息
|
||||
commit('updateFriendInfo', info)
|
||||
commit('updateContactInfo', info)
|
||||
})
|
||||
} else {
|
||||
// 直接将信息,写入数据库
|
||||
const info = {
|
||||
userId: userInfo.userId,
|
||||
name: userInfo.name,
|
||||
hash: userInfo.hash,
|
||||
portraitUrl: userInfo.portraitUrl,
|
||||
targetId: contactInfo.targetId,
|
||||
name: contactInfo.name,
|
||||
hash: contactInfo.hash,
|
||||
portraitUrl: contactInfo.portraitUrl,
|
||||
localAvatar: ''
|
||||
}
|
||||
model.insert(info, (err, res) => {
|
||||
})
|
||||
model.insert(info, (err, res) => {})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const saveAvatar = (userInfo, callback) => {
|
||||
const saveAvatar = (contactInfo, callback) => {
|
||||
uni.downloadFile({
|
||||
url: userInfo.portraitUrl,
|
||||
url: contactInfo.portraitUrl,
|
||||
success: ({
|
||||
tempFilePath
|
||||
}) => {
|
||||
@@ -143,7 +142,6 @@ const saveAvatar = (userInfo, callback) => {
|
||||
}
|
||||
})
|
||||
},
|
||||
fail: (err) => {
|
||||
}
|
||||
fail: (err) => {}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user