57 lines
1.9 KiB
PHP
57 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller\user;
|
|
|
|
use app\api\controller\BaseApiController;
|
|
use app\common\model\user\User;
|
|
use app\common\model\user\UserBalance;
|
|
use app\common\validate\user\UserBalanceValidate;
|
|
use think\facade\Db;
|
|
use think\facade\Log;
|
|
use think\response\Json;
|
|
|
|
class UserBalanceController extends BaseApiController
|
|
{
|
|
//用户余额变更记录
|
|
public function addBalanceRecord(): Json
|
|
{
|
|
// 获取参数
|
|
$params = (new UserBalanceValidate())->post()->goCheck('add');
|
|
//获取用户信息
|
|
$user = User::field('id,total_balance')->where('id',$this->userId)->findOrEmpty();
|
|
if($user->isEmpty()){
|
|
return $this->fail('未找到用户信息');
|
|
}
|
|
if($params['type'] == 0) {
|
|
$total_balance = $user['total_balance'] - $params['amount'];
|
|
}else{
|
|
$total_balance = $user['total_balance'] + $params['amount'];
|
|
}
|
|
// 添加数据
|
|
Db::startTrans();
|
|
try {
|
|
UserBalance::create([
|
|
'user_id' => $this->userId,
|
|
'record_id' => $params['record_id'],
|
|
'record_table' => $params['record_table'],
|
|
'amount' => $params['amount'],
|
|
'total_amount' => $total_balance,
|
|
'type' => $params['type'],
|
|
'pay_type' => $params['pay_type'],
|
|
'mark' => $params['mark'],
|
|
'appid' => $this->request->header('appid'),
|
|
'create_time' => time(),
|
|
]);
|
|
User::update([
|
|
'id' => $this->userId,
|
|
'total_balance' => $total_balance,
|
|
]);
|
|
Db::commit();
|
|
return $this->success('添加成功');
|
|
}catch (\Exception $e) {
|
|
Db::rollback();
|
|
Log::error($e->getMessage());
|
|
return $this->fail($e->getMessage());
|
|
}
|
|
}
|
|
} |