electricity_bill_calc_wx/pages/login/index.js

112 lines
2.5 KiB
JavaScript

// pages/login/index.js
import { getPrivaciList, login } from "../../service/user";
import { alertError, alertInfo, alertSuccess, loadingFunc, wxLogin } from "../../utils/index";
import request from "../../utils/request";
import { getUserInfo } from "../../service/user"
import Dialog from '@vant/weapp/dialog/dialog';
const { OK } = request;
Page({
/**
* 页面的初始数据
*/
data: {
input: "",
disabled: false,
show: false,
privacyList: [],
},
/**
* 生命周期函数--监听页面加载
*/
async setUser() {
const result = await getUserInfo();
if (result.code !== OK) {
return;
}
this.setData({ user: result.data })
wx.setStorageSync('user', result.data)
},
onLoad() {
this.getPrivacy();
},
async getPrivacy() {
const { code, message, data } = await getPrivaciList()
if (code !== OK) {
alertError(message)
return;
}
this.setData({ privacyList: data })
},
async handleLogin(phoneCode) {
loadingFunc(async () => {
const wxLoginCode = await wxLogin()
const result = await login({ code: wxLoginCode, phoneCode })
const { code, message, data } = result;
if (code !== OK) {
alertError(message)
return
}
if (!data?.id) {
this.noPermission()
return;
}
alertSuccess("注册成功")
const { token, ...user } = data
wx.setStorageSync('user', user)
wx.setStorageSync('token', data?.token)
wx.switchTab({
url: '/pages/home/index',
})
})
},
getPhoneNumber(e) {
const { errno, code: phoneCode } = e.detail;
switch(errno) {
case 103:
alertInfo("已拒绝");
return;
case 1400001:
alertInfo("服务达到上限")
return;
default:
this.handleLogin(phoneCode)
return;
}
},
onChangeAgree(e) {
this.setData({
agree: e.detail
})
},
beforeClose(e) {
return false;
},
noPermission() {
Dialog.confirm({
title: '提示',
message: '当前手机号未录入系统,请联系管理员,或点击手动绑定',
confirmButtonText: '手动绑定',
cancelButtonText: '取消',
})
.then(() => {
// on confirm
wx.navigateTo({
url: '/pages/handleLogin/index',
})
})
.catch(() => {
// on cancel
});
},
jumpToUserAgreement(e) {
const { url } = e.currentTarget.dataset
wx.navigateTo({
url: '/childPackage/pages/agreements/index?url=' + url,
})
},
})