WechatController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v1\wechat;
  12. use app\Request;
  13. use app\services\wechat\WechatServices as WechatAuthServices;
  14. use crmeb\services\CacheService;
  15. /**
  16. * 微信公众号
  17. * Class WechatController
  18. * @package app\api\controller\wechat
  19. */
  20. class WechatController
  21. {
  22. protected $services = NUll;
  23. /**
  24. * WechatController constructor.
  25. * @param WechatAuthServices $services
  26. */
  27. public function __construct(WechatAuthServices $services)
  28. {
  29. $this->services = $services;
  30. }
  31. /**
  32. * 微信公众号服务
  33. * @return \think\Response
  34. */
  35. public function serve()
  36. {
  37. return $this->services->serve();
  38. }
  39. /**
  40. * 支付异步回调
  41. */
  42. public function notify()
  43. {
  44. return $this->services->notify();
  45. }
  46. public function v3notify()
  47. {
  48. return $this->services->v3notify();
  49. }
  50. /**
  51. * 公众号权限配置信息获取
  52. * @param Request $request
  53. * @return mixed
  54. */
  55. public function config(Request $request)
  56. {
  57. return app('json')->success($this->services->config($request->get('url')));
  58. }
  59. /**
  60. * 公众号授权登陆
  61. * @param Request $request
  62. * @return mixed
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public function auth(Request $request)
  67. {
  68. [$spreadId, $login_type] = $request->getMore([
  69. [['spread', 'd'], 0],
  70. ['login_type', ''],
  71. ], true);
  72. $token = $this->services->auth($spreadId, $login_type);
  73. if ($token && isset($token['key'])) {
  74. return app('json')->success(410022, $token);
  75. } else if ($token) {
  76. return app('json')->success(410001, ['userInfo' => $token['userInfo']]);
  77. } else
  78. return app('json')->fail(410019);
  79. }
  80. /**
  81. * App微信登陆
  82. * @param Request $request
  83. * @return mixed
  84. * @throws \Psr\SimpleCache\InvalidArgumentException
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. public function appAuth(Request $request)
  89. {
  90. [$userInfo, $phone, $captcha] = $request->postMore([
  91. ['userInfo', []],
  92. ['phone', ''],
  93. ['code', '']
  94. ], true);
  95. if ($phone) {
  96. if (!$captcha) {
  97. return app('json')->fail(410004);
  98. }
  99. //验证验证码
  100. $verifyCode = CacheService::get('code_' . $phone);
  101. if (!$verifyCode)
  102. return app('json')->fail(410009);
  103. $verifyCode = substr($verifyCode, 0, 6);
  104. if ($verifyCode != $captcha) {
  105. CacheService::delete('code_' . $phone);
  106. return app('json')->fail(410010);
  107. }
  108. }
  109. $token = $this->services->appAuth($userInfo, $phone);
  110. if ($token) {
  111. return app('json')->success(410001, $token);
  112. } else if ($token === false) {
  113. return app('json')->success(410001, ['isbind' => true]);
  114. } else {
  115. return app('json')->fail(410019);
  116. }
  117. }
  118. /**
  119. * 关注二维码
  120. * @return mixed
  121. * @throws \Exception
  122. */
  123. public function follow()
  124. {
  125. $data = $this->services->follow();
  126. if ($data) {
  127. return app('json')->success($data);
  128. } else {
  129. return app('json')->fail(100016);
  130. }
  131. }
  132. }