对接i深圳授权登录
<?php
namespace App\Services;
use App\Models\XXX;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use App\Models\XXX;
class IszService extends BaseService
{
protected $appIds = 'XXX';
protected $appKeys = 'XXX';
public function __construct(JobSeekerUser $model)
{
parent::__construct($model);
}
// 获取i深圳initCode
public function getInitCode(Request $request){
$randomSeries = mt_rand(1000000000, 9999999999) . '';
$appId = $this->appIds;
$appKey = $this->appKeys;
$timestamp = strtotime(date('Y-m-d H:i:s'));
$cipherText = $this->getCipherText($appId, $appKey, $randomSeries, $timestamp);
$url = "https://isz-app.sz.gov.cn/smtapp/openPlatform/initCode/getInitCode.do";
$data = [
'appId' => $appId,
'timestamp' => $timestamp,
'randomSeries' => $randomSeries,
'cipherText' => $cipherText
];
$json_data = Http::withOptions([
'verify' => false
])->post($url, $data);
$result = json_decode($json_data,true);
if ($result['code'] == 200) {
return $result;
}else{
return false;
}
}
// 获取i深圳authCode
public function getAuthCode(){
$randomSeries = mt_rand(1000000000, 9999999999) . '';
$appId = $this->appIds;
$appKey = $this->appKeys;
$timestamp = strtotime(date('Y-m-d H:i:s'));
$cipherText = $this->getCipherText($appId, $appKey, $randomSeries, $timestamp);
$url = "https://isz-app.sz.gov.cn/smtapp/openPlatform/authCode/getAuthCode.do";
$data = [
'appId' => $appId,
'timestamp' => $timestamp,
'randomSeries' => $randomSeries,
'cipherText' => $cipherText
];
$json_data = Http::withOptions([
'verify' => false
])->post($url, $data);
$result = json_decode($json_data,true);
if ($result) {
return $result['data'];
}else{
return response()->failed($result['msg'], '', 500);
}
}
// 获取i深圳accessToken
public function getAccessToken($authCode){
$appId = $this->appIds;
$url = "https://isz-app.sz.gov.cn/smtapp/openPlatform/accessToken/getAccessToken.do";
$data = [
'appId' => $appId,
'authCode' => $authCode
];
$json_data = Http::withOptions([
'verify' => false
])->post($url, $data);
$result = json_decode($json_data,true);
if ($result['code'] == 200) {
return $result['data'];
}else{
return response()->failed($result['msg'], '', 500);
}
}
// 获取i深圳refreshToken
public function refreshAccessToken($refreshToken){
$appId = $this->appIds;
$url = "https://isz-app.sz.gov.cn/smtapp/openPlatform/accessToken/refreshAccessToken.do";
$data = [
'appId' => $appId,
'refreshToken' => $refreshToken
];
$json_data = Http::withOptions([
'verify' => false
])->post($url, $data);
$result = json_decode($json_data,true);
if ($result['code'] == 200) {
return $result['data'];
}else{
return response()->failed($result['msg'], '', 500);
}
}
// 获取i深圳checkAccessToken
public function checkAccessToken($accessToken){
$appId = $this->appIds;
$url = "https://isz-app.sz.gov.cn/smtapp/openPlatform/accessToken/checkAccessToken.do";
$data = [
'appId' => $appId,
'accessToken' => $accessToken
];
$json_data = Http::withOptions([
'verify' => false
])->post($url, $data);
$result = json_decode($json_data,true);
if ($result['code'] == 200) {
return $result['data'];
}else{
return response()->failed($result['msg'], '', 500);
}
}
// 获取i深圳校验checkRequestCode
public function checkRequestCode($requestCode, $accessToken){
$url = "https://isz-app.sz.gov.cn/smtapp/openPlatform/request/checkRequestCode.do";
$data = [
'appId' => $this->appIds,
'requestCode' => $requestCode,
'accessToken' => $accessToken
];
$json_data = Http::withOptions([
'verify' => false
])->post($url, $data);
$result = json_decode($json_data,true);
if ($result['code'] == 200) {
return $result['data'];
}else{
return response()->failed($result['msg'], '', 500);
}
}
// 获取i深圳checkCorporateRequestCode校验法人
public function checkCorporateRequestCode($requestCode, $accessToken){
$url = "https://isz-app.sz.gov.cn/smtapp/openPlatform/corporate/checkCorporateRequestCode.do";
$data = [
'appId' => $this->appIds,
'requestCode' => $requestCode,
'accessToken' => $accessToken
];
$json_data = Http::withOptions([
'verify' => false
])->post($url, $data);
$result = json_decode($json_data,true);
if ($result['code'] == 200) {
return $result['data'];
}else{
return response()->failed($result['msg'], '', 500);
}
}
// 获取i深圳getUserInfo用户信息
public function getIszUserInfo($userAccessToken){
$url = "https://isz-app.sz.gov.cn/smtapp/openPlatform/userInfo/getUserInfo.do";
$data = [
'userAccessToken' => $userAccessToken
];
$json_data = Http::withOptions([
'verify' => false
])->post($url, $data);
$result = json_decode($json_data,true);
if ($result['code'] == 200) {
return $result;
}else{
return response()->failed($result['msg'], '', 500);
}
}
// isz总用户接口(all)
public function getUserInfo(Request $request) {
try{
$authData = $this->getAuthCode();
$accessData = $this->getAccessToken($authData['authCode']);
$isVerify = $this->checkAccessToken($accessData['accessToken']);
if (!$isVerify) {
// 判断访问令牌是否过期,如果过期,则刷新访问令牌
$accessData = $this->refreshAccessToken($accessData['refreshToken']);
}
$response = $this->checkRequestCode($request->input('requestCode'), $accessData['accessToken']);
$info = $this->getIszUserInfo($response['userAccessToken']);
$user = JobSeekerUser::query()->where('isz_openid', $info['data']['openId'])->first();
if(is_null($user)){
$user = JobSeekerUser::query()->where('isz_token', $accessData['accessToken'])->first();
if(is_null($user)){
$user = JobSeekerUser::query()->where('phone', $info['data']['mobileNo'])->first();
}
}
if(!isset($info['data']['headImg'])) {
$info['data']['headImg'] = '';
}
if(isset($info['data']['userName'])) {
$info['data']['loginName'] = $info['data']['userName'];
}
if (is_null($user)) {
$user = $this->stores($info['data']['loginName'], $info['data']['headImg'], '', 'zhjy', '', 0, $info['data']['mobileNo'], $accessData['accessToken'], $info['data']['openId'], $info['data']['unionId']);
if (!$user) {
return false;// response()->failed('创建用户失败');
}
} else {
$user->phone = $info['data']['mobileNo'];
$user->wechat_name = $info['data']['loginName'];
if($user->head_img == ''){
$user->head_img = $info['data']['headImg'];
}
$user->isz_openid = $info['data']['openId'];
$user->isz_token = $accessData['accessToken'];
$user->isz_unionid = $info['data']['unionId'];
$user->save();
}
if(isset($info['data']['userName'])) {
$user->name = $info['data']['userName'];
$user->save();
}
if(empty($user->resume_id) || $user->resume_id == 0){
$no = 'JL' . Carbon::now()->getTimestamp();
$user->save();
}
// 加密生成token
Cache::put($accessData['accessToken'], $user->id);
$data = [
'head_img' => $user->head_img,
'phone' => $user->phone,
'token' => $user_info->original['token'],
'nickName' => $user['wechat_name'],
];
return $data;
}catch(\Exception $exception){
Log::info($exception);
return false;
}
}
// 获取i深圳cipherText
public function getCipherText($appId, $appKey, $randomSeries, $timestamp){
$str = 'appId'. $appId . 'appKey'. $appKey . 'randomSeries' . $randomSeries . 'timestamp' . $timestamp;
$md5 = md5($str);
$cipherText = strtolower($md5);
return $cipherText;
}
// 数组
public function returnData($user, $no, $info)
{
return [
];
}
}