优化商品、文章编辑新增方法

This commit is contained in:
hdm 2022-06-20 11:40:57 +08:00
parent ff6c1c0d48
commit 4ffdc6f774
5 changed files with 83 additions and 109 deletions

View File

@ -171,29 +171,23 @@ class Article extends BaseController
$param['update_time'] = time(); $param['update_time'] = time();
Db::startTrans(); Db::startTrans();
try { try {
$res = ArticleList::strict(false)->field(true)->update($param); ArticleList::strict(false)->field(true)->update($param);
$aid = $param['id']; $aid = $param['id'];
if ($res) { //关联关键字
//关联关键字 if (isset($param['keyword_names']) && $param['keyword_names']) {
if (isset($param['keyword_names']) && $param['keyword_names']) { Db::name('ArticleKeywords')->where(['aid'=>$aid])->delete();
Db::name('ArticleKeywords')->where(['aid'=>$aid])->delete(); $keywordArray = explode(',', $param['keyword_names']);
$keywordArray = explode(',', $param['keyword_names']); $res_keyword = (new ArticleList())->insertKeyword($keywordArray,$aid);
$res_keyword = (new ArticleList())->insertKeyword($keywordArray,$aid); }
} add_log('edit', $aid, $param);
else{ Db::commit();
$res_keyword == true; $DbRes=true;
}
if($res_keyword!== false){
add_log('edit', $param['id'], $param);
Db::commit();
$DbRes=true;
}
} else {
Db::rollback();
}
} }
catch (\Exception $e) { ##这里参数不能删除($e错误信息) catch (\Exception $e) {
Db::rollback(); //回滚事务
$DbRes = false;
Db::rollback();
return to_assign(1,'捕获到异常'.$e->getMessage());
} }
} else { } else {
try { try {
@ -209,27 +203,20 @@ class Article extends BaseController
$param['desc'] = getDescriptionFromContent($param['content'], 100); $param['desc'] = getDescriptionFromContent($param['content'], 100);
} }
$aid = ArticleList::strict(false)->field(true)->insertGetId($param); $aid = ArticleList::strict(false)->field(true)->insertGetId($param);
if ($aid) { //关联关键字
//关联关键字 if (isset($param['keyword_names']) && $param['keyword_names']) {
if (isset($param['keyword_names']) && $param['keyword_names']) { $keywordArray = explode(',', $param['keyword_names']);
Db::name('ArticleKeywords')->where(['aid'=>$aid])->delete(); $res_keyword = (new ArticleList())->insertKeyword($keywordArray,$aid);
$keywordArray = explode(',', $param['keyword_names']); }
$res_keyword = (new ArticleList())->insertKeyword($keywordArray,$aid); add_log('add', $aid, $param);
} Db::commit();
else{ $DbRes=true;
$res_keyword == true;
}
if($res_keyword!== false){
add_log('add', $aid, $param);
Db::commit();
$DbRes=true;
}
} else {
Db::rollback();
}
} }
catch (\Exception $e) { ##这里参数不能删除($e错误信息) catch (\Exception $e) {
Db::rollback(); //回滚事务
$DbRes = false;
Db::rollback();
return to_assign(1,'捕获到异常'.$e->getMessage());
} }
} }
if($DbRes){ if($DbRes){

View File

@ -174,29 +174,23 @@ class Goods extends BaseController
$param['update_time'] = time(); $param['update_time'] = time();
Db::startTrans(); Db::startTrans();
try { try {
$res = GoodsList::strict(false)->field(true)->update($param); GoodsList::strict(false)->field(true)->update($param);
$aid = $param['id']; $aid = $param['id'];
if ($res) { //关联关键字
//关联关键字 if (isset($param['keyword_names']) && $param['keyword_names']) {
if (isset($param['keyword_names']) && $param['keyword_names']) { Db::name('GoodsKeywords')->where(['aid'=>$aid])->delete();
Db::name('GoodsKeywords')->where(['aid'=>$aid])->delete(); $keywordArray = explode(',', $param['keyword_names']);
$keywordArray = explode(',', $param['keyword_names']); $res_keyword = (new GoodsList())->insertKeyword($keywordArray,$aid);
$res_keyword = (new GoodsList())->insertKeyword($keywordArray,$aid); }
} add_log('edit', $aid, $param);
else{ Db::commit();
$res_keyword == true; $DbRes=true;
}
if($res_keyword!== false){
add_log('edit', $param['id'], $param);
Db::commit();
$DbRes=true;
}
} else {
Db::rollback();
}
} }
catch (\Exception $e) { ##这里参数不能删除($e错误信息) catch (\Exception $e) {
Db::rollback(); //回滚事务
$DbRes = false;
Db::rollback();
return to_assign(1,'捕获到异常'.$e->getMessage());
} }
} else { } else {
try { try {
@ -206,33 +200,26 @@ class Goods extends BaseController
return to_assign(1, $e->getError()); return to_assign(1, $e->getError());
} }
$param['create_time'] = time(); $param['create_time'] = time();
if(empty($param['desc'])){
$param['desc'] = getDescriptionFromContent($param['content'], 100);
}
Db::startTrans(); Db::startTrans();
try { try {
if(empty($param['desc'])){
$param['desc'] = getDescriptionFromContent($param['content'], 100);
}
$aid = GoodsList::strict(false)->field(true)->insertGetId($param); $aid = GoodsList::strict(false)->field(true)->insertGetId($param);
if ($aid) { //关联关键字
//关联关键字 if (isset($param['keyword_names']) && $param['keyword_names']) {
if (isset($param['keyword_names']) && $param['keyword_names']) { $keywordArray = explode(',', $param['keyword_names']);
Db::name('GoodsKeywords')->where(['aid'=>$aid])->delete(); $res_keyword = (new GoodsList())->insertKeyword($keywordArray,$aid);
$keywordArray = explode(',', $param['keyword_names']); }
$res_keyword = (new GoodsList())->insertKeyword($keywordArray,$aid); add_log('edit', $aid, $param);
} Db::commit();
else{ $DbRes=true;
$res_keyword == true;
}
if($res_keyword!== false){
add_log('add', $aid, $param);
Db::commit();
$DbRes=true;
}
} else {
Db::rollback();
}
} }
catch (\Exception $e) { ##这里参数不能删除($e错误信息) catch (\Exception $e) {
Db::rollback(); //回滚事务
$DbRes = false;
Db::rollback();
return to_assign(1,'捕获到异常'.$e->getMessage());
} }
} }
if($DbRes){ if($DbRes){

View File

@ -15,8 +15,8 @@ class Article extends Model
// 获取文章详情 // 获取文章详情
public function detail($id) public function detail($id)
{ {
$article = \think\facade\Db::name('Article')->where(['id' => $id])->find(); $detail = \think\facade\Db::name('Article')->where(['id' => $id])->find();
if (!empty($article)) { if (!empty($detail)) {
$keyword_array = \think\facade\Db::name('ArticleKeywords') $keyword_array = \think\facade\Db::name('ArticleKeywords')
->field('i.aid,i.keywords_id,k.title') ->field('i.aid,i.keywords_id,k.title')
->alias('i') ->alias('i')
@ -25,11 +25,11 @@ class Article extends Model
->where(array('i.aid' => $id, 'k.status' => 1)) ->where(array('i.aid' => $id, 'k.status' => 1))
->select()->toArray(); ->select()->toArray();
$article['keyword_ids'] = implode(",", array_column($keyword_array, 'keywords_id')); $detail['keyword_ids'] = implode(",", array_column($keyword_array, 'keywords_id'));
$article['keyword_names'] = implode(',', array_column($keyword_array, 'title')); $detail['keyword_names'] = implode(',', array_column($keyword_array, 'title'));
$article['keyword_array'] = $keyword_array; $detail['keyword_array'] = $keyword_array;
} }
return $article; return $detail;
} }
//插入关键字 //插入关键字

View File

@ -15,11 +15,11 @@ class Goods extends Model
// 获取商品详情 // 获取商品详情
public function detail($id) public function detail($id)
{ {
$goods = \think\facade\Db::name('Goods')->where(['id'=>$id])->find(); $detail = \think\facade\Db::name('Goods')->where(['id'=>$id])->find();
if(!empty($goods)) { if(!empty($detail)) {
//轮播图 //轮播图
if(!empty($goods['banner'])) { if(!empty($detail['banner'])) {
$goods['banner_array'] = explode(',',$goods['banner']); $detail['banner_array'] = explode(',',$detail['banner']);
} }
//关键字 //关键字
$keywrod_array = \think\facade\Db::name('GoodsKeywords') $keywrod_array = \think\facade\Db::name('GoodsKeywords')
@ -30,35 +30,35 @@ class Goods extends Model
->where(array('i.aid' => $id, 'k.status' => 1)) ->where(array('i.aid' => $id, 'k.status' => 1))
->select()->toArray(); ->select()->toArray();
$goods['keyword_ids'] = implode(",", array_column($keywrod_array, 'keywords_id')); $detail['keyword_ids'] = implode(",", array_column($keywrod_array, 'keywords_id'));
$goods['keyword_names'] = implode(',', array_column($keywrod_array, 'title')); $detail['keyword_names'] = implode(',', array_column($keywrod_array, 'title'));
//标签设置 //标签设置
$goods['tag1'] = $goods['tag2'] = $goods['tag3'] = $goods['tag4'] = $goods['tag5'] = $goods['tag6'] =0; $detail['tag1'] = $detail['tag2'] = $detail['tag3'] = $detail['tag4'] = $detail['tag5'] = $detail['tag6'] =0;
if(!empty($goods['tag_values'])) { if(!empty($detail['tag_values'])) {
$tag_values_array = explode(',', $goods['tag_values']); $tag_values_array = explode(',', $detail['tag_values']);
if(in_array('1', $tag_values_array)){ if(in_array('1', $tag_values_array)){
$goods['tag1'] = 1; $detail['tag1'] = 1;
} }
if(in_array('2', $tag_values_array)){ if(in_array('2', $tag_values_array)){
$goods['tag2'] = 1; $detail['tag2'] = 1;
} }
if(in_array('3', $tag_values_array)){ if(in_array('3', $tag_values_array)){
$goods['tag3'] = 1; $detail['tag3'] = 1;
} }
if(in_array('4', $tag_values_array)){ if(in_array('4', $tag_values_array)){
$goods['tag4'] = 1; $detail['tag4'] = 1;
} }
if(in_array('5', $tag_values_array)){ if(in_array('5', $tag_values_array)){
$goods['tag5'] = 1; $detail['tag5'] = 1;
} }
if(in_array('6', $tag_values_array)){ if(in_array('6', $tag_values_array)){
$goods['tag6'] = 1; $detail['tag6'] = 1;
} }
} }
} }
return $goods; return $detail;
} }
//插入关键字 //插入关键字

View File

@ -5,8 +5,8 @@
<table class="layui-hide" id="list" lay-filter="list"></table> <table class="layui-hide" id="list" lay-filter="list"></table>
</div> </div>
<script type="text/html" id="toolbarDemo"> <script type="text/html" id="toolbarDemo">
<div class="layui-btn-container"> <div>
<button class="layui-btn layui-btn-normal layui-btn-sm addNew" type="button">+ 添加功能模块</button> <span class="red">模块功能即将推出,该列表目前只是预设数据,不影响开发者进行二次开发。</span>
</div> </div>
</script> </script>
{/block} {/block}