-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
496 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
|
||
namespace app\admin\controller; | ||
|
||
use app\admin\validate\RegionValidate; | ||
use app\common\facade\RegionFacade; | ||
use Overtrue\Pinyin\Pinyin; | ||
use think\Db; | ||
|
||
class RegionController extends BaseController | ||
{ | ||
|
||
/** | ||
* 区域管理 | ||
*/ | ||
public function index($keyword = "", $cate_id = 0) | ||
{ | ||
if ($this->request->isPost()) { | ||
return redirect(url('', ['cate_id' => $cate_id, 'keyword' => base64url_encode($keyword)])); | ||
} | ||
$keyword = empty($keyword) ? "" : base64url_decode($keyword); | ||
$model = Db::view('region', '*') | ||
->view('region category', ['name' => 'category_name', 'title' => 'category_title'], 'region.pid=category.id', 'LEFT') | ||
->where('region.pid', '>', 0); | ||
if (!empty($keyword)) { | ||
$model->whereLike('region.title|region.title_en', "%$keyword%"); | ||
} | ||
if ($cate_id > 0) { | ||
$model->whereIn('region.pid', RegionFacade::getSubCateIds($cate_id, 2)); | ||
} | ||
|
||
$lists = $model->order('id DESC')->paginate(10); | ||
$this->assign('lists', $lists); | ||
$this->assign('page', $lists->render()); | ||
$this->assign('keyword', $keyword); | ||
$this->assign('cate_id', $cate_id); | ||
$this->assign("category", RegionFacade::getSubCategory(0, 2)); | ||
$this->assign("regions", RegionFacade::getCategories()); | ||
|
||
return $this->fetch(); | ||
} | ||
|
||
public function batch($pid = 0) | ||
{ | ||
$content = $this->request->post('content'); | ||
$rows = explode("\n", $content); | ||
$datas = []; | ||
$names = Db::name('region')->field('name')->select(); | ||
$names = array_column($names, 'name'); | ||
$pinyin = new Pinyin(); | ||
$sort = 0; | ||
if ($pid > 0) { | ||
$sort = Db::name('region')->where('pid', $pid)->max('sort') + 1; | ||
} else { | ||
$sort = Db::name('region')->max('sort') + 1; | ||
} | ||
foreach ($rows as $item) { | ||
$item = trim($item); | ||
if (empty($item)) continue; | ||
$fields = explode(',', $item); | ||
$fieldCount = count($fields); | ||
$data = ['pid' => $pid, 'sort' => $sort++]; | ||
if ($fieldCount > 2) { | ||
$data['title'] = trim($fields[0]); | ||
$data['title_en'] = trim($fields[1]); | ||
$data['name'] = trim($fields[2]); | ||
} elseif ($fieldCount > 1) { | ||
$data['title'] = trim($fields[0]); | ||
$data['title_en'] = trim($fields[1]); | ||
$data['name'] = strtolower(str_replace([' ', "'"], ['_', ''], trim($fields[1]))); | ||
} else { | ||
$data['title'] = trim($fields[0]); | ||
$data['title_en'] = trim($fields[0]); | ||
$data['name'] = $pinyin->permalink(trim($fields[0]), ''); | ||
} | ||
if (in_array($data['name'], $names)) { | ||
$parts = explode('_', $data['name']); | ||
$partCount = count($parts); | ||
if (count($parts) > 1) { | ||
$parts[$partCount - 1] += 1; | ||
while (((in_array(implode('_', $parts), $names)))) { | ||
$parts[$partCount - 1] += 1; | ||
} | ||
} else { | ||
$parts[] = 1; | ||
while (((in_array(implode('_', $parts), $names)))) { | ||
$parts[$partCount] += 1; | ||
} | ||
} | ||
$data['name'] = implode('_', $parts); | ||
} | ||
$names[] = $data['name']; | ||
|
||
$datas[] = $data; | ||
unset($data); | ||
} | ||
if (!empty($datas)) { | ||
Db::name('region')->insertAll($datas); | ||
$this->success('添加成功'); | ||
} | ||
$this->error('未提交数据'); | ||
} | ||
|
||
/** | ||
* 删除地区 | ||
* @param $id | ||
*/ | ||
public function delete($id) | ||
{ | ||
$model = Db::name('region'); | ||
$result = $model->whereIn("id", idArr($id))->delete(); | ||
if ($result) { | ||
user_log($this->mid, 'deleteregion', 1, '删除地区 ' . $id, 'manager'); | ||
$this->success(lang('Delete success!'), url('region/index')); | ||
} else { | ||
$this->error(lang('Delete failed!')); | ||
} | ||
} | ||
|
||
public function category($id = 0) | ||
{ | ||
|
||
if ($this->request->isPost()) { | ||
$data = $this->request->post(); | ||
|
||
$validate = new RegionValidate(); | ||
$validate->setId($id); | ||
|
||
if (!$validate->check($data)) { | ||
$this->error($validate->getError()); | ||
} else { | ||
|
||
try { | ||
if ($id > 0) { | ||
Db::name('region')->where('id', $id)->update($data); | ||
} else { | ||
Db::name('region')->insert($data); | ||
} | ||
|
||
RegionFacade::clearCache(); | ||
} catch (\Exception $err) { | ||
$this->error(lang('Update failed: %s', [$err->getMessage()])); | ||
} | ||
|
||
$this->success(lang('Update success!'), url('region/index')); | ||
} | ||
} | ||
$model = Db::name('region')->find($id); | ||
if (empty($model)) { | ||
$this->error('地区不存在'); | ||
} | ||
return json(['data' => $model, 'code' => 1]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace app\admin\validate; | ||
|
||
|
||
use app\common\core\BaseUniqueValidate; | ||
|
||
/** | ||
* 地区验证 | ||
* Class CategoryValidate | ||
* @package app\admin\validate | ||
*/ | ||
class RegionValidate extends BaseUniqueValidate | ||
{ | ||
protected $rule=array( | ||
'title'=>'require', | ||
'short'=>'max:20', | ||
'name'=>'require|unique:region,%id%' | ||
); | ||
protected $message=array( | ||
'title.require'=>'请填写地区名称', | ||
'name.require'=>'请填写地区拼音', | ||
'name.unique'=>'地区拼音不可重复', | ||
'short.max'=>'地区简称长度不能超过20' | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
{extend name="public:base" /} | ||
|
||
{block name="body"} | ||
{include file="public/bread" menu="region_index" title="地区管理" /} | ||
<div id="page-wrapper"> | ||
<div class="row"> | ||
<div class="col" style="width: 320px;flex-basis:320px;flex-grow: 0;"> | ||
<div class="card side-cate"> | ||
<div class="card-header"> | ||
<a href="javascript:" class="btn float-right btn-outline-primary btn-sm addcate">添加</a> | ||
<a href="javascript:" class="btn float-right btn-outline-primary btn-sm btn-batch-add"><i | ||
class="ion-md-albums"></i> {:lang('Batch add')}</a> | ||
区域 | ||
</div> | ||
<ul class="list-group list-group-flush" style="max-height: 80vh; overflow: auto;"> | ||
<li class="list-group-item"><a class="list-cate-item" | ||
href="{:murl('index',['key'=>$keyword,'cate_id'=>0])}" data-value="0">不限区域</a></li> | ||
{foreach name="category" item="v"} | ||
<li class="list-group-item{$cate_id == $v['id']?' active':''}"> | ||
<a href="javascript:" data-id="{$v['id']}" title="删除地区" | ||
class="float-right ml-2 text-danger delcate"><i class="ion-md-trash"></i></a> | ||
<a href="javascript:" data-id="{$v['id']}" title="编辑地区" data-title="{$v['title']}" | ||
data-short="{$v['short']}" data-title_en="{$v['title_en']}" data-code="{$v['code']}" | ||
data-name="{$v['name']}" data-sort="{$v['sort']}" data-pid="{$v['pid']}" | ||
class="float-right ml-2 addcate"><i class="ion-md-create"></i></a> | ||
<a href="javascript:" data-pid="{$v['id']}" title="添加地区" class="float-right addcate"><i | ||
class="ion-md-add"></i></a> | ||
<a class="list-cate-item" href="{:murl('index',['key'=>$keyword,'cate_id'=>$v['id']])}" | ||
data-value="{$v.id}">{$v.html} {$v.title}</a> | ||
</li> | ||
{/foreach} | ||
</ul> | ||
</div> | ||
</div> | ||
<div class="col"> | ||
<table class="table table-hover table-striped"> | ||
<thead> | ||
<tr> | ||
<th width="50">编号</th> | ||
<th>地区</th> | ||
<th>英文</th> | ||
<th>区号</th> | ||
<th>拼音</th> | ||
<th>地区</th> | ||
<th>排序</th> | ||
<th width="160"> </th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{empty name="lists"}{:list_empty(7)}{/empty} | ||
{volist name="lists" id="v" } | ||
<tr> | ||
<td><input type="checkbox" name="id" value="{$v.id}" /></td> | ||
<td>{$v.title}</td> | ||
<td>{$v.title_en}</td> | ||
<td>{$v.code}</td> | ||
<td>{$v.category_title}</td> | ||
<td> | ||
{$v.sort} | ||
</td> | ||
<td class="operations"> | ||
<a class="btn btn-outline-primary addcate" href="javascript:" data-id="{$v['id']}" | ||
title="编辑地区" data-title="{$v['title']}" data-short="{$v['short']}" | ||
data-title_en="{$v['title_en']}" data-code="{$v['code']}" data-name="{$v['name']}" | ||
data-sort="{$v['sort']}" data-pid="{$v['pid']}"><i class="ion-md-create"></i> </a> | ||
<a class="btn btn-outline-danger link-confirm" title="{:lang('Delete')}" | ||
data-confirm="您真的确定要删除吗?\n删除后将不能恢复!" | ||
href="{:murl('region/delete',array('id'=>$v['id']))}"><i class="ion-md-trash"></i> </a> | ||
</td> | ||
</tr> | ||
{/volist} | ||
</tbody> | ||
</table> | ||
<div class="clearfix"></div> | ||
{$page|raw} | ||
</div> | ||
</div> | ||
|
||
|
||
|
||
</div> | ||
</block> | ||
<block name="script"> | ||
<script type="text/html" id="cate-template"> | ||
<div class="form-vertical"> | ||
<div class="form-group"> | ||
<div class="input-group"><span class="input-group-prepend"><span class="input-group-text">区域名称</span></span><input class="form-control" name="title" placeholder="区域显示的名称"/></div> | ||
</div> | ||
<div class="form-group"> | ||
<div class="input-group"><span class="input-group-prepend"><span class="input-group-text">英文名称</span></span><input class="form-control" name="title_en" placeholder="区域名称的英文"/></div> | ||
</div> | ||
<div class="form-group"> | ||
<div class="input-group"><span class="input-group-prepend"><span class="input-group-text">区号</span></span><input class="form-control" name="code" placeholder="移动电话区号"/></div> | ||
</div> | ||
<div class="form-group"> | ||
<div class="input-group"><span class="input-group-prepend"><span class="input-group-text">区域简称</span></span><input class="form-control" name="short" placeholder="区域名称的简写"/></div> | ||
</div> | ||
<div class="form-group"> | ||
<div class="input-group"><span class="input-group-prepend"><span class="input-group-text">区域拼音</span></span><input class="form-control" name="name" placeholder="区域对应的唯一识别符号,只能为英文"/></div> | ||
</div> | ||
<div class="form-group"> | ||
<div class="input-group"><span class="input-group-prepend"><span class="input-group-text">所属区域</span></span><select class="form-control" name="pid"> | ||
<option value="0">顶级区域</option> | ||
{foreach $category as $v} | ||
<option value="{$v.id}" >{$v.html} {$v.title}</option> | ||
{/foreach} | ||
</select> | ||
</div> | ||
</div> | ||
</div> | ||
</script> | ||
<script type="text/html" id="cateselect"> | ||
<div class="form-group"> | ||
<select class="form-control"> | ||
<option value="0">顶级分类</option> | ||
{volist name="regions" id="cate"} | ||
<option value="{$cate.id}">{$cate.html|raw} {$cate.title}</option> | ||
{/volist} | ||
</select> | ||
</div> | ||
<div class="form-group text-muted">每行一个分类,每个分类以空格区分名称、简称、别名,简称、别名可依次省略,别名必须使用英文字母<br />例:分类名称 分类简称 catename</div> | ||
</script> | ||
<script type="text/javascript"> | ||
jQuery(function ($) { | ||
$('.addcate').click(function () { | ||
var data = $(this).data(); | ||
var dlg = new Dialog({ | ||
backdrop: 'static', | ||
onshown: function (body) { | ||
bindData(body, data) | ||
}, | ||
onsure: function (body) { | ||
var newData = getData(body) | ||
newData.id = data.id ? data.id : 0 | ||
$.ajax({ | ||
url: "{:murl('category')}", | ||
data: newData, | ||
type: 'POST', | ||
dataType: 'json', | ||
success: function (json) { | ||
if (json.code == 1) { | ||
dialog.alert(json.msg, function () { | ||
location.reload() | ||
}) | ||
} else { | ||
dialog.error(json.msg); | ||
} | ||
} | ||
}) | ||
return false; | ||
} | ||
}).show($('#cate-template').html(), data.id > 0 ? '编辑分类' : '添加分类'); | ||
}) | ||
$('.btn-batch-add').click(function (e) { | ||
var prmpt = dialog.prompt({ | ||
title: '批量添加', | ||
content: $('#cateselect').html(), | ||
is_textarea: true | ||
}, function (args, body) { | ||
var pid = body.find('select').val(); | ||
var loading = dialog.loading('正在提交...'); | ||
$.ajax({ | ||
url: "{:murl('batch')}", | ||
type: 'POST', | ||
dataType: 'json', | ||
data: { | ||
pid: pid, | ||
content: args | ||
}, | ||
success: function (json) { | ||
loading.close(); | ||
if (json.code == 1) { | ||
dialog.success(json.msg) | ||
prmpt.close() | ||
setTimeout(function () { | ||
location.reload() | ||
}, 1500); | ||
} else { | ||
dialog.error(json.msg) | ||
} | ||
} | ||
}) | ||
return false; | ||
}) | ||
}) | ||
}) | ||
</script> | ||
{/block} |
Oops, something went wrong.