index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. },
  15. onLoad() {
  16. if (!this.storeBindings) {
  17. this.storeBindings = createStoreBindings(this, {
  18. store,
  19. actions: ['setUser']
  20. })
  21. }
  22. },
  23. /**
  24. * 退出页面时触发基础库回调,由基础库内部处理系统登录态。
  25. */
  26. onUnload() {
  27. this.storeBindings.destroyStoreBindings()
  28. },
  29. /**
  30. * 触发小程序登录,登录成功后自动退出页面
  31. */
  32. onTapWeixinMiniProgramLogin() {
  33. wx.weixinMiniProgramLogin({
  34. success: () => {
  35. wx.getMiniProgramCode({
  36. success: async (res) => {
  37. let data = {
  38. code: res.code,
  39. userChannelCode: 3001
  40. }
  41. let userRes = await userLogin(data)
  42. this.setUser(userRes.data)
  43. wx.setStorageSync('uid', userRes.data.uid)
  44. wx.setStorageSync('user', userRes.data)
  45. wx.switchTab({
  46. url: '/pages/index/index',
  47. })
  48. }
  49. })
  50. },
  51. fail: () => {
  52. wx.showToast({
  53. title: '小程序登录失败',
  54. icon: 'none'
  55. });
  56. }
  57. })
  58. },
  59. onCheckboxChange() {
  60. this.setData({
  61. checkedAgree: !this.data.checkedAgree
  62. });
  63. },
  64. /**
  65. *
  66. * 使用单独的 webview 页面展示用户协议
  67. */
  68. onShowAgreement(e) {
  69. const urls = [
  70. 'link1',
  71. 'link2'
  72. ];
  73. const url = urls[e.target.dataset.idx];
  74. // wx.navigateTo({
  75. // url: `/pages/webview/index?url=${url}`,
  76. // });
  77. },
  78. })