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.
 
 

99 lines
2.9 KiB

  1. const request = require('./utils/request')
  2. import { timestampToTime } from './utils/util'
  3. App({
  4. globalData: {
  5. },
  6. onLaunch() {
  7. const that = this
  8. // 登录
  9. wx.login({
  10. success: res => {
  11. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  12. console.log(res, 'wx.login')
  13. doLogin(res.code)
  14. getWxToken()
  15. },
  16. })
  17. // 获取小程序AccessToken
  18. const getWxToken = () => {
  19. const data = {
  20. grant_type: 'client_credential',
  21. appid: 'wxf288a9b8167ff2ca',
  22. secret: 'cf012a9015594f0a95f718a801497fbd'
  23. }
  24. wx.request({
  25. url: 'https://api.weixin.qq.com/cgi-bin/stable_token',
  26. header: {
  27. "Content-Type": "application/json;charset=UTF-8",
  28. },
  29. data: data,
  30. method: "POST",
  31. success: res => {
  32. console.log(res, 'getWxToken');
  33. wx.setStorageSync('AccessToken', res.data.access_token)
  34. },
  35. fail: err => { }
  36. });
  37. }
  38. /**
  39. * @description 登录
  40. * @param {*} loginData {code , appid}
  41. * @returns token , sessionKey , openId
  42. */
  43. const doLogin = code => {
  44. const data = {
  45. code,
  46. appId: request.appId
  47. }
  48. request.post({
  49. url: '/api/user/login',
  50. data
  51. }).then(res => {
  52. console.log(res, 'loginSuccess');
  53. // 存储数据
  54. request.setHead(res.data.token, res.data.tenantId)
  55. wx.setStorageSync('sessionKey', res.data.sessionKey)
  56. that.globalData.sessionKey = res.data.sessionKey
  57. wx.setStorageSync('openId', res.data.openId)
  58. that.globalData.openId = res.data.openId
  59. wx.setStorageSync('token', res.data.token)
  60. that.globalData.token = res.data.token
  61. // 检查用户信息
  62. checkUserInfo()
  63. }).catch(err => {
  64. console.log(err);
  65. wx.showToast({
  66. title: '网络错误,请稍后再试',
  67. icon: 'none'
  68. })
  69. })
  70. }
  71. /**
  72. * @description 检查用户信息
  73. * @returns userInfo
  74. */
  75. const checkUserInfo = () => {
  76. const that = this
  77. request.get({
  78. url: '/api/user/userinfo'
  79. }).then(res => {
  80. console.log(res, 'userinfo');
  81. res.data.createDate = res.data.createDate ? timestampToTime(res.data.createDate, 'YYYY-MM-DD hh:mm:ss') : ''
  82. res.data.updateDate = res.data.updateDate ? timestampToTime(res.data.updateDate, 'YYYY-MM-DD hh:mm:ss') : ''
  83. res.data.validStartTime = res.data.validStartTime ? timestampToTime(res.data.validStartTime, 'YYYY-MM-DD hh:mm:ss') : ''
  84. res.data.validEndTime = res.data.validEndTime ? timestampToTime(res.data.validEndTime, 'YYYY-MM-DD hh:mm:ss') : ''
  85. const userInfo = res.data
  86. that.globalData.userInfo = userInfo
  87. // 请求完成后的回调,在index.js中调用防止异步
  88. that.userInfoCallback(res)
  89. }).catch(err => {
  90. console.log(err, 'err');
  91. })
  92. }
  93. },
  94. })