// index.js // 获取应用实例 import { getopenid, getmobile, wxlogin } from "../../serverUser"; import { createStoreBindings } from 'mobx-miniprogram-bindings' import { store } from "../../../store/index"; Page({ data: { radio: false }, onLoad() { this.storeBindings = createStoreBindings(this, { store, actions: ["setStore"], }); }, onUnload() { this.storeBindings.destroyStoreBindings(); }, onShow: function () { // wx.hideHomeButton(); }, loginTap() { const { radio } = this.data if (radio === false) { this.showToastMsg('请阅读并勾选用户协议!') return } }, clickTap(e: any) { const { type } = e.currentTarget.dataset wx.navigateTo({ url: `../${type}/index` }) }, getPhoneNumber(e) { if (e.detail.code) { // 通过wx.login获取登录凭证(code),然后通过code去获取我们用户的openid let openid = "", phoneNumber = "" wx.login({ success: async (res) => { openid = await this.getWXOpenid(res.code) if (openid) { phoneNumber = await this.getPhone(e.detail.code) if (phoneNumber) { const aa = await this.register(openid, phoneNumber) wx.reLaunch({ url: '/pages/index/index', }) } } else { wx.reLaunch({ url: '/pages/index/index', }) } }, }) } }, async getWXOpenid(loginCode: string) { let openid = "" const { code, data } = await getopenid({ code: loginCode }) if (code === 1) { openid = data.openid if (!openid) { await this.setStore(data.token, data.mobile) } } return openid }, async getPhone(code: string) { let phoneNumber = "" const { code: rcode, data, msg: rmsg } = await getmobile({ code }) if (rcode === 1) { const { phone_info } = data phoneNumber = phone_info.phoneNumber } else if (rcode === 101) { } else { this.showToastMsg(rmsg) } return phoneNumber }, async register(openid: string, mobile: string) { let isok = false const { code: rcode, data, msg: rmsg } = await wxlogin({ openid, mobile }) if (rcode === 1) { isok = true await this.setStore(data.token, data.mobile) } else if (rcode === 101) { this.showToastMsg('用户状态异常') } else { this.showToastMsg(rmsg) } return isok }, showToastMsg(msg: string) { wx.showToast({ title: msg, icon: 'none', duration: 2000 }) }, radioTap() { this.setData({ radio: !this.data.radio, }); }, })