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

28
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,12 +30,19 @@ 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
uni.$sql.connect({ usqlite.connect({
name: 'zh-health',// 数据库名称 name: 'zh-health', // 数据库名称
path:'_doc/health.db', // 路径 path: '_doc/health.db', // 路径
}, (err, res) => {
uni.getStorage({
key: 'FIRST_RUN',
fail: () => {
contactModel.create((err, res) => {
console.error('SQLITE 创建表格', err, res)
})
}
})
}) })
App.mpType = 'app' App.mpType = 'app'

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: {
@@ -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)
@@ -75,13 +77,13 @@ export default {
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)
}) })
@@ -90,17 +92,17 @@ export default {
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,7 +112,6 @@ 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) => {
@@ -118,11 +119,13 @@ export default {
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)
}) })
@@ -132,11 +135,13 @@ export default {
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)
}) })

View File

@@ -21,6 +21,6 @@ const contactModel = usqlite.model('contacts', {
localAvatar: String localAvatar: String
}) })
export default { export {
contactModel contactModel
} }