|
|
|
@ -17,7 +17,8 @@ class LoginService extends BaseService |
|
|
|
* 判断登录状态 |
|
|
|
* @return bool |
|
|
|
*/ |
|
|
|
public function isLogin() { |
|
|
|
public function isLogin() |
|
|
|
{ |
|
|
|
if (!$this->user) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
@ -32,23 +33,24 @@ class LoginService extends BaseService |
|
|
|
* @return array |
|
|
|
* @throws \fast\FuncException |
|
|
|
*/ |
|
|
|
public function code2session($code, $iv, $encryptedData){ |
|
|
|
public function code2session($code, $iv, $encryptedData) |
|
|
|
{ |
|
|
|
|
|
|
|
$http = new Http(); |
|
|
|
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=".env("app.appid")."&secret=".env("app.appsecret")."&js_code={$code}&grant_type=authorization_code"; |
|
|
|
$res = $http::get($url); |
|
|
|
if($res['code'] != 200){ |
|
|
|
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . env("app.appid") . "&secret=" . env("app.appsecret") . "&js_code={$code}&grant_type=authorization_code"; |
|
|
|
$res = $http::get($url); |
|
|
|
if ($res['code'] != 200) { |
|
|
|
throw new \fast\FuncException($res['msg']); |
|
|
|
} |
|
|
|
$res['data'] = json_decode($res['data'], true); |
|
|
|
if(isset($res['data']['errcode'])){ |
|
|
|
if (isset($res['data']['errcode'])) { |
|
|
|
throw new \fast\FuncException($res['data']['errmsg']); |
|
|
|
} |
|
|
|
session('app_openid', $res['data']['openid']); |
|
|
|
session('app_session_key', $res['data']['session_key']); |
|
|
|
$res['userInfo'] = json_decode($this->decodeWechatIv($iv, $encryptedData), true); |
|
|
|
|
|
|
|
$result = []; |
|
|
|
$result = []; |
|
|
|
$result['openid'] = $res['data']['openid']; |
|
|
|
if (isset($res['data']['unionid'])) $result['unionid'] = $res['data']['unionid']; |
|
|
|
$result['phone'] = $res['userInfo']['phoneNumber']; |
|
|
|
@ -63,10 +65,11 @@ class LoginService extends BaseService |
|
|
|
* @return false|string |
|
|
|
* @throws \fast\FuncException |
|
|
|
*/ |
|
|
|
public function decodeWechatIv($iv, $encryptedData){ |
|
|
|
$openid = session('app_openid'); |
|
|
|
public function decodeWechatIv($iv, $encryptedData) |
|
|
|
{ |
|
|
|
$openid = session('app_openid'); |
|
|
|
$session_key = session('app_session_key'); |
|
|
|
if(!$openid || !$session_key){ |
|
|
|
if (!$openid || !$session_key) { |
|
|
|
throw new \fast\FuncException('缺少主要参数'); |
|
|
|
} |
|
|
|
if (strlen($session_key) != 24) { |
|
|
|
@ -75,15 +78,15 @@ class LoginService extends BaseService |
|
|
|
if (strlen($iv) != 24) { |
|
|
|
throw new \fast\FuncException('iv长度错误'); |
|
|
|
} |
|
|
|
$aesKey=base64_decode($session_key); |
|
|
|
$aesIV=base64_decode($iv); |
|
|
|
$aesCipher=base64_decode($encryptedData); |
|
|
|
$result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV); |
|
|
|
$dataObj=json_decode($result); |
|
|
|
if( $dataObj == NULL ) { |
|
|
|
$aesKey = base64_decode($session_key); |
|
|
|
$aesIV = base64_decode($iv); |
|
|
|
$aesCipher = base64_decode($encryptedData); |
|
|
|
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV); |
|
|
|
$dataObj = json_decode($result); |
|
|
|
if ($dataObj == NULL) { |
|
|
|
throw new \fast\FuncException('登录失败,请稍候再试'); |
|
|
|
} |
|
|
|
if( $dataObj->watermark->appid != env("app.appid") ) { |
|
|
|
if ($dataObj->watermark->appid != env("app.appid")) { |
|
|
|
throw new \fast\FuncException('小程序appid不一致,登录失败'); |
|
|
|
} |
|
|
|
return $result; |
|
|
|
@ -102,22 +105,43 @@ class LoginService extends BaseService |
|
|
|
* @throws \think\db\exception\DbException |
|
|
|
* @throws \think\db\exception\ModelNotFoundException |
|
|
|
*/ |
|
|
|
public function userLogin($phone, $openid, $unionid) { |
|
|
|
public function userLogin($phone, $openid, $unionid) |
|
|
|
{ |
|
|
|
$field = 'id,openid,phone,nickname,sex,headimgurl'; |
|
|
|
$user = WechatUser::where('phone', $phone)->where('openid', $openid)->where('delete_time', 0)->field($field)->find(); |
|
|
|
if($user){ |
|
|
|
$user = WechatUser::where('phone', $phone)->where('openid', $openid)->where('delete_time', 0)->field($field)->find(); |
|
|
|
if ($user) { |
|
|
|
return $this->userSuccess($user); |
|
|
|
} |
|
|
|
return $this->register($phone, $openid, $unionid); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* @param $phone |
|
|
|
* @return WechatUser|array|mixed |
|
|
|
* @throws \fast\FuncException |
|
|
|
* @throws \think\db\exception\DataNotFoundException |
|
|
|
* @throws \think\db\exception\DbException |
|
|
|
* @throws \think\db\exception\ModelNotFoundException |
|
|
|
*/ |
|
|
|
public function userPhoneLogin($phone) |
|
|
|
{ |
|
|
|
$field = 'id,openid,phone,nickname,sex,headimgurl'; |
|
|
|
$user = WechatUser::where('phone', $phone)->where('delete_time', 0)->field($field)->find(); |
|
|
|
if ($user) { |
|
|
|
return $this->userSuccess($user); |
|
|
|
} |
|
|
|
return $this->register($phone); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 用户登录成功 |
|
|
|
* @param WechatUser $user |
|
|
|
* @return array |
|
|
|
*/ |
|
|
|
public function userSuccess(WechatUser $user) { |
|
|
|
public function userSuccess(WechatUser $user) |
|
|
|
{ |
|
|
|
session('user', $user->toArray()); |
|
|
|
$this->userKeeplogin($user->id,$user->openid,3600 * 24 * 7); |
|
|
|
// $user->visible(['id', 'name', 'logo']); |
|
|
|
@ -131,13 +155,14 @@ class LoginService extends BaseService |
|
|
|
* @param $keeptime |
|
|
|
* @return bool |
|
|
|
*/ |
|
|
|
protected function userKeeplogin($user_id,$token,$keeptime = 0) { |
|
|
|
protected function userKeeplogin($user_id, $token, $keeptime = 0) |
|
|
|
{ |
|
|
|
if ($keeptime) { |
|
|
|
$expiretime = time() + $keeptime; |
|
|
|
|
|
|
|
$key = md5(md5(strval($user_id)) . md5(strval($keeptime)) . md5(strval($expiretime)) . $token); |
|
|
|
error_reporting(E_ALL); |
|
|
|
ini_set('display_errors','1'); |
|
|
|
ini_set('display_errors', '1'); |
|
|
|
$data = [$user_id, $keeptime, $expiretime, $key]; |
|
|
|
|
|
|
|
Cookie::set('userKeeplogin', implode('|', $data), 86400 * 30); |
|
|
|
@ -149,25 +174,26 @@ class LoginService extends BaseService |
|
|
|
/** |
|
|
|
* 用户端注册 |
|
|
|
* @param $phone |
|
|
|
* @param $openid |
|
|
|
* @param $unionid |
|
|
|
* @param string $openid |
|
|
|
* @param string $unionid |
|
|
|
* @return WechatUser|mixed |
|
|
|
* @throws \fast\FuncException |
|
|
|
* @throws \think\db\exception\DataNotFoundException |
|
|
|
* @throws \think\db\exception\DbException |
|
|
|
* @throws \think\db\exception\ModelNotFoundException |
|
|
|
*/ |
|
|
|
public function register($phone, $openid, $unionid) { |
|
|
|
public function register($phone, string $openid = '', string $unionid = '') |
|
|
|
{ |
|
|
|
|
|
|
|
$add = [ |
|
|
|
'phone' => $phone, |
|
|
|
'openid' => $openid, |
|
|
|
'phone' => $phone, |
|
|
|
'openid' => $openid, |
|
|
|
'nickname' => '微信用户', |
|
|
|
'unionid' => $unionid ?? '', |
|
|
|
'unionid' => $unionid ?? '', |
|
|
|
]; |
|
|
|
$id = (new WechatUser())->insertGetId($add); |
|
|
|
$id = (new WechatUser())->insertGetId($add); |
|
|
|
|
|
|
|
if(!$id){ |
|
|
|
if (!$id) { |
|
|
|
throw new \fast\FuncException('注册失败,请稍候再试'); |
|
|
|
} |
|
|
|
$user = WechatUser::where('id', $id)->find(); |
|
|
|
|