WechatController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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. * @return \think\Response
  42. */
  43. public function miniServe()
  44. {
  45. return $this->services->miniServe();
  46. }
  47. /**
  48. * 支付异步回调
  49. */
  50. public function notify()
  51. {
  52. return $this->services->notify();
  53. }
  54. public function v3notify()
  55. {
  56. return $this->services->v3notify();
  57. }
  58. /**
  59. * 公众号权限配置信息获取
  60. * @param Request $request
  61. * @return mixed
  62. */
  63. public function config(Request $request)
  64. {
  65. return app('json')->success($this->services->config($request->get('url')));
  66. }
  67. /**
  68. * App微信登陆
  69. * @param Request $request
  70. * @return mixed
  71. * @throws \Psr\SimpleCache\InvalidArgumentException
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. */
  75. public function appAuth(Request $request)
  76. {
  77. [$userInfo, $phone, $captcha] = $request->postMore([
  78. ['userInfo', []],
  79. ['phone', ''],
  80. ['code', '']
  81. ], true);
  82. if ($phone) {
  83. if (!$captcha) {
  84. return app('json')->fail(410004);
  85. }
  86. //验证验证码
  87. $verifyCode = CacheService::get('code_' . $phone);
  88. if (!$verifyCode)
  89. return app('json')->fail(410009);
  90. $verifyCode = substr($verifyCode, 0, 6);
  91. if ($verifyCode != $captcha) {
  92. CacheService::delete('code_' . $phone);
  93. return app('json')->fail(410010);
  94. }
  95. }
  96. $token = $this->services->appAuth($userInfo, $phone);
  97. if ($token) {
  98. return app('json')->success(410001, $token);
  99. } else if ($token === false) {
  100. return app('json')->success(410001, ['isbind' => true]);
  101. } else {
  102. return app('json')->fail(410019);
  103. }
  104. }
  105. /**
  106. * 关注二维码
  107. * @return mixed
  108. * @throws \Exception
  109. */
  110. public function follow()
  111. {
  112. $data = $this->services->follow();
  113. if ($data) {
  114. return app('json')->success($data);
  115. } else {
  116. return app('json')->fail(100016);
  117. }
  118. }
  119. }