AuthController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\RoutineServices;
  14. use crmeb\services\CacheService;
  15. /**
  16. * Class AuthController
  17. * @package app\api\controller\v2\wechat
  18. */
  19. class AuthController
  20. {
  21. protected $services = NUll;
  22. /**
  23. * AuthController constructor.
  24. * @param RoutineServices $services
  25. */
  26. public function __construct(RoutineServices $services)
  27. {
  28. $this->services = $services;
  29. }
  30. /**
  31. * 小程序授权登录
  32. * @param Request $request
  33. * @return mixed
  34. * @throws \Psr\SimpleCache\InvalidArgumentException
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. * @throws \think\exception\DbException
  38. */
  39. public function auth(Request $request)
  40. {
  41. [$code, $spid, $spread, $iv, $encryptedData] = $request->postMore([
  42. ['code', ''],
  43. ['spread_spid', 0],
  44. ['spread_code', ''],
  45. ['iv', ''],
  46. ['encryptedData', ''],
  47. ], true);
  48. $token = $this->services->newAuth($code, $spid, $spread, $iv, $encryptedData);
  49. if ($token) {
  50. if (isset($token['key']) && $token['key']) {
  51. return app('json')->success(410022, $token);
  52. } else {
  53. return app('json')->success(410001, ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp']]);
  54. }
  55. } else
  56. return app('json')->fail(410019);
  57. }
  58. /**
  59. * 静默授权
  60. * @param $code
  61. * @param string $spread_code
  62. * @param string $spread_spid
  63. * @return mixed
  64. */
  65. public function silenceAuth($code, $spread_code = '', $spread_spid = '')
  66. {
  67. $token = $this->services->silenceAuth($code, $spread_code, $spread_spid);
  68. if ($token && isset($token['key'])) {
  69. return app('json')->success(410022, $token);
  70. } else if ($token) {
  71. return app('json')->success(410001, ['token' => $token['token'], 'expires_time' => $token['params']['exp'], 'new_user' => $token['new_user']]);
  72. } else
  73. return app('json')->fail(410019);
  74. }
  75. /**
  76. * 静默授权 不登录
  77. * @param $code
  78. * @param string $spread_code
  79. * @param string $spread_spid
  80. * @return mixed
  81. */
  82. public function silenceAuthNoLogin($code, $spread_code = '', $spread_spid = '')
  83. {
  84. $token = $this->services->silenceAuthNoLogin($code, $spread_code, $spread_spid);
  85. if ($token && isset($token['auth_login'])) {
  86. return app('json')->success(410023);
  87. } else if ($token) {
  88. return app('json')->success(410001, ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp']]);
  89. } else
  90. return app('json')->fail(410019);
  91. }
  92. /**
  93. * 静默授权
  94. * @param string $code
  95. * @param string $spread_code
  96. * @param string $spread_spid
  97. * @param string $phone
  98. * @param string $captcha
  99. * @return mixed
  100. * @throws \Psr\SimpleCache\InvalidArgumentException
  101. */
  102. public function silenceAuthBindingPhone($code = '', $spread_code = '', $spread_spid = '', $phone = '', $captcha = '')
  103. {
  104. //验证验证码
  105. $verifyCode = CacheService::get('code_' . $phone);
  106. if (!$verifyCode)
  107. return app('json')->fail(410009);
  108. $verifyCode = substr($verifyCode, 0, 6);
  109. if ($verifyCode != $captcha) {
  110. CacheService::delete('code_' . $phone);
  111. return app('json')->fail(410010);
  112. }
  113. CacheService::delete('code_' . $phone);
  114. $token = $this->services->silenceAuthBindingPhone($code, $spread_code, $spread_spid, $phone);
  115. if ($token) {
  116. return app('json')->success(410001, ['token' => $token['token'], 'expires_time' => $token['params']['exp'], 'new_user' => $token['new_user']]);
  117. } else
  118. return app('json')->fail(410019);
  119. }
  120. /**
  121. * 授权获取小程序用户手机号 直接绑定
  122. * @param string $code
  123. * @param string $iv
  124. * @param string $encryptedData
  125. * @param string $spread_code
  126. * @param string $spread_spid
  127. * @param string $key
  128. * @return mixed
  129. */
  130. public function authBindingPhone($code = '', $iv = '', $encryptedData = '', $spread_code = '', $spread_spid = '', $key = '')
  131. {
  132. if (!$code || !$iv || !$encryptedData)
  133. return app('json')->fail(100100);
  134. $token = $this->services->authBindingPhone($code, $iv, $encryptedData, $spread_code, $spread_spid, $key);
  135. if ($token) {
  136. return app('json')->success(410001, $token);
  137. } else
  138. return app('json')->fail(410019);
  139. }
  140. /**
  141. * 更新用户信息
  142. * @param Request $request
  143. * @param $userInfo
  144. * @return mixed
  145. */
  146. public function updateInfo(Request $request, $userInfo)
  147. {
  148. if (!$userInfo) {
  149. return app('json')->fail(100100);
  150. }
  151. $uid = (int)$request->uid();
  152. $re = $this->services->updateUserInfo($uid, $userInfo);
  153. if ($re) {
  154. return app('json')->success(100012);
  155. } else
  156. return app('json')->fail(100013);
  157. }
  158. }