uni-app 153读取朋友圈动态功能
Posted 2019ab
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uni-app 153读取朋友圈动态功能相关的知识,希望对你有一定的参考价值。
下图是我测试的截图
/pages/tabbar/find/find.vue
<template>
<view class="page">
<!-- 导航栏 -->
<free-nav-bar title="发现"></free-nav-bar>
<free-list-item title="朋友圈" showRight @click="openMoments">
<text slot='icon' class="iconfont font-lg py-1"></text>
<view slot="right" class="position-relative p-1">
<free-avatar src="/static/images/mail/friend.png" size="55" ></free-avatar>
<text class="rounded-circle bg-danger position-absolute" style="width: 20rpx;height: 20rpx;top: 0;right: 0;"></text>
</view>
</free-list-item>
<free-divider></free-divider>
<free-list-item title="朋友圈" showRight>
<text slot='icon' class="iconfont font-lg py-1"></text>
<view slot="right" class="position-relative p-1"></view>
</free-list-item>
<free-list-item title="朋友圈" showRight>
<text slot='icon' class="iconfont font-lg py-1"></text>
<view slot="right" class="position-relative p-1"></view>
</free-list-item>
<free-divider></free-divider>
<free-list-item title="朋友圈" showRight>
<text slot='icon' class="iconfont font-lg py-1"></text>
<view slot="right" class="position-relative p-1"></view>
</free-list-item>
<free-list-item title="朋友圈" showRight>
<text slot='icon' class="iconfont font-lg py-1"></text>
<view slot="right" class="position-relative p-1"></view>
</free-list-item>
<free-divider></free-divider>
<free-list-item title="朋友圈" showRight>
<text slot='icon' class="iconfont font-lg py-1"></text>
<view slot="right" class="position-relative p-1"></view>
</free-list-item>
<free-divider></free-divider>
</view>
</template>
<script>
import freeNavBar from '@/components/free-ui/free-nav-bar.vue';
import freeListItem from '@/components/free-ui/free-list-item.vue';
import freeAvatar from "@/components/free-ui/free-avatar.vue";
import freeDivider from "@/components/free-ui/free-divider.vue";
import auth from '@/common/mixin/auth.js';
import mapState from 'vuex';
export default
mixins:[auth],
components:
freeNavBar,
freeListItem,
freeAvatar,
freeDivider
,
data()
return
,
computed:
...mapState(
notice:state=>state.user.notice,
chat:state=>state.user.chat
)
,
methods:
// 打开朋友圈
openMoments()
uni.navigateTo(
url: '../../find/moments/moments',
);
this.chat.readMoments();
</script>
<style>
</style>
/common/free-lib/chat.js
import $U from "./util.js";
import $H from './request.js';
import $store from '@/store/index.js';
class chat
constructor(arg)
this.url = arg.url
this.isOnline = false
this.socket = null
// 获取当前用户相关信息
let user = $U.getStorage('user');
this.user = user ? JSON.parse(user) : ,
// 初始化聊天对象
this.TO = false;
// 连接和监听
if (this.user.token)
this.connectSocket()
// 连接socket
connectSocket()
console.log(this.user);
this.socket = uni.connectSocket(
url: this.url + '?token=' + this.user.token,
complete: () =>
)
// 监听连接成功
this.socket.onOpen(() => this.onOpen())
// 监听接收信息
this.socket.onMessage((res) => this.onMessage(res))
// 监听断开
this.socket.onClose(() => this.onClose())
// 监听错误
this.socket.onError(() => this.onError())
// 监听打开
onOpen()
// 用户状态上线
this.isOnline = true;
console.log('socket连接成功');
// 获取用户离线消息
this.getMessage();
// 获取离线消息
getMessage()
$H.post('/chat/getmessage');
// 获取聊天记录
getChatDetail(key = false)
key = key ? key : `chatDetail_$this.user.id_$this.TO.chat_type_$this.TO.id`
return this.getStorage(key)
// 监听关闭
onClose()
// 用户下线
this.isOnline = false;
this.socket = null;
console.log('socket连接关闭');
// 监听消息
onMessage(data)
console.log('监听消息', data);
let res = JSON.parse(data.data)
// console.log('监听接收消息',res)
// 错误
switch (res.msg)
case 'fail':
return uni.showToast(
title: res.data,
icon: 'none'
);
break;
case 'recall': // 撤回消息
this.handleOnRecall(res.data)
break;
case 'updateApplyList': // 新的好友申请
$store.dispatch('getApply');
break;
case 'moment': // 朋友圈更新
this.handleMoment(res.data)
break;
default:
// 处理消息
this.handleOnMessage(res.data)
break;
// 处理朋友圈通知
async handleMoment(message)
let notice = $U.getStorage('moment_'+this.user.id);
notice = notice ? JSON.parse(notice) :
avatar:'',
user_id:0,
num:0
switch(message.type)
case 'new':
if(message.user_id !== this.user.id)
notice.avatar = message.avatar;
notice.user_id = message.user_id;
uni.showTabBarRedDot(
index:notice
)
break;
default:
break;
uni.$emit('momentNotice',notice);
$U.setStorage('moment_'+this.user.id,JSON.stringify(notice));
// 读取朋友圈动态
async readMoments()
let notice =
avatar:'',
user_id:0,
num:0
;
$U.setStorage('moment_'+this.user.id,JSON.stringify(notice));
uni.hideTabBarRedDot(
index:2
)
uni.removeTabBarBadge(
index:2
)
uni.$emit('momentNotice',notice);
// 监听撤回消息处理
async handleOnRecall(message)
// 通知聊天页撤回消息
uni.$emit('onMessage',
...message,
isremove: 1
)
// 修改聊天记录
let id = message.chat_type === 'group' ? message.to_id : message.from_id
// key值:chatDetail_当前用户id_会话类型_接收人/群id
let key = `chatDetail_$this.user.id_$message.chat_type_$id`
// 获取原来的聊天记录
let list = this.getChatDetail(key)
// 根据k查找对应聊天记录
let index = list.findIndex(item => item.id === message.id)
if (index === -1) return;
list[index].isremove = 1
// 存储
this.setStorage(key, list)
// 当前会话最后一条消息的显示
this.updateChatItem(
id,
chat_type: message.chat_type
, (item) =>
item.data = '对方撤回了一条消息'
item.update_time = (new Date()).getTime()
return item
)
// 处理消息
async handleOnMessage(message)
// 添加消息记录到本地存储中
let
data
= this.addChatDetail(message, false)
// 更新会话列表
this.updateChatList(data, false)
// 全局通知
uni.$emit('onMessage', data)
// 消息提示
// this.messageNotice()
// 监听连接错误
onError()
// 用户下线
this.isOnline = false;
this.socket = null;
console.log('socket连接错误');
// 关闭连接
close()
this.socket.close()
// 创建聊天对象
createChatObject(detail)
this.TO = detail;
console.log('创建聊天对象', this.TO)
// 销毁聊天对象
destoryChatObject()
this.TO = false
// 组织发送信息格式
formatSendData(params)
return
id: 0, // 唯一id,后端生成,用于撤回指定消息
from_avatar: this.user.avatar, // 发送者头像
from_name: this.user.nickname || this.user.username, // 发送者昵称
from_id: this.user.id, // 发送者id
to_id: params.to_id || this.TO.id, // 接收人/群 id
to_name: params.to_name || this.TO.name, // 接收人/群 名称
to_avatar: params.to_avatar || this.TO.avatar, // 接收人/群 头像
chat_type: params.chat_type || this.TO.chat_type, // 接收类型
type: params.type, // 消息类型
data: params.data, // 消息内容
options: params.options ? params.options : , // 其他参数
create_time: (new Date()).getTime(), // 创建时间
isremove: 0, // 是否撤回
sendStatus: params.sendStatus ? params.sendStatus : "pending" // 发送状态,success发送成功,fail发送失败,pending发送中
// 发送信息
send(message, onProgress = false)
return new Promise(async (result, reject) =>
// 添加消息历史记录
// this.addChatDetail();
let
k
= this.addChatDetail(message);
// 更新会话列表
this.updateChatList(message);
// 验证是否上线
if (!this.checkOnLine()) return reject('未上线');
// 上传文件
let isUpload = (message.type !== 'text' && message.type !== 'emoticon' && message.type !==
'card' && !message.data.startsWith('http://akyan.oss-cn-beijing.aliyuncs.com/'))
let uploadResult = ''
if (isUpload)
uploadResult = await $H.upload('/upload',
filePath: message.data
, onProgress);
// 提交到后端
let data = isUpload ? uploadResult : message.data;
$H.post('/chat/send',
to_id: this.TO.id,
type: message.type,
chat_type: this.TO.chat_type,
data,
options: JSON.stringify(message.options)
).then(res =>
// 发送成功
console.log('chat.js发送成功');
message.id = res.id
message.sendStatus = 'success';
if (message.type === 'video'uni-app 12.1设置朋友圈动态权限