WechatController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\v2\wechat;
  12. use app\Request;
  13. use app\services\wechat\WechatServices;
  14. use crmeb\services\CacheService;
  15. /**
  16. * Class WechatController
  17. * @package app\api\controller\v2\wechat
  18. */
  19. class WechatController
  20. {
  21. protected $services = NUll;
  22. /**
  23. * WechatController constructor.
  24. * @param WechatServices $services
  25. */
  26. public function __construct(WechatServices $services)
  27. {
  28. $this->services = $services;
  29. }
  30. /**
  31. * 公众号授权登陆
  32. * @param Request $request
  33. * @return mixed
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. * @throws \think\exception\DbException
  37. */
  38. public function auth(Request $request)
  39. {
  40. [$spreadId, $login_type] = $request->getMore([
  41. [['spread', 'd'], 0],
  42. ['login_type', 'wechat'],
  43. ], true);
  44. $token = $this->services->newAuth($spreadId, $login_type);
  45. if ($token && isset($token['key'])) {
  46. return app('json')->success(410022, $token);
  47. } else if ($token) {
  48. return app('json')->success(410001, ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp']]);
  49. } else
  50. return app('json')->fail(410019);
  51. }
  52. /**
  53. * 微信公众号静默授权
  54. * @param string $spread
  55. * @return mixed
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public function silenceAuth($spread = '')
  60. {
  61. $token = $this->services->silenceAuth($spread);
  62. if ($token && isset($token['key'])) {
  63. return app('json')->success(410022, $token);
  64. } else if ($token) {
  65. return app('json')->success(410001, ['token' => $token['token'], 'expires_time' => $token['params']['exp']]);
  66. } else
  67. return app('json')->fail(410019);
  68. }
  69. /**
  70. * 微信公众号静默授权
  71. * @param string $spread
  72. * @return mixed
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. public function silenceAuthNoLogin($spread = '')
  77. {
  78. $token = $this->services->silenceAuthNoLogin($spread);
  79. if ($token && isset($token['auth_login'])) {
  80. return app('json')->success(410023, $token);
  81. } else if ($token) {
  82. return app('json')->success(410001, ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp']]);
  83. } else
  84. return app('json')->fail(410019);
  85. }
  86. /**
  87. * 静默授权 手机号直接注册登录
  88. * @param string $key
  89. * @param string $phone
  90. * @param string $captcha
  91. * @return mixed
  92. * @throws \Psr\SimpleCache\InvalidArgumentException
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. public function silenceAuthBindingPhone($key = '', $phone = '', $captcha = '')
  97. {
  98. //验证验证码
  99. $verifyCode = CacheService::get('code_' . $phone);
  100. if (!$verifyCode)
  101. return app('json')->fail(410009);
  102. $verifyCode = substr($verifyCode, 0, 6);
  103. if ($verifyCode != $captcha) {
  104. CacheService::delete('code_' . $phone);
  105. return app('json')->fail(410010);
  106. }
  107. CacheService::delete('code_' . $phone);
  108. $token = $this->services->silenceAuthBindingPhone($key, $phone);
  109. if ($token) {
  110. return app('json')->success(410001, $token);
  111. } else
  112. return app('json')->fail(410019);
  113. }
  114. }