创建虚拟人物,调整登录逻辑

This commit is contained in:
唐明明
2022-06-07 17:34:05 +08:00
parent 1c6091371e
commit b76167eb0d
19 changed files with 4434 additions and 4784 deletions

View File

@@ -10,32 +10,33 @@ import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
token: uni.getStorageSync('token') || '',
address: {},
refresh: 0
},
getters: {
getToken: state => {
return state.token
},
getAddress: state => {
return state.address
},
getRefresh: state => {
return state.refresh
}
},
mutations: {
setToken(state, tokenString) {
state.token = tokenString
uni.setStorageSync('token', tokenString)
},
setAddress(state, value) {
state.address = value
},
setRefresh(state, value) {
state.refresh = value
}
}
state: {
token: uni.getStorageSync('token') || '',
address: {},
isnew: uni.getStorageSync('isNew') || 0
},
getters: {
getToken: state => {
return state.token
},
getAddress: state => {
return state.address
},
getIsNew: state => {
return state.isnew
}
},
mutations: {
setToken(state, tokenString) {
state.token = tokenString
uni.setStorageSync('token', tokenString)
},
setAddress(state, value) {
state.address = value
},
setIsNew(state, isNew) {
state.isnew = isNew
uni.setStorageSync('isnew', isNew)
}
}
})

View File

@@ -1,189 +0,0 @@
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) => {}
})
}