* @version : HUOSDK 8.0 */ namespace agent\common\controller; use huo\controller\agent\AgentCache; use huo\controller\common\HuoSession; use huo\controller\wap\Option; use huo\model\user\AdminLoginLogModel; use huo\model\user\AdminOperateLog; use huolib\constant\FormatConst; use huolib\constant\GameConst; use huolib\status\CommonStatus; use huolib\status\MemberStatus; use huolib\tool\Ip; use think\exception\HttpResponseException; use think\Response; class AgentBaseController extends BaseController { public $agent_id = 0; protected $response_type; protected $classify; function _initialize() { parent::_initialize(); $this->agent_id = HuoSession::getAgentId(); $this->response_type = $this->request->param('format/s', FormatConst::FORMAT_HTML); if (APP_DEBUG) { $_debug_agent_id = $this->request->param('debug_agent_id/d', 0); if (!empty($_debug_agent_id)) { $this->agent_id = $_debug_agent_id; HuoSession::setAgentId($this->agent_id); } } $this->classify = ''; } /** * 检查是否登陆 * * @param int $agent_id * * @return int */ public function checkLogin($agent_id = 0) { $_agent_id = $agent_id; if (empty($_agent_id)) { $_agent_id = $this->agent_id; if (empty($_agent_id)) { $_code = MemberStatus::LOGIN_IS_OUT; $this->error(lang(MemberStatus::getMsg($_code)), [], $_code); } } return $_agent_id; } /** * 获取当前的response 输出类型 * * @access protected * @return string */ protected function getResponseType() { $_is_ajax = $this->request->isAjax(); $this->response_type = $_is_ajax ? config('default_ajax_return') : $this->response_type; return $this->response_type; } /** * 获取当前登录用户的id * * @return int */ public function getAgentId() { return $this->agent_id; } /** * 返回数据 * * @param array $data */ protected function returnData($data = []) { $_msg = $data['msg']; $_code = $data['code']; $_data = $data['data']; if (CommonStatus::NO_ERROR != $_code) { $this->error($_msg, $_data, $_code); } $this->success($_msg, $_data, $_code); } /** * 操作错误跳转的快捷方法 * * @access protected * * @param mixed $msg 提示信息,若要指定错误码,可以传数组,格式为['code'=>您的错误码,'msg'=>'您的错误消息'] * @param mixed $data 返回的数据 * @param int $code * * @param array $header 发送的Header信息 * * @param null $url * @param int $wait * * @return void */ protected function error($msg = '', $data = '', $code = 400, array $header = [], $url = null, $wait = 3) { $this->insertActionLog(1,$msg.'||'.json_encode($data)); $type = $this->getResponseType(); if (FormatConst::FORMAT_HTML == $type) { parent::error($msg, $data, $code, $header, $url, $wait); } if (empty($data)) { $data = null; } $result = [ 'code' => $code, 'msg' => $msg, 'data' => $data, ]; $response = Response::create($result, $type)->header($header); throw new HttpResponseException($response); } /** * 插入后台操作记录 * * @param int $type 操作类型,1操作成功, 2 操作失败 * @param $remark */ protected function insertActionLog($type = 0, $remark) { $_user_id = cmf_get_current_admin_id(); if (empty($_user_id)) { $_user_id = 0; $_username = ''; } else { $_agent_data = AgentCache::ins()->getInfoByAgentId($_user_id); $_username = $_agent_data['user_login']; } $_data['user_id'] = $_user_id; $_data['username'] = $_username; $_data['type'] = $type; $_data['ip'] = $this->request->ip(0, true); $_data['addr'] = Ip::getIpHome($_data['ip']); $_data['action'] = $this->request->url(true); $_data['create_time'] = time(); $_data['param'] = $this->request->method().':'.http_build_query($this->request->param()); $_data['remark'] = $remark; (new AdminOperateLog())->addLog($_data); } /** * 插入登陆记录 * * @param int $type 1 表示登陆 2输入网址再次登陆,3表示登出 */ protected function insertLoginLog($type = 0) { $_user_id = cmf_get_current_admin_id(); if (empty($_user_id)) { $_user_id = 0; $_username = ''; } else { $_agent_data = AgentCache::ins()->getInfoByAgentId($_user_id); $_username = $_agent_data['user_login']; } $_data['user_id'] = $_user_id; $_data['username'] = $_username; $_data['type'] = $type; $_data['ip'] = $this->request->ip(0, true); $_data['addr'] = Ip::getIpHome($_data['ip']); $_data['device_info'] = $_SERVER["HTTP_USER_AGENT"]; $_data['login_time'] = time(); (new AdminLoginLogModel())->addLog($_data); } /** * 操作成功跳转的快捷方法 * * @access protected * * @param mixed $msg 提示信息 * @param mixed $data 返回的数据 * @param array $header 发送的Header信息 * * @param int $code 返回码 * * @return void */ protected function success($msg = '', $data = '', $code = 200, array $header = [], $url = null, $wait = 3) { $this->insertActionLog(2,$msg.'||'.json_encode($data)); $type = $this->getResponseType(); if (FormatConst::FORMAT_HTML == $type) { parent::success($msg, $data, $code, $header, $url, $wait); } if (empty($data)) { $data = null; } $result = [ 'code' => $code, 'msg' => $msg, 'data' => $data, ]; $header['Access-Control-Allow-Origin'] = '*'; $header['Access-Control-Allow-Headers'] = 'X-Requested-With,Content-Type,HS-Device-Type,HS-Token,HS-Lang'; $header['Access-Control-Allow-Methods'] = 'GET,POST,PATCH,PUT,DELETE,OPTIONS'; $response = Response::create($result, $type)->header($header); throw new HttpResponseException($response); } /** * 获取网页基本信息 */ protected function getWebBasic() { $_m = new Option(); $_setting_name = 'agent_basic'; $_agent_basic_item = $_m->getOptionData($_setting_name); $_setting_name = 'agent_index_seo'; $_agent_index_seo_item = $_m->getOptionData($_setting_name); $_setting_name = 'agent_icon'; $_agent_icon_item = $_m->getOptionData($_setting_name); $_setting_name = 'agent_content'; $_agent_content_item = $_m->getOptionData($_setting_name); $_agent_basic = $_agent_index_seo = $_agent_icon = $_agent_content = $_agent_qq = []; if (!empty($_agent_basic_item['option_value'])) { $_agent_basic = json_decode($_agent_basic_item['option_value'], true); } if (!empty($_agent_index_seo_item['option_value'])) { $_agent_index_seo = json_decode($_agent_index_seo_item['option_value'], true); } if (!empty($_agent_icon_item['option_value'])) { $_agent_icon = json_decode($_agent_icon_item['option_value'], true); if (isset($_agent_icon['agent_logo'])) { $_agent_icon['agent_logo'] = cmf_get_image_url($_agent_icon['agent_logo']); } } if (!empty($_agent_content_item['option_value'])) { $_agent_content = json_decode($_agent_content_item['option_value'], true); $_agent_qq = explode(',', $_agent_content['service_qq']); } $_data = [ 'title' => isset($_agent_index_seo['title']) ? $_agent_index_seo['title'] : 'title', 'keyword' => isset($_agent_index_seo['keyword']) ? $_agent_index_seo['keyword'] : 'keyword', 'description' => isset($_agent_index_seo['description']) ? $_agent_index_seo['description'] : 'description', 'company_name' => isset($_agent_basic['company_name']) ? $_agent_basic['company_name'] : 'company_name', 'company_address' => isset($_agent_basic['company_address']) ? $_agent_basic['company_address'] : 'company_address', 'culture_management' => isset($_agent_basic['culture_management']) ? $_agent_basic['culture_management'] : 'culture_management', 'logo' => isset($_agent_icon['agent_logo']) ? $_agent_icon['agent_logo'] : 'logo', 'icp' => isset($_agent_basic['record_number']) ? $_agent_basic['record_number'] : 'record_number', 'qq' => [] ]; if (!empty($_agent_qq)) { foreach ($_agent_qq as $_k => $_v) { $_num = $_k + 1; $_data['qq'][] = [ 'name' => "客服".$_num, 'qq_num' => $_v, ]; } } $this->assign('web_basic', $_data); // print_r($_data); // exit; return $_data; } }