AppHomeNavService.php 12.4 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;
14
use app\service\ResourcesService;
D
v1.2.0  
devil_gong 已提交
15 16

/**
D
devil_gong 已提交
17
 * APP首页导航服务层
D
v1.2.0  
devil_gong 已提交
18 19 20 21 22
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
D
devil_gong 已提交
23
class AppHomeNavService
D
v1.2.0  
devil_gong 已提交
24 25 26 27 28 29 30 31 32
{
    /**
     * 首页导航列表
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
33
    public static function AppHomeNavList($params = [])
D
v1.2.0  
devil_gong 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    {
        $where = empty($params['where']) ? [] : $params['where'];
        $field = empty($params['field']) ? '*' : $params['field'];
        $order_by = empty($params['order_by']) ? 'sort asc' : trim($params['order_by']);

        $m = isset($params['m']) ? intval($params['m']) : 0;
        $n = isset($params['n']) ? intval($params['n']) : 10;

        // 获取品牌列表
        $data = Db::name('AppHomeNav')->where($where)->order($order_by)->limit($m, $n)->select();
        if(!empty($data))
        {
            $common_platform_type = lang('common_platform_type');
            $common_is_enable_tips = lang('common_is_enable_tips');
            $common_app_event_type = lang('common_app_event_type');
D
devil_gong 已提交
49
            $common_is_text_list = lang('common_is_text_list');
D
v1.2.0  
devil_gong 已提交
50 51
            foreach($data as &$v)
            {
D
devil_gong 已提交
52 53 54 55 56 57
                // 是否需要登录
                if(isset($v['is_need_login']))
                {
                    $v['is_need_login_text'] = $common_is_text_list[$v['is_need_login']]['name'];
                }

D
v1.2.0  
devil_gong 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
                // 是否启用
                if(isset($v['is_enable']))
                {
                    $v['is_enable_text'] = $common_is_enable_tips[$v['is_enable']]['name'];
                }

                // 平台类型
                if(isset($v['platform']))
                {
                    $v['platform_text'] = $common_platform_type[$v['platform']]['name'];
                }

                // 事件类型
                if(isset($v['event_type']) && $v['event_type'] != -1)
                {
                    $v['event_type_text'] = $common_app_event_type[$v['event_type']]['name'];
                }

                // 图片地址
                if(isset($v['images_url']))
                {
                    $v['images_url_old'] = $v['images_url'];
80
                    $v['images_url'] = ResourcesService::AttachmentPathViewHandle($v['images_url']);
D
v1.2.0  
devil_gong 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
                }

                // 时间
                if(isset($v['add_time']))
                {
                    $v['add_time_time'] = date('Y-m-d H:i:s', $v['add_time']);
                    $v['add_time_date'] = date('Y-m-d', $v['add_time']);
                }
                if(isset($v['upd_time']))
                {
                    $v['upd_time_time'] = date('Y-m-d H:i:s', $v['upd_time']);
                    $v['upd_time_date'] = date('Y-m-d', $v['upd_time']);
                }
            }
        }
        return DataReturn('处理成功', 0, $data);
    }

    /**
     * 首页导航总数
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-10T22:16:29+0800
     * @param    [array]          $where [条件]
     */
107
    public static function AppHomeNavTotal($where)
D
v1.2.0  
devil_gong 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120
    {
        return (int) Db::name('AppHomeNav')->where($where)->count();
    }

    /**
     * 首页导航列表条件
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
121
    public static function AppHomeNavListWhere($params = [])
D
v1.2.0  
devil_gong 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135
    {
        $where = [];

        if(!empty($params['keywords']))
        {
            $where[] = ['name', 'like', '%'.$params['keywords'].'%'];
        }

        // 是否更多条件
        if(isset($params['is_more']) && $params['is_more'] == 1)
        {
            // 等值
            if(isset($params['is_enable']) && $params['is_enable'] > -1)
            {
G
gongfuxiang 已提交
136
                
D
v1.2.0  
devil_gong 已提交
137 138
                $where[] = ['is_enable', '=', intval($params['is_enable'])];
            }
G
gongfuxiang 已提交
139 140 141 142
            if(isset($params['is_need_login']) && $params['is_need_login'] > -1)
            {
                $where[] = ['is_need_login', '=', intval($params['is_need_login'])];
            }
D
v1.2.0  
devil_gong 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
            if(isset($params['event_type']) && $params['event_type'] > -1)
            {
                $where[] = ['event_type', '=', intval($params['event_type'])];
            }
            if(!empty($params['platform']))
            {
                $where[]= ['platform', '=', $params['platform']];
            }

            if(!empty($params['time_start']))
            {
                $where[] = ['add_time', '>', strtotime($params['time_start'])];
            }
            if(!empty($params['time_end']))
            {
                $where[] = ['add_time', '<', strtotime($params['time_end'])];
            }
        }

        return $where;
    }

    /**
     * 首页导航数据保存
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-19
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
174
    public static function AppHomeNavSave($params = [])
D
v1.2.0  
devil_gong 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    {
        // 请求类型
        $p = [
            [
                'checked_type'      => 'length',
                'key_name'          => 'name',
                'checked_data'      => '2,60',
                'error_msg'         => '名称长度 2~60 个字符',
            ],
            [
                'checked_type'      => 'in',
                'key_name'          => 'platform',
                'checked_data'      => array_column(lang('common_platform_type'), 'value'),
                'error_msg'         => '平台类型有误',
            ],
            [
                'checked_type'      => 'in',
                'key_name'          => 'event_type',
                'checked_data'      => array_column(lang('common_app_event_type'), 'value'),
                'is_checked'        => 2,
                'error_msg'         => '事件值类型有误',
            ],
            [
                'checked_type'      => 'length',
                'key_name'          => 'event_value',
                'checked_data'      => '255',
                'error_msg'         => '事件值最多 255 个字符',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'images_url',
                'checked_data'      => '255',
                'error_msg'         => '请上传图片',
            ],
            [
                'checked_type'      => 'length',
                'key_name'          => 'sort',
                'checked_data'      => '3',
                'error_msg'         => '顺序 0~255 之间的数值',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 附件
        $data_fields = ['images_url'];
        $attachment = ResourcesService::AttachmentParams($params, $data_fields);

        // 数据
        $data = [
            'name'          => $params['name'],
            'platform'      => $params['platform'],
            'event_type'    => isset($params['event_type']) ? intval($params['event_type']) : -1,
            'event_value'   => $params['event_value'],
            'images_url'    => $attachment['data']['images_url'],
            'bg_color'      => isset($params['bg_color']) ? $params['bg_color'] : '',
            'sort'          => intval($params['sort']),
            'is_enable'     => isset($params['is_enable']) ? intval($params['is_enable']) : 0,
D
devil_gong 已提交
236
            'is_need_login' => isset($params['is_need_login']) ? intval($params['is_need_login']) : 0,
D
v1.2.0  
devil_gong 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        ];

        if(empty($params['id']))
        {
            $data['add_time'] = time();
            if(Db::name('AppHomeNav')->insertGetId($data) > 0)
            {
                return DataReturn('添加成功', 0);
            }
            return DataReturn('添加失败', -100);
        } else {
            $data['upd_time'] = time();
            if(Db::name('AppHomeNav')->where(['id'=>intval($params['id'])])->update($data))
            {
                return DataReturn('编辑成功', 0);
            }
            return DataReturn('编辑失败', -100); 
        }
    }

    /**
     * 首页导航删除
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-18
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
266
    public static function AppHomeNavDelete($params = [])
D
v1.2.0  
devil_gong 已提交
267 268 269 270 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
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '操作id有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 删除操作
        if(Db::name('AppHomeNav')->where(['id'=>$params['id']])->delete())
        {
            return DataReturn('删除成功');
        }

        return DataReturn('删除失败或资源不存在', -100);
    }

    /**
     * 首页导航状态更新
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
299
    public static function AppHomeNavStatusUpdate($params = [])
D
v1.2.0  
devil_gong 已提交
300 301 302 303 304 305 306 307
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '操作id有误',
            ],
D
devil_gong 已提交
308 309 310 311 312
            [
                'checked_type'      => 'empty',
                'key_name'          => 'field',
                'error_msg'         => '操作字段有误',
            ],
D
v1.2.0  
devil_gong 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326
            [
                'checked_type'      => 'in',
                'key_name'          => 'state',
                'checked_data'      => [0,1],
                'error_msg'         => '状态有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 数据更新
D
devil_gong 已提交
327
        if(Db::name('AppHomeNav')->where(['id'=>intval($params['id'])])->update([$params['field']=>intval($params['state'])]))
D
v1.2.0  
devil_gong 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
        {
           return DataReturn('编辑成功');
        }
        return DataReturn('编辑失败或数据未改变', -100);
    }

    /**
     * APP获取首页导航
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-19
     * @desc    description
     * @param   array           $params [description]
     */
343
    public static function AppHomeNav($params = [])
D
v1.2.0  
devil_gong 已提交
344
    {
D
devil 已提交
345 346 347 348 349
        // 平台
        $platform = APPLICATION_CLIENT_TYPE;

        // web端手机访问
        if($platform == 'pc' && IsMobile())
D
v1.2.0  
devil_gong 已提交
350
        {
D
devil 已提交
351 352 353 354 355 356 357 358 359 360 361 362
            $platform = 'h5';
        }

        // 缓存
        $key = config('shopxo.cache_navigation_key').$platform;
        $data = cache($key);

        if(empty($data))
        {
            // 获取导航数据
            $data = Db::name('AppHomeNav')->field('id,name,images_url,event_value,event_type,bg_color,is_need_login')->where(['platform'=>$platform, 'is_enable'=>1])->order('sort asc')->select();
            if(!empty($data))
D
v1.2.0  
devil_gong 已提交
363
            {
D
devil 已提交
364 365 366 367 368 369
                foreach($data as &$v)
                {
                    $v['images_url_old'] = $v['images_url'];
                    $v['images_url'] = ResourcesService::AttachmentPathViewHandle($v['images_url']);
                    $v['event_value'] = empty($v['event_value']) ? null : $v['event_value'];
                }
D
v1.2.0  
devil_gong 已提交
370
            }
D
devil 已提交
371 372 373

            // 存储缓存
            cache($key, $data, 3600*24);
D
v1.2.0  
devil_gong 已提交
374 375 376 377 378
        }
        return $data;
    }
}
?>