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

237 lines
9.0 KiB
PHTML

1 year ago
<?php
declare(strict_types=1);
namespace App\Service;
use App\Exception\BusinessException;
use App\Helper\Baofu\Baofu;
1 year ago
use App\Model\App;
1 year ago
use App\Model\BankCard;
use App\Model\User;
class UserService extends AbstractService
{
1 year ago
public function companyRegister(array $data, App $app, string $token)
{
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
if ($user) {
throw new BusinessException('用户已存在');
}
$emial = $data['email'];
$memberId = $this->generateMemberId($app->app_id, $data['userId']);
$baofu = new Baofu();
$url = $baofu->apply($memberId, $emial, $token);
return $url;
}
1 year ago
1 year ago
public function register(array $data, App $app, string $token)
{
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
1 year ago
if ($user) {
throw new BusinessException('用户已存在');
}
1 year ago
$memberId = $this->generateMemberId($app->app_id, $data['userId']);
1 year ago
$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) {
1 year ago
$memberId = $userInfo['loginNo'] ?? $userInfo['loginMobile'];
$user = User::where('app_id', $appId)->where('member_id', $memberId)->first();
1 year ago
if ($user) {
$user->contract_no = $userInfo['contractNo'];
1 year ago
$user->realname_flag = $userInfo['realNameFlag'] ?? 'Y';
1 year ago
$user->bind_card_flag = $userInfo['bindCardFlag'];
$user->set_pwd_flag = $userInfo['setPwdFlag'];
1 year ago
$user->status = $userInfo['operatorStatus'] ?? $userInfo['status'];
$user->email = $userInfo['email'] ?? '';
$user->mobile = $userInfo['mobile'] ?? '';
$user->real_name = $userInfo['customerName'] ?? '';
$user->certificate_no = $userInfo['certificateNo'] ?? '';
1 year ago
$user->user_type = $userInfo['customerType'];
1 year ago
$user->apply_no = $userInfo['applyNo'] ?? '';
1 year ago
} else {
$user = new User();
$user->user_id = $userId;
1 year ago
$user->app_id = $appId;
$user->member_id = $memberId;
1 year ago
$user->contract_no = $userInfo['contractNo'];
1 year ago
$user->realname_flag = $userInfo['realNameFlag'] ?? 'Y';
1 year ago
$user->bind_card_flag = $userInfo['bindCardFlag'];
$user->set_pwd_flag = $userInfo['setPwdFlag'];
$user->status = $userInfo['operatorStatus'] ?: $userInfo['status'];
1 year ago
$user->email = $userInfo['email'] ?? '';
$user->mobile = $userInfo['mobile'] ?? '';
$user->real_name = $userInfo['customerName'] ?? '';
$user->certificate_no = $userInfo['certificateNo'] ?? '';
1 year ago
$user->user_type = $userInfo['customerType'];
1 year ago
$user->apply_no = $userInfo['applyNo'] ?? '';
1 year ago
}
$user->save();
return $user;
}
public function rsyncBankCards($memberId) {
$user = User::where('member_id', $memberId)->first();
1 year ago
if (empty($user)) {
throw new BusinessException('用户不存在');
}
1 year ago
$baofu = new Baofu();
$bankCards = $baofu->findBindBankCards([
'loginNo' => $memberId,
'contractNo' => $user->contract_no,
]);
1 year ago
$oldAgreementNos = BankCard::where('member_id', $memberId)->get()->pluck('agreement_no')->toArray();
$agreementNos = array_column($bankCards, 'agreementNo');
$deletedAgreementNos = array_diff($oldAgreementNos, $agreementNos);
BankCard::where('member_id', $memberId)->whereIn('agreement_no', $deletedAgreementNos)->delete();
1 year ago
foreach ($bankCards as $bankCard) {
1 year ago
$this->saveBankCard($bankCard, $user);
1 year ago
}
}
1 year ago
public function getUserBankCards($userId, App $app)
{
$bankCards = BankCard::where('app_id', $app->app_id)->where('user_id', $userId)->get();
$records = [];
foreach ($bankCards as $bankCard) {
$records[] = [
'agreementNo' => $bankCard->agreement_no,
'cardUserName' => $bankCard->card_user_name,
'lastCardNo' => $bankCard->last_card_no,
'bankMobile' => $bankCard->bank_mobile,
'bankCode' => $bankCard->bank_code,
'bankName' => $bankCard->bank_name,
'cardType' => $bankCard->card_type,
'cnapsCode' => $bankCard->cnaps_code,
'publicFlag' => $bankCard->public_flag,
'repaymentDate' => $bankCard->repayment_date,
'mainFlag' => $bankCard->main_flag,
'status' => $bankCard->status,
'avaFlag' => $bankCard->ava_flag,
'remark' => $bankCard->remark,
];
}
return $records;
}
1 year ago
public function saveBankCard(array $data, User $user)
{
$appId = $user->app_id;
$userId = $user->user_id;
1 year ago
$bankCard = BankCard::where('app_id', $appId)
1 year ago
->where('member_id', $user->member_id)
1 year ago
->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();
1 year ago
$bankCard->app_id = $appId;
1 year ago
$bankCard->user_id = $userId;
1 year ago
$bankCard->member_id = $user->member_id;
1 year ago
$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);
}
1 year ago
public function bindCard(array $data, App $app, string $token)
{
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
1 year ago
if (empty($user)) {
throw new BusinessException('用户不存在');
}
$baofu = new Baofu();
$url = $baofu->cardBind($user->member_id, $token);
return $url;
}
1 year ago
public function unbindCard(array $data, App $app) {
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
1 year ago
if (empty($user)) {
throw new BusinessException('用户不存在');
}
$baofu = new Baofu();
1 year ago
$result = $baofu->cardUnbind($user->member_id, $data['agreementNo']);
1 year ago
if ($result) {
1 year ago
$this->rsyncBankCards($user->member_id) ;
return true;
}
return false;
}
1 year ago
public function pwdForget(array $data, App $app, $token) {
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
if (empty($user)) {
throw new BusinessException('用户不存在');
}
$baofu = new Baofu();
return $baofu->pwdForget($user->member_id, $token);
}
1 year ago
public function pwdModify(array $data, App $app, $token)
{
1 year ago
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
if (empty($user)) {
throw new BusinessException('用户不存在');
}
$baofu = new Baofu();
return $baofu->pwdModify($user->member_id, $token);
}
1 year ago
public function getBalance($memberId)
{
$baofu = new Baofu();
return $baofu->getBalance($memberId);
}
1 year ago
}