webman_duanju/app/admin/controller/Integral.php

83 lines
2.3 KiB
PHP

<?php
namespace app\admin\controller;
use app\admin\model\UserIntegral;
use app\common\controller\Backend;
use app\common\library\DatabaseRoute;
use ba\Exception;
use think\facade\Db;
class Integral extends Backend
{
protected array $noNeedPermission = ['selectByUserId', 'details'];
public function selectByUserId()
{
$get = $this->request->get();
if(empty($get['userId'])) {
$this->error('userId is not empty');
}
$data = Db::connect(get_slave_connect_name())->name('user_integral')->where(['user_id' => $get['userId']])->find();
$data = [
'user_id' => (string)$get['userId'],
'integral_num' => !empty($data['integral_num'])?$data['integral_num']:'0',
];
$this->n_success(['data' => convertToCamelCase($data)]);
}
public function details()
{
$get = $this->request->get();
if(empty($get['userId'])) {
$this->error('userId is not empty');
}
$this->n_success(['data' => UserIntegral::selectUserIntegralDetailsByUserId($get['page'], $get['limit'], $get['userId'])]);
}
public function updateUserIntegral()
{
$post = $this->request->post();
$userId = $post['userId'] ?? $this->error('参数不完整');
$type = $post['type'] ?? $this->error('参数不完整');
$integral = $post['integral'] ?? $this->error('参数不完整');
Db::startTrans();
try {
$db = Db::connect(get_master_connect_name());
if(UserIntegral::updateIntegral($type, $userId, $integral, $db)) {
// 记录积分变动明细
$details['classify'] = 2;
$details['content'] = $type == 1
? "系统增加积分:{$integral}"
: "系统减少积分:{$integral}";
$currentTime = time();
$details['create_time'] = date('Y-m-d H:i:s', $currentTime);
$details['num'] = $integral;
$details['type'] = $type;
$details['user_id'] = $userId;
$db->name('user_integral_details')->insert($details);
Db::commit();
}
$this->success();
}catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
}
}