王飞
2025-01-23 99365608dec6eade7d645a91fb0f2205a332d1f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// app.js
App({
    globalData: { 
        // base_url: 'http://192.168.31.112:6544/', // 全局基本接口地址
          base_url: 'https://hj.iotposition.com:6544/', // 全局基本接口地址
        // base_url: 'http://47.108.70.204:6544/', // 全局基本接口地址
        token: null
      },
      onLaunch() {
        this.checkLogin(res => {
            console.log('is_login : ', res.is_login);
            if (!res.is_login) {
                wx.reLaunch({ url: '/pages/home/home' })
            //   this.login();
            }  else  {
                wx.reLaunch({ url: '/pages/index/index' })
            //   this.login();
            }
          })
      },
  //微信登录逻辑
  login: function () {
    let _this = this
    wx.login({
      success: (res) => {
        //console.log("code: " + res.code);
        wx.request({
          url: _this.globalData.base_url + "/wxlogin",
          header: {
            'content-type': 'application/json;charset=UTF-8'
          },
          method: 'POST',
          data: {
            code: res.code
          },
          success: (res) => {
            console.log("token : " + res.data.data)
            _this.globalData.token = res.data.data
            wx.setStorage({
              key: 'token',
              data: res.data.data
            })
          },
          fail: (error) => {
            console.error("QwQ请求失败:", error);
            wx.showToast({
              title: '请求失败',
              icon: 'none',
              duration: 2000
            });
          }
        })
      }
    })
  },
 
//检查是否登录
checkLogin: function (callback) {
    let _this = this
    var token = _this.globalData.token
 
    if (!token) {
      token = wx.getStorageSync('token')
      if (token) {
        _this.globalData.token = token
      } else {
        callback({
          is_login: false
        })
      }
    }
    // wx.request({
    //   url: _this.globalData.base_url + "/checkwxlogin",
    //   data: {
    //     token: token
    //   },
    //   header:{
    //     Authorization:'Bearer '+ _this.globalData.token
    //   },
    //   success: (res) => {
    //       console.log(res);
    //     callback({
    //       is_login: res.data.code == 0 ? true : false 
    //     })
    //   }
    // })
  },
 
 
 
})