weapp_controller.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. /**
  3. * weapp控制器
  4. *
  5. * @author Lan
  6. * @date 2025/12/22
  7. * @version
  8. */
  9. const DashboardStats = require('../lib/dashboard_stats');
  10. module.exports = app => {
  11. class WeappController extends app.BaseController {
  12. async accountLogin(ctx) {
  13. try {
  14. const { code, account, password } = ctx.request.body;
  15. if (!code || !account || !password) {
  16. throw '缺少必要参数';
  17. }
  18. const { accessToken, refreshToken, pList } = await ctx.service.weapp.accountlogin({ code, account, password });
  19. ctx.body = { code: 0, msg: '登录成功', data: { access_token: accessToken, refresh_token: refreshToken, platformProjects: pList } };
  20. } catch (error) {
  21. this.log(error);
  22. ctx.body = { code: -1, msg: error.toString(), data: null };
  23. }
  24. }
  25. async wxLogin(ctx) {
  26. try {
  27. const {
  28. code,
  29. } = ctx.request.body;
  30. if (!code) {
  31. throw '微信授权失败, 请检查';
  32. }
  33. const { accessToken, refreshToken, pList } = await ctx.service.weapp.weappLogin(code);
  34. ctx.body = { code: 0, msg: '登录成功', data: { access_token: accessToken, refresh_token: refreshToken, platformProjects: pList } };
  35. } catch (error) {
  36. this.log(error);
  37. ctx.body = { code: -1, msg: error.toString(), data: null };
  38. }
  39. }
  40. // 获取手机验证码
  41. async getCaptcha(ctx) {
  42. try {
  43. const { mobile } = ctx.request.query;
  44. if (!mobile) {
  45. throw '缺少手机号';
  46. }
  47. await ctx.service.weapp.sendCaptcha(mobile);
  48. ctx.body = { code: 0, msg: '验证码发送成功', data: null };
  49. } catch (error) {
  50. this.log(error);
  51. ctx.body = { code: -1, msg: error.toString(), data: null };
  52. }
  53. }
  54. // 手机号验证码登陆
  55. async mobileLogin(ctx) {
  56. try {
  57. const { mobile, captcha } = ctx.request.body;
  58. if (!mobile) {
  59. throw '缺少手机号';
  60. }
  61. if (!captcha) {
  62. throw '缺少验证码';
  63. }
  64. const token = await ctx.service.weapp.mobileLogin({ mobile, captcha });
  65. ctx.body = { code: 0, msg: '登录成功', data: { access_token: token } };
  66. } catch (error) {
  67. this.log(error);
  68. ctx.body = { code: -1, msg: error.toString(), data: null };
  69. }
  70. }
  71. async refresh(ctx) {
  72. const { refresh_token } = ctx.request.body;
  73. if (!refresh_token) {
  74. ctx.body = { code: -1, msg: '缺少 refresh_token', data: null };
  75. return;
  76. }
  77. try {
  78. const token = await ctx.service.weapp.refresh(refresh_token);
  79. ctx.body = { code: 0, msg: '', data: { access_token: token } };
  80. } catch (error) {
  81. this.log(error);
  82. ctx.body = { code: -1, msg: '登录已过期,请重新登录', data: null };
  83. }
  84. }
  85. // 获取用户信息
  86. async getUserInfo(ctx) {
  87. try {
  88. const mobile = ctx.projectAccount.auth_mobile || ctx.projectAccount.mobile;
  89. const projectAccount = {
  90. code: ctx.project.code,
  91. name: ctx.project.name,
  92. userName: ctx.projectAccount.name,
  93. mobile: mobile.slice(0, 3) + '****' + mobile.slice(-4),
  94. };
  95. ctx.body = {
  96. code: 0, msg: '', data: {
  97. userInfo: projectAccount,
  98. },
  99. };
  100. } catch (error) {
  101. this.log(error);
  102. ctx.body = { code: -1, msg: '', data: null };
  103. }
  104. }
  105. }
  106. return WeappController;
  107. };