index.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // index.js
  2. // 获取应用实例
  3. import { getopenid, getmobile, wxlogin } from "../../serverUser";
  4. import {
  5. createStoreBindings
  6. } from 'mobx-miniprogram-bindings'
  7. import {
  8. store
  9. } from "../../../store/index";
  10. Page({
  11. data: {
  12. radio: false
  13. },
  14. onLoad() {
  15. this.storeBindings = createStoreBindings(this, {
  16. store,
  17. actions: ["setStore"],
  18. });
  19. },
  20. onUnload() {
  21. this.storeBindings.destroyStoreBindings();
  22. },
  23. onShow: function () {
  24. // wx.hideHomeButton();
  25. },
  26. loginTap() {
  27. const {
  28. radio
  29. } = this.data
  30. if (radio === false) {
  31. this.showToastMsg('请阅读并勾选用户协议!')
  32. return
  33. }
  34. },
  35. clickTap(e: any) {
  36. const {
  37. type
  38. } = e.currentTarget.dataset
  39. wx.navigateTo({
  40. url: `../${type}/index`
  41. })
  42. },
  43. getPhoneNumber(e) {
  44. if (e.detail.code) {
  45. // 通过wx.login获取登录凭证(code),然后通过code去获取我们用户的openid
  46. let openid = "",
  47. phoneNumber = ""
  48. wx.login({
  49. success: async (res) => {
  50. openid = await this.getWXOpenid(res.code)
  51. if (openid) {
  52. phoneNumber = await this.getPhone(e.detail.code)
  53. if (phoneNumber) {
  54. const aa = await this.register(openid, phoneNumber)
  55. wx.reLaunch({
  56. url: '/pages/index/index',
  57. })
  58. }
  59. } else {
  60. wx.reLaunch({
  61. url: '/pages/index/index',
  62. })
  63. }
  64. },
  65. })
  66. }
  67. },
  68. async getWXOpenid(loginCode: string) {
  69. let openid = ""
  70. const {
  71. code,
  72. data
  73. } = await getopenid({
  74. code: loginCode
  75. })
  76. if (code === 1) {
  77. openid = data.openid
  78. if (!openid) {
  79. await this.setStore(data.token, data.mobile)
  80. }
  81. }
  82. return openid
  83. },
  84. async getPhone(code: string) {
  85. let phoneNumber = ""
  86. const {
  87. code: rcode,
  88. data,
  89. msg: rmsg
  90. } = await getmobile({
  91. code
  92. })
  93. if (rcode === 1) {
  94. const {
  95. phone_info
  96. } = data
  97. phoneNumber = phone_info.phoneNumber
  98. } else if (rcode === 101) {
  99. } else {
  100. this.showToastMsg(rmsg)
  101. }
  102. return phoneNumber
  103. },
  104. async register(openid: string, mobile: string) {
  105. let isok = false
  106. const {
  107. code: rcode,
  108. data,
  109. msg: rmsg
  110. } = await wxlogin({
  111. openid,
  112. mobile
  113. })
  114. if (rcode === 1) {
  115. isok = true
  116. await this.setStore(data.token, data.mobile)
  117. } else if (rcode === 101) {
  118. this.showToastMsg('用户状态异常')
  119. } else {
  120. this.showToastMsg(rmsg)
  121. }
  122. return isok
  123. },
  124. showToastMsg(msg: string) {
  125. wx.showToast({
  126. title: msg,
  127. icon: 'none',
  128. duration: 2000
  129. })
  130. },
  131. radioTap() {
  132. this.setData({
  133. radio: !this.data.radio,
  134. });
  135. },
  136. })