app.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // app.js
  2. const { utils } = require('./utils/api.js');
  3. App({
  4. onload() {
  5. wx.hideTabBar();
  6. },
  7. onLaunch() {
  8. // 展示本地存储能力
  9. const logs = wx.getStorageSync('logs') || []
  10. // logs.unshift(Date.now())
  11. // wx.setStorageSync('logs', logs)
  12. // 微信登录获取token
  13. this.wxLogin()
  14. },
  15. // 微信登录方法
  16. wxLogin() {
  17. wx.login({
  18. success: (res) => {
  19. if (res.code) {
  20. console.log('微信登录获取code成功:', res.code)
  21. // 调用登录接口
  22. this.callLoginApi(res.code)
  23. } else {
  24. console.error('微信登录失败:', res.errMsg)
  25. }
  26. },
  27. fail: (error) => {
  28. console.error('微信登录调用失败:', error)
  29. }
  30. })
  31. },
  32. // 调用登录接口
  33. callLoginApi(code) {
  34. wx.request({
  35. url: 'https://sit-kelamayi.4dage.com/api/museum/wxMini/login',
  36. method: 'GET',
  37. data: {
  38. code: code
  39. },
  40. header: {
  41. 'content-type': 'application/x-www-form-urlencoded'
  42. },
  43. success: (response) => {
  44. console.log('登录接口调用成功:', response.data)
  45. if (response.data && response.data.code === 0) {
  46. // 存储基本用户信息(微信用户)
  47. const basicUserInfo = {
  48. nickName: '微信用户',
  49. avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
  50. isWxUser: true
  51. }
  52. const token = response.data.data.token
  53. this.globalData.userInfo = basicUserInfo
  54. wx.setStorageSync('userInfo', basicUserInfo)
  55. // 存储token到全局数据
  56. this.globalData.token = token
  57. // 也存储到本地存储
  58. wx.setStorageSync('token', token)
  59. console.log('token获取成功:', token)
  60. // 登录成功后获取token
  61. this.getAccessToken()
  62. } else {
  63. console.error('登录失败:', response.data.message || '未知错误')
  64. }
  65. },
  66. fail: (error) => {
  67. console.error('登录接口调用失败:', error)
  68. }
  69. })
  70. },
  71. // 获取访问token
  72. getAccessToken() {
  73. wx.request({
  74. url: 'https://sit-kelamayi.4dage.com/api/museum/wxMini/getAccessToken',
  75. method: 'GET',
  76. header: {
  77. 'content-type': 'application/x-www-form-urlencoded'
  78. },
  79. success: (response) => {
  80. console.log('获取accessToken接口调用成功:', response.data)
  81. if (response.data && response.data.code === 0) {
  82. const accessToken = response.data.data
  83. // 存储token到全局数据
  84. this.globalData.accessToken = accessToken
  85. // 也存储到本地存储
  86. wx.setStorageSync('accessToken', accessToken)
  87. console.log('accessToken获取成功:', accessToken)
  88. } else {
  89. console.error('获取token失败:', response.data.message || '未知错误')
  90. }
  91. },
  92. fail: (error) => {
  93. console.error('获取token接口调用失败:', error)
  94. }
  95. })
  96. },
  97. globalData: {
  98. userInfo: null,
  99. token: null
  100. }
  101. })