这是一个接口分类和本站分类进行绑定的表单,把绑定结果写到了一个文件中。
页面效果如下图
* 绑定情况渲染页面
* @return string
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function bindcats()
{
$get = $this->request->get();
$api_info = $this->model->where('id',$get['id'])
->find();
$categorys = Custom::apiget($api_info,'category',[]);
$file = APP_PATH . 'extra/catbind_'.$api_info['name'].'.php';
$type ='';
$binds = [];
if(file_exists($file)){
$binds = require($file);
/**
* 左边是api的id,右边是项目本地的分类id
* return array (
* 70 => '89',
* 69 => '61',
* 68 => '64',
* 67 => '59'
* );
*/
}
foreach ($categorys as $key=>$value){
$categorys[$key]['bind_id'] = isset($binds[$value['id']]) ? $binds[$value['id']] : '';
}
$lang_categorys = Custom::apiget($api_info,'lang',[]);
$file = APP_PATH . 'extra/langbind_'.$api_info['name'].'.php';
$type ='';
$binds = [];
if(file_exists($file)){
$binds = require($file);
/**
* 左边是api的id,右边是项目本地的语言id
* return array (
* 70 => '89',
* 69 => '61',
* 68 => '64',
* 67 => '59'
* );
*/
}
foreach ($lang_categorys as $key=>$value){
$lang_categorys[$key]['bind_id'] = isset($binds[$value['id']]) ? $binds[$value['id']] : '';
}
//根据语言整理成分组
foreach ($lang_categorys as $key=>$value){
$child = [];
foreach ($categorys as $k=>$v){
if($v['language_mark'] == $value['mark']){
$child[] = $v;
unset($categorys[$k]);
}
}
$lang_categorys[$key]['child'] = $child;
unset($child);
if($key == 0){
$lang_categorys[$key]['active'] = 1;
}else{
$lang_categorys[$key]['active'] = 0;
}
}
$this->view->assign('lang_categorys', $lang_categorys);
$this->view->assign('apiname', $api_info['name']);
return $this->view->fetch();
}
public function dobind(){
if ($this->request->isPost()) {
$this->token();
$apiname = $this->request->post('apiname');
$file = APP_PATH . 'extra/catbind_'.$apiname.'.php';
$bind_data = $this->request->post("bind/a", [], 'trim');
// dump($bind_data);die;
if ($bind_data) {
foreach($bind_data as $v){
$k= trim($v['key']);
if($v['val'])
$data[$k]=$v['val'];
}
file_put_contents($file,
'<?php' . "\n\nreturn " . var_export_short($data) . ";\n"
);
$this->success();
}
$this->error();
}
}
<style type="text/css">
/* 保持原有样式不变 */
@media (max-width: 375px) {
.edit-form tr td input {
width: 100%;
}
.edit-form tr th:first-child, .edit-form tr td:first-child {
width: 20%;
}
.edit-form tr th:nth-last-of-type(-n+2), .edit-form tr td:nth-last-of-type(-n+2) {
display: none;
}
}
.edit-form table > tbody > tr td a.btn-delcfg {
visibility: hidden;
}
.edit-form table > tbody > tr:hover td a.btn-delcfg {
visibility: visible;
}
.btn,.btn-danger,.btn-xs,.btn-trash{
display: none !important;
}
</style>
<form id="set-form" role="form" data-toggle="validator" method="POST" action="{:url('dobind')}">
{:token()}
<input name="apiname" type="hidden" value="{$apiname}">
<div class="panel-heading">
<!-- 标签页导航 -->
<ul class="nav nav-tabs" role="tablist">
{foreach $lang_categorys as $key=>$vo}
<li role="presentation" class="{$vo.active?'active':''}">
<a href="#tab-{$vo.mark}" aria-controls="tab-{$vo.mark}" role="tab" data-toggle="tab">
{:__($vo.name)}
</a>
</li>
{/foreach}
</ul>
<!-- 标签页内容 -->
<div class="tab-content">
{foreach $lang_categorys as $key=>$vo}
<div role="tabpanel" class="tab-pane fade {if $vo.active}active in{/if}" id="tab-{$vo.mark}">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>语言标识</th>
<th width="250">绑定分类</th>
</tr>
</thead>
<tbody>
{foreach $vo.child as $index=>$item}
<tr>
<td>{$item.id}</td>
<td>{$item.name}</td>
<td>{$item.language_mark}</td>
<td>
<div class="form-group">
<input name="bind[{$item.id}][key]" type="hidden" value="{$item.id}">
{empty name="$vo.bind_id"}
<input name="bind[{$item.id}][val]" type="text" value="{$item.bind_id}" data-source="category/index?type=video" data-params='{"custom[type]":"video"}' class="form-control selectpage">
{else /}
<input name="bind[{$item.id}][val]" type="text" value="{$item.bind_id}" data-source="category/index?type=video" data-params='{"custom[type]":"video","custom[lang_id]":"{$vo.bind_id}"}' class="form-control selectpage">
{/empty}
</div>
</td>
</tr>
{/foreach}
</tbody>
</table>
</div>
{/foreach}
</div>
</div>
<div class="form-group layer-footer">
<label class="control-label col-xs-12 col-sm-4"></label>
<div class="col-xs-12 col-sm-8">
<button type="submit" class="btn btn-primary btn-embossed disabled">{:__('OK')}</button>
</div>
</div>
</form>
绑定后生成的文件
<?php
return array (
70 => '89',
69 => '61',
68 => '64',
67 => '59',
66 => '65',
65 => '64',
64 => '89',
33 => '61',
43 => '62',
42 => '89',
41 => '58',
);