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