新版数智员工

This commit is contained in:
wong
2026-03-24 10:39:16 +08:00
parent 1853934d85
commit 7fb61b77a4
123 changed files with 38491 additions and 1018 deletions

View File

@@ -2,253 +2,372 @@
namespace app\store\controller;
use app\common\controller\Api;
use app\store\model\FlowPackageModel;
use app\store\model\UserFlowPackageModel;
use app\store\model\FlowPackageOrderModel;
use think\facade\Config;
use app\store\model\UserFlowPackageModel;
use think\facade\Log;
/**
* 流量套餐控制器
* 流量套餐控制器 - V2版本
* 门店端流量采购功能
*/
class FlowPackageController extends Api
class FlowPackageController extends BaseController
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
/**
* 获取流量套餐列表
* GET /v2/store/flow-packages
*
* @return \think\Response
* @return \think\response\Json
*/
public function getList()
{
$params = $this->request->param();
// 查询条件
$where = [];
// 只获取未删除的数据
$where[] = ['isDel', '=', 0];
// 套餐模型
$model = new FlowPackageModel();
// 查询数据
$list = $model->where($where)
->field('id, name, tag, originalPrice, price, monthlyFlow, duration, privileges')
->order('sort', 'asc')
->select();
// 格式化返回数据,添加计算字段
$result = [];
foreach ($list as $item) {
$result[] = [
'id' => $item['id'],
'name' => $item['name'],
'tag' => $item['tag'],
'originalPrice' => $item['originalPrice'],
'price' => $item['price'],
'monthlyFlow' => $item['monthlyFlow'],
'duration' => $item['duration'],
'discount' => $item->discount,
'totalFlow' => $item->totalFlow,
'privileges' => $item['privileges'],
try {
// 查询条件
$where = [
['isDel', '=', 0],
['status', '=', 1], // 只获取启用的套餐
['companyId', '=', $this->userInfo['companyId']]
];
// 查询数据包含公司ID和创建用户ID
$list = FlowPackageModel::where($where)
->field('id, name, tag, originalPrice, price, monthlyFlow, duration, privileges, companyId, userId, createTime')
->order('sort', 'asc')
->select();
// 格式化返回数据,添加计算字段
$result = [];
foreach ($list as $item) {
$result[] = [
'id' => $item['id'],
'name' => $item['name'],
'tag' => $item['tag'],
'originalPrice' => $item['originalPrice'],
'price' => $item['price'],
'monthlyFlow' => $item['monthlyFlow'],
'duration' => $item['duration'],
'discount' => $item->discount,
'totalFlow' => $item->totalFlow,
'privileges' => $item['privileges'],
'createTime' => !empty($item['createTime']) ? date('Y-m-d H:i:s', $item['createTime']) : '', // 创建时间
];
}
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $result
]);
} catch (\Exception $e) {
Log::error('获取流量套餐列表失败: ' . $e->getMessage());
return json(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]);
}
return successJson($result, '获取成功');
}
/**
* 获取流量套餐详情
* GET /v2/store/flow-packages/:id
*
* @param int $id 套餐ID
* @return \think\Response
* @return \think\response\Json
*/
public function detail($id)
{
if (empty($id)) {
return errorJson('参数错误');
try {
if (empty($id)) {
return json(['code' => 400, 'msg' => '参数错误']);
}
// 查询数据
$info = FlowPackageModel::where('id', $id)
->where('isDel', 0)
->find();
if (empty($info)) {
return json(['code' => 404, 'msg' => '套餐不存在']);
}
// 格式化返回数据,添加计算字段
$result = [
'id' => $info['id'],
'name' => $info['name'],
'tag' => $info['tag'],
'originalPrice' => $info['originalPrice'],
'price' => $info['price'],
'monthlyFlow' => $info['monthlyFlow'],
'duration' => $info['duration'],
'discount' => $info->discount,
'totalFlow' => $info->totalFlow,
'privileges' => $info['privileges'],
'companyId' => $info['companyId'] ?? 0, // 公司ID
'userId' => $info['userId'] ?? 0, // 创建用户ID
'createTime' => !empty($info['createTime']) ? date('Y-m-d H:i:s', $info['createTime']) : '', // 创建时间
];
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $result
]);
} catch (\Exception $e) {
Log::error('获取流量套餐详情失败: ' . $e->getMessage());
return json(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]);
}
// 套餐模型
$model = new FlowPackageModel();
// 查询数据
$info = $model->where('id', $id)->where('isDel', 0)->find();
if (empty($info)) {
return errorJson('套餐不存在');
}
// 格式化返回数据,添加计算字段
$result = [
'id' => $info['id'],
'name' => $info['name'],
'tag' => $info['tag'],
'originalPrice' => $info['originalPrice'],
'price' => $info['price'],
'monthlyFlow' => $info['monthlyFlow'],
'duration' => $info['duration'],
'discount' => $info->discount,
'totalFlow' => $info->totalFlow,
'privileges' => $info['privileges'],
];
return successJson($result, '获取成功');
}
/**
* 展示用户流量套餐使用情况
* 获取剩余流量
* GET /v2/store/flow-packages/remaining-flow
*
* @return \think\Response
* @return \think\response\Json
*/
public function remainingFlow()
{
$params = $this->request->param();
$userInfo = request()->userInfo;
// 获取用户ID通常应该从会话或令牌中获取
$userId = $userInfo['id'];
if (empty($userId)) {
return errorJson('请先登录');
}
// 获取用户当前有效的流量套餐
$userPackage = UserFlowPackageModel::getUserActivePackage($userId);
try {
// 从认证中间件获取用户信息
$userInfo = $this->request->userInfo ?? [];
$userId = $userInfo['id'] ?? 0;
if (empty($userPackage)) {
return errorJson('您没有有效的流量套餐');
if (empty($userId)) {
return json(['code' => 401, 'msg' => '请先登录']);
}
// 获取用户当前有效的流量套餐
$userPackage = UserFlowPackageModel::getUserActivePackage($userId);
if (empty($userPackage)) {
return json(['code' => 404, 'msg' => '您没有有效的流量套餐']);
}
// 获取套餐详情
$packageId = $userPackage['packageId'];
$flowPackage = FlowPackageModel::where('id', $packageId)
->where('isDel', 0)
->find();
if (empty($flowPackage)) {
return json(['code' => 404, 'msg' => '套餐信息不存在']);
}
// 计算剩余流量
$totalFlow = intval($userPackage['totalFlow'] ?? $flowPackage->totalFlow ?? 0); // 总流量
$usedFlow = intval($userPackage['usedFlow'] ?? 0); // 已使用流量
$remainingFlow = $totalFlow - $usedFlow; // 剩余流量
$remainingFlow = $remainingFlow > 0 ? $remainingFlow : 0; // 确保不为负数
// 计算剩余天数
$now = time();
$expireTime = intval($userPackage['expireTime'] ?? 0);
$duration = intval($userPackage['duration'] ?? 0);
if ($expireTime <= 0) {
return json(['code' => 400, 'msg' => '套餐数据异常,到期时间无效']);
}
$remainingDays = ceil(($expireTime - $now) / 86400); // 向上取整,剩余天数
$remainingDays = $remainingDays > 0 ? $remainingDays : 0; // 确保不为负数
// 剩余百分比
$flowPercentage = $totalFlow > 0 ? round(($remainingFlow / $totalFlow) * 100, 1) : 0;
$timePercentage = $duration > 0 ?
round(($remainingDays / ($duration * 30)) * 100, 1) : 0;
// 返回数据
$result = [
'packageName' => $flowPackage['name'], // 套餐名称
'remainingFlow' => $remainingFlow, // 剩余流量(人)
'totalFlow' => $totalFlow, // 总流量(人)
'flowPercentage' => $flowPercentage, // 剩余流量百分比
'remainingDays' => $remainingDays, // 剩余天数
'totalDays' => $duration * 30, // 总天数(按30天/月计算)
'timePercentage' => $timePercentage, // 剩余时间百分比
'expireTime' => date('Y-m-d', $expireTime), // 到期日期
'startTime' => date('Y-m-d', $userPackage['startTime']), // 开始日期
];
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $result
]);
} catch (\Exception $e) {
Log::error('获取剩余流量失败: ' . $e->getMessage());
return json(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]);
}
// 获取套餐详情
$packageId = $userPackage['packageId'];
$flowPackage = FlowPackageModel::where('id', $packageId)->where('isDel', 0)->find();
if (empty($flowPackage)) {
return errorJson('套餐信息不存在');
}
// 计算剩余流量
$totalFlow = $userPackage['totalFlow'] ?? $flowPackage->totalFlow; // 总流量
$usedFlow = $userPackage['usedFlow'] ?? 0; // 已使用流量
$remainingFlow = $totalFlow - $usedFlow; // 剩余流量
$remainingFlow = $remainingFlow > 0 ? $remainingFlow : 0; // 确保不为负数
// 计算剩余天数
$now = time();
$expireTime = $userPackage['expireTime'];
$remainingDays = ceil(($expireTime - $now) / 86400); // 向上取整,剩余天数
$remainingDays = $remainingDays > 0 ? $remainingDays : 0; // 确保不为负数
// 剩余百分比
$flowPercentage = $totalFlow > 0 ? round(($remainingFlow / $totalFlow) * 100, 1) : 0;
$timePercentage = $userPackage['duration'] > 0 ?
round(($remainingDays / ($userPackage['duration'] * 30)) * 100, 1) : 0;
// 返回数据
$result = [
'packageName' => $flowPackage['name'], // 套餐名称
'remainingFlow' => $remainingFlow, // 剩余流量(人)
'totalFlow' => $totalFlow, // 总流量(人)
'flowPercentage' => $flowPercentage, // 剩余流量百分比
'remainingDays' => $remainingDays, // 剩余天数
'totalDays' => $userPackage['duration'] * 30, // 总天数(按30天/月计算)
'timePercentage' => $timePercentage, // 剩余时间百分比
'expireTime' => date('Y-m-d', $expireTime), // 到期日期
'startTime' => date('Y-m-d', $userPackage['startTime']), // 开始日期
];
return successJson($result, '获取成功');
}
/**
* 创建流量采购订单
* POST /v2/store/flow-packages/order
*
* @return \think\Response
* @return \think\response\Json
*/
public function createOrder()
{
$params = $this->request->param();
$userInfo = request()->userInfo;
// 获取用户ID通常应该从会话或令牌中获取
$userId = $userInfo['id'];
if (empty($userId)) {
return errorJson('请先登录');
}
// 获取套餐ID
$packageId = isset($params['packageId']) ? intval($params['packageId']) : 0;
if (empty($packageId)) {
return errorJson('请选择套餐');
}
// 查询套餐信息
$flowPackage = FlowPackageModel::where('id', $packageId)->where('isDel', 0)->find();
if (empty($flowPackage)) {
return errorJson('套餐不存在');
}
// 获取支付方式(可选)
$payType = isset($params['payType']) ? $params['payType'] : 'wechat';
// 套餐价格和信息
$amount = floatval($flowPackage['price']);
$packageName = $flowPackage['name'];
$duration = intval($flowPackage['duration']);
$remark = isset($params['remark']) ? $params['remark'] : '';
// 处理金额为0的特殊情况
if ($amount <= 0) {
// 金额为0无需支付直接创建订单并设置为已支付
$order = FlowPackageOrderModel::createOrder(
$userId,
$packageId,
$packageName,
0,
$duration,
'nopay',
$remark
);
try {
// 从BaseController获取用户信息
$userId = $this->userInfo['id'] ?? 0;
$companyId = $this->userInfo['companyId'] ?? 0;
if (!$order) {
return errorJson('订单创建失败');
if (empty($userId)) {
return json(['code' => 401, 'msg' => '请先登录']);
}
// 创建用户流量套餐记录
$this->createUserFlowPackage($userId, $packageId, $order['id']);
// 返回成功信息
return successJson(['orderNo' => $order['orderNo'],'status' => 'success'], '购买成功');
} else {
// 创建正常需要支付的订单
$order = FlowPackageOrderModel::createOrder(
$userId,
$packageId,
$packageName,
$amount,
$duration,
$payType,
$remark
);
if (!$order) {
return errorJson('订单创建失败');
if (empty($companyId)) {
return json(['code' => 400, 'msg' => '公司信息不存在']);
}
// 返回订单信息,前端需要跳转到支付页面
return successJson([
'orderNo' => $order['orderNo'],
'amount' => $amount,
'payType' => $payType,
'status' => 'pending'
], '订单创建成功');
// 获取套餐ID
$packageId = $this->request->param('packageId', 0);
if (empty($packageId)) {
return json(['code' => 400, 'msg' => '请选择套餐']);
}
// 查询套餐信息
$flowPackage = FlowPackageModel::where('id', $packageId)
->where('isDel', 0)
->find();
if (empty($flowPackage)) {
return json(['code' => 404, 'msg' => '套餐不存在']);
}
// 获取支付方式(可选)
$payType = $this->request->param('payType', 'wechat');
// 套餐价格和信息
$amount = floatval($flowPackage['price']);
$packageName = $flowPackage['name'];
$duration = intval($flowPackage['duration']);
$remark = $this->request->param('remark', '');
// 处理金额为0的特殊情况
if ($amount <= 0) {
// 金额为0无需支付直接创建订单并设置为已支付
$order = FlowPackageOrderModel::createOrder(
$userId,
$companyId,
$packageId,
$packageName,
0,
$duration,
'nopay',
$remark
);
if (!$order) {
return json(['code' => 500, 'msg' => '订单创建失败']);
}
// 创建用户流量套餐记录
$this->createUserFlowPackage($userId, $packageId, $order['id']);
// 返回成功信息
return json([
'code' => 200,
'msg' => '购买成功',
'data' => [
'orderNo' => $order['orderNo'],
'status' => 'success'
]
]);
} else {
// 创建正常需要支付的订单
$order = FlowPackageOrderModel::createOrder(
$userId,
$companyId,
$packageId,
$packageName,
$amount,
$duration,
$payType,
$remark
);
if (!$order) {
return json(['code' => 500, 'msg' => '订单创建失败']);
}
// 返回订单信息,前端需要跳转到支付页面
return json([
'code' => 200,
'msg' => '订单创建成功',
'data' => [
'orderNo' => $order['orderNo'],
'amount' => $amount,
'payType' => $payType,
'status' => 'pending'
]
]);
}
} catch (\Exception $e) {
Log::error('创建订单失败: ' . $e->getMessage());
return json(['code' => 500, 'msg' => '创建订单失败:' . $e->getMessage()]);
}
}
/**
* 获取订单列表
* GET /v2/store/flow-packages/orders
*
* @return \think\response\Json
*/
public function getOrderList()
{
try {
// 从认证中间件获取用户信息
$page = intval($this->request->param('page', 1));
$limit = intval($this->request->param('limit', 10));
$status = $this->request->param('status', ''); // 订单状态筛选
// 确保分页参数有效
$page = $page > 0 ? $page : 1;
$limit = $limit > 0 ? $limit : 10;
$where = [
['userId', '=', $this->userInfo['id']],
['companyId', '=', $this->userInfo['companyId']], // 按公司ID查询
['isDel', '=', 0]
];
if ($status !== '' && $status !== null) {
$status = intval($status);
$where[] = ['status', '=', $status];
}
$query = FlowPackageOrderModel::where($where)
->order('id', 'desc');
$list = $query->page($page, $limit)->select();
$total = $query->count();
// 格式化数据
foreach ($list as &$item) {
$item['createTime'] = !empty($item['createTime']) && is_numeric($item['createTime']) ? date('Y-m-d H:i:s', intval($item['createTime'])) : '';
$item['payTime'] = !empty($item['payTime']) && is_numeric($item['payTime']) ? date('Y-m-d H:i:s', intval($item['payTime'])) : '';
}
unset($item);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $list,
'total' => $total,
'page' => $page,
'limit' => $limit
]
]);
} catch (\Exception $e) {
Log::error('获取订单列表失败: ' . $e->getMessage());
return json(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]);
}
}
@@ -263,7 +382,9 @@ class FlowPackageController extends Api
private function createUserFlowPackage($userId, $packageId, $orderId)
{
// 获取套餐信息
$flowPackage = FlowPackageModel::where('id', $packageId)->where('isDel', 0)->find();
$flowPackage = FlowPackageModel::where('id', $packageId)
->where('isDel', 0)
->find();
if (empty($flowPackage)) {
return false;
@@ -273,23 +394,21 @@ class FlowPackageController extends Api
$now = time();
$expireTime = $now + (intval($flowPackage['duration']) * 30 * 86400);
// 用户流量套餐数据
// 用户流量套餐数据注意ck_user_flow_package表没有packageName和monthlyFlow字段
$data = [
'userId' => $userId,
'packageId' => $packageId,
'orderId' => $orderId,
'packageName' => $flowPackage['name'],
'monthlyFlow' => $flowPackage['monthlyFlow'],
'duration' => $flowPackage['duration'],
'totalFlow' => $flowPackage->totalFlow, // 使用计算属性获取总流量
'usedFlow' => 0,
'startTime' => $now,
'expireTime' => $expireTime,
'status' => 1, // 1:有效 0:无效
'isDel' => 0
];
// 创建用户流量套餐记录
return UserFlowPackageModel::create($data) ? true : false;
}
}