'use strict'; /** * weapp控制器 * * @author Lan * @date 2025/12/22 * @version */ const DashboardStats = require('../lib/dashboard_stats'); module.exports = app => { class WeappController extends app.BaseController { async accountLogin(ctx) { try { const { code, account, password } = ctx.request.body; if (!code || !account || !password) { throw '缺少必要参数'; } const { accessToken, refreshToken, pList } = await ctx.service.weapp.accountlogin({ code, account, password }); ctx.body = { code: 0, msg: '登录成功', data: { access_token: accessToken, refresh_token: refreshToken, platformProjects: pList } }; } catch (error) { this.log(error); ctx.body = { code: -1, msg: error.toString(), data: null }; } } async wxLogin(ctx) { try { const { code, } = ctx.request.body; if (!code) { throw '微信授权失败, 请检查'; } const { accessToken, refreshToken, pList } = await ctx.service.weapp.weappLogin(code); ctx.body = { code: 0, msg: '登录成功', data: { access_token: accessToken, refresh_token: refreshToken, platformProjects: pList } }; } catch (error) { this.log(error); ctx.body = { code: -1, msg: error.toString(), data: null }; } } // 获取手机验证码 async getCaptcha(ctx) { try { const { mobile } = ctx.request.query; if (!mobile) { throw '缺少手机号'; } await ctx.service.weapp.sendCaptcha(mobile); ctx.body = { code: 0, msg: '验证码发送成功', data: null }; } catch (error) { this.log(error); ctx.body = { code: -1, msg: error.toString(), data: null }; } } // 手机号验证码登陆 async mobileLogin(ctx) { try { const { mobile, captcha } = ctx.request.body; if (!mobile) { throw '缺少手机号'; } if (!captcha) { throw '缺少验证码'; } const token = await ctx.service.weapp.mobileLogin({ mobile, captcha }); ctx.body = { code: 0, msg: '登录成功', data: { access_token: token } }; } catch (error) { this.log(error); ctx.body = { code: -1, msg: error.toString(), data: null }; } } async refresh(ctx) { const { refresh_token } = ctx.request.body; if (!refresh_token) { ctx.body = { code: -1, msg: '缺少 refresh_token', data: null }; return; } try { const token = await ctx.service.weapp.refresh(refresh_token); ctx.body = { code: 0, msg: '', data: { access_token: token } }; } catch (error) { this.log(error); ctx.body = { code: -1, msg: '登录已过期,请重新登录', data: null }; } } // 获取用户信息 async getUserInfo(ctx) { try { const mobile = ctx.projectAccount.auth_mobile || ctx.projectAccount.mobile; const projectAccount = { code: ctx.project.code, name: ctx.project.name, userName: ctx.projectAccount.name, mobile: mobile.slice(0, 3) + '****' + mobile.slice(-4), }; ctx.body = { code: 0, msg: '', data: { userInfo: projectAccount, }, }; } catch (error) { this.log(error); ctx.body = { code: -1, msg: '', data: null }; } } } return WeappController; };