头像本地缓存

This commit is contained in:
2022-01-19 14:13:18 +08:00
parent 070c6c729e
commit ced9257131
12 changed files with 1211 additions and 33 deletions

View File

@@ -1,7 +1,8 @@
import * as RongIMLib from '@rongcloud/imlib-uni'
import store from '@/store/index.js'
import {
getFriends
getFriends,
getUserInfo
} from '@/apis/interfaces/im.js'
const initIm = (KEY) => {
@@ -18,12 +19,12 @@ const setNotifyBadge = (count) => {
if (code === 0) {
if (count > 0) {
uni.setTabBarBadge({
index: 2,
index: 3,
text: String(count > 99 ? '99+' : count)
})
} else {
uni.removeTabBarBadge({
index: 2
index: 3
})
}
}
@@ -43,10 +44,18 @@ const connect = (token, userInfo) => {
store.dispatch('setSenderInfo', userInfo)
setNotifyBadge()
}
const disconnect = () => {
RongIMLib.disconnect()
const model = uni.model.friendModel
model.find((err, results) => {
results.map(item => {
store.dispatch('updateFriends', item)
})
})
}
const disconnect = () => {
RongIMLib.disconnect()
}
const addListeners = () => {
@@ -76,9 +85,19 @@ const newMessage = (msg) => {
setNotifyBadge()
if (!store.getters.hasUser(msg.targetId)) {
syncUserInfo(msg.targetId)
}
store.dispatch('newMessage', msg)
}
function syncUserInfo(targetId) {
getUserInfo(targetId).then(res => {
store.dispatch('updateFriends', res)
})
}
// 播放状态
let tipState = false
@@ -133,7 +152,10 @@ const sendMsg = (conversationType, targetId, content, callback) => {
*/
const syncFriends = () => {
getFriends().then(res => {
store.dispatch('updateFriends', res)
res.map(item => {
console.log('item', item);
store.dispatch('updateFriends', item)
})
})
}
@@ -142,5 +164,6 @@ export default {
connect,
sendMsg,
setNotifyBadge,
syncFriends
syncFriends,
syncUserInfo
}

22
utils/im/models.js Normal file
View File

@@ -0,0 +1,22 @@
import {
usqlite
} from '@/uni_modules/onemue-USQLite/js_sdk/usqlite.js'
const friendModel = usqlite.model('friends', {
userId: {
type: String,
primaryKey: true,
unique: true
},
name: String,
address: String,
hash: {
type: String,
unique: true
},
portraitUrl: String
})
export default {
friendModel
}

44
utils/im/sqlite.js Normal file
View File

@@ -0,0 +1,44 @@
const openSqlite = (callback) => {
plus.sqlite.openDatabase({
name: 'friends',
path: '_doc/friends.db',
success: (res) => {
callback(res)
},
fail: (err) => {
callback(err)
}
})
}
const executeSQL = (callback) => {
plus.sqlite.selectSql({
name: 'friends',
sql: sql,
success: (res) => {
callback(res)
},
fail: (err) => {
callback(err)
}
})
}
const closeSqlite = (callback) => {
plus.sqlite.closeDatabase({
name: 'friends',
path: '_doc/friends.db',
success: (res) => {
callback(res)
},
fail: (err) => {
callback(err)
}
})
}
export {
openSqlite,
executeSQL,
closeSqlite
}