Pārlūkot izejas kodu

更新 获取手机号openid逻辑

wzh 3 gadi atpakaļ
vecāks
revīzija
bd00a650b5

+ 3 - 2
miniprogram/app.js

@@ -1,9 +1,9 @@
 import {
-  wxSetSessionKey,
   getOpenId
 } from "./utils/wxUtils"
 App({
   onLaunch: function () {
+    getOpenId()
     const updateManager = wx.getUpdateManager()
 
     updateManager.onCheckForUpdate(function (res) {
@@ -39,6 +39,7 @@ App({
   checkEnvVersion() {
     let v = wx.getAccountInfoSync()
     let appVersion = wx.getStorageSync('appVersion')
+    let openId = wx.getStorageSync('openId')
     let {
       envVersion
     } = v.miniProgram
@@ -46,12 +47,12 @@ App({
       wx.clearStorage({
         success: (res) => {
           wx.setStorageSync('appVersion', envVersion)
+          wx.setStorageSync('openId', openId)
           wx.redirectTo({
             url: '/pages/index/index',
           })
         },
       })
     }
-    wxSetSessionKey()
   }
 })

+ 6 - 19
miniprogram/pages/newCachePage/newCachePage.js

@@ -1,5 +1,4 @@
 // pages/newCachePage/newCahePage.js
-import cloudApi from "../../apis/cloudApi"
 import {
   postApi
 } from "../../apis/api"
@@ -171,16 +170,13 @@ Page({
           title: '正在登录...',
           mask: true
         })
-        let session_key = wx.getStorageSync('session_key')
-        let {
-          result
-        } = await cloudApi("getWxPhoneNumber", {
-          ...e.detail,
-          session_key
+
+        let res0 = await postApi('/wx/getPhoneNumber', {
+          code: e.detail.code
         })
         let {
-          phone,
-        } = result
+          phoneNumber: phone
+        } = res0.data.result;
         if (phone) {
           wx.setStorageSync('phone', phone)
           try {
@@ -298,24 +294,15 @@ Page({
             title: '获取手机号失败',
             duration: 5000
           })
-          await cloudApi('sendError', {
-            msg: "获取手机号失败"
-          })
         }
       } else {
         wx.showToast({
           title: '请授权以登录',
           icon: "error"
         })
-        await cloudApi('sendError', {
-          msg: "未授权手机号"
-        })
       }
     } catch (error) {
-      await cloudApi('sendError', {
-        msg: "全局trycatch",
-        error
-      })
+
     }
 
 

+ 13 - 17
miniprogram/utils/wxUtils.js

@@ -1,3 +1,6 @@
+import {
+  postApi
+} from "../apis/api"
 import cloudApi from "../apis/cloudApi"
 
 function wxSetSessionKey() {
@@ -44,29 +47,22 @@ function getUserProfile() {
 
 function getOpenId() {
   return new Promise(async (resolve, reject) => {
-    let res1 = await cloudApi('getOpenId')
-    resolve(res1.result.openId)
-  })
-
-}
-
-function subMsg() {
-  return new Promise((resolve, reject) => {
-    wx.requestSubscribeMessage({
-      tmplIds: ['q1joCPFWjhAxSJtrZ30QFi_aA9LVva4PQZmBcxZIPhU'],
-      success: res => {
-        console.log(res)
-      },
-      complete: e => {
-        console.log(e)
+    wx.login({
+      success: async e => {
+        let res1 = await postApi('/wx/getOpenId', {
+          code: e.code
+        })
+        wx.setStorageSync('openId', res1.data.result)
+        resolve(res1.data.result)
       }
     })
+
   })
+
 }
 
 module.exports = {
   wxSetSessionKey,
   getUserProfile,
-  getOpenId,
-  subMsg
+  getOpenId
 }