index.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. console.log(res);
  38. let data = {
  39. code: res.code,
  40. userChannelCode: 3001
  41. }
  42. let userRes = await userLogin(data)
  43. this.setUser(userRes.data)
  44. wx.setStorageSync('uid', userRes.data.uid)
  45. wx.setStorageSync('user', userRes.data)
  46. wx.switchTab({
  47. url: '/pages/index/index',
  48. })
  49. }
  50. })
  51. },
  52. fail: () => {
  53. wx.showToast({
  54. title: '小程序登录失败',
  55. icon: 'none'
  56. });
  57. }
  58. })
  59. },
  60. onCheckboxChange() {
  61. this.setData({
  62. checkedAgree: !this.data.checkedAgree
  63. });
  64. },
  65. /**
  66. *
  67. * 使用单独的 webview 页面展示用户协议
  68. */
  69. onShowAgreement(e) {
  70. const urls = [
  71. 'link1',
  72. 'link2'
  73. ];
  74. const url = urls[e.target.dataset.idx];
  75. // wx.navigateTo({
  76. // url: `/pages/webview/index?url=${url}`,
  77. // });
  78. },
  79. })