AgentBaseController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * AgentBaseController.php UTF-8
  4. * 渠道通用控制器
  5. *
  6. * @date : 2018/3/2 11:28
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : linjiebin <ljb@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace agent\common\controller;
  13. use huo\controller\agent\AgentCache;
  14. use huo\controller\common\HuoSession;
  15. use huo\controller\wap\Option;
  16. use huo\model\user\AdminLoginLogModel;
  17. use huo\model\user\AdminOperateLog;
  18. use huolib\constant\FormatConst;
  19. use huolib\constant\GameConst;
  20. use huolib\status\CommonStatus;
  21. use huolib\status\MemberStatus;
  22. use huolib\tool\Ip;
  23. use think\exception\HttpResponseException;
  24. use think\Response;
  25. class AgentBaseController extends BaseController {
  26. public $agent_id = 0;
  27. protected $response_type;
  28. protected $classify;
  29. function _initialize() {
  30. parent::_initialize();
  31. $this->agent_id = HuoSession::getAgentId();
  32. $this->response_type = $this->request->param('format/s', FormatConst::FORMAT_HTML);
  33. if (APP_DEBUG) {
  34. $_debug_agent_id = $this->request->param('debug_agent_id/d', 0);
  35. if (!empty($_debug_agent_id)) {
  36. $this->agent_id = $_debug_agent_id;
  37. HuoSession::setAgentId($this->agent_id);
  38. }
  39. }
  40. $this->classify = '';
  41. }
  42. /**
  43. * 检查是否登陆
  44. *
  45. * @param int $agent_id
  46. *
  47. * @return int
  48. */
  49. public function checkLogin($agent_id = 0) {
  50. $_agent_id = $agent_id;
  51. if (empty($_agent_id)) {
  52. $_agent_id = $this->agent_id;
  53. if (empty($_agent_id)) {
  54. $_code = MemberStatus::LOGIN_IS_OUT;
  55. $this->error(lang(MemberStatus::getMsg($_code)), [], $_code);
  56. }
  57. }
  58. return $_agent_id;
  59. }
  60. /**
  61. * 获取当前的response 输出类型
  62. *
  63. * @access protected
  64. * @return string
  65. */
  66. protected function getResponseType() {
  67. $_is_ajax = $this->request->isAjax();
  68. $this->response_type = $_is_ajax ? config('default_ajax_return') : $this->response_type;
  69. return $this->response_type;
  70. }
  71. /**
  72. * 获取当前登录用户的id
  73. *
  74. * @return int
  75. */
  76. public function getAgentId() {
  77. return $this->agent_id;
  78. }
  79. /**
  80. * 返回数据
  81. *
  82. * @param array $data
  83. */
  84. protected function returnData($data = []) {
  85. $_msg = $data['msg'];
  86. $_code = $data['code'];
  87. $_data = $data['data'];
  88. if (CommonStatus::NO_ERROR != $_code) {
  89. $this->error($_msg, $_data, $_code);
  90. }
  91. $this->success($_msg, $_data, $_code);
  92. }
  93. /**
  94. * 操作错误跳转的快捷方法
  95. *
  96. * @access protected
  97. *
  98. * @param mixed $msg 提示信息,若要指定错误码,可以传数组,格式为['code'=>您的错误码,'msg'=>'您的错误消息']
  99. * @param mixed $data 返回的数据
  100. * @param int $code
  101. *
  102. * @param array $header 发送的Header信息
  103. *
  104. * @param null $url
  105. * @param int $wait
  106. *
  107. * @return void
  108. */
  109. protected function error($msg = '', $data = '', $code = 400, array $header = [], $url = null, $wait = 3) {
  110. $this->insertActionLog(1,$msg.'||'.json_encode($data));
  111. $type = $this->getResponseType();
  112. if (FormatConst::FORMAT_HTML == $type) {
  113. parent::error($msg, $data, $code, $header, $url, $wait);
  114. }
  115. if (empty($data)) {
  116. $data = null;
  117. }
  118. $result = [
  119. 'code' => $code,
  120. 'msg' => $msg,
  121. 'data' => $data,
  122. ];
  123. $response = Response::create($result, $type)->header($header);
  124. throw new HttpResponseException($response);
  125. }
  126. /**
  127. * 插入后台操作记录
  128. *
  129. * @param int $type 操作类型,1操作成功, 2 操作失败
  130. * @param $remark
  131. */
  132. protected function insertActionLog($type = 0, $remark) {
  133. $_user_id = cmf_get_current_admin_id();
  134. if (empty($_user_id)) {
  135. $_user_id = 0;
  136. $_username = '';
  137. } else {
  138. $_agent_data = AgentCache::ins()->getInfoByAgentId($_user_id);
  139. $_username = $_agent_data['user_login'];
  140. }
  141. $_data['user_id'] = $_user_id;
  142. $_data['username'] = $_username;
  143. $_data['type'] = $type;
  144. $_data['ip'] = $this->request->ip(0, true);
  145. $_data['addr'] = Ip::getIpHome($_data['ip']);
  146. $_data['action'] = $this->request->url(true);
  147. $_data['create_time'] = time();
  148. $_data['param'] = $this->request->method().':'.http_build_query($this->request->param());
  149. $_data['remark'] = $remark;
  150. (new AdminOperateLog())->addLog($_data);
  151. }
  152. /**
  153. * 插入登陆记录
  154. *
  155. * @param int $type 1 表示登陆 2输入网址再次登陆,3表示登出
  156. */
  157. protected function insertLoginLog($type = 0) {
  158. $_user_id = cmf_get_current_admin_id();
  159. if (empty($_user_id)) {
  160. $_user_id = 0;
  161. $_username = '';
  162. } else {
  163. $_agent_data = AgentCache::ins()->getInfoByAgentId($_user_id);
  164. $_username = $_agent_data['user_login'];
  165. }
  166. $_data['user_id'] = $_user_id;
  167. $_data['username'] = $_username;
  168. $_data['type'] = $type;
  169. $_data['ip'] = $this->request->ip(0, true);
  170. $_data['addr'] = Ip::getIpHome($_data['ip']);
  171. $_data['device_info'] = $_SERVER["HTTP_USER_AGENT"];
  172. $_data['login_time'] = time();
  173. (new AdminLoginLogModel())->addLog($_data);
  174. }
  175. /**
  176. * 操作成功跳转的快捷方法
  177. *
  178. * @access protected
  179. *
  180. * @param mixed $msg 提示信息
  181. * @param mixed $data 返回的数据
  182. * @param array $header 发送的Header信息
  183. *
  184. * @param int $code 返回码
  185. *
  186. * @return void
  187. */
  188. protected function success($msg = '', $data = '', $code = 200, array $header = [], $url = null, $wait = 3) {
  189. $this->insertActionLog(2,$msg.'||'.json_encode($data));
  190. $type = $this->getResponseType();
  191. if (FormatConst::FORMAT_HTML == $type) {
  192. parent::success($msg, $data, $code, $header, $url, $wait);
  193. }
  194. if (empty($data)) {
  195. $data = null;
  196. }
  197. $result = [
  198. 'code' => $code,
  199. 'msg' => $msg,
  200. 'data' => $data,
  201. ];
  202. $header['Access-Control-Allow-Origin'] = '*';
  203. $header['Access-Control-Allow-Headers'] = 'X-Requested-With,Content-Type,HS-Device-Type,HS-Token,HS-Lang';
  204. $header['Access-Control-Allow-Methods'] = 'GET,POST,PATCH,PUT,DELETE,OPTIONS';
  205. $response = Response::create($result, $type)->header($header);
  206. throw new HttpResponseException($response);
  207. }
  208. /**
  209. * 获取网页基本信息
  210. */
  211. protected function getWebBasic() {
  212. $_m = new Option();
  213. $_setting_name = 'agent_basic';
  214. $_agent_basic_item = $_m->getOptionData($_setting_name);
  215. $_setting_name = 'agent_index_seo';
  216. $_agent_index_seo_item = $_m->getOptionData($_setting_name);
  217. $_setting_name = 'agent_icon';
  218. $_agent_icon_item = $_m->getOptionData($_setting_name);
  219. $_setting_name = 'agent_content';
  220. $_agent_content_item = $_m->getOptionData($_setting_name);
  221. $_agent_basic = $_agent_index_seo = $_agent_icon = $_agent_content = $_agent_qq = [];
  222. if (!empty($_agent_basic_item['option_value'])) {
  223. $_agent_basic = json_decode($_agent_basic_item['option_value'], true);
  224. }
  225. if (!empty($_agent_index_seo_item['option_value'])) {
  226. $_agent_index_seo = json_decode($_agent_index_seo_item['option_value'], true);
  227. }
  228. if (!empty($_agent_icon_item['option_value'])) {
  229. $_agent_icon = json_decode($_agent_icon_item['option_value'], true);
  230. if (isset($_agent_icon['agent_logo'])) {
  231. $_agent_icon['agent_logo'] = cmf_get_image_url($_agent_icon['agent_logo']);
  232. }
  233. }
  234. if (!empty($_agent_content_item['option_value'])) {
  235. $_agent_content = json_decode($_agent_content_item['option_value'], true);
  236. $_agent_qq = explode(',', $_agent_content['service_qq']);
  237. }
  238. $_data = [
  239. 'title' => isset($_agent_index_seo['title']) ? $_agent_index_seo['title'] : 'title',
  240. 'keyword' => isset($_agent_index_seo['keyword']) ? $_agent_index_seo['keyword'] : 'keyword',
  241. 'description' => isset($_agent_index_seo['description']) ? $_agent_index_seo['description']
  242. : 'description',
  243. 'company_name' => isset($_agent_basic['company_name']) ? $_agent_basic['company_name']
  244. : 'company_name',
  245. 'company_address' => isset($_agent_basic['company_address']) ? $_agent_basic['company_address']
  246. : 'company_address',
  247. 'culture_management' => isset($_agent_basic['culture_management']) ? $_agent_basic['culture_management']
  248. : 'culture_management',
  249. 'logo' => isset($_agent_icon['agent_logo']) ? $_agent_icon['agent_logo'] : 'logo',
  250. 'icp' => isset($_agent_basic['record_number']) ? $_agent_basic['record_number']
  251. : 'record_number',
  252. 'qq' => []
  253. ];
  254. if (!empty($_agent_qq)) {
  255. foreach ($_agent_qq as $_k => $_v) {
  256. $_num = $_k + 1;
  257. $_data['qq'][] = [
  258. 'name' => "客服".$_num,
  259. 'qq_num' => $_v,
  260. ];
  261. }
  262. }
  263. $this->assign('web_basic', $_data);
  264. // print_r($_data);
  265. // exit;
  266. return $_data;
  267. }
  268. }