index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.showToast({
  46. icon: 'loading',
  47. title: '登录成功!',
  48. })
  49. wx.switchTab({
  50. url: '/pages/index/index',
  51. })
  52. }
  53. })
  54. },
  55. fail: () => {
  56. wx.showToast({
  57. title: '小程序登录失败',
  58. icon: 'none'
  59. });
  60. }
  61. })
  62. },
  63. onCheckboxChange() {
  64. this.setData({
  65. checkedAgree: !this.data.checkedAgree
  66. });
  67. },
  68. /**
  69. *
  70. * 使用单独的 webview 页面展示用户协议
  71. */
  72. onShowAgreement(e) {
  73. const urls = [
  74. 'link1',
  75. 'link2'
  76. ];
  77. const url = urls[e.target.dataset.idx];
  78. // wx.navigateTo({
  79. // url: `/pages/webview/index?url=${url}`,
  80. // });
  81. },
  82. })