login.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /**
  90. * 生命周期函数--监听页面初次渲染完成
  91. */
  92. onReady() {
  93. },
  94. /**
  95. * 生命周期函数--监听页面显示
  96. */
  97. onShow() {
  98. },
  99. /**
  100. * 生命周期函数--监听页面隐藏
  101. */
  102. onHide() {
  103. },
  104. /**
  105. * 生命周期函数--监听页面卸载
  106. */
  107. onUnload() {
  108. },
  109. /**
  110. * 页面相关事件处理函数--监听用户下拉动作
  111. */
  112. onPullDownRefresh() {
  113. },
  114. /**
  115. * 页面上拉触底事件的处理函数
  116. */
  117. onReachBottom() {
  118. },
  119. /**
  120. * 用户点击右上角分享
  121. */
  122. onShareAppMessage() {
  123. }
  124. })