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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user