|
@@ -2,15 +2,11 @@
|
|
|
|
|
|
namespace App\Http\Controllers\V1;
|
|
namespace App\Http\Controllers\V1;
|
|
|
|
|
|
-use App\Models\Job;
|
|
|
|
|
|
+use App\Models\Account;
|
|
use App\Models\User;
|
|
use App\Models\User;
|
|
-use App\Models\UserInfo;
|
|
|
|
-use App\Services\Api\ErrorMsgServive;
|
|
|
|
-use App\Services\Api\UserService;
|
|
|
|
-use App\Services\JPushService;
|
|
|
|
-use App\Services\SmsServer;
|
|
|
|
use Cache;
|
|
use Cache;
|
|
use EasyWeChat\Factory;
|
|
use EasyWeChat\Factory;
|
|
|
|
+use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\DB;
|
|
@@ -20,294 +16,110 @@ use Validator;
|
|
|
|
|
|
class AuthController extends Controller
|
|
class AuthController extends Controller
|
|
{
|
|
{
|
|
- public function __construct()
|
|
|
|
- {
|
|
|
|
- $this->wxConfig = ['app_id' => env("WECHAT_MINI_PROGRAM_APPID"), 'secret' => env("WECHAT_MINI_PROGRAM_SECRET"), 'response_type' => 'array'];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //注册
|
|
|
|
- public function register(Request $request)
|
|
|
|
- {
|
|
|
|
- $mobile = $request->input('mobile', '');
|
|
|
|
- $password = $request->input('password', '');
|
|
|
|
-
|
|
|
|
- $validator = Validator::make($request->all(), [
|
|
|
|
- 'mobile' => 'required',
|
|
|
|
- 'password' => 'required|min:6',
|
|
|
|
- ]);
|
|
|
|
- if ($validator->fails()) {
|
|
|
|
- return $this->error($validator->errors()->first());
|
|
|
|
- }
|
|
|
|
- if (UserService::checkUserByMobile($mobile)) {
|
|
|
|
- return $this->error('手机号已被占用');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- try { //手机验证码验证
|
|
|
|
- SmsServer::checkSmsCodeByVerifyKey($request->verifyKey, $request->code);
|
|
|
|
- } catch (Exception $exception) {
|
|
|
|
- return $this->error($exception->getMessage());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- $user = App::make('getUserInstance'); //在 app/Providers/AppServiceProvider.php 里面可以创一个单例模式
|
|
|
|
- $user->name = 'User' . mb_substr($mobile, 0, 6);
|
|
|
|
- $user->avatar = '';
|
|
|
|
- $user->mobile = $mobile;
|
|
|
|
- $user->password = $password; //这个不是直接存密码,User模型中使用了修改器
|
|
|
|
- $user->register_ip = request()->ip();
|
|
|
|
-
|
|
|
|
- return $this->success('创建成功!');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //账号密码登录
|
|
|
|
public function login(Request $request)
|
|
public function login(Request $request)
|
|
- {
|
|
|
|
- $account = $request->input('account');
|
|
|
|
- $password = $request->input('password');
|
|
|
|
- $jpush_reg_id = $request->input('jpush_reg_id');
|
|
|
|
-
|
|
|
|
- if (!$user = User::query()->where(['mobile' => $account])->orWhere(['email' => $account])->first()) {
|
|
|
|
- return $this->error('账号不存在');
|
|
|
|
- }
|
|
|
|
- $credentials1 = ['mobile' => $account, 'password' => $password];
|
|
|
|
- $credentials2 = ['email' => $account, 'password' => $password];
|
|
|
|
- if (!auth('api')->attempt($credentials1) && !auth('api')->attempt($credentials2)) {
|
|
|
|
- return $this->error('密码错误!');
|
|
|
|
- }
|
|
|
|
- $data = $this->doLogin($user, $jpush_reg_id);
|
|
|
|
-
|
|
|
|
- return $this->success($data);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //APP第三方授权登录(微信)
|
|
|
|
- public function authLogin(Request $request)
|
|
|
|
{
|
|
{
|
|
try {
|
|
try {
|
|
- $socialite = Socialite::driver('weixin')->stateless()->user();
|
|
|
|
- $user = User::query()->where('open_id', $socialite->getId())->first();
|
|
|
|
- if (!$user) {
|
|
|
|
- $data['open_id'] = $socialite->getId();
|
|
|
|
- $data['user'] = [];
|
|
|
|
- } else {
|
|
|
|
- $account = $user->mobile ?: $user->email;
|
|
|
|
- $data = $this->doLogin($account, $request->post('jpush_reg_id', ''));
|
|
|
|
|
|
+ $req = $request->post();
|
|
|
|
+ $this->validate($request, [
|
|
|
|
+ 'account' => 'required|digits:11',
|
|
|
|
+ 'password' => 'required',
|
|
|
|
+ 'code' => 'required',
|
|
|
|
+ ]);
|
|
|
|
+
|
|
|
|
+ $account = Account::where('account', $req['account'])
|
|
|
|
+ ->where('status', 1)
|
|
|
|
+ ->first();
|
|
|
|
+
|
|
|
|
+ if(!$account){
|
|
|
|
+ return $this->error('没有找到相关账号');
|
|
}
|
|
}
|
|
- } catch (Exception $exception) {
|
|
|
|
- ErrorMsgServive::write($exception, requst()->url());
|
|
|
|
- return $this->error('微信授权登录出错~');
|
|
|
|
- }
|
|
|
|
- return $this->success($data);
|
|
|
|
- }
|
|
|
|
|
|
|
|
- //微信小程序登录(微信)
|
|
|
|
- public function miniProgram(Request $request)
|
|
|
|
- {
|
|
|
|
- try {
|
|
|
|
- $mini = Factory::miniProgram($this->wxConfig);
|
|
|
|
- $newMini = $mini->auth->session($request->input('code'));
|
|
|
|
-
|
|
|
|
- $iv = $request->input('iv');
|
|
|
|
- $encryptData = $request->input('encryptData');
|
|
|
|
- $decryptedData = $mini->encryptor->decryptData($newMini['session_key'], $iv, $encryptData);
|
|
|
|
- $openId = $decryptedData['openid'];
|
|
|
|
- $user = User::query()->where('open_id', $openId)->first();
|
|
|
|
- if (!$user) {
|
|
|
|
- $data['open_id'] = $openId;
|
|
|
|
- $data['user'] = [];
|
|
|
|
- } else {
|
|
|
|
- $account = $user->mobile ?: $user->email;
|
|
|
|
- $data = $this->doLogin($account, $request->post('jpush_reg_id', ''));
|
|
|
|
|
|
+ if(!\Hash::check($req['password'], $account->password)){
|
|
|
|
+ return $this->error('账号或密码错误');
|
|
}
|
|
}
|
|
- } catch (Exception $exception) {
|
|
|
|
- ErrorMsgServive::write($exception, requst()->url());
|
|
|
|
- return $this->error('微信授权登录出错~');
|
|
|
|
- }
|
|
|
|
- return $this->success($data);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //微信小程序获取手机号
|
|
|
|
- public function decryptPhone(Request $request)
|
|
|
|
- {
|
|
|
|
- $user = auth('api')->user();
|
|
|
|
- try {
|
|
|
|
- $mini = Factory::miniProgram($this->wxConfig);
|
|
|
|
- $newMini = $mini->auth->session($request->input('code'));
|
|
|
|
-
|
|
|
|
- $iv = $request->input('iv');
|
|
|
|
- $encryptData = $request->input('encryptData');
|
|
|
|
- $decryptedData = $mini->encryptor->decryptData($newMini['session_key'], $iv, $encryptData);
|
|
|
|
|
|
|
|
- $user = User::query()->where('id', $user->id)->first();
|
|
|
|
- $user->mobile = $decryptedData['purePhoneNumber'];
|
|
|
|
|
|
+ $app = Factory::miniProgram(config('wechat.mini_program.default'));
|
|
|
|
+ $data = $app->auth->session($req['code']);
|
|
|
|
+ $user = User::where('open_id',$data['openid'])->first();
|
|
|
|
+ $user->account_id = $account->id;
|
|
$user->save();
|
|
$user->save();
|
|
- } catch (\Exception $exception) {
|
|
|
|
- ErrorMsgServive::write($exception, requst()->url());
|
|
|
|
- return $this->error('获取手机号出错~');
|
|
|
|
- }
|
|
|
|
- return $this->success();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //H5 应用进行微信授权登录
|
|
|
|
- public function h5Oauth()
|
|
|
|
- {
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //微信小程序 code
|
|
|
|
- public function miniCode()
|
|
|
|
- {
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 字节跳动登陆 code
|
|
|
|
- public function bytedance(Request $request)
|
|
|
|
- {
|
|
|
|
- try {
|
|
|
|
- $code = $request->input('code');
|
|
|
|
- $app = $this->getUniFactory();
|
|
|
|
- $res = $app->login($code);
|
|
|
|
-
|
|
|
|
- $openId = $res['openid'];
|
|
|
|
- $user = User::where('open_id', $openId)->first();
|
|
|
|
- if (!$user) {
|
|
|
|
- $user = new User();
|
|
|
|
- $user->open_id = $openId;
|
|
|
|
- $user->union_id = $res['unionid'];
|
|
|
|
- $user->remember_token = $res['session_key'];
|
|
|
|
- $user->save();
|
|
|
|
-
|
|
|
|
- $info = new UserInfo();
|
|
|
|
- $info->user_id = $user->id;
|
|
|
|
- $info->platform = 1;
|
|
|
|
- $info->save();
|
|
|
|
-
|
|
|
|
- $user = User::where('id', $user->id)->first();
|
|
|
|
- }else{
|
|
|
|
- $user->remember_token = $res['session_key'];
|
|
|
|
- $user->save();
|
|
|
|
- }
|
|
|
|
|
|
|
|
$token = Auth::guard('api')->fromUser($user);
|
|
$token = Auth::guard('api')->fromUser($user);
|
|
- $user = User::with(['info'])->where('id', $user->id)->first();
|
|
|
|
|
|
+
|
|
|
|
+ $user = User::with(['account'])->where('id', $user->id)->first();
|
|
$data = [
|
|
$data = [
|
|
'token' => "Bearer " . $token,
|
|
'token' => "Bearer " . $token,
|
|
'user_info' => $user,
|
|
'user_info' => $user,
|
|
];
|
|
];
|
|
return $this->success($data);
|
|
return $this->success($data);
|
|
- } catch (\Exception $e) {
|
|
|
|
- ErrorMsgServive::write($e, \request()->url());
|
|
|
|
- return $this->error('字节授权登陆出错');
|
|
|
|
|
|
+ }catch (\Exception $ex){
|
|
|
|
+ return $this->error('账号或密码不正确,请重新输入');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // 快手登陆 code
|
|
|
|
- public function kuaishou(Request $request)
|
|
|
|
|
|
+ public function wechatMiniCode(Request $request): JsonResponse
|
|
{
|
|
{
|
|
try {
|
|
try {
|
|
- $code = $request->input('code');
|
|
|
|
- $app = $this->getUniFactory(2);
|
|
|
|
- $res = $app->login($code);
|
|
|
|
-
|
|
|
|
- $openId = $res['open_id'];
|
|
|
|
- $user = User::where('open_id', $openId)->first();
|
|
|
|
- if (!$user) {
|
|
|
|
|
|
+ $req = $request->post();
|
|
|
|
+ $this->validate($request, [
|
|
|
|
+ 'code' => 'required',
|
|
|
|
+ ]);
|
|
|
|
+ $app = Factory::miniProgram(config('wechat.mini_program.default'));
|
|
|
|
+ $data = $app->auth->session($req['code']);
|
|
|
|
+ $user = User::where('open_id',$data['openid'])->first();
|
|
|
|
+ if(!$user){
|
|
$user = new User();
|
|
$user = new User();
|
|
- $user->open_id = $openId;
|
|
|
|
- $user->union_id = ''; // 没有 union_id
|
|
|
|
- $user->remember_token = $res['session_key'];
|
|
|
|
- $user->save();
|
|
|
|
|
|
+ $user->open_id = $data['openid'];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $user->remember_token = $data['session_key'];
|
|
|
|
+ $user->save();
|
|
|
|
|
|
- $info = new UserInfo();
|
|
|
|
- $info->user_id = $user->id;
|
|
|
|
- $info->platform = 2;
|
|
|
|
- $info->save();
|
|
|
|
|
|
+ return $this->success();
|
|
|
|
+ }catch (\Exception $ex){
|
|
|
|
+ return $this->error($ex->getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function wechatMiniPhone(Request $request)
|
|
|
|
+ {
|
|
|
|
+ try{
|
|
|
|
+ $req = $request->post();
|
|
|
|
+ $this->validate($request, [
|
|
|
|
+ 'encryptedData' => 'required',
|
|
|
|
+ 'iv' => 'required',
|
|
|
|
+ ]);
|
|
|
|
+ $app = Factory::miniProgram(config('wechat.mini_program.default'));
|
|
|
|
+ $user = \user()->makeVisible('remember_token');
|
|
|
|
+ $decryptedData = $app->encryptor->decryptData($user['remember_token'], $req['iv'], $req['encryptedData']);
|
|
|
|
+
|
|
|
|
+ $account = Account::where('account', $decryptedData['phoneNumber'])
|
|
|
|
+ ->where('status', 1)
|
|
|
|
+ ->first();
|
|
|
|
+ if(!$account){
|
|
|
|
+ return $this->error('没有找到相关账号');
|
|
|
|
+ }
|
|
|
|
|
|
- $user = User::where('id', $user->id)->first();
|
|
|
|
- }else{
|
|
|
|
- $user->remember_token = $res['session_key'];
|
|
|
|
|
|
+ $user = User::where('id', $user['id'])->first();
|
|
|
|
+ if($user->mobile != $decryptedData['phoneNumber']) {
|
|
|
|
+ $user->mobile = $decryptedData['phoneNumber'];
|
|
|
|
+ $user->account_id = $account->id;
|
|
$user->save();
|
|
$user->save();
|
|
}
|
|
}
|
|
|
|
|
|
$token = Auth::guard('api')->fromUser($user);
|
|
$token = Auth::guard('api')->fromUser($user);
|
|
- $user = User::with(['info'])->where('id', $user->id)->first();
|
|
|
|
|
|
+
|
|
|
|
+ $user = User::with(['account'])->where('id', $user->id)->first();
|
|
$data = [
|
|
$data = [
|
|
'token' => "Bearer " . $token,
|
|
'token' => "Bearer " . $token,
|
|
'user_info' => $user,
|
|
'user_info' => $user,
|
|
];
|
|
];
|
|
return $this->success($data);
|
|
return $this->success($data);
|
|
- } catch (\Exception $e) {
|
|
|
|
- ErrorMsgServive::write($e, \request()->url());
|
|
|
|
- return $this->error('快手授权登陆出错');
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //执行登录
|
|
|
|
- public function doLogin($user, $jpush_reg_id = null)
|
|
|
|
- {
|
|
|
|
- if (!empty($jpush_reg_id)) {
|
|
|
|
- //清除登陆过本设备的账号设备id
|
|
|
|
- User::query()->where('jpush_reg_id', $jpush_reg_id)->update(['jpush_reg_id' => '']);
|
|
|
|
- //当前登录用户绑定设备
|
|
|
|
- $user->jpush_reg_id = $jpush_reg_id;
|
|
|
|
- //清除别名
|
|
|
|
- JPushService::deleteAlias('user_id_' . $user->id);
|
|
|
|
- //设置极光推送别名
|
|
|
|
- JPushService::updateAlias($user->jpush_reg_id, 'user_id_' . $user->id);
|
|
|
|
- }
|
|
|
|
- $user->online = 1;
|
|
|
|
- $user->last_login_time = date('Y-m-d H:i:s');
|
|
|
|
- $user->last_login_ip = request()->ip();
|
|
|
|
- if (!$user->save()) {
|
|
|
|
- return $this->error('数据保存失败');
|
|
|
|
- }
|
|
|
|
- $token = Auth::guard('api')->fromUser($user);
|
|
|
|
- $userInfo = UserService::getUserInfoById($user->id);
|
|
|
|
- $data = [
|
|
|
|
- 'token' => "Bearer " . $token,
|
|
|
|
- 'user_info' => $userInfo,
|
|
|
|
- ];
|
|
|
|
-
|
|
|
|
- return $data;
|
|
|
|
- }
|
|
|
|
|
|
|
|
- //用户是否存在
|
|
|
|
- public function isUserExist($account)
|
|
|
|
- {
|
|
|
|
- $user = User::where(['mobile' => $account])
|
|
|
|
- ->orWhere(['email' => $account])
|
|
|
|
- ->first();
|
|
|
|
- if (!$user) {
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- return $user;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //忘记密码
|
|
|
|
- public function forgetPassword(Request $request)
|
|
|
|
- {
|
|
|
|
- if ($request->new_password != $request->confirm_password) {
|
|
|
|
- return $this->error('两次密码不一致');
|
|
|
|
|
|
+ }catch (\Exception $ex){
|
|
|
|
+ return $this->error($ex->getMessage());
|
|
}
|
|
}
|
|
- try {
|
|
|
|
- SmsServer::checkSmsCodeByVerifyKey($request->verifyKey, $request->code);
|
|
|
|
- } catch (Exception $exception) {
|
|
|
|
- return $this->error($exception->getMessage());
|
|
|
|
- }
|
|
|
|
- $user->password = $request->new_password;
|
|
|
|
- $user->save();
|
|
|
|
-
|
|
|
|
- return $this->success();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- //退出
|
|
|
|
- public function logout()
|
|
|
|
- {
|
|
|
|
- $user = auth('api')->user();
|
|
|
|
- //清空极光别名
|
|
|
|
- JPushService::updateAlias($user->jpush_reg_id, '');
|
|
|
|
- $user->online = 0;
|
|
|
|
- $user->save();
|
|
|
|
- auth('api')->logout();
|
|
|
|
|
|
|
|
- return $this->success();
|
|
|
|
- }
|
|
|
|
}
|
|
}
|