index.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import {
  2. userLogin,
  3. } from '~/api/user'
  4. import {
  5. createStoreBindings
  6. } from 'mobx-miniprogram-bindings'
  7. import {
  8. store
  9. } from '~/store/index'
  10. let storeBindings
  11. Page({
  12. data: {
  13. checkedAgree: false,
  14. loginSuccess: false, // 标记是否登录成功
  15. },
  16. onLoad() {
  17. if (!this.storeBindings) {
  18. this.storeBindings = createStoreBindings(this, {
  19. store,
  20. actions: ['setUser']
  21. })
  22. }
  23. },
  24. /**
  25. * 退出页面时触发基础库回调,由基础库内部处理系统登录态。
  26. */
  27. onUnload() {
  28. this.storeBindings.destroyStoreBindings()
  29. const eventChannel = this.getOpenerEventChannel();
  30. if (eventChannel) {
  31. eventChannel.emit('__donutLogin__', {
  32. success: this.data.loginSuccess
  33. });
  34. }
  35. },
  36. /**
  37. * 触发小程序登录,登录成功后自动退出页面
  38. */
  39. onTapWeixinMiniProgramLogin() {
  40. wx.weixinMiniProgramLogin({
  41. success: () => {
  42. this.setData({
  43. loginSuccess: true
  44. });
  45. wx.getMiniProgramCode({
  46. success: async (res) => {
  47. console.log(res);
  48. let data = {
  49. code: res.code,
  50. userChannelCode: 3001
  51. }
  52. let userRes = await userLogin(data)
  53. this.setUser(userRes.data)
  54. wx.setStorageSync('uid', userRes.data.uid)
  55. wx.setStorageSync('user', userRes.data)
  56. wx.switchTab({
  57. url: '/pages/index/index',
  58. })
  59. }
  60. })
  61. },
  62. fail: () => {
  63. wx.showToast({
  64. title: '小程序登录失败',
  65. icon: 'none'
  66. });
  67. }
  68. })
  69. },
  70. onCheckboxChange() {
  71. this.setData({
  72. checkedAgree: !this.data.checkedAgree
  73. });
  74. },
  75. /**
  76. *
  77. * 使用单独的 webview 页面展示用户协议
  78. */
  79. onShowAgreement(e) {
  80. const urls = [
  81. 'link1',
  82. 'link2'
  83. ];
  84. const url = urls[e.target.dataset.idx];
  85. // wx.navigateTo({
  86. // url: `/pages/webview/index?url=${url}`,
  87. // });
  88. },
  89. })