You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
payment/app/Service/UserService.php

168 lines
6.5 KiB
PHTML

1 year ago
<?php
declare(strict_types=1);
namespace App\Service;
use App\Exception\BusinessException;
use App\Helper\Baofu\Baofu;
use App\Model\BankCard;
use App\Model\RequestLog;
use App\Model\User;
use App\Request\BindCardRequest;
use App\Request\RegisterRequest;
use App\Request\UnBindCardRequest;
class UserService extends AbstractService
{
public function register($params, $token) {
$req = new RegisterRequest($params);
$app = $req->getApp();
$reqData = $req->getData();
$user = User::where('app_id', $app->app_id)->where('user_id', $reqData['userId'])->first();
if ($user) {
throw new BusinessException('用户已存在');
}
$memberId = $this->generateMemberId($app->app_id, $reqData['userId']);
$baofu = new Baofu();
$url = $baofu->register($memberId, $token);
return $url;
}
public function rsyncUser($memberId, $appId, $userId) {
$baofu = new Baofu();
$userInfo = $baofu->queryCustomerInfo($memberId);
return $this->saveUser($userInfo, $appId, $userId);
}
public function saveUser($userInfo, $appId, $userId) {
$user = User::where('app_id', $appId)->where('member_id', $userInfo['loginNo'])->first();
if ($user) {
$user->contract_no = $userInfo['contractNo'];
$user->realname_flag = $userInfo['realNameFlag'] ?: 'Y';
$user->bind_card_flag = $userInfo['bindCardFlag'];
$user->set_pwd_flag = $userInfo['setPwdFlag'];
$user->status = $userInfo['operatorStatus'] ?: $userInfo['status'];
$user->email = $userInfo['email'] ?: '';
$user->mobile = $userInfo['mobile'] ?: '';
$user->real_name = $userInfo['customerName'] ?: '';
$user->certificate_no = $userInfo['certificateNo'] ?: '';
$user->user_type = $userInfo['customerType'];
$user->apply_no = $userInfo['applyNo'] ?: '';
} else {
$user = new User();
$user->user_id = $userId;
$user->member_id = $this->generateMemberId($appId , $userId);
$user->contract_no = $userInfo['contractNo'];
$user->realname_flag = $userInfo['realNameFlag'] ?: 'Y';
$user->bind_card_flag = $userInfo['bindCardFlag'];
$user->set_pwd_flag = $userInfo['setPwdFlag'];
$user->status = $userInfo['operatorStatus'] ?: $userInfo['status'];
$user->email = $userInfo['email'] ?: '';
$user->mobile = $userInfo['mobile'] ?: '';
$user->real_name = $userInfo['customerName'] ?: '';
$user->certificate_no = $userInfo['certificateNo'] ?: '';
$user->user_type = $userInfo['customerType'];
$user->apply_no = $userInfo['applyNo'] ?: '';
}
$user->save();
return $user;
}
public function rsyncBankCards($memberId) {
$user = User::where('member_id', $memberId)->first();
$baofu = new Baofu();
$bankCards = $baofu->findBindBankCards([
'loginNo' => $memberId,
'contractNo' => $user->contract_no,
]);
foreach ($bankCards as $bankCard) {
$this->saveBankCard($bankCard, $user->app_id, $user->user_id);
}
}
public function saveBankCard($data, $appId, $userId) {
$bankCard = BankCard::where('app_id', $appId)
->where('member_id', $data['loginNo'])
->where('agreement_no', $data['agreementNo'])
->first();
if ($bankCard) {
$bankCard->agreement_no = $data['agreementNo'];
$bankCard->card_user_name = $data['cardUserName'];
$bankCard->last_card_no = $data['lastCardNo'];
$bankCard->bank_mobile = $data['bankMobile'] ?: '';
$bankCard->bank_code = $data['bankCode'];
$bankCard->bank_name = $data['bankName'];
$bankCard->card_type = $data['cardType'];
$bankCard->cnaps_code = $data['cnapsCode'] ?: '';
$bankCard->public_flag = $data['publicFlag'];
$bankCard->repayment_date = $data['repaymentDate'] ?: '';
$bankCard->main_flag = $data['mainFlag'];
$bankCard->status = $data['status'];
$bankCard->ava_flag = $data['avaFlag'] ?: '';
$bankCard->remark = $data['remark'] ?: '';
} else {
$bankCard = new BankCard();
$bankCard->user_id = $userId;
$bankCard->member_id = $this->generateMemberId($appId , $userId);
$bankCard->agreement_no = $data['agreementNo'];
$bankCard->card_user_name = $data['cardUserName'];
$bankCard->last_card_no = $data['lastCardNo'];
$bankCard->bank_mobile = $data['bankMobile'] ?: '';
$bankCard->bank_code = $data['bankCode'];
$bankCard->bank_name = $data['bankName'];
$bankCard->card_type = $data['cardType'];
$bankCard->cnaps_code = $data['cnapsCode'] ?: '';
$bankCard->public_flag = $data['publicFlag'];
$bankCard->repayment_date = $data['repaymentDate'] ?: '';
$bankCard->main_flag = $data['mainFlag'];
$bankCard->status = $data['status'];
$bankCard->ava_flag = $data['avaFlag'] ?: '';
$bankCard->remark = $data['remark'] ?: '';
}
$bankCard->save();
return $bankCard;
}
public function generateMemberId($appId, $userId) {
return md5($appId . '-' . $userId);
}
public function bindCard($params, $token) {
$req = new BindCardRequest($params);
$app = $req->getApp();
$reqData = $req->getData();
$user = User::where('app_id', $app->app_id)->where('user_id', $reqData['userId'])->first();
if (empty($user)) {
throw new BusinessException('用户不存在');
}
$baofu = new Baofu();
$url = $baofu->cardBind($user->member_id, $token);
return $url;
}
public function unbindCard($params) {
$req = new UnBindCardRequest($params);
$app = $req->getApp();
$reqData = $req->getData();
$user = User::where('app_id', $app->app_id)->where('user_id', $reqData['userId'])->first();
if (empty($user)) {
throw new BusinessException('用户不存在');
}
$baofu = new Baofu();
$result = $baofu->cardUnbind($user->member_id, $reqData['agreementNo']);
if ($result && $result != 'false') {
$this->rsyncBankCards($user->member_id) ;
return true;
}
return false;
}
}