WechatController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\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. /**
  47. * 公众号权限配置信息获取
  48. * @param Request $request
  49. * @return mixed
  50. */
  51. public function config(Request $request)
  52. {
  53. return app('json')->success($this->services->config($request->get('url')));
  54. }
  55. /**
  56. * 公众号授权登陆
  57. * @param Request $request
  58. * @return mixed
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. */
  62. public function auth(Request $request)
  63. {
  64. [$spreadId, $login_type] = $request->getMore([
  65. [['spread', 'd'], 0],
  66. ['login_type', ''],
  67. ], true);
  68. $token = $this->services->auth($spreadId, $login_type);
  69. if ($token && isset($token['key'])) {
  70. return app('json')->success(410022, $token);
  71. } else if ($token) {
  72. return app('json')->success(410001, ['userInfo' => $token['userInfo']]);
  73. } else
  74. return app('json')->fail(410019);
  75. }
  76. /**
  77. * App微信登陆
  78. * @param Request $request
  79. * @return mixed
  80. * @throws \Psr\SimpleCache\InvalidArgumentException
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. */
  84. public function appAuth(Request $request)
  85. {
  86. [$userInfo, $phone, $captcha] = $request->postMore([
  87. ['userInfo', []],
  88. ['phone', ''],
  89. ['code', '']
  90. ], true);
  91. if ($phone) {
  92. if (!$captcha) {
  93. return app('json')->fail(410004);
  94. }
  95. //验证验证码
  96. $verifyCode = CacheService::get('code_' . $phone);
  97. if (!$verifyCode)
  98. return app('json')->fail(410009);
  99. $verifyCode = substr($verifyCode, 0, 6);
  100. if ($verifyCode != $captcha) {
  101. CacheService::delete('code_' . $phone);
  102. return app('json')->fail(410010);
  103. }
  104. }
  105. $token = $this->services->appAuth($userInfo, $phone);
  106. if ($token) {
  107. return app('json')->success(410001, $token);
  108. } else if ($token === false) {
  109. return app('json')->success(410001, ['isbind' => true]);
  110. } else {
  111. return app('json')->fail(410019);
  112. }
  113. }
  114. /**
  115. * 关注二维码
  116. * @return mixed
  117. * @throws \Exception
  118. */
  119. public function follow()
  120. {
  121. $data = $this->services->follow();
  122. if ($data) {
  123. return app('json')->success($data);
  124. } else {
  125. return app('json')->fail(100016);
  126. }
  127. }
  128. }