Files
code-bang/backend/application/api/controller/Account.php
T

162 lines
5.9 KiB
PHP
Raw Normal View History

2026-09-17 16:25:31 +08:00
<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\Accounts;
use app\common\service\AccountLogService;
use app\common\service\AccountService;
use app\common\service\AccountWithdrawService;
use app\common\service\CategoryService;
use app\common\service\DemandBiddingService;
use app\common\service\DemandService;
2026-10-10 14:10:14 +08:00
use app\common\service\EasyWeChatService;
2026-09-17 16:25:31 +08:00
use app\common\service\GoodsService;
use app\common\model\Category;
use app\common\service\OrderService;
2026-10-10 14:10:14 +08:00
use app\common\service\TransferAuthorizationService;
2026-09-17 16:25:31 +08:00
use app\common\service\UserService;
2026-10-10 14:10:14 +08:00
use EasyWeChat\Pay\Message as PayMessage;
2026-09-17 16:25:31 +08:00
use fast\Tree;
use think\Exception;
use think\exception\HttpResponseException;
2026-10-10 14:10:14 +08:00
use think\Log;
2026-09-17 16:25:31 +08:00
use think\Validate;
class Account extends Api
{
2026-10-10 14:10:14 +08:00
protected $noNeedLogin = ['transferAuthorizationNotify'];
2026-09-17 16:25:31 +08:00
protected $noNeedRight = ['*'];
public function logs()
{
try {
$user_id = $this->auth->id ?? -1;
$page = $this->request->get('page', 1);
$page_size = $this->request->get('page_size', 10);
$tab = $this->request->get('tab', 'all');
$accountType = $this->request->get('accountType', '');
$where = [
'user_id' => $user_id,
];
if ($tab != 'all') {
$where['inc_or_exp'] = $tab == 'income' ? 1 : 2;
}
if ($accountType) {
$where['account_type'] = $accountType == 'amount' ? ['in', ['amount', 'freeze']] : $accountType;
}
$list = AccountLogService::getList($where, $page_size, 'id desc')->toArray();
$this->success('', $list);
} catch (\Exception $e) {
if ($e instanceof HttpResponseException) {
throw $e;
}
$this->error($e->getMessage());
}
}
public function info()
{
$user_id = $this->auth->id ?? -1;
$data = AccountService::getOne(['user_id' => $user_id]);
$this->success('success', $data);
}
public function applyWithdraw()
{
try {
$user_id = $this->auth->id ?? -1;
$amount = $this->request->post('amount', 0);
$name = $this->request->post('name', '');
2026-09-23 17:52:36 +08:00
// 验证参数(单笔 0.1~200 元,与 AccountWithdrawService 中的限制保持一致)
2026-09-17 16:25:31 +08:00
$error = $this->validate($this->request->post(), [
'name' => 'require',
2026-09-23 17:52:36 +08:00
'amount' => 'require|number|float|between:'
. AccountWithdrawService::WITHDRAW_MIN_AMOUNT . ','
. AccountWithdrawService::WITHDRAW_MAX_AMOUNT,
2026-09-17 16:25:31 +08:00
], [
'name.require' => '微信真实姓名必填',
'amount.require' => '提现金额不正确',
'amount.number' => '提现金额必须为数字',
2026-09-23 17:52:36 +08:00
'amount.float' => '提现金额必须为数字',
'amount.between' => '单笔提现金额需在' . AccountWithdrawService::WITHDRAW_MIN_AMOUNT . '~'
. AccountWithdrawService::WITHDRAW_MAX_AMOUNT . '元之间',
2026-09-17 16:25:31 +08:00
]);
if (true !== $error) {
throw new \Exception($error);
}
AccountWithdrawService::applyWithdraw($user_id, $amount, $name);
$this->success('提现申请成功,1-3个工作日到账');
} catch (\Exception $e) {
if ($e instanceof HttpResponseException) {
throw $e;
}
$this->error($e->getMessage());
}
}
2026-10-10 14:10:14 +08:00
/**
* 免确认收款授权(发起/查询),供前端 wx.requestMerchantTransfer 拉起授权页
* @ApiMethod (POST)
*/
public function transferAuthorization()
{
try {
$user_id = $this->auth->id ?? 0;
if (!$user_id) {
throw new \Exception('请先登录');
}
$user = UserService::get($user_id);
if (!$user) {
throw new \Exception('用户不存在');
}
//授权详情展示账号名:优先微信实名(提现填写的姓名),其次昵称
$displayName = $user->username ?: ($user->nickname ?: '');
$res = TransferAuthorizationService::apply($user, $displayName);
$this->success('', $res);
} catch (\Exception $e) {
if ($e instanceof HttpResponseException) {
throw $e;
}
$this->error($e->getMessage());
}
}
/**
* 免确认收款授权结果回调(微信异步通知,已加入 noNeedLogin)
* @return void
*/
public function transferAuthorizationNotify()
{
header('Content-Type: application/json; charset=utf-8');
try {
// PSR-7 响应(SDK 已完成验签/解密,业务失败返回 500 由微信重试)
$response = (new EasyWeChatService())->handleTransferAuthorizationNotify(
function (PayMessage $message) {
TransferAuthorizationService::handleAuthorizationNotify($message->toArray());
}
);
http_response_code($response->getStatusCode());
foreach ($response->getHeaders() as $name => $values) {
header($name . ': ' . implode(', ', $values));
}
$body = $response->getBody()->getContents();
Log::info('免确认收款授权回调[应答] http_code=' . $response->getStatusCode() . ' body=' . $body);
echo $body;
exit;
} catch (\Throwable $e) {
Log::error('免确认收款授权回调[异常]:' . json_encode([
'msg' => $e->getMessage(),
'file' => $e->getFile() . ':' . $e->getLine(),
], JSON_UNESCAPED_UNICODE));
http_response_code(500);
echo json_encode(['code' => 'FAIL', 'message' => $e->getMessage()], JSON_UNESCAPED_UNICODE);
exit;
}
}
2026-09-17 16:25:31 +08:00
}