webman_duanju/app/admin/controller/VipDetails.php

128 lines
3.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use app\admin\model\SysCaptcha;
use app\common\library\DatabaseRoute;
use think\facade\Db;
class VipDetails extends Backend
{
protected array $noNeedPermission = ['selectVipDetailsList'];
public function sendVip()
{
$data = $this->request->post();
$userId = $data['userId'] ?? null;
$num = $data['num'] ?? null;
$userVip = \app\admin\model\User::selectUserVipByUserId($userId);
// 获取当前时间戳
$currentTime = time();
// 如果用户已有 VIP 记录
if ($userVip) {
// 如果当前是 VIP 会员(假设 is_vip=2 表示 VIP
if ($userVip['is_vip'] == 2) {
// 从现有结束时间开始叠加(转换为时间戳)
$endTime = strtotime($userVip['end_time']);
} else {
// 非 VIP 从当前时间开始
$endTime = $currentTime;
}
} else {
// 新建 VIP 记录
$userVip = [
'user_id' => $userId,
'create_time' => date('Y-m-d H:i:s', $currentTime)
];
$endTime = $currentTime;
}
// 设置 VIP 类型和状态
$userVip['vip_type'] = 1;
$userVip['is_vip'] = 2;
// 计算新的结束时间(增加指定天数,每天 86400 秒)
$endTime += $num * 86400;
$userVip['end_time'] = date('Y-m-d H:i:s', $endTime);
$db = Db::connect(get_master_connect_name())->name('user_vip');
// 保存或更新记录
if (isset($userVip['vip_id'])) {
// 更新记录
$db->where('vip_id', $userVip['vip_id'])
->update($userVip);
} else {
// 插入新记录
$db->insert($userVip);
}
$this->success();
}
public function selectVipDetailsList()
{
$get = $this->request->get();
$page = $get['page'] ?? null;
$limit = $get['limit'] ?? null;
$db = Db::connect(get_slave_connect_name())->name('vip_details');
$count = $db->count();
$data = $db->limit(page($page, $limit), $limit)->select()->toArray();
$this->n_success(['data' => [
'currPage' => $get['page'],
'pageSize' => $get['limit'],
'list' => array_map(function ($item) {
$item['id'] = (string)$item['id'];
return $item;
}, $data),
'totalCount' => $count,
'totalPage' => ceil($count / $get['limit']),
]]);
}
public function insertVipDetails()
{
$post = $this->request->post();
$data['vip_name_type'] = $post['vipNameType'] ?? null;
$data['money'] = $post['money'] ?? null;
$data['pay_diamond'] = $post['payDiamond'] ?? null;
$db = Db::connect(get_master_connect_name());
$res = $db->name('vip_details')->insert($data);
if($res) {
$this->success('成功');
}else {
$this->error('失败');
}
}
public function updateVipDetails()
{
$post = $this->request->post();
$id = $post['id'] ?? null;
$data['vip_name_type'] = $post['vipNameType'] ?? null;
$data['money'] = $post['money'] ?? null;
$data['pay_diamond'] = $post['payDiamond'] ?? null;
$db = Db::connect(get_master_connect_name());
$res = $db->name('vip_details')->where(['id' => $id])->update($data);
$this->success('成功');
}
public function deleteVipDetails()
{
$post = $this->request->param();
if(empty($post['id'])) {
$this->error('参数不完整');
}
$id = $post['id'];
$db = Db::connect(get_master_connect_name());
$res = $db->name('vip_details')->where(['id' => $id])->delete();
$this->success('成功');
}
}