Guild.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * 开发联盟的公共控制器类
  4. *
  5. */
  6. namespace app\guildapi\controller;
  7. use app\common\controller\Base;
  8. use think\Config;
  9. class Guild extends Base
  10. {
  11. protected $channelIds; //登录渠道自己及子渠道ID集
  12. protected $sub_channelIds; //登录渠道子渠道ID集(不包含自身)
  13. protected $_channelLevel; //渠道等级
  14. protected $current_node; //当前操作的action
  15. protected $_adminId; //当前操作的渠道管理员ID
  16. protected $_channelIdPath; //当前渠道路径
  17. /**
  18. * 初始化操作
  19. */
  20. protected function _initialize()
  21. {
  22. // 后台代码跨域
  23. $this->handleCors();
  24. //已登录
  25. if (session('guild_info')) {
  26. $channelModel = model('Common/Channel');
  27. $channelIds = $channelModel->getChildIds(session('guild_info')['channel_id']);
  28. $this->sub_channelIds = $channelIds;
  29. if (!empty($channelIds)) {
  30. array_push($channelIds, session('guild_info')['channel_id']);
  31. $this->channelIds = $channelIds;
  32. } else {
  33. $this->channelIds = session('guild_info')['channel_id'];
  34. }
  35. $this->_channelLevel = get_channel_level(session('guild_info')['channel_id']);
  36. $this->_channelId = session('guild_info')['channel_id'];
  37. $this->_channelName = session('guild_info')['channel_name'];
  38. $this->_adminId = session('guild_info')['id'];
  39. $ParentIdPath = model('Channel')->where(['id' => $this->_channelId])->value('id_path');
  40. if ($ParentIdPath) {
  41. $this->_channelIdPath = $ParentIdPath . $this->_channelId . ',';
  42. } else {
  43. $this->_channelIdPath = ',' . $this->_channelId . ',';
  44. }
  45. } else {
  46. json(['data' => '', 'code' => 10007, 'msg' => '您还没有登录!'])->send();
  47. exit;
  48. }
  49. $this->input = input();
  50. }
  51. /**
  52. * 后端解决跨域
  53. */
  54. public function handleCors()
  55. {
  56. header('Access-Control-Allow-Origin: *');
  57. header("Access-Control-Allow-Credentials: true");
  58. header("Access-Control-Allow-Methods: *");
  59. header("Access-Control-Allow-Headers: Origin, Content-Type, Cookie, X-CSRF-TOKEN, Accept, Authorization, X-XSRF-TOKEN, X-Token");
  60. header("Access-Control-Expose-Headers: *");
  61. }
  62. /**
  63. * 插入操作日志
  64. *
  65. * @param string $node
  66. * @param string $content
  67. * @param number $type
  68. * @param string $admin_id
  69. * @param string $username
  70. * @return mixed
  71. */
  72. protected function insertLog($node = '', $content = '', $type = 0, $admin_id = '', $username = '')
  73. {
  74. if (empty($admin_id)) {
  75. $admin_id = session('guild_info')['id'] ?: 1;
  76. }
  77. if (empty($username)) {
  78. $username = session('guild_info')['username'] ?: '';
  79. }
  80. return \app\common\logic\Log::insertOperateLog($node, $content, $type, $admin_id, $username);
  81. }
  82. /**
  83. * 获取参数
  84. * @param string $key 数组index
  85. * @param string $default 默认值
  86. * @return mixed|null
  87. */
  88. protected function input($key, $default = null)
  89. {
  90. return isset($this->input[$key]) ? $this->input[$key] : ($default !== null ? $default : null);
  91. }
  92. /**
  93. * 返回封装后的 API json数据到客户端
  94. * @access protected
  95. * @param mixed $data 要返回的数据
  96. * @param int $code 返回的 code
  97. * @param mixed $msg 提示信息
  98. * @param $isExit boolean 是否强制退出,默认true
  99. * @return void
  100. *
  101. */
  102. public function jsonResult($data, $code = 0, $msg = '', $isExit = true)
  103. {
  104. $result = [
  105. 'code' => $code,
  106. 'msg' => $msg,
  107. 'time' => request()->server('REQUEST_TIME'),
  108. 'data' => $data,
  109. ];
  110. echo json_encode($result);
  111. //为了使用fastcgi_finish_request等函数时,后续的执行能够继续生效
  112. if ($isExit) exit;
  113. }
  114. }