您现在的位置: 微信小程序 > 微信小程序DEMO >

微信小程序学习用demo:微信登录

来源:微信小程序 编辑:易用通 发布时间:03-14热度:

 

代码示例:
[AppleScript] 纯文本查看 复制代码
?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var cfg_js=require('cfg.js');
 
//用户登录
function getNewSession(saveSesionOk){
    wx.login({
      success: function(res) {
          wx.request({
            url: cfg_js.gets('loginURI'),
            data: {code: res.code},
            success: function(res){
                var new_session=res.data.session;
                wx.setStorage({
                  key:"session",
                  data:new_session,
                  success:function(res){
                      saveSesionOk(res)
                  }
                })
            }
          })
      }
    });
}
 
 
 
//set本地存储
function setStrg(key,value,success){
  wx.setStorage({
    key:key,
    data:value,
    success: function(res){
      success(res)
    }
  })
}
 
//get本地存储
function getStrg(key,success,fail){
  wx.getStorage({
    key: key,
    success: function(res){
      success(res)
    },
    fail: function(res) {
      fail(res)
    }
  })
}
 
 
//封装post请求
function post(url,data,success){
   wx.request({
        url: url,
        data:data,
        method: 'POST',
        header: {'content-type': 'application/x-www-form-urlencoded'},
        success: function(res) {
          success(res)
        }
    })
}
 
//封装get请求
function got(url,data,success){
   wx.request({
        url: url,
        data:data,
        header: {'content-type': 'application/json'},
        success: function(res) {
          success(res)
        }
    })
}
 
 
//3rd_session检测
function sessionCheck(session,success){
    //0代表不合法,1代表ok
    var url= cfg_js.gets('sessionCheckURI')+"&session="+session;
    post(url,{},function(res){
        success(res)
    })
}
 
 
//第一次验证。一般情况下,登录只需要在程序开始的第页面验证一次就好
function firstReqest(sessionCheckOk){
        // ---------------------------------验证登录开始------------------------------
        getStrg("session",function(res){
          //获取本地seesin成功
            var session=res.data;
            sessionCheck(session,function(res){
                 var status=res.data;
                 status=1;//假设验证成功(本地的3rd_session和服务器的3rd_session一致)
                //与后台匹配失败
                if(status==0){
                      //重新登录
                      getNewSession(function(res){
                          getStrg("session",function(res){
                              //获取本地session成功
                                var session=res.data;
                                sessionCheckOk(session)
                          },null)
                      })
                }else{
                  sessionCheckOk(session)
                }
            })
        },function(res){
          //获取本地seesin失败
             getNewSession(function(res){
                  getStrg("session",function(res){
                        var session=res.data;
                        sessionCheckOk(session)
                  },null)
              })
        })
        // ---------------------------------结束---------------------------
}
 
module.exports = {
  getNewSession: getNewSession,
  setStrg:setStrg,
  getStrg:getStrg,
  post:post,
  got:got,
  sessionCheck:sessionCheck,
  firstReqest:firstReqest
}