|
@@ -1,5 +1,6 @@
|
|
//app.js
|
|
//app.js
|
|
import { default as commonApi } from "./api/commonApi"
|
|
import { default as commonApi } from "./api/commonApi"
|
|
|
|
+import { default as requestApi } from "./utils/request"
|
|
import { default as userApi } from "./api/user"
|
|
import { default as userApi } from "./api/user"
|
|
|
|
|
|
import { promisifyAll, promisify } from 'miniprogram-api-promise';
|
|
import { promisifyAll, promisify } from 'miniprogram-api-promise';
|
|
@@ -9,12 +10,14 @@ promisifyAll(wx, wxp)
|
|
App({
|
|
App({
|
|
onLaunch: async function () {
|
|
onLaunch: async function () {
|
|
// 展示本地存储能力
|
|
// 展示本地存储能力
|
|
- if(!wx.getStorageSync('token')){
|
|
|
|
- const tokenData = await commonApi.getToken();
|
|
|
|
- if(tokenData.token){
|
|
|
|
- wx.setStorageSync('token', tokenData.token)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ // if(!wx.getStorageSync('token')){
|
|
|
|
+ // const tokenData = await commonApi.getToken();
|
|
|
|
+ // if(tokenData.token){
|
|
|
|
+ // wx.setStorageSync('token', tokenData.token)
|
|
|
|
+ // requestApi.HEAD.token = tokenData.token
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
|
|
+ const self = this
|
|
// 登录
|
|
// 登录
|
|
const auth = await wxp.getSetting()
|
|
const auth = await wxp.getSetting()
|
|
if (!auth.authSetting['scope.userInfo']){
|
|
if (!auth.authSetting['scope.userInfo']){
|
|
@@ -23,13 +26,53 @@ App({
|
|
}
|
|
}
|
|
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
|
|
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
|
|
const info = await wxp.getUserInfo()
|
|
const info = await wxp.getUserInfo()
|
|
- this.globalData.userInfo = res.userInfo
|
|
|
|
|
|
+ this.globalData.userInfo = info.userInfo
|
|
if (this.userInfoReadyCallback) {
|
|
if (this.userInfoReadyCallback) {
|
|
this.userInfoReadyCallback(res)
|
|
this.userInfoReadyCallback(res)
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ await self.wxLogin(info.userInfo);
|
|
},
|
|
},
|
|
globalData: {
|
|
globalData: {
|
|
userInfo: null
|
|
userInfo: null
|
|
- }
|
|
|
|
|
|
+ },
|
|
|
|
+ wxLogin:async function(data){
|
|
|
|
+ console.log(data)
|
|
|
|
+ if (!data){
|
|
|
|
+ console.log('你还没有授权')
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ let _self = this;
|
|
|
|
+ let res = await wxp.login()
|
|
|
|
+ console.log(res)
|
|
|
|
+ if (!res.code){
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ data['code'] = res.code
|
|
|
|
+ console.log(data)
|
|
|
|
+ let info = await userApi.userLogin(data)
|
|
|
|
+ wx.setStorageSync('token', info.userInfo.token)
|
|
|
|
+ requestApi.HEAD.token = info.userInfo.token
|
|
|
|
+ this.setUserInfo(info.userInfo)
|
|
|
|
+ return info.userInfo
|
|
|
|
+ },
|
|
|
|
+ setUserInfo: function(userInfo){
|
|
|
|
+ wx.setStorage({
|
|
|
|
+ data: userInfo,
|
|
|
|
+ key: 'userInfo',
|
|
|
|
+ });
|
|
|
|
+ this.globalData.userInfo = userInfo
|
|
|
|
+ },
|
|
|
|
+ getWxUserInfo: async function(){
|
|
|
|
+ if (!this.globalData.userInfo){
|
|
|
|
+ try {
|
|
|
|
+ let userInfo = wx.getStorageSync({key: 'userInfo'})
|
|
|
|
+ return userInfo.data
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.log('缓存中没有用户信息')
|
|
|
|
+ return null
|
|
|
|
+ }
|
|
|
|
+ }else {
|
|
|
|
+ return this.globalData.userInfo
|
|
|
|
+ }
|
|
|
|
+ },
|
|
})
|
|
})
|