ConfigService.php 15.7 KB
Newer Older
D
v1.2.0  
devil_gong 已提交
1 2 3 4
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
D
2.0  
Devil 已提交
5
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
D
v1.2.0  
devil_gong 已提交
6
// +----------------------------------------------------------------------
D
2.0  
Devil 已提交
7
// | Licensed ( https://opensource.org/licenses/mit-license.php )
D
v1.2.0  
devil_gong 已提交
8 9 10 11 12
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;

D
Devil 已提交
13
use think\facade\Db;
14
use app\service\ResourcesService;
D
v1.2.0  
devil_gong 已提交
15 16 17 18 19 20 21 22 23 24

/**
 * 配置服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class ConfigService
{
D
Devil 已提交
25 26 27 28 29 30
    // 不参与缓存的配置
    public static $not_cache_field_list = [
        'common_agreement_userregister',
        'common_agreement_userprivacy',
    ];

D
devil_gong 已提交
31 32
    // 富文本,不实例化的字段
    public static $rich_text_list = [
D
Devil 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
        'common_agreement_userregister',
        'common_agreement_userprivacy',
        'common_email_currency_template',
        'home_footer_info',
        'home_email_user_reg',
        'home_email_user_forget_pwd',
        'home_email_user_email_binding',
        'home_site_close_reason',
        'common_self_extraction_address',
        'home_index_floor_top_right_keywords',
        'home_index_floor_manual_mode_goods',
        'home_index_floor_left_top_category',
        'admin_email_login_template',
        'home_email_login_template',
        'home_site_security_record_url',
    ];
D
devil_gong 已提交
49

G
gongfuxiang 已提交
50 51 52 53
    // 附件字段列表
    public static $attachment_field_list = [
        'home_site_logo',
        'home_site_logo_wap',
G
gongfuxiang 已提交
54
        'home_site_logo_square',
G
gongfuxiang 已提交
55 56
        'common_customer_store_qrcode',
        'home_site_user_register_bg_images',
D
devil_gong 已提交
57 58 59 60 61 62 63 64 65 66 67
        'home_site_user_login_ad1_images',
        'home_site_user_login_ad2_images',
        'home_site_user_login_ad3_images',
        'home_site_user_forgetpwd_ad1_images',
        'home_site_user_forgetpwd_ad2_images',
        'home_site_user_forgetpwd_ad3_images',
    ];

    // 字符串转数组字段列表, 默认使用英文逗号处理 [ , ]
    public static $string_to_array_field_list = [
        'common_images_verify_rules',
68 69 70
        'home_user_login_type',
        'home_user_reg_type',
        'admin_login_type',
D
Devil 已提交
71
        'home_search_params_type',
G
gongfuxiang 已提交
72 73
    ];

D
Devil 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    // 需要文件缓存的key
    public static $file_cache_keys = [
        // 伪静态后缀
        'home_seo_url_html_suffix',

        // 前端默认主题
        'common_default_theme',

        // 时区
        'common_timezone',

        // 是否开启redis缓存
        'common_data_is_use_cache',
        'common_cache_data_redis_host',
        'common_cache_data_redis_port',
        'common_cache_data_redis_password',
        'common_cache_data_redis_expire',
        'common_cache_data_redis_prefix',

        // session是否开启redis缓存
        'common_session_is_use_cache',
        'common_cache_session_redis_prefix',

        // cdn地址
        'common_cdn_attachment_host',
        'common_cdn_public_host',

        // 编辑器配置信息
        'home_max_limit_image',
        'home_max_limit_video',
        'home_max_limit_file',
G
gongfuxiang 已提交
105 106 107

        // 是否采用https连接商店
        'common_is_https_connect_store',
D
Devil 已提交
108 109
    ];

D
v1.2.0  
devil_gong 已提交
110 111 112 113 114 115 116 117 118
    /**
     * 配置列表,唯一标记作为key
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-07
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
119
    public static function ConfigList($params = [])
D
v1.2.0  
devil_gong 已提交
120 121
    {
        $field = isset($params['field']) ? $params['field'] : 'only_tag,name,describe,value,error_tips';
D
Devil 已提交
122
        $data = Db::name('Config')->column($field, 'only_tag');
123 124 125 126 127 128 129 130 131
        if(!empty($data))
        {
            foreach($data as $k=>&$v)
            {
                // 字符串转数组
                foreach(self::$string_to_array_field_list as $fv)
                {
                    if($k == $fv)
                    {
D
Devil 已提交
132
                        $v['value'] = (!isset($v['value']) || $v['value'] == '' || is_array($v['value'])) ? [] : explode(',', $v['value']);
133 134 135 136 137
                    }
                }
            }
        }
        return $data;
D
v1.2.0  
devil_gong 已提交
138 139 140 141 142 143 144 145 146 147
    }

    /**
     * 配置数据保存
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-01-02T23:08:19+0800
     * @param   [array]          $params [输入参数]
     */
148
    public static function ConfigSave($params = [])
D
v1.2.0  
devil_gong 已提交
149 150 151 152 153 154 155
    {
        // 参数校验
        if(empty($params))
        {
            return DataReturn('参数不能为空', -1);
        }

G
gongfuxiang 已提交
156
        // 当前参数中不存在则移除
G
gongfuxiang 已提交
157
        $data_fields = self::$attachment_field_list;
G
gongfuxiang 已提交
158 159 160 161 162 163 164 165 166
        foreach($data_fields as $key=>$field)
        {
            if(!isset($params[$field]))
            {
                unset($data_fields[$key]);
            }
        }

        // 获取附件
D
v1.2.0  
devil_gong 已提交
167 168 169 170 171 172
        $attachment = ResourcesService::AttachmentParams($params, $data_fields);
        foreach($attachment['data'] as $k=>$v)
        {
            $params[$k] = $v;
        }

173 174 175 176 177 178
        // 处理百度地图 ak, 空则默认变量
        if(array_key_exists('common_baidu_map_ak', $params))
        {
            $map_ak_old = MyC('common_baidu_map_ak', '{{common_baidu_map_ak}}', true);
        }

D
v1.2.0  
devil_gong 已提交
179 180 181 182 183 184
        // 循环保存数据
        $success = 0;

        // 开始更新数据
        foreach($params as $k=>$v)
        {
D
devil_gong 已提交
185
            if(in_array($k, self::$rich_text_list))
D
v1.2.0  
devil_gong 已提交
186
            {
D
devil_gong 已提交
187 188
                $v = ResourcesService::ContentStaticReplace($v, 'add');
            } else {
D
v1.2.0  
devil_gong 已提交
189 190 191 192 193
                $v = htmlentities($v);
            }
            if(Db::name('Config')->where(['only_tag'=>$k])->update(['value'=>$v, 'upd_time'=>time()]))
            {
                $success++;
D
devil_gong 已提交
194 195

                // 单条配置缓存删除
D
Devil 已提交
196
                MyCache($k, null);
D
Devil 已提交
197
                MyCache($k.'_row_data', null);
D
v1.2.0  
devil_gong 已提交
198 199 200 201
            }
        }
        if($success > 0)
        {
D
devil 已提交
202
            // 删除所有配置的缓存数据
D
Devil 已提交
203
            MyCache(MyConfig('shopxo.cache_common_my_config_key'), null);
D
devil 已提交
204 205

            // 所有配置信息更新
206
            self::ConfigInit(1);
D
v1.2.0  
devil_gong 已提交
207

G
gongfuxiang 已提交
208
            // 是否需要更新路由规则
209
            $ret = self::RouteSeparatorHandle($params);
G
gongfuxiang 已提交
210 211 212 213 214
            if($ret['code'] != 0)
            {
                return $ret;
            }

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
            // 处理百度地图 ak
            if(array_key_exists('common_baidu_map_ak', $params) && isset($map_ak_old))
            {
                $file_all = [
                    ROOT.'public/static/common/lib/ueditor/dialogs/map/map.html',
                    ROOT.'public/static/common/lib/ueditor/dialogs/map/show.html',
                ];
                foreach($file_all as $f)
                {
                    // 是否有权限
                    if(!is_writable($f))
                    {
                        return DataReturn('编辑器文件没有权限['.$f.']', -1);
                    }

                    // 替换
                    $search = ['ak={{common_baidu_map_ak}}', 'ak='.$map_ak_old];
G
gongfuxiang 已提交
232
                    $replace = 'ak='.(empty($params['common_baidu_map_ak']) ? '{{common_baidu_map_ak}}' : $params['common_baidu_map_ak']);
233 234 235 236 237 238 239 240
                    $status = file_put_contents($f, str_replace($search, $replace, file_get_contents($f)));
                    if($status === false)
                    {
                        return DataReturn('百度地图密钥配置失败', -5);
                    }
                }
            }

D
v1.2.0  
devil_gong 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253
            return DataReturn('编辑成功'.'['.$success.']');
        }
        return DataReturn('编辑失败', -100);
    }

    /**
     * 系统配置信息初始化
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-01-03T21:36:55+0800
     * @param    [int] $status [是否更新数据,0否,1是]
     */
254
    public static function ConfigInit($status = 0)
D
v1.2.0  
devil_gong 已提交
255
    {
D
Devil 已提交
256 257
        $key = MyConfig('shopxo.cache_common_my_config_key');
        $data = MyCache($key);
D
Devil 已提交
258
        if($data === null || $status == 1)
D
v1.2.0  
devil_gong 已提交
259 260 261
        {
            // 所有配置
            $data = Db::name('Config')->column('value', 'only_tag');
D
Devil 已提交
262
            if(!empty($data))
D
v1.2.0  
devil_gong 已提交
263
            {
D
Devil 已提交
264 265 266
                // 数据处理
                // 字符串转数组
                foreach(self::$string_to_array_field_list as $fv)
D
devil_gong 已提交
267
                {
D
Devil 已提交
268 269 270 271
                    if(isset($data[$fv]))
                    {
                        $data[$fv] = ($data[$fv] == '') ? [] : explode(',', $data[$fv]);
                    }
D
devil_gong 已提交
272
                }
D
devil_gong 已提交
273

D
Devil 已提交
274 275
                // 数据处理
                foreach($data as $k=>&$v)
D
devil_gong 已提交
276
                {
D
Devil 已提交
277 278 279 280 281 282
                    // 不参与缓存的配置
                    if(in_array($k, self::$not_cache_field_list))
                    {
                        continue;
                    }

D
Devil 已提交
283 284 285 286 287
                    // 富文本字段处理
                    if(in_array($k, self::$rich_text_list))
                    {
                        $v = ResourcesService::ContentStaticReplace($v, 'get');
                    }
D
devil 已提交
288

D
Devil 已提交
289 290
                    // 公共内置数据缓存
                    MyCache($k, $v);
D
devil 已提交
291

D
Devil 已提交
292 293 294 295 296
                    // 数据文件缓存
                    if(in_array($k, self::$file_cache_keys))
                    {
                        MyFileConfig($k, $v);
                    }
D
Devil 已提交
297
                }
D
Devil 已提交
298 299
            } else {
                $data = [];
D
devil_gong 已提交
300 301
            }

D
devil 已提交
302
            // 所有配置缓存集合
D
Devil 已提交
303
            MyCache($key, $data);
D
v1.2.0  
devil_gong 已提交
304 305
        }
    }
G
gongfuxiang 已提交
306 307 308 309 310 311 312 313 314

    /**
     * 路由规则处理
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-01-02T23:08:19+0800
     * @param   [array]          $params [输入参数]
     */
315
    public static function RouteSeparatorHandle($params = [])
G
gongfuxiang 已提交
316 317 318
    {
        if(isset($params['home_seo_url_model']))
        {
D
Devil 已提交
319 320
            $route_file = APP_PATH.'route'.DS.'route.config';
            $route_arr = ['admin', 'index'];
G
gongfuxiang 已提交
321

D
Devil 已提交
322 323
            // 后端+前端都生成对应的路由定义规则、为了后台进入前端url保持一致
            foreach($route_arr as $module)
G
gongfuxiang 已提交
324
            {
D
Devil 已提交
325 326
                // 生成路由文件
                $route_file_php = APP_PATH.$module.DS.'route'.DS.'route.php';
G
gongfuxiang 已提交
327

D
Devil 已提交
328 329
                // 文件目录
                if(!is_writable(APP_PATH.$module.DS.'route'))
G
gongfuxiang 已提交
330
                {
D
Devil 已提交
331
                    return DataReturn('路由目录没有操作权限'.'[./app/'.$module.'/route]', -11);
G
gongfuxiang 已提交
332 333
                }

D
Devil 已提交
334 335
                // 路配置文件权限
                if(file_exists($route_file_php) && !is_writable($route_file_php))
G
gongfuxiang 已提交
336
                {
D
Devil 已提交
337
                    return DataReturn('路由配置文件没有操作权限'.'[./app/'.$module.'/route/route.php]', -11);
G
gongfuxiang 已提交
338 339
                }

D
Devil 已提交
340 341
                // pathinfo+短地址模式
                if($params['home_seo_url_model'] == 2)
G
gongfuxiang 已提交
342
                {
D
Devil 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
                    
                    if(!file_exists($route_file))
                    {
                        return DataReturn('路由规则文件不存在'.'[./app/route/route.config]', -14);
                    }

                    // 开始生成规则文件
                    if(file_put_contents($route_file_php, file_get_contents($route_file)) === false)
                    {
                        return DataReturn('路由规则文件生成失败', -10);
                    }

                // 兼容模式+pathinfo模式
                } else {
                    if(file_exists($route_file_php) && @unlink($route_file_php) === false)
                    {
                        return DataReturn('路由规则处理失败', -10);
                    }
G
gongfuxiang 已提交
361 362 363 364 365 366
                }
            }
            return DataReturn('处理成功', 0);
        }
        return DataReturn('无需处理', 0);
    }
D
devil_gong 已提交
367 368 369 370 371 372 373 374

    /**
     * 根据唯一标记获取条配置内容
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-05-16
     * @desc    description
D
devil 已提交
375
     * @param   [string]           $key [唯一标记]
D
devil_gong 已提交
376
     */
D
devil 已提交
377
    public static function ConfigContentRow($key)
D
devil_gong 已提交
378
    {
D
Devil 已提交
379 380
        $cache_key = $key.'_row_data';
        $data = MyCache($cache_key);
D
Devil 已提交
381
        if($data === null)
D
devil_gong 已提交
382
        {
D
devil 已提交
383
            $data = Db::name('Config')->where(['only_tag'=>$key])->field('name,value,type,upd_time')->find();
D
devil_gong 已提交
384
            if(!empty($data))
D
devil_gong 已提交
385
            {
D
devil_gong 已提交
386
                // 富文本处理
D
devil 已提交
387
                if(in_array($key, self::$rich_text_list))
D
devil_gong 已提交
388 389 390 391
                {
                    $data['value'] = ResourcesService::ContentStaticReplace($data['value'], 'get');
                }
                $data['upd_time_time'] = empty($data['upd_time']) ? null : date('Y-m-d H:i:s', $data['upd_time']);
D
Devil 已提交
392 393
            } else {
                $data = [];
D
devil_gong 已提交
394
            }
D
Devil 已提交
395
            MyCache($cache_key, $data);
D
devil_gong 已提交
396
        }
D
devil_gong 已提交
397
        
D
devil_gong 已提交
398 399
        return DataReturn('操作成功', 0, $data);
    }
D
devil_gong 已提交
400 401 402 403 404 405 406 407

    /**
     * 站点自提模式 - 自提地址列表
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-11-13
     * @desc    description
D
Devil 已提交
408 409
     * @param   [string]          $value  [自提的配置数据]
     * @param   [array]           $params [输入参数]
D
devil_gong 已提交
410
     */
D
Devil 已提交
411
    public static function SiteTypeExtractionAddressList($value = null, $params = [])
D
devil_gong 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
    {
        // 未指定内容则从缓存读取
        if(empty($value))
        {
            $value = MyC('common_self_extraction_address');
        }

        // 数据处理
        $data = [];
        if(!empty($value) && is_string($value))
        {
            $temp_data = json_decode($value, true);
            if(!empty($temp_data) && is_array($temp_data))
            {
                $data = $temp_data;
            }
        }
D
Devil 已提交
429 430 431 432
        if(!empty($data))
        {
            foreach($data as &$v)
            {
D
Devil 已提交
433 434 435 436
                if(array_key_exists('logo', $v))
                {
                    $v['logo'] = ResourcesService::AttachmentPathViewHandle($v['logo']);
                }
D
Devil 已提交
437 438
            }
        }
D
devil_gong 已提交
439

D
devil 已提交
440 441
        // 自提点地址列表数据钩子
        $hook_name = 'plugins_service_site_extraction_address_list';
D
Devil 已提交
442
        MyEventTrigger($hook_name, [
D
devil 已提交
443 444 445 446 447
            'hook_name'     => $hook_name,
            'is_backend'    => true,
            'data'          => &$data,
        ]);

D
Devil 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
        // 数据距离处理
        if(!empty($data) && is_array($data) && !empty($params) && !empty($params['lng']) && !empty($params['lat']))
        {
            $unit = 'km';
            foreach($data as &$v)
            {
                if(!empty($v) && is_array($v))
                {
                    // 计算距离
                    $v['distance_value'] = \base\GeoTransUtil::GetDistance($v['lng'], $v['lat'], $params['lng'], $params['lat'], 2);
                    $v['distance_unit'] = $unit;
                }
            }

            // 根据距离排序
            if(count($data) > 1)
            {
                $data = ArrayQuickSort($data, 'distance_value');
            }
        }

D
devil_gong 已提交
469 470
        return DataReturn('操作成功', 0, $data);
    }
D
devil_gong 已提交
471 472

    /**
D
devil_gong 已提交
473
     * 站点虚拟模式 - 虚拟销售信息
D
devil_gong 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-11-19
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function SiteFictitiousConfig($params = [])
    {
        // 标题
        $title = MyC('common_site_fictitious_return_title', '密钥信息', true);

        // 提示信息
D
devil_gong 已提交
487
        $tips =  MyC('common_site_fictitious_return_tips', null, true);
D
devil_gong 已提交
488 489 490 491 492 493 494

        $result = [
            'title'     => $title,
            'tips'      => str_replace("\n", '<br />', $tips),
        ];
        return DataReturn('操作成功', 0, $result);
    }
D
v1.2.0  
devil_gong 已提交
495 496
}
?>