From ad8477d3ee82a3fffd5de4cd60a237c9ee6b1fb7 Mon Sep 17 00:00:00 2001
From: quanwei <419654421@qq.com>
Date: Wed, 04 Mar 2026 14:27:05 +0800
Subject: [PATCH] 后台添加供需求发布
---
admin/app/shop/model/plus/release/SupplyProject.php | 238 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 236 insertions(+), 2 deletions(-)
diff --git a/admin/app/shop/model/plus/release/SupplyProject.php b/admin/app/shop/model/plus/release/SupplyProject.php
index e3916ff..3f5c7f5 100644
--- a/admin/app/shop/model/plus/release/SupplyProject.php
+++ b/admin/app/shop/model/plus/release/SupplyProject.php
@@ -5,6 +5,11 @@
use app\common\model\plus\release\Project as ProjectModel;
use app\common\model\plus\release\ReleaseProjectImage as ReleaseProjectImageModel;
use app\common\model\plus\release\ReleaseProjectTag as ReleaseProjectTagModel;
+use app\common\model\plus\release\ReleaseCategory as ReleaseCategoryModel;
+use app\common\model\plus\release\Tag as TagModel;
+use app\common\model\plus\release\SupplyUser as SupplyUserModel;
+use app\common\model\plus\release\Setting as SettingModel;
+use app\common\model\user\PointsLog as PointsLogModel;
/**
* 模型
@@ -21,9 +26,9 @@
if (!empty($data['name'])) {
$model = $model->where('name', 'like', '%' . $data['name'] . '%');
}
- $list= $model->with('category')->where('project_type', '=', 1)
+ $list= $model->with(['category','user'])->where('project_type', '=', 1)
->where('is_delete', '=', 0)
- ->order(['sort' => 'asc', 'create_time' => 'asc'])
+ ->order(['sort' => 'asc', 'create_time' => 'desc'])
->paginate($data);
foreach($list as &$val){
@@ -36,6 +41,235 @@
}
/**
+ * 获取默认数据(分类、标签、设置)
+ */
+ public function getDefaultData()
+ {
+ $setting = SettingModel::getAll();
+ $settlement = $setting['settlement']['values'] ?? [];
+ return [
+ 'category_list' => ReleaseCategoryModel::getALL(),
+ 'tag_list' => TagModel::getALL(),
+ 'supply_price' => $settlement['supply_price'] ?? 0,
+ ];
+ }
+
+ /**
+ * 后台发布供应
+ * @param array $data 表单数据
+ * @param int $user_id 发布用户ID
+ * @param int $pay_points 是否支付连盟币(0不支付,1支付)
+ * @return bool
+ */
+ public function addByAdmin($data, $user_id, $pay_points = 0)
+ {
+ // 验证必填项
+ if (empty($data['name'])) {
+ $this->error = '请输入标题';
+ return false;
+ }
+ if (empty($data['category_id'])) {
+ $this->error = '请选择分类';
+ return false;
+ }
+ if (empty($data['price'])) {
+ $this->error = '请输入您的价格';
+ return false;
+ }
+ if (empty($data['content'])) {
+ $this->error = '请输入详细描述';
+ return false;
+ }
+
+ // 验证供应方用户是否存在
+ $supply_user = SupplyUserModel::detail($user_id);
+ if (!$supply_user || $supply_user['is_delete'] == 1) {
+ $this->error = '供应方用户不存在';
+ return false;
+ }
+
+ // 如果需要支付连盟币,从设置中获取金额并检查余额
+ $pay_price = 0;
+ if ($pay_points > 0) {
+ $setting = SettingModel::getAll();
+ $settlement = $setting['settlement']['values'] ?? [];
+ $pay_price = $settlement['supply_price'] ?? 0;
+
+ $main_user = \app\common\model\user\User::detail($user_id);
+ if ($main_user['points'] < $pay_price) {
+ $this->error = '用户连盟币不足';
+ return false;
+ }
+ }
+
+ $save_data = [
+ 'user_id' => $user_id,
+ 'name' => $data['name'],
+ 'category_id' => $data['category_id'],
+ 'price' => $data['price'],
+ 'content' => $data['content'],
+ 'product_content' => $data['product_content'] ?? '',
+ 'product_case' => $data['product_case'] ?? '',
+ 'detail' => $data['detail'] ?? '',
+ 'project_type' => 1, // 供应类型
+ 'is_show' => $data['is_show'] ?? 1,
+ 'status' => 1, // 后台发布自动通过审核
+ 'app_id' => self::$app_id,
+ ];
+
+ if (!empty($data['finish_time']) && $data['finish_time'] != '请选择日期') {
+ $save_data['finish_time'] = strtotime($data['finish_time']);
+ } else {
+ $save_data['finish_time'] = 0;
+ }
+
+ return $this->transaction(function () use ($data, $save_data, $pay_points, $user_id) {
+ // 记录内容
+ $this->save($save_data);
+ // 记录图片
+ $this->saveAllImages($this['project_id'], $data);
+ // 记录标签
+ $this->saveTag($this['project_id'], $data);
+
+ // 如果需要支付连盟币
+ if ($pay_points > 0) {
+ // 从设置中获取支付金额
+ $setting = SettingModel::getAll();
+ $settlement = $setting['settlement']['values'] ?? [];
+ $pay_price = $settlement['supply_price'] ?? 0;
+
+ // 减少主用户表的连盟币
+ \app\common\model\user\User::where('user_id', '=', $user_id)->dec('points', $pay_price)->update();
+ PointsLogModel::add([
+ 'user_id' => $user_id,
+ 'value' => -$pay_price,
+ 'describe' => "后台发布供应消耗连盟币",
+ 'app_id' => self::$app_id,
+ ]);
+ }
+
+ return $this['project_id'];
+ });
+ }
+
+ /**
+ * 后台编辑供应
+ * @param int $project_id 项目ID
+ * @param array $data 表单数据
+ * @return bool
+ */
+ public function editByAdmin($project_id, $data)
+ {
+ // 验证必填项
+ if (empty($data['name'])) {
+ $this->error = '请输入标题';
+ return false;
+ }
+ if (empty($data['category_id'])) {
+ $this->error = '请选择分类';
+ return false;
+ }
+ if (empty($data['price'])) {
+ $this->error = '请输入您的价格';
+ return false;
+ }
+ if (empty($data['content'])) {
+ $this->error = '请输入详细描述';
+ return false;
+ }
+
+ $model = self::detail($project_id);
+ if (!$model) {
+ $this->error = '项目不存在';
+ return false;
+ }
+
+ $save_data = [
+ 'name' => $data['name'],
+ 'category_id' => $data['category_id'],
+ 'price' => $data['price'],
+ 'content' => $data['content'],
+ 'product_content' => $data['product_content'] ?? '',
+ 'product_case' => $data['product_case'] ?? '',
+ 'detail' => $data['detail'] ?? '',
+ 'is_show' => $data['is_show'] ?? 1,
+ ];
+
+ if (!empty($data['finish_time']) && $data['finish_time'] != '请选择日期') {
+ $save_data['finish_time'] = strtotime($data['finish_time']);
+ } else {
+ $save_data['finish_time'] = 0;
+ }
+
+ return $model->transaction(function () use ($model, $project_id, $data, $save_data) {
+ // 更新内容
+ $model->save($save_data);
+ // 更新图片
+ $model->saveAllImages($project_id, $data);
+ // 更新标签
+ $model->saveTag($project_id, $data);
+
+ return true;
+ });
+ }
+
+ /**
+ * 记录图片
+ */
+ private function saveAllImages($id, $formData)
+ {
+ (new ReleaseProjectImageModel())->where("project_id", "=", $id)->delete();
+ // 生成图片数据
+ if (!empty($formData['image_list']) && is_array($formData['image_list'])) {
+ $imageData = [];
+ foreach ($formData['image_list'] as $imageItem) {
+ // 处理图片ID:可能是对象数组或纯ID
+ $image_id = 0;
+ if (is_array($imageItem)) {
+ $image_id = !empty($imageItem['image_id']) ? $imageItem['image_id'] : ($imageItem['file_id'] ?? 0);
+ } elseif (is_numeric($imageItem)) {
+ $image_id = $imageItem;
+ }
+ if ($image_id > 0) {
+ $imageData[] = [
+ 'project_id' => $id,
+ 'image_id' => $image_id,
+ 'app_id' => self::$app_id
+ ];
+ }
+ }
+ if (!empty($imageData)) {
+ $model = new ReleaseProjectImageModel;
+ return $model->saveAll($imageData);
+ }
+ }
+ return true;
+ }
+
+ /**
+ * 记录标签
+ */
+ private function saveTag($id, $formData)
+ {
+ (new ReleaseProjectTagModel())->where("project_id", "=", $id)->delete();
+
+ // 生成数据
+ if (!empty($formData['tag_list'])) {
+ $tagData = [];
+ foreach ($formData['tag_list'] as $val) {
+ $tagData[] = [
+ 'project_id' => $id,
+ 'tag_id' => $val,
+ 'app_id' => self::$app_id
+ ];
+ }
+ $model = new ReleaseProjectTagModel;
+ return !empty($tagData) && $model->saveAll($tagData);
+ }
+ return true;
+ }
+
+ /**
* 审核
*/
public function submit($param)
--
Gitblit v1.9.2