AuthController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace App\Http\Controllers\V1;
  3. use App\Models\Job;
  4. use App\Models\User;
  5. use App\Services\Api\CommonService;
  6. use App\Services\Api\UserService;
  7. use App\Services\JPushService;
  8. use App\Services\SmsServer;
  9. use Cache;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\Validator;
  13. class AuthController extends Controller
  14. {
  15. //注册
  16. public function register(Request $request)
  17. {
  18. $account = $request->input('account', '');
  19. $password = $request->input('password', '');
  20. $passwords = $request->input('passwords', '');
  21. $captcha = $request->input('captcha', '');
  22. $captcha_key = $request->input('captcha_key','');
  23. $validator = Validator::make($request->all(), [
  24. 'account' => 'required',
  25. 'name' => 'required|alpha_num',
  26. 'email' => 'required',
  27. 'password' => 'required|min:6',
  28. 'passwords' => 'required|min:6',
  29. ]);
  30. if ($validator->fails()) {
  31. return $this->error($validator->errors()->first());
  32. }
  33. if(!captcha_api_check($captcha,$captcha_key)){
  34. return $this->error("图形验证码错误!");
  35. }
  36. if($password != $passwords){
  37. return $this->error('密码不一致!');
  38. }
  39. // 查询用户是否存在
  40. $user = User::query()
  41. ->where('account','=',$account)
  42. ->orWhere('email','=',$request->email)
  43. ->first();
  44. if($user){
  45. return $this->error('账号已存在或邮箱已被注册!');
  46. }
  47. if (CommonService::is_email($request->email)){ // 邮箱格式
  48. if(!EmailController::isEmailCodeRight($request->email,$request->code)){
  49. return $this->error("验证码错误或已过期,请重新发送!");
  50. }
  51. }else{
  52. return $this->error('账号格式不正确!');
  53. }
  54. $user = \App::make('getUserInstance'); //在 app/Providers/AppServiceProvider.php 里面可以创一个单例模式
  55. $user->name = $request->name; // 姓名
  56. $user->account = $request->account; // 账号
  57. $user->email = $request->email; // 邮箱
  58. $user->password = $password; //这个不是直接存密码,User模型中使用了修改器
  59. $user->register_ip = request()->ip();
  60. $user->save();
  61. return $this->success('创建成功!');
  62. }
  63. //账号密码登录
  64. public function login(Request $request)
  65. {
  66. $account = $request->input('account');
  67. $password = $request->input('password');
  68. $jpush_reg_id = $request->input('jpush_reg_id');
  69. if (!$user = User::query()->where('account','=',$account)->orWhere('email','=',$account)->first()) {
  70. return $this->error('账号不存在');
  71. }
  72. $credentials1 = ['account' => $account, 'password' => $password];
  73. $credentials2 = ['email' => $account, 'password' => $password];
  74. if (!auth('api')->attempt($credentials1) && !auth('api')->attempt($credentials2)) {
  75. return $this->error('密码错误!');
  76. }
  77. $data = $this->doLogin($user, $jpush_reg_id);
  78. return $this->success($data);
  79. }
  80. //短信验证码登录
  81. public function loginBySmsCode(Request $request)
  82. {
  83. try {
  84. if (!$user = User::query()->where(['mobile' => $request->mobile])->first()) {
  85. return $this->error('账号不存在');
  86. }
  87. //手机验证码验证
  88. SmsServer::checkSmsCodeByVerifyKey($request->mobile, $request->smsCode);
  89. //如果登录类型和 openid 不为空
  90. $type = $request->type;
  91. if (isset($type) && !empty($type)) {
  92. if ($type == 'weixin') {
  93. if ($user->wx_openid != '') {
  94. return $this->error('已经绑定微信');
  95. }
  96. $user->wx_openid = $request->openid;
  97. $user->save();
  98. }
  99. }
  100. $data = $this->doLogin($request->mobile, $request->post('jpush_reg_id', ''));
  101. } catch (\Exception $exception) {
  102. return $this->error($exception);
  103. }
  104. return $this->success($data);
  105. }
  106. //执行登录
  107. public function doLogin($user, $jpush_reg_id = null)
  108. {
  109. if (!empty($jpush_reg_id)) {
  110. //清除登陆过本设备的账号设备id
  111. User::query()->where('jpush_reg_id', $jpush_reg_id)->update(['jpush_reg_id' => '']);
  112. //当前登录用户绑定设备
  113. $user->jpush_reg_id = $jpush_reg_id;
  114. //清除别名
  115. JPushService::deleteAlias('user_id_' . $user->id);
  116. //设置极光推送别名
  117. JPushService::updateAlias($user->jpush_reg_id, 'user_id_' . $user->id);
  118. }
  119. $user->online = 1;
  120. $user->last_login_time = date('Y-m-d H:i:s');
  121. $user->last_login_ip = request()->ip();
  122. if (!$user->save()) {
  123. return $this->error('登录失败!');
  124. }
  125. $token = Auth::guard('api')->fromUser($user);
  126. $userInfo = UserService::getUserInfoById($user->id);
  127. $data = [
  128. 'token' => "Bearer " . $token,
  129. 'user_info' => $userInfo,
  130. ];
  131. return $data;
  132. }
  133. //用户是否存在
  134. public function isUserExist($account)
  135. {
  136. $user = User::where(['mobile' => $account])
  137. ->orWhere(['email' => $account])
  138. ->first();
  139. if (!$user) {
  140. return false;
  141. }
  142. return $user;
  143. }
  144. //忘记密码
  145. public function forgetPassword(Request $request)
  146. {
  147. $account = $request->input('account', '');
  148. $captcha = $request->input('captcha', '');
  149. $captcha_key = $request->input('captcha_key','');
  150. $validator = Validator::make($request->all(), [
  151. 'account' => 'required',
  152. 'captcha' => 'required',
  153. 'captcha_key' => 'required',
  154. ]);
  155. if ($validator->fails()) {
  156. return $this->error($validator->errors()->first());
  157. }
  158. if(!captcha_api_check($captcha,$captcha_key)){
  159. return $this->error("图形验证码错误!");
  160. }
  161. // 查询用户是否存在
  162. $user = User::query()
  163. ->where('account','=',$account)
  164. ->first();
  165. if(!$user){
  166. return $this->error('账号不存在!');
  167. }
  168. if($user->status == 0){
  169. return $this->error('账号已被禁用!');
  170. }
  171. // 随机生成密码
  172. $password = rand(100000, 999999);
  173. $content = '您找回的密码为系统重新生成:'.$password.',登录后请自行修改!';
  174. $res = EmailController::sendNotice($user->email,'找回密码通知',$content);
  175. if(!$res){
  176. return $this->error("找回密码失败!");
  177. }
  178. $user->password = $password; // 处理过加密的
  179. $user->save();
  180. return $this->success('',0,'我们已将密码发至您的电子邮件!');
  181. }
  182. //找回ID
  183. public function findId(Request $request)
  184. {
  185. $email = $request->input('email', '');
  186. $captcha = $request->input('captcha', '');
  187. $captcha_key = $request->input('captcha_key','');
  188. $validator = Validator::make($request->all(), [
  189. 'captcha' => 'required',
  190. 'captcha_key' => 'required',
  191. 'email' => 'required',
  192. ]);
  193. if ($validator->fails()) {
  194. return $this->error($validator->errors()->first());
  195. }
  196. if(!captcha_api_check($captcha,$captcha_key)){
  197. return $this->error("图形验证码错误!");
  198. }
  199. // 查询用户是否存在
  200. $user = User::query()
  201. ->where('email','=',$email)
  202. ->first();
  203. if(!$user){
  204. return $this->error('邮箱不存在!');
  205. }
  206. if($user->status == 0){
  207. return $this->error('账号已被禁用!');
  208. }
  209. $content = '您找回的ID为:'.$user->account.',请妥善保存!';
  210. $res = EmailController::sendNotice($user->email,'找回ID通知',$content);
  211. if(!$res){
  212. return $this->error("找回ID失败!");
  213. }
  214. return $this->success('',0,'我们已将ID发至您的电子邮箱!');
  215. }
  216. //退出
  217. public function logout()
  218. {
  219. $user = auth('api')->user();
  220. if($user){
  221. if(!empty($user->jpush_reg_id)){
  222. //清空极光别名
  223. JPushService::updateAlias($user->jpush_reg_id, '');
  224. }
  225. $user->online = 0;
  226. $user->save();
  227. }
  228. auth('api')->logout();
  229. return $this->success('',0,'退出成功!');
  230. }
  231. /**
  232. * @return void
  233. * 图形验证码
  234. */
  235. public function captcha(){
  236. $captcha = app('captcha')->create('default', true);
  237. return $this->success($captcha);
  238. }
  239. }