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/Controller/Payment/AccountController.php

189 lines
7.3 KiB
PHTML

1 year ago
<?php
declare(strict_types=1);
namespace App\Controller\Payment;
use App\Exception\BusinessException;
use App\Helper\Redis;
use App\Helper\RedisKey;
use App\Helper\StringHelper;
use App\Model\Account;
use App\Model\BankCard;
1 year ago
use App\Model\User;
1 year ago
use Hyperf\HttpServer\Contract\RequestInterface;
use App\Service\PaymentService;
use App\Service\RequestService;
use App\Service\UserService;
class AccountController extends AbstractController
{
private RequestService $requestService;
private UserService $userService;
private PaymentService $paymentService;
public function __construct(RequestService $requestService, PaymentService $paymentService, UserService $userService)
{
$this->requestService = $requestService;
$this->userService = $userService;
$this->paymentService = $paymentService;
}
private function createRequestLog($data) {
$params = [
1 year ago
'app_id' => '202308070000001',
1 year ago
'data' => json_encode($data),
];
return $this->requestService->createRequestLog('', $params);
}
private function checkUser(RequestInterface $request) {
$token = $request->input('token');
if (empty($token)) {
throw new BusinessException('未登录');
}
$result = Redis::get(RedisKey::getUserTokenKey($token));
if (empty($result)) {
throw new BusinessException('未登录');
}
$userInfo = json_decode($result, true);
if (empty($userInfo)) {
throw new BusinessException('未登录');
}
return $userInfo;
}
public function register(RequestInterface $request)
{
$username = $request->input('username');
$password = $request->input('password');
if (strlen($username) < 6 || strlen($username) > 20) {
throw new BusinessException('账号必须为620位');
}
if (strlen($password) < 6 || strlen($password) > 20) {
throw new BusinessException('密码必须为620位');
}
$account = Account::where('username', $username)->first();
if (!empty($account)) {
throw new BusinessException('账号已存在');
}
$account = new Account();
$account->salt = StringHelper::getRandomString(6);
$account->username = $username;
$account->password = md5($password . $account->salt);
$account->save();
return $this->success();
}
public function login(RequestInterface $request)
{
$username = $request->input('username');
$password = $request->input('password');
$account = Account::where('username', $username)->first();
if (empty($account)) {
throw new BusinessException('账号或密码错误');
}
if (md5($password . $account->salt) != $account->password) {
throw new BusinessException('账号或密码错误');
}
1 year ago
$userInfo = json_encode(['userId' => $account->id]);
1 year ago
$token = StringHelper::getRandomString(16);
Redis::set(RedisKey::getUserTokenKey($token), $userInfo);
Redis::expire(RedisKey::getUserTokenKey($token), 3600);
return $this->success(['token' => $token]);
}
public function openAccount(RequestInterface $request)
{
$userInfo = $this->checkUser($request);
1 year ago
$userId = $this->resetUserId($userInfo['userId']);
$requestLog = $this->createRequestLog(['userId' => $userId, 'returnUrl' => 'http://124.223.222.61:9701/account.html']);
1 year ago
$url = $this->userService->register($requestLog->getData(), $requestLog->app, $requestLog->request_token);
return $this->success(['url' => $url]);
}
public function bindCard(RequestInterface $request)
{
$userInfo = $this->checkUser($request);
1 year ago
$userId = $this->resetUserId($userInfo['userId']);
$requestLog = $this->createRequestLog(['userId' => $userId]);
1 year ago
$url = $this->userService->bindCard($requestLog->getData(), $requestLog->app, $requestLog->request_token);
return $this->success(['url' => $url]);
}
1 year ago
public function unbindCard(RequestInterface $request)
{
$userInfo = $this->checkUser($request);
$userId = $this->resetUserId($userInfo['userId']);
$requestLog = $this->createRequestLog(['userId' => $userId]);
$result = $this->userService->unbindCard($requestLog->getData(), $requestLog->app);
return $this->success(['result' => $result]);
}
public function setPayPassword(RequestInterface $request)
{
$userInfo = $this->checkUser($request);
$userId = $this->resetUserId($userInfo['userId']);
$requestLog = $this->createRequestLog(['userId' => $userId]);
$url = $this->userService->pwdForget($requestLog->getData(), $requestLog->app, $requestLog->request_token);
return $this->success(['url' => $url]);
}
public function resetPayPassword(RequestInterface $request)
{
$userInfo = $this->checkUser($request);
$userId = $this->resetUserId($userInfo['userId']);
$requestLog = $this->createRequestLog(['userId' => $userId]);
$url = $this->userService->pwdModify($requestLog->getData(), $requestLog->app, $requestLog->request_token);
return $this->success(['url' => $url]);
}
1 year ago
public function transferPay(RequestInterface $request)
{
$userInfo = $this->checkUser($request);
1 year ago
$userId = $this->resetUserId($userInfo['userId']);
$bankCard = BankCard::where('user_id', $userId)->first();
1 year ago
$requestLog = $this->createRequestLog([
1 year ago
'userId' => $userId,
1 year ago
'goodsName' => '充值',
'agreementNo' => $bankCard->agreement_no,
'notifyUrl' => 'http://www.baidu.com',
'returnUrl' => 'http://www.baidu.com',
'amount' => intval($request->input('amount') * 100),
'outOrderNo' => time() . rand(1000, 9999),
'validDate' => date('Y-m-d', time() + 12*3600),
'marketInfo' => [
'amount' => 0,
'remark' => 'test',
],
'splitInfoList' => [
[
'splitUserId' => 'RLX1990',
'sellerFlag' => 1,
'splitAmount' => 100,
'subOutOrderNo' => time() . rand(1000, 9999),
]
]
]);
$acsNo = $this->paymentService->transferPay($requestLog->getData(), $requestLog->app, $requestLog->request_token);
return $this->success(['acsNo' => $acsNo]);
}
1 year ago
public function getUserInfo(RequestInterface $request)
{
$userInfo = $this->checkUser($request);
1 year ago
$account = Account::where('id', $userInfo['userId'])->first();
1 year ago
$userId = $this->resetUserId($userInfo['userId']);
1 year ago
$user = User::where('user_id', $userId)->first(['realname_flag', 'bind_card_flag', 'set_pwd_flag', 'mobile', 'real_name', 'certificate_no', 'user_type', 'apply_no', 'is_platform_account']);
$bankCard = BankCard::where('user_id', $userId)->first(['bank_name', 'card_user_name']);
1 year ago
return $this->success(['username' => $account->username, 'userInfo' => $user, 'bankCard' => $bankCard]);
1 year ago
}
1 year ago
private function resetUserId($accountId) {
if ($accountId == 2) {
return 'ELF1990';
}
return 'ACT_' . $accountId;
}
1 year ago
}