123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- // app.js
- const { utils } = require('./utils/api.js');
- App({
- onLaunch() {
- // 展示本地存储能力
- const logs = wx.getStorageSync('logs') || []
- // logs.unshift(Date.now())
- // wx.setStorageSync('logs', logs)
- // 微信登录获取token
- this.wxLogin()
- },
- // 微信登录方法
- wxLogin() {
- wx.login({
- success: (res) => {
- if (res.code) {
- console.log('微信登录获取code成功:', res.code)
- // 调用登录接口
- this.callLoginApi(res.code)
- } else {
- console.error('微信登录失败:', res.errMsg)
- }
- },
- fail: (error) => {
- console.error('微信登录调用失败:', error)
- }
- })
- },
- // 调用登录接口
- callLoginApi(code) {
- wx.request({
- url: 'https://sit-kelamayi.4dage.com/api/museum/wxMini/login',
- method: 'GET',
- data: {
- code: code
- },
- header: {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- success: (response) => {
- console.log('登录接口调用成功:', response.data)
- if (response.data && response.data.code === 0) {
- // 存储基本用户信息(微信用户)
- const basicUserInfo = {
- nickName: '微信用户',
- avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
- isWxUser: true
- }
- const token = response.data.data.token
- this.globalData.userInfo = basicUserInfo
- wx.setStorageSync('userInfo', basicUserInfo)
- // 存储token到全局数据
- this.globalData.token = token
- // 也存储到本地存储
- wx.setStorageSync('token', token)
- console.log('token获取成功:', token)
- // 登录成功后获取token
- this.getAccessToken()
- } else {
- console.error('登录失败:', response.data.message || '未知错误')
- }
- },
- fail: (error) => {
- console.error('登录接口调用失败:', error)
- }
- })
- },
- // 获取访问token
- getAccessToken() {
- wx.request({
- url: 'https://sit-kelamayi.4dage.com/api/museum/wxMini/getAccessToken',
- method: 'GET',
- header: {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- success: (response) => {
- console.log('获取accessToken接口调用成功:', response.data)
- if (response.data && response.data.code === 0) {
- const accessToken = response.data.data
- // 存储token到全局数据
- this.globalData.accessToken = accessToken
-
- // 也存储到本地存储
- wx.setStorageSync('accessToken', accessToken)
-
- console.log('accessToken获取成功:', accessToken)
- } else {
- console.error('获取token失败:', response.data.message || '未知错误')
- }
- },
- fail: (error) => {
- console.error('获取token接口调用失败:', error)
- }
- })
- },
- globalData: {
- userInfo: null,
- token: null
- }
- })
|