加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
App.vue 17.87 KB
一键复制 编辑 原始数据 按行查看 历史
大师兄 提交于 2021-12-04 22:58 . commitMessages1
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
<script>
import Vue from 'vue'
let WebIM = wx.WebIM = require("./utils/WebIM")["default"];
let msgStorage = require("./pages/user-chat/msgstorage");
let msgType = require("./pages/user-chat/msgtype");
let disp = require("./utils/broadcast");
// let emedia = uni.emedia = require("./emediaSDK/emedia_for_miniProgram")
// emedia.config({useUniappPlugin: true})
let logout = false;
function ack(receiveMsg) {
// 处理未读消息回执
var bodyId = receiveMsg.id; // 需要发送已读回执的消息id
var ackMsg = new WebIM.message("read", WebIM.conn.getUniqueId());
ackMsg.set({
id: bodyId,
to: receiveMsg.from
});
WebIM.conn.send(ackMsg.body);
}
function onMessageError(err) {
if (err.type === "error") {
uni.showToast({
title: err.errorText
});
return false;
}
return true;
}
function getCurrentRoute() {
let pages = getCurrentPages();
if (pages.length > 0) {
let currentPage = pages[pages.length - 1];
return currentPage.route;
}
return '/'
}
// // 不包含陌生人版本
// function calcUnReadSpot(message) {
// let userinfo = uni.getStorageSync("IMuserinfo");
// let myName = userinfo.username;
// let members = uni.getStorageSync("member") || []; //好友
// var listGroups = uni.getStorageSync("listGroup") || []; //群组
// let allMembers = members.concat(listGroups);
// let count = allMembers.reduce(function(result, curMember, idx) {
// let chatMsgs = [];
// if (curMember.groupid) {
// chatMsgs =
// uni.getStorageSync(curMember.groupid + myName.toLowerCase()) || [];
// } else {
// chatMsgs =
// uni.getStorageSync(
// curMember.name && curMember.name.toLowerCase() + myName.toLowerCase()
// ) || [];
// }
// console.log(result + chatMsgs.length);
// return result + chatMsgs.length;
// }, 0);
// getApp().globalData.unReadMessageNum = count;
// disp.fire("em.unreadspot", message);
// }
// 包含陌生人版本
function calcUnReadSpot(message){
let userinfo = uni.getStorageSync("IMuserinfo");
let myName =userinfo.username;
uni.getStorageInfo({
success: function(res){
let storageKeys = res.keys
let newChatMsgKeys = [];
let historyChatMsgKeys = [];
storageKeys.forEach((item) => {
if (item.indexOf(myName) > -1 && item.indexOf('rendered_') == -1) {
newChatMsgKeys.push(item)
}
})
let count = newChatMsgKeys.reduce(function(result, curMember, idx){
let chatMsgs;
chatMsgs = uni.getStorageSync(curMember) || [];
return result + chatMsgs.length;
}, 0)
getApp().globalData.unReadMessageNum = count;
uni.setStorageSync("unReadMessageNum",count);
console.log("未读消息数:"+count);
disp.fire("em.unreadspot", message);
console.log("ok----->1")
},
fail:function(err){
console.log(err);
}
})
}
function saveGroups() {
var me = this;
return WebIM.conn.getGroup({
limit: 50,
success: function(res) {
uni.setStorage({
key: "listGroup",
data: res.data
});
},
error: function(err) {
console.log(err);
}
});
}
export default {
globalData: {
unReadMessageNum: 0,
userInfo: null,
saveFriendList: [],
saveGroupInvitedList: [],
isIPX: false, //是否为iphone X
conn: {
closed: false,
curOpenOpt: {},
open(opt) {
this.curOpenOpt = opt;
WebIM.conn.open(opt);
this.closed = false;
},
reopen() {
if (this.closed) {
//this.open(this.curOpenOpt);
WebIM.conn.open(this.curOpenOpt);
this.closed = false;
}
}
}
},
onReadMessage(message) {
console.log('已读', message)
},
onLaunch: function() {
// 调用 API 从本地缓存中获取数据
// uni.setInnerAudioOption({
// obeyMuteSwitch: false
// });
var me = this;
var logs = uni.getStorageSync("logs") || [];
logs.unshift(Date.now());
uni.setStorageSync("logs", logs); //
disp.on("em.main.ready", function() {
calcUnReadSpot();
});
disp.on("em.chatroom.leave", function() {
calcUnReadSpot();
});
disp.on("em.chat.session.remove", function() {
calcUnReadSpot();
});
disp.on("em.chat.audio.fileLoaded", function() {
calcUnReadSpot();
});
disp.on("em.main.deleteFriend", function() {
calcUnReadSpot();
});
disp.on("em.chat.audio.fileLoaded", function() {
calcUnReadSpot();
}); //
WebIM.conn.listen({
onOpened(message) {
// if (
// getCurrentRoute() == "pages/login/login" ||
// getCurrentRoute() == "pages/login/phone_login"||
// getCurrentRoute() == "pages/register/register"
// ) {
// me.globalData.onLoginSuccess(
// uni.getStorageSync("myUsername").toLowerCase()
// );
// }
},
onReconnect() {
uni.showToast({
title: "重连中...",
duration: 2000
});
},
onSocketConnected() {
uni.showToast({
title: "socket连接成功",
duration: 2000
});
},
onClosed() {
uni.showToast({
title: "请检查网络",
icon: "none",
duration: 2000
});
return;
// me.globalData.conn.closed = true;
// WebIM.conn.close();
},
onInviteMessage(message) {
me.globalData.saveGroupInvitedList.push(message);
disp.fire("em.invite.joingroup", message);
uni.showModal({
title: message.from + " 已邀你入群 " + message.roomid,
success(){
disp.fire("em.invite.joingroup", message);
},
error(){
disp.fire("em.invite.joingroup", message);
}
});
},
onReadMessage(message) {
console.log('已读', message)
},
onPresence(message) {
console.log("onPresence", message);
switch (message.type) {
case "unsubscribe":
break;
// 好友邀请列表
case "subscribe":
if (message.status === "[resp:true]") {
} else {
// pages[0].handleFriendMsg(message);
for (let i = 0; i < me.globalData.saveFriendList.length; i++) {
if (me.globalData.saveFriendList[i].from === message.from) {
me.globalData.saveFriendList[i] = message;
uni.setStorageSync("saveFriendList",me.globalData.saveFriendList);
disp.fire("em.subscribe");
return;
}
}
me.globalData.saveFriendList.push(message);
uni.setStorageSync("saveFriendList",me.globalData.saveFriendList);
disp.fire("em.subscribe");
}
break;
case "subscribed":
uni.showToast({
title: "添加成功",
duration: 1000
});
disp.fire("em.subscribed");
break;
case "unsubscribed":
uni.showToast({
title: "已拒绝",
duration: 1000
});
disp.fire("em.unsubscribed");
break;
case "direct_joined":
saveGroups();
uni.showToast({
title: "已进群",
duration: 1000
});
break;
case "memberJoinPublicGroupSuccess":
saveGroups();
uni.showToast({
title: "已进群",
duration: 1000
});
break;
case "invite":
let info = message.from + "邀请你加入群组";
uni.showModal({
title: "提示",
content: info,
success(res) {
if (res.confirm) {
console.log("用户点击确定");
WebIM.conn.agreeInviteIntoGroup({
invitee: WebIM.conn.context.userId,
groupId: message.gid,
success: () => {
saveGroups()
console.log("加入成功");
}
});
} else if (res.cancel) {
console.log("用户点击取消");
WebIM.conn.rejectInviteIntoGroup({
invitee: WebIM.conn.context.userId,
groupId: message.gid
});
}
}
});
break;
case "unavailable":
disp.fire("em.contacts.remove");
disp.fire("em.group.leaveGroup", message);
break;
case "deleteGroupChat":
disp.fire("em.invite.deleteGroup", message);
break;
case "leaveGroup":
disp.fire("em.group.leaveGroup", message);
break;
case "removedFromGroup":
disp.fire("em.group.leaveGroup", message);
break;
default:
break;
}
},
// onRoster(message) {
// let pages = getCurrentPages();
// if(pages[0]){
// pages[0].onShow();
// }
// },
onVideoMessage(message) {
console.log("onVideoMessage: ", message);
if (message) {
msgStorage.saveReceiveMsg(message, msgType.VIDEO);
}
calcUnReadSpot(message);
ack(message);
},
onAudioMessage(message) {
console.log("onAudioMessage", message);
if (message) {
if (onMessageError(message)) {
msgStorage.saveReceiveMsg(message, msgType.AUDIO);
}
calcUnReadSpot(message);
ack(message);
}
},
onCmdMessage(message) {
console.log("onCmdMessage", message);
// if (message) {
// if (onMessageError(message)) {
// msgStorage.saveReceiveMsg(message, msgType.CMD);
// }
// calcUnReadSpot(message);
// ack(message);
// }
},
async onLocationMessage(message){
console.log("Location message: ", message);
if(message){
await msgStorage.saveReceiveMsg(message, msgType.LOCATION);
}
},
async onTextMessage(message) {
console.log("onTextMessage" + message);
console.log(message);
if (message) {
if (onMessageError(message)) {
await msgStorage.saveReceiveMsg(message, msgType.TEXT);
}
calcUnReadSpot(message);
ack(message);
}
},
onEmojiMessage(message) {
console.log("onEmojiMessage", message);
if (message) {
if (onMessageError(message)) {
msgStorage.saveReceiveMsg(message, msgType.EMOJI);
}
calcUnReadSpot(message);
ack(message);
}
},
onPictureMessage(message) {
console.log("onPictureMessage", message);
if (message) {
if (onMessageError(message)) {
msgStorage.saveReceiveMsg(message, msgType.IMAGE);
}
calcUnReadSpot(message);
ack(message);
}
},
onFileMessage(message) {
console.log("onFileMessage", message);
// if (message) {
// if (onMessageError(message)) {
// msgStorage.saveReceiveMsg(message, msgType.FILE);
// }
// calcUnReadSpot(message);
// ack(message);
// }
},
// 各种异常
onError(error) {
console.log(error); // 16: server-side close the websocket connection
if (error.type == WebIM.statusCode.WEBIM_CONNCTION_DISCONNECTED) {
// if(error.type == WebIM.statusCode.WEBIM_CONNCTION_DISCONNECTED && !logout){
// if(WebIM.conn.autoReconnectNumTotal < WebIM.conn.autoReconnectNumMax){
// return;
// }
uni.showToast({
title: "websocket 断开",
duration: 1000
});
uni.redirectTo({
url: "../login/login"
});
logout = true;
return;
} // 8: offline by multi login
if (error.type == WebIM.statusCode.WEBIM_CONNCTION_SERVER_ERROR) {
uni.showToast({
title: "offline by multi login",
duration: 1000
});
uni.redirectTo({
url: "../login/login"
});
}
// if (error.type == WebIM.statusCode.WEBIM_CONNCTION_OPEN_ERROR) {
// uni.hideLoading();
// disp.fire("em.error.passwordErr"); // uni.showModal({
// // title: "用户名或密码错误",
// // confirmText: "OK",
// // showCancel: false
// // });
// }
if (error.type == WebIM.statusCode.WEBIM_CONNCTION_AUTH_ERROR) {
uni.hideLoading();
disp.fire("em.error.tokenErr");
}
if (error.type == "socket_error") {
///sendMsgError
console.log("socket_errorsocket_error", error);
uni.showToast({
title: "网络已断开",
icon: "none",
duration: 2000
});
disp.fire("em.error.sendMsgErr", error);
}
}
});
// #ifdef APP-PLUS
// token标志来判断
let token = uni.getStorageSync('token');
let imuserinfo = uni.getStorageSync("IMuserinfo");
if(imuserinfo){
this.globalData.conn.open({
apiUrl: WebIM.config.apiURL,
user: imuserinfo.username,
pwd: imuserinfo.password,
grant_type: "password",
appKey: WebIM.config.appkey
});
}
console.log(token);
if (!token || !imuserinfo) {
//不存在则跳转至登录页
uni.reLaunch({
url: "/pages/login/login",
success: () => {
plus.navigator.closeSplashscreen();
}
})
} else {
//存在则关闭启动页进入首页
plus.navigator.closeSplashscreen();
}
// #endif
// 网络监听
const NetWork = {
// 网络状态
isConnect: false,
// 监听网络状态
On() {
// 获取当前网络状态
uni.getNetworkType({
success: (res) => {
if (res.networkType !== 'none') {
this.isConnect = true;
return;
}
uni.showToast({
icon: "none",
title: '请先连接网络',
});
}
})
// 监听网络状态变化
uni.onNetworkStatusChange((res) => {
this.isConnect = res.isConnected;
if (!res.isConnected) {
uni.showToast({
icon: "none",
title: '您目前处于断网状态',
});
}
})
}
}
// 初始化用户状态
this.User.__init();
// 更新检测
uni.getSystemInfo({
success: function(e) {
// #ifndef MP
Vue.prototype.StatusBar = e.statusBarHeight;
if (e.platform == 'android') {
Vue.prototype.CustomBar = e.statusBarHeight + 50;
} else {
Vue.prototype.CustomBar = e.statusBarHeight + 45;
};
// #endif
// #ifdef MP-WEIXIN
Vue.prototype.StatusBar = e.statusBarHeight;
let custom = wx.getMenuButtonBoundingClientRect();
Vue.prototype.Custom = custom;
Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
// #endif
// #ifdef MP-ALIPAY
Vue.prototype.StatusBar = e.statusBarHeight;
Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
// #endif
}
})
Vue.prototype.ColorList = [{
title: '嫣红',
name: 'red',
color: '#e54d42'
},
{
title: '桔橙',
name: 'orange',
color: '#f37b1d'
},
{
title: '明黄',
name: 'yellow',
color: '#fbbd08'
},
{
title: '橄榄',
name: 'olive',
color: '#8dc63f'
},
{
title: '森绿',
name: 'green',
color: '#39b54a'
},
{
title: '天青',
name: 'cyan',
color: '#1cbbb4'
},
{
title: '海蓝',
name: 'blue',
color: '#0081ff'
},
{
title: '姹紫',
name: 'purple',
color: '#6739b6'
},
{
title: '木槿',
name: 'mauve',
color: '#9c26b0'
},
{
title: '桃粉',
name: 'pink',
color: '#e03997'
},
{
title: '棕褐',
name: 'brown',
color: '#a5673f'
},
{
title: '玄灰',
name: 'grey',
color: '#8799a3'
},
{
title: '草灰',
name: 'gray',
color: '#aaaaaa'
},
{
title: '墨黑',
name: 'black',
color: '#333333'
},
{
title: '雅白',
name: 'white',
color: '#ffffff'
},
]
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
}
}
</script>
<style lang="scss">
/*每个页面公共css */
/*每个页面公共css */
@import "colorui/main.css";
@import "colorui/icon.css";
/*引入官方CSS库*/
@import './common/uni.css';
/*引入自定义图标库*/
@import './common/iconfont.css';
/*引入css动画库*/
@import './common/animate.css';
/*公共样式*/
@import './common/common.css';
.nav-list {
display: flex;
flex-wrap: wrap;
padding: 0px 40upx 0px;
justify-content: space-between;
}
.nav-li {
padding: 30upx;
border-radius: 12upx;
width: 45%;
margin: 0 2.5% 40upx;
background-image: url(https://cdn.nlark.com/yuque/0/2019/png/280374/1552996358352-assets/web-upload/cc3b1807-c684-4b83-8f80-80e5b8a6b975.png);
background-size: cover;
background-position: center;
position: relative;
z-index: 1;
}
.nav-li::after {
content: "";
position: absolute;
z-index: -1;
background-color: inherit;
width: 100%;
height: 100%;
left: 0;
bottom: -10%;
border-radius: 10upx;
opacity: 0.2;
transform: scale(0.9, 0.9);
}
.nav-li.cur {
color: #fff;
background: rgb(94, 185, 94);
box-shadow: 4upx 4upx 6upx rgba(94, 185, 94, 0.4);
}
.nav-title {
font-size: 32upx;
font-weight: 300;
}
.nav-title::first-letter {
font-size: 40upx;
margin-right: 4upx;
}
.nav-name {
font-size: 28upx;
text-transform: Capitalize;
margin-top: 20upx;
position: relative;
}
.nav-name::before {
content: "";
position: absolute;
display: block;
width: 40upx;
height: 6upx;
background: #fff;
bottom: 0;
right: 0;
opacity: 0.5;
}
.nav-name::after {
content: "";
position: absolute;
display: block;
width: 100upx;
height: 1px;
background: #fff;
bottom: 0;
right: 40upx;
opacity: 0.3;
}
.nav-name::first-letter {
font-weight: bold;
font-size: 36upx;
margin-right: 1px;
}
.nav-li text {
position: absolute;
right: 30upx;
top: 30upx;
font-size: 52upx;
width: 60upx;
height: 60upx;
text-align: center;
line-height: 60upx;
}
.text-light {
font-weight: 300;
}
@keyframes show {
0% {
transform: translateY(-50px);
}
60% {
transform: translateY(40upx);
}
100% {
transform: translateY(0px);
}
}
@-webkit-keyframes show {
0% {
transform: translateY(-50px);
}
60% {
transform: translateY(40upx);
}
100% {
transform: translateY(0px);
}
}
</style>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化