feat: init project codebase
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
use Restserver\Libraries\REST_Controller;
|
||||
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
//To Solve File REST_Controller not found
|
||||
require APPPATH . 'libraries/REST_Controller.php';
|
||||
require APPPATH . 'libraries/Format.php';
|
||||
|
||||
class Article extends REST_Controller
|
||||
{
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->model('Base_model');
|
||||
// $this->load->model('Record_model');
|
||||
// $this->load->model('Dept_model', 'Dept');
|
||||
// $this->config->load('config', true);
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$this->load->view('login_view');
|
||||
}
|
||||
|
||||
public function testapi()
|
||||
{
|
||||
echo "test api ok...";
|
||||
}
|
||||
|
||||
public function phpinfo()
|
||||
{
|
||||
phpinfo();
|
||||
}
|
||||
|
||||
public function testdb()
|
||||
{
|
||||
$this->load->database();
|
||||
$query = $this->db->query("show tables");
|
||||
var_dump($query);
|
||||
var_dump($query->result());
|
||||
var_dump($query->row_array());
|
||||
// 有结果表明数据库连接正常 reslut() 与 row_array 结果有时不太一样
|
||||
// 一般加载到时model里面使用。
|
||||
}
|
||||
|
||||
|
||||
function list_get()
|
||||
{
|
||||
|
||||
$items = [
|
||||
["id" => 1,
|
||||
"timestamp" => "20180808",
|
||||
|
||||
"author" => "qiaokun",
|
||||
"reviewer" => "draft",
|
||||
"title" => "draft",
|
||||
"content_short" => "我是测试数据",
|
||||
"content" => "<p>我是测试数据我是测试数据</p><p><img src=\"https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943\"></p>",
|
||||
"forecast" => '3.1515',
|
||||
"importance" => '1',
|
||||
"type|1" => 'CN',
|
||||
"status" => 'published',
|
||||
"display_time" => "",
|
||||
"comment_disabled" => true,
|
||||
"pageviews" => 300,
|
||||
"image_uri" => "https://wpimg.wallstcn.com/e4558086-631c-425c-9430-56ffb46e70b3'",
|
||||
"platforms" =>"a-platform"
|
||||
],
|
||||
["id" => 2,
|
||||
"timestamp" => "20180808",
|
||||
|
||||
"author" => "qiaokun",
|
||||
"reviewer" => "draft",
|
||||
"title" => "draft",
|
||||
"content_short" => "我是测试数据",
|
||||
"content" => "<p>我是测试数据我是测试数据</p><p><img src=\"https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943\"></p>",
|
||||
"forecast" => '3.1515',
|
||||
"importance" => '1',
|
||||
"type|1" => 'CN',
|
||||
"status" => 'published',
|
||||
"display_time" => "",
|
||||
"comment_disabled" => true,
|
||||
"pageviews" => 300,
|
||||
"image_uri" => "https://wpimg.wallstcn.com/e4558086-631c-425c-9430-56ffb46e70b3'",
|
||||
"platforms" =>"a-platform"
|
||||
],
|
||||
];
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => [
|
||||
"items" => $items,
|
||||
"total" => count($items)
|
||||
]
|
||||
|
||||
];
|
||||
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
|
||||
}
|
||||
|
||||
function goods_get()
|
||||
{
|
||||
|
||||
$items = array(
|
||||
array('id' => 1, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 2, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 3, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 4, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 5, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 6, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 7, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 8, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 9, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 10, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 11, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 31, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 13, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 24, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 35, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 19, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 22, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 33, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
);
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => [
|
||||
"items" => $items
|
||||
]
|
||||
];
|
||||
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* End of file welcome.php */
|
||||
/* Location: ./application/controllers/welcome.php */
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
use Restserver\Libraries\REST_Controller;
|
||||
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
//To Solve File REST_Controller not found
|
||||
require APPPATH . 'libraries/REST_Controller.php';
|
||||
require APPPATH . 'libraries/Format.php';
|
||||
|
||||
class Table extends REST_Controller
|
||||
{
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->model('Base_model');
|
||||
// $this->load->model('Record_model');
|
||||
// $this->load->model('Dept_model', 'Dept');
|
||||
// $this->config->load('config', true);
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$this->load->view('login_view');
|
||||
}
|
||||
|
||||
public function testapi()
|
||||
{
|
||||
echo "test api ok...";
|
||||
}
|
||||
|
||||
public function phpinfo()
|
||||
{
|
||||
phpinfo();
|
||||
}
|
||||
|
||||
public function testdb()
|
||||
{
|
||||
$this->load->database();
|
||||
$query = $this->db->query("show tables");
|
||||
var_dump($query);
|
||||
var_dump($query->result());
|
||||
var_dump($query->row_array());
|
||||
// 有结果表明数据库连接正常 reslut() 与 row_array 结果有时不太一样
|
||||
// 一般加载到时model里面使用。
|
||||
}
|
||||
|
||||
|
||||
function list_get()
|
||||
{
|
||||
|
||||
$items = [
|
||||
["id" => 1,
|
||||
"title" => "www",
|
||||
"status" => "draft",
|
||||
"author" => "qiaokun",
|
||||
"display_time" => "",
|
||||
"pageviews" => 300
|
||||
],
|
||||
["id" => 3,
|
||||
"title" => "bbb",
|
||||
"status" => "bbbb",
|
||||
"author" => "乔锟",
|
||||
"display_time" => "",
|
||||
"pageviews" => 300
|
||||
],
|
||||
];
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => [
|
||||
"items" => $items
|
||||
]
|
||||
];
|
||||
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
|
||||
}
|
||||
|
||||
function goods_get()
|
||||
{
|
||||
|
||||
$items = array(
|
||||
array('id' => 1, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 2, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 3, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 4, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 5, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 6, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 7, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 8, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 9, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 10, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 11, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 31, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 13, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 24, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 35, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 19, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 22, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 33, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
);
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => [
|
||||
"items" => $items
|
||||
]
|
||||
];
|
||||
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* End of file welcome.php */
|
||||
/* Location: ./application/controllers/welcome.php */
|
||||
@@ -0,0 +1,418 @@
|
||||
<?php
|
||||
|
||||
use Restserver\Libraries\REST_Controller;
|
||||
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
//To Solve File REST_Controller not found
|
||||
require APPPATH . 'libraries/REST_Controller.php';
|
||||
require APPPATH . 'libraries/Format.php';
|
||||
|
||||
//require APPPATH . 'libraries/kindeditor/php/JSON.php';
|
||||
|
||||
class Uploadimg extends REST_Controller
|
||||
{
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->model('Base_model');
|
||||
}
|
||||
|
||||
|
||||
public function testapi_get()
|
||||
{
|
||||
echo "test api ok...";
|
||||
|
||||
echo APPPATH . "\n";
|
||||
echo SELF . "\n";
|
||||
echo BASEPATH . "\n";
|
||||
echo FCPATH . "\n";
|
||||
echo SYSDIR . "\n";
|
||||
var_dump($this->config->item('rest_language'));
|
||||
var_dump($this->config->item('language'));
|
||||
|
||||
var_dump($this->config);
|
||||
}
|
||||
|
||||
public function upload_post()
|
||||
{
|
||||
$uploadDir = FCPATH . 'uploads/';
|
||||
$id = 'T' . $this->POST('identify');
|
||||
$php_path = dirname(__FILE__) . '/';//dirname($_SERVER['DOCUMENT_ROOT']); dirname(__FILE__)
|
||||
// $php_url = "";//dirname($_SERVER['HTTP_HOST']) . '/';//PHP_SELF
|
||||
$php_url = $_SERVER['HTTP_HOST'] . '/'; //PHP_SELF
|
||||
|
||||
$save_path = $uploadDir;
|
||||
//文件保存目录URL
|
||||
$save_url = $php_url . 'uploads/';
|
||||
// nginx服务器端修改绝对路径
|
||||
//$save_url = "http://172.30.3.11/static/home/kindeditor/attached/";
|
||||
|
||||
// var_dump($save_path);
|
||||
// var_dump($save_url);
|
||||
// string(46) "D:\Q\code\vue\CodeIgniter-3.1.10\uploads\imgs\"
|
||||
// string(28) "www.cirest.com:8889/uploads/"
|
||||
|
||||
|
||||
//定义允许上传的文件扩展名
|
||||
$ext_arr = array(
|
||||
'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
|
||||
'flash' => array('swf', 'flv'),
|
||||
'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
|
||||
'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
|
||||
);
|
||||
//最大文件大小 10M 默认是1M
|
||||
$max_size = 10000000;
|
||||
|
||||
$save_path = realpath($save_path) . '/';
|
||||
$save_path = str_replace('\\', '/', $save_path);
|
||||
|
||||
//PHP上传失败
|
||||
if (!empty($_FILES['file']['error'])) {
|
||||
switch ($_FILES['file']['error']) {
|
||||
case '1':
|
||||
$error = '超过php.ini允许的大小。';
|
||||
break;
|
||||
case '2':
|
||||
$error = '超过表单允许的大小。';
|
||||
break;
|
||||
case '3':
|
||||
$error = '图片只有部分被上传。';
|
||||
break;
|
||||
case '4':
|
||||
$error = '请选择图片。';
|
||||
break;
|
||||
case '6':
|
||||
$error = '找不到临时目录。';
|
||||
break;
|
||||
case '7':
|
||||
$error = '写文件到硬盘出错。';
|
||||
break;
|
||||
case '8':
|
||||
$error = 'File upload stopped by extension。';
|
||||
break;
|
||||
case '999':
|
||||
default:
|
||||
$error = '未知错误。';
|
||||
}
|
||||
$this->alert($error);
|
||||
}
|
||||
|
||||
//有上传文件时
|
||||
if (empty($_FILES) === false) {
|
||||
//原文件名
|
||||
$file_name = $_FILES['file']['name'];
|
||||
//服务器上临时文件名
|
||||
$tmp_name = $_FILES['file']['tmp_name'];
|
||||
//文件大小
|
||||
$file_size = $_FILES['file']['size'];
|
||||
//检查文件名
|
||||
if (!$file_name) {
|
||||
$this->alert("请选择文件。");
|
||||
}
|
||||
//检查目录
|
||||
if (@is_dir($save_path) === false) {
|
||||
$this->alert("上传目录不存在。");
|
||||
}
|
||||
//检查目录写权限
|
||||
if (@is_writable($save_path) === false) {
|
||||
$this->alert("上传目录没有写权限。");
|
||||
}
|
||||
//检查是否已上传
|
||||
if (@is_uploaded_file($tmp_name) === false) {
|
||||
$this->alert("上传失败。");
|
||||
}
|
||||
//检查文件大小
|
||||
if ($file_size > $max_size) {
|
||||
$this->alert("上传文件大小超过限制(<10M)。");
|
||||
}
|
||||
//检查目录名
|
||||
$dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
|
||||
if (empty($ext_arr[$dir_name])) {
|
||||
$this->alert("目录名不正确。");
|
||||
}
|
||||
//获得文件扩展名
|
||||
$temp_arr = explode(".", $file_name);
|
||||
$file_ext = array_pop($temp_arr);
|
||||
$file_ext = trim($file_ext);
|
||||
$file_ext = strtolower($file_ext);
|
||||
|
||||
//检查扩展名
|
||||
if (!in_array($file_ext, $ext_arr[$dir_name])) {
|
||||
$this->alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr[$dir_name]) . "格式。");
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* 以 T+身份证号作为临时目录 , 以身份证作为正式目录
|
||||
*/
|
||||
$identify = empty($id) ? '' : trim($id);
|
||||
|
||||
if ($identify == '') {
|
||||
echo "Invalid session identify.";
|
||||
exit;
|
||||
}
|
||||
//创建文件夹
|
||||
if ($dir_name !== '') {
|
||||
$save_path .= $dir_name . "/" . $identify . "/";
|
||||
$save_url .= $dir_name . "/" . $identify . "/";
|
||||
if (!file_exists($save_path)) {
|
||||
mkdir($save_path, 0777, true); // true 允许创建多级目录
|
||||
}
|
||||
}
|
||||
$ymd = date("Ym");
|
||||
$save_path .= $ymd . "/";
|
||||
$save_url .= $ymd . "/";
|
||||
if (!file_exists($save_path)) {
|
||||
mkdir($save_path);
|
||||
}
|
||||
//新文件名
|
||||
$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
|
||||
//移动文件
|
||||
$file_path = $save_path . $new_file_name;
|
||||
if (move_uploaded_file($tmp_name, $file_path) === false) {
|
||||
$this->alert("上传文件失败。");
|
||||
}
|
||||
@chmod($file_path, 0644);
|
||||
$file_url = $save_url . $new_file_name;
|
||||
|
||||
header('Content-type: text/html; charset=UTF-8');
|
||||
// Insert file information in the database
|
||||
// $insert = $db->query("INSERT INTO files (file_name, uploaded_on) VALUES ('".$fileName."', NOW())");
|
||||
$link = "http://" . $file_url;
|
||||
// http://www.cirest.com:8889/uploads/image/T410000000000000000/201902/20190228071354_96833.png
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"error" => 0,
|
||||
"message" => "上传成功",
|
||||
"link" => $link,
|
||||
"filepath" => preg_replace('/^http.*uploads/', '/uploads', $link)
|
||||
];
|
||||
|
||||
echo json_encode($message);
|
||||
// $this->set_response($message, REST_Controller::HTTP_OK);
|
||||
// alert里面使用时 exit() 产生的是空,或字符串,使用原生的json_encode返回统一的字符串,在客户端在统一处理成对象
|
||||
}
|
||||
}
|
||||
|
||||
public function delimg_post()
|
||||
{
|
||||
$php_path = dirname(__FILE__) . '/';//dirname($_SERVER['DOCUMENT_ROOT']); dirname(__FILE__)
|
||||
|
||||
//文件保存目录路径
|
||||
$save_path = $php_path . '../../../../';
|
||||
|
||||
$save_path = realpath($save_path) . '/';
|
||||
$save_path = str_replace('\\', '/', $save_path);
|
||||
// var_dump($save_path);
|
||||
// "D:/Q/code/vue/CodeIgniter-3.1.10/"
|
||||
|
||||
$DelFileName = $this->POST('filename');
|
||||
$DelFileType = $this->POST('isdir');
|
||||
|
||||
$FilePath = $save_path . $DelFileName;
|
||||
|
||||
// var_dump($FilePath);return;
|
||||
|
||||
if ($DelFileType == 'F') {
|
||||
if (!is_file($FilePath)) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => "文件不存在 - " . $DelFileName,
|
||||
"message" => "文件不存在 - " . $DelFileName
|
||||
];
|
||||
|
||||
echo json_encode($message);
|
||||
|
||||
} else {
|
||||
|
||||
if (!unlink($FilePath)) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => "Error deleting " . $FilePath,
|
||||
"message" => "Error deleting " . $FilePath
|
||||
];
|
||||
echo json_encode($message);
|
||||
|
||||
} else {
|
||||
// 必须返回code 由于前端vue封装的 request 请求,返回数据对code进行判断
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => '服务器删除成功!',
|
||||
"message" => '服务器删除成功!'
|
||||
];
|
||||
echo json_encode($message);
|
||||
}
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($DelFileType == 'D') {
|
||||
if (!@rmdir($FilePath)) {
|
||||
echo "文件夹 " . $FilePath . " 不为空,不能删除!";
|
||||
} else {
|
||||
echo "succeed";
|
||||
}
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
private function alert($msg)
|
||||
{
|
||||
header('Content-type: text/html; charset=UTF-8');
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"error" => 1,
|
||||
"message" => $msg
|
||||
];
|
||||
echo json_encode($message);
|
||||
exit();
|
||||
// var_dump($message);
|
||||
// $this->set_response($message, REST_Controller::HTTP_OK);
|
||||
// die();
|
||||
}
|
||||
|
||||
|
||||
public function onsubmit_post()
|
||||
{
|
||||
$identify = $this->POST('identify');
|
||||
$phone = $this->POST('phone');
|
||||
$idinfo = $this->POST('idinfo');
|
||||
$bankinfo = $this->POST('bankinfo');
|
||||
// $data = [
|
||||
// 'identify' => $identify,
|
||||
// 'phone' => $phone,
|
||||
// 'idinfo' => $idinfo,
|
||||
// 'check' => '待审核'
|
||||
// ];
|
||||
|
||||
// 写入数据库表 身份证号,手机号,证件照,文件路径等
|
||||
$where = [
|
||||
'identify' => $identify,
|
||||
'phone' => $phone,
|
||||
];
|
||||
|
||||
$data = [
|
||||
'idinfo' => $idinfo,
|
||||
'bankinfo' => $bankinfo,
|
||||
'check' => '待审核'
|
||||
];
|
||||
|
||||
$result = $this->Base_model->_update_key('upload_tbl', $data, $where);
|
||||
|
||||
if ($result) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"message" => '写入数据库表成功,请请待审核通知!',
|
||||
"data" => array_merge($where, $data)
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
} else {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"message" => '写入数据库表失败!',
|
||||
"data" => array_merge($where, $data)
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
}
|
||||
|
||||
// Chevereto 图床免费版本地址:https://github.com/Chevereto/Chevereto-Free
|
||||
// https://chevereto.com/docs/api-v1
|
||||
// 上传图片测试
|
||||
public function chevereto_post()
|
||||
{
|
||||
// 1. 上传本地文件时应使用 $fields post base64_encode方法
|
||||
// Always use POST when uploading local files. Url encoding may alter the base64 source
|
||||
// due to encoded characters or just by URL request length limit due to GET request.
|
||||
// base64编码 会导致过长 url request
|
||||
// var_dump($_FILES); 参考 uploadimg 可以做一些前置校验处理 // 前置判断 if (empty($_FILES) === false)
|
||||
$key = '5486424e4dfb6b87453dd4bb25c0dcb0';
|
||||
$url = 'http://172.17.1.110/chevereto/api/1/upload';
|
||||
|
||||
// What do we send to chevereto api?
|
||||
$fields = array(
|
||||
'key' => urlencode($key),
|
||||
// The image encoded in base64
|
||||
'source' => base64_encode(file_get_contents($_FILES["file"]['tmp_name'])),
|
||||
// format: txt / json txt 只返回图片地址或错误信息 eg.Duplicated upload 较为简洁
|
||||
'format' => urlencode('json')
|
||||
);
|
||||
|
||||
//open connection
|
||||
$ch = curl_init();
|
||||
|
||||
//set the url, number of POST vars, POST data
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_POST, sizeof($fields));
|
||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 240);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
|
||||
|
||||
//execute post
|
||||
$result = curl_exec($ch);
|
||||
//释放curl句柄
|
||||
curl_close($ch);
|
||||
echo $result;
|
||||
//close connection
|
||||
|
||||
// 2. 上传远程图片地址可使用 _get source=urlencode{source} 方法即可
|
||||
|
||||
// $key = '6a55c7f9fa13813c2da613dc7b5b920b';
|
||||
// // 设定远程图片地址
|
||||
// $source = 'https://img3.doubanio.com/view/group_topic/large/public/p67032015.jpg';
|
||||
// // format: txt / json txt 只返回图片地址或错误信息 eg.Duplicated upload 较为简洁
|
||||
// $url = 'http://172.17.1.110:8888/api/1/upload/?key={key}&source={source}&format=txt';
|
||||
// $url = str_replace(array('{key}','{source}'),array($key,urlencode($source)),$url);
|
||||
//
|
||||
// //初始化
|
||||
// $ch = curl_init();
|
||||
// //设置选项,包括URL
|
||||
// curl_setopt($ch, CURLOPT_URL, $url);
|
||||
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
// curl_setopt($ch, CURLOPT_HEADER, 0);
|
||||
// //执行并获取HTML文档内容
|
||||
// $output = curl_exec($ch);
|
||||
// //释放curl句柄
|
||||
// curl_close($ch);
|
||||
// echo $output;
|
||||
|
||||
|
||||
// 失败
|
||||
// {
|
||||
// "status_txt": "Bad Request",
|
||||
// "error": {
|
||||
// "context": "Exception",
|
||||
// "code": 102,
|
||||
// "message": "Duplicated upload"
|
||||
// },
|
||||
// "status_code": 400
|
||||
// }
|
||||
// 成功
|
||||
// {
|
||||
// "status_txt": "OK",
|
||||
// "image": {
|
||||
// "image": {
|
||||
// "size": "89163",
|
||||
// "url": "http://172.17.1.110:8888/images/2019/07/09/p67032015.jpg",
|
||||
// "extension": "jpg",
|
||||
// "mime": "image/jpeg",
|
||||
// "name": "p67032015",
|
||||
// "filename": "p67032015.jpg"
|
||||
// },
|
||||
// },
|
||||
// "success": {
|
||||
// "code": 200,
|
||||
// "message": "image uploaded"
|
||||
// },
|
||||
// "status_code": 200
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,358 @@
|
||||
<?php
|
||||
|
||||
use Restserver\Libraries\REST_Controller;
|
||||
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
//To Solve File REST_Controller not found
|
||||
require APPPATH . 'libraries/REST_Controller.php';
|
||||
require APPPATH . 'libraries/Format.php';
|
||||
|
||||
class Menu extends REST_Controller
|
||||
{
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->model('Base_model');
|
||||
// $this->config->load('config', true);
|
||||
}
|
||||
|
||||
public function index_get()
|
||||
{
|
||||
$this->load->view('login_view');
|
||||
}
|
||||
|
||||
public function insertx_post()
|
||||
{
|
||||
// $id = $this->post('id'); // POST param
|
||||
$parms = $this->post(); // 获取表单参数,类型为数组
|
||||
var_dump($parms);
|
||||
$result = $this->Base_model->_insert_key('sys_role_perm', $parms);
|
||||
var_dump($result);
|
||||
}
|
||||
|
||||
public function gettest_post()
|
||||
{
|
||||
$result = $this->Base_model->_get_key('sys_perm', 'perm_type,r_id rid', 'perm_type="role" and r_id=1');
|
||||
var_dump($result);
|
||||
var_dump($result[0]['perm_type']);
|
||||
var_dump($this->uri->uri_string);
|
||||
var_dump($this->uri);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function testapi_get()
|
||||
{
|
||||
echo "test api ok...";
|
||||
|
||||
echo APPPATH . "\n";
|
||||
echo SELF . "\n";
|
||||
echo BASEPATH . "\n";
|
||||
echo FCPATH . "\n";
|
||||
echo SYSDIR . "\n";
|
||||
var_dump($this->config->item('rest_language'));
|
||||
var_dump($this->config->item('language'));
|
||||
|
||||
var_dump($this->config);
|
||||
|
||||
// $message = [
|
||||
// "code" => 20000,
|
||||
// "data" => [
|
||||
// "__FUNCTION__" => __FUNCTION__,
|
||||
// "__CLASS__" => __CLASS__,
|
||||
// "uri" => $this->uri
|
||||
// ],
|
||||
//
|
||||
// ];
|
||||
// "data": {
|
||||
// "__FUNCTION__": "router_get",
|
||||
// "__CLASS__": "User",
|
||||
// "uri": {
|
||||
// "keyval": [],
|
||||
// "uri_string": "api/v2/user/router",
|
||||
// "segments": {
|
||||
// "1": "api",
|
||||
// "2": "v2",
|
||||
// "3": "user",
|
||||
// "4": "router"
|
||||
// },
|
||||
}
|
||||
|
||||
public function phpinfo_get()
|
||||
{
|
||||
phpinfo();
|
||||
}
|
||||
|
||||
public function testdb_get()
|
||||
{
|
||||
$this->load->database();
|
||||
$query = $this->db->query("show tables");
|
||||
var_dump($query);
|
||||
var_dump($query->result());
|
||||
var_dump($query->row_array());
|
||||
// 有结果表明数据库连接正常 reslut() 与 row_array 结果有时不太一样
|
||||
// 一般加载到时model里面使用。
|
||||
}
|
||||
|
||||
// 增
|
||||
function add_post()
|
||||
{
|
||||
$uri = $this->uri->uri_string;
|
||||
$Token = $this->input->get_request_header('X-Token', TRUE);
|
||||
$retPerm = $this->permission->HasPermit($Token, $uri);
|
||||
if ($retPerm['code'] != 50000) {
|
||||
$this->set_response($retPerm, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
// $id = $this->post('id'); // POST param
|
||||
$parms = $this->post(); // 获取表单参数,类型为数组
|
||||
// var_dump($parms);
|
||||
// var_dump($parms['path']);
|
||||
|
||||
// 参数检验/数据预处理
|
||||
// 菜单类型为目录
|
||||
if (!$parms['type']) {
|
||||
$parms['component'] = 'Layout';
|
||||
}
|
||||
|
||||
$menu_id = $this->Base_model->_insert_key('sys_menu', $parms);
|
||||
if (!$menu_id) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'error',
|
||||
"message" => $parms['title'] . ' - 菜单添加失败'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
// 生成该菜单对应的权限: sys_perm, 权限类型为: menu, 生成唯一的 perm_id
|
||||
$perm_id = $this->Base_model->_insert_key('sys_perm', ['perm_type' => 'menu', "r_id" => $menu_id]);
|
||||
if (!$perm_id) {
|
||||
var_dump($this->uri->uri_string . ' 生成该菜单对应的权限: sys_perm, 失败...');
|
||||
var_dump(['perm_type' => 'menu', "r_id" => $menu_id]);
|
||||
return;
|
||||
}
|
||||
|
||||
// 超级管理员角色自动拥有该权限 perm_id
|
||||
$role_perm_id = $this->Base_model->_insert_key('sys_role_perm', ["role_id" => 1, "perm_id" => $perm_id]);
|
||||
if (!$role_perm_id) {
|
||||
var_dump($this->uri->uri_string . ' 超级管理员角色自动拥有该权限 perm_id, 失败...');
|
||||
var_dump(["role_id" => 1, "perm_id" => $perm_id]);
|
||||
return;
|
||||
}
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'success',
|
||||
"message" => $parms['title'] . ' - 菜单添加成功'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
// 改
|
||||
function edit_post()
|
||||
{
|
||||
$uri = $this->uri->uri_string;
|
||||
$Token = $this->input->get_request_header('X-Token', TRUE);
|
||||
$retPerm = $this->permission->HasPermit($Token, $uri);
|
||||
if ($retPerm['code'] != 50000) {
|
||||
$this->set_response($retPerm, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
// $id = $this->post('id'); // POST param
|
||||
$parms = $this->post(); // 获取表单参数,类型为数组
|
||||
// var_dump($parms['path']);
|
||||
|
||||
// 参数检验/数据预处理
|
||||
if ($parms['id'] == $parms['pid']) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'error',
|
||||
"message" => '父节点不能是自己'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
// 菜单类型为目录
|
||||
if ($parms['type'] == 0) {
|
||||
$parms['component'] = 'Layout';
|
||||
}
|
||||
// 菜单类型为功能按钮时
|
||||
if ($parms['type'] == 2) {
|
||||
$parms['component'] = '';
|
||||
$parms['icon'] = '';
|
||||
}
|
||||
|
||||
$id = $parms['id'];
|
||||
unset($parms['id']); // 择出索引id
|
||||
$where = ["id" => $id];
|
||||
|
||||
if (!$this->Base_model->_update_key('sys_menu', $parms, $where)) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'error',
|
||||
"message" => $parms['title'] . ' - 菜单更新错误'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'success',
|
||||
"message" => $parms['title'] . ' - 菜单更新成功'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
// 删
|
||||
function del_post()
|
||||
{
|
||||
$uri = $this->uri->uri_string;
|
||||
$Token = $this->input->get_request_header('X-Token', TRUE);
|
||||
$retPerm = $this->permission->HasPermit($Token, $uri);
|
||||
if ($retPerm['code'] != 50000) {
|
||||
$this->set_response($retPerm, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
$parms = $this->post(); // 获取表单参数,类型为数组
|
||||
// var_dump($parms['path']);
|
||||
|
||||
// 参数检验/数据预处理
|
||||
// 存在子节点 不能删除返回
|
||||
$hasChild = $this->Base_model->hasChildMenu($parms['id']);
|
||||
if ($hasChild) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'error',
|
||||
"message" => $parms['title'] . ' - 存在子节点不能删除'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
// 删除外键关联表 sys_role_perm , sys_perm, sys_menu
|
||||
// 1. 根据sys_menu id及'menu' 查找 perm_id
|
||||
// 2. 删除sys_role_perm 中perm_id记录
|
||||
// 3. 删除sys_perm中 perm_type='menu' and r_id = menu_id 记录,即第1步中获取的 perm_id, 一一对应
|
||||
// 4. 删除sys_menu 中 id = menu_id 的记录
|
||||
$where = 'perm_type="menu" and r_id=' . $parms['id'];
|
||||
$arr = $this->Base_model->_get_key('sys_perm', '*', $where);
|
||||
if (empty($arr)) {
|
||||
var_dump($this->uri->uri_string . ' 未查找到 sys_perm 表中记录');
|
||||
var_dump($where);
|
||||
return;
|
||||
}
|
||||
|
||||
$perm_id = $arr[0]['id']; // 正常只有一条记录
|
||||
$this->Base_model->_delete_key('sys_role_perm', ['perm_id' => $perm_id]);
|
||||
$this->Base_model->_delete_key('sys_perm', ['id' => $perm_id]);
|
||||
|
||||
// 删除基础表 sys_menu
|
||||
if (!$this->Base_model->_delete_key('sys_menu', $parms)) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'error',
|
||||
"message" => $parms['title'] . ' - 菜单删除错误'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'success',
|
||||
"message" => $parms['title'] . ' - 菜单删除成功'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
|
||||
}
|
||||
|
||||
// 查
|
||||
function view_post()
|
||||
{
|
||||
$uri = $this->uri->uri_string;
|
||||
$Token = $this->input->get_request_header('X-Token', TRUE);
|
||||
|
||||
$retPerm = $this->permission->HasPermit($Token, $uri);
|
||||
if ($retPerm['code'] != 50000) {
|
||||
$this->set_response($retPerm, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
$MenuTreeArr = $this->permission->getPermission($Token, 'menu', true);
|
||||
$MenuTree = $this->permission->genVueMenuTree($MenuTreeArr, 'id', 'pid', 0);
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => $MenuTree,
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
// 根据token拉取 treeselect 下拉选项菜单
|
||||
function treeoptions_get()
|
||||
{
|
||||
// 此 uri 可不做权限/token过期验证,则在菜单里,可以不加入此项路由path /sys/menu/treeoptions。
|
||||
//
|
||||
// $uri = $this->uri->uri_string;
|
||||
// $Token = $this->input->get_request_header('X-Token', TRUE);
|
||||
// $retPerm = $this->permission->HasPermit($Token, $uri);
|
||||
// if ($retPerm['code'] != 50000) {
|
||||
// $this->set_response($retPerm, REST_Controller::HTTP_OK);
|
||||
// return;
|
||||
// }
|
||||
|
||||
$Token = $this->input->get_request_header('X-Token', TRUE);
|
||||
|
||||
$MenuTreeArr = $this->permission->getPermission($Token, 'menu', false);
|
||||
array_unshift($MenuTreeArr, ['id' => 0, 'pid' => -1, 'title' => '顶级菜单']);
|
||||
$MenuTree = $this->permission->genVueMenuTree($MenuTreeArr, 'id', 'pid', -1);
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => $MenuTree,
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
|
||||
function list_get()
|
||||
{
|
||||
// $result = $this->some_model();
|
||||
$result['success'] = TRUE;
|
||||
|
||||
if ($result['success']) {
|
||||
$List = array(
|
||||
array('order_no' => '201805138451313131', 'timestamp' => 'iphone 7 ', 'username' => 'iphone 7 ', 'price' => 399, 'status' => 'success'),
|
||||
array('order_no' => '300000000000000000', 'timestamp' => 'iphone 7 ', 'username' => 'iphone 7 ', 'price' => 399, 'status' => 'pending'),
|
||||
array('order_no' => '444444444444444444', 'timestamp' => 'iphone 7 ', 'username' => 'iphone 7 ', 'price' => 399, 'status' => 'success'),
|
||||
array('order_no' => '888888888888888888', 'timestamp' => 'iphone 7 ', 'username' => 'iphone 7 ', 'price' => 399, 'status' => 'pending'),
|
||||
);
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => [
|
||||
"total" => count($List),
|
||||
"items" => $List
|
||||
]
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
} else {
|
||||
$message = [
|
||||
"code" => 50008,
|
||||
"message" => 'Login failed, unable to get user details.'
|
||||
];
|
||||
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,478 @@
|
||||
<?php
|
||||
|
||||
use Restserver\Libraries\REST_Controller;
|
||||
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
//To Solve File REST_Controller not found
|
||||
require APPPATH . 'libraries/REST_Controller.php';
|
||||
require APPPATH . 'libraries/Format.php';
|
||||
|
||||
class Role extends REST_Controller
|
||||
{
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->model('Base_model');
|
||||
$this->load->model('Role_model');
|
||||
// $this->config->load('config', true);
|
||||
}
|
||||
|
||||
public function index_get()
|
||||
{
|
||||
$this->load->view('login_view');
|
||||
}
|
||||
|
||||
public function insertx_post()
|
||||
{
|
||||
// $id = $this->post('id'); // POST param
|
||||
$parms = $this->post(); // 获取表单参数,类型为数组
|
||||
var_dump($parms);
|
||||
$result = $this->Base_model->_insert_key('sys_role_perm', $parms);
|
||||
var_dump($result);
|
||||
}
|
||||
|
||||
public function gettest_post()
|
||||
{
|
||||
$result = $this->Base_model->_get_key('sys_perm', 'perm_type,r_id rid', 'perm_type="role" and r_id=1');
|
||||
var_dump($result);
|
||||
var_dump($result[0]['perm_type']);
|
||||
var_dump($this->uri->uri_string);
|
||||
var_dump($this->uri);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function testapi_get()
|
||||
{
|
||||
echo "test api ok...";
|
||||
|
||||
echo APPPATH . "\n";
|
||||
echo SELF . "\n";
|
||||
echo BASEPATH . "\n";
|
||||
echo FCPATH . "\n";
|
||||
echo SYSDIR . "\n";
|
||||
var_dump($this->config->item('rest_language'));
|
||||
var_dump($this->config->item('language'));
|
||||
|
||||
var_dump($this->config);
|
||||
|
||||
// $message = [
|
||||
// "code" => 20000,
|
||||
// "data" => [
|
||||
// "__FUNCTION__" => __FUNCTION__,
|
||||
// "__CLASS__" => __CLASS__,
|
||||
// "uri" => $this->uri
|
||||
// ],
|
||||
//
|
||||
// ];
|
||||
// "data": {
|
||||
// "__FUNCTION__": "router_get",
|
||||
// "__CLASS__": "User",
|
||||
// "uri": {
|
||||
// "keyval": [],
|
||||
// "uri_string": "api/v2/user/router",
|
||||
// "segments": {
|
||||
// "1": "api",
|
||||
// "2": "v2",
|
||||
// "3": "user",
|
||||
// "4": "router"
|
||||
// },
|
||||
}
|
||||
|
||||
public function phpinfo_get()
|
||||
{
|
||||
phpinfo();
|
||||
}
|
||||
|
||||
public function testdb_get()
|
||||
{
|
||||
$this->load->database();
|
||||
$query = $this->db->query("show tables");
|
||||
var_dump($query);
|
||||
var_dump($query->result());
|
||||
var_dump($query->row_array());
|
||||
// 有结果表明数据库连接正常 reslut() 与 row_array 结果有时不太一样
|
||||
// 一般加载到时model里面使用。
|
||||
}
|
||||
|
||||
// 增
|
||||
function add_post()
|
||||
{
|
||||
$uri = $this->uri->uri_string;
|
||||
$Token = $this->input->get_request_header('X-Token', TRUE);
|
||||
$retPerm = $this->permission->HasPermit($Token, $uri);
|
||||
if ($retPerm['code'] != 50000) {
|
||||
$this->set_response($retPerm, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
$parms = $this->post(); // 获取表单参数,类型为数组
|
||||
|
||||
if ($this->Base_model->_key_exists('sys_role', ['name' => $parms['name']])) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'error',
|
||||
"message" => $parms['name'] . ' - 角色名重复'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
// 加入新增时间
|
||||
$parms['create_time'] = time();
|
||||
|
||||
$role_id = $this->Base_model->_insert_key('sys_role', $parms);
|
||||
if (!$role_id) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'error',
|
||||
"message" => $parms['name'] . ' - 角色新增失败'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
// 生成该角色对应的权限: sys_perm, 权限类型为: role, 生成唯一的 perm_id
|
||||
$perm_id = $this->Base_model->_insert_key('sys_perm', ['perm_type' => 'role', "r_id" => $role_id]);
|
||||
if (!$perm_id) {
|
||||
var_dump($this->uri->uri_string . ' 生成该角色对应的权限: sys_perm, 失败...');
|
||||
var_dump(['perm_type' => 'role', "r_id" => $role_id]);
|
||||
return;
|
||||
}
|
||||
|
||||
// 超级管理员角色自动拥有该权限 perm_id
|
||||
$role_perm_id = $this->Base_model->_insert_key('sys_role_perm', ["role_id" => 1, "perm_id" => $perm_id]);
|
||||
if (!$role_perm_id) {
|
||||
var_dump($this->uri->uri_string . ' 超级管理员角色自动拥有该权限 perm_id, 失败...');
|
||||
var_dump(["role_id" => 1, "perm_id" => $perm_id]);
|
||||
return;
|
||||
}
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'success',
|
||||
"message" => $parms['name'] . ' - 角色新增成功'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
// 改
|
||||
function edit_post()
|
||||
{
|
||||
$uri = $this->uri->uri_string;
|
||||
$Token = $this->input->get_request_header('X-Token', TRUE);
|
||||
$retPerm = $this->permission->HasPermit($Token, $uri);
|
||||
if ($retPerm['code'] != 50000) {
|
||||
$this->set_response($retPerm, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
// $id = $this->post('id'); // POST param
|
||||
$parms = $this->post(); // 获取表单参数,类型为数组
|
||||
// var_dump($parms['path']);
|
||||
|
||||
// 参数检验/数据预处理
|
||||
// 超级管理员角色不允许修改
|
||||
if ($parms['id'] == 1) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'error',
|
||||
"message" => $parms['name'] . ' - 角色不允许修改'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
$id = $parms['id'];
|
||||
unset($parms['id']); // 剃除索引id
|
||||
|
||||
// 加入更新时间
|
||||
$parms['update_time'] = time();
|
||||
$where = ["id" => $id];
|
||||
|
||||
if (!$this->Base_model->_update_key('sys_role', $parms, $where)) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'error',
|
||||
"message" => $parms['name'] . ' - 角色更新错误'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'success',
|
||||
"message" => $parms['name'] . ' - 角色更新成功'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
// 删
|
||||
function del_post()
|
||||
{
|
||||
$uri = $this->uri->uri_string;
|
||||
$Token = $this->input->get_request_header('X-Token', TRUE);
|
||||
$retPerm = $this->permission->HasPermit($Token, $uri);
|
||||
if ($retPerm['code'] != 50000) {
|
||||
$this->set_response($retPerm, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
$parms = $this->post(); // 获取表单参数,类型为数组
|
||||
// var_dump($parms['path']);
|
||||
|
||||
// 参数检验/数据预处理
|
||||
// 超级管理员角色不允许删除
|
||||
if ($parms['id'] == 1) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'error',
|
||||
"message" => $parms['name'] . ' - 角色不允许删除'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
// 删除外键关联表 sys_role_perm , sys_perm, sys_role
|
||||
// 1. 根据sys_role id及'menu' 查找 perm_id
|
||||
// 2. 删除sys_role_perm 中perm_id记录
|
||||
// 3. 删除sys_perm中 perm_type='role' and r_id = role_id 记录,即第1步中获取的 perm_id, 一一对应
|
||||
// 4. 删除sys_role 中 id = role_id 的记录
|
||||
$where = 'perm_type="role" and r_id=' . $parms['id'];
|
||||
$arr = $this->Base_model->_get_key('sys_perm', '*', $where);
|
||||
if (empty($arr)) {
|
||||
var_dump($this->uri->uri_string . ' 未查找到 sys_perm 表中记录');
|
||||
var_dump($where);
|
||||
return;
|
||||
}
|
||||
|
||||
$perm_id = $arr[0]['id']; // 正常只有一条记录
|
||||
$this->Base_model->_delete_key('sys_role_perm', ['perm_id' => $perm_id]); // 必须删除权限id 因为超级管理员角色自动拥有该权限否则会造成删除关联错误
|
||||
$this->Base_model->_delete_key('sys_role_perm', ['role_id' => $parms['id']]); // 再删除该角色对应的权限id(原有的菜单)
|
||||
$this->Base_model->_delete_key('sys_perm', ['id' => $perm_id]);
|
||||
|
||||
$this->Base_model->_delete_key('sys_user_role', ['role_id' => $parms['id']]);
|
||||
|
||||
// 删除基础表 sys_role
|
||||
if (!$this->Base_model->_delete_key('sys_role', $parms)) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'error',
|
||||
"message" => $parms['name'] . ' - 角色删除错误'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'success',
|
||||
"message" => $parms['name'] . ' - 角色删除成功'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
|
||||
}
|
||||
|
||||
// 查
|
||||
function view_post()
|
||||
{
|
||||
$uri = $this->uri->uri_string;
|
||||
$Token = $this->input->get_request_header('X-Token', TRUE);
|
||||
|
||||
$retPerm = $this->permission->HasPermit($Token, $uri);
|
||||
if ($retPerm['code'] != 50000) {
|
||||
$this->set_response($retPerm, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
$RoleArr = $this->Role_model->getRoleList();
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => $RoleArr,
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
// 获取所有菜单 不需权限验证
|
||||
function allmenus_get()
|
||||
{
|
||||
$MenuTreeArr = $this->Role_model->getAllMenus();
|
||||
if (empty($MenuTreeArr)) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => $MenuTreeArr,
|
||||
"message" => "数据库表中没有菜单"
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
$MenuTree = $this->permission->genVueMenuTree($MenuTreeArr, 'id', 'pid', 0);
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => $MenuTree,
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
// 获取所有角色带perm_id 不需权限验证
|
||||
function allroles_get()
|
||||
{
|
||||
$AllRolesArr = $this->Role_model->getAllRoles();
|
||||
if (empty($AllRolesArr)) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => $AllRolesArr,
|
||||
"message" => "数据库表中没有角色"
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => $AllRolesArr,
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
// 获取角色拥有的菜单权限 不需权限验证
|
||||
function rolemenu_post()
|
||||
{
|
||||
$parms = $this->post(); // 获取表单参数,类型为数组
|
||||
$RoleId = $parms['roleId'];
|
||||
|
||||
$MenuTreeArr = $this->Role_model->getRoleMenu($RoleId);
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => $MenuTreeArr,
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
// 获取角色拥有的角色权限 不需权限验证
|
||||
function rolerole_post()
|
||||
{
|
||||
$parms = $this->post(); // 获取表单参数,类型为数组
|
||||
$RoleId = $parms['roleId'];
|
||||
|
||||
$RoleRoleArr = $this->Role_model->getRoleRole($RoleId);
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => $RoleRoleArr,
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
// 保存角色对应权限
|
||||
function saveroleperm_post()
|
||||
{
|
||||
$parms = $this->post(); // 获取表单参数,类型为数组
|
||||
// var_dump($parms['roleId']);
|
||||
// var_dump($parms['rolePerms']);
|
||||
// 参数检验/数据预处理
|
||||
// 超级管理员角色不允许删除
|
||||
if ($parms['roleId'] == 1) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'error',
|
||||
"message" => '超级管理员角色拥有所有权限,不允许修改!'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
$RolePermArr = $this->Role_model->getRolePerm($parms['roleId']);
|
||||
|
||||
$AddArr = $this->permission->array_diff_assoc2($parms['rolePerms'], $RolePermArr);
|
||||
// var_dump('------------只存在于前台传参 做添加操作-------------');
|
||||
// var_dump($AddArr);
|
||||
$failed = false;
|
||||
$failedArr = [];
|
||||
foreach ($AddArr as $k => $v) {
|
||||
$ret = $this->Base_model->_insert_key('sys_role_perm', $v);
|
||||
if (!$ret) {
|
||||
$failed = true;
|
||||
array_push($failedArr, $v);
|
||||
}
|
||||
}
|
||||
if ($failed) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'error',
|
||||
"message" => '授权失败 ' . json_encode($failedArr)
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
$DelArr = $this->permission->array_diff_assoc2($RolePermArr, $parms['rolePerms']);
|
||||
// var_dump('------------只存在于后台数据库 删除操作-------------');
|
||||
// var_dump($DelArr);
|
||||
$failed = false;
|
||||
$failedArr = [];
|
||||
foreach ($DelArr as $k => $v) {
|
||||
$ret = $this->Base_model->_delete_key('sys_role_perm', $v);
|
||||
if (!$ret) {
|
||||
$failed = true;
|
||||
array_push($failedArr, $v);
|
||||
}
|
||||
}
|
||||
if ($failed) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'error',
|
||||
"message" => '授权失败 ' . json_encode($failedArr)
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"type" => 'success',
|
||||
"data" => $parms,
|
||||
"message" => '授权操作成功',
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
|
||||
function list_get()
|
||||
{
|
||||
// $result = $this->some_model();
|
||||
$result['success'] = TRUE;
|
||||
|
||||
if ($result['success']) {
|
||||
$List = array(
|
||||
array('order_no' => '201805138451313131', 'timestamp' => 'iphone 7 ', 'username' => 'iphone 7 ', 'price' => 399, 'status' => 'success'),
|
||||
array('order_no' => '300000000000000000', 'timestamp' => 'iphone 7 ', 'username' => 'iphone 7 ', 'price' => 399, 'status' => 'pending'),
|
||||
array('order_no' => '444444444444444444', 'timestamp' => 'iphone 7 ', 'username' => 'iphone 7 ', 'price' => 399, 'status' => 'success'),
|
||||
array('order_no' => '888888888888888888', 'timestamp' => 'iphone 7 ', 'username' => 'iphone 7 ', 'price' => 399, 'status' => 'pending'),
|
||||
);
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => [
|
||||
"total" => count($List),
|
||||
"items" => $List
|
||||
]
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
} else {
|
||||
$message = [
|
||||
"code" => 50008,
|
||||
"message" => 'Login failed, unable to get user details.'
|
||||
];
|
||||
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user