From 83a56766a25b89c070a7e299b5850fc939ce5d36 Mon Sep 17 00:00:00 2001 From: yanzai Date: Thu, 14 Aug 2025 10:28:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=AF=B9=E6=8E=A5=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/XcxController.php | 83 +++++++- app/Libraries/SM4.php | 192 ++++++++++++++++++ config/code.php | 2 + routes/web.php | 1 + 高埗医院体检平台报告对比/api/api.js | 1 + 高埗医院体检平台报告对比/api/index.js | 4 + 高埗医院体检平台报告对比/lu/axios.js | 3 +- .../pages/main/home/login.vue | 52 +++-- 8 files changed, 317 insertions(+), 21 deletions(-) create mode 100644 app/Libraries/SM4.php diff --git a/app/Http/Controllers/XcxController.php b/app/Http/Controllers/XcxController.php index 89c1a28..c656693 100644 --- a/app/Http/Controllers/XcxController.php +++ b/app/Http/Controllers/XcxController.php @@ -2,9 +2,90 @@ namespace App\Http\Controllers; +use Illuminate\Database\Schema\Blueprint; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Schema; +use SM4; +use Yo; +require_once __DIR__ . '/../../Libraries/SM4.php'; class XcxController extends Controller { - // + public static $key='llDhjopdshYJdfjk'; + public static $request; + public static $GetUserInfoUrl='https://api.gaobuyy.com:10010/cms-api/cms/patientInfo/getHealthCheckPatient'; + public function GetUserInfo(Request $request){ + $token = $request->post('token'); + //$token='eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJhcHBfdXNlcjoxOTA3NjYwNzQ2NjkwMDkzMDU3Iiwicm5TdHIiOiJGTWw5SFhOVktJMFlJQUdTdUpYY2tVcFdDeDIwaXduYSIsImNsaWVudGlkIjoiNDI4YTgzMTBjZDQ0Mjc1N2FlNjk5ZGY1ZDg5NGYwNTEiLCJ0ZW5hbnRJZCI6IjAwMDAwMCIsInVzZXJJZCI6MTkwNzY2MDc0NjY5MDA5MzA1NywidXNlck5hbWUiOiJvUUFGYzdOTFR4a0E5YmpFTnBCYV9seW9vdkxZIn0.JeDw-NFZ_cRDQHwOSGEaGYcIK12BfnDTeiBLqUBaqNQ'; + $url=env('APP_URL').'/casLogin'; + $data = []; + self::RequestLog(self::$GetUserInfoUrl , $data, "获取用户信息", '小程序接口'); + $response = Http::withHeaders([ + 'Authorization' => 'Bearer '.$token, + 'clientid' => '428a8310cd442757ae699df5d894f051' + ])->get(self::$GetUserInfoUrl,$data); + + if (!$response->successful()) { + self::$request->response_data = "请求小程序接口失败"; + self::$request->save(); + return Yo::error_echo(200092); + } + $res=$response->json(); + // 处理成功的响应 + $res_string = json_encode($res, JSON_UNESCAPED_UNICODE); + + $str_len = mb_strlen($res_string, 'utf-8'); + $str_size = $str_len / 1024; + $save_res = $res_string; + if ($str_size > 10) $save_res = '{"data":"Row size too large"}'; + self::$request->response_data = $save_res; + self::$request->save(); + if(isset($res['data']['idCardNo'])){ + $SM4= new SM4(); + $res['data']['idCardNo'] = $SM4->decrypt(self::$key,$res['data']['idCardNo']); + return Yo::echo(['id_number' => $res['data']['idCardNo']]); + }else{ + return Yo::error_echo(200093); + } + + } + + public static function RequestLog($url, $post_data, $mark, $code = 0) + { + self::CheckTableName(); + foreach ($post_data as $key => $post_datum) { + $str_len = mb_strlen(json_encode($post_datum, JSON_UNESCAPED_UNICODE), 'utf-8'); + $str_size = $str_len / 1024; + if ($str_size > 10) { + $post_data["$key"] = 'Row size too large'; + } + } + $post_data = json_encode($post_data, JSON_UNESCAPED_UNICODE); + self::$request->code = $code; + self::$request->mark = $mark; + self::$request->post_data = $post_data == '[]' ? '{}' : $post_data; + self::$request->request_url = $url; + self::$request->save(); + } + + public static function CheckTableName() + { + $table_name = 'zz_peis_log_' . date('ym'); + $table_count = DB::select('select count(1) as c from information_schema.TABLES where table_schema = ? and table_name = ?', [env('DB_DATABASE'), $table_name])[0]; + if ($table_count->c === 0) { + Schema::create($table_name, function (Blueprint $table) { + $table->id(); + $table->string('code', 50)->index(); + $table->string('mark', 50)->index(); + $table->text('post_data'); + $table->text('response_data')->nullable(); + $table->string('request_url', 2000); + $table->timestamps(); + }); + } + self::$request = new \App\Models\PEISLog(); + self::$request->setTable($table_name); + } } diff --git a/app/Libraries/SM4.php b/app/Libraries/SM4.php new file mode 100644 index 0000000..c625f10 --- /dev/null +++ b/app/Libraries/SM4.php @@ -0,0 +1,192 @@ +_rk = array(); + $this->sM4KeySchedule($key); + $bytes = $this->pad($data); + $chunks = array_chunk($bytes, $this->_block_size); + $ciphertext = ""; + foreach ($chunks as $chunk) { + $ciphertext .= $this->sM4Encrypt($chunk); + } + return base64_encode($ciphertext); + } + + public function decrypt($key, $data) + { + $this->_rk = array(); + $data = base64_decode($data); + if (strlen($data) < 0 || strlen($data) % $this->_block_size != 0) { + return false; + } + $this->sM4KeySchedule($key); + $bytes = unpack("C*", $data); + $chunks = array_chunk($bytes, $this->_block_size); + $plaintext = ""; + foreach ($chunks as $chunk) { + $plaintext .= substr($this->sM4Decrypt($chunk), 0, 16); + } + $plaintext = $this->un_pad($plaintext); + return $plaintext; + } + + private function sM4Decrypt($cipherText) + { + $x = array(); + for ($j = 0; $j < 4; $j++) { + $x[$j] = ($cipherText[$j * 4] << 24) | ($cipherText[$j * 4 + 1] << 16) | ($cipherText[$j * 4 + 2] << 8) + | ($cipherText[$j * 4 + 3]); + } + for ($i = 0; $i < 32; $i++) { + $tmp = $x[$i + 1] ^ $x[$i + 2] ^ $x[$i + 3] ^ $this->_rk[31 - $i]; + $buf = (self::$SM4_SBOX[($tmp >> 24) & 0xFF]) << 24 | (self::$SM4_SBOX[($tmp >> 16) & 0xFF]) << 16 + | (self::$SM4_SBOX[($tmp >> 8) & 0xFF]) << 8 | (self::$SM4_SBOX[$tmp & 0xFF]); + $x[$i + 4] = $x[$i] ^ ($buf ^ $this->sm4Rotl32(($buf), 2) ^ $this->sm4Rotl32(($buf), 10) + ^ $this->sm4Rotl32(($buf), 18) ^ $this->sm4Rotl32(($buf), 24)); + } + $plainText = array(); + for ($k = 0; $k < 4; $k++) { + $plainText[4 * $k] = ($x[35 - $k] >> 24) & 0xFF; + $plainText[4 * $k + 1] = ($x[35 - $k] >> 16) & 0xFF; + $plainText[4 * $k + 2] = ($x[35 - $k] >> 8) & 0xFF; + $plainText[4 * $k + 3] = ($x[35 - $k]) & 0xFF; + } + return $this->bytesToString($plainText); + } + + private function sM4Encrypt($plainText) + { + $x = array(); + for ($j = 0; $j < 4; $j++) { + $x[$j] = ($plainText[$j * 4] << 24) | ($plainText[$j * 4 + 1] << 16) | ($plainText[$j * 4 + 2] << 8) + | ($plainText[$j * 4 + 3]); + } + for ($i = 0; $i < 32; $i++) { + $tmp = $x[$i + 1] ^ $x[$i + 2] ^ $x[$i + 3] ^ $this->_rk[$i]; + $buf = (self::$SM4_SBOX[($tmp >> 24) & 0xFF]) << 24 | (self::$SM4_SBOX[($tmp >> 16) & 0xFF]) << 16 + | (self::$SM4_SBOX[($tmp >> 8) & 0xFF]) << 8 | (self::$SM4_SBOX[$tmp & 0xFF]); + $x[$i + 4] = $x[$i] ^ ($buf ^ $this->sm4Rotl32(($buf), 2) ^ $this->sm4Rotl32(($buf), 10) + ^ $this->sm4Rotl32(($buf), 18) ^ $this->sm4Rotl32(($buf), 24)); + } + $cipherText = array(); + for ($k = 0; $k < 4; $k++) { + $cipherText[4 * $k] = ($x[35 - $k] >> 24) & 0xFF; + $cipherText[4 * $k + 1] = ($x[35 - $k] >> 16) & 0xFF; + $cipherText[4 * $k + 2] = ($x[35 - $k] >> 8) & 0xFF; + $cipherText[4 * $k + 3] = ($x[35 - $k]) & 0xFF; + } + return $this->bytesToString($cipherText); + } + + private function stringToBytes($string) + { + return unpack('C*', $string); + } + + private function bytesToString($bytes) + { + return vsprintf(str_repeat('%c', count($bytes)), $bytes); + } + + private function pad($data) + { + $bytes = $this->stringToBytes($data); + $rem = $this->_block_size - count($bytes) % $this->_block_size; + for ($i = 0; $i < $rem; $i++) { + array_push($bytes, $rem); + } + return $bytes; + } + + private function un_pad($data) + { + $bytes = $this->stringToBytes($data); + $rem = $bytes[count($bytes)]; + $bytes = array_slice($bytes, 0, count($bytes) - $rem); + return $this->bytesToString($bytes); + } + + private function sm4Rotl32($buf, $n) + { + return (($buf << $n) & 0xffffffff) | ($buf >> (32 - $n)); + } + + private function sM4KeySchedule($key) + { + $this->_rk = array(); + $key = array_values(unpack("C*", $key)); + $k = array(); +// for ($i = 0; $i < 4; $i++) { +// $k[$i] = self::$SM4_FK[$i] +// ^ ((($key[4 * $i] ?? 0) << 24) +// | (($key[4 * $i + 1] ?? 0) << 16) +// | (($key[4 * $i + 2] ?? 0) << 8) +// | ($key[4 * $i + 3] ?? null)); +// } + + for ($i = 0; $i < 4; $i++) { + $k[$i] = self::$SM4_FK[$i] + ^ (((isset($key[4 * $i]) ? $key[4 * $i] : 0) << 24) + | ((isset($key[4 * $i + 1]) ? $key[4 * $i + 1] : 0) << 16) + | ((isset($key[4 * $i + 2]) ? $key[4 * $i + 2] : 0) << 8) + | (isset($key[4 * $i + 3]) ? $key[4 * $i + 3] : null)); + } + for ($j = 0; $j < 32; $j++) { + $tmp = $k[$j + 1] ^ $k[$j + 2] ^ $k[$j + 3] ^ self::$SM4_CK[$j]; + $buf = (self::$SM4_SBOX[($tmp >> 24) & 0xFF]) << 24 | (self::$SM4_SBOX[($tmp >> 16) & 0xFF]) << 16 + | (self::$SM4_SBOX[($tmp >> 8) & 0xFF]) << 8 | (self::$SM4_SBOX[$tmp & 0xFF]); + $k[$j + 4] = $k[$j] ^ (($buf) ^ ($this->sm4Rotl32(($buf), 13)) ^ ($this->sm4Rotl32(($buf), 23))); + $this->_rk[$j] = $k[$j + 4]; + } + } + +} + diff --git a/config/code.php b/config/code.php index 8f6930e..6a7b73f 100644 --- a/config/code.php +++ b/config/code.php @@ -140,4 +140,6 @@ return [ 200090 => '报告未出', 200091 => '参数不全', + 200092 => '请求接口失败', + 200093 => '获取身份证号码失败', ]; diff --git a/routes/web.php b/routes/web.php index 2ed6248..7133807 100644 --- a/routes/web.php +++ b/routes/web.php @@ -106,6 +106,7 @@ Route::post("api/$mp_api/Hospital/info", [\App\Http\Controllers\HospitalControll Route::post("api/$mp_api/HospitalActivityPackage/list", [\App\Http\Controllers\HospitalActivityPackageController::class, 'mp_list']); Route::post("api/$mp_api/Carousel/list", [\App\Http\Controllers\CarouselController::class, 'mp_list']); Route::post("api/$mp_api/Config/get", [\App\Http\Controllers\ConfigController::class, 'mp_get']); +Route::post("api/$mp_api/Xcx/GetUserInfo", [\App\Http\Controllers\XcxController::class, 'GetUserInfo']); Route::post("api/$admin_api/UserPerson/update", [\App\Http\Controllers\UserPersonController::class, 'admin_update']); Route::post("api/$admin_api/UserPerson/list", [\App\Http\Controllers\UserPersonController::class, 'admin_list']); diff --git a/高埗医院体检平台报告对比/api/api.js b/高埗医院体检平台报告对比/api/api.js index b4d8717..6ce377c 100644 --- a/高埗医院体检平台报告对比/api/api.js +++ b/高埗医院体检平台报告对比/api/api.js @@ -12,6 +12,7 @@ url_array['PrepareCheckReport'] = `${url_}/api/Mp/Report/PrepareCheckReport`; url_array['ReportContrast'] = `${url_}/api/Mp/Report/contrast`; url_array['ReportInfo'] = `${url_}/api/Mp/Report/info`; url_array['ReportList'] = `${url_}/api/Mp/Report/list`; +url_array['XcxGetUserInfo'] = `${url_}/api/Mp/Xcx/GetUserInfo`; url_array['YO'] = `${url_}/api/yo`; const api = (mark) => { if (mark === '') return url_; diff --git a/高埗医院体检平台报告对比/api/index.js b/高埗医院体检平台报告对比/api/index.js index 76c4a3d..5fc129c 100644 --- a/高埗医院体检平台报告对比/api/index.js +++ b/高埗医院体检平台报告对比/api/index.js @@ -37,6 +37,10 @@ export const ReportListAction = async (data) => await $post({ url: 'ReportList', data }) +export const XcxGetUserInfoAction = async (data) => await $post({ + url: 'XcxGetUserInfo', + data +}) export const yo = async (data) => await $post({ url: 'YO', data diff --git a/高埗医院体检平台报告对比/lu/axios.js b/高埗医院体检平台报告对比/lu/axios.js index 60024a7..1175210 100644 --- a/高埗医院体检平台报告对比/lu/axios.js +++ b/高埗医院体检平台报告对比/lu/axios.js @@ -13,7 +13,8 @@ const noLogin = [ 'ReportAnalysis', 'ReportDown', 'HospitalExtraReportStatus', - 'PrepareCheckReport' + 'PrepareCheckReport', + 'XcxGetUserInfo' ] diff --git a/高埗医院体检平台报告对比/pages/main/home/login.vue b/高埗医院体检平台报告对比/pages/main/home/login.vue index eea0e64..aa5648a 100644 --- a/高埗医院体检平台报告对比/pages/main/home/login.vue +++ b/高埗医院体检平台报告对比/pages/main/home/login.vue @@ -9,6 +9,7 @@ ref } from 'vue' import { + XcxGetUserInfoAction, $image, $response } from '@/api' @@ -22,28 +23,41 @@ }, }); const id_number = ref('') - - onLoad((e)=>{ - id_number.value=e.idnumber - try{ - if(id_number.value.length>18){ - let base64Str=decodeURIComponent(id_number.value) - id_number.value=atob(base64Str) + const token = ref('') + + const GetUserInfo = async () => { + const response = await XcxGetUserInfoAction({ + token:token.value + }) + $response(response, () => { + id_number.value = response.data.id_number + if(id_number.value){ + uni.navigateTo({ + url: `/pages/main/list/list?hospital=1&id_number=${id_number.value}` + }) + }else{ + uni.$lu.toast("身份证不能为空") } - }catch(e){ - console.error('解码失败', e) - uni.$lu.toast("解码失败"+e) - return false - } + }) + } + onLoad((e)=>{ + token.value=e.token + GetUserInfo() + // return false; + // try{ + // if(token.value.length>18){ + // let base64Str=decodeURIComponent(token.value) + // token.value=atob(base64Str) + // } + // }catch(e){ + // console.error('解码失败', e) + // uni.$lu.toast("解码失败"+e) + // return false + // } + + - if(id_number.value){ - uni.navigateTo({ - url: `/pages/main/list/list?hospital=1&id_number=${id_number.value}` - }) - }else{ - uni.$lu.toast("身份证不能为空") - } })