邃芒智像相册
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

60 lines
1.4 KiB

  1. import { timestampToTime } from './utils/util'
  2. const request = require('./utils/request')
  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 that = this
  30. const data = {
  31. code,
  32. appId: request.appId
  33. }
  34. request.post({
  35. url: '/api/miniApp/login',
  36. data
  37. }).then(res => {
  38. console.log(res, 'loginSuccess');
  39. // 存储数据
  40. wx.setStorageSync('openId', res.data.openId)
  41. if (res.data.sessionKey) {
  42. wx.setStorageSync('sessionKey', res.data.sessionKey)
  43. }
  44. if (res.data.token) {
  45. wx.setStorageSync('token', res.data.token)
  46. request.setHead(res.data.token)
  47. }
  48. that.tokenCallBack(res.data.token || false)
  49. }).catch(err => {
  50. console.log(err);
  51. wx.showToast({
  52. title: '网络错误,请稍后再试',
  53. icon: 'none'
  54. })
  55. })
  56. }
  57. },
  58. })