SQLITE 数据库优化,创建时机调整

This commit is contained in:
2022-02-24 17:51:59 +08:00
parent 90584f1101
commit e0185e589d
5 changed files with 59 additions and 46 deletions

32
main.js
View File

@@ -10,15 +10,16 @@ import Vue from 'vue'
import store from './store' import store from './store'
import uView from 'uview-ui' import uView from 'uview-ui'
import filters from './utils/filters.js' import filters from './utils/filters.js'
import {
usqlite
} from '@/uni_modules/onemue-USQLite/js_sdk/usqlite.js'
import model from '@/utils/im/models.js'
import { import {
router, router,
RouterMount RouterMount
} from 'router' } from 'router'
import {
usqlite
} from '@/uni_modules/onemue-USQLite/js_sdk/usqlite.js'
import {
contactModel
} from '@/utils/im/models.js'
Object.keys(filters).forEach(key => { Object.keys(filters).forEach(key => {
Vue.filter(key, filters[key]) Vue.filter(key, filters[key])
@@ -29,13 +30,20 @@ Vue.use(router)
Vue.config.productionTip = false Vue.config.productionTip = false
Vue.prototype.$store = store Vue.prototype.$store = store
uni.$sql = usqlite
uni.model = model usqlite.connect({
name: 'zh-health', // 数据库名称
uni.$sql.connect({ path: '_doc/health.db', // 路径
name: 'zh-health',// 数据库名称 }, (err, res) => {
path:'_doc/health.db', // 路径 uni.getStorage({
}) key: 'FIRST_RUN',
fail: () => {
contactModel.create((err, res) => {
console.error('SQLITE 创建表格', err, res)
})
}
})
})
App.mpType = 'app' App.mpType = 'app'
const app = new Vue({ const app = new Vue({

View File

@@ -66,13 +66,11 @@
actionMap: [], actionMap: [],
actionTitle: '', actionTitle: '',
currentUser: {}, currentUser: {},
avatarSize: 40,
labelSize: 14, labelSize: 14,
iconSize: 14 iconSize: 14
} }
}, },
created() { created() {
this.avatarSize = utils.rpx2px(84)
this.labelSize = utils.rpx2px(24) this.labelSize = utils.rpx2px(24)
this.iconSize = utils.rpx2px(26) this.iconSize = utils.rpx2px(26)
}, },

View File

@@ -1,4 +1,7 @@
import im from "@/utils/im/index.js" import im from "@/utils/im/index.js"
import {
contactModel
} from '@/utils/im/models.js'
export default { export default {
state: { state: {
@@ -9,10 +12,10 @@ export default {
contacts(state) { contacts(state) {
return state.contacts return state.contacts
}, },
contactInfo: (state) => (targetId) => { contactInfo: (state) => (targetId) => {
if (state.contacts[targetId]) { if (state.contacts[targetId]) {
const info = state.contacts[targetId] const info = state.contacts[targetId]
return { return {
name: info.name, name: info.name,
hash: info.hash, hash: info.hash,
@@ -38,7 +41,7 @@ export default {
updateContactInfo(state, contactInfo) { updateContactInfo(state, contactInfo) {
Vue.set(state.contacts, contactInfo.targetId, contactInfo) Vue.set(state.contacts, contactInfo.targetId, contactInfo)
}, },
setSenderInfo(state, contactInfo) { setSenderInfo(state, contactInfo) {
state.myInfo = { state.myInfo = {
userId: contactInfo.targetId, userId: contactInfo.targetId,
name: contactInfo.name, name: contactInfo.name,
@@ -50,7 +53,7 @@ export default {
setSenderInfo({ setSenderInfo({
commit commit
}, contactInfo) { }, contactInfo) {
commit('setSenderInfo', contactInfo) commit('setSenderInfo', contactInfo)
}, },
// 载入好友信息 // 载入好友信息
launchContact({ launchContact({
@@ -63,8 +66,7 @@ export default {
commit, commit,
dispatch dispatch
}, contactInfo) { }, contactInfo) {
const model = uni.model.contactModel contactModel.find('targetId="' + contactInfo.targetId + '"', (err, result) => {
model.find('targetId="' + contactInfo.targetId + '"', (err, result) => {
if (result.length == 0) { if (result.length == 0) {
// 没有数据,直接新增一条 // 没有数据,直接新增一条
dispatch('initContact', contactInfo) dispatch('initContact', contactInfo)
@@ -74,14 +76,14 @@ export default {
const info = { const info = {
targetId: contactInfo.targetId, targetId: contactInfo.targetId,
name: contactInfo.name, name: contactInfo.name,
hash: contactInfo.hash, hash: contactInfo.hash,
type: contactInfo.type,
portraitUrl: contactInfo.portraitUrl, portraitUrl: contactInfo.portraitUrl,
localAvatar: savedFilePath, localAvatar: savedFilePath
type: contactInfo.type
} }
model.update('targetId="' + contactInfo.targetId + '"', info, (err, contactModel.update('targetId="' + contactInfo.targetId + '"', info, (err,
res) => { res) => {
console.log('UPDATE AVATAR, ERR', err, info); console.log('UPDATE AVATAR, ERR', err, info)
}) })
commit('updateContactInfo', info) commit('updateContactInfo', info)
}) })
@@ -89,18 +91,18 @@ export default {
const info = { const info = {
targetId: contactInfo.targetId, targetId: contactInfo.targetId,
name: contactInfo.name, name: contactInfo.name,
hash: contactInfo.hash, hash: contactInfo.hash,
type: contactInfo.type,
portraitUrl: contactInfo.portraitUrl, portraitUrl: contactInfo.portraitUrl,
localAvatar: result[0].localAvatar, localAvatar: result[0].localAvatar
type: contactInfo.type
} }
model.update('targetId="' + contactInfo.targetId + '"', info, (err, res) => { contactModel.update('targetId="' + contactInfo.targetId + '"', info, (err, res) => {
console.log('UPDATE NAME, ERR', err, info); console.log('UPDATE NAME, ERR', err, info);
}) })
commit('updateContactInfo', info) commit('updateContactInfo', info)
} }
} else { } else {
console.log('updateContact, 无操作'); console.log('updateContact, 无操作')
} }
}) })
}, },
@@ -110,19 +112,20 @@ export default {
}, contactInfo) { }, contactInfo) {
// 将好友信息保存到vuex的内存中方便立即使用 // 将好友信息保存到vuex的内存中方便立即使用
commit('updateContactInfo', contactInfo) commit('updateContactInfo', contactInfo)
const model = uni.model.contactModel
// 用户头像,是否需要下载到本地 // 用户头像,是否需要下载到本地
if (contactInfo.portraitUrl) { if (contactInfo.portraitUrl) {
saveAvatar(contactInfo, (savedFilePath) => { saveAvatar(contactInfo, (savedFilePath) => {
const info = { const info = {
targetId: contactInfo.targetId, targetId: contactInfo.targetId,
name: contactInfo.name, name: contactInfo.name,
hash: contactInfo.hash, hash: contactInfo.hash,
type: contactInfo.type,
portraitUrl: contactInfo.portraitUrl, portraitUrl: contactInfo.portraitUrl,
localAvatar: savedFilePath, localAvatar: savedFilePath
type: contactInfo.type
} }
model.insert(info, (err, res) => {}) contactModel.insert(info, (err, res) => {
console.error('保存头像', err, res)
})
// 保存头像后,更新信息 // 保存头像后,更新信息
commit('updateContactInfo', info) commit('updateContactInfo', info)
}) })
@@ -131,12 +134,14 @@ export default {
const info = { const info = {
targetId: contactInfo.targetId, targetId: contactInfo.targetId,
name: contactInfo.name, name: contactInfo.name,
hash: contactInfo.hash, hash: contactInfo.hash,
type: contactInfo.type,
portraitUrl: contactInfo.portraitUrl, portraitUrl: contactInfo.portraitUrl,
localAvatar: '', localAvatar: ''
type: contactInfo.type
} }
model.insert(info, (err, res) => {}) contactModel.insert(info, (err, res) => {
console.error('没保存头像', err, res)
})
} }
} }
} }

View File

@@ -9,6 +9,9 @@ import {
getImToken, getImToken,
getMyGroups getMyGroups
} from '@/apis/interfaces/im.js' } from '@/apis/interfaces/im.js'
import {
contactModel
} from './models.js'
const initIm = (KEY) => { const initIm = (KEY) => {
RongIMLib.init(KEY) RongIMLib.init(KEY)
@@ -62,13 +65,12 @@ const connect = (token, userInfo, callback) => {
// 设置未读消息数量 // 设置未读消息数量
setNotifyBadge() setNotifyBadge()
// 首次运行获取好友列表 // 首次运行获取好友列表
const FK = 'ZH_V_' + userInfo.targetId const FK = 'ZH_CONTACT_' + userInfo.targetId
uni.getStorage({ uni.getStorage({
key: FK, key: FK,
success: () => { success: () => {
const model = uni.model.contactModel contactModel.find((err, results) => {
model.find((err, results) => {
results.map(item => { results.map(item => {
store.dispatch('launchContact', item) store.dispatch('launchContact', item)
}) })
@@ -81,7 +83,7 @@ const connect = (token, userInfo, callback) => {
contacts.map(item => { contacts.map(item => {
store.dispatch('initContact', item) store.dispatch('initContact', item)
}) })
}) })
uni.setStorageSync(FK, userInfo.targetId) uni.setStorageSync(FK, userInfo.targetId)
}) })
} }

View File

@@ -1,7 +1,7 @@
import { import {
usqlite usqlite
} from '@/uni_modules/onemue-USQLite/js_sdk/usqlite.js' } from '@/uni_modules/onemue-USQLite/js_sdk/usqlite.js'
const contactModel = usqlite.model('contacts', { const contactModel = usqlite.model('contacts', {
targetId: { targetId: {
type: String, type: String,
@@ -21,6 +21,6 @@ const contactModel = usqlite.model('contacts', {
localAvatar: String localAvatar: String
}) })
export default { export {
contactModel contactModel
} }