AuthController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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->nickname = $request->account; // 账号
  58. $user->email = $request->email; // 邮箱
  59. $user->password = $password; //这个不是直接存密码,User模型中使用了修改器
  60. $user->register_ip = request()->ip();
  61. $user->save();
  62. return $this->success('创建成功!');
  63. }
  64. //账号密码登录
  65. public function login(Request $request)
  66. {
  67. $account = $request->input('account');
  68. $password = $request->input('password');
  69. $jpush_reg_id = $request->input('jpush_reg_id');
  70. if (!$user = User::query()->where('account','=',$account)->orWhere('email','=',$account)->first()) {
  71. return $this->error('账号不存在');
  72. }
  73. $credentials1 = ['account' => $account, 'password' => $password];
  74. $credentials2 = ['email' => $account, 'password' => $password];
  75. if (!auth('api')->attempt($credentials1) && !auth('api')->attempt($credentials2)) {
  76. return $this->error('密码错误!');
  77. }
  78. // $user->nickname = $user->nickname.'@huabook.net';
  79. $data = $this->doLogin($user, $jpush_reg_id);
  80. return $this->success($data);
  81. }
  82. //短信验证码登录
  83. public function loginBySmsCode(Request $request)
  84. {
  85. try {
  86. if (!$user = User::query()->where(['mobile' => $request->mobile])->first()) {
  87. return $this->error('账号不存在');
  88. }
  89. //手机验证码验证
  90. SmsServer::checkSmsCodeByVerifyKey($request->mobile, $request->smsCode);
  91. //如果登录类型和 openid 不为空
  92. $type = $request->type;
  93. if (isset($type) && !empty($type)) {
  94. if ($type == 'weixin') {
  95. if ($user->wx_openid != '') {
  96. return $this->error('已经绑定微信');
  97. }
  98. $user->wx_openid = $request->openid;
  99. $user->save();
  100. }
  101. }
  102. $data = $this->doLogin($request->mobile, $request->post('jpush_reg_id', ''));
  103. } catch (\Exception $exception) {
  104. return $this->error($exception);
  105. }
  106. return $this->success($data);
  107. }
  108. //执行登录
  109. public function doLogin($user, $jpush_reg_id = null)
  110. {
  111. if (!empty($jpush_reg_id)) {
  112. //清除登陆过本设备的账号设备id
  113. User::query()->where('jpush_reg_id', $jpush_reg_id)->update(['jpush_reg_id' => '']);
  114. //当前登录用户绑定设备
  115. $user->jpush_reg_id = $jpush_reg_id;
  116. //清除别名
  117. JPushService::deleteAlias('user_id_' . $user->id);
  118. //设置极光推送别名
  119. JPushService::updateAlias($user->jpush_reg_id, 'user_id_' . $user->id);
  120. }
  121. $user->online = 1;
  122. $user->last_login_time = date('Y-m-d H:i:s');
  123. $user->last_login_ip = request()->ip();
  124. if (!$user->save()) {
  125. return $this->error('登录失败!');
  126. }
  127. $token = Auth::guard('api')->fromUser($user);
  128. $userInfo = UserService::getUserInfoById($user->id);
  129. $data = [
  130. 'token' => "Bearer " . $token,
  131. 'user_info' => $userInfo,
  132. ];
  133. return $data;
  134. }
  135. //用户是否存在
  136. public function isUserExist($account)
  137. {
  138. $user = User::where(['mobile' => $account])
  139. ->orWhere(['email' => $account])
  140. ->first();
  141. if (!$user) {
  142. return false;
  143. }
  144. return $user;
  145. }
  146. //忘记密码
  147. public function forgetPassword(Request $request)
  148. {
  149. $account = $request->input('account', '');
  150. $captcha = $request->input('captcha', '');
  151. $captcha_key = $request->input('captcha_key','');
  152. $validator = Validator::make($request->all(), [
  153. 'account' => 'required',
  154. 'captcha' => 'required',
  155. 'captcha_key' => 'required',
  156. ]);
  157. if ($validator->fails()) {
  158. return $this->error($validator->errors()->first());
  159. }
  160. if(!captcha_api_check($captcha,$captcha_key)){
  161. return $this->error("图形验证码错误!");
  162. }
  163. // 查询用户是否存在
  164. $user = User::query()
  165. ->where('account','=',$account)
  166. ->first();
  167. if(!$user){
  168. return $this->error('账号不存在!');
  169. }
  170. if($user->status == 0){
  171. return $this->error('账号已被禁用!');
  172. }
  173. // 随机生成密码
  174. $password = rand(100000, 999999);
  175. $content = '您找回的密码为系统重新生成:'.$password.',登录后请自行修改!';
  176. $res = EmailController::sendNotice($user->email,'找回密码通知',$content);
  177. if(!$res){
  178. return $this->error("找回密码失败!");
  179. }
  180. $user->password = $password; // 处理过加密的
  181. $user->save();
  182. return $this->success('',0,'我们已将密码发至您的电子邮件!');
  183. }
  184. //找回ID
  185. public function findId(Request $request)
  186. {
  187. $email = $request->input('email', '');
  188. $captcha = $request->input('captcha', '');
  189. $captcha_key = $request->input('captcha_key','');
  190. $validator = Validator::make($request->all(), [
  191. 'captcha' => 'required',
  192. 'captcha_key' => 'required',
  193. 'email' => 'required',
  194. ]);
  195. if ($validator->fails()) {
  196. return $this->error($validator->errors()->first());
  197. }
  198. if(!captcha_api_check($captcha,$captcha_key)){
  199. return $this->error("图形验证码错误!");
  200. }
  201. // 查询用户是否存在
  202. $user = User::query()
  203. ->where('email','=',$email)
  204. ->first();
  205. if(!$user){
  206. return $this->error('邮箱不存在!');
  207. }
  208. if($user->status == 0){
  209. return $this->error('账号已被禁用!');
  210. }
  211. $content = '您找回的ID为:'.$user->account.',请妥善保存!';
  212. $res = EmailController::sendNotice($user->email,'找回ID通知',$content);
  213. if(!$res){
  214. return $this->error("找回ID失败!");
  215. }
  216. return $this->success('',0,'我们已将ID发至您的电子邮箱!');
  217. }
  218. //退出
  219. public function logout()
  220. {
  221. $user = auth('api')->user();
  222. if($user){
  223. if(!empty($user->jpush_reg_id)){
  224. //清空极光别名
  225. JPushService::updateAlias($user->jpush_reg_id, '');
  226. }
  227. $user->online = 0;
  228. $user->save();
  229. }
  230. auth('api')->logout();
  231. return $this->success('',0,'退出成功!');
  232. }
  233. /**
  234. * @return void
  235. * 图形验证码
  236. */
  237. public function captcha(){
  238. $captcha = app('captcha')->create('default', true);
  239. return $this->success($captcha);
  240. }
  241. }