electricity_bill_calc_wx/pages/apply/index.js
2024-03-27 17:37:11 +08:00

158 lines
3.1 KiB
JavaScript

import { getTenementDetail } from "../../service/tenement";
import { userApply } from "../../service/user";
import { alertInfo, alertSuccess } from "../../utils/index";
import request from '../../utils/request'
const { OK } = request;
// pages/apply/index.js
Page({
/**
* 页面的初始数据
*/
data: {
id: "",
tenement: {},
nickName: ""
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
console.log('options', options)
const querys = decodeURIComponent(options.scene)
console.log('querys', querys)
// const { id } = querys;
const id = querys.slice(querys.indexOf("=") + 1)
console.log('id', id)
// this.setData({ id, name })
this.getInfo(id);
},
async getInfo(id) {
const { code, message, data } = await getTenementDetail(id);
if (code !== OK) {
alertInfo(message)
return
}
this.setData({
tenement: data,
id
})
},
exit() {
wx.exitMiniProgram()
},
onChangeName(e) {
this.setData({
nickName: e.detail
})
},
async handleLogin(phoneCode) {
const { id, nickName } = this.data;
loadingFunc(async () => {
const wxLoginCode = await wxLogin()
const result = await userApply({ code: wxLoginCode, phoneCode, id, name: nickName })
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 { nickName } = this.data;
if (!nickName) {
alertInfo("请填写你的名字")
return;
}
const { errno, code: phoneCode } = e.detail;
switch(errno) {
case 103:
alertInfo("已拒绝");
return;
case 1400001:
alertInfo("服务达到上限")
return;
default:
this.handleLogin(phoneCode)
return;
}
},
async join() {
const { id } = this.data;
const { code, message, data } = await userApply({ id })
if (code !== OK) {
alertInfo(message)
return;
}
alertSuccess("加入成功")
const { token, ...currentUser } = data;
wx.setStorageSync('user', currentUser)
wx.setStorageSync('token', token)
setTimeout(() => {
wx.switchTab({
url: '/pages/home/index',
})
}, 500)
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})