123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- // 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,
- });
- },
- })
|