AuthController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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\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 $code
  33. * @param string $spread_code
  34. * @param string $spread_spid
  35. * @return mixed
  36. * @throws \Psr\SimpleCache\InvalidArgumentException
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function silenceAuth($code, $spread_code = '', $spread_spid = '')
  41. {
  42. $token = $this->services->silenceAuth($code, $spread_code, $spread_spid);
  43. if ($token && isset($token['key'])) {
  44. return app('json')->success(410022, $token);
  45. } else if ($token) {
  46. return app('json')->success(410001, ['token' => $token['token'], 'expires_time' => $token['params']['exp'], 'new_user' => $token['new_user']]);
  47. } else
  48. return app('json')->fail(410019);
  49. }
  50. /**
  51. * 授权获取小程序用户手机号 直接绑定
  52. * @param string $code
  53. * @param string $iv
  54. * @param string $encryptedData
  55. * @param string $spread_code
  56. * @param string $spread_spid
  57. * @param string $key
  58. * @return mixed
  59. * @throws \Psr\SimpleCache\InvalidArgumentException
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function authBindingPhone($code = '', $iv = '', $encryptedData = '', $spread_code = '', $spread_spid = '', $key = '')
  64. {
  65. if (!$code || !$iv || !$encryptedData)
  66. return app('json')->fail(100100);
  67. $token = $this->services->authBindingPhone($code, $iv, $encryptedData, $spread_code, $spread_spid, $key);
  68. if ($token) {
  69. return app('json')->success(410001, $token);
  70. } else
  71. return app('json')->fail(410019);
  72. }
  73. /**
  74. * 小程序绑定手机号
  75. * @param string $code
  76. * @param string $iv
  77. * @param string $encryptedData
  78. * @return \think\Response
  79. * @author 吴汐
  80. * @email 442384644@qq.com
  81. * @date 2023/02/24
  82. */
  83. public function bindingPhone($code = '', $iv = '', $encryptedData = '')
  84. {
  85. if (!$code || !$iv || !$encryptedData) return app('json')->fail(100100);
  86. $this->services->bindingPhone($code, $iv, $encryptedData);
  87. return app('json')->success(410016);
  88. }
  89. /** 以下方法该版本暂未使用 */
  90. /**
  91. * 小程序授权登录
  92. * @param Request $request
  93. * @return mixed
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. public function auth(Request $request)
  98. {
  99. [$code, $spid, $spread, $iv, $encryptedData] = $request->postMore([
  100. ['code', ''],
  101. ['spread_spid', 0],
  102. ['spread_code', ''],
  103. ['iv', ''],
  104. ['encryptedData', ''],
  105. ], true);
  106. $token = $this->services->newAuth($code, $spid, $spread, $iv, $encryptedData);
  107. if ($token) {
  108. if (isset($token['key']) && $token['key']) {
  109. return app('json')->success(410022, $token);
  110. } else {
  111. return app('json')->success(410001, ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp']]);
  112. }
  113. } else
  114. return app('json')->fail(410019);
  115. }
  116. /**
  117. * 静默授权 不登录
  118. * @param $code
  119. * @param string $spread_code
  120. * @param string $spread_spid
  121. * @return mixed
  122. * @throws \Psr\SimpleCache\InvalidArgumentException
  123. */
  124. public function silenceAuthNoLogin($code, $spread_code = '', $spread_spid = '')
  125. {
  126. $token = $this->services->silenceAuthNoLogin($code, $spread_code, $spread_spid);
  127. if ($token && isset($token['auth_login'])) {
  128. return app('json')->success(410023);
  129. } else if ($token) {
  130. return app('json')->success(410001, ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp']]);
  131. } else
  132. return app('json')->fail(410019);
  133. }
  134. /**
  135. * 静默授权
  136. * @param string $code
  137. * @param string $spread_code
  138. * @param string $spread_spid
  139. * @param string $phone
  140. * @param string $captcha
  141. * @return mixed
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. */
  145. public function silenceAuthBindingPhone($code = '', $spread_code = '', $spread_spid = '', $phone = '', $captcha = '')
  146. {
  147. //验证验证码
  148. $verifyCode = CacheService::get('code_' . $phone);
  149. if (!$verifyCode)
  150. return app('json')->fail(410009);
  151. $verifyCode = substr($verifyCode, 0, 6);
  152. if ($verifyCode != $captcha) {
  153. CacheService::delete('code_' . $phone);
  154. return app('json')->fail(410010);
  155. }
  156. CacheService::delete('code_' . $phone);
  157. $token = $this->services->silenceAuthBindingPhone($code, $spread_code, $spread_spid, $phone);
  158. if ($token) {
  159. return app('json')->success(410001, ['token' => $token['token'], 'expires_time' => $token['params']['exp'], 'new_user' => $token['new_user']]);
  160. } else
  161. return app('json')->fail(410019);
  162. }
  163. /**
  164. * 更新用户信息
  165. * @param Request $request
  166. * @param $userInfo
  167. * @return mixed
  168. * @throws \think\db\exception\DataNotFoundException
  169. * @throws \think\db\exception\DbException
  170. * @throws \think\db\exception\ModelNotFoundException
  171. */
  172. public function updateInfo(Request $request, $userInfo)
  173. {
  174. if (!$userInfo) {
  175. return app('json')->fail(100100);
  176. }
  177. $uid = (int)$request->uid();
  178. $re = $this->services->updateUserInfo($uid, $userInfo);
  179. if ($re) {
  180. return app('json')->success(100012);
  181. } else
  182. return app('json')->fail(100013);
  183. }
  184. }