weapp_controller.js 3.5 KB

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