Commit 65dc6dfb by jscat

nyx: 公屏

优化一系列socket的问题
1. nyxCode如果存在,则必为全局的userId
2. onLoad的initSocket在onShow之前执行,同时onShow不会再次执行(readyStatus==0,不执行initSocket)
3. 因为测试的原因,某一个uid可能在数据库里删除,那么通过regUser的方式重新replace成新的
4. onMessage里,如果uid删除,那么onMessage中的getData可能先于regUser执行,那么getData拿不到数据,针对这种情况
将getData的数据初始化为匿名用户。
parent 68761269
//app.js
//app.js
......@@ -7,6 +7,7 @@ App({
globalData: {
// userInfo setting
nyxCode: "", //userId, 唯一识别码
nickName: "",
authStatus: "", //授权状态, 0表示未授权, 1表示已授权
userInfo : {}, //用户信息
matchSeason : "",
......@@ -206,25 +207,29 @@ App({
});
},
regUser: function () {
regUser: function (uuid) {
var _this = this;
var uuid = "uid_"+util.wxuuid()
_this.globalData.nyxCode = uuid;
_this.globalData.nickName = "匿名用户"
wx.setStorageSync('nyxCode', uuid)
var strUrl = config.user_reg_url + "?userid=" + uuid
config.debug == 1 ? console.log("===regUser_strUrl_" + strUrl) : ""
wx.request({
url: strUrl,
method: 'GET',
success: function (res) {
config.debug == 1 ? console.info("===regUser_data_", res) : ""
wx.setStorageSync('nyxCode', res.data.data.id)
wx.setStorageSync('nickName', "匿名用户")
wx.setStorageSync('authStatus', "0")
wx.setStorageSync('matchSeason', _this.getSeason())
var userInfo = {}
userInfo['userId'] = res.data.data.id
userInfo['userId'] = uuid
userInfo['nickName'] = "匿名用户"
userInfo['avatarUrl'] = "https://930-test-sh.oss-cn-shanghai.aliyuncs.com/u_image/icon_avatar1.png"
//设置全局参数
wx.setStorageSync('userInfo', userInfo)
_this.globalData.userInfo = userInfo
var strUrl = config.user_reg_url + "?userid=" + uuid
config.debug == 1 ? console.log("===regUser_strUrl_" + strUrl) : ""
wx.request({
url: strUrl,
method: 'GET',
success: function (res) {
config.debug == 1 ? console.info("===regUser_success_data_", res) : ""
},
fail: function () {
console.log('系统错误')
......
.page{
.page{
.page{
height:100vh;
height:100vh; /* 相对于视口(Layout Viewport)的高度; 视口被均分为100单位的vh */
background-color:#f5f8fa;
}
......
// pages/key/key.js
// pages/key/key.js
......@@ -4,6 +4,7 @@ var config = wx.getStorageSync("config");
var app = getApp();
var log = require('./../../utils/log.js')
var util = require('./../../utils/util.js')
Page({
......@@ -58,34 +59,53 @@ Page({
*/
onLoad: function (options) {
var _this = this;
console.log('===onLoad, options...', JSON.stringify(options));
console.log('===onLoad, options_', JSON.stringify(options));
/*step1 先确定用户信息 */
if (!wx.getStorageSync('nyxCode'))
var nyxCode = wx.getStorageSync('nyxCode');
//不存在
if (!nyxCode)
{
//注册新用户
wx.clearStorageSync();
app.regUser();
console.log("===onLoad_regUser")
wx.clearStorageSync('nyxCode');
nyxCode = "uid_" + util.wxuuid()
wx.setStorageSync('nyxCode', nyxCode);
app.globalData.nyxCode = nyxCode;
app.regUser(nyxCode);
}
else
else //存在
{
//更新用户信息
var strUrl = config.userinfo_query_url + "?userid=" + wx.getStorageSync('nyxCode')
var strUrl = config.userinfo_query_url + "?userid=" + nyxCode
wx.setStorageSync('nyxCode', nyxCode);
app.globalData.nyxCode = nyxCode;
config.debug == 1 ? console.log("===getLatestUserInfo strUrl_" + strUrl) : 1
getData(strUrl, "").then(res => {
console.log(res.data)
if(res.data.length==0) //数据库不存在该用户(误删除或者测试数据已删除)
{
//以该id注册新用户
console.log("===onLoad_Update User Info")
app.regUser(nyxCode);
}
else
{
var list = res.data[0]
//wx.setStorageSync('authStatus', "0")
wx.setStorageSync('userInfo', list)
app.globalData.nyxCode = nyxCode;
wx.setStorageSync('nyxCode', nyxCode);
app.globalData.userInfo = list
wx.setStorageSync('userInfo', list)
console.log("==onLoad_userInfo success")
}
})
}
if (app.globalData.socketTask.readyState != 1) {
//初始化socket
if (app.globalData.socketTask.readyState != 1 && app.globalData.socketTask.readyState != 0) {
var sid = _this.data.sid
var uid = wx.getStorageSync('nyxCode')
if (sid != undefined && uid != undefined && sid != "" && uid != "") {
app.initSocket(sid, uid, "进入竞答环节")
if (sid != undefined && nyxCode != undefined && sid != "" && nyxCode != "") {
app.initSocket(sid, nyxCode, "进入竞答环节")
app.globalData.socketClose = false
}
}
......@@ -93,15 +113,7 @@ Page({
//每次重置matchSeason
wx.setStorageSync('matchSeason', app.getSeason())
//1. 从local storage里读取最新的数据
//2. 存储到globalData供所有页面使用
app.globalData.nyxCode = wx.getStorageSync('nyxCode')
app.globalData.authStatus = wx.getStorageSync('authStatus')
app.globalData.userInfo = wx.getStorageSync('userInfo')
app.globalData.matchSeason = wx.getStorageSync('matchSeason')
log.info("key_onLoad_nyxCode", app.globalData.nyxCode)
log.info("key_onLoad_authStatus", app.globalData.authStatus)
log.info("key_onLoad_userInfo", app.globalData.userInfo)
log.info("key_onLoad_matchSeason", app.globalData.matchSeason)
wx.setNavigationBarTitle({
title: "发现有趣的你"
......@@ -115,8 +127,6 @@ Page({
var windowHeight = wx.getSystemInfoSync().windowHeight;
var keyHeight = windowHeight*0.4
var chatHeight = windowHeight - keyHeight
console.log("keyHeight_"+keyHeight)
console.log("chatHeight_" + chatHeight)
_this.setData({
keyHeight: keyHeight,
chatHeight: chatHeight,
......@@ -135,10 +145,10 @@ Page({
// 初始化socket, 监听socket
onShow: function () {
var _this = this;
if (app.globalData.socketTask.readyState != 1) {
var sid = _this.data.sid
console.log("===onShow_readyState_", app.globalData.socketTask.readyState)
var uid = wx.getStorageSync('nyxCode')
if (app.globalData.socketTask.readyState != 1 && app.globalData.socketTask.readyState != 0 && uid != "") {
var sid = _this.data.sid
if (sid != undefined && uid != undefined && sid != "" && uid != "") {
app.initSocket(sid, uid, "进入竞答环节")
app.globalData.socketClose = false
......@@ -147,7 +157,7 @@ Page({
// 最好放在onShow
app.globalData.socketTask.onOpen(function (res) {
console.log('chat-onOpen webSocket连接已打开! readyState=' + app.globalData.socketTask.readyState)
console.log("chat-open res_", res)
//console.log("chat-open res_", res)
if (app.globalData.socketClose) {
app.closeSocket();
......@@ -190,23 +200,41 @@ Page({
})
app.globalData.socketTask.onMessage(onMessage => {
console.log('监听WebSocket接受到服务器的消息事件。服务器返回的消息', onMessage.data)
log.info('监听WebSocket接受到服务器的消息事件。服务器返回的消息', onMessage.data)
//console.log('监听WebSocket接受到服务器的消息事件。服务器返回的消息', onMessage.data)
var json = JSON.parse(onMessage.data);
// uid
var uid = json["uid"];
if (json["cmd"] != "onHeart") {
console.log("json: ", json)
console.log("===onMessage_json: ", json)
var strUrl = config.userinfo_query_url + "?userid=" + uid
config.debug == 1 ? console.log("===getData strUrl_" + strUrl) : 1
//tofix 这个地方特别容易出错
config.debug == 1 ? console.log("===onMessage getData strUrl_" + strUrl) : 1
getData(strUrl, "").then(res => {
console.log(res.data)
var list = res.data
console.log("===onMessage_res_",res)
var list = {}
//成功返回,服务器没问题
if (res.resultCode == "200")
{
if (res.data.length == 0) {
list['userId'] = uid
list['nickName'] = "匿名用户"
list['avatarUrl'] = "https://930-test-sh.oss-cn-shanghai.aliyuncs.com/u_image/icon_avatar1.png"
}
else {
list = res.data[0]
}
console.log("===onMessage_chatUserInfo, ", list)
_this.setData({
chatUserInfo: list[0]
chatUserInfo: list
})
_this.processData(json)
}
else
{
console.log("===onMessage, 系统错误")
}
})
}
......@@ -436,6 +464,7 @@ Page({
var text = ""
var nickName = ""
var avater = ""
console.log("====chatUserInfo, ", _this.data.chatUserInfo)
if (json["cmd"] == "onOpen" || json["cmd"] == "onScore") {
text = " " + _this.data.chatUserInfo.nickName + " " +json["msg"]
nickName = "系统"
......
<!--pages/key/key.wxml-->
<!--pages/key/key.wxml-->
......@@ -7,7 +7,7 @@
<view class="weui-cell__hd">
<image src="/icon/daily.png" />
</view>
<view class="weui-cell__bd">每日一学</view>
<view class="weui-cell__bd">随便看看</view>
<view class="weui-cell__ft weui-cell__ft_in-access"></view>
</navigator>
<navigator class="weui-cell weui-cell_access" hover-class="weui-cell_active" url="/pages/key/matchStudy/matchStudy">
......
{
{
{
"usingComponents": {},
"navigationBarTitleText": "每日一学"
"navigationBarTitleText": "随便看看"
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论