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; $path = explode('/', $this->request->path()); 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 (!$authorization) { $this->error('请登录后操作', null, 0, -1); } try { $key = md5(config('app.jwt')); $check_authorization = JWT::decode($authorization, $key, array('HS256')); if ($check_authorization['code'] != 200) { $this->exception($check_authorization['msg']); } $authInfo = json_decode(json_encode($check_authorization['data']), true); if (!$authInfo) { $this->error('Token验证不通过,账号不存在', null, 0, -1); } $member = User::field('is_deleted', true) ->where('is_deleted', CommonConstant::IS_DELETED_0) ->find($authInfo['uid']); if (!$member) { $this->error('账号不存在', null, 0, -1); } if (!$member['status']) { $this->error('账号已经被禁用', null, 0, -1); } $this->user_id = $authInfo['uid']; $this->user = $member; return $this->user_id; } catch (\Exception $e) { $this->error($e->getMessage(), '', 0, -1); } } /** * token加密 **/ public function createJwt($uid, $facility_code = '') { $key = md5(config('app.jwt')); //jwt的签发密钥,验证token的时候需要用到 $time = time(); //签发时间 $expire = $time + config('app.jwt_time'); //过期时间 $token = array( "uid" => $uid, "iss" => "https://zain.com",//签发组织 "aud" => "https://zain.com", //签发作者 "iat" => $time, "nbf" => $time, "exp" => $expire, // "facility_code" => $facility_code, ); $jwt = JWT::encode($token, $key); return $jwt; } protected function setUid() { $authorization = app()->request->header('Authorization'); $key = md5(config('app.jwt')); 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']); $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']) { $this->user_id = $authInfo['uid']; $this->user_id; return true; } } } catch (\Exception $e) { return false; //$this->error($e->getMessage(),'',0,-1); } } // 验证短信验证码 protected function checkPhoneCode($phone, $code) { //return true; $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; } // 更新验证码状态 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 array $header 发送的 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 array $header 发送的 Header 信息 */ protected function error($msg = '', $data = null, $is_login = 1, $code = 0, $type = null, array $header = []) { 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 信息 * @return void * @throws HttpResponseException */ protected function results($msg, $data = null, $is_login, $code = 0, $type = null, array $header = []) { $result = [ 'code' => $code, 'is_login' => $is_login, 'msg' => $msg, 'time' => \think\facade\Request::instance()->server('REQUEST_TIME'), 'data' => $data, ]; // 如果未设置类型则自动判断 $type = $type ? $type : 'json'; if (isset($header['statuscode'])) { $code = $header['statuscode']; unset($header['statuscode']); } else { //未设置状态码,根据code值判断 $code = $code >= 1000 || $code < 200 ? 200 : $code; } $response = Response::create($result, $type, $code)->header($header); throw new HttpResponseException($response); } 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 checking($modulename, $controllername, $action_name) { $params = $this->request->post(); if ($controllername == 'approveflow') { if ($action_name == 'get_data') { $get_module_list = CommonConstant::get_module_list(); $rule = [ 'module|模块类型' => 'require|in:' . implode(',', array_keys($get_module_list)), ]; $message = [ 'module.in' => '请选择正确的模块类型', ]; $validate = new Validate($rule, $message); if (!$validate->check($params)) { $this->error($validate->getError()); } } } if ($controllername == 'approveinfo') { if ($action_name == 'create') { $get_way_list = CommonConstant::way; $get_module_list = CommonConstant::get_module_list(); $rule = [ 'way|方式' => 'require|in:' . implode(',', array_keys($get_way_list)), 'module|模块类型' => 'require|in:' . implode(',', array_keys($get_module_list)), ]; $message = [ 'way.in' => '请选择正确的方式', 'module.in' => '请选择正确的模块类型', ]; $validate = new Validate($rule, $message); if (!$validate->check($params)) { $this->error($validate->getError()); } $way = $this->request->post('way'); $id = $this->request->post('id'); if (!$id && in_array($way, [CommonConstant::update, CommonConstant::edit])) { $this->error('申请参数不能为空'); } $module = $this->request->post('module'); $validates = CommonConstant::get_module_validate_list(); $validate = $validates[$module]; $validate = new $validate; if (!$validate->check($params, [], $get_way_list[$way])) { $this->error($validate->getError()); } } if (in_array($action_name, ['get_detail', 'get_info', 'urging', 'cancel','download'])) { $rule = [ 'id|申请参数' => 'require|gt:0', ]; $message = []; $validate = new Validate($rule, $message); if (!$validate->check($params)) { $this->error($validate->getError()); } } if ($action_name == 'comment') { $rule = [ 'id|申请参数' => 'require|gt:0', 'comment_score|满意程度' => 'require', 'comment|评价内容' => 'require', ]; $message = [ 'comment_score.require' => '请选择满意程度', 'comment.require' => '请输入评价内容', ]; $validate = new Validate($rule, $message); if (!$validate->check($params)) { $this->error($validate->getError()); } } } if ($controllername == 'approve') { if (in_array($action_name, ['get_detail', 'get_info'])) { $rule = [ 'approve_id|审批参数' => 'require|gt:0', ]; $message = []; $validate = new Validate($rule, $message); if (!$validate->check($params)) { $this->error($validate->getError()); } } if ($action_name == 'audit') { $rule = [ 'approve_id|审批参数' => 'require|gt:0', 'status|审批状态' => 'require|in:' . CommonConstant::STATUS_3 . ',' . CommonConstant::STATUS_4, ]; $message = [ 'status.in' => '请选择正确的审批状态', ]; $validate = new Validate($rule, $message); if (!$validate->check($params)) { $this->error($validate->getError()); } } if ($action_name == 'feedback') { $rule = [ 'approve_id|审批参数' => 'require|gt:0', 'feedback|反馈结果' => 'require', ]; $message = [ 'feedback.require' => '请输入反馈结果', ]; $validate = new Validate($rule, $message); if (!$validate->check($params)) { $this->error($validate->getError()); } } } } }