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