通过微擎作为跳板,在菜单栏中也就是site.php文件中可以通过浏览器中的菜单链接直接触发doWebAdmin函数,然后安装数据库,并且改变sql文件的后后缀,实现sql导入。然后通过路由跳转,直接跳转到自己写的后台当中即可。
private $dbhost;
private $dbusername;
private $dbpassword;
private $dbdatabase;
private $dbtype;
public $sql_url;
public $lock_url;
public $dp_url;
public $db_url;
public function __construct(){
global $_W;
$this->sql_url = IA_ROOT .'/addons/'.$_W['current_module']['name'].'/sql/install.sql';
$this->lock_url = IA_ROOT .'/addons/'.$_W['current_module']['name'].'/sql/install.lock';
$this->dp_url = '../addons/'.$_W['current_module']['name'].'/water/';
$configfile = IA_ROOT . "/data/config.php";
require $configfile;
if(isset($config['db']['master'])){
//新版微擎
$this->dbhost=$config['db']['master']['host'];
$this->dbusername=$config['db']['master']['username'];
$this->dbpassword=$config['db']['master']['password'];
$this->dbdatabase=$config['db']['master']['database'];
}else{
//旧版微擎
$this->dbhost=$config['db']['host'];
$this->dbusername=$config['db']['username'];
$this->dbpassword=$config['db']['password'];
$this->dbdatabase=$config['db']['database'];
}
}
public function doWebAdmin() {
//这个操作被定义用来呈现 管理中心导航菜单
global $_W;
//判断用户是否有卸载模块卸载时则改变文件名
$is_sql = pdo_fetchall("SELECT id FROM kejia_water_admin_user", array(), 'id');
if(empty($is_sql)){
rename($this->lock_url,$this->sql_url);
}
// 运行SQL文件
$this->create_tables();
$session=[
'user_auth' =>
[
'uid' => '1',
'group' => '0',
'role' => '1',
'role_name' => '超級管理',
'avatar' => '0',
'username' => 'admin',
'nickname' => '管理员',
'last_login_time' => '1527995266',
'last_login_ip' => '3232243609'
],
'user_auth_sign' => 'b1fd0fb986ab7757a811d5e9030f47c4468a80b8',
'role_menu_auth' =>[],
'b50d2a4d' => [
'table' => 'admin_config',
'prefix' => 1,
'module' => 'admin',
'controller' => 'system',
'action' => 'quickedit',
],
'weiqing_uid'=>$_W['user']['uid'],
];
session_start();
$_SESSION['dolphin_admin_']=$session;
// 跳转到后台
$jump_url = $this->dp_url . 'public/admin.php?s=/water/order/index';
header("Location: ".$jump_url);
exit;
}
/**
* 创建数据表
* @param $db 数据库连接资源
* @param string $prefix 表前缀
*/
private function create_tables(){
global $_W;
if(is_file($this->sql_url)){
$db = new mysqli($this->dbhost,$this->dbusername,$this->dbpassword,$this->dbdatabase); //我的数据库
if (mysqli_connect_errno()){
echo "Error: could not connect the database;<br>";
exit;
}
$sql = file_get_contents($this->sql_url);
$a = explode(";", $sql);
foreach ($a as $b) {
$c = $b.";";
$d = $db->query($c);
}
rename($this->sql_url,$this->lock_url);
}
}