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

141 lines
5.1 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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;
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 = [
'app_id' => '',
'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('账号或密码错误');
}
$userInfo = json_encode(['userId' => $account->account_id]);
$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);
$requestLog = $this->createRequestLog(['userId' => 'ACT_' . $userInfo['userId']]);
$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);
$requestLog = $this->createRequestLog(['userId' => 'ACT_' . $userInfo['userId']]);
$url = $this->userService->bindCard($requestLog->getData(), $requestLog->app, $requestLog->request_token);
return $this->success(['url' => $url]);
}
public function transferPay(RequestInterface $request)
{
$userInfo = $this->checkUser($request);
$bankCard = BankCard::where('user_id', 'ACT_' . $userInfo['userId'])->first();
$requestLog = $this->createRequestLog([
'userId' => 'ACT_' . $userInfo['userId'],
'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]);
}
}