WechatController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * @throws \think\exception\DbException
  62. */
  63. public function auth(Request $request)
  64. {
  65. [$spreadId, $login_type] = $request->getMore([
  66. [['spread', 'd'], 0],
  67. ['login_type', ''],
  68. ], true);
  69. $token = $this->services->auth($spreadId, $login_type);
  70. if ($token && isset($token['key'])) {
  71. return app('json')->success('授权成功,请绑定手机号', $token);
  72. } else if ($token) {
  73. return app('json')->success('登录成功', ['userInfo' => $token['userInfo']]);
  74. } else
  75. return app('json')->fail('登录失败');
  76. }
  77. /**
  78. * App微信登陆
  79. * @param Request $request
  80. * @return mixed
  81. */
  82. public function appAuth(Request $request)
  83. {
  84. [$userInfo, $phone, $captcha] = $request->postMore([
  85. ['userInfo', []],
  86. ['phone', ''],
  87. ['code', '']
  88. ], true);
  89. if ($phone) {
  90. if (!$captcha) {
  91. return app('json')->fail('请输入验证码');
  92. }
  93. //验证验证码
  94. $verifyCode = CacheService::get('code_' . $phone);
  95. if (!$verifyCode)
  96. return app('json')->fail('请先获取验证码');
  97. $verifyCode = substr($verifyCode, 0, 6);
  98. if ($verifyCode != $captcha) {
  99. CacheService::delete('code_' . $phone);
  100. return app('json')->fail('验证码错误');
  101. }
  102. }
  103. $token = $this->services->appAuth($userInfo, $phone);
  104. if ($token) {
  105. return app('json')->success('登录成功', $token);
  106. } else if ($token === false) {
  107. return app('json')->success('登录成功', ['isbind' => true]);
  108. } else {
  109. return app('json')->fail('登陆失败');
  110. }
  111. }
  112. public function follow()
  113. {
  114. $data = $this->services->follow();
  115. if ($data) {
  116. return app('json')->success('ok', $data);
  117. } else {
  118. return app('json')->fail('获取失败');
  119. }
  120. }
  121. }