用户资料更新规则,基础流程

This commit is contained in:
2022-02-08 17:04:35 +08:00
parent 03fdc9b9d0
commit 56c212d9ad
8 changed files with 179 additions and 134 deletions

View File

@@ -7,10 +7,17 @@
import store from '@/store' import store from '@/store'
import { import {
request request
} from '../index' } from '../index'
// 获取好友列表 // 获取好友列表
const getFriends = () => { const getFriends = () => {
return request({
url: 'im/friends',
}, true)
}
// 获取好友列表
const getFriendsLetter = () => {
return request({ return request({
url: 'im/friends/letter', url: 'im/friends/letter',
}, true) }, true)
@@ -140,7 +147,8 @@ const uploadMessageFile = (file, type) => {
export { export {
getImToken, getImToken,
deleteFriend, deleteFriend,
getFriends, getFriends,
getFriendsLetter,
getUserInfo, getUserInfo,
getFriendInfo, getFriendInfo,
getPedings, getPedings,

View File

@@ -14,28 +14,20 @@
</view> </view>
</view> </view>
<block v-if="friends.length > 0"> <block v-if="friends.length > 0">
<template v-for="(item, friend) in friends"> <template v-for="(item, fkey) in friends">
<!-- #ifdef APP-NVUE --> <!-- #ifdef APP-NVUE -->
<u-index-anchor :text="indexs[friend]" bgColor="#F3F6FB" height="20" size="12" color="#666"> <u-index-anchor :text="indexs[fkey]" bgColor="#F3F6FB" height="20" size="12" color="#666">
</u-index-anchor> </u-index-anchor>
<!-- #endif --> <!-- #endif -->
<u-index-item> <u-index-item>
<!-- #ifndef APP-NVUE --> <!-- #ifndef APP-NVUE -->
<u-index-anchor :text="indexs[friend]" bgColor="#F3F6FB" height="20" size="12" color="#666"> <u-index-anchor :text="indexs[fkey]" bgColor="#F3F6FB" height="20" size="12" color="#666">
</u-index-anchor> </u-index-anchor>
<!-- #endif --> <!-- #endif -->
<view v-for="(friendItem, index) in item" :key="friendItem.userId" <view v-for="(friendItem, index) in item" :key="friendItem.userId"
class="friend-flex u-border-bottom" class="friend-flex u-border-bottom" @click="toFriend(friendItem.userId)">
@click="$Router.push({ name: 'imFriendsInfo', params: { targetId: friendItem.userId } })"> <u-avatar size="40" shape="square" :src="friend(friendItem.userId).portraitUrl" />
<block v-if="friendItem.portraitUrl != ''"> <view class="name">{{ friend(friendItem.userId).name }}</view>
<u-avatar class="cover" size="40" shape="square" :src="friendItem.portraitUrl || ''"
:default-url="require('@/static/user/cover.png')"></u-avatar>
</block>
<block v-else>
<u-avatar class="cover" size="40" shape="square"
:src="require('@/static/user/cover.png')"></u-avatar>
</block>
<view class="name">{{ friendItem.name }}</view>
</view> </view>
</u-index-item> </u-index-item>
</template> </template>
@@ -50,7 +42,7 @@
<script> <script>
import { import {
getFriends, getFriendsLetter,
getPendingCount getPendingCount
} from '@/apis/interfaces/im'; } from '@/apis/interfaces/im';
@@ -62,12 +54,19 @@
pendingCount: 0 pendingCount: 0
}; };
}, },
onShow() { computed: {
getFriends().then(res => { friend() {
return function(targetId) {
return this.$store.getters.userInfo(targetId)
}
}
},
onLoad() {
getFriendsLetter().then(res => {
this.indexs = res.indexList this.indexs = res.indexList
this.friends = res.itemArr this.friends = res.itemArr
}) })
getPendingCount().then(res => { getPendingCount().then(res => {
console.log(res); console.log(res);
this.pendingCount = res this.pendingCount = res
}) })
@@ -79,6 +78,11 @@
icon: 'none' icon: 'none'
}); });
}, },
toFriend(targetId) {
uni.navigateTo({
url: '/pages/im/friends/info?targetId=' + targetId
})
},
// 新朋友 // 新朋友
toPending() { toPending() {
uni.navigateTo({ uni.navigateTo({
@@ -108,7 +112,6 @@
flex-direction: row; flex-direction: row;
align-items: center; align-items: center;
// .cover
.name { .name {
flex: 1; flex: 1;
padding-left: $padding; padding-left: $padding;

View File

@@ -2,7 +2,8 @@
<view class="content"> <view class="content">
<!-- 用户信息 --> <!-- 用户信息 -->
<view class="info-flex"> <view class="info-flex">
<u-avatar :src="userInfo.portraitUrl || require('@/static/user/cover.png')" shape="square" size="50" bg-color="#fff"></u-avatar> <u-avatar :src="userInfo.portraitUrl || require('@/static/user/cover.png')" shape="square" size="50"
bg-color="#fff"></u-avatar>
<view class="info-text"> <view class="info-text">
<view class="nickname">{{userInfo.name}}</view> <view class="nickname">{{userInfo.name}}</view>
<view class="address" @longpress="copyAddress">地址{{userInfo.address}}</view> <view class="address" @longpress="copyAddress">地址{{userInfo.address}}</view>
@@ -104,11 +105,12 @@
callShow: false callShow: false
} }
}, },
onLoad(e) { onLoad(e) {
console.log(e);
this.targetId = e.targetId this.targetId = e.targetId
getFriendInfo(e.targetId).then(res => { getFriendInfo(e.targetId).then(res => {
this.userInfo = res this.userInfo = res
// 获取到用户信息之后,去检查一下要不要更新
this.$store.dispatch('updateFriend', res)
uni.setNavigationBarTitle({ uni.setNavigationBarTitle({
title: res.name title: res.name
}) })
@@ -313,14 +315,16 @@
background: white; background: white;
margin: $margin; margin: $margin;
border-radius: $radius; border-radius: $radius;
.u-border-bottom{
.u-border-bottom {
border-bottom: solid 1rpx #f9f9f9 !important; border-bottom: solid 1rpx #f9f9f9 !important;
} }
.item { .item {
line-height: 100rpx; line-height: 100rpx;
display: flex; display: flex;
align-items: center; align-items: center;
padding:10rpx $padding; padding: 10rpx $padding;
justify-content: space-between; justify-content: space-between;
font-size: $title-size-lg; font-size: $title-size-lg;

View File

@@ -40,8 +40,7 @@
<view class="avatar" @click="toFriend(item.targetId)"> <view class="avatar" @click="toFriend(item.targetId)">
<u-badge numberType="ellipsis" max="99" shape="horn" absolute :offset="[-5, -5]" <u-badge numberType="ellipsis" max="99" shape="horn" absolute :offset="[-5, -5]"
:value="item.unreadMessageCount" /> :value="item.unreadMessageCount" />
<u-avatar :src="friend(item.targetId).portraitUrl || require('@/static/user/cover.png')" <u-avatar :src="friend(item.targetId).portraitUrl" shape="square" size="44" />
shape="square" size="44" />
</view> </view>
<view class="content u-border-bottom"> <view class="content u-border-bottom">
<view class="header"> <view class="header">
@@ -125,7 +124,7 @@
// todo ,不知道这个获取未读数量的,有没有办法能获取私聊的,还有群组的 // todo ,不知道这个获取未读数量的,有没有办法能获取私聊的,还有群组的
// RongIMLib.getTotalUnreadCount(({ // RongIMLib.getTotalUnreadCount(({
// count // count
// }) => { // }) => {
// this.privateUnread = count // this.privateUnread = count
// }) // })
}) })

View File

@@ -41,7 +41,8 @@
</view> </view>
</view> </view>
<!-- 弹出层 --> <!-- 弹出层 -->
<sent-popups :show="showPopups" :conversationType="conversationType" :targetId="targetId" @success="() => {showPopups = false, getMessageList()}"></sent-popups> <sent-popups :show="showPopups" :conversationType="conversationType" :targetId="targetId"
@success="() => {showPopups = false, getMessageList()}"></sent-popups>
</view> </view>
</template> </template>
@@ -89,6 +90,7 @@
this.conversationType = e.conversationType // 会话类型 this.conversationType = e.conversationType // 会话类型
this.userInfo = this.$store.getters.userInfo(this.targetId) this.userInfo = this.$store.getters.userInfo(this.targetId)
console.log(this.userInfo);
// 获取消息列表 // 获取消息列表
this.initMessageList() this.initMessageList()
@@ -147,13 +149,13 @@
}) })
}, },
// 滚动到底部 // 滚动到底部
scrollBottom(type) { scrollBottom(type) {
if(type === 'msgPopups'){ if (type === 'msgPopups') {
this.showPopups = !this.showPopups this.showPopups = !this.showPopups
} }
setTimeout(() => { setTimeout(() => {
let el = this.$refs.chatBottom let el = this.$refs.chatBottom
ChatList.scrollToElement(el, { ChatList.scrollToElement(el, {
offset: 0, offset: 0,
animated: false animated: false
}) })
@@ -168,14 +170,18 @@
.chat { .chat {
background: $window-color; background: $window-color;
flex: 1; flex: 1;
.body {
flex: 1; .body {
.cell { flex: 1;
.cell {
padding: 10rpx 30rpx; padding: 10rpx 30rpx;
.time { .time {
justify-content: center; justify-content: center;
align-items: center; align-items: center;
padding-bottom: 20rpx; padding-bottom: 20rpx;
.text { .text {
background: #fff; background: #fff;
font-size: 24rpx; font-size: 24rpx;
@@ -236,10 +242,12 @@
padding: 20rpx 30rpx; padding: 20rpx 30rpx;
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
flex-direction: row; flex-direction: row;
.msg-type { .msg-type {
width: 70rpx; width: 70rpx;
height: 70rpx; height: 70rpx;
.icon { .icon {
margin: 5rpx; margin: 5rpx;
width: 60rpx; width: 60rpx;

View File

@@ -11,27 +11,27 @@ export default {
}, },
userInfo: (state) => (targetId) => { userInfo: (state) => (targetId) => {
if (state.friends[targetId]) { if (state.friends[targetId]) {
return state.friends[targetId] const info = state.friends[targetId]
} else {
console.log('没找到,死循环了,得处理 todo', targetId);
// im.syncUserInfo(targetId)
return { return {
userId: info.userId,
name: info.name,
portraitUrl: info.localAvatar ? info.localAvatar : require('@/static/user/cover.png')
}
} else {
return {
userId: '',
name: '', name: '',
address: '',
hash: '',
portraitUrl: '' portraitUrl: ''
} }
} }
}, },
hasUser: (state) => (targetId) => {
return Boolean(state.friends[targetId])
},
sender(state) { sender(state) {
return state.sender return state.sender
} }
}, },
mutations: { mutations: {
updateFriends(state, userInfo) { updateFriendInfo(state, userInfo) {
Vue.set(state.friends, userInfo.userId, userInfo) Vue.set(state.friends, userInfo.userId, userInfo)
}, },
SET_state_sender(state, userInfo) { SET_state_sender(state, userInfo) {
@@ -44,49 +44,79 @@ export default {
}, userInfo) { }, userInfo) {
commit('SET_state_sender', userInfo) commit('SET_state_sender', userInfo)
}, },
updateFriends({ // 载入好友信息
launchFriend({
commit
}, data) {
commit('updateFriendInfo', data)
},
// 更新好友信息这个时候要校验hash值了
updateFriend({
commit commit
}, userInfo) { }, userInfo) {
commit('updateFriends', userInfo)
const model = uni.model.friendModel const model = uni.model.friendModel
model.find('userId=' + userInfo.userId, (err, user) => { model.find('userId=' + userInfo.userId, (err, result) => {
if (!err && user.length == 0) { if (userInfo.hash != result[0].hash) {
saveAvatar(userInfo, (savedFilePath) => { commit('updateFriendInfo', userInfo)
model.insert({ if (userInfo.portraitUrl && userInfo.portraitUrl != result[0].portraitUrl) {
saveAvatar(userInfo, (savedFilePath) => {
const info = {
userId: userInfo.userId,
name: userInfo.name,
hash: userInfo.hash,
portraitUrl: userInfo.portraitUrl,
localAvatar: savedFilePath
}
model.update('userId=' + userInfo.userId, info)
commit('updateFriendInfo', info)
})
} else {
const info = {
userId: userInfo.userId, userId: userInfo.userId,
name: userInfo.name, name: userInfo.name,
hash: userInfo.hash, hash: userInfo.hash,
address: userInfo.address, portraitUrl: userInfo.portraitUrl,
portraitUrl: savedFilePath, localAvatar: ''
}, (err, result) => {}) }
userInfo.portraitUrl = savedFilePath model.update('userId=' + userInfo.userId, info)
commit('updateFriends', userInfo) }
})
} else if (!err && user[0].hash != userInfo.hash) {
saveAvatar(userInfo, (savedFilePath) => {
model.update('userId=' + userInfo.userId, {
name: userInfo.name,
hash: userInfo.hash,
portraitUrl: savedFilePath,
}, (err, result) => {})
userInfo.portraitUrl = savedFilePath
commit('updateFriends', userInfo)
})
} else if (!err && user[0].portraitUrl.length > 50) {
saveAvatar(userInfo, (savedFilePath) => {
model.update('userId=' + userInfo.userId, {
name: userInfo.name,
hash: userInfo.hash,
portraitUrl: savedFilePath,
}, (err, result) => {})
userInfo.portraitUrl = savedFilePath
commit('updateFriends', userInfo)
})
} else { } else {
console.log('不用操作', user[0]); console.log('不用更新的用户', userInfo.userId, userInfo.name);
} }
}) })
},
// 初始化好友信息
initFriend({
commit
}, userInfo) {
// 将好友信息保存到vuex的内存中方便立即使用
commit('updateFriendInfo', userInfo)
const model = uni.model.friendModel
// 用户头像,是否需要下载到本地
if (userInfo.portraitUrl) {
saveAvatar(userInfo, (savedFilePath) => {
const info = {
userId: userInfo.userId,
name: userInfo.name,
hash: userInfo.hash,
portraitUrl: userInfo.portraitUrl,
localAvatar: savedFilePath
}
model.insert(info)
// 保存头像后,更新信息
commit('updateFriendInfo', info)
})
} else {
// 直接将信息,写入数据库
const info = {
userId: userInfo.userId,
name: userInfo.name,
hash: userInfo.hash,
portraitUrl: userInfo.portraitUrl,
localAvatar: ''
}
model.insert(info)
}
} }
} }
} }

View File

@@ -51,21 +51,38 @@ const setNotifyBadge = () => {
*/ */
const connect = (token, userInfo, callback) => { const connect = (token, userInfo, callback) => {
RongIMLib.connect(token, res => { RongIMLib.connect(token, res => {
console.log('连接结果', res);
callback(res) callback(res)
}) // 更新个人信息
store.dispatch('setSenderInfo', userInfo)
// 设置未读消息数量
setNotifyBadge()
// 首次运行获取好友列表
const FK = 'IFT_' + userInfo.userId
store.dispatch('setSenderInfo', userInfo) const model = uni.model.friendModel
model.find((err, results) => {
console.log('SQL 全部数据', results);
})
setNotifyBadge() uni.getStorage({
key: FK,
const model = uni.model.friendModel success: () => {
const model = uni.model.friendModel
model.find((err, results) => { model.find((err, results) => {
console.log('好友列表', results); results.map(item => {
results.map(item => { store.dispatch('launchFriend', item)
store.dispatch('updateFriends', item) })
})
},
fail: () => {
// 程序是首次运行,初始化加载好友信息
getFriends().then(res => {
res.map(item => {
store.dispatch('initFriend', item)
})
uni.setStorageSync(FK, userInfo.userId)
})
}
}) })
}) })
} }
@@ -122,15 +139,15 @@ const addListeners = () => {
console.log('new Message'); console.log('new Message');
newMessage(message) newMessage(message)
} }
}) })
// 监听消息回执 // 监听消息回执
RongIMLib.addReadReceiptReceivedListener(({ RongIMLib.addReadReceiptReceivedListener(({
data data
}) => { }) => {
uni.$emit('onReadReceiptReceived', data) uni.$emit('onReadReceiptReceived', data)
}) })
// 音视频通话相关的 // 音视频通话相关的
// 监听通话呼入 // 监听通话呼入
CallLib.onCallReceived(({ CallLib.onCallReceived(({
@@ -184,19 +201,9 @@ const newMessage = (msg) => {
setNotifyBadge() setNotifyBadge()
if (!store.getters.hasUser(msg.targetId)) {
syncUserInfo(msg.targetId)
}
uni.$emit('onReceiveMessage', msg); uni.$emit('onReceiveMessage', msg);
} }
function syncUserInfo(targetId) {
getUserInfo(targetId).then(res => {
store.dispatch('updateFriends', res)
})
}
// 播放状态 // 播放状态
let tipState = false let tipState = false
@@ -209,29 +216,15 @@ const triTone = () => {
tipState = true tipState = true
}) })
innerAudioContext.onEnded(() => { innerAudioContext.onEnded(() => {
tipState = false tipState = false
innerAudioContext.destroy() innerAudioContext.destroy()
}) })
} }
} }
/**
* 同步好友信息,保存头像地址等
*/
const syncFriends = () => {
getFriends().then(res => {
res.map(item => {
console.log('item', item);
store.dispatch('updateFriends', item)
})
})
}
export default { export default {
initIm, initIm,
connect, connect,
setNotifyBadge, setNotifyBadge,
syncFriends,
syncUserInfo,
...message ...message
} }

View File

@@ -9,12 +9,12 @@ const friendModel = usqlite.model('friends', {
unique: true unique: true
}, },
name: String, name: String,
address: String,
hash: { hash: {
type: String, type: String,
unique: true unique: true
}, },
portraitUrl: String portraitUrl: String,
localAvatar: String
}) })
export default { export default {