123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- var rootDocment = 'https://data.kuxuan.com';//域名
- // var rootDocment = 'http://47.93.151.217:8006';//测试域名
- // var rootDocment = 'http://apitbk.726p.com';//预发域名
- var md5 = require('./md5.js');
- var count = 0;
- wx.setStorageSync('doneLogin',false)
- function req(url, method, data, cb, fail_cb, selfHeader) {
- var dataes = {'admin_id':wx.getStorageSync('admin_id'),
- 'ttl':wx.getStorageSync('ttl'),
- 'sign':wx.getStorageSync('sign')}
- var header = {
- 'sessionKey':wx.getStorageSync('session_key')?wx.getStorageSync('session_key'):false
- }
- wx.request({
- url: rootDocment + url,
- data: data ? Object.assign(data, dataes) : dataes,
- method: method,
- header: selfHeader ? Object.assign(selfHeader,header) : header,
- success: function (res) {
- if(res.data.errno == '4002' || res.data.errno == '4001'){
- console.log(count+"~~"+wx.getStorageSync('isLogin')+"~~~~~"+wx.getStorageSync('doneLogin'))
- if(!wx.getStorageSync('isLogin')&&!wx.getStorageSync('doneLogin')){
- // 新加
- wx.setStorageSync('doneLogin',true);
- checkSession()
- return;
- }else{
- console.log(count + '~~~~~')
- if(count>=1||wx.getStorageSync('doneLogin')){
- return
- }
- wx.clearStorage()
- wx.setStorageSync('isRequest',true)
- wx.setStorageSync('doneLogin',true)
- count++;
- console.log("过期~~~"+count)
- login()
- }
- }else{
- count = 0;
- return typeof cb == "function" && cb(res.data)
- }
- },
- fail: function (err) {
- // if(!wx.getStorageSync('doneLogin')) {
- // wx.setStorageSync('doneLogin',false)
- // count = 0;
- // console.log("跳转授权2")
- // wx.redirectTo({
- // url: '/pages/welcome/welcome?from=index',
- // })
- // }
- console.log("失败")
- return typeof fail_cb == "function" && fail_cb(err)
- }
- })
- }
- module.exports = {
- req: req
- }
- //判断微信登录是否过期
- function checkSession (cb) {
- var _this = this;
- wx.checkSession({
- success (cb) {
- //session_key 未过期,并且在本生命周期一直有效
- console.log('未过期')
- console.log(cb)
- console.log(!wx.getStorageSync('isLogin') +"~~~"+ !wx.getStorageSync('isRequest'))
- wx.setStorageSync('hasWxLogin',true);//微信登录是否过期
- if(!wx.getStorageSync('isLogin') && !wx.getStorageSync('isRequest')){
- login()
- }else{
- wx.setStorageSync('doneLogin', false)
- count=0;
- wx.switchTab({
- url: '/pages/index/index',
- })
- }
- },
- fail () {
- console.log('过期,重新登录')
- // session_key 已经失效,需要重新执行登录流程
- login()
- }
- })
- }
- // 旧login
- function login(){
- var _this = this;
- console.log("新login")
- wx.getSetting({
- success: function(res){
- if (res.authSetting['scope.userInfo']) {
- wx.getUserInfo({
- success:function (e){
- console.log(e)
- wx.setStorageSync('encryptedData',e.encryptedData)
- wx.setStorageSync('iv',e.iv)
-
- wx.login({
- success (res) {
- if (res.code) {
- // 发起网络请求 发送 res.code 到后台换取 openId, sessionKey, unionId
- req('/v2/api/wx/login', 'get', {
- code: res.code,
- encrypted_data:wx.getStorageSync('encryptedData'),
- iv: wx.getStorageSync('iv')
- }, function (res) {
- 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){//用户未绑定手机号
- console.log('用户未绑定手机号')
- }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)
- wx.setStorageSync('doneLogin', false)
- count = 0;
- wx.switchTab({
- url: '/pages/index/index?from=login',
- success: function(res){
- //小程序跳转页面后,动态刷新跳转页面
- var page = getCurrentPages().pop();
- console.log("页面ing")
- console.log(page)
- if (page == undefined || page == null) return;
- page.onLoad();
- console.log("刷新")
- },
- })
- }
- }else{
- wx.redirectTo({
- url: '/pages/welcome/welcome',
- })
- }
- },()=>{
- count = 0;
- wx.setStorageSync('doneLogin', false)
- console.log("跳转授权3")
- count = 0;
- wx.setStorageSync('doneLogin', false)
- wx.redirectTo({
- url: '/pages/welcome/welcome',
- })
- })
- } else {
- console.log('登录失败!' + res.errMsg)
- }
- }
- })
- }
- })
- }else{
- count = 0;
- wx.setStorageSync('doneLogin', false)
- //未授权
- console.log("跳转授权4")
- wx.redirectTo({
- url: '/pages/welcome/welcome',
- })
- return
- }
- }
- })
- }
|