123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import {
- Config
- } from './config.js'
- class Token {
- constructor() {
- this.loginUrl = Config.restUrl + '/wx/login';
- this.verifyUrl = Config.restUrl + '/wx/verifyToken'
- this.verifyLoginUrl = Config.restUrl + '/wx/isRegister'
- }
- verify(){
- var token = wx.getStorageInfoSync('token')
- if(!token){
- this.getTokenFromServer();
- }else{
- this._verifyFromServer(token)
- }
- }
-
- //携带令牌判断令牌是否正确
- _verifyFromServer(token){
- var that = this;
- wx.request({
- url: that.verifyUrl,
- method:'GET',
- data: {
- token:token
- },
- success:function(res){
- console.log(res)
- var valid = true;
- if(!valid){
- that.getTokenFromServer();
- }
- }
- })
- }
-
-
-
-
-
- async getTest(){
- await wxLogin();
-
- }
-
- wxLogin(){
-
- uni.login({
- success (res) {
- console.log(res)
- }
- })
-
- }
- getTokenFromServer() {
- console.log("进入登陆")
- //只有根据appid和对应的code才能拿到token
- var that = this
- uni.login({
- success: function(res) {
- console.log("登陆成功")
- uni.request({
- url: that.loginUrl,
- method:'GET',
- data: {
- code: res.code,
- appId: uni.getAccountInfoSync().miniProgram.appId
- },
- success: (res) => {
- console.log(1111)
- console.log(res)
- if(res.data.code == 200){
- //判断用户是否注册
- uni.request({
- url:that.verifyLoginUrl,
- method:'GET',
- data:{
- openid:res.data.data.openid
- },
- success:(ress) => {
- if(!ress.data){
-
- }
- }
- })
- //拿到openid和token存储到服务端
- console.log("开始存储")
- uni.setStorageSync("token",res.data.data.token)
- uni.setStorageSync("openid",res.data.data.openid)
- uni.setStorageSync("sessionKey",res.data.data.sessionKey)
- console.log("结束存储")
- }
-
- }
- })
- }
- })
- }
- }
- export {
- Token
- }
|