You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

374 lines
13 KiB

<?php
namespace app\service\user;
use app\model\Admin;
use app\model\Cert;
use app\model\User;
use app\service\BaseService;
use fast\Http;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\exception\HttpResponseException;
class UserService extends BaseService
{
protected $reset_pass = "123456";
public function __construct()
{
parent::__construct();
$this->http = new Http();
}
public function checkUser($user_isli = null){
$user = User::where('user_isli', $user_isli)->find();
if($user){
// return true;
}
$path = env('app.real_url') . "/users/usermgt/v1/user-info/{$user_isli}";
$sign = parent::createSign("user_real");
$headers = array(
CURLOPT_HTTPHEADER => array(
"api_token:{$sign}",
// "Content-Type: application/json"
)
);
//todo 请求根据账号ID查询用户信息接口 接口3.1
$result = Http::get($path, [], $headers);
if($result['code'] != 200){
// throw new \think\Exception($result['msg'], 400);
throw new HttpResponseException(_error($result['msg'], 400));
}
if(!isset($result['data'])){
// throw new \think\Exception("未获取到用户信息", 400);
throw new HttpResponseException(_error('未获取到用户信息', 400));
}
$result = json_decode($result['data'], true);
if($result['resultCode'] != "00000000"){
// throw new \think\Exception($result['resultMsg'], 400);
throw new HttpResponseException(_error($result['resultMsg'], 400));
}
if(!isset($result['data'])){
// throw new \think\Exception("未查询到该用户信息", 400);
throw new HttpResponseException(_error('未查询到该用户信息', 400));
}
$userinfo = $result['data'];
if(!isset($userinfo['legalsCellPhone'])){
// var_dump($userinfo);die;
}
$insert = [];
if($userinfo['userType'] == "机构"){
$insert = [
"agency_type" => 2,
"attest_status" => $userinfo['state'] + 1,
"user_isli" => $userinfo['islicode'],
"username" => $userinfo['name'],
"institution_code" => $userinfo['uscc'],
"legals_name" => $userinfo['legalsName'],
"legals_type" => $userinfo['legalsType'],
"legals_idnum" => $userinfo['legalsIdnum'],
"legals_phone" => (isset($userinfo['legalsCellPhone']) ? $userinfo['legalsCellPhone'] : ''),
"bank_account" => $userinfo['publicAccount'],
"bank_card_type" => $userinfo['bankCardType'],
"bank_name" => $userinfo['bankName'],
"bank_type" => $userinfo['bankType'],
"bank_username" => $userinfo['bankAccountName'],
"bank_userid" => $userinfo['certIdnum'],
"bank_userphone" => $userinfo['bankCellPhone'],
"bank_address" => $userinfo['bankAddress'],
];
}else{
$insert = [
"agency_type" => 1,
"attest_status" => $userinfo['state'] + 1,
"user_isli" => $userinfo['islicode'],
"username" => $userinfo['name'],
"cert_type" => $userinfo['certType'],
"idNumber" => $userinfo['idNumber'],
"cell_phone" => $userinfo['cellPhone'],
"bank_account" => $userinfo['publicAccount'],
"bank_card_type" => $userinfo['bankCardType'],
"bank_name" => $userinfo['bankName'],
"bank_username" => $userinfo['bankAccountName'],
"bank_userid" => $userinfo['certIdnum'],
"bank_userphone" => $userinfo['bankCellPhone'],
"bank_address" => $userinfo['bankAddress'],
];
}
$insert['registertime'] = date('Y-m-d H:i:s');
$insert['attesttime'] = date('Y-m-d H:i:s');
(new User())->insert($insert);
return true;
}
public function getApiUser($user_isli = null){
if(empty($user_isli)){
return [];
}
$path = env('app.real_url') . "/users/usermgt/v1/user-info/{$user_isli}";
$sign = parent::createSign("user_real");
$headers = array(
CURLOPT_HTTPHEADER => array(
"api_token:{$sign}",
// "Content-Type: application/json"
)
);
//todo 请求根据账号ID查询用户信息接口 接口3.1
$result = Http::get($path, [], $headers);
if($result['code'] != 200){
throw new \think\Exception($result['msg'], 400);
}
$result = json_decode($result['data'], true);
if($result['resultCode'] != "00000000"){
throw new \think\Exception($result['resultMsg'], 400);
}
if(!isset($result['data'])){
return [];
}
$userinfo = $result['data'];
if(!isset($userinfo['legalsCellPhone'])){
// var_dump($userinfo);die;
}
return $userinfo;
// $insert = [];
// if($userinfo['userType'] == "机构"){
// $res = [
// "agency_type" => 2,
// "attest_status" => $userinfo['state'] + 1,
// "user_isli" => $userinfo['islicode'],
// "username" => $userinfo['name'],
// "institution_code" => $userinfo['uscc'],
// "legals_name" => $userinfo['legalsName'],
// "legals_type" => $userinfo['legalsType'],
// "legals_idnum" => $userinfo['legalsIdnum'],
// "legals_phone" => (isset($userinfo['legalsCellPhone']) ? $userinfo['legalsCellPhone'] : ''),
// "bank_account" => $userinfo['publicAccount'],
// "bank_card_type" => $userinfo['bankCardType'],
// "bank_name" => $userinfo['bankName'],
// "bank_type" => $userinfo['bankType'],
// "bank_username" => $userinfo['bankAccountName'],
// "bank_userid" => $userinfo['certIdnum'],
// "bank_userphone" => $userinfo['bankCellPhone'],
// "bank_address" => $userinfo['bankAddress'],
// ];
// }else{
// $res = [
// "agency_type" => 1,
// "attest_status" => $userinfo['state'] + 1,
// "user_isli" => $userinfo['islicode'],
// "username" => $userinfo['Name'],
// "cert_type" => $userinfo['certType'],
// "idNumber" => $userinfo['idNumber'],
// "cell_phone" => $userinfo['cellPhone'],
// "bank_account" => $userinfo['publicAccount'],
// "bank_card_type" => $userinfo['bankCardType'],
// "bank_name" => $userinfo['bankName'],
// "bank_username" => $userinfo['bankAccountName'],
// "bank_userid" => $userinfo['certIdnum'],
// "bank_userphone" => $userinfo['bankCellPhone'],
// "bank_address" => $userinfo['bankAddress'],
// ];
// }
}
public function getUserInfo($user_isli){
$userinfo = $this->getApiUser($user_isli);
if($userinfo['userType'] == "机构"){
$res = [
"agency_type" => 2,
"attest_status" => $userinfo['state'] + 1,
"user_isli" => $userinfo['islicode'],
"username" => $userinfo['name'],
"institution_code" => $userinfo['uscc'],
"legals_name" => $userinfo['legalsName'],
"legals_type" => $userinfo['legalsType'],
"legals_idnum" => $userinfo['legalsIdnum'],
"legals_phone" => (isset($userinfo['legalsCellPhone']) ? $userinfo['legalsCellPhone'] : ''),
"bank_account" => $userinfo['publicAccount'],
"bank_card_type" => $userinfo['bankCardType'],
"bank_name" => $userinfo['bankName'],
"bank_type" => $userinfo['bankType'],
"bank_username" => $userinfo['bankAccountName'],
"bank_userid" => $userinfo['certIdnum'],
"bank_userphone" => $userinfo['bankCellPhone'],
"bank_address" => $userinfo['bankAddress'],
];
}else{
$res = [
"agency_type" => 1,
"attest_status" => $userinfo['state'] + 1,
"user_isli" => $userinfo['islicode'],
"username" => $userinfo['name'],
"cert_type" => $userinfo['certType'],
"idNumber" => $userinfo['idNumber'],
"cell_phone" => $userinfo['cellPhone'],
"bank_account" => $userinfo['publicAccount'],
"bank_card_type" => $userinfo['bankCardType'],
"bank_name" => $userinfo['bankName'],
"bank_username" => $userinfo['bankAccountName'],
"bank_userid" => $userinfo['certIdnum'],
"bank_userphone" => $userinfo['bankCellPhone'],
"bank_address" => $userinfo['bankAddress'],
];
}
return $res;
}
public function getUserFind($user_isli){
$where = [];
$where['user_isli'] = $user_isli;
$result = User::where($where)->find();
$result->toArray();
return $result;
}
public function list($user_id, $username, $page, $limit){
$where = [];
$where['is_deleted'] = 0;
if(!empty($user_id)){
$where['id'] = $user_id;
}
if(!empty($username)){
$where['username'] = $username;
}
$search = $this->buildSearch(['username'], $where);
$result = (new Admin())->list($search, $where, $limit);
return $result;
}
public function add($name, $username, $password, $status){
if(Admin::where('username', $username)->where('is_deleted', 0)->value('id')){
throw new \think\Exception('该账号已存在,请勿重复添加', 400);
}
$password_hash = password_hash($password, PASSWORD_DEFAULT);
$userinfo = [
'name' => $name,
'username' => $username,
'password' => $password_hash,
'status' => $status,
'role_id' => 0
];
$result = (new Admin())->save($userinfo);
return $result;
}
public function update($user_id, $name, $status){
$result = Admin::where('id', $user_id)->where('is_deleted', 0)->find();
if(!$result){
throw new \think\Exception('该账号不存在', 400);
}
$result->name = $name;
$result->status = $status;
return $result->save();
}
public function forbidden($user_id){
$result = Admin::where('id', $user_id)->where('is_deleted', 0)->find();
if(!$result){
throw new \think\Exception('该账号不存在', 400);
}
$status = $result->status == 0 ? 1 : 0;
$result->status = $status;
$result->save();
return $status;
}
public function resetPass($user_id){
$result = Admin::where('id', $user_id)->where('is_deleted', 0)->find();
if(!$result){
throw new \think\Exception('该账号不存在', 400);
}
if($result->status == 1){
throw new \think\Exception('该用户已禁用', 400);
}
$password_hash = password_hash($this->reset_pass, PASSWORD_DEFAULT);
$result->password = $password_hash;
return $result->save();
}
public function remove($user_id){
$result = Admin::where('id', $user_id)->where('is_deleted', 0)->find();
if(!$result){
throw new \think\Exception('该账号不存在', 400);
}
$result->is_deleted = 1;
return $result->save();
}
public function updateMyPass($password){
$result = Admin::where('id', $this->admin_id)->where('is_deleted', 0)->find();
if(!$result){
throw new \think\Exception('该账号不存在', 400);
}
if($result->status == 1){
throw new \think\Exception('该用户已禁用', 400);
}
$password_hash = password_hash($password, PASSWORD_DEFAULT);
$result->password = $password_hash;
return $result->save();
}
public function getUser(){
$result = (new Admin())->select()->toArray();
return $result;
}
public function getUserData(){
if(!isset($_SERVER['HTTP_TOKEN'])){
throw new \think\Exception('缺少TOKEN', 401);
}
if(!isset($_SERVER['HTTP_URL'])){
throw new \think\Exception('缺少模块名', 401);
}
$token = $_SERVER['HTTP_TOKEN'];
$nick = $_SERVER['HTTP_URL'];
$cert = Cert::where('status', 0)->where('type', "entrust")->find();
if(!$cert){
throw new \think\Exception('缺少证书', 400);
}
$key = $cert->token;
$jwt = new JWT();
$data = $jwt::decode($token, new Key($key, 'HS512'));
$user = json_decode( json_encode($data), true);
$loginService = new LoginService();
$auth = $loginService->checkUser($user['accountId'], $nick);
$user['auth'] = $auth;
return $user;
}
}