|
|
<?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 App\Model\User;
|
|
|
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' => '202308070000001',
|
|
|
'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('账号必须为6~20位');
|
|
|
}
|
|
|
if (strlen($password) < 6 || strlen($password) > 20) {
|
|
|
throw new BusinessException('密码必须为6~20位');
|
|
|
}
|
|
|
$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->id]);
|
|
|
$token = StringHelper::getRandomString(16);
|
|
|
Redis::set(RedisKey::getUserTokenKey($token), $userInfo);
|
|
|
Redis::expire(RedisKey::getUserTokenKey($token), 3600);
|
|
|
return $this->success(['token' => $token]);
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
}
|
|
|
|
|
|
public function openAccount(RequestInterface $request)
|
|
|
{
|
|
|
$userInfo = $this->checkUser($request);
|
|
|
$userId = $this->resetUserId($userInfo['userId']);
|
|
|
$requestLog = $this->createRequestLog(['userId' => $userId, 'returnUrl' => 'http://124.223.222.61:9701/account.html']);
|
|
|
$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);
|
|
|
$userId = $this->resetUserId($userInfo['userId']);
|
|
|
$requestLog = $this->createRequestLog(['userId' => $userId, 'returnUrl' => 'http://124.223.222.61:9701/account.html']);
|
|
|
$url = $this->userService->bindCard($requestLog->getData(), $requestLog->app, $requestLog->request_token);
|
|
|
return $this->success(['url' => $url]);
|
|
|
}
|
|
|
|
|
|
public function unbindCard(RequestInterface $request)
|
|
|
{
|
|
|
$userInfo = $this->checkUser($request);
|
|
|
$userId = $this->resetUserId($userInfo['userId']);
|
|
|
$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]);
|
|
|
$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, 'returnUrl' => 'http://124.223.222.61:9701/account.html']);
|
|
|
$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, 'returnUrl' => 'http://124.223.222.61:9701/account.html']);
|
|
|
$url = $this->userService->pwdModify($requestLog->getData(), $requestLog->app, $requestLog->request_token);
|
|
|
return $this->success(['url' => $url]);
|
|
|
}
|
|
|
|
|
|
public function transferPay(RequestInterface $request)
|
|
|
{
|
|
|
$userInfo = $this->checkUser($request);
|
|
|
$userId = $this->resetUserId($userInfo['userId']);
|
|
|
$bankCard = BankCard::where('user_id', $userId)->first();
|
|
|
$outOrderNo = StringHelper::generateOrderNo(StringHelper::ORDER_NO_TYPE_ACCOUNT_PAY);
|
|
|
$requestLog = $this->createRequestLog([
|
|
|
'userId' => $userId,
|
|
|
'goodsName' => '充值',
|
|
|
'agreementNo' => $bankCard->agreement_no,
|
|
|
'notifyUrl' => '',
|
|
|
'returnUrl' => 'http://124.223.222.61:9701/account.html',
|
|
|
'amount' => intval($request->input('amount') * 100),
|
|
|
'outOrderNo' => $outOrderNo,
|
|
|
'validDate' => date('Y-m-d', time() + 12*3600),
|
|
|
'marketInfo' => [
|
|
|
'amount' => 0,
|
|
|
'remark' => 'test',
|
|
|
],
|
|
|
'splitInfoList' => [
|
|
|
],
|
|
|
'isAccountPay' => true
|
|
|
]);
|
|
|
$acsNo = $this->paymentService->transferPay($requestLog->getData(), $requestLog->app, $requestLog->request_token);
|
|
|
return $this->success(['acsNo' => $acsNo, 'outOrderNo' => $outOrderNo]);
|
|
|
}
|
|
|
|
|
|
public function getUserInfo(RequestInterface $request)
|
|
|
{
|
|
|
$userInfo = $this->checkUser($request);
|
|
|
$account = Account::where('id', $userInfo['userId'])->first();
|
|
|
$userId = $this->resetUserId($userInfo['userId']);
|
|
|
$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(['id', 'bank_name', 'card_user_name']);
|
|
|
return $this->success(['username' => $account->username, 'userInfo' => $user, 'bankCard' => $bankCard]);
|
|
|
}
|
|
|
|
|
|
private function resetUserId($accountId) {
|
|
|
if ($accountId == 2) {
|
|
|
return 'ELF1990';
|
|
|
}
|
|
|
return 'ACT_' . $accountId;
|
|
|
}
|
|
|
|
|
|
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]);
|
|
|
}
|
|
|
}
|