AuthController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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\ErrorMsgServive;
  7. use App\Services\Api\UserService;
  8. use App\Services\JPushService;
  9. use App\Services\SmsServer;
  10. use Cache;
  11. use EasyWeChat\Factory;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\Auth;
  14. use Illuminate\Support\Facades\DB;
  15. use Laravel\Socialite\Facades\Socialite;
  16. use PHPUnit\Util\Exception;
  17. use Illuminate\Support\Facades\Validator;
  18. class AuthController extends Controller
  19. {
  20. public function __construct()
  21. {
  22. $this->wxConfig = ['app_id' => env("WECHAT_MINI_PROGRAM_APPID"), 'secret' => env("WECHAT_MINI_PROGRAM_SECRET"), 'response_type' => 'array'];
  23. }
  24. //注册
  25. public function register(Request $request)
  26. {
  27. $account = $request->input('account', '');
  28. $password = $request->input('password', '');
  29. $passwords = $request->input('passwords', '');
  30. $validator = Validator::make($request->all(), [
  31. 'account' => 'required',
  32. 'name' => 'required|alpha_num',
  33. 'email' => 'required',
  34. 'password' => 'required|min:6',
  35. 'passwords' => 'required|min:6',
  36. ]);
  37. if ($validator->fails()) {
  38. return $this->error($validator->errors()->first());
  39. }
  40. if($password != $passwords){
  41. return $this->error('密码不一致!');
  42. }
  43. // 查询用户是否存在
  44. $user = User::query()
  45. ->where('account','=',$account)
  46. ->first();
  47. if($user){
  48. return $this->error('账号已存在!');
  49. }
  50. if (CommonService::is_email($request->email)){ // 邮箱格式
  51. if(!EmailController::isEmailCodeRight($request->email,$request->code)){
  52. return $this->error("验证码验证失败!");
  53. }
  54. }else{
  55. return $this->error('账号格式不正确!');
  56. }
  57. $user = \App::make('getUserInstance'); //在 app/Providers/AppServiceProvider.php 里面可以创一个单例模式
  58. $user->name = $request->name; // 姓名
  59. $user->account = $request->account; // 账号
  60. $user->email = $request->email; // 邮箱
  61. $user->password = $password; //这个不是直接存密码,User模型中使用了修改器
  62. $user->register_ip = request()->ip();
  63. $user->save();
  64. return $this->success('创建成功!');
  65. }
  66. //账号密码登录
  67. public function login(Request $request)
  68. {
  69. $account = $request->input('account');
  70. $password = $request->input('password');
  71. $jpush_reg_id = $request->input('jpush_reg_id');
  72. if (!$user = User::query()->where('account','=',$account)->first()) {
  73. return $this->error('账号不存在');
  74. }
  75. // 账号是否禁用
  76. if($user->status == 0){
  77. return $this->error('账号已被禁用!');
  78. }
  79. $credentials1 = ['account' => $account, 'password' => $password];
  80. if (!auth('api')->attempt($credentials1)) {
  81. return $this->error('密码错误!');
  82. }
  83. $data = $this->doLogin($user, $jpush_reg_id);
  84. return $this->success($data);
  85. }
  86. //短信验证码登录
  87. public function loginBySmsCode(Request $request)
  88. {
  89. try {
  90. if (!$user = User::query()->where(['mobile' => $request->mobile])->first()) {
  91. return $this->error('账号不存在');
  92. }
  93. //手机验证码验证
  94. SmsServer::checkSmsCodeByVerifyKey($request->mobile, $request->smsCode);
  95. //如果登录类型和 openid 不为空
  96. $type = $request->type;
  97. if (isset($type) && !empty($type)) {
  98. if ($type == 'weixin') {
  99. if ($user->wx_openid != '') {
  100. return $this->error('已经绑定微信');
  101. }
  102. $user->wx_openid = $request->openid;
  103. $user->save();
  104. }
  105. }
  106. $data = $this->doLogin($request->mobile, $request->post('jpush_reg_id', ''));
  107. } catch (\Exception $exception) {
  108. return $this->error($exception);
  109. }
  110. return $this->success($data);
  111. }
  112. //APP第三方授权登录(微信)
  113. public function authLogin(Request $request)
  114. {
  115. try {
  116. $socialite = Socialite::driver('weixin')->stateless()->user();
  117. $user = User::query()->where('open_id', $socialite->getId())->first();
  118. if (!$user) {
  119. $data['open_id'] = $socialite->getId();
  120. $data['user'] = [];
  121. } else {
  122. $account = $user->mobile ?: $user->email;
  123. $data = $this->doLogin($account, $request->post('jpush_reg_id', ''));
  124. }
  125. } catch (Exception $exception) {
  126. ErrorMsgServive::write($exception, requst()->url());
  127. return $this->error('微信授权登录出错~');
  128. }
  129. return $this->success($data);
  130. }
  131. //微信小程序登录(微信)
  132. public function miniProgram(Request $request)
  133. {
  134. try {
  135. $mini = Factory::miniProgram($this->wxConfig);
  136. $newMini = $mini->auth->session($request->input('code'));
  137. $iv = $request->input('iv');
  138. $encryptData = $request->input('encryptData');
  139. $decryptedData = $mini->encryptor->decryptData($newMini['session_key'], $iv, $encryptData);
  140. $openId = $decryptedData['openid'];
  141. $user = User::query()->where('open_id', $openId)->first();
  142. if (!$user) {
  143. $data['open_id'] = $openId;
  144. $data['user'] = [];
  145. } else {
  146. $account = $user->mobile ?: $user->email;
  147. $data = $this->doLogin($account, $request->post('jpush_reg_id', ''));
  148. }
  149. } catch (Exception $exception) {
  150. ErrorMsgServive::write($exception, requst()->url());
  151. return $this->error('微信授权登录出错~');
  152. }
  153. return $this->success($data);
  154. }
  155. //微信小程序获取手机号
  156. public function decryptPhone(Request $request)
  157. {
  158. $user = auth('api')->user();
  159. try {
  160. $mini = Factory::miniProgram($this->wxConfig);
  161. $newMini = $mini->auth->session($request->input('code'));
  162. $iv = $request->input('iv');
  163. $encryptData = $request->input('encryptData');
  164. $decryptedData = $mini->encryptor->decryptData($newMini['session_key'], $iv, $encryptData);
  165. $user = User::query()->where('id', $user->id)->first();
  166. $user->mobile = $decryptedData['purePhoneNumber'];
  167. $user->save();
  168. } catch (\Exception $exception) {
  169. ErrorMsgServive::write($exception, requst()->url());
  170. return $this->error('获取手机号出错~');
  171. }
  172. return $this->success();
  173. }
  174. //H5 应用进行微信授权登录
  175. public function h5Oauth()
  176. {
  177. }
  178. //微信小程序 code
  179. public function miniCode()
  180. {
  181. }
  182. //执行登录
  183. public function doLogin($user, $jpush_reg_id = null)
  184. {
  185. if (!empty($jpush_reg_id)) {
  186. //清除登陆过本设备的账号设备id
  187. User::query()->where('jpush_reg_id', $jpush_reg_id)->update(['jpush_reg_id' => '']);
  188. //当前登录用户绑定设备
  189. $user->jpush_reg_id = $jpush_reg_id;
  190. //清除别名
  191. JPushService::deleteAlias('user_id_' . $user->id);
  192. //设置极光推送别名
  193. JPushService::updateAlias($user->jpush_reg_id, 'user_id_' . $user->id);
  194. }
  195. $user->online = 1;
  196. $user->last_login_time = date('Y-m-d H:i:s');
  197. $user->last_login_ip = request()->ip();
  198. if (!$user->save()) {
  199. return $this->error('登录失败!');
  200. }
  201. $token = Auth::guard('api')->fromUser($user);
  202. $userInfo = UserService::getUserInfoById($user->id);
  203. $data = [
  204. 'token' => "Bearer " . $token,
  205. 'user_info' => $userInfo,
  206. ];
  207. return $data;
  208. }
  209. //用户是否存在
  210. public function isUserExist($account)
  211. {
  212. $user = User::where(['mobile' => $account])
  213. ->orWhere(['email' => $account])
  214. ->first();
  215. if (!$user) {
  216. return false;
  217. }
  218. return $user;
  219. }
  220. //忘记密码
  221. public function forgetPassword(Request $request)
  222. {
  223. $account = $request->input('account', '');
  224. $validator = Validator::make($request->all(), [
  225. 'account' => 'required',
  226. ]);
  227. if ($validator->fails()) {
  228. return $this->error($validator->errors()->first());
  229. }
  230. // 查询用户是否存在
  231. $user = User::query()
  232. ->where('account','=',$account)
  233. ->first();
  234. if(!$user){
  235. return $this->error('账号不存在!');
  236. }
  237. if($user->status == 0){
  238. return $this->error('账号已被禁用!');
  239. }
  240. // 随机生成密码
  241. $password = rand(100000, 999999);
  242. $content = '您找回的密码为系统重新生成:'.$password.',登录后请自行修改!';
  243. $res = EmailController::sendNotice($user->email,'找回密码通知',$content);
  244. if(!$res){
  245. return $this->error("找回密码失败!");
  246. }
  247. $user->password = $password; // 处理过加密的
  248. $user->save();
  249. return $this->success('',0,'我们已将密码发至您的电子邮件!');
  250. }
  251. //找回ID
  252. public function findId(Request $request)
  253. {
  254. $email = $request->input('email', '');
  255. $validator = Validator::make($request->all(), [
  256. 'email' => 'required',
  257. ]);
  258. if ($validator->fails()) {
  259. return $this->error($validator->errors()->first());
  260. }
  261. // 查询用户是否存在
  262. $user = User::query()
  263. ->where('email','=',$email)
  264. ->first();
  265. if(!$user){
  266. return $this->error('邮箱不存在!');
  267. }
  268. if($user->status == 0){
  269. return $this->error('账号已被禁用!');
  270. }
  271. $content = '您找回的ID为:'.$user->account.',请妥善保存!';
  272. $res = EmailController::sendNotice($user->email,'找回ID通知',$content);
  273. if(!$res){
  274. return $this->error("找回ID失败!");
  275. }
  276. return $this->success('',0,'我们已将ID发至您的电子邮箱!');
  277. }
  278. //退出
  279. public function logout()
  280. {
  281. $user = auth('api')->user();
  282. if($user){
  283. if(!empty($user->jpush_reg_id)){
  284. //清空极光别名
  285. JPushService::updateAlias($user->jpush_reg_id, '');
  286. }
  287. $user->online = 0;
  288. $user->save();
  289. }
  290. auth('api')->logout();
  291. return $this->success('',0,'退出成功!');
  292. }
  293. }