Weapp.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\weapp;
  12. use app\service\api\notice\NoticeService;
  13. use app\service\api\weapp\WeappAuthService;
  14. use app\service\api\weapp\WeappDeliveryService;
  15. use core\base\BaseApiController;
  16. use think\Response;
  17. class Weapp extends BaseApiController
  18. {
  19. /**
  20. * 授权登录
  21. * @return Response
  22. */
  23. public function login()
  24. {
  25. $data = $this->request->params([
  26. [ 'code', '' ],
  27. [ 'nickname', '' ],
  28. [ 'headimg', '' ],
  29. [ 'mobile', '' ],
  30. [ 'mobile_code', '' ]
  31. ]);
  32. $weapp_auth_service = new WeappAuthService();
  33. return success($weapp_auth_service->login($data));
  34. }
  35. /**
  36. * 注册
  37. * @return Response
  38. */
  39. public function register()
  40. {
  41. $data = $this->request->params([
  42. [ 'openid', '' ],
  43. [ 'unionid', '' ],
  44. [ 'mobile_code', '' ],
  45. [ 'mobile', '' ],
  46. ]);
  47. $weapp_auth_service = new WeappAuthService();
  48. return success($weapp_auth_service->register($data[ 'openid' ], $data[ 'mobile' ], $data[ 'mobile_code' ], $data[ 'unionid' ]));
  49. }
  50. public function subscribeMessage()
  51. {
  52. $data = $this->request->params([ [ 'keys', '' ] ]);
  53. return success(( new NoticeService() )->getWeappNoticeTemplateId($data[ 'keys' ]));
  54. }
  55. /**
  56. * 查询小程序是否已开通发货信息管理服务
  57. * @return bool
  58. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  59. */
  60. public function getIsTradeManaged()
  61. {
  62. try {
  63. $wechat_template_service = new WeappDeliveryService();
  64. $result = $wechat_template_service->getIsTradeManaged();
  65. if ($result) {
  66. return success([ 'is_trade_managed' => true ]);
  67. }
  68. } catch (\Exception $e) {
  69. }
  70. return success([ 'is_trade_managed' => false ]);
  71. }
  72. /**
  73. * 通过外部交易号获取消息跳转路径
  74. */
  75. public function getMsgJumpPath()
  76. {
  77. $data = $this->request->params([
  78. [ 'out_trade_no', '' ],
  79. ]);
  80. $wechat_template_service = new WeappDeliveryService();
  81. $result = $wechat_template_service->getMsgJumpPath($data[ 'out_trade_no' ]);
  82. return success([ 'path' => $result ]);
  83. }
  84. /**
  85. * 更新openid
  86. * @return Response
  87. */
  88. public function updateOpenid()
  89. {
  90. $data = $this->request->params([ [ 'code', '' ] ]);
  91. $weapp_auth_service = new WeappAuthService();
  92. return success($weapp_auth_service->updateOpenid($data[ 'code' ]));
  93. }
  94. /**
  95. * 获取用户信息
  96. * @return Response
  97. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  98. */
  99. public function getUser(){
  100. $data = $this->request->params([ [ 'code', '' ] ]);
  101. $weapp_auth_service = new WeappAuthService();
  102. return success($weapp_auth_service->getUserInfoByCode($data[ 'code' ]));
  103. }
  104. }