|
@@ -14,6 +14,7 @@
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
|
namespace app\api\controller;
|
|
|
+
|
|
|
use app\common\constant\CommonConstant;
|
|
|
use app\common\model\User;
|
|
|
use Firebase\JWT\JWT;
|
|
@@ -42,72 +43,76 @@ class Base extends Controller
|
|
|
protected $ret_msg = ''; // 返回提示信息
|
|
|
protected $is_test = false;
|
|
|
protected $need_login = [];
|
|
|
- public function initialize(){
|
|
|
+
|
|
|
+ public function initialize()
|
|
|
+ {
|
|
|
$modulename = $this->request->module();
|
|
|
$controllername = Loader::parseName($this->request->controller());
|
|
|
$actionname = strtolower($this->request->action());
|
|
|
$this->checking($modulename, $controllername, $actionname);
|
|
|
|
|
|
- $this->page = input('page',1);
|
|
|
- $this->page_num = input('page_num',20);
|
|
|
- $this->off_set = $this->page * $this->page_num - $this->page_num;
|
|
|
- $this->is_test = input('test',0);// 测试用的
|
|
|
- $path = explode('/',$this->request->path());
|
|
|
- if( input('user_id')) $this->user_id = input('user_id');
|
|
|
- if( input('user')) $this->user_id = input('user');
|
|
|
- if(!empty($this->need_login) && in_array(end($path),$this->need_login)) $this->checkLogin();
|
|
|
+ $this->page = input('page', 1);
|
|
|
+ $this->page_num = input('page_num', 20);
|
|
|
+ $this->off_set = $this->page * $this->page_num - $this->page_num;
|
|
|
+ $this->is_test = input('test', 0);// 测试用的
|
|
|
+ $path = explode('/', $this->request->path());
|
|
|
+ if (input('user_id')) $this->user_id = input('user_id');
|
|
|
+ if (input('user')) $this->user_id = input('user');
|
|
|
+ if (!empty($this->need_login) && in_array(end($path), $this->need_login)) $this->checkLogin();
|
|
|
}
|
|
|
+
|
|
|
//校验jwt权限API
|
|
|
protected function checkLogin()
|
|
|
{
|
|
|
$authorization = app()->request->header('Authorization');
|
|
|
- if(empty($authorization) || $authorization == null){
|
|
|
- if($this->is_test == 1) {
|
|
|
- $this->user_id = input('user_id') ? input('user_id') : 22;
|
|
|
- if(input('user')) $this->user_id = input('user');
|
|
|
+ if (empty($authorization) || $authorization == null) {
|
|
|
+ if ($this->is_test == 1) {
|
|
|
+ $this->user_id = input('user_id') ? input('user_id') : 22;
|
|
|
+ if (input('user')) $this->user_id = input('user');
|
|
|
return true;
|
|
|
}
|
|
|
- $this->error('Token不存在,拒绝访问--','',0,-1);
|
|
|
+ $this->error('Token不存在,拒绝访问--', '', 0, -1);
|
|
|
}
|
|
|
$key = md5(config('app.jwt'));
|
|
|
try {
|
|
|
$check_authorization = JWT::decode($authorization, $key, array('HS256'));
|
|
|
- if($check_authorization['code'] !=200) $this->exception($check_authorization['msg']);
|
|
|
+ if ($check_authorization['code'] != 200) $this->exception($check_authorization['msg']);
|
|
|
$authInfo = json_decode(json_encode($check_authorization['data']), true);
|
|
|
if (!empty($authInfo['uid'])) {
|
|
|
$member = User::find($authInfo['uid']);
|
|
|
- if(empty($member) || $member['is_deleted']) $this->error('会员不存在','',0);
|
|
|
+ if (empty($member) || $member['is_deleted']) $this->error('会员不存在', '', 0);
|
|
|
//if(!empty($authInfo['facility_code']) && !in_array($authInfo['facility_code'],[$member['facility_1'],$member['facility_2'],$member['facility_3'],$member['facility_4'],$member['facility_5']])) $this->error('该设备已被禁用','',0,-1);
|
|
|
- if($member['status']){
|
|
|
+ if ($member['status']) {
|
|
|
$this->user_id = $authInfo['uid'];
|
|
|
$this->user = $member;
|
|
|
return $this->user_id;
|
|
|
- }else{
|
|
|
- $this->error('该会员已被禁用','',0,-1);
|
|
|
+ } else {
|
|
|
+ $this->error('该会员已被禁用', '', 0, -1);
|
|
|
}
|
|
|
} else {
|
|
|
- $this->error('Token验证不通过,用户不存在','',0,-1);
|
|
|
+ $this->error('Token验证不通过,用户不存在', '', 0, -1);
|
|
|
}
|
|
|
} catch (\Exception $e) {
|
|
|
- $this->error($e->getMessage(),'',0,-1);
|
|
|
+ $this->error($e->getMessage(), '', 0, -1);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
- protected function setUid(){
|
|
|
+ protected function setUid()
|
|
|
+ {
|
|
|
$authorization = app()->request->header('Authorization');
|
|
|
$key = md5(config('app.jwt'));
|
|
|
- if(empty($authorization) || $authorization == null) return false;
|
|
|
+ if (empty($authorization) || $authorization == null) return false;
|
|
|
try {
|
|
|
$check_authorization = JWT::decode($authorization, $key, array('HS256'));
|
|
|
- if($check_authorization['code'] !=200) $this->exception($check_authorization['msg']);
|
|
|
+ if ($check_authorization['code'] != 200) $this->exception($check_authorization['msg']);
|
|
|
$authInfo = json_decode(json_encode($check_authorization['data']), true);
|
|
|
if (!empty($authInfo['uid'])) {
|
|
|
- $member = Db::name('store_member')->field('status')->where('id',$authInfo['uid'])->find();
|
|
|
- if($member['status']){
|
|
|
+ $member = Db::name('store_member')->field('status')->where('id', $authInfo['uid'])->find();
|
|
|
+ if ($member['status']) {
|
|
|
$this->user_id = $authInfo['uid'];
|
|
|
$this->user_id;
|
|
|
- return true;
|
|
|
+ return true;
|
|
|
}
|
|
|
}
|
|
|
} catch (\Exception $e) {
|
|
@@ -117,62 +122,67 @@ class Base extends Controller
|
|
|
}
|
|
|
|
|
|
// 获取用户信息
|
|
|
- function userInfo($field =''){
|
|
|
- if(!$this->user_id) return [];
|
|
|
- return $field ? Db::name('store_member')->field($field)->find($this->user_id) : Db::name('store_member')->find($this->user_id);
|
|
|
+ function userInfo($field = '')
|
|
|
+ {
|
|
|
+ if (!$this->user_id) return [];
|
|
|
+ return $field ? Db::name('store_member')->field($field)->find($this->user_id) : Db::name('store_member')->find($this->user_id);
|
|
|
}
|
|
|
|
|
|
// 验证短信验证码
|
|
|
- protected function checkPhoneCode($phone,$code){
|
|
|
+ protected function checkPhoneCode($phone, $code)
|
|
|
+ {
|
|
|
//return true;
|
|
|
- $sel_time =date('Y-m-d H:i:s',time()-600);
|
|
|
+ $sel_time = date('Y-m-d H:i:s', time() - 600);
|
|
|
$store_member_sms = Db::name('store_member_sms')
|
|
|
- ->field('id,code')->where('phone',$phone)
|
|
|
- ->where('create_at','> time',$sel_time)
|
|
|
- ->where('used',0)->order('id desc')->find();
|
|
|
- return !empty($store_member_sms) && $store_member_sms['code'] == $code ? $store_member_sms['id'] :0;
|
|
|
+ ->field('id,code')->where('phone', $phone)
|
|
|
+ ->where('create_at', '> time', $sel_time)
|
|
|
+ ->where('used', 0)->order('id desc')->find();
|
|
|
+ return !empty($store_member_sms) && $store_member_sms['code'] == $code ? $store_member_sms['id'] : 0;
|
|
|
}
|
|
|
|
|
|
// 更新验证码状态
|
|
|
- protected function updatePhoneCode($code_id){
|
|
|
- Db::name('store_member_sms')->where('id',$code_id)->update(['used'=>1]);
|
|
|
+ protected function updatePhoneCode($code_id)
|
|
|
+ {
|
|
|
+ Db::name('store_member_sms')->where('id', $code_id)->update(['used' => 1]);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 操作成功返回的数据
|
|
|
- * @param string $msg 提示信息
|
|
|
- * @param mixed $data 要返回的数据
|
|
|
- * @param int $code 错误码,默认为1
|
|
|
- * @param string $type 输出类型
|
|
|
+ * @param string $msg 提示信息
|
|
|
+ * @param mixed $data 要返回的数据
|
|
|
+ * @param int $code 错误码,默认为1
|
|
|
+ * @param string $type 输出类型
|
|
|
* @param array $header 发送的 Header 信息
|
|
|
*/
|
|
|
- protected function success($msg = 'ok', $data = null , $is_login = 1, $code = 1, $type = null, array $header = [])
|
|
|
+ protected function success($msg = 'ok', $data = null, $is_login = 1, $code = 1, $type = null, array $header = [])
|
|
|
{
|
|
|
$this->results($msg, $data, $is_login, $code, $type, $header);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 操作失败返回的数据
|
|
|
- * @param string $msg 提示信息
|
|
|
- * @param mixed $data 要返回的数据
|
|
|
- * @param int $code 错误码,默认为0
|
|
|
- * @param string $type 输出类型
|
|
|
+ * @param string $msg 提示信息
|
|
|
+ * @param mixed $data 要返回的数据
|
|
|
+ * @param int $code 错误码,默认为0
|
|
|
+ * @param string $type 输出类型
|
|
|
* @param array $header 发送的 Header 信息
|
|
|
*/
|
|
|
protected function error($msg = '', $data = null, $is_login = 1, $code = 0, $type = null, array $header = [])
|
|
|
{
|
|
|
- if(empty($this->user_id)){
|
|
|
+ if (empty($this->user_id)) {
|
|
|
$is_login = 0;
|
|
|
}
|
|
|
$this->results($msg, $data, $is_login, $code, $type, $header);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 返回封装后的 API 数据到客户端
|
|
|
* @access protected
|
|
|
- * @param mixed $msg 提示信息
|
|
|
- * @param mixed $data 要返回的数据
|
|
|
- * @param int $code 错误码,默认为0
|
|
|
- * @param string $type 输出类型,支持json/xml/jsonp
|
|
|
- * @param array $header 发送的 Header 信息
|
|
|
+ * @param mixed $msg 提示信息
|
|
|
+ * @param mixed $data 要返回的数据
|
|
|
+ * @param int $code 错误码,默认为0
|
|
|
+ * @param string $type 输出类型,支持json/xml/jsonp
|
|
|
+ * @param array $header 发送的 Header 信息
|
|
|
* @return void
|
|
|
* @throws HttpResponseException
|
|
|
*/
|
|
@@ -181,20 +191,17 @@ class Base extends Controller
|
|
|
$result = [
|
|
|
'code' => $code,
|
|
|
'is_login' => $is_login,
|
|
|
- 'msg' => $msg,
|
|
|
+ 'msg' => $msg,
|
|
|
'time' => \think\facade\Request::instance()->server('REQUEST_TIME'),
|
|
|
'data' => $data,
|
|
|
];
|
|
|
// 如果未设置类型则自动判断
|
|
|
$type = $type ? $type : 'json';
|
|
|
|
|
|
- if (isset($header['statuscode']))
|
|
|
- {
|
|
|
+ if (isset($header['statuscode'])) {
|
|
|
$code = $header['statuscode'];
|
|
|
unset($header['statuscode']);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
+ } else {
|
|
|
//未设置状态码,根据code值判断
|
|
|
$code = $code >= 1000 || $code < 200 ? 200 : $code;
|
|
|
}
|
|
@@ -204,7 +211,7 @@ class Base extends Controller
|
|
|
|
|
|
|
|
|
//token加密
|
|
|
- public function createJwt($uid,$facility_code='')
|
|
|
+ public function createJwt($uid, $facility_code = '')
|
|
|
{
|
|
|
$key = md5(config('app.jwt')); //jwt的签发密钥,验证token的时候需要用到
|
|
|
$time = time(); //签发时间
|
|
@@ -222,22 +229,24 @@ class Base extends Controller
|
|
|
return $jwt;
|
|
|
}
|
|
|
|
|
|
- protected function exception($msg){
|
|
|
+ protected function exception($msg)
|
|
|
+ {
|
|
|
throw new Exception($msg);
|
|
|
}
|
|
|
|
|
|
// 事务返回
|
|
|
- protected function transReturn($data = []){
|
|
|
- $this->is_commit ? $this->success($this->ret_msg,$data):$this->error($this->ret_msg);
|
|
|
+ protected function transReturn($data = [])
|
|
|
+ {
|
|
|
+ $this->is_commit ? $this->success($this->ret_msg, $data) : $this->error($this->ret_msg);
|
|
|
}
|
|
|
|
|
|
protected function checking($modulename, $controllername, $action_name)
|
|
|
{
|
|
|
$params = $this->request->post();
|
|
|
- if (in_array($action_name,['get_data','create'])) {
|
|
|
+ if (in_array($action_name, ['get_data', 'create', 'edit'])) {
|
|
|
$get_module_list = CommonConstant::get_module_list();
|
|
|
$rule = [
|
|
|
- 'module|模块类型' => 'require|in:'.implode(',',array_keys($get_module_list)),
|
|
|
+ 'module|模块类型' => 'require|in:' . implode(',', array_keys($get_module_list)),
|
|
|
];
|
|
|
$message = [
|
|
|
'module.in' => '请选择正确的模块类型',
|
|
@@ -256,19 +265,18 @@ class Base extends Controller
|
|
|
$this->error($validate->getError());
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- if ($controllername == 'approveinfo') {
|
|
|
- if ($action_name == 'get_detail') {
|
|
|
- $rule = [
|
|
|
- 'id|申请参数' => 'require|gt:0',
|
|
|
- ];
|
|
|
- $message = [];
|
|
|
- $validate = new Validate($rule, $message);
|
|
|
- if (!$validate->check($params)) {
|
|
|
+ if ($action_name == 'edit') {
|
|
|
+ $module = $this->request->post('module');
|
|
|
+ $validates = CommonConstant::get_module_validate_list();
|
|
|
+ $validate = $validates[$module];
|
|
|
+ $validate = new $validate;
|
|
|
+ if (!$validate->check($params, [], 'edit')) {
|
|
|
$this->error($validate->getError());
|
|
|
}
|
|
|
}
|
|
|
- if ($action_name == 'get_info') {
|
|
|
+ }
|
|
|
+ if ($controllername == 'approveinfo') {
|
|
|
+ if (in_array($action_name, ['get_detail', 'get_info', 'urging', 'cancel'])) {
|
|
|
$rule = [
|
|
|
'id|申请参数' => 'require|gt:0',
|
|
|
];
|
|
@@ -278,9 +286,11 @@ class Base extends Controller
|
|
|
$this->error($validate->getError());
|
|
|
}
|
|
|
}
|
|
|
- if ($action_name == 'urging') {
|
|
|
+ }
|
|
|
+ if ($controllername == 'approve') {
|
|
|
+ if (in_array($action_name, ['get_detail', 'get_info'])) {
|
|
|
$rule = [
|
|
|
- 'id|申请参数' => 'require|gt:0',
|
|
|
+ 'approve_id|审批参数' => 'require|gt:0',
|
|
|
];
|
|
|
$message = [];
|
|
|
$validate = new Validate($rule, $message);
|
|
@@ -288,11 +298,14 @@ class Base extends Controller
|
|
|
$this->error($validate->getError());
|
|
|
}
|
|
|
}
|
|
|
- if ($action_name == 'cancel') {
|
|
|
+ if ($action_name == 'audit') {
|
|
|
$rule = [
|
|
|
- 'id|申请参数' => 'require|gt:0',
|
|
|
+ 'approve_id|审批参数' => 'require|gt:0',
|
|
|
+ 'status|审批状态' => 'require|in:' . CommonConstant::STATUS_3 . ',' . CommonConstant::STATUS_4,
|
|
|
+ ];
|
|
|
+ $message = [
|
|
|
+ 'status.in' => '请选择正确的审批状态',
|
|
|
];
|
|
|
- $message = [];
|
|
|
$validate = new Validate($rule, $message);
|
|
|
if (!$validate->check($params)) {
|
|
|
$this->error($validate->getError());
|