//app.js var http = require('utils/util.js'); var md5 = require('utils/md5.js'); var commonJs = require('utils/common.js') App({ data:{ windowWidth:0,//屏幕的宽度 windowHeight:0,//屏幕的高度 loginGoPage:'pages/index/index?from=login', loginGoPageQuery:{} }, onLaunch: function () { this.verified();//版本控制(过审用) }, onShow:function(options){ console.log("onshow",options) if (options.scene == 1007 || options.scene == 1008) { // 1007通过单人聊天会话分享进入 1008 通过群聊会话分享进入 this.data.loginGoPage = options.path this.data.loginGoPageQuery = options.query } if (options.scene == 1001) { // 通过发现栏小程序进入 } }, verified(){//版本控制 var _this = this; console.log("开始") http.req('/v2/api/wx/verified', 'get', { version: 'v1.7' }, function (res) { if(res && res.errno == 0){ wx.setStorageSync('isLogin',false) wx.setStorageSync('admin_id',res.rst.data.id) if(!wx.getStorageSync('ttl')||wx.setStorageSync('ttl')!=res.rst.data.ttl) { wx.setStorageSync('ttl',res.rst.data.ttl) var sign=md5.hex_md5("random="+res.rst.data.random+"&ttl="+res.rst.data.ttl+"&user_id="+res.rst.data.id); wx.setStorageSync('sign',sign) } wx.setStorageSync('doneLogin', false) _this.loginGoPageEvent() }else if(res.errno == 9014){ console.log("自己的权限") wx.getSystemInfo({ success (res) { _this.data.windowWidth = res.windowWidth _this.data.windowHeight = res.windowHeight } }) _this.getSetting() } },(err)=>{ console.log("err",err) }) }, getSetting(){ var _this = this; wx.getSetting({ success: function(res){ if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称 wx.getUserInfo({ success:function (e){ wx.setStorageSync('encryptedData',e.encryptedData) wx.setStorageSync('iv',e.iv) _this.checkSession() } }) }else{ //未授权 console.log("未授权--verified") wx.redirectTo({ url: '/pages/welcome/welcome', }) } } }) }, checkSession (cb) {//判断微信登录是否过期 var _this = this; wx.checkSession({ success (cb) { //session_key 未过期,并且在本生命周期一直有效 wx.setStorageSync('hasWxLogin',true);//微信登录是否过期 if(!wx.getStorageSync('isLogin') && !wx.getStorageSync('isRequest')){ _this.login() }else{//已在登录状态 console.log("已在登录状态") } }, fail() { console.log('过期,重新登录') // session_key 已经失效,需要重新执行登录流程 _this.login() } }) }, login(cb){ console.log("登录ing") var _this = this; wx.login({ success (res) { if (res.code) { // 发起网络请求 发送 res.code 到后台换取 openId, sessionKey, unionId http.req('/v2/api/wx/login', 'get', { code: res.code, encrypted_data:wx.getStorageSync('encryptedData'), iv: wx.getStorageSync('iv'), platform_id: 1 }, function (res) { wx.hideLoading() console.log(res,'logiin') wx.setStorageSync('isRequest', false) if(res && res.errno == 0){ //成功 var session_key = res.rst.session_key; wx.setStorageSync('session_key',session_key) if(!res.rst.info || res.rst.info.length == 0){//用户未绑定手机号 wx.setStorageSync('noneLogin',true);//微信登录是否过期 wx.redirectTo({ url: '/pages/login/login', }) }else{ wx.setStorageSync('userInfo',res.rst.info) wx.setStorageSync('admin_id',res.rst.info.id) wx.setStorageSync('ttl',res.rst.ttl) var sign=md5.hex_md5("random="+res.rst.random+"&ttl="+res.rst.ttl+"&user_id="+res.rst.info.id); wx.setStorageSync('sign',sign) wx.setStorageSync('isLogin',true) cb?cb():'' _this.loginGoPageEvent() } }else{ console.log("跳转授权- login接口 errno != 0") wx.redirectTo({ url: '/pages/welcome/welcome', }) } },()=>{ wx.hideLoading() }) } else { console.log('登录失败!' + res.errMsg) } } }) }, loginGoPageEvent(){//登录跳转的页面事件 var queryString = '?'; for(var i in this.data.loginGoPageQuery){ if(queryString != '?'){ queryString = '&' + queryString + i + '=' + this.data.loginGoPageQuery[i] }else{ queryString = queryString + i + '=' + this.data.loginGoPageQuery[i] } } if(queryString == '?'){ queryString = ''; } console.log(queryString,this.data.loginGoPage,this.data.loginGoPageQuery) if(this.data.loginGoPage.indexOf('pages/index/index') != -1 || this.data.loginGoPage.indexOf('pages/live/live') != -1 || this.data.loginGoPage.indexOf('pages/originality/originality/index') != -1 || this.data.loginGoPage.indexOf('pages/follow/follow') != -1){ wx.switchTab({ url: '/'+this.data.loginGoPage + queryString, success: function(res){ var page = getCurrentPages().pop(); if (page == undefined || page == null) return; page.onLoad(); }, }) }else{ wx.navigateTo({ url: '/'+this.data.loginGoPage + queryString }) } }, func: { req: http.req, md5: md5.hex_md5, getDay: commonJs.getDay, NumberHandle: commonJs.NumberHandle }, })