Commit 344e5b60 by jscat

nyx: map和socket功能改进

1. 在initSocket之前在小程序客户端提前生成uuid,而不需要等待后台服务器返回
2. map添加图片临时路径是否存在的判断逻辑。
3. util.js里添加生成uuid的函数
parent d8ae4ffb
//app.js //app.js
//app.js //app.js
var config=require('./config.js'); var config=require('./config.js');
var util = require('./utils/util.js')
App({ App({
...@@ -207,7 +208,9 @@ App({ ...@@ -207,7 +208,9 @@ App({
regUser: function () { regUser: function () {
var _this = this; var _this = this;
var strUrl = config.user_reg_url var uuid = "uid_"+util.wxuuid()
wx.setStorageSync('nyxCode', uuid)
var strUrl = config.user_reg_url + "?userid=" + uuid
config.debug == 1 ? console.log("===regUser_strUrl_" + strUrl) : "" config.debug == 1 ? console.log("===regUser_strUrl_" + strUrl) : ""
wx.request({ wx.request({
url: strUrl, url: strUrl,
......
// pages/key/key.js // pages/key/key.js
...@@ -65,7 +65,7 @@ Page({ ...@@ -65,7 +65,7 @@ Page({
{ {
//注册新用户 //注册新用户
wx.clearStorageSync(); wx.clearStorageSync();
app.regUser(); app.regUser();
} }
else else
{ {
...@@ -81,6 +81,14 @@ Page({ ...@@ -81,6 +81,14 @@ Page({
}) })
} }
if (app.globalData.socketTask.readyState != 1) {
var sid = _this.data.sid
var uid = wx.getStorageSync('nyxCode')
if (sid != undefined && uid != undefined && sid != "" && uid != "") {
app.initSocket(sid, uid, "进入竞答环节")
app.globalData.socketClose = false
}
}
//每次重置matchSeason //每次重置matchSeason
wx.setStorageSync('matchSeason', app.getSeason()) wx.setStorageSync('matchSeason', app.getSeason())
...@@ -127,6 +135,7 @@ Page({ ...@@ -127,6 +135,7 @@ Page({
// 初始化socket, 监听socket // 初始化socket, 监听socket
onShow: function () { onShow: function () {
var _this = this; var _this = this;
if (app.globalData.socketTask.readyState != 1) { if (app.globalData.socketTask.readyState != 1) {
var sid = _this.data.sid var sid = _this.data.sid
var uid = wx.getStorageSync('nyxCode') var uid = wx.getStorageSync('nyxCode')
...@@ -135,7 +144,6 @@ Page({ ...@@ -135,7 +144,6 @@ Page({
app.globalData.socketClose = false app.globalData.socketClose = false
} }
} }
// 最好放在onShow // 最好放在onShow
app.globalData.socketTask.onOpen(function (res) { app.globalData.socketTask.onOpen(function (res) {
console.log('chat-onOpen webSocket连接已打开! readyState=' + app.globalData.socketTask.readyState) console.log('chat-onOpen webSocket连接已打开! readyState=' + app.globalData.socketTask.readyState)
...@@ -144,8 +152,7 @@ Page({ ...@@ -144,8 +152,7 @@ Page({
if (app.globalData.socketClose) { if (app.globalData.socketClose) {
app.closeSocket(); app.closeSocket();
} }
else else {
{
app.globalData.socketOpen = true; app.globalData.socketOpen = true;
// 发送答题积分消息 // 发送答题积分消息
...@@ -174,8 +181,7 @@ Page({ ...@@ -174,8 +181,7 @@ Page({
console.log('chat-WebSocket连接已关闭! readyState=' + app.globalData.socketTask.readyState) console.log('chat-WebSocket连接已关闭! readyState=' + app.globalData.socketTask.readyState)
//对应case1 //对应case1
if(!app.globalData.socketClose) if (!app.globalData.socketClose) {
{
clearTimeout(app.globalData.connectSocketTimeOut) clearTimeout(app.globalData.connectSocketTimeOut)
app.globalData.connectSocketTimeOut = setTimeout(() => { app.globalData.connectSocketTimeOut = setTimeout(() => {
app.initSocket(_this.data.sid, app.globalData.nyxCode, "进入竞答环节") app.initSocket(_this.data.sid, app.globalData.nyxCode, "进入竞答环节")
...@@ -187,11 +193,10 @@ Page({ ...@@ -187,11 +193,10 @@ Page({
console.log('监听WebSocket接受到服务器的消息事件。服务器返回的消息', onMessage.data) console.log('监听WebSocket接受到服务器的消息事件。服务器返回的消息', onMessage.data)
log.info('监听WebSocket接受到服务器的消息事件。服务器返回的消息', onMessage.data) log.info('监听WebSocket接受到服务器的消息事件。服务器返回的消息', onMessage.data)
var json = JSON.parse(onMessage.data); var json = JSON.parse(onMessage.data);
// uid // uid
var uid = json["uid"]; var uid = json["uid"];
if(json["cmd"]!="onHeart") if (json["cmd"] != "onHeart") {
{
console.log("json: ", json) console.log("json: ", json)
var strUrl = config.userinfo_query_url + "?userid=" + uid var strUrl = config.userinfo_query_url + "?userid=" + uid
config.debug == 1 ? console.log("===getData strUrl_" + strUrl) : 1 config.debug == 1 ? console.log("===getData strUrl_" + strUrl) : 1
...@@ -205,8 +210,14 @@ Page({ ...@@ -205,8 +210,14 @@ Page({
}) })
} }
}) })
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function (){
var _this = this;
}, },
......
// pages/key/matchDaily/matchDaily.js // pages/key/matchDaily/matchDaily.js
...@@ -134,7 +134,7 @@ Page({ ...@@ -134,7 +134,7 @@ Page({
{ {
var score = JSON.stringify({ var score = JSON.stringify({
'cmd': 'onScore', 'cmd': 'onScore',
'uid': app.globalData.nyxCode, 'uid': app.globalData.nyxCode == "" ? wx.getStorageSync('nyxCode'):app.globalData.nyxCode,
'msg': "完成每日一学,喜提" + _this.data.score + "分" 'msg': "完成每日一学,喜提" + _this.data.score + "分"
}) })
wx.setStorageSync('sendMsg', score); wx.setStorageSync('sendMsg', score);
......
// pages/key/matchStudy/matchStudyDetail/matchStudyDetail.js // pages/key/matchStudy/matchStudyDetail/matchStudyDetail.js
...@@ -162,7 +162,7 @@ Page({ ...@@ -162,7 +162,7 @@ Page({
var score = parseInt(_this.data.studyStatus.total_score) - parseInt(_this.data.studyStatus.init_score) var score = parseInt(_this.data.studyStatus.total_score) - parseInt(_this.data.studyStatus.init_score)
var sendMsg = JSON.stringify({ var sendMsg = JSON.stringify({
'cmd': 'onScore', 'cmd': 'onScore',
'uid': app.globalData.nyxCode, 'uid': app.globalData.nyxCode == "" ? wx.getStorageSync('nyxCode') : app.globalData.nyxCode,
'msg': "完成" + _this.data.studyType + "栏目,喜提" + score + "分" 'msg': "完成" + _this.data.studyType + "栏目,喜提" + score + "分"
}) })
wx.setStorageSync('sendMsg', sendMsg); wx.setStorageSync('sendMsg', sendMsg);
......
var config = wx.getStorageSync("config"); var config = wx.getStorageSync("config");
...@@ -114,7 +114,6 @@ Page({ ...@@ -114,7 +114,6 @@ Page({
*/ */
onShow: function () { onShow: function () {
var self = this; var self = this;
// self.cacheImage();
}, },
uploadScaleStart(e) { //缩放图片 uploadScaleStart(e) { //缩放图片
...@@ -269,7 +268,7 @@ Page({ ...@@ -269,7 +268,7 @@ Page({
allText[allText.length - 1].id = "mid_002"; allText[allText.length - 1].id = "mid_002";
allText[allText.length - 1].name = name allText[allText.length - 1].name = name
allText[allText.length - 1].address = address allText[allText.length - 1].address = address
console.log(allText[allText.length - 1])
_this.setData( _this.setData(
{ {
allText: allText, allText: allText,
...@@ -457,11 +456,11 @@ Page({ ...@@ -457,11 +456,11 @@ Page({
var result = {} var result = {}
result["id"] = res.data.data[i].id result["id"] = res.data.data[i].id
result["name"] = res.data.data[i].memberName result["name"] = res.data.data[i].memberName
result["textL"] = parseInt(res.data.data[i].posLeft) result["textL"] = parseFloat(res.data.data[i].posLeft)
result["textT"] = parseInt(res.data.data[i].posTop) result["textT"] = parseFloat(res.data.data[i].posTop)
result["address"] = res.data.data[i].memberAddress result["address"] = res.data.data[i].memberAddress
result["initTextL"] = parseInt(res.data.data[i].posLeft) result["initTextL"] = parseFloat(res.data.data[i].posLeft)
result["initTextT"] = parseInt(res.data.data[i].posTop) result["initTextT"] = parseFloat(res.data.data[i].posTop)
list.push(result) list.push(result)
} }
...@@ -510,17 +509,51 @@ Page({ ...@@ -510,17 +509,51 @@ Page({
_this.cacheImage(); _this.cacheImage();
}, },
//从网络imgUrl或者临时文件tempFile载入图片
loadMap: function (self, imgUrl, areaName) loadMap: function (self, imgUrl, areaName)
{ {
var img_cache_key = areaName + "_imgcache" var img_cache_key = areaName + "_imgcache"
var path = wx.getStorageSync(img_cache_key) == "" ? imgUrl : wx.getStorageSync(img_cache_key) //先获取临时文件tempFile
console.log("===loadMap, "+areaName+" image path: "+path) var tempPath = wx.getStorageSync(img_cache_key)
self.setData({ if(tempPath != "")
tempImageSrc: path, {
img_cache_key: img_cache_key, //通过文件管理系统来判断tempFile是否存在
imgUrl: imgUrl const fs = wx.getFileSystemManager()
}) fs.access({
loadImgOnImage(self, path) path: tempPath,
success: function () {
console.log("===loadMap, success Load res from temp")
console.log("===loadMap, " + areaName + " image path: " + tempPath)
self.setData({
tempImageSrc: tempPath,
img_cache_key: img_cache_key,
imgUrl: imgUrl
})
loadImgOnImage(self, tempPath)
},
fail: function (errmsg) {
console.log("===loadMap, fail Load res from temp")
wx.removeStorageSync(img_cache_key)
console.log("===loadMap, " + areaName + " image path: " + imgUrl)
self.setData({
tempImageSrc: imgUrl,
img_cache_key: img_cache_key,
imgUrl: imgUrl
})
loadImgOnImage(self, imgUrl)
},
})
}
else
{
self.setData({
tempImageSrc: imgUrl,
img_cache_key: img_cache_key,
imgUrl: imgUrl
})
loadImgOnImage(self, imgUrl)
}
}, },
...@@ -558,6 +591,8 @@ Page({ ...@@ -558,6 +591,8 @@ Page({
}) })
}, },
// refer https://www.jianshu.com/p/c07c0c81986f
// 微信小程序 实现网络图片本地缓存
cacheImageFunc: function (img_cache_key, imgUrl) { cacheImageFunc: function (img_cache_key, imgUrl) {
wx.downloadFile({ wx.downloadFile({
url: imgUrl, url: imgUrl,
......
function formatTime(date) { function formatTime(date) {
...@@ -16,6 +16,23 @@ function formatNumber(n) { ...@@ -16,6 +16,23 @@ function formatNumber(n) {
return n[1] ? n : '0' + n return n[1] ? n : '0' + n
} }
function wxuuid () {
var s = [];
var hexDigits = "0123456789abcdef";
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = "-";
var uuid = s.join("");
return uuid
}
module.exports = { module.exports = {
formatTime: formatTime formatTime: formatTime,
wxuuid: wxuuid
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论