weapp_controller.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. async getProjectList(ctx) {
  41. try {
  42. const paList = await ctx.service.projectAccount.getAllDataByCondition({ where: { weapp_openid: ctx.session.sessionUser.weapp_openid, enable: 1 } });
  43. const pidList = this.ctx.app._.uniq(this.ctx.app._.map(paList, 'project_id'));
  44. const pList = [];
  45. for (const p of pidList) {
  46. const project = await this.ctx.service.project.getDataById(p);
  47. if (project) {
  48. pList.push({ code: project.code, name: project.name });
  49. }
  50. }
  51. ctx.body = { code: 0, msg: '', data: { platformProjects: pList } };
  52. } catch (error) {
  53. this.log(error);
  54. ctx.body = { code: -1, msg: error.toString(), data: null };
  55. }
  56. }
  57. // 获取手机验证码
  58. async getCaptcha(ctx) {
  59. try {
  60. const { mobile } = ctx.request.query;
  61. if (!mobile) {
  62. throw '缺少手机号';
  63. }
  64. await ctx.service.weapp.sendCaptcha(mobile);
  65. ctx.body = { code: 0, msg: '验证码发送成功', data: null };
  66. } catch (error) {
  67. this.log(error);
  68. ctx.body = { code: -1, msg: error.toString(), data: null };
  69. }
  70. }
  71. // 手机号验证码登陆
  72. async mobileLogin(ctx) {
  73. try {
  74. const { mobile, captcha } = ctx.request.body;
  75. if (!mobile) {
  76. throw '缺少手机号';
  77. }
  78. if (!captcha) {
  79. throw '缺少验证码';
  80. }
  81. const token = await ctx.service.weapp.mobileLogin({ mobile, captcha });
  82. ctx.body = { code: 0, msg: '登录成功', data: { access_token: token } };
  83. } catch (error) {
  84. this.log(error);
  85. ctx.body = { code: -1, msg: error.toString(), data: null };
  86. }
  87. }
  88. async refresh(ctx) {
  89. const { refresh_token } = ctx.request.body;
  90. if (!refresh_token) {
  91. ctx.body = { code: -1, msg: '缺少 refresh_token', data: null };
  92. return;
  93. }
  94. try {
  95. const token = await ctx.service.weapp.refresh(refresh_token);
  96. ctx.body = { code: 0, msg: '', data: { access_token: token } };
  97. } catch (error) {
  98. this.log(error);
  99. ctx.body = { code: -1, msg: error.toString(), data: null };
  100. }
  101. }
  102. // 获取用户信息
  103. async getUserInfo(ctx) {
  104. try {
  105. const mobile = ctx.session.sessionUser.auth_mobile || ctx.session.sessionUser.mobile;
  106. const projectAccount = {
  107. id: ctx.session.sessionUser.id,
  108. code: ctx.session.sessionProject.code,
  109. name: ctx.session.sessionProject.name,
  110. userName: ctx.session.sessionUser.name,
  111. mobile: mobile.slice(0, 3) + '****' + mobile.slice(-4),
  112. };
  113. ctx.body = {
  114. code: 0, msg: '', data: {
  115. userInfo: projectAccount,
  116. },
  117. };
  118. } catch (error) {
  119. this.log(error);
  120. ctx.body = { code: -1, msg: '', data: null };
  121. }
  122. }
  123. }
  124. return WeappController;
  125. };