Commit 681bbb12 authored by ZhuChunYang's avatar ZhuChunYang

update

parent d9b1bc63
......@@ -24,7 +24,7 @@ class ApplicationCategoryController extends BaseController
$page = $request->get('page', 1);
$limit = $request->get('limit', 10);
$name = $request->get('category_name', '');
$where = [];
$where[] = ['pid' => 0];
if($name){
$where[] = ['name' => $name];
}
......@@ -75,4 +75,34 @@ class ApplicationCategoryController extends BaseController
return ['code' => 1, 'msg' => 'failed'];
}
/**
*子分类列表
*/
public function actionSublist()
{
if(Yii::$app->request->isAjax){
$id = Yii::$app->request->get('id');
Yii::$app->response->format = 'json';
$request = Yii::$app->request;
$page = $request->get('page', 1);
$limit = $request->get('limit', 10);
$where[] = ['pid' => $id];
$data = CoinApplicationCategory::getList($page, $limit, $where);
return $data;
}else{
$id = Yii::$app->request->get('id');
if($id){
$parent_category = CoinApplicationCategory::getCategoryById($id);
if($parent_category){
return $this->render('sublist',['parent_category' => $parent_category]);
}else{
$this->error('id参数不合法', Yii::$app->request->getReferrer());
}
}else{
$this->error('id参数不能为空', Yii::$app->request->getReferrer());
}
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: ZCY
* Date: 2018/10/15
* Time: 10:26
*/
namespace backend\controllers;
use Yii;
class ApplicationController extends BaseController
{
public function actionIndex()
{
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = 'json';
$request = Yii::$app->request;
$page = $request->get('page', 1);
$limit = $request->get('limit', 10);
$name = $request->get('category_name', '');
$where = [];
if($name){
$where[] = ['name' => $name];
}
$data = CoinApplicationCategory::getList($page, $limit, $where);
return $data;
}
$items = CoinApplicationCategory::getItemsByPid(0);
return $this->render('index',['items' => $items]);
}
/**
* 添加一个application category
*/
public function actionAddAndEdit()
{
if (Yii::$app->request->isPost) {
Yii::$app->response->format = 'json';
$fields = ['id','name','sort','icon','pid'];
$params = $this->initParams(Yii::$app->request->post(), $fields);
$application_category = new CoinApplicationCategoryForm();
if($params['id']){ //edit
$application_category->setScenario(CoinApplicationCategoryForm::SCENARIO_EDIT);
$application_category->load($params,'');
return $application_category->edit();
}else{
$application_category->setScenario(CoinApplicationCategoryForm::SCENARIO_ADD);
$application_category->load($params,'');
return $application_category->add();
}
}
}
/**
* @return array
* @throws \Throwable
* @throws \yii\db\StaleObjectException
*/
public function actionDelete()
{
Yii::$app->response->format = 'json';
$id = Yii::$app->request->get('id');
if ($id) {
$application_category = new CoinApplicationCategoryForm();
return $application_category->del($id);
}
return ['code' => 1, 'msg' => 'failed'];
}
}
\ No newline at end of file
......@@ -9,6 +9,7 @@ namespace backend\models\coin;
use backend\models\BaseForm;
use common\models\psources\CoinApplicationCategory;
use common\service\coin\Coin;
class CoinApplicationCategoryForm extends BaseForm
{
......@@ -46,10 +47,12 @@ class CoinApplicationCategoryForm extends BaseForm
return [
[['name'], 'required', 'on' => self::SCENARIO_ADD],
[['id','name','sort'], 'required', 'on' => self::SCENARIO_EDIT],
['sort', 'default', 'value' => 99],
['sort', 'default', 'value' => 1],
['sort', 'integer'],
['icon', 'default', 'value' => 0],
['pid', 'default', 'value' => 0],
['name', 'checkName'],
['pid','checkPid','on' => self::SCENARIO_EDIT],
];
}
......@@ -68,6 +71,14 @@ class CoinApplicationCategoryForm extends BaseForm
}
}
public function checkPid($attribute, $params)
{
if($this->pid == $this->id){
$this->addError($attribute,'不能设置为自己的子分类');
}
}
/**
* 添加应用分类
*/
......@@ -77,6 +88,7 @@ class CoinApplicationCategoryForm extends BaseForm
$coin_applicate_category = new CoinApplicationCategory();
$coin_applicate_category->setAttributes($this->attributes,false);
$coin_applicate_category->save();
$items = CoinApplicationCategory::getItemsByPid(0);
return ['code' => 0,'msg' => '应用分类添加成功!'];
}else{
$error = self::getModelError($this);
......@@ -94,8 +106,7 @@ class CoinApplicationCategoryForm extends BaseForm
if($coin_applicate_category){
$coin_applicate_category->setAttributes($this->attributes,false);
$coin_applicate_category->save();
$items = CoinApplicationCategory::getItemsByPid(0);
return ['code' => 0,'msg' => '应用分类修改成功!','items' => $items];
return ['code' => 0,'msg' => '应用分类修改成功!'];
}else{
return ['code' => 1,'msg' => '应用分类不存在'];
}
......@@ -113,7 +124,12 @@ class CoinApplicationCategoryForm extends BaseForm
{
$coin_applicate_category = CoinApplicationCategory::getCategoryById($id);
if($coin_applicate_category){
$hasChild = CoinApplicationCategory::hasChildCategory($id);
if($hasChild){
return ['code' => 1,'msg' => '应用分类存在子分类,不能删除'];
}
$coin_applicate_category->delete();
$items = CoinApplicationCategory::getItemsByPid(0);
return ['code' => 0,'msg' => '应用分类删除成功'];
}else{
return ['code' => 1,'msg' => '应用分类不存在'];
......
......@@ -35,17 +35,7 @@
<form class="layui-form form_add" action="javascript:void(0)" id="form1" method="post" lay-filter="form1">
<input type="hidden" name="_csrf" value="<?= Yii::$app->request->getCsrfToken() ?>">
<input type="hidden" name="id" value="">
<div class="layui-form-item">
<label class="layui-form-label" style="margin-bottom: 0; width: 100px;">所属分类</label>
<div class="layui-input-block">
<select name="pid" id="select">
<option value="0">顶级分类</option>
<?php foreach ($items as $key => $value): ?>
<option value="<?= $key ?>"><?= $value ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<input type="hidden" name="pid" value="">
<div class="layui-form-item layui-form-text">
<label class="layui-form-label">名称</label>
......@@ -84,8 +74,10 @@
<img src="{{d.icon_url}}" style="max-width: 32px; max-height: 32px;"/>
</script>
<script type="text/html" id="operationTpl">
<button class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon">&#xe642;</i></button>
<button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del"><i class="layui-icon">&#xe640;</i></button>
<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="add">添加子分类</a>
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
<a class="layui-btn layui-btn-primary layui-btn-xs" href="/admin/application-category/sublist?id={{d.id}}" >管理</a>
</script>
<script type="text/javascript">
......@@ -169,8 +161,6 @@
curr: 1
}
});
refreshSelect(rev.items);
}
});
},
......@@ -194,6 +184,43 @@
}
});
});
}else if ('add' === event){
var index = layer.open({
title: '添加应用分类',
area: '500px',
type: 1,
content: $("#_form"),
btn: ['保存', '取消'],
success: function() {
clearForm();
form.val("form1", {
pid: data.id,
});
},
btn1: function() {
$.post('/admin/application-category/add-and-edit', $("#form1").serialize(), function(rev) {
layer.msg(rev.msg);
if (0 == rev.code) {
layer.close(index);
$("#_form").css('display', 'none');
clearForm();
table.reload('table1', {
page: {
curr: 1
}
});
}
});
},
btn2: function() {
layer.close(index);
$("#_form").css('display', 'none');
},
cancel: function() {
layer.close(index);
$("#_form").css('display', 'none');
}
});
}
});
form.on('submit(search)', function (obj) {
......@@ -267,15 +294,6 @@
});
$("#icon1").attr('src', '');
}
//刷新分类下拉框
function refreshSelect(items){
var html = '<option value="0">顶级分类</option>';
$.each(items, function(i, c) {
html += '<option value="' + i + '">' + c + '</option>';
});
$('#select').html(html);
form.render('select');
}
</script>
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: ZCY
* Date: 2018/10/15
* Time: 14:29
*/
?>
<h4>所属分类---<?= $parent_category->name ?></h4>
<div class="layui-col-md8">
<table class="layui-table" id="table1" lay-filter="table1"></table>
</div>
<input type="hidden" id="parent_id" name="parent_id" value="<?= $parent_category->id?>">
<!-- 添加页面 -->
<div class="layui-row add" style="display: none;padding: 5px;" id="_form">
<div class="layui-col-xs6 layui-col-sm6 layui-col-md11">
<form class="layui-form form_add" action="javascript:void(0)" id="form1" method="post" lay-filter="form1">
<input type="hidden" name="_csrf" value="<?= Yii::$app->request->getCsrfToken() ?>">
<input type="hidden" name="id" value="">
<input type="hidden" name="pid" value="">
<div class="layui-form-item layui-form-text">
<label class="layui-form-label">名称</label>
<div class="layui-input-block">
<input type="text" name="name" required lay-verify="required" placeholder="" autocomplete="off"
class="layui-input">
</div>
</div>
<div class="layui-form-item layui-form-text">
<label class="layui-form-label">排序</label>
<div class="layui-input-block">
<input type="text" name="sort" required lay-verify="required" placeholder=""
autocomplete="off"
class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">图标</label>
<div class="layui-input-block">
<img src="" style="margin-top: 11px; max-width: 32px; max-height: 32px"
id="icon1">
</div>
<input type="hidden" name="icon" value="">
</div>
<div class="layui-inline" style="margin-left: 50px;">
<button type="button" class="layui-btn" id="upload1" style="">
<i class="layui-icon">&#xe67c;</i>上传新图片
</button>
</div>
</div>
</form>
</div>
</div>
<script type="text/html" id="iconTpl">
<img src="{{d.icon_url}}" style="max-width: 32px; max-height: 32px;"/>
</script>
<script type="text/html" id="operationTpl">
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/javascript">
var table = layui.table;
var pid = $("#parent_id").val();
console.log(pid);
table.render({
elem: '#table1',
page: true,
limit: 10,
url: '/admin/application-category/sublist?id='+pid,
cols: [[
{
field: 'id',
title: 'ID'
},
{
field: 'name',
title: '名称',
},
{
field: 'icon_url',
title: '图标',
templet: '#iconTpl'
},
{
field: 'sort',
title: '排序'
},
{
title: '操作',
templet: '#operationTpl'
},
{
field: 'icon',
title: '图标',
hide:true,
width:50
},
{
field: 'pid',
title: '所属父级',
hide:true,
width:50
},
]],
});
var form = layui.form;
form.render();
table.on('tool(table1)', function(obj) {
var event = obj.event;
var data = obj.data;
if (event === 'edit') {
var index = layer.open({
title: '编辑应用分类',
area: '500px',
type: 1,
content: $("#_form"),
btn: ['保存', '取消'],
success: function() {
form.val("form1", {
id: data.id,
name: data.name,
icon: data.icon,
sort: data.sort,
pid: data.pid,
});
$("#icon1").attr('src', data.icon_url);
},
btn1: function() {
$.post('/admin/application-category/add-and-edit', $("#form1").serialize(), function(rev) {
layer.msg(rev.msg);
if (0 == rev.code) {
layer.close(index);
$("#_form").css('display', 'none');
table.reload('table1', {
page: {
curr: 1
}
});
}
});
},
btn2: function() {
layer.close(index);
$("#_form").css('display', 'none');
},
cancel: function() {
layer.close(index);
$("#_form").css('display', 'none');
}
});
} else if ('del' === event) {
var index = layer.confirm("确认删除?", {icon: 3, title: '删除'}, function () {
$.get('/admin/application-category/delete', {id: data.id}, function (rev) {
layer.msg(rev.msg);
if (0==rev.code) {
table.reload('table1',{
page:{curr:1}
});
}
});
});
}
});
//图片上传
var uploader = layui.upload;
$_csrf = $("input[name='_csrf']").val();
uploader.render({
elem: "#upload1",
url: '/admin/image/upload',
data: {_csrf: $_csrf},
done: function (res) {
if(res.code == 0){
$("input[name='icon']").val(res.data.image_id);
$("#icon1").attr('src', res.data.image_src);
}
},
error: function (res) {
}
});
</script>
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: ZCY
* Date: 2018/10/15
* Time: 10:48
*/
\ No newline at end of file
......@@ -80,4 +80,9 @@ class CoinApplicationCategory extends BaseActiveRecord
$items = array_column($data,'name','id');
return $items;
}
public static function hasChildCategory($pid)
{
return self::find()->where(['pid' => $pid])->count();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment