Files
ZhHealth/store/modules/im.js

190 lines
6.9 KiB
JavaScript
Raw Permalink 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"
import {
contactModel
} from '@/utils/im/models.js'
export default {
state: {
contacts: {},
myInfo: {}
},
getters: {
contacts(state) {
return state.contacts
},
contactInfo: (state) => (targetId) => {
if (state.contacts[targetId]) {
const info = state.contacts[targetId]
return {
name: info.remark ? info.remark : info.name,
hash: info.hash,
portraitUrl: info.localAvatar ? info.localAvatar : require('@/static/user/cover.png')
}
} else {
return {
name: '',
hash: '',
portraitUrl: require('@/static/user/cover.png')
}
}
},
// 联系人是否存在
contactIsExist: (state) => (targetId) => {
return Boolean(state.contacts[targetId])
},
sender(state) {
return state.myInfo
}
},
mutations: {
updateContactInfo(state, contactInfo) {
Vue.set(state.contacts, contactInfo.targetId, contactInfo)
},
// 设置我的资料
setSenderInfo(state, contactInfo) {
state.myInfo = {
userId: contactInfo.targetId,
name: contactInfo.name,
portraitUrl: contactInfo.portraitUrl
}
},
setContactRemark(state, contactInfo) {
Vue.set(state.contacts, contactInfo.targetId, contactInfo)
}
},
actions: {
setContactRemark({
commit
}, {
targetId,
remark
}) {
contactModel.find('targetId="' + targetId + '"', (err, result) => {
if (!err && result.length > 0) {
result[0].remark = remark
contactModel.update('targetId="' + targetId + '"', result[0], (err, res) => {
commit('setContactRemark', result[0])
})
}
})
},
setSenderInfo({
commit,
dispatch
}, contactInfo) {
contactInfo.type = 0 // type 更改为0标记是我自己
dispatch('updateContact', contactInfo)
commit('setSenderInfo', contactInfo)
},
// 载入好友信息
launchContact({
commit
}, data) {
commit('updateContactInfo', data)
},
// 更新好友信息这个时候要校验hash值了
updateContact({
commit,
dispatch
}, contactInfo) {
contactModel.find('targetId="' + contactInfo.targetId + '"', (err, result) => {
if (result.length == 0) {
// 没有数据,直接新增一条
dispatch('initContact', contactInfo)
} else if (contactInfo.hash != result[0].hash) {
if (contactInfo.portraitUrl && contactInfo.portraitUrl != result[0].portraitUrl) {
saveAvatar(contactInfo, (savedFilePath) => {
const info = {
targetId: contactInfo.targetId,
name: contactInfo.name,
hash: contactInfo.hash,
type: contactInfo.type,
portraitUrl: contactInfo.portraitUrl,
localAvatar: savedFilePath
}
contactModel.update('targetId="' + contactInfo.targetId + '"', info, (err,
res) => {
console.log('UPDATE AVATAR, ERR', err, info)
})
commit('updateContactInfo', info)
})
} else {
const info = {
targetId: contactInfo.targetId,
name: contactInfo.name,
hash: contactInfo.hash,
type: contactInfo.type,
portraitUrl: contactInfo.portraitUrl,
localAvatar: result[0].localAvatar
}
contactModel.update('targetId="' + contactInfo.targetId + '"', info, (err, res) => {
console.log('UPDATE NAME, ERR', err, info);
})
commit('updateContactInfo', info)
}
} else {
console.log('updateContact, 无操作')
}
})
},
// 初始化好友信息
initContact({
commit
}, contactInfo) {
// 将好友信息保存到vuex的内存中方便立即使用
commit('updateContactInfo', contactInfo)
// 用户头像,是否需要下载到本地
if (contactInfo.portraitUrl) {
saveAvatar(contactInfo, (savedFilePath) => {
const info = {
targetId: contactInfo.targetId,
name: contactInfo.name,
hash: contactInfo.hash,
type: contactInfo.type,
portraitUrl: contactInfo.portraitUrl,
localAvatar: savedFilePath
}
contactModel.insert(info, (err, res) => {
console.error('保存头像', err, res)
})
// 保存头像后,更新信息
commit('updateContactInfo', info)
})
} else {
// 直接将信息,写入数据库
const info = {
targetId: contactInfo.targetId,
name: contactInfo.name,
hash: contactInfo.hash,
type: contactInfo.type,
portraitUrl: contactInfo.portraitUrl,
localAvatar: ''
}
contactModel.insert(info, (err, res) => {
console.error('没保存头像', err, res)
})
}
}
}
}
const saveAvatar = (contactInfo, callback) => {
uni.downloadFile({
url: contactInfo.portraitUrl,
success: ({
tempFilePath
}) => {
uni.saveFile({
tempFilePath: tempFilePath,
success: ({
savedFilePath
}) => {
callback(savedFilePath)
}
})
},
fail: (err) => {}
})
}