Config.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\model\system;
  11. use think\facade\Cache;
  12. use app\model\BaseModel;
  13. /**
  14. * 系统配置类
  15. */
  16. class Config extends BaseModel
  17. {
  18. /**
  19. * 配置系统配置项
  20. * @param array $value
  21. * @param string $config_desc
  22. * @param int $is_use
  23. * @param array $condition
  24. */
  25. public function setConfig($value, $config_desc, $is_use, $condition)
  26. {
  27. $check_condition = array_column($condition, 2, 0);
  28. $site_id = isset($check_condition[ 'site_id' ]) ? $check_condition[ 'site_id' ] : '';
  29. if ($site_id === '') {
  30. return $this->error('', 'REQUEST_SITE_ID');
  31. }
  32. $app_module = isset($check_condition[ 'app_module' ]) ? $check_condition[ 'app_module' ] : '';
  33. if ($app_module === '') {
  34. return $this->error('', 'REQUEST_APP_MODULE');
  35. }
  36. $config_key = isset($check_condition[ 'config_key' ]) ? $check_condition[ 'config_key' ] : '';
  37. if (empty($config_key)) {
  38. return $this->error('', 'REQUEST_CONFIG_KEY');
  39. }
  40. $data = $check_condition;
  41. $data[ 'value' ] = json_encode($value);
  42. $data[ 'config_desc' ] = $config_desc;
  43. $data[ 'is_use' ] = $is_use;
  44. $json_condition = json_encode($condition);
  45. $config_model = model('config');
  46. $info = $config_model->getInfo($condition, 'id');
  47. Cache::tag("config")->clear();
  48. Cache::tag("config")->set("CONFIG_" . $json_condition, "");
  49. if (empty($info)) {
  50. $data[ 'create_time' ] = time();
  51. $res = $config_model->add($data);
  52. } else {
  53. $data[ 'modify_time' ] = time();
  54. $res = $config_model->update($data, $condition);
  55. }
  56. return $this->success($res);
  57. }
  58. /**
  59. * 获取系统配置信息
  60. * @param array $condition
  61. */
  62. public function getConfig($condition)
  63. {
  64. $json_condition = json_encode($condition);
  65. $cache = Cache::get("CONFIG_" . $json_condition, "");
  66. if (!empty($cache)) {
  67. return $this->success($cache);
  68. }
  69. $check_condition = array_column($condition, 2, 0);
  70. $site_id = isset($check_condition[ 'site_id' ]) ? $check_condition[ 'site_id' ] : '';
  71. if ($site_id === '') {
  72. return $this->error('', 'REQUEST_SITE_ID');
  73. }
  74. $app_module = isset($check_condition[ 'app_module' ]) ? $check_condition[ 'app_module' ] : '';
  75. if ($app_module === '') {
  76. return $this->error('', 'REQUEST_APP_MODULE');
  77. }
  78. $config_key = isset($check_condition[ 'config_key' ]) ? $check_condition[ 'config_key' ] : '';
  79. if (empty($config_key)) {
  80. return $this->error('', 'REQUEST_CONFIG_KEY');
  81. }
  82. $info = model('config')->getInfo($condition, 'site_id, app_module, config_key, value, config_desc, is_use, create_time, modify_time');
  83. if (!empty($info)) {
  84. $info[ 'value' ] = json_decode($info[ 'value' ], true);
  85. } else {
  86. $info = [
  87. 'site_id' => $site_id,
  88. 'app_module' => $app_module,
  89. 'config_key' => $config_key,
  90. 'value' => [],
  91. 'config_desc' => '',
  92. 'is_use' => 0,
  93. 'create_time' => 0,
  94. 'modify_time' => 0
  95. ];
  96. }
  97. Cache::tag("config")->set("CONFIG_" . $json_condition, $info);
  98. return $this->success($info);
  99. }
  100. /**
  101. * 修改配置项的使用状态
  102. * @param int $is_use
  103. * @param array $condition
  104. */
  105. public function modifyConfigIsUse($is_use, $condition)
  106. {
  107. $json_condition = json_encode($condition);
  108. $config = model('config')->getInfo($condition);
  109. if (!empty($config)) {
  110. //配置过
  111. $res = model('config')->update([ 'is_use' => $is_use ], $condition);
  112. Cache::tag("config")->set("CONFIG_" . $json_condition, "");
  113. return $this->success($res);
  114. } else {
  115. return $this->error('', 'CONFIG_NOT_EXIST');
  116. }
  117. }
  118. /**
  119. * 获取系统信息
  120. */
  121. public function getSystemConfig()
  122. {
  123. $system_config[ 'os' ] = php_uname(); // 服务器操作系统
  124. $system_config[ 'server_software' ] = $_SERVER[ 'SERVER_SOFTWARE' ]; // 服务器环境
  125. $system_config[ 'upload_max_filesize' ] = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknow'; // 文件上传限制
  126. $system_config[ 'gd_version' ] = gd_info()[ 'GD Version' ]; // GD(图形处理)版本
  127. $system_config[ 'max_execution_time' ] = ini_get("max_execution_time") . "秒"; // 最大执行时间
  128. $system_config[ 'port' ] = $_SERVER[ 'SERVER_PORT' ]; // 端口
  129. $system_config[ 'dns' ] = $_SERVER[ 'HTTP_HOST' ]; // 服务器域名
  130. $system_config[ 'php_version' ] = PHP_VERSION; // php版本
  131. $system_config[ 'sockets' ] = extension_loaded('sockets'); //是否支付sockets
  132. $system_config[ 'openssl' ] = extension_loaded('openssl'); //是否支付openssl
  133. $system_config[ 'curl' ] = function_exists('curl_init'); // 是否支持curl功能
  134. $system_config[ 'upload_dir_jurisdiction' ] = check_dir_iswritable(realpath('./upload') . DIRECTORY_SEPARATOR); // upload目录读写权限
  135. $system_config[ 'runtime_dir_jurisdiction' ] = check_dir_iswritable(realpath('./runtime') . DIRECTORY_SEPARATOR); // runtime目录读写权限
  136. $system_config[ 'fileinfo' ] = extension_loaded('fileinfo'); //是否支付fileinfo
  137. return $this->success($system_config);
  138. }
  139. /**
  140. * 删除配置
  141. * @param $condition
  142. */
  143. public function deleteConfig($condition)
  144. {
  145. $res = model('config')->delete($condition);
  146. $this->success($res);
  147. }
  148. }