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

212 lines
8.7 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]);
}
1 year ago
public function updatePassword(RequestInterface $request)
{
$userInfo = $this->checkUser($request);
$oldPassword = $request->input('oldPassword');
$password = $request->input('password');
$account = Account::where('id', $userInfo['userId'])->first();
if (md5($oldPassword . $account->salt) != $account->password) {
throw new BusinessException('旧密码错误');
}
$account->password = md5($password . $account->salt);
$account->save();
return $this->success();
}
1 year ago
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']);
1 year ago
$requestLog = $this->createRequestLog(['userId' => $userId, 'returnUrl' => 'http://124.223.222.61:9701/account.html']);
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']);
1 year ago
$bankCard = BankCard::where('id', $request->input('id', 0))->first();
if (!$bankCard || $bankCard->user_id != $userId) {
throw new BusinessException('银行卡不存在');
}
$requestLog = $this->createRequestLog(['userId' => $userId, 'agreementNo' => $bankCard->agreement_no]);
1 year ago
$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']);
1 year ago
$requestLog = $this->createRequestLog(['userId' => $userId, 'returnUrl' => 'http://124.223.222.61:9701/account.html']);
1 year ago
$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']);
1 year ago
$requestLog = $this->createRequestLog(['userId' => $userId, 'returnUrl' => 'http://124.223.222.61:9701/account.html']);
1 year ago
$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
$outOrderNo = StringHelper::generateOrderNo(StringHelper::ORDER_NO_TYPE_ACCOUNT_PAY);
1 year ago
$requestLog = $this->createRequestLog([
1 year ago
'userId' => $userId,
1 year ago
'goodsName' => '充值',
'agreementNo' => $bankCard->agreement_no,
1 year ago
'notifyUrl' => '',
'returnUrl' => 'http://124.223.222.61:9701/account.html',
1 year ago
'amount' => intval($request->input('amount') * 100),
1 year ago
'outOrderNo' => $outOrderNo,
1 year ago
'validDate' => date('Y-m-d', time() + 12*3600),
'marketInfo' => [
'amount' => 0,
'remark' => 'test',
],
'splitInfoList' => [
1 year ago
],
'isAccountPay' => true
1 year ago
]);
$acsNo = $this->paymentService->transferPay($requestLog->getData(), $requestLog->app, $requestLog->request_token);
1 year ago
return $this->success(['acsNo' => $acsNo, 'outOrderNo' => $outOrderNo]);
1 year ago
}
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']);
1 year ago
$bankCard = BankCard::where('user_id', $userId)->first(['id', '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
public function companyRegister(RequestInterface $request)
{
$userInfo = $this->checkUser($request);
$userId = $this->resetUserId($userInfo['userId']);
$requestLog = $this->createRequestLog(['userId' => $userId, 'email' => $request->input('email'), 'returnUrl' => 'http://124.223.222.61:9701/account.html']);
$url = $this->userService->companyRegister($requestLog->getData(), $requestLog->app, $requestLog->request_token);
return $this->success(['url' => $url]);
}
1 year ago
}