sunxf 2 years ago
parent
commit
a16275d3dd
3 changed files with 73 additions and 34 deletions
  1. 2 0
      api/user.js
  2. 61 33
      app.js
  3. 10 1
      pages/my/index.js

+ 2 - 0
api/user.js

@@ -3,6 +3,8 @@ import {
 } from "../utils/request";
 
 module.exports = {
+    // 用户登录
+    userLogin: data => request('/user/register', 'post', data),
     //获取用户信息
     getUserInfo: data => request('/v3/user/my', 'get', data),
     //获取是否vip及过期时间

+ 61 - 33
app.js

@@ -1,38 +1,66 @@
-//app.js
 import {
-  GetQueryString
+    GetQueryString
 } from 'utils/util'
-
+import {
+    userLogin
+} from '~/api/user'
 // const Towxml = require('/towxml/main');
 App({
-  onLaunch: function (options) {
-    // 判断设备是否为 iPhone X
-    this.checkIsIPhoneX()
-    options.referrerInfo.extraData && (this.globalData.upgradeHide = options.referrerInfo.extraData.upgrade)
-  },
-  globalData: {
-    isIPX: false, // 当前设备是否为 iPhone X
-    isIOS: false, // 判断设备是否为苹果
-    userInfo: null,
-    statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
-    userGrade: '二年级',
-    upgradeHide: true
-  },
-  checkIsIPhoneX: function () {
-    const self = this
-    wx.getSystemInfo({
-      success: function (res) {
-        // 根据 model 进行判断
-        console.log(res)
-        console.log(res.system.search('iOS'))
-        if (res.model.search('iPhone X') != -1) {
-          self.globalData.isIPX = true
-        }
-        if (res.system.search('iOS') != -1) {
-          self.globalData.isIOS = true
-        }
-      }
-    })
-  },
-  // towxml: new Towxml()
+    watch(method) {
+        let obj = this.globalData
+        // 这里监听 openId
+        Object.defineProperty(obj, "openId", {
+            configurable: true,
+            enumerable: true,
+            set: function (value) {
+                console.log('zzeweqweqw', value);
+                method(value) // 触发页面回调函数
+            }
+        })
+    },
+    async onLaunch(options) {
+        wx.login({
+            success(res) {
+                if (res.code) {
+                    console.log(res.code);
+                    // 获取openid
+                    userLogin({
+                        code: res.code,
+                        grade: null
+                    }).then(res => {
+                        console.log(res);
+                    })
+                }
+            }
+        })
+        // 判断设备是否为 iPhone X
+        this.checkIsIPhoneX()
+        options.referrerInfo.extraData && (this.globalData.upgradeHide = options.referrerInfo.extraData.upgrade)
+    },
+    globalData: {
+        isIPX: false, // 当前设备是否为 iPhone X
+        isIOS: false, // 判断设备是否为苹果
+        userInfo: null,
+        statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
+        userGrade: '二年级',
+        upgradeHide: true,
+        openId: null
+    },
+    checkIsIPhoneX: function () {
+        const self = this
+        wx.getSystemInfo({
+            success: function (res) {
+                // 根据 model 进行判断
+                console.log(res)
+                console.log(res.system.search('iOS'))
+                if (res.model.search('iPhone X') != -1) {
+                    self.globalData.isIPX = true
+                }
+                if (res.system.search('iOS') != -1) {
+                    self.globalData.isIOS = true
+                }
+            }
+        })
+    },
+    // towxml: new Towxml()
 })

+ 10 - 1
pages/my/index.js

@@ -10,7 +10,7 @@ import {
 import {
     getOpenidNoLogin
 } from '~/utils/httpUtilNoLogin';
-let app = getApp()
+const app = getApp()
 Page({
     data: {
         userInfo: {},
@@ -23,10 +23,18 @@ Page({
     },
     onLoad() {
         this.getProducts()
+        /* app.watch(() => {
+            console.log('执行了');
+            this.getProducts()
+            this.setUserInfo()
+        }) */
     },
     async onShow() {
         let uid = wx.getStorageSync('uid') || ''
         // 没登陆先走静默登录,登录后直接获取用户信息
+        /*  if (uid) {
+             this.setUserInfo()
+         } */
         if (!uid) {
             getOpenidNoLogin(async () => {
                 this.setUserInfo()
@@ -67,6 +75,7 @@ Page({
         let productNum = products.find(item => {
             return item.type == 2
         })
+        console.log(products);
         this.setData({
             productNum,
             productVip