| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- '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;
- };
|