WechatController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\v1\wechat;
  12. use app\Request;
  13. use app\services\wechat\RoutineServices;
  14. use app\services\wechat\WechatServices as WechatAuthServices;
  15. use crmeb\services\CacheService;
  16. use crmeb\services\wechat\OfficialAccount;
  17. use crmeb\services\wechat\Work;
  18. use EasyWeChat\Kernel\Exceptions\BadRequestException;
  19. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  20. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  21. use think\Response;
  22. /**
  23. * 微信公众号
  24. * Class WechatController
  25. * @package app\controller\api\wechat
  26. */
  27. class WechatController
  28. {
  29. protected $services = NUll;
  30. /**
  31. * WechatController constructor.
  32. * @param WechatAuthServices $services
  33. */
  34. public function __construct(WechatAuthServices $services)
  35. {
  36. $this->services = $services;
  37. }
  38. /**
  39. * 微信公众号服务
  40. * @return Response
  41. * @throws BadRequestException
  42. * @throws InvalidArgumentException
  43. * @throws InvalidConfigException
  44. * @throws \ReflectionException
  45. */
  46. public function serve()
  47. {
  48. return $this->services->serve();
  49. }
  50. /**
  51. * 微信小程序公众号服务
  52. * @param RoutineServices $routineServices
  53. * @return Response
  54. */
  55. public function miniServe(RoutineServices $routineServices)
  56. {
  57. return $routineServices->serve();
  58. }
  59. /**
  60. * 企业微信服务
  61. * @return Response
  62. * @throws BadRequestException
  63. * @throws InvalidArgumentException
  64. * @throws InvalidConfigException
  65. * @throws \ReflectionException
  66. */
  67. public function work()
  68. {
  69. return $this->services->workServe();
  70. }
  71. /**
  72. * 公众号权限配置信息获取
  73. * @param Request $request
  74. * @return mixed
  75. */
  76. public function config(Request $request)
  77. {
  78. $url = $request->get('url', '') ?: sys_config('site_url');
  79. return app('json')->success($this->services->config($url));
  80. }
  81. /**
  82. * 公众号授权登陆
  83. * @param Request $request
  84. * @return mixed
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. * @throws \think\exception\DbException
  88. */
  89. public function auth(Request $request)
  90. {
  91. [$spread_spid, $login_type] = $request->getMore([
  92. [['spread_spid', 'd'], 0],
  93. ['login_type', ''],
  94. ], true);
  95. $token = $this->services->auth($spread_spid, $login_type);
  96. if ($token && isset($token['key'])) {
  97. return app('json')->success('授权成功,请绑定手机号', $token);
  98. } else if ($token) {
  99. return app('json')->success('登录成功', ['userInfo' => $token['userInfo']]);
  100. } else
  101. return app('json')->fail('登录失败');
  102. }
  103. /**
  104. * App微信登陆
  105. * @param Request $request
  106. * @return mixed
  107. */
  108. public function appAuth(Request $request)
  109. {
  110. [$userInfo, $phone, $captcha] = $request->postMore([
  111. ['userInfo', []],
  112. ['phone', ''],
  113. ['code', '']
  114. ], true);
  115. if ($phone) {
  116. if (!$captcha) {
  117. return app('json')->fail('请输入验证码');
  118. }
  119. //验证验证码
  120. $verifyCode = CacheService::get('code_' . $phone);
  121. if (!$verifyCode)
  122. return app('json')->fail('请先获取验证码');
  123. $verifyCode = substr($verifyCode, 0, 6);
  124. if ($verifyCode != $captcha) {
  125. CacheService::delete('code_' . $phone);
  126. return app('json')->fail('验证码错误');
  127. }
  128. }
  129. $token = $this->services->appAuth($userInfo, $phone);
  130. if ($token) {
  131. CacheService::delete('code_' . $phone);
  132. return app('json')->success('登录成功', $token);
  133. } else if ($token === false) {
  134. return app('json')->success('登录成功', ['isbind' => true]);
  135. } else {
  136. return app('json')->fail('登陆失败');
  137. }
  138. }
  139. public function follow()
  140. {
  141. $data = $this->services->follow();
  142. if ($data) {
  143. return app('json')->success('ok', $data);
  144. } else {
  145. return app('json')->fail('获取失败');
  146. }
  147. }
  148. }