login.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { api_login, api_queryUserInfo } from "../../api/login"
  2. // pages/login/login.ts
  3. const app = getApp<IAppOption>()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. isAgree: false,
  10. },
  11. /**
  12. * 生命周期函数--监听页面加载
  13. */
  14. onLoad() {},
  15. getLogin(e: any) {
  16. console.log(e, 'any')
  17. const { detail } = e
  18. // if(!this.data.isAgree) {
  19. // wx.showToast({
  20. // title: '请先阅读并勾选协议',
  21. // icon: 'none'
  22. // })
  23. // return
  24. // }
  25. wx.login({
  26. success: (res) => {
  27. const params = {
  28. code: res.code,
  29. iv: detail.iv,
  30. encryptedData: detail.encryptedData
  31. }
  32. this.onLoginSync(params)
  33. }
  34. })
  35. },
  36. onAgree() {
  37. this.setData({
  38. isAgree: !this.data.isAgree
  39. })
  40. },
  41. /** 微信登录 */
  42. async onLoginSync(params: { code: string, iv: string, encryptedData: string }) {
  43. try {
  44. // 开始登录
  45. console.log({
  46. autoRegister: false,
  47. client_id: 'cooleshow-student-wxlite',
  48. client_secret: 'cooleshow-student-wxlite',
  49. deviceNum: app.globalData.deviceNum,
  50. extra: JSON.stringify({
  51. iv: params.iv,
  52. encryptedData: params.encryptedData
  53. }),
  54. grant_type: 'password',
  55. loginType: 'WECHAT_MA',
  56. multiUser: false,
  57. username: app.globalData.appId,
  58. password: params.code
  59. })
  60. const { data } = await api_login({
  61. autoRegister: false,
  62. client_id: 'cooleshow-student-wxlite',
  63. client_secret: 'cooleshow-student-wxlite',
  64. deviceNum: app.globalData.deviceNum,
  65. extra: JSON.stringify({
  66. iv: params.iv,
  67. encryptedData: params.encryptedData
  68. }),
  69. grant_type: 'password',
  70. loginType: 'WECHAT_MA',
  71. multiUser: false,
  72. username: app.globalData.appId,
  73. password: params.code
  74. })
  75. if(data.code === 200) {
  76. const userToken = data.data.authentication.token_type + " " + data.data.authentication.access_token;
  77. wx.setStorageSync("token", userToken);
  78. app.globalData.isLogin = true;
  79. const users = await api_queryUserInfo({ wxAppId: app.globalData.appId })
  80. if(users.data.code === 200) {
  81. app.globalData.userInfo = users.data.data
  82. } else {
  83. wx.removeStorageSync("token");
  84. app.globalData.isLogin = false
  85. }
  86. console.log(users)
  87. } else {
  88. app.globalData.isLogin = false;
  89. }
  90. } catch {
  91. console.log('error')
  92. }
  93. },
  94. /**
  95. * 生命周期函数--监听页面初次渲染完成
  96. */
  97. onReady() {
  98. },
  99. /**
  100. * 生命周期函数--监听页面显示
  101. */
  102. onShow() {
  103. },
  104. /**
  105. * 生命周期函数--监听页面隐藏
  106. */
  107. onHide() {
  108. },
  109. /**
  110. * 生命周期函数--监听页面卸载
  111. */
  112. onUnload() {
  113. },
  114. /**
  115. * 页面相关事件处理函数--监听用户下拉动作
  116. */
  117. onPullDownRefresh() {
  118. },
  119. /**
  120. * 页面上拉触底事件的处理函数
  121. */
  122. onReachBottom() {
  123. },
  124. /**
  125. * 用户点击右上角分享
  126. */
  127. onShareAppMessage() {
  128. }
  129. })