Conf.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace app\service;
  3. use laytp\library\UploadDomain;
  4. use laytp\traits\Error;
  5. use think\facade\Cache;
  6. use think\facade\Config;
  7. /**
  8. * 系统配置服务器实现者
  9. * Class Auth
  10. * @package app\service
  11. */
  12. class Conf
  13. {
  14. use Error;
  15. // 是否使用redis,如果不想使用redis,修改此处为false即可
  16. protected $useRedis = true;
  17. // 数据库连接句柄
  18. protected $db = null;
  19. //判断是否配置了redis
  20. protected function hasRedis(){
  21. $redisConf = Config::get('cache');
  22. if(isset($redisConf['stores']['redis']['type']) && $this->useRedis){
  23. return true;
  24. }
  25. return false;
  26. }
  27. /**
  28. * 通过一个完整的key,获取配置信息
  29. * @param $wholeKey
  30. * @param $defaultValue
  31. * @return bool|mixed|string
  32. */
  33. public function get($wholeKey, $defaultValue='')
  34. {
  35. global $_W,$_GPC;
  36. if(!empty($_GPC['uniacid'])){
  37. $uniacid = $_GPC['uniacid'];
  38. }else{
  39. $uniacid = $_W['uniacid'];
  40. }
  41. if($this->hasRedis()){
  42. $redis = Cache::store('redis')->handler();
  43. if($redis){
  44. $value = $redis->hget($wholeKey, 'value');
  45. $formType = $redis->hget($wholeKey, 'form_type');
  46. if($formType == 'array') return json_decode($value, JSON_UNESCAPED_UNICODE);
  47. return $value ? $value : $defaultValue;
  48. }
  49. }
  50. list($group, $key) = $this->getGroupKey($wholeKey);
  51. $conf = \app\model\Conf::where(['group'=>$group, 'key'=>$key,'uniacid'=>$uniacid])->findOrEmpty()->toArray();
  52. if($conf){
  53. $value = $conf['value'];
  54. $formType = $conf['form_type'];
  55. if($formType == 'array') return json_decode($value, JSON_UNESCAPED_UNICODE);
  56. if($formType == 'upload'){
  57. $fileInfo = UploadDomain::multiJoin($value);
  58. $return[$key] = $value;
  59. if($fileInfo){
  60. $return[$key.'_path'] = $fileInfo['path'];
  61. $return[$key.'_filename'] = $fileInfo['filename'];
  62. }else{
  63. $return[$key.'_path'] = '';
  64. $return[$key.'_filename'] = '';
  65. }
  66. return $return;
  67. }
  68. return $value ? $value : $defaultValue;
  69. }else{
  70. return '';
  71. }
  72. }
  73. /**
  74. * 通过一个完整的key,设置配置信息
  75. * @param $wholeKey
  76. * @param $value
  77. * @return bool
  78. */
  79. public function set($wholeKey, $value)
  80. {
  81. global $_W,$_GPC;
  82. if(!empty($_GPC['uniacid'])){
  83. $uniacid = $_GPC['uniacid'];
  84. }else{
  85. $uniacid = $_W['uniacid'];
  86. }
  87. if(is_array($value)){
  88. $value = json_encode($value, JSON_UNESCAPED_UNICODE);
  89. }
  90. list($group, $key) = $this->getGroupKey($wholeKey);
  91. $id = \app\model\Conf::where(['group'=>$group, 'key'=>$key,'uniacid'=>$uniacid])->value('id');
  92. if($id){
  93. \app\model\Conf::where('id', '=', $id)->save(['group' => $group, 'key' => $key,'uniacid'=>$uniacid]);
  94. }else{
  95. \app\model\Conf::insert(['group' => $group, 'key' => $key, 'value' => $value,'uniacid'=>$uniacid]);
  96. }
  97. if($this->hasRedis()){
  98. $redis = Cache::store('redis')->handler();
  99. $redis->set($wholeKey, $value);
  100. }
  101. return true;
  102. }
  103. public function del($group, $key)
  104. {
  105. \app\model\Conf::where(['group' => $group, 'key' => $key])->delete();
  106. if($this->hasRedis()){
  107. $redis = Cache::store('redis')->handler();
  108. $redis->del($group . $key);
  109. }
  110. return ;
  111. }
  112. /**
  113. * 通过数组,设置配置信息
  114. * @param $array
  115. * @return bool
  116. */
  117. public function groupSet($array)
  118. {
  119. global $_W;
  120. foreach($array as $item){
  121. $item['value'] = is_array($item['value']) ? json_encode($item['value'], JSON_UNESCAPED_UNICODE) : $item['value'];
  122. $id = \app\model\Conf::where(['group'=>$item['group'], 'key'=>$item['key'],'uniacid' => $_W['uniacid']])->value('id');
  123. if($id){
  124. \app\model\Conf::where('id', '=', $id)->save($item);
  125. }else{
  126. \app\model\Conf::create($item);
  127. }
  128. if($this->hasRedis()){
  129. $redis = Cache::store('redis')->handler();
  130. $hashKey = $item['group'] . '.' . $item['key'];
  131. $redis->hset($hashKey, 'group', $item['group']);
  132. $redis->hset($hashKey, 'key', $item['key']);
  133. $redis->hset($hashKey, 'value', $item['value']);
  134. $redis->hset($hashKey, 'form_type', $item['form_type']);
  135. }
  136. }
  137. return true;
  138. }
  139. /**
  140. * 通过配置分组名称,获取整个分组的信息
  141. * @param $group
  142. * @param $onlyMysql boolean 是否仅从数据库取配置
  143. * @return array
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\DbException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. */
  148. public function groupGet($group, $onlyMysql=false)
  149. {
  150. global $_W,$_GPC;
  151. if(!empty($_GPC['uniacid'])){
  152. $where = ['group'=>$group,'uniacid' => $_GPC['uniacid']];
  153. }else{
  154. $where = ['group'=>$group,'uniacid' => $_W['uniacid']];
  155. }
  156. $return = [];
  157. $items = [];
  158. if(!$onlyMysql){
  159. if($this->hasRedis()){
  160. $redis = Cache::store('redis')->handler();
  161. $keys = $redis->keys($group.'*');
  162. foreach($keys as $key){
  163. $items[$key] = $redis->hGetAll($key);
  164. }
  165. if(!$items){
  166. $items = \app\model\Conf::where($where)->select()->toArray();
  167. }
  168. }else{
  169. $items = \app\model\Conf::where($where)->select()->toArray();
  170. }
  171. }else{
  172. $items = \app\model\Conf::where($where)->select()->toArray();
  173. }
  174. foreach ($items as $k => $v) {
  175. if ($v['form_type'] === 'array') {
  176. $array = json_decode($v['value'], true);
  177. if(!$array){
  178. $return[$v['key']] = [""=>""];
  179. }else{
  180. $return[$v['key']] =$array;
  181. }
  182. } elseif($v['form_type'] === 'upload') {
  183. $fileInfo = UploadDomain::multiJoin($v['value']);
  184. if($fileInfo){
  185. $return[$v['key']] = $fileInfo['id'];
  186. $return[$v['key'].'_path'] = $fileInfo['path'];
  187. $return[$v['key'].'_filename'] = $fileInfo['filename'];
  188. }else{
  189. $return[$v['key']] = '';
  190. $return[$v['key'].'_path'] = '';
  191. $return[$v['key'].'_filename'] = '';
  192. }
  193. } else {
  194. $return[$v['key']] = $v['value'];
  195. }
  196. }
  197. return $return;
  198. }
  199. /**
  200. * 通过完整的key,获取到分组名称和key值
  201. * @param $wholeKey
  202. * @return array|boolean
  203. */
  204. protected function getGroupKey($wholeKey)
  205. {
  206. $arr = explode('.', $wholeKey);
  207. if(!$arr || count($arr) == 1){
  208. $this->setError('请输入一个完整的key,一个完整的key必须包含至少一个.号');
  209. return false;
  210. }
  211. $key = $arr[count($arr) - 1];
  212. $group = substr($wholeKey, 0 , strrpos($wholeKey, '.'));
  213. return [$group, $key];
  214. }
  215. }