162 lines
5.1 KiB
JavaScript
162 lines
5.1 KiB
JavaScript
|
|
"use strict";
|
||
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
|
};
|
||
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
|
// pages/apply/apply.ts
|
||
|
|
const userService_1 = __importDefault(require("../../services/userService"));
|
||
|
|
Page({
|
||
|
|
/**
|
||
|
|
* 页面的初始数据
|
||
|
|
*/
|
||
|
|
data: {
|
||
|
|
applyForm: {
|
||
|
|
name: '',
|
||
|
|
phone: ''
|
||
|
|
}
|
||
|
|
},
|
||
|
|
/**
|
||
|
|
* 生命周期函数--监听页面加载
|
||
|
|
*/
|
||
|
|
onLoad() {
|
||
|
|
console.log('申请页面加载');
|
||
|
|
},
|
||
|
|
/**
|
||
|
|
* 表单输入处理
|
||
|
|
*/
|
||
|
|
onApplyFormInput(e) {
|
||
|
|
const { field } = e.currentTarget.dataset;
|
||
|
|
const { value } = e.detail;
|
||
|
|
this.setData({
|
||
|
|
[`applyForm.${field}`]: value
|
||
|
|
});
|
||
|
|
},
|
||
|
|
/**
|
||
|
|
* 取消按钮点击
|
||
|
|
*/
|
||
|
|
onCancel() {
|
||
|
|
// 返回上一页
|
||
|
|
wx.navigateBack();
|
||
|
|
},
|
||
|
|
/**
|
||
|
|
* 提交申请
|
||
|
|
*/
|
||
|
|
async onSubmit() {
|
||
|
|
const { name, phone } = this.data.applyForm;
|
||
|
|
// 表单验证
|
||
|
|
if (!this.validateForm()) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
wx.showLoading({
|
||
|
|
title: '提交中...',
|
||
|
|
mask: true
|
||
|
|
});
|
||
|
|
// 调用API提交申请
|
||
|
|
const result = await this.submitApplication({ name, phone });
|
||
|
|
if (result.success) {
|
||
|
|
wx.showToast({
|
||
|
|
title: '绑定成功',
|
||
|
|
icon: 'success',
|
||
|
|
duration: 2000
|
||
|
|
});
|
||
|
|
// 更新全局用户信息
|
||
|
|
const app = getApp();
|
||
|
|
if (app.globalData.userInfo) {
|
||
|
|
app.globalData.userInfo = {
|
||
|
|
...app.globalData.userInfo,
|
||
|
|
name: result.employeeInfo.name,
|
||
|
|
phone: result.employeeInfo.phone,
|
||
|
|
role: result.employeeInfo.role
|
||
|
|
};
|
||
|
|
console.log('全局用户信息已更新:', app.globalData.userInfo);
|
||
|
|
}
|
||
|
|
// 延迟返回并刷新首页
|
||
|
|
setTimeout(async () => {
|
||
|
|
// 获取当前页面栈
|
||
|
|
const pages = getCurrentPages();
|
||
|
|
if (pages.length >= 2) {
|
||
|
|
// 获取首页实例并调用刷新方法
|
||
|
|
const indexPage = pages[pages.length - 2];
|
||
|
|
if (indexPage && indexPage.refreshPageAfterLogin) {
|
||
|
|
// 重新获取用户状态,确保绑定成功后状态正确更新
|
||
|
|
if (indexPage.data.mainPageModule) {
|
||
|
|
const loginModule = indexPage.data.mainPageModule.getLoginModule();
|
||
|
|
const app = getApp();
|
||
|
|
const userStatus = await loginModule.determineUserStatus(app.globalData.userInfo);
|
||
|
|
// 更新页面状态
|
||
|
|
indexPage.setData({
|
||
|
|
'authStatus.userStatus': userStatus
|
||
|
|
});
|
||
|
|
console.log('✅ 绑定成功后重新获取用户状态:', userStatus);
|
||
|
|
}
|
||
|
|
indexPage.refreshPageAfterLogin();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
wx.navigateBack();
|
||
|
|
}, 1500);
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
wx.showToast({
|
||
|
|
title: result.message || '提交失败',
|
||
|
|
icon: 'none',
|
||
|
|
duration: 2000
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (error) {
|
||
|
|
console.error('提交申请失败:', error);
|
||
|
|
wx.showToast({
|
||
|
|
title: '网络错误,请重试',
|
||
|
|
icon: 'none',
|
||
|
|
duration: 2000
|
||
|
|
});
|
||
|
|
}
|
||
|
|
finally {
|
||
|
|
wx.hideLoading();
|
||
|
|
}
|
||
|
|
},
|
||
|
|
/**
|
||
|
|
* 表单验证
|
||
|
|
*/
|
||
|
|
validateForm() {
|
||
|
|
const { name, phone } = this.data.applyForm;
|
||
|
|
if (!name || !phone) {
|
||
|
|
wx.showToast({
|
||
|
|
title: '请填写完整信息',
|
||
|
|
icon: 'none',
|
||
|
|
duration: 2000
|
||
|
|
});
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
},
|
||
|
|
/**
|
||
|
|
* 提交申请到服务器
|
||
|
|
*/
|
||
|
|
async submitApplication(data) {
|
||
|
|
try {
|
||
|
|
// 先执行微信登录获取token
|
||
|
|
const loginResult = await userService_1.default.wxLogin();
|
||
|
|
if (!loginResult.success) {
|
||
|
|
throw new Error('微信登录失败,请重试');
|
||
|
|
}
|
||
|
|
// 调用实际的绑定接口
|
||
|
|
return await userService_1.default.register({
|
||
|
|
name: data.name,
|
||
|
|
phone: data.phone
|
||
|
|
});
|
||
|
|
}
|
||
|
|
catch (error) {
|
||
|
|
console.error('绑定流程失败:', error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
},
|
||
|
|
/**
|
||
|
|
* 生命周期函数--监听页面卸载
|
||
|
|
*/
|
||
|
|
onUnload() {
|
||
|
|
console.log('申请页面卸载');
|
||
|
|
}
|
||
|
|
});
|