|
- import { timestampToTime } from './utils/util'
- const request = require('./utils/request')
-
- App({
- globalData: {
- sessionKey: "",
- openId: "",
- token: "",
- userInfo: null,
- promotContentCount: "",
- completionContentCount: "",
- noticeText: ""
- },
- onLaunch() {
- const that = this
- // 登录
- wx.login({
- success: res => {
- // 发送 res.code 到后台换取 openId, sessionKey, unionId
- console.log(res, 'wx.login')
- doLogin(res.code)
- },
- })
-
- /**
- * @description 登录
- * @param {*} loginData {code , appid}
- * @returns token , sessionKey , openId
- */
- const doLogin = code => {
- const that = this
- const data = {
- code,
- appId: request.appId
- }
- request.post({
- url: '/api/miniApp/login',
- data
- }).then(res => {
- console.log(res, 'loginSuccess');
- // 存储数据
- wx.setStorageSync('openId', res.data.openId)
- if (res.data.sessionKey) {
- wx.setStorageSync('sessionKey', res.data.sessionKey)
- }
- if (res.data.token) {
- wx.setStorageSync('token', res.data.token)
- request.setHead(res.data.token)
- }
- that.tokenCallBack(res.data.token || false)
- }).catch(err => {
- console.log(err);
- wx.showToast({
- title: '网络错误,请稍后再试',
- icon: 'none'
- })
- })
- }
- },
- })
|