邃芒智像相册
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

58 lines
1.3 KiB

  1. const request = require('./utils/request')
  2. import { timestampToTime } from './utils/util'
  3. App({
  4. globalData: {
  5. sessionKey: "",
  6. openId: "",
  7. token: "",
  8. userInfo: null,
  9. promotContentCount: "",
  10. completionContentCount: "",
  11. noticeText: ""
  12. },
  13. onLaunch() {
  14. const that = this
  15. // 登录
  16. wx.login({
  17. success: res => {
  18. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  19. console.log(res, 'wx.login')
  20. doLogin(res.code)
  21. },
  22. })
  23. /**
  24. * @description 登录
  25. * @param {*} loginData {code , appid}
  26. * @returns token , sessionKey , openId
  27. */
  28. const doLogin = code => {
  29. const data = {
  30. code,
  31. appId: request.appId
  32. }
  33. request.post({
  34. url: '/api/miniApp/login',
  35. data
  36. }).then(res => {
  37. console.log(res, 'loginSuccess');
  38. // 存储数据
  39. wx.setStorageSync('openId', res.data.openId)
  40. if (res.data.sessionKey) {
  41. wx.setStorageSync('sessionKey', res.data.sessionKey)
  42. }
  43. if (res.data.token) {
  44. wx.setStorageSync('token', res.data.token)
  45. request.setHead(res.data.token)
  46. }
  47. }).catch(err => {
  48. console.log(err);
  49. wx.showToast({
  50. title: '网络错误,请稍后再试',
  51. icon: 'none'
  52. })
  53. })
  54. }
  55. },
  56. })