CustomViewService.php 9.6 KB
Newer Older
G
gongfuxiang 已提交
1 2 3
<?php
namespace app\service;

G
gongfuxiang 已提交
4
use think\Db;
G
gongfuxiang 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
use app\service\ResourcesService;

/**
 * 自定义页面服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class CustomViewService
{
    /**
     * 获取自定义列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function CustomViewList($params = [])
    {
        $where = empty($params['where']) ? [] : $params['where'];
D
admin  
devil_gong 已提交
28
        $field = empty($params['field']) ? 'id,title,content,is_header,is_footer,is_full_screen,access_count,is_enable' : $params['field'];
G
gongfuxiang 已提交
29 30 31
        $m = isset($params['m']) ? intval($params['m']) : 0;
        $n = isset($params['n']) ? intval($params['n']) : 10;

G
gongfuxiang 已提交
32
        $data = Db::name('CustomView')->field($field)->where($where)->order('id desc')->limit($m, $n)->select();
G
gongfuxiang 已提交
33 34
        if(!empty($data))
        {
D
admin  
devil_gong 已提交
35
            $common_is_enable_list = lang('common_is_enable_list');
G
gongfuxiang 已提交
36 37
            foreach($data as &$v)
            {
D
admin  
devil_gong 已提交
38 39 40 41 42 43 44
                // 是否启用
                if(isset($v['is_enable']))
                {
                    $v['is_enable_text'] = $common_is_enable_list[$v['is_enable']]['name'];
                }

                // 内容
G
gongfuxiang 已提交
45 46 47 48
                if(isset($v['content']))
                {
                    $v['content'] = ResourcesService::ContentStaticReplace($v['content'], 'get');
                }
D
admin  
devil_gong 已提交
49 50

                // 时间
G
gongfuxiang 已提交
51 52 53 54 55
                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']);
                }
D
admin  
devil_gong 已提交
56 57 58 59 60
                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']);
                }
G
gongfuxiang 已提交
61 62
            }
        }
D
admin  
devil_gong 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76
        return DataReturn('处理成功', 0, $data);
    }

    /**
     * 总数
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $where [条件]
     */
    public static function CustomViewTotal($where = [])
    {
G
gongfuxiang 已提交
77
        return (int) Db::name('CustomView')->where($where)->count();
D
admin  
devil_gong 已提交
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    }

    /**
     * 列表条件
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function CustomViewListWhere($params = [])
    {
        $where = [];

        // id
        if(!empty($params['id']))
        {
            $where[] = ['id', '=', $params['id']];
        }

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

        // 是否更多条件
        if(isset($params['is_more']) && $params['is_more'] == 1)
        {
            // 等值
            if(isset($params['is_enable']) && $params['is_enable'] > -1)
            {
                $where[] = ['is_enable', '=', intval($params['is_enable'])];
            }
            if(isset($params['is_header']) && $params['is_header'] > -1)
            {
                $where[] = ['is_header', '=', intval($params['is_header'])];
            }
            if(isset($params['is_footer']) && $params['is_footer'] > -1)
            {
                $where[] = ['is_footer', '=', intval($params['is_footer'])];
            }

            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;
G
gongfuxiang 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    }

    /**
     * 自定义页面访问统计加1
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-10-15
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function CustomViewAccessCountInc($params = [])
    {
        if(!empty($params['id']))
        {
G
gongfuxiang 已提交
147
            return Db::name('CustomView')->where(array('id'=>intval($params['id'])))->setInc('access_count');
G
gongfuxiang 已提交
148 149 150
        }
        return false;
    }
D
admin  
devil_gong 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

    /**
     * 保存
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-18
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function CustomViewSave($params = [])
    {
        // 请求类型
        $p = [
            [
                'checked_type'      => 'length',
                'key_name'          => 'title',
                'checked_data'      => '2,60',
                'error_msg'         => '标题长度 2~60 个字符',
            ],
            [
                'checked_type'      => 'length',
                'key_name'          => 'content',
                'checked_data'      => '50,105000',
                'error_msg'         => '内容长度最少 50~105000 个字符',
            ],
        ];
D
devil_gong 已提交
178
        $ret = ParamsChecked($params, $p);
D
admin  
devil_gong 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 数据
        $content = ResourcesService::ContentStaticReplace($params['content'], 'add');
        $image = self::MatchContentImage($content);
        $data = [
            'title'         => $params['title'],
            'content'       => $content,
            'image'         => empty($image) ? '' : json_encode($image),
            'image_count'   => count($image),
G
gongfuxiang 已提交
192 193 194 195
            'is_enable'     => isset($params['is_enable']) ? intval($params['is_enable']) : 0,
            'is_header'     => isset($params['is_header']) ? intval($params['is_header']) : 0,
            'is_footer'     => isset($params['is_footer']) ? intval($params['is_footer']) : 0,
            'is_full_screen'=> isset($params['is_full_screen']) ? intval($params['is_full_screen']) : 0,
D
admin  
devil_gong 已提交
196 197 198 199 200
        ];

        if(empty($params['id']))
        {
            $data['add_time'] = time();
G
gongfuxiang 已提交
201
            if(Db::name('CustomView')->insertGetId($data) > 0)
D
admin  
devil_gong 已提交
202 203 204 205 206 207
            {
                return DataReturn('添加成功', 0);
            }
            return DataReturn('添加失败', -100);
        } else {
            $data['upd_time'] = time();
G
gongfuxiang 已提交
208
            if(Db::name('CustomView')->where(['id'=>intval($params['id'])])->update($data))
D
admin  
devil_gong 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
            {
                return DataReturn('编辑成功', 0);
            }
            return DataReturn('编辑失败', -100); 
        }
    }

    /**
     * 正则匹配文章图片
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-01-22T18:06:53+0800
     * @param    [string]         $content [文章内容]
     * @return   [array]                   [文章图片数组(一维)]
     */
    private static function MatchContentImage($content)
    {
        if(!empty($content))
        {
G
gongfuxiang 已提交
229
            $pattern = '/<img.*?src=[\'|\"](\/static\/upload\/customview\/image\/.*?[\.gif|\.jpg|\.jpeg|\.png|\.bmp])[\'|\"].*?[\/]?>/';
D
admin  
devil_gong 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
            preg_match_all($pattern, $content, $match);
            return empty($match[1]) ? [] : $match[1];
        }
        return [];
    }

    /**
     * 删除
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-18
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function CustomViewDelete($params = [])
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '操作id有误',
            ],
        ];
D
devil_gong 已提交
255
        $ret = ParamsChecked($params, $p);
D
admin  
devil_gong 已提交
256 257 258 259 260 261
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 删除操作
G
gongfuxiang 已提交
262
        if(Db::name('CustomView')->where(['id'=>$params['id']])->delete())
D
admin  
devil_gong 已提交
263 264 265 266 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
        {
            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 [输入参数]
     */
    public static function CustomViewStatusUpdate($params = [])
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '操作id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'field',
                'error_msg'         => '字段有误',
            ],
            [
                'checked_type'      => 'in',
                'key_name'          => 'state',
                'checked_data'      => [0,1],
                'error_msg'         => '状态有误',
            ],
        ];
D
devil_gong 已提交
299
        $ret = ParamsChecked($params, $p);
D
admin  
devil_gong 已提交
300 301 302 303 304 305
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 数据更新
G
gongfuxiang 已提交
306
        if(Db::name('CustomView')->where(['id'=>intval($params['id'])])->update([$params['field']=>intval($params['state'])]))
D
admin  
devil_gong 已提交
307 308 309 310 311
        {
           return DataReturn('编辑成功');
        }
        return DataReturn('编辑失败或数据未改变', -100);
    }
G
gongfuxiang 已提交
312 313
}
?>