Browse Source

onlauch 和 onlad 异步了 卸载在进去bug

roose 4 năm trước cách đây
mục cha
commit
20682d6d57
2 tập tin đã thay đổi với 110 bổ sung5 xóa
  1. 86 0
      app.js
  2. 24 5
      pages/index/index.js

+ 86 - 0
app.js

@@ -5,6 +5,7 @@ App({
     name: null,
   },
   onLaunch(options) {
+    console.log("onLaunchonLaunchonLaunchonLaunchonLaunchonLaunchonLaunchonLaunchonLaunchonLaunchonLaunchonLaunch");
     // if (wx.getStorageSync('openId')) {
     //   wx.request({
     //     url: 'http://info.windd.cn:8080/test/applet/isExist',
@@ -24,7 +25,44 @@ App({
     // } else {
 
     // }
+    // wx.login({
+    //   success: res => {
+    //     // 获取到用户的 code 之后:res.code
+    //     console.log("用户的code:" + res.code);
+    //     const appid = "wx1c015df104db7030"
+    //     const secret = "d1956c1b5d3601657c98b0dc80a006f7"
+    //     let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + res.code + '&grant_type=authorization_code';
+    //     wx.request({
+    //       // 自行补上自己的 APPID 和 SECRET
+    //       url: url,
+    //       success: res => {
+    //         // 获取到用户的 openid
+    //         console.log("用户的openid:" + res.data.openid);
+    //         wx.setStorageSync('openId', res.data.openid)
+
+    //         wx.request({
+    //           url: 'http://info.windd.cn:8080/test/applet/isExist',
+    //           method: "GET",
+    //           data: {
+    //             appletsId: wx.getStorageSync('openId')
+    //           },
+    //           success: (res) => {
+    //             console.log(res, "apppppppppppppppppppppppppp");
+    //             if (res.data.code == 0) {
+    //               if(res.data.data){
+    //                 wx.setStorageSync('user', 'user');
+    //               }
+    //             }
+
+    //           }
+    //         })
+    //       }
+    //     });
+    //   }
+    // });
+
   },
+ 
   onShow(options) {
     // Do something when show.
   },
@@ -34,4 +72,52 @@ App({
   onError(msg) {
     console.log(msg)
   },
+  getAuthKey: function () {  
+    var that = this;  
+    return new Promise(function (resolve, reject) {  
+        // 调用登录接口  
+        wx.login({  
+          success: function (res) {  
+            if (res.code) {  
+              console.log("用户的code:" + res.code);
+              const appid = "wx1c015df104db7030"
+              const secret = "d1956c1b5d3601657c98b0dc80a006f7"
+              let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + res.code + '&grant_type=authorization_code';
+              wx.request({
+                // 自行补上自己的 APPID 和 SECRET
+                url: url,
+                success: res => {
+                  // 获取到用户的 openid
+                  console.log("用户的openid:" + res.data.openid);
+                  wx.setStorageSync('openId', res.data.openid)
+                  wx.request({
+                    url: 'http://info.windd.cn:8080/test/applet/isExist',
+                    method: "GET",
+                    data: {
+                      appletsId: wx.getStorageSync('openId')
+                    },
+                    success: (res) => {
+                      console.log(res, "apppppppppppppppppppppppppp");
+                      if (res.data.code == 0) {
+                        if(res.data.data){
+                          wx.setStorageSync('user', 'user');
+                        }
+                        resolve(res);  
+                      }
+                    }
+                  })
+                }
+              });
+            } else {  
+              console.log('获取用户登录态失败!' + res.errMsg);  
+              var res = {  
+                status: 300,  
+                data: '错误'  
+              }  
+              reject('error');  
+            }    
+          }  
+        })  
+    });  
+  },  
 })

+ 24 - 5
pages/index/index.js

@@ -88,8 +88,8 @@ Page({
     })
   },
   depts() {
-    console.log(wx.getStorageSync('openId'), "openid的问题");
-
+    console.log("打印走了么的接口呢");
+    console.log(wx.getStorageSync('openId'));
     wx.request({
       url: app.globalData.publicUrl + '/applet/homeStatistics',
       method: "GET",
@@ -106,6 +106,8 @@ Page({
     })
   },
   welcomeMessage() {
+    console.log("welcomeMessage");
+    console.log(wx.getStorageSync('openId'));
     this.setData({
       msgList: []
     })
@@ -174,11 +176,28 @@ Page({
     }
 
   },
-  onLoad() {
-    this.depts();
-  },
+  // onLoad() {
+  //   console.log("onloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonloadonload");
+  //   this.depts();
+  // },
+
+  
   onShow() {
+    console.log("onshowssshshshhshshshss");
     this.depts();
     this.welcomeMessage();
+    
+  },
+  onLoad: function () {
+    console.log("onload11111111111111111111111");
+    var that = this;
+    const apps = getApp()
+    apps.getAuthKey().then(function (res) {
+      that.depts();
+    })
+    console.log("onload12222222222222222222222222222221");
+
   }
+
+  
 })