feat 添加手机端添加数据接口

This commit is contained in:
lwh 2023-08-25 20:17:02 +08:00
parent cbda35e257
commit f3ebe436f5
11 changed files with 126 additions and 600 deletions

View File

@ -1,202 +0,0 @@
<?php
namespace app\admin\controller\News;
use app\BaseController;
use app\common\model\News\News as ModelNews;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use app\common\exception\Tool;
use think\facade\Db;
use think\facade\Env;
class News extends BaseController
{
/**
* 获取新闻列表
*/
public function getNewsList(Request $request, $isExport = false): array
{
$params = $request->param();
$con = [];
$con = Tool::getOptionalQuery(['news_type', '='], ['news_title', 'LIKE'],);
$query = ModelNews::where($con)
->field([
'news_id',
'news_guid',
'news_type',
'news_cover',
'news_title',
'news_intro',
'news_source',
'news_link',
'news_issue_date',
'news_views_num',
'news_sort',
'news_content'
])
->order('news_sort', 'asc');
return $isExport ? $query->select()->toArray() : msg("获取新闻列表成功!", $query);
}
/**
* 添加新闻
*/
public function addNews(Request $request): array
{
Db::startTrans();
Tool::adminLockTableWrite('news');
try {
$params = $request->param();
$this->validate($params, [
'news_type|新闻类型' => 'require',
'news_title|新闻标题' => 'require',
'news_cover|新闻封面' => 'require',
'news_sort|新闻排序' => 'require'
]);
$model = ModelNews::create($params, [
'news_type',
'news_title',
'news_cover',
'news_intro',
'news_source',
'news_link',
'news_issue_date',
'news_views_num',
'news_sort',
'news_content',
'news_guid',
'news_create_user_guid',
'news_update_user_guid'
]);
Db::commit();
Tool::unlockTable();
return msg('添加成功!');
} catch (\Throwable $th) {
Db::rollback();
Tool::unlockTable();
throw $th;
}
}
/**
* 编辑新闻
*/
public function editNews(Request $request): array
{
Db::startTrans();
Tool::adminLockTableWrite('news');
try {
$params = $request->param();
$this->validate($params, [
'news_type|新闻类型' => 'require',
'news_title|新闻标题' => 'require',
'news_cover|新闻封面' => 'require',
'news_sort|新闻排序' => 'require'
]);
$model = ModelNews::where('news_guid', $params['news_guid'])->find();
if (!$model) throwErrorMsg("该新闻不存在", 1);
$model->allowField([
'news_type',
'news_title',
'news_cover',
'news_intro',
'news_source',
'news_link',
'news_issue_date',
'news_views_num',
'news_sort',
'news_content',
'news_update_user_guid'
])->save($params);
Db::commit();
Tool::unlockTable();
return msg('编辑成功!');
} catch (\Throwable $th) {
Db::rollback();
Tool::unlockTable();
throw $th;
}
}
/**
* 删除新闻
*/
public function deleteNews(Request $request): array
{
Db::startTrans();
Tool::adminLockTableWrite('news');
try {
$params = $request->param();
$this->validate($params, [
'news_guid' => 'require',
]);
$news = ModelNews::where([
'news_guid' => explode(',', $params['news_guid'])
])->select();
$news->delete();
Db::commit();
Tool::unlockTable();
return msg('删除成功!');
} catch (\Throwable $th) {
Db::rollback();
Tool::unlockTable();
throw $th;
}
}
/**
* 导出Excel
*/
public function exportExcel(Request $request): void
{
ModelNews::exportExcel(self::getNewsList($request, true));
}
/**
* 下载导入模板
*/
public function downloadTemplate(Request $request): void
{
$params = $request->param();
$data = [
array_values(ModelNews::EXCELFIELD),
[
'公司新闻',
'想学嵌入式Linux领免费的瑞萨RZ/G2L开发板',
'https://www.myir-tech.com/attached/image/20230609/1-1.jpg',
'学习嵌入式系统开发是一个渐进的过程一般我们从51单片机开始逐步迁移到STM32微控制器然后学习使用FreeRTOS操作系统最终进入嵌入式Linux领域。以下是一个典型的学习路线51单片机作为嵌入式系统的入门级平台学习51单片机可以帮助我们了解基本的嵌入式开发概念和编程技巧。可以学习使用C语言编写简单的驱动程序、控制IO口和外设等。 ',
'米尔科技',
'',
'2023.6.9',
'0',
'1',
'<p style="text-align: start;"><strong>需要报名RZ/G2L免费开发板请扫码</strong></p><p style="text-align: center;"><img src="https://www.myir-tech.com/attached/image/20230609/22.png" alt="" data-href="" style=""></p>',
]
];
$excel = (new Excel())->exporTsheet($data);
$excel->save('新闻导入模板.xlsx');
}
/**
* 导入excel
*/
public function importExcel(Request $request): array
{
$file = new UploadFile('uploads', 'fileExt:xlsx');
$file->putFile('news');
$msg = ModelNews::importExcel($file);
return [
'code' => 0,
'msg' => $msg
];
}
}

View File

@ -1,163 +0,0 @@
<?php
namespace app\admin\controller\Products;
use app\BaseController;
use app\common\model\Products\Product as ModelProduct;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use app\common\exception\Tool;
use think\facade\Db;
use think\facade\Env;
class Product extends BaseController
{
/**
* 获取产品列表
*/
public function getProductList(Request $request, $isExport = false): array
{
$params = $request->param();
$con = [];
$con = Tool::getOptionalQuery(['a.product_type_guid', '='], ['product_name', 'LIKE'],);
$query = ModelProduct::alias('a')
->leftjoin('product_type b', 'a.product_type_guid = b.product_type_guid')
->where($con)
->field([
'a.product_id',
'a.product_guid',
'a.product_type_guid',
'b.product_type_name',
'a.product_name',
'a.product_img',
'a.product_description',
'a.product_link',
'a.product_details',
'a.product_sort'
])
->order('product_sort', 'asc');
return $isExport ? $query->select()->toArray() : msg("获取产品列表成功!", $query);
}
/**
* 添加产品
*/
public function addProduct(Request $request): array
{
$params = $request->param();
$this->validate($params, [
'product_type_guid|产品类目' => 'require',
'product_name|产品名称' => 'require',
'product_img|产品图片' => 'require',
'product_sort|产品排序' => 'require'
]);
$model = ModelProduct::create($params, [
'product_type_guid',
'product_name',
'product_img',
'product_description',
'product_link',
'product_details',
'product_sort',
'product_guid',
'product_create_user_guid',
'product_update_user_guid'
]);
return msg('添加成功!');
}
/**
* 编辑产品
*/
public function editProduct(Request $request): array
{
$params = $request->param();
$this->validate($params, [
'product_type_guid|产品类目' => 'require',
'product_name|产品名称' => 'require',
'product_img|产品图片' => 'require',
'product_sort|产品排序' => 'require'
]);
$model = ModelProduct::where('product_guid', $params['product_guid'])->find();
if (!$model) throwErrorMsg("该产品不存在", 1);
$model->allowField([
'product_type_guid',
'product_name',
'product_img',
'product_description',
'product_link',
'product_details',
'product_sort',
'product_update_user_guid'
])->save($params);
return msg('编辑成功!');
}
/**
* 删除产品
*/
public function deleteProduct(Request $request): array
{
$params = $request->param();
$this->validate($params, [
'product_guid' => 'require',
]);
$product = ModelProduct::where([
'product_guid' => explode(',', $params['product_guid'])
])->select();
$product->delete();
return msg('删除成功!');
}
/**
* 导出Excel
*/
public function exportExcel(Request $request): void
{
ModelProduct::exportExcel(self::getProductList($request, true));
}
/**
* 下载导入模板
*/
public function downloadTemplate(Request $request): void
{
$params = $request->param();
$data = [
array_values(ModelProduct::EXCELFIELD),
[
'软件工具',
'Arm Development StudioArm',
'http://localhost:3000/uploads/ProductImg/20230628/f5a7065c3db295c1e3e2a26b36cb41ed.jpg',
'Arm全面端到端的嵌入式开发工具',
'',
'',
'1',
]
];
$excel = (new Excel())->exporTsheet($data);
$excel->save('产品导入模板.xlsx');
}
/**
* 导入excel
*/
public function importExcel(Request $request): array
{
$file = new UploadFile('uploads', 'fileExt:xlsx');
$file->putFile('product');
$msg = ModelProduct::importExcel($file);
return [
'code' => 0,
'msg' => $msg
];
}
}

View File

@ -1,233 +0,0 @@
<?php
namespace app\admin\controller\Products;
use app\BaseController;
use app\common\model\Products\ProductType as ModelProductType;
use app\common\arw\adjfut\src\Traverse;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use app\common\exception\Tool;
use think\facade\Db;
use think\facade\Env;
class ProductType extends BaseController
{
/**
* 获取产品类目树形列表接口
*
* @param Request request
* @date 2023-03-25
* @example
* @author xjh
* @since 1.0.0
*/
public function getProductTypeTree(Request $request, $isExport = false): array
{
$con = Tool::getOptionalQuery(
['b.product_delete_time', 'NULL'],
['a.product_type_name', 'LIKE']
);
$product_type = ModelProductType::field([
'a.product_type_parent_guid',
'a.product_type_guid',
'a.product_type_name',
'b.product_type_name' => "product_type_parent_name",
'a.product_type_title',
'a.product_type_link',
'a.product_type_icon',
'a.product_type_order',
])
->alias('a')
->leftjoin('product_type b', 'a.product_type_parent_guid = b.product_type_guid')
->where($con)
->order(['product_type_order' => 'asc'])
->select()->toArray();
$Traverse = new Traverse('product_type_guid', 'product_type_parent_guid');
$product_type_tree = $Traverse->tree($product_type, '0', function ($v) {
return [
'product_type_guid' => $v['product_type_guid'],
'product_type_parent_guid' => $v['product_type_parent_guid'],
'product_type_name' => $v['product_type_name'],
'product_type_parent_name' => $v['product_type_parent_name'],
'product_type_title' => $v['product_type_title'],
'product_type_link' => $v['product_type_link'],
'product_type_icon' => $v['product_type_icon'],
'product_type_order' => $v['product_type_order'],
];
});
return $isExport ? $product_type : msg("获取产品类目列表成功!", $product_type_tree);
}
/**
* 添加产品类目接口
*
* @param Request request
* @date 2023-03-25
* @example
* @author xjh
* @since 1.0.0
*/
public function addProductType(Request $request): array
{
$params = $request->param();
$this->validate($params, [
'product_type_name|产品类型名称' => 'require',
'product_type_order|产品类目排序' => 'require'
]);
ModelProductType::isDuplicateName($params['product_type_name'], $params['product_type_parent_guid']);
$product_type_parent_where = [];
if (!empty($params['product_type_parent_guid'])) {
$params['product_type_ancestors_guid'] = ModelProductType::buildAncestorsGuid($params['product_type_parent_guid']);
$product_type_parent_where['product_type_parent_guid'] = $params['product_type_parent_guid'];
}
ModelProductType::create($params, [
'product_type_guid',
'product_type_create_user_guid',
'product_type_update_user_guid',
'product_type_parent_guid',
'product_type_ancestors_guid',
'product_type_name',
'product_type_title',
'product_type_link',
'product_type_icon',
'product_type_order'
]);
return msg('添加成功!');
}
/**
* 编辑产品类目接口
*
* @param Request request
* @date 2023-03-25
* @example
* @author xjh
* @since 1.0.0
*/
public function editProductType(Request $request): array
{
$params = $request->param();
$this->validate($params, [
'product_type_guid|产品类目guid' => 'require',
'product_type_name|产品类目名称' => 'require',
'product_type_order|产品类目排序' => 'require',
]);
$model = ModelProductType::where('product_type_guid', $params['product_type_guid'])->find();
if (!$model) throwErrorMsg("该产品类目不存在", 1);
if ($model->product_type_parent_guid == $model->product_type_guid) {
throwErrorMsg("上级菜单不能选择与当前菜单一样的", 1);
}
ModelProductType::isDuplicateName($params['product_type_name'], $params['product_type_guid']);
$params['product_type_ancestors_guid'] = ModelProductType::buildAncestorsGuid($params['product_type_parent_guid']);
$model->allowField([
'product_type_update_user_guid',
'product_type_name',
'product_type_title',
'product_type_link',
'product_type_icon',
'product_type_ancestors_guid',
'product_type_parent_guid',
'product_type_order',
])->save($params);
return msg('编辑成功!');
}
/**
* 删除产品类目接口
*
* @param Request request
* @date 2023-03-25
* @example
* @author xjh
* @since 1.0.0
*/
public function deleteProductType(Request $request): array
{
Db::startTrans();
try {
$params = $request->param();
$this->validate($params, [
'product_type_guid|产品类目guid' => 'require',
]);
$guids = explode(',', $params['product_type_guid']);
ModelProductType::where(['product_type_guid' => $guids])->select()->delete();
Db::commit();
return msg('删除成功!');
} catch (\Throwable $th) {
Db::rollback();
throw $th;
}
}
/**
* 导出Excel接口
*
* @param Request request
* @date 2023-03-25
* @example
* @author xjh
* @since 1.0.0
*/
public function exportExcel(Request $request)
{
ModelProductType::exportExcel(self::getProductTypeTree($request, true));
}
/**
* 下载导入模板接口
*
* @param Request request
* @date 2023-03-25
* @example
* @author xjh
* @since 1.0.0
*/
public function downloadTemplate(Request $request)
{
$params = $request->param();
$data = array_values(ModelProductType::EXCELFIELD);
$excel = (new Excel())->exporTsheet($data);
$excel->save('产品类目导入模板.xlsx');
}
/**
* 导入excel接口
*
* @param Request request
* @date 2023-03-25
* @example
* @author xjh
* @since 1.0.0
*/
public function importExcel(Request $request)
{
$file = new UploadFile('uploads', 'fileExt:xlsx');
$file->putFile('product_type');
$msg = ModelProductType::importExcel($file);
return [
'code' => 0,
'msg' => $msg
];
}
}

View File

@ -29,4 +29,52 @@ class CommonApi extends BaseController
{
return Captcha::create('verify');
}
/**
* 上传图片
*/
public function uploadImg(Request $request)
{
$dirName = $request->param('dirName');
$upload = new UploadFile('uploads', 'file');
$path = $upload->putFile($dirName . 'Img');
$url = "/uploads/" . $path;
//图片大小>500k压缩图片质量
$absolute_path = public_path() . $url;
if (ceil(filesize($absolute_path) / 1000) > 500) {
$image = \think\Image::open($absolute_path);
$image->save($absolute_path, null, 80);
}
return [
'code' => 0,
'data' => [
"name" => $path,
"url" => $url,
],
'msg' => '上传成功!'
];
}
/**
* 上传文件
*/
public function uploadFile(Request $request)
{
$dirName = $request->param('dirName');
$uploadedFile = $request->file('file');
$originalFilename = $uploadedFile->getOriginalName(); // 获取上传文件的原始文件名
$upload = new UploadFile('uploads', 'file');
$path = $upload->putFileAs($dirName . 'File', $originalFilename);
return [
'code' => 0,
'data' => [
"fileName" => $originalFilename,
"url" => "/uploads/" . $path,
],
'msg' => '上传成功!'
];
}
}

View File

@ -36,7 +36,7 @@ class Home extends BaseController
'love_story_cover',
])
->order('love_story_sort', 'asc')
->limit(4)
->limit(3)
->select();

View File

@ -79,4 +79,44 @@ class LoveStory extends BaseController
return msg(0, '获取爱情故事详情成功!', ['data' => $find]);
}
/**
* 添加爱情故事
*/
public function addLoveStory(Request $request): array
{
Db::startTrans();
Tool::adminLockTableWrite('love_story');
try {
$params = $request->param();
$this->validate($params, [
'love_story_title|标题' => 'require',
'love_story_place|地点' => 'require',
'love_story_date|日期' => 'require',
'love_story_cover|封面' => 'require',
// 'love_story_sort|排序' => 'require'
]);
$model = ModelLoveStory::create($params, [
'love_story_title',
'love_story_author',
'love_story_place',
'love_story_date',
'love_story_cover',
'love_story_sort',
'love_story_music',
'love_story_content',
'love_story_guid',
'love_story_create_user_guid',
'love_story_update_user_guid'
]);
Db::commit();
Tool::unlockTable();
return msg('添加成功!');
} catch (\Throwable $th) {
Db::rollback();
Tool::unlockTable();
throw $th;
}
}
}

View File

@ -45,4 +45,37 @@ class WishList extends BaseController
]);
}
/**
* 添加心愿单
*/
public function addWishList(Request $request): array
{
Db::startTrans();
Tool::adminLockTableWrite('wish_list');
try {
$params = $request->param();
$this->validate($params, [
'wish_list_name|心愿名称' => 'require',
]);
$params['wish_list_status'] = 1;
$model = ModelWishList::create($params, [
'wish_list_name',
'wish_list_author',
'wish_list_status',
'wish_list_complete_date',
'wish_list_sort',
'wish_list_guid',
'wish_list_create_user_guid',
'wish_list_update_user_guid'
]);
Db::commit();
Tool::unlockTable();
return msg('添加成功!');
} catch (\Throwable $th) {
Db::rollback();
Tool::unlockTable();
throw $th;
}
}
}

View File

@ -26,6 +26,7 @@ class Auth
'Login' => ['*'],
'admin' => ['*'],
'Consult' => ['*'],
'CommonApi' => ['*'],
];
/**

View File

@ -7,6 +7,6 @@ return [
// 中间件
'middleware' => [
// 接口鉴权
// \app\api\middleware\Auth::class
\app\api\middleware\Auth::class
],
];

View File

@ -68,6 +68,7 @@ class LoveStory extends BaseModel
*/
public static function onBeforeInsert(self $model): void
{
BaseModel::setUserGuid(false);
Tool::dataAddSortProc($model);
$model->completeCreateField();
}

View File

@ -64,6 +64,7 @@ class WishList extends BaseModel
*/
public static function onBeforeInsert(self $model): void
{
BaseModel::setUserGuid(false);
Tool::dataAddSortProc($model);
$model->completeCreateField();
}