自定义配置
目的
实现后台设置自定义模块设置,然后在前台显示,当前比较流行的操作,但是我还不知道别人是怎么操作的;以下将说明我自己的方法。
数据库建表
-- -------- 模块表 -----------
CREATE TABLE `jn_auth_module` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '' COMMENT '模块名称',
`name_cn` varchar(255) NOT NULL DEFAULT '' COMMENT '模块中文明名称',
`add_time` int(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-- ------- 模块字段表 从属于模块---------
CREATE TABLE `jn_auth_module_field` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`module_id` int(11) NOT NULL DEFAULT '0' COMMENT '关联的认证模块ID',
`name` varchar(20) NOT NULL DEFAULT '' COMMENT '字段名',
`name_cn` varchar(30) NOT NULL DEFAULT '' COMMENT '字段名 中文意思',
`is_must` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否是必填字段',
`add_time` int(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
-- ---------- 商户后台配置表 ------
CREATE TABLE `jn_merchant_auth_deploy` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`merchant_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '商户ID',
`body` text NOT NULL COMMENT '商户认证模块配置',
`add_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '商户添加该认证的时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 COMMENT='商户 对于认证模块的配置部署';
代码
前台页面
由于没有使用框架,mode与view耦合性比较高
<?
include_once "class/config.php";
include "class/skysee_check.php";
require_once 'util/db/Medoo.php';
use MedooMedoo;
$medoo = new Medoo(Medoo::JN_DB);
$auth_modules = $medoo->select('jn_auth_module',['id','name','name_cn']);
$auth_deploy = $medoo->get('jn_merchant_auth_deploy',['body'],['merchant_id'=>$bid]);
$auth_deploy = ($auth_deploy && !empty($auth_deploy)) ? json_decode($auth_deploy['body'], true) : DEFAULT_AUTH_DEPLOY;
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>认证设置</title>
<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="js/jquery1.9.1.js"></script>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<link rel="stylesheet" type="text/css" href="js/layui/css/layui.css" />
<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="js/layui/layui.js"></script>
<style>
.settings_box {
width: 75%;
background-color: #FFF;
padding: 35px;
float: left;
margin-top: 30px;
margin-left: 7%;
margin-right: 4%;
}
.layui-form-item{
margin-bottom: 0;
}
.layui-input-block{
min-height: 38px;
height: 30px;
}
.layui-btn{
background-color: #0096EC;
}
</style>
<script type="text/javascript">
window.onload=function(){
get_notify();
}
</script>
</head>
<body>
<audio id="music">
<source class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="img/remind-wav.wav" type="audio/wav">
</audio>
<?php
$flow_data = [];
$user = $_COOKIE['shop_user'];
$sql2 = "SELECT vip from `jn_merchant_user` where user='$user'";
$result1 = $skysee->query($sql2);
foreach ($result1 as $data2) {
$vip = $data2["vip"];
}
if ($vip == '2') {
echo '<script>location.href="free.html";</script>';
exit();
}
?>
<div id="response"></div>
<div class="core_right">
<?include_once"top.php";?>
<?php
// 验证权限
$user_phone = $_COOKIE["shop_user"];
$sql2 = "SELECT id from `jn_merchant_user` where user='" . $user_phone . "'";
$result1 = $skysee->query($sql2);
foreach ($result1 as $data2) {
$m_uid = $data2["id"];
}
if (empty($m_uid)) {
echo '<script>top.location.href="/m/manage";</script>';
exit();
}
?>
<div class="dh_an_02" style="padding-bottom: 15px;">
<div class="right_nav">
<li class="right_nav_01" style="text-align: center;"><a
href="working.php">首页</a></li>
<li class="right_nav_02">/</li>
<li class="right_nav_01" style="text-align: center; width: 88px;"><a
href="#">认证设置</a></li>
<li class="right_nav_01">/</li>
</div>
</div>
</div>
<div class="settings_box">
<form class="layui-form " >
<input type="hidden" name="bid" value="<?=$bid ?>" />
<?php foreach ( $auth_modules as $auth_module):
$is_add_module = isset($auth_deploy[$auth_module['name']]['switch']) && $auth_deploy[$auth_module['name']]['switch']=='on';?>
<fieldset class="layui-elem-field">
<legend><?=$auth_module['name_cn'] ?></legend>
<div class="layui-field-box">
<div class="layui-form-item">
<label class="layui-form-label">开关</label>
<div class="layui-input-block">
<input type="checkbox" name="<?=$auth_module['name']?>[switch]" lay-skin="switch" lay-text="添加|关闭" lay-filter="add"
<?php if ( $is_add_module ) :?>checked<?php endif;?>>
</div>
</div>
<?php $auth_module_fields = $medoo->select('jn_auth_module_field',['name','name_cn'],['module_id'=>$auth_module['id']]);
foreach ( $auth_module_fields as $auth_module_field ):
$is_add_field = isset($auth_deploy[$auth_module['name']][$auth_module_field['name']]) &&
$auth_deploy[$auth_module['name']][$auth_module_field['name']] == 'on' ;?>
<div class="layui-form-item" <?php if ( !$is_add_module ):?>style="display: none;" <?php endif;?>>
<div class="layui-input-block">
<input type="checkbox" name="<?=$auth_module['name'] ?>[<?=$auth_module_field['name']?>]" title="<?=$auth_module_field['name_cn']?>" lay-skin="primary"
<?php if ( $is_add_field ) :?>checked<?php endif;?>>
</div>
</div>
<?php endforeach; ?>
</div>
</fieldset>
<?php endforeach;?>
<fieldset class="layui-elem-field layui-field-title" style="margin-top:20px ; float: right;">
<div class="layui-field-box">
<div class="layui-form-item">
<button class="layui-btn" lay-submit lay-filter="formDemo">保存</button>
</div>
</div>
</fieldset>
</form>
</div>
<?php $medoo->pdo = null;?>
<script>
//Demo
layui.use(['form','element'], function(){
var form = layui.form,layer = layui.layer;
form.on('switch(add)',function (data) {
if (data.elem.checked===true){
$(data.elem).parent().parent().nextAll().show();
} else{
$(data.elem).parent().parent().nextAll().hide();
}
});
// form.verify({});
//监听提交
form.on('submit(formDemo)', function(data){
console.log(data.field);
$.ajax({
type:"POST",
url:"shopclass/auth_deploy.php",
data: data.field,
dataType:"html",
success:function(html){
$('#response').html(html);
location.href='auth_deploy.php';
}
});
return false;
});
});
</script>
</body>
</html>
后台页面
<?php
require_once '../util/db/Medoo.php';
use MedooMedoo;
/**
* 目前有[运营商, 个人信息, 芝麻分, 淘宝, 网银, 公积金] 认证
* @var array
*/
const AUTH_MODULES = ['telecom','profile','sesame','taobao','netbank','fund'];
$medoo = new Medoo(Medoo::JN_DB);
// merchant_id判断
if( ! empty($_POST['bid']) && $_POST['bid'] !='undefined' )
{
$merchant_id = $_POST['bid'];
$deploy_body = [];
foreach ( AUTH_MODULES as $auth_module )
{
if ( isset($_POST[$auth_module]['switch']) && $_POST[$auth_module]['switch'] === 'on' )
{
$deploy_body[$auth_module] = $_POST[$auth_module];
}
}
$has = $medoo->has('jn_merchant_auth_deploy', ['merchant_id'=>$merchant_id]);
if ( $has ){
$medoo->update('jn_merchant_auth_deploy',['body'=>json_encode($deploy_body), 'add_time'=>time()], ['merchant_id'=>$merchant_id]);
}else{
$medoo->insert( 'jn_merchant_auth_deploy',['merchant_id'=>$merchant_id,'body'=>json_encode($deploy_body),'add_time'=>time()] );
}
if ( $medoo->error()[1] === null ){
echo "<script>alert('保存成功!');</script>";
}else{
$err = $medoo->error()[2] ;
echo "<script>alert('数据库保存失败!');console.log('$err')</script>";
}
}
else
{
print "<script>alert('商户ID无效,请重新登录尝试!');</script>";
}
//var_dump($_POST);
最后后台效果
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦