login.ts 3.1 KB

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