Wechat.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\wechat;
  12. use app\common\ApiBaseController;
  13. use app\Request;
  14. use app\services\wechat\RoutineServices;
  15. use app\services\wechat\WechatServices;
  16. use GuzzleHttp\Exception\GuzzleException;
  17. use qiniu\services\huifu\CacheService;
  18. use qiniu\services\huifu\wechat\OfficialAccount;
  19. use qiniu\services\huifu\wechat\Work;
  20. use EasyWeChat\Kernel\Exceptions\BadRequestException;
  21. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  22. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  23. use ReflectionException;
  24. use think\db\exception\DataNotFoundException;
  25. use think\db\exception\DbException;
  26. use think\db\exception\ModelNotFoundException;
  27. use think\Response;
  28. /**
  29. * 微信公众号
  30. * Class WechatController
  31. * @package app\controller\api\wechat
  32. */
  33. class Wechat extends ApiBaseController
  34. {
  35. /**
  36. * WechatController constructor.
  37. * @param Request $request
  38. * @param WechatServices $service
  39. */
  40. public function __construct(Request $request, WechatServices $service)
  41. {
  42. parent::__construct($request);
  43. $this->service = $service;
  44. }
  45. /**
  46. * 微信公众号服务
  47. * @return Response
  48. * @throws BadRequestException
  49. * @throws InvalidArgumentException
  50. * @throws InvalidConfigException
  51. * @throws ReflectionException
  52. */
  53. public function serve()
  54. {
  55. return $this->service->serve();
  56. }
  57. /**
  58. * 公众号权限配置信息获取
  59. * @param Request $request
  60. * @return mixed
  61. * @throws GuzzleException
  62. * @throws \Psr\SimpleCache\InvalidArgumentException
  63. */
  64. public function config(Request $request)
  65. {
  66. $url = $request->get('url', '') ?: sys_config('site_url');
  67. return $this->success($this->service->config($url));
  68. }
  69. /**
  70. * 公众号授权登陆
  71. * @param Request $request
  72. * @return mixed
  73. * @throws DbException
  74. * @throws InvalidConfigException
  75. * @throws DataNotFoundException
  76. * @throws ModelNotFoundException
  77. */
  78. public function auth(Request $request)
  79. {
  80. [$spread_spid, $login_type] = $request->getMore([
  81. [['spread_spid', 'd'], 0],
  82. ['login_type', 'wechat'],
  83. ], true);
  84. $token = $this->service->auth($spread_spid, $login_type);
  85. if ($token && isset($token['key'])) {
  86. return $this->success('授权成功,请绑定手机号', $token);
  87. } else if ($token) {
  88. return $this->success('登录成功', ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp']]);
  89. } else
  90. return $this->error('登录失败');
  91. }
  92. /**
  93. * 微信公众号静默授权
  94. * @param string $spread_spid
  95. * @return mixed
  96. * @throws DataNotFoundException
  97. * @throws DbException
  98. * @throws ModelNotFoundException
  99. */
  100. public function silenceAuth($spread_spid = '')
  101. {
  102. $token = $this->service->silenceAuth($spread_spid);
  103. if ($token && isset($token['key'])) {
  104. return $this->success('授权成功,请绑定手机号', $token);
  105. } else if ($token) {
  106. return $this->success('登录成功', ['token' => $token['token'], 'expires_time' => $token['params']['exp']]);
  107. } else
  108. return $this->error('登录失败');
  109. }
  110. /**
  111. * 微信公众号静默授权
  112. * @param $spread_spid
  113. * @param $snsapi
  114. * @return mixed
  115. * @throws DataNotFoundException
  116. * @throws ModelNotFoundException
  117. */
  118. public function silenceAuthNoLogin($spread_spid = '', $snsapi = '')
  119. {
  120. $token = $this->service->silenceAuth($spread_spid, true, $snsapi);
  121. if ($token && isset($token['auth_login'])) {
  122. return $this->success('授权成功', $token);
  123. } else if ($token) {
  124. return $this->success('登录成功', ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp']]);
  125. } else
  126. return $this->error('登录失败');
  127. }
  128. /**
  129. * 静默授权 手机号直接注册登录
  130. * @param string $key
  131. * @param string $phone
  132. * @param string $captcha
  133. * @return mixed
  134. * @throws \Psr\SimpleCache\InvalidArgumentException
  135. * @throws DataNotFoundException
  136. * @throws ModelNotFoundException|DbException
  137. */
  138. public function authBindingPhone($key = '', $phone = '', $captcha = '')
  139. {
  140. //验证验证码
  141. check_sms_captcha($phone, 'wechat_auth', $captcha);
  142. $token = $this->service->authBindingPhone($key, $phone);
  143. if ($token) {
  144. return $this->success('登录成功', $token);
  145. } else
  146. return $this->error('登录失败');
  147. }
  148. /**
  149. * 关注二维码
  150. * @return mixed
  151. */
  152. public function follow()
  153. {
  154. $data = $this->service->follow();
  155. if ($data) {
  156. return $this->success('ok', $data);
  157. } else {
  158. return $this->error('获取失败');
  159. }
  160. }
  161. }