AuthController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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\controller\api\v2\wechat;
  12. use app\Request;
  13. use app\services\wechat\RoutineServices;
  14. use crmeb\services\CacheService;
  15. /**
  16. * Class AuthController
  17. * @package app\controller\api\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. * 返回用户信息的缓存key,返回是否强制绑定手机号
  32. * @param $code
  33. * @param $spread_code
  34. * @param $spread_spid
  35. * @return \think\Response
  36. */
  37. public function authType($code, $spread_code = '', $spread_spid = '')
  38. {
  39. if (!$code) return app('json')->fail('参数有误');
  40. $data = $this->services->authType($code, $spread_spid, $spread_code);
  41. return app('json')->success($data);
  42. }
  43. /**
  44. * 根据缓存获取token
  45. * @param $key
  46. * @return \think\Response
  47. */
  48. public function authLogin($key)
  49. {
  50. if (!$key) return app('json')->fail('参数有误');
  51. $data = $this->services->authLogin($key);
  52. return app('json')->success($data);
  53. }
  54. /**
  55. * @param $key
  56. * @param $phone
  57. * @param $captcha
  58. * @param $spread_code
  59. * @param $spread_spid
  60. * @param $code
  61. * @return \think\Response
  62. * @throws \Psr\SimpleCache\InvalidArgumentException
  63. */
  64. public function phoneLogin($key = '', $phone = '', $captcha = '', $spread_code = '', $spread_spid = '', $code = '')
  65. {
  66. //验证验证码
  67. $verifyCode = CacheService::get('code_' . $phone);
  68. if (!$verifyCode)
  69. return app('json')->fail('请先获取验证码');
  70. $verifyCode = substr($verifyCode, 0, 6);
  71. if ($verifyCode != $captcha) {
  72. return app('json')->fail('验证码错误');
  73. }
  74. CacheService::delete('code_' . $phone);
  75. $data = $this->services->phoneLogin($key, $phone, $spread_spid, $spread_code, $code);
  76. return app('json')->success($data);
  77. }
  78. /**
  79. * 小程序绑定手机号
  80. * @param $code
  81. * @param $iv
  82. * @param $encryptedData
  83. * @return \think\Response
  84. */
  85. public function bindingPhone($code = '', $iv = '', $encryptedData = '')
  86. {
  87. if (!$code || !$iv || !$encryptedData) return app('json')->fail('参数有误');
  88. $this->services->bindingPhone($code, $iv, $encryptedData);
  89. return app('json')->success(410016);
  90. }
  91. /**
  92. * 小程序授权登录
  93. * @param Request $request
  94. * @return mixed
  95. * @throws \Psr\SimpleCache\InvalidArgumentException
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. * @throws \think\exception\DbException
  99. */
  100. public function auth(Request $request)
  101. {
  102. [$code, $spread_spid, $spread_code, $iv, $encryptedData] = $request->postMore([
  103. ['code', ''],
  104. ['spread_spid', 0],
  105. ['spread_code', ''],
  106. ['iv', ''],
  107. ['encryptedData', ''],
  108. ], true);
  109. $token = $this->services->newAuth($code, $spread_spid, $spread_code, $iv, $encryptedData);
  110. if ($token) {
  111. if (isset($token['key']) && $token['key']) {
  112. return app('json')->successful('授权成功,请绑定手机号', $token);
  113. } else {
  114. return app('json')->successful('登录成功!', ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp'], 'store_user_avatar' => $token['store_user_avatar'] ?? 0]);
  115. }
  116. } else
  117. return app('json')->fail('获取用户访问token失败!');
  118. }
  119. /**
  120. * 静默授权
  121. * @param $code
  122. * @param $spread
  123. * @return mixed
  124. */
  125. public function silenceAuth($code, $spread_code = '', $spread_spid = '')
  126. {
  127. $token = $this->services->silenceAuth($code, (int)$spread_code, (int)$spread_spid);
  128. if ($token && isset($token['key'])) {
  129. return app('json')->success('授权成功,请绑定手机号', $token);
  130. } else if ($token) {
  131. return app('json')->success('登录成功', ['token' => $token['token'], 'expires_time' => $token['params']['exp'], 'store_user_avatar' => $token['store_user_avatar'] ?? 0]);
  132. } else
  133. return app('json')->fail('登录失败');
  134. }
  135. /**
  136. * 静默授权 不登录
  137. * @param $code
  138. * @param $spread
  139. * @return mixed
  140. */
  141. public function silenceAuthNoLogin($code, $spread_code = '', $spread_spid = '')
  142. {
  143. $token = $this->services->silenceAuth($code, (int)$spread_code, (int)$spread_spid, true);
  144. if ($token && isset($token['auth_login'])) {
  145. return app('json')->success('授权成功');
  146. } else if ($token) {
  147. return app('json')->success('登录成功', ['token' => $token['token'], 'userInfo' => $token['userInfo'], 'expires_time' => $token['params']['exp'], 'store_user_avatar' => $token['store_user_avatar'] ?? 0]);
  148. } else
  149. return app('json')->fail('登录失败');
  150. }
  151. /**
  152. * 静默授权
  153. * @param $code
  154. * @param $spread
  155. * @return mixed
  156. */
  157. public function silenceAuthBindingPhone($code = '', $spread_code = '', $spread_spid = '', $phone = '', $captcha = '')
  158. {
  159. //验证验证码
  160. $verifyCode = CacheService::get('code_' . $phone);
  161. if (!$verifyCode)
  162. return app('json')->fail('请先获取验证码');
  163. $verifyCode = substr($verifyCode, 0, 6);
  164. if ($verifyCode != $captcha) {
  165. CacheService::delete('code_' . $phone);
  166. return app('json')->fail('验证码错误');
  167. }
  168. $token = $this->services->silenceAuthBindingPhone($code, $spread_code, $spread_spid, $phone);
  169. if ($token) {
  170. CacheService::delete('code_' . $phone);
  171. return app('json')->success('登录成功', ['token' => $token['token'], 'expires_time' => $token['params']['exp'], 'store_user_avatar' => $token['store_user_avatar'] ?? 0]);
  172. } else
  173. return app('json')->fail('登录失败');
  174. }
  175. /**
  176. * 授权获取小程序用户手机号 直接绑定
  177. * @param $code
  178. * @param $iv
  179. * @param $encryptedData
  180. * @return mixed
  181. */
  182. public function authBindingPhone($code = '', $iv = '', $encryptedData = '', $spread_code = '', $spread_spid = '', $key = '')
  183. {
  184. if (!$code || !$iv || !$encryptedData)
  185. return app('json')->fail('参数有误');
  186. $token = $this->services->authBindingPhone($code, $iv, $encryptedData, $spread_code, $spread_spid, $key);
  187. if ($token) {
  188. return app('json')->success('登录成功', $token);
  189. } else
  190. return app('json')->fail('登录失败');
  191. }
  192. /**
  193. * 更新用户信息
  194. * @param $userInfo
  195. * @return mixed
  196. */
  197. public function updateInfo(Request $request, $userInfo)
  198. {
  199. if (!$userInfo) {
  200. return app('json')->fail('参数有误');
  201. }
  202. $uid = (int)$request->uid();
  203. $re = $this->services->updateUserInfo($uid, $userInfo);
  204. if ($re) {
  205. return app('json')->success('更新成功');
  206. } else
  207. return app('json')->fail('更新失败');
  208. }
  209. }