weapp_controller.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 login(ctx) {
  12. try {
  13. const {
  14. code,
  15. } = ctx.request.body;
  16. if (!code) {
  17. throw '微信授权失败, 请检查';
  18. }
  19. const token = await ctx.service.weapp.weappLogin(code);
  20. ctx.body = { err: 0, msg: '登录成功', data: { access_token: token } };
  21. } catch (error) {
  22. this.log(error);
  23. ctx.body = { err: 1, msg: error.toString(), data: null };
  24. }
  25. }
  26. async refresh(ctx) {
  27. const { refresh_token } = ctx.request.body;
  28. if (!refresh_token) {
  29. ctx.body = { err: 1, msg: '缺少 refresh_token', data: null };
  30. return;
  31. }
  32. try {
  33. const token = await ctx.service.weapp.refresh(refresh_token);
  34. ctx.body = { err: 0, msg: '刷新成功', data: { access_token: token } };
  35. } catch (error) {
  36. this.log(error);
  37. ctx.body = { err: 1, msg: '刷新失败', data: null };
  38. }
  39. }
  40. }
  41. return WeappController;
  42. };