From e0185e589d9d7bd145e29087c9560d2ac9f770dd Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 24 Feb 2022 17:51:59 +0800 Subject: [PATCH] =?UTF-8?q?SQLITE=20=E6=95=B0=E6=8D=AE=E5=BA=93=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E5=88=9B=E5=BB=BA=E6=97=B6=E6=9C=BA=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.js | 32 +++++++++------ pages/im/components/groupUserList.vue | 2 - store/modules/im.js | 57 +++++++++++++++------------ utils/im/index.js | 10 +++-- utils/im/models.js | 4 +- 5 files changed, 59 insertions(+), 46 deletions(-) diff --git a/main.js b/main.js index 2940461..36108b7 100644 --- a/main.js +++ b/main.js @@ -10,15 +10,16 @@ import Vue from 'vue' import store from './store' import uView from 'uview-ui' 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 { router, RouterMount } 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 => { Vue.filter(key, filters[key]) @@ -29,13 +30,20 @@ Vue.use(router) Vue.config.productionTip = false Vue.prototype.$store = store -uni.$sql = usqlite -uni.model = model - -uni.$sql.connect({ - name: 'zh-health',// 数据库名称 - path:'_doc/health.db', // 路径 -}) + +usqlite.connect({ + name: 'zh-health', // 数据库名称 + path: '_doc/health.db', // 路径 +}, (err, res) => { + uni.getStorage({ + key: 'FIRST_RUN', + fail: () => { + contactModel.create((err, res) => { + console.error('SQLITE 创建表格', err, res) + }) + } + }) +}) App.mpType = 'app' const app = new Vue({ diff --git a/pages/im/components/groupUserList.vue b/pages/im/components/groupUserList.vue index a4009aa..034f8cf 100644 --- a/pages/im/components/groupUserList.vue +++ b/pages/im/components/groupUserList.vue @@ -66,13 +66,11 @@ actionMap: [], actionTitle: '', currentUser: {}, - avatarSize: 40, labelSize: 14, iconSize: 14 } }, created() { - this.avatarSize = utils.rpx2px(84) this.labelSize = utils.rpx2px(24) this.iconSize = utils.rpx2px(26) }, diff --git a/store/modules/im.js b/store/modules/im.js index 770eb7e..ebb50f1 100644 --- a/store/modules/im.js +++ b/store/modules/im.js @@ -1,4 +1,7 @@ import im from "@/utils/im/index.js" +import { + contactModel +} from '@/utils/im/models.js' export default { state: { @@ -9,10 +12,10 @@ export default { contacts(state) { return state.contacts }, - contactInfo: (state) => (targetId) => { + contactInfo: (state) => (targetId) => { if (state.contacts[targetId]) { - const info = state.contacts[targetId] - + const info = state.contacts[targetId] + return { name: info.name, hash: info.hash, @@ -38,7 +41,7 @@ export default { updateContactInfo(state, contactInfo) { Vue.set(state.contacts, contactInfo.targetId, contactInfo) }, - setSenderInfo(state, contactInfo) { + setSenderInfo(state, contactInfo) { state.myInfo = { userId: contactInfo.targetId, name: contactInfo.name, @@ -50,7 +53,7 @@ export default { setSenderInfo({ commit }, contactInfo) { - commit('setSenderInfo', contactInfo) + commit('setSenderInfo', contactInfo) }, // 载入好友信息 launchContact({ @@ -63,8 +66,7 @@ export default { commit, dispatch }, contactInfo) { - const model = uni.model.contactModel - model.find('targetId="' + contactInfo.targetId + '"', (err, result) => { + contactModel.find('targetId="' + contactInfo.targetId + '"', (err, result) => { if (result.length == 0) { // 没有数据,直接新增一条 dispatch('initContact', contactInfo) @@ -74,14 +76,14 @@ export default { const info = { targetId: contactInfo.targetId, name: contactInfo.name, - hash: contactInfo.hash, + hash: contactInfo.hash, + type: contactInfo.type, portraitUrl: contactInfo.portraitUrl, - localAvatar: savedFilePath, - type: contactInfo.type + localAvatar: savedFilePath } - model.update('targetId="' + contactInfo.targetId + '"', info, (err, + contactModel.update('targetId="' + contactInfo.targetId + '"', info, (err, res) => { - console.log('UPDATE AVATAR, ERR', err, info); + console.log('UPDATE AVATAR, ERR', err, info) }) commit('updateContactInfo', info) }) @@ -89,18 +91,18 @@ export default { const info = { targetId: contactInfo.targetId, name: contactInfo.name, - hash: contactInfo.hash, + hash: contactInfo.hash, + type: contactInfo.type, portraitUrl: contactInfo.portraitUrl, - localAvatar: result[0].localAvatar, - type: contactInfo.type + localAvatar: result[0].localAvatar } - model.update('targetId="' + contactInfo.targetId + '"', info, (err, res) => { + contactModel.update('targetId="' + contactInfo.targetId + '"', info, (err, res) => { console.log('UPDATE NAME, ERR', err, info); }) commit('updateContactInfo', info) } } else { - console.log('updateContact, 无操作'); + console.log('updateContact, 无操作') } }) }, @@ -110,19 +112,20 @@ export default { }, contactInfo) { // 将好友信息保存到vuex的内存中,方便立即使用 commit('updateContactInfo', contactInfo) - const model = uni.model.contactModel // 用户头像,是否需要下载到本地 if (contactInfo.portraitUrl) { saveAvatar(contactInfo, (savedFilePath) => { const info = { targetId: contactInfo.targetId, name: contactInfo.name, - hash: contactInfo.hash, + hash: contactInfo.hash, + type: contactInfo.type, portraitUrl: contactInfo.portraitUrl, - localAvatar: savedFilePath, - type: contactInfo.type + localAvatar: savedFilePath } - model.insert(info, (err, res) => {}) + contactModel.insert(info, (err, res) => { + console.error('保存头像', err, res) + }) // 保存头像后,更新信息 commit('updateContactInfo', info) }) @@ -131,12 +134,14 @@ export default { const info = { targetId: contactInfo.targetId, name: contactInfo.name, - hash: contactInfo.hash, + hash: contactInfo.hash, + type: contactInfo.type, portraitUrl: contactInfo.portraitUrl, - localAvatar: '', - type: contactInfo.type + localAvatar: '' } - model.insert(info, (err, res) => {}) + contactModel.insert(info, (err, res) => { + console.error('没保存头像', err, res) + }) } } } diff --git a/utils/im/index.js b/utils/im/index.js index ca8477c..3c66184 100644 --- a/utils/im/index.js +++ b/utils/im/index.js @@ -9,6 +9,9 @@ import { getImToken, getMyGroups } from '@/apis/interfaces/im.js' +import { + contactModel +} from './models.js' const initIm = (KEY) => { RongIMLib.init(KEY) @@ -62,13 +65,12 @@ const connect = (token, userInfo, callback) => { // 设置未读消息数量 setNotifyBadge() // 首次运行获取好友列表 - const FK = 'ZH_V_' + userInfo.targetId + const FK = 'ZH_CONTACT_' + userInfo.targetId uni.getStorage({ key: FK, success: () => { - const model = uni.model.contactModel - model.find((err, results) => { + contactModel.find((err, results) => { results.map(item => { store.dispatch('launchContact', item) }) @@ -81,7 +83,7 @@ const connect = (token, userInfo, callback) => { contacts.map(item => { store.dispatch('initContact', item) }) - }) + }) uni.setStorageSync(FK, userInfo.targetId) }) } diff --git a/utils/im/models.js b/utils/im/models.js index e7d4031..9220b98 100644 --- a/utils/im/models.js +++ b/utils/im/models.js @@ -1,7 +1,7 @@ import { usqlite } from '@/uni_modules/onemue-USQLite/js_sdk/usqlite.js' - + const contactModel = usqlite.model('contacts', { targetId: { type: String, @@ -21,6 +21,6 @@ const contactModel = usqlite.model('contacts', { localAvatar: String }) -export default { +export { contactModel }