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

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

/**
 * 配置服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class ConfigService
{
D
devil_gong 已提交
26 27 28 29 30 31 32 33 34
    // 富文本,不实例化的字段
    public static $rich_text_list = [
            'home_footer_info',
            'common_email_currency_template',
            'home_email_user_reg',
            'home_email_user_forget_pwd',
            'home_email_user_email_binding',
            'home_site_close_reason',
            'common_agreement_userregister',
D
devil_gong 已提交
35
            'common_self_extraction_address',
D
devil_gong 已提交
36 37
        ];

G
gongfuxiang 已提交
38 39 40 41 42 43 44
    // 附件字段列表
    public static $attachment_field_list = [
        'home_site_logo',
        'home_site_logo_wap',
        'home_site_desktop_icon',
        'common_customer_store_qrcode',
        'home_site_user_register_bg_images',
D
devil_gong 已提交
45 46 47 48 49 50 51 52 53 54 55 56
        '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 = [
        'home_user_reg_state',
        'common_images_verify_rules',
G
gongfuxiang 已提交
57 58
    ];

D
v1.2.0  
devil_gong 已提交
59 60 61 62 63 64 65 66 67
    /**
     * 配置列表,唯一标记作为key
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-07
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
68
    public static function ConfigList($params = [])
D
v1.2.0  
devil_gong 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81
    {
        $field = isset($params['field']) ? $params['field'] : 'only_tag,name,describe,value,error_tips';
        return Db::name('Config')->column($field);
    }

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

G
gongfuxiang 已提交
90
        // 当前参数中不存在则移除
G
gongfuxiang 已提交
91
        $data_fields = self::$attachment_field_list;
G
gongfuxiang 已提交
92 93 94 95 96 97 98 99 100
        foreach($data_fields as $key=>$field)
        {
            if(!isset($params[$field]))
            {
                unset($data_fields[$key]);
            }
        }

        // 获取附件
D
v1.2.0  
devil_gong 已提交
101 102 103 104 105 106 107 108 109 110 111 112
        $attachment = ResourcesService::AttachmentParams($params, $data_fields);
        foreach($attachment['data'] as $k=>$v)
        {
            $params[$k] = $v;
        }

        // 循环保存数据
        $success = 0;

        // 开始更新数据
        foreach($params as $k=>$v)
        {
D
devil_gong 已提交
113
            if(in_array($k, self::$rich_text_list))
D
v1.2.0  
devil_gong 已提交
114
            {
D
devil_gong 已提交
115 116
                $v = ResourcesService::ContentStaticReplace($v, 'add');
            } else {
D
v1.2.0  
devil_gong 已提交
117 118 119 120 121
                $v = htmlentities($v);
            }
            if(Db::name('Config')->where(['only_tag'=>$k])->update(['value'=>$v, 'upd_time'=>time()]))
            {
                $success++;
D
devil_gong 已提交
122 123 124

                // 单条配置缓存删除
                cache(config('shopxo.cache_config_row_key').$k, null);
D
v1.2.0  
devil_gong 已提交
125 126 127 128 129
            }
        }
        if($success > 0)
        {
            // 配置信息更新
130
            self::ConfigInit(1);
D
v1.2.0  
devil_gong 已提交
131

G
gongfuxiang 已提交
132
            // 是否需要更新路由规则
133
            $ret = self::RouteSeparatorHandle($params);
G
gongfuxiang 已提交
134 135 136 137 138
            if($ret['code'] != 0)
            {
                return $ret;
            }

D
v1.2.0  
devil_gong 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151
            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是]
     */
152
    public static function ConfigInit($status = 0)
D
v1.2.0  
devil_gong 已提交
153
    {
D
devil_gong 已提交
154
        $key = config('shopxo.cache_common_my_config_key');
D
v1.2.0  
devil_gong 已提交
155 156 157 158 159 160 161 162
        $data = cache($key);
        if($status == 1 || empty($data))
        {
            // 所有配置
            $data = Db::name('Config')->column('value', 'only_tag');

            // 数据处理
            // 开启用户注册列表
D
devil_gong 已提交
163
            foreach(self::$string_to_array_field_list as $field)
D
v1.2.0  
devil_gong 已提交
164
            {
D
devil_gong 已提交
165 166 167 168
                if(isset($data[$field]))
                {
                    $data[$field] = empty($data[$field]) ? [] : explode(',', $data[$field]);
                }
D
v1.2.0  
devil_gong 已提交
169
            }
D
devil_gong 已提交
170 171 172 173 174 175 176 177 178 179

            // 富文本字段处理
            foreach($data as $k=>$v)
            {
                if(in_array($k, self::$rich_text_list))
                {
                    $data[$k] = ResourcesService::ContentStaticReplace($v, 'get');
                }
            }

D
v1.2.0  
devil_gong 已提交
180 181 182
            cache($key, $data);
        }
    }
G
gongfuxiang 已提交
183 184 185 186 187 188 189 190 191

    /**
     * 路由规则处理
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-01-02T23:08:19+0800
     * @param   [array]          $params [输入参数]
     */
192
    public static function RouteSeparatorHandle($params = [])
G
gongfuxiang 已提交
193 194 195
    {
        if(isset($params['home_seo_url_model']))
        {
G
gongfuxiang 已提交
196
            $route_file = ROOT.'route'.DS.'route.config';
G
gongfuxiang 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
            $route_file_php = ROOT.'route'.DS.'route.php';

            // 文件目录
            if(!is_writable(ROOT.'route'))
            {
                return DataReturn('路由目录没有操作权限'.'[./route]', -11);
            }

            // 路配置文件权限
            if(file_exists($route_file_php) && !is_writable($route_file_php))
            {
                return DataReturn('路由配置文件没有操作权限'.'[./route/route.php]', -11);
            }

            // pathinfo+短地址模式
            if($params['home_seo_url_model'] == 2)
            {
                
                if(!file_exists($route_file))
                {
G
gongfuxiang 已提交
217
                    return DataReturn('路由规则文件不存在'.'[./route/route.config]', -14);
G
gongfuxiang 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
                }

                // 开始生成规则文件
                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);
                }
            }
            return DataReturn('处理成功', 0);
        }
        return DataReturn('无需处理', 0);
    }
D
devil_gong 已提交
237 238 239 240 241 242 243 244 245 246 247 248

    /**
     * 根据唯一标记获取条配置内容
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-05-16
     * @desc    description
     * @param   [string]           $only_tag [唯一标记]
     */
    public static function ConfigContentRow($only_tag)
    {
D
devil_gong 已提交
249 250 251 252
        // 缓存key
        $key = config('shopxo.cache_config_row_key').$only_tag;
        $data = cache($key);

D
devil_gong 已提交
253
        // 获取内容
D
devil_gong 已提交
254
        if(empty($data))
D
devil_gong 已提交
255
        {
D
devil_gong 已提交
256 257
            $data = Db::name('Config')->where(['only_tag'=>$only_tag])->field('name,value,type,upd_time')->find();
            if(!empty($data))
D
devil_gong 已提交
258
            {
D
devil_gong 已提交
259 260 261 262 263 264
                // 富文本处理
                if(in_array($only_tag, self::$rich_text_list))
                {
                    $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_gong 已提交
265
            }
D
devil_gong 已提交
266
            cache($key, $data);
D
devil_gong 已提交
267
        }
D
devil_gong 已提交
268
        
D
devil_gong 已提交
269 270
        return DataReturn('操作成功', 0, $data);
    }
D
devil_gong 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

    /**
     * 站点自提模式 - 自提地址列表
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-11-13
     * @desc    description
     * @param   [string]          $value [自提的配置数据]
     */
    public static function SiteTypeExtractionAddressList($value = null)
    {
        // 未指定内容则从缓存读取
        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_gong 已提交
299 300 301 302 303 304 305 306 307

        // 自提点地址列表数据钩子
        $hook_name = 'plugins_service_site_extraction_address_list';
        $ret = Hook::listen($hook_name, [
            'hook_name'     => $hook_name,
            'is_backend'    => true,
            'data'          => &$data,
        ]);

D
devil_gong 已提交
308 309 310 311 312 313 314 315 316 317 318
        // 坐标处理
        if(!empty($data) && is_array($data) && in_array(APPLICATION_CLIENT_TYPE, ['weixin', 'alipay']))
        {
            foreach($data as &$v)
            {
                // 坐标转换 百度转火星(高德,谷歌,腾讯坐标)
                if(isset($v['lng']) && isset($v['lat']) && $v['lng'] > 0 && $v['lat'] > 0)
                {
                    $map = \base\GeoTransUtil::BdToGcj($v['lng'], $v['lat']);
                    if(isset($map['lng']) && isset($map['lat']))
                    {
D
devil_gong 已提交
319 320
                        $v['lng'] = $map['lng'];
                        $v['lat'] = $map['lat'];
D
devil_gong 已提交
321 322 323 324 325
                    }
                }
            }
        }

D
devil_gong 已提交
326 327
        return DataReturn('操作成功', 0, $data);
    }
D
devil_gong 已提交
328 329

    /**
D
devil_gong 已提交
330
     * 站点虚拟模式 - 虚拟销售信息
D
devil_gong 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343
     * @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 已提交
344
        $tips =  MyC('common_site_fictitious_return_tips', null, true);
D
devil_gong 已提交
345 346 347 348 349 350 351

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