feat: init project codebase
This commit is contained in:
@@ -0,0 +1,322 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
use chriskacerguis\RestServer\RestController;
|
||||
|
||||
//统一API的返回
|
||||
class ApiController extends RestController
|
||||
{
|
||||
protected $_user_id = 0;
|
||||
|
||||
protected $http_status = [
|
||||
'METHOD_NOT_ALLOWED'=>400
|
||||
];
|
||||
|
||||
protected function json_input($require=TRUE){
|
||||
$json_params = file_get_contents('php://input');
|
||||
if(empty($json_params)){
|
||||
if($require){
|
||||
$this->error('请输入参数');
|
||||
}
|
||||
return [];
|
||||
}
|
||||
try{
|
||||
return json_decode($json_params, true);
|
||||
}catch (Exception $e){
|
||||
return [];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected function error($msg='',$data=[]){
|
||||
$message = [
|
||||
"code" => 50000,
|
||||
"data" => $data,
|
||||
"message" => $msg
|
||||
];
|
||||
$this->response($message, parent::HTTP_OK);
|
||||
die;
|
||||
}
|
||||
|
||||
public function success($data=[],$msg=''){
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => $data,
|
||||
"message" => $msg
|
||||
];
|
||||
$this->response($message, parent::HTTP_OK);
|
||||
die;
|
||||
}
|
||||
|
||||
|
||||
//接收请求检验
|
||||
function json_validation($config){
|
||||
$data = $this->json_input();
|
||||
$this->load->library('form_validation');
|
||||
$this->form_validation->set_data($data);
|
||||
$this->form_validation->set_rules($config);
|
||||
if ($this->form_validation->run() === FALSE)
|
||||
{
|
||||
$error_arr = $this->form_validation->error_array();
|
||||
$this->error(reset($error_arr),$error_arr);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务器端请求校验
|
||||
* @DATE 20220522
|
||||
**/
|
||||
function checkUser(){
|
||||
//尝试跟服务端对接
|
||||
$ret = $this->serverSign();
|
||||
if($ret !== null){
|
||||
if(!$ret){
|
||||
$this->error('您的签名错误');
|
||||
}
|
||||
//通过头部的open id获取用户信息
|
||||
$this->load->model('customer_model');
|
||||
$customer = $this->customer_model->get(
|
||||
$this->_head_args['Uah-Open-Id'],
|
||||
'openid'
|
||||
);
|
||||
if(!$customer){
|
||||
$this->error('用户OPENID不存在');
|
||||
}
|
||||
$this->_user_id = $customer['id'];
|
||||
return;
|
||||
}
|
||||
//跟前端对接,未登录告警
|
||||
if(empty($this->session->{Customer_model::SESSION_KEY})){
|
||||
$this->error('您的授权信息不存在或已过期,请从微信公众号菜单进入');
|
||||
}
|
||||
$this->_user_id = $this->session->{Customer_model::SESSION_KEY};
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务器端请求校验
|
||||
* @DATE 20220522
|
||||
**/
|
||||
function serverSign(){
|
||||
$str = '';
|
||||
foreach (['Uah-Open-Id','Uah-Timestamp'] as $val) {
|
||||
if(empty($this->_head_args[$val])){
|
||||
return null;
|
||||
}
|
||||
$str.=$this->_head_args[$val];
|
||||
}
|
||||
if(empty($this->_head_args['Uah-Sign'])){
|
||||
return null;
|
||||
}
|
||||
$conf = $this->config->item('uah_server_token');
|
||||
//echo $str.$conf;
|
||||
//echo md5($str.$conf);
|
||||
//echo $this->_head_args['Uah-Sign'];
|
||||
return md5($str.$conf) == $this->_head_args['Uah-Sign'];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//提供后台的统一方法
|
||||
class AdminController extends ApiController
|
||||
{
|
||||
protected $base_model = 'sample_model';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->model($this->base_model);
|
||||
|
||||
$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->response($retPerm, RestController::HTTP_OK);
|
||||
}
|
||||
}
|
||||
|
||||
//单条更新的校验规则
|
||||
protected function _validate_rule(){
|
||||
return [];
|
||||
}
|
||||
|
||||
//批量导入的字段对应表
|
||||
protected function _import_field_map(){
|
||||
return [];
|
||||
}
|
||||
|
||||
function list_get()
|
||||
{
|
||||
return $this->_list_get();
|
||||
}
|
||||
|
||||
function _list_get($is_return = false, $select = null)
|
||||
{
|
||||
$filters = $this->input->get();
|
||||
$page = $this->input->get('page');
|
||||
$limit = $this->input->get('limit');
|
||||
$page = empty($page)?1:$page;
|
||||
$limit = empty($limit)?50:$limit;
|
||||
$sort = $this->input->get('sort');
|
||||
$sort_type = substr($sort,0,1);
|
||||
$sort_name = 'base.'.substr($sort,1);
|
||||
$sort_type = $sort_type=='-'?'DESC':'ASC';
|
||||
unset($filters['sort']);
|
||||
unset($filters['limit']);
|
||||
unset($filters['page']);
|
||||
$items = $this->{$this->base_model}->listing($filters, ($page-1)*$limit,$limit,$select,$sort_name,$sort_type);
|
||||
if($is_return){
|
||||
return $items;
|
||||
}
|
||||
$this->success($items);
|
||||
}
|
||||
|
||||
function detail_get()
|
||||
{
|
||||
return $this->_detail_get();
|
||||
}
|
||||
|
||||
function _detail_get($is_return = false)
|
||||
{
|
||||
$id = $this->security->xss_clean($this->input->get('id'));
|
||||
$item = $this->{$this->base_model}->get($id);
|
||||
if($is_return){
|
||||
return $item;
|
||||
}
|
||||
$this->success([
|
||||
"item" => $item
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
function update_post()
|
||||
{
|
||||
$this->_update_post($this->_validate_rule());
|
||||
}
|
||||
|
||||
function _update_post($validate_config,$data = [])
|
||||
{
|
||||
if(empty($data)){
|
||||
$data = $this->json_validation($validate_config);
|
||||
}
|
||||
$fields = array_column($validate_config,'field');
|
||||
foreach ($data as $key => $val) {
|
||||
if(!in_array($key,$fields)){
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
if(empty($data['id'])){
|
||||
$ret = $this->{$this->base_model}->add($data);
|
||||
if($ret === -1){
|
||||
$this->error('名称重复');
|
||||
}
|
||||
}else{
|
||||
$this->{$this->base_model}->update($data, ['id'=>$data['id']]);
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
|
||||
function delete_post()
|
||||
{
|
||||
return $this->_delete_post();
|
||||
}
|
||||
|
||||
function _delete_post()
|
||||
{
|
||||
$data = $this->json_input();
|
||||
$this->{$this->base_model}->batchDel($data);
|
||||
$this->success();
|
||||
}
|
||||
|
||||
//批量新建
|
||||
function _batch_create_post($validate_config,$data_arr=[])
|
||||
{
|
||||
if(empty($data_arr)){
|
||||
$data_arr = $this->json_input();
|
||||
}
|
||||
$this->load->library('form_validation');
|
||||
foreach ($data_arr as $item) {
|
||||
$this->form_validation->set_data($item);
|
||||
$this->form_validation->set_rules($validate_config);
|
||||
if ($this->form_validation->run() === FALSE)
|
||||
{
|
||||
$error_arr = $this->form_validation->error_array();
|
||||
$this->error(reset($error_arr),$error_arr);
|
||||
}
|
||||
$ret = $this->{$this->base_model}->batchAdd($data_arr);
|
||||
if(is_string($ret)){
|
||||
$this->error($ret);
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
}
|
||||
|
||||
//批量更新单一属性
|
||||
function batch_update_post()
|
||||
{
|
||||
$data_arr = $this->json_input();
|
||||
//id必传,其他字段检查规则中有没有
|
||||
if(empty($data_arr['id'])){
|
||||
$this->error('请传入id');
|
||||
}
|
||||
$ids = [];
|
||||
foreach ($data_arr['id'] as $id) {
|
||||
$ids[] = (int)$id;
|
||||
}
|
||||
unset($data_arr['id']);
|
||||
$this->{$this->base_model}->update($data_arr, 'id in ('.implode(',',$ids).')');
|
||||
$this->success();
|
||||
}
|
||||
|
||||
//批量更新插入
|
||||
function batch_update_insert_post()
|
||||
{
|
||||
$data = $this->_arr_fill($this->_import_field_map(),$this->json_input());
|
||||
$this->_batch_update_insert_post($this->_validate_rule(),$data);
|
||||
$this->success();
|
||||
}
|
||||
|
||||
//批量更新插入
|
||||
function _batch_update_insert_post($validate_config,$data_arr=[])
|
||||
{
|
||||
if(empty($data_arr)){
|
||||
$data_arr = $this->json_input();
|
||||
}
|
||||
$this->load->library('form_validation');
|
||||
foreach ($data_arr as $item) {
|
||||
$this->form_validation->set_data($item);
|
||||
$this->form_validation->set_rules($validate_config);
|
||||
if ($this->form_validation->run() === FALSE)
|
||||
{
|
||||
$error_arr = $this->form_validation->error_array();
|
||||
$this->error(reset($error_arr),$error_arr);
|
||||
}
|
||||
$ret = $this->{$this->base_model}->batchUpdateInsert($data_arr);
|
||||
$this->success();
|
||||
}
|
||||
}
|
||||
|
||||
//数组字段映射并补充不存在的字段
|
||||
function _arr_fill($map,$input)
|
||||
{
|
||||
$data = [];
|
||||
foreach ($input as $item) {
|
||||
$tmp = [];
|
||||
//中文改成英文
|
||||
foreach ($item as $k=>$v) {
|
||||
if(!isset($map[$k])){
|
||||
continue;
|
||||
}
|
||||
$tmp[$map[$k]] = $v;
|
||||
}
|
||||
//补充不存在的字段
|
||||
foreach ($map as $k=>$v) {
|
||||
if(!isset($tmp[$v])){
|
||||
$tmp[$v] = '';
|
||||
}
|
||||
}
|
||||
$data[] = $tmp;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
//定义一些可复用的功能
|
||||
class MY_Model extends CI_Model
|
||||
{
|
||||
protected $table = '';
|
||||
protected $join = [];//定义以下属性[table,on,type]
|
||||
protected $keyword_field = [];//作为关键词搜索的字段
|
||||
protected $has_create_time = true;//是否有创建时间create_time
|
||||
protected $has_update_time = true;//是否有更新时间update_time
|
||||
protected $list_field = 'base.*';//默认列表筛选的字段
|
||||
protected $unique_field = null;//不允许重复的字段
|
||||
protected $where_field_map = [];//过滤条件字段重命名
|
||||
protected $null_field = [];//特定字段的0值填充为null值
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->load->database();
|
||||
}
|
||||
|
||||
function listing($filters = [], $offset, $segment, $select=null, $order_field='base.id',$order='DESC')
|
||||
{
|
||||
if(empty($select)){
|
||||
$select = $this->list_field;
|
||||
}
|
||||
$this->db->select('SQL_CALC_FOUND_ROWS '.$select,false);
|
||||
$this->db->from($this->table.' as base');
|
||||
if(!empty($this->join)){
|
||||
foreach ($this->join as $_join) {
|
||||
$this->db->join($_join['table'], $_join['on'],$_join['type']);
|
||||
}
|
||||
}
|
||||
if(!empty($order_field)){
|
||||
$this->db->order_by($order_field,$order);
|
||||
}
|
||||
|
||||
//包含筛选条件
|
||||
if(!empty($filters)) foreach ($filters as $key=>$val) {
|
||||
if($val === ''){
|
||||
continue;
|
||||
}
|
||||
//容易冲突的键
|
||||
if(in_array($key,['id','name','create_time'])){
|
||||
$key = 'base.'.$key;
|
||||
}
|
||||
//字段别名
|
||||
if(isset($this->where_field_map[$key])){
|
||||
$key = $this->where_field_map[$key];
|
||||
}
|
||||
//某些字段的0值填充为null
|
||||
if($val == '0' && in_array($key,$this->null_field)){
|
||||
$this->db->where("$key is null");
|
||||
continue;
|
||||
}
|
||||
//含有关键词
|
||||
if(!empty($this->keyword_field)) {
|
||||
if($key === 'title'){
|
||||
//对于关键词,拼合所有筛选条件
|
||||
$likeCriteria = [];
|
||||
foreach ($this->keyword_field as $_field) {
|
||||
$likeCriteria[] = $_field." LIKE '%".$val."%'";
|
||||
}
|
||||
$this->db->where('('.implode(' OR ',$likeCriteria).')');
|
||||
continue;
|
||||
}elseif(in_array($key,$this->keyword_field)){
|
||||
$this->db->where($key." LIKE '%".$val."%'");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
//对于数组
|
||||
if(is_array($val)){
|
||||
$this->db->where("$key BETWEEN {$val[0]} and {$val[1]}");
|
||||
continue;
|
||||
}
|
||||
//含有比较符号
|
||||
$op = substr($val,0,1);
|
||||
if(in_array($op,['>','<'])){
|
||||
$this->db->where($key.' '.$op,substr($val,1));
|
||||
continue;
|
||||
}
|
||||
//普通值
|
||||
$this->db->where($key,$val);
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->db->limit($segment, $offset);
|
||||
$query = $this->db->get();
|
||||
$result['items'] = $query->result_array();
|
||||
//echo $this->db->last_query(); die;
|
||||
$query = $this->db->query('SELECT FOUND_ROWS() AS `Count`');
|
||||
$result['total'] = (int)$query->row()->Count;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function get($id=false, $field='base.id', $is_list=false, $select=null)
|
||||
{
|
||||
if(empty($select)){
|
||||
$select = $this->list_field;
|
||||
}
|
||||
$this->db->select($select);
|
||||
$this->db->from($this->table.' as base');
|
||||
if(!empty($this->join)){
|
||||
foreach ($this->join as $_join) {
|
||||
$this->db->join($_join['table'], $_join['on'],$_join['type']);
|
||||
}
|
||||
}
|
||||
if($id !== false){
|
||||
if(is_array($id)){
|
||||
$this->db->where_in($field, $id);
|
||||
}else{
|
||||
$this->db->where($field, $id);
|
||||
}
|
||||
}
|
||||
$query = $this->db->get();
|
||||
if($is_list){
|
||||
return $query->result_array();
|
||||
}else{
|
||||
return $query->row_array();
|
||||
}
|
||||
}
|
||||
|
||||
//更新单个或多个
|
||||
public function update($data, $where, $one_row = true)
|
||||
{
|
||||
if($one_row){
|
||||
if(!empty($this->unique_field)){
|
||||
$ret = $this->get($data[$this->unique_field],$this->unique_field);
|
||||
if($ret && $ret['id'] != $data['id']){
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if($this->has_update_time){
|
||||
$data['update_time'] = empty($data['update_time'])?time():$data['update_time'];
|
||||
}
|
||||
$this->db->where($where);
|
||||
$this->db->update($this->table,$data);
|
||||
return $this->db->affected_rows();
|
||||
}
|
||||
|
||||
public function add($data)
|
||||
{
|
||||
if($this->has_create_time){
|
||||
$data['create_time'] = empty($data['create_time'])?time():$data['create_time'];
|
||||
}
|
||||
if(!empty($this->unique_field)){
|
||||
$ret = $this->get($data[$this->unique_field],$this->unique_field);
|
||||
if($ret){
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
$this->db->insert($this->table,$data);
|
||||
return $this->db->insert_id();
|
||||
}
|
||||
|
||||
function batchAdd($data)
|
||||
{
|
||||
if(!empty($this->unique_field)){
|
||||
$val = array_column($data,$this->unique_field);
|
||||
$ret = $this->get($val,$this->unique_field);
|
||||
if($ret){
|
||||
return $ret[$this->unique_field].'已存在,不能重复添加';
|
||||
}
|
||||
}
|
||||
if($this->has_create_time){
|
||||
$time = time();
|
||||
foreach ($data as &$item) {
|
||||
$item['create_time'] = $time;
|
||||
}
|
||||
}
|
||||
return $this->db->insert_batch($this->table, $data);
|
||||
}
|
||||
|
||||
//与批量新建的区别是检测到重复字段则更新
|
||||
function batchUpdateInsert($data)
|
||||
{
|
||||
//需要新增的条目
|
||||
$left = [];
|
||||
foreach ($data as $item) {
|
||||
//有id则更新
|
||||
if(!empty($item['id'])){
|
||||
$this->update($item,'id = '.$item['id']);
|
||||
continue;
|
||||
}
|
||||
if(!empty($this->unique_field)){
|
||||
$val = array_column($item,$this->unique_field);
|
||||
$this->get($val,$this->unique_field);
|
||||
//存在则更新
|
||||
$this->update($item,$this->unique_field.' = "'.$item[$this->unique_field].'"');
|
||||
continue;
|
||||
}
|
||||
$left[] = $item;
|
||||
}
|
||||
if($this->has_create_time){
|
||||
$time = time();
|
||||
unset($item);
|
||||
foreach ($left as &$item) {
|
||||
$item['create_time'] = $time;
|
||||
}
|
||||
}
|
||||
|
||||
/*$temp1 = array_keys($left[0]);
|
||||
* 检查字段情况
|
||||
foreach ($left as $k=>$item) {
|
||||
foreach ($temp1 as $key) {
|
||||
if(!in_array($key, array_keys($item))){
|
||||
die($k.$key);
|
||||
}
|
||||
}
|
||||
}
|
||||
die('');
|
||||
*/
|
||||
return $this->db->insert_batch($this->table, $left);
|
||||
}
|
||||
|
||||
function batchDel($id, $field='id')
|
||||
{
|
||||
$this->db->where_in($field, $id);
|
||||
return $this->db->delete($this->table);
|
||||
}
|
||||
|
||||
//获取总数
|
||||
function total($filters=[],$join=false)
|
||||
{
|
||||
$this->db->select('count(*) as count');
|
||||
$this->db->from($this->table.' base');
|
||||
//支持筛选条件
|
||||
if($filters){
|
||||
foreach ($filters as $filter) {
|
||||
$this->db->where($filter);
|
||||
}
|
||||
}
|
||||
//支持连表
|
||||
if($join && !empty($this->join)){
|
||||
foreach ($this->join as $_join) {
|
||||
$this->db->join($_join['table'], $_join['on'],$_join['type']);
|
||||
}
|
||||
}
|
||||
$query = $this->db->get();
|
||||
$row = $query->row_array();
|
||||
return $row['count'];
|
||||
}
|
||||
|
||||
//按天统计
|
||||
function sumByDay($start=0,$end=0)
|
||||
{
|
||||
$this->db->select('count(*) as count');
|
||||
$this->db->from($this->table);
|
||||
$this->db->where('create_time>=', $start);
|
||||
$this->db->where('create_time<=', $end);
|
||||
$this->db->group_by('DATE_FORMAT(from_unixtime(create_time),\'%Y-%m-%d\')');
|
||||
$query = $this->db->get();
|
||||
return $query->result_array();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>403 Forbidden</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<p>Directory access is forbidden.</p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user