diff --git a/Server/.env b/Server/.env index 9a16c12e..19693b28 100644 --- a/Server/.env +++ b/Server/.env @@ -4,11 +4,11 @@ trace = true [database] type = mysql -hostname = 103.144.2.26 -database = yi_54iis_com -username = yi_54iis_com -password = c1RbMwrZCCyxF1bC -hostport = 3306 +hostname = 56b4c23f6853c.gz.cdb.myqcloud.com +database = cunkebao_v3 +username = root +password = Zhiqun1984 +hostport = 14413 prefix = tk_ [api] diff --git a/Server/application/api/config/route.php b/Server/application/api/config/route.php new file mode 100644 index 00000000..75855dcf --- /dev/null +++ b/Server/application/api/config/route.php @@ -0,0 +1,77 @@ +request->header('authorization', '')); + if (empty($authorization)) { + return errorJson('缺少授权信息'); + } + + try { + // 设置请求参数 + $isAll = $this->request->param('isAll', 'false'); + + // 设置请求头 + $headerData = ['client:system']; + $header = setHeader($headerData, $authorization, 'json'); + + // 发送请求获取部门列表 + $url = $this->baseUrl . 'api/Department/tenantDepartmentsForSelect?isAll=' . $isAll; + $result = requestCurl($url, [], 'GET', $header, 'json'); + + // 处理返回结果 + $response = handleApiResponse($result); + + + // 保存数据到数据库 + if (!empty($response)) { + foreach ($response as $item) { + $this->saveDepartment($item); + } + } + + return successJson($response, '获取部门列表成功'); + } catch (\Exception $e) { + return errorJson('获取部门列表失败:' . $e->getMessage()); + } + } + + /** + * 保存部门数据到数据库 + * @param array $item 部门数据 + */ + private function saveDepartment($item) + { + $data = [ + 'id' => isset($item['id']) ? $item['id'] : 0, + 'name' => isset($item['name']) ? $item['name'] : '', + 'memo' => isset($item['memo']) ? $item['memo'] : '', + ]; + + // 使用imei作为唯一性判断 + $department= CompanyModel::where('id', $item['id'])->find(); + + if ($department) { + $department->save($data); + } else { + CompanyModel::create($data); + } + } + + + + + /** * 保存账号数据到数据库 * @param array $item 账号数据 diff --git a/Server/application/api/controller/UserController.php b/Server/application/api/controller/UserController.php index 5fc6b159..af558031 100644 --- a/Server/application/api/controller/UserController.php +++ b/Server/application/api/controller/UserController.php @@ -150,18 +150,17 @@ class UserController extends BaseController } $headerData = ['client:' . self::CLIENT_TYPE]; - $header = setHeader($headerData, $authorization, 'plain'); + $header = setHeader($headerData, $authorization, 'json'); try { - $result = requestCurl($this->baseUrl . 'api/Account/self', [], 'GET', $header); + $result = requestCurl($this->baseUrl . 'api/Account/self', [], 'GET', $header,'json'); $response = handleApiResponse($result); - if (!empty($response['account'])) { $accountData = $response['account']; // 准备数据库字段映射,保持驼峰命名 $dbData = [ - 'accountId' => $accountData['id'], + 'tenantId' => $accountData['id'], 'realName' => $accountData['realName'], 'nickname' => $accountData['nickname'], 'memo' => $accountData['memo'], @@ -169,7 +168,7 @@ class UserController extends BaseController 'userName' => $accountData['userName'], 'secret' => $accountData['secret'], 'accountType' => $accountData['accountType'], - 'departmentId' => $accountData['departmentId'], + 'companyId' => $accountData['departmentId'], 'useGoogleSecretKey' => $accountData['useGoogleSecretKey'], 'hasVerifyGoogleSecret' => $accountData['hasVerifyGoogleSecret'], 'updateTime' => time() diff --git a/Server/application/api/model/CompanyAccountModel.php b/Server/application/api/model/CompanyAccountModel.php index 183e61bd..d482d19e 100644 --- a/Server/application/api/model/CompanyAccountModel.php +++ b/Server/application/api/model/CompanyAccountModel.php @@ -6,5 +6,6 @@ use think\Model; class CompanyAccountModel extends Model { - + // 设置表名 + protected $name = 'company_account'; } \ No newline at end of file diff --git a/Server/application/api/model/CompanyModel.php b/Server/application/api/model/CompanyModel.php new file mode 100644 index 00000000..ac112748 --- /dev/null +++ b/Server/application/api/model/CompanyModel.php @@ -0,0 +1,10 @@ +middleware(['jwt'])*/; \ No newline at end of file diff --git a/Server/application/store/controller/FlowPackageController.php b/Server/application/store/controller/FlowPackageController.php index e3d9f72c..fb7ea9d2 100644 --- a/Server/application/store/controller/FlowPackageController.php +++ b/Server/application/store/controller/FlowPackageController.php @@ -5,6 +5,7 @@ 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; /** @@ -108,7 +109,7 @@ class FlowPackageController extends Api $params = $this->request->param(); // 获取用户ID,通常应该从会话或令牌中获取 - $userId = isset($params['userId']) ? intval($params['userId']) : 0; + $userId = isset($params['userId']) ? intval($params['userId']) : 1; if (empty($userId)) { return errorJson('请先登录'); @@ -161,4 +162,132 @@ class FlowPackageController extends Api return successJson($result, '获取成功'); } + + /** + * 创建流量采购订单 + * + * @return \think\Response + */ + public function createOrder() + { + $params = $this->request->param(); + + // 获取用户ID,通常应该从会话或令牌中获取 + $userId = isset($params['userId']) ? intval($params['userId']) : 1; + + 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 + ); + + if (!$order) { + return errorJson('订单创建失败'); + } + + // 创建用户流量套餐记录 + $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('订单创建失败'); + } + + // 返回订单信息,前端需要跳转到支付页面 + return successJson([ + 'orderNo' => $order['orderNo'], + 'amount' => $amount, + 'payType' => $payType, + 'status' => 'pending' + ], '订单创建成功'); + } + } + + /** + * 创建用户流量套餐记录 + * + * @param int $userId 用户ID + * @param int $packageId 套餐ID + * @param int $orderId 订单ID + * @return bool + */ + private function createUserFlowPackage($userId, $packageId, $orderId) + { + // 获取套餐信息 + $flowPackage = FlowPackageModel::where('id', $packageId)->where('isDel', 0)->find(); + + if (empty($flowPackage)) { + return false; + } + + // 计算到期时间(当前时间 + 套餐时长(月) * 30天) + $now = time(); + $expireTime = $now + (intval($flowPackage['duration']) * 30 * 86400); + + // 用户流量套餐数据 + $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; + } } diff --git a/Server/application/store/model/FlowPackageOrderModel.php b/Server/application/store/model/FlowPackageOrderModel.php new file mode 100644 index 00000000..4bbe3787 --- /dev/null +++ b/Server/application/store/model/FlowPackageOrderModel.php @@ -0,0 +1,93 @@ + 'integer', + 'userId' => 'integer', + 'packageId' => 'integer', + 'amount' => 'float', + 'duration' => 'integer', + 'createTime' => 'timestamp', + 'updateTime' => 'timestamp', + 'payTime' => 'timestamp', + 'status' => 'integer', + 'payStatus' => 'integer', + 'isDel' => 'integer', + ]; + + /** + * 生成订单号 + * 规则:LL + 年月日时分秒 + 5位随机数 + * + * @return string + */ + public static function generateOrderNo() + { + $prefix = 'LL'; + $date = date('YmdHis'); + $random = mt_rand(10000, 99999); + + return $prefix . $date . $random; + } + + /** + * 创建订单 + * + * @param int $userId 用户ID + * @param int $packageId 套餐ID + * @param string $packageName 套餐名称 + * @param float $amount 订单金额 + * @param int $duration 购买时长(月) + * @param string $payType 支付类型 (wechat|alipay|nopay) + * @param string $remark 备注 + * @return array|false + */ + public static function createOrder($userId, $packageId, $packageName, $amount, $duration, $payType = 'wechat', $remark = '') + { + // 生成订单号 + $orderNo = self::generateOrderNo(); + + // 订单数据 + $data = [ + 'userId' => $userId, + 'packageId' => $packageId, + 'packageName' => $packageName, + 'orderNo' => $orderNo, + 'amount' => $amount, + 'duration' => $duration, + 'payType' => $payType, + 'createTime' => time(), + 'status' => 0, // 0:待支付 1:已完成 2:已取消 3:已退款 + 'payStatus' => $payType == 'nopay' ? 10 : 0, // 0:未支付 1:已支付 10:无需支付 + 'remark' => $remark, + 'isDel' => 0, + ]; + + // 创建订单 + $model = new self(); + $result = $model->save($data); + + if ($result) { + return $model->toArray(); + } else { + return false; + } + } +} \ No newline at end of file diff --git a/Server/database/flow_order.sql b/Server/database/flow_order.sql new file mode 100644 index 00000000..6b7ac147 --- /dev/null +++ b/Server/database/flow_order.sql @@ -0,0 +1,24 @@ +-- 创建流量订单表 +CREATE TABLE IF NOT EXISTS `tk_flow_order` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID', + `userId` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '用户ID', + `packageId` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '套餐ID', + `orderNo` varchar(32) NOT NULL DEFAULT '' COMMENT '订单号', + `price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '支付价格', + `originalPrice` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '原价', + `payType` varchar(20) NOT NULL DEFAULT '' COMMENT '支付类型(wxpay|alipay|nopay)', + `tradeNo` varchar(64) NOT NULL DEFAULT '' COMMENT '支付平台交易号', + `createTime` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间', + `updateTime` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间', + `payTime` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '支付时间', + `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态(0:待支付 1:已完成 2:已取消)', + `payStatus` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '支付状态(0:未支付 10:已支付)', + `memo` varchar(255) NOT NULL DEFAULT '' COMMENT '备注', + `isDel` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否删除(0:否 1:是)', + PRIMARY KEY (`id`), + UNIQUE KEY `idx_order_no` (`orderNo`), + KEY `idx_user_id` (`userId`), + KEY `idx_package_id` (`packageId`), + KEY `idx_create_time` (`createTime`), + KEY `idx_status` (`status`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='流量订单表'; \ No newline at end of file diff --git a/Server/route/route.php b/Server/route/route.php index 1885d66d..013d1c6d 100644 --- a/Server/route/route.php +++ b/Server/route/route.php @@ -11,6 +11,9 @@ use think\facade\Route; +// 加载Store模块路由配置 +include __DIR__ . '/../application/api/config/route.php'; + // 加载Common模块路由配置 include __DIR__ . '/../application/common/config/route.php';