alias('a')->join(['__ARTICLE_CATEGORY__'=>'ac'], 'a.article_category_id=ac.id')->field($field)->where($where)->order('a.id desc')->limit($m, $n)->select(); if(!empty($data)) { $common_is_enable_tips = lang('common_is_enable_tips'); foreach($data as &$v) { // url $v['url'] = MyUrl('index/article/index', ['id'=>$v['id']]); // 分类名称 if(isset($v['article_category_id'])) { $v['article_category_name'] = Db::name('ArticleCategory')->where(['id'=>$v['article_category_id']])->value('name'); } // 是否启用 if(isset($v['is_enable'])) { $v['is_enable_text'] = $common_is_enable_tips[$v['is_enable']]['name']; } // 内容 if(isset($v['content'])) { $v['content'] = ResourcesService::ContentStaticReplace($v['content'], 'get'); } 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 [条件] */ public static function ArticleTotal($where) { return (int) Db::name('Article')->alias('a')->join(['__ARTICLE_CATEGORY__'=>'ac'], 'a.article_category_id=ac.id')->where($where)->count(); } /** * 列表条件 * @author Devil * @blog http://gong.gg/ * @version 1.0.0 * @date 2018-09-29 * @desc description * @param [array] $params [输入参数] */ public static function ArticleListWhere($params = []) { $where = []; if(!empty($params['keywords'])) { $where[] = ['a.title', 'like', '%'.$params['keywords'].'%']; } // 是否更多条件 if(isset($params['is_more']) && $params['is_more'] == 1) { // 等值 if(isset($params['is_enable']) && $params['is_enable'] > -1) { $where[] = ['a.is_enable', '=', intval($params['is_enable'])]; } if(isset($params['article_category_id']) && $params['article_category_id'] > -1) { $where[] = ['a.article_category_id', '=', intval($params['article_category_id'])]; } if(isset($params['is_home_recommended']) && $params['is_home_recommended'] > -1) { $where[] = ['a.is_home_recommended', '=', intval($params['is_home_recommended'])]; } if(!empty($params['time_start'])) { $where[] = ['a.add_time', '>', strtotime($params['time_start'])]; } if(!empty($params['time_end'])) { $where[] = ['a.add_time', '<', strtotime($params['time_end'])]; } } return $where; } /** * 文章保存 * @author Devil * @blog http://gong.gg/ * @version 1.0.0 * @date 2018-12-18 * @desc description * @param [array] $params [输入参数] */ public static function ArticleSave($params = []) { // 请求类型 $p = [ [ 'checked_type' => 'length', 'key_name' => 'title', 'checked_data' => '2,60', 'error_msg' => '标题长度 2~60 个字符', ], [ 'checked_type' => 'empty', 'key_name' => 'article_category_id', 'error_msg' => '请选择文章分类', ], [ 'checked_type' => 'fun', 'key_name' => 'jump_url', 'checked_data' => 'CheckUrl', 'is_checked' => 1, 'error_msg' => '跳转url地址格式有误', ], [ 'checked_type' => 'length', 'key_name' => 'content', 'checked_data' => '10,105000', 'error_msg' => '内容 10~105000 个字符', ], ]; $ret = ParamsChecked($params, $p); if($ret !== true) { return DataReturn($ret, -1); } // 编辑器内容 $content = isset($params['content']) ? htmlspecialchars_decode($params['content']) : ''; // 数据 $image = self::MatchContentImage($content); $data = [ 'title' => $params['title'], 'title_color' => empty($params['title_color']) ? '' : $params['title_color'], 'article_category_id' => intval($params['article_category_id']), 'jump_url' => empty($params['jump_url']) ? '' : $params['jump_url'], 'content' => ResourcesService::ContentStaticReplace($content, 'add'), 'image' => empty($image) ? '' : json_encode($image), 'image_count' => count($image), 'is_enable' => isset($params['is_enable']) ? intval($params['is_enable']) : 0, 'is_home_recommended' => isset($params['is_home_recommended']) ? intval($params['is_home_recommended']) : 0, ]; if(empty($params['id'])) { $data['add_time'] = time(); if(Db::name('Article')->insertGetId($data) > 0) { return DataReturn('添加成功', 0); } return DataReturn('添加失败', -100); } else { $data['upd_time'] = time(); if(Db::name('Article')->where(['id'=>intval($params['id'])])->update($data)) { 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)) { $pattern = '//'; preg_match_all($pattern, $content, $match); return empty($match[1]) ? [] : $match[1]; } return array(); } /** * 获取分类和所有文章 * @author Devil * @blog http://gong.gg/ * @version 1.0.0 * @date 2018-10-19 * @desc description * @param [array] $params [输入参数] */ public static function ArticleCategoryListContent($params = []) { $data = Db::name('ArticleCategory')->field('id,name')->where(['is_enable'=>1])->order('id asc, sort asc')->select(); if(!empty($data)) { foreach($data as &$v) { $items = Db::name('Article')->field('id,title,title_color')->where(['article_category_id'=>$v['id'], 'is_enable'=>1])->select(); if(!empty($items)) { foreach($items as &$vs) { // url $vs['url'] = MyUrl('index/article/index', ['id'=>$vs['id']]); } } $v['items'] = $items; } } return DataReturn('处理成功', 0, $data); } /** * 文章访问统计加1 * @author Devil * @blog http://gong.gg/ * @version 1.0.0 * @date 2018-10-15 * @desc description * @param [array] $params [输入参数] */ public static function ArticleAccessCountInc($params = []) { if(!empty($params['id'])) { return Db::name('Article')->where(array('id'=>intval($params['id'])))->setInc('access_count'); } return false; } /** * 文章分类 * @author Devil * @blog http://gong.gg/ * @version 1.0.0 * @date 2018-08-29 * @desc description * @param [array] $params [输入参数] */ public static function ArticleCategoryList($params = []) { $field = empty($params['field']) ? '*' : $params['field']; $order_by = empty($params['order_by']) ? 'sort asc' : trim($params['order_by']); $data = Db::name('ArticleCategory')->where(['is_enable'=>1])->field($field)->order($order_by)->select(); return DataReturn('处理成功', 0, $data); } /** * 删除 * @author Devil * @blog http://gong.gg/ * @version 1.0.0 * @date 2018-12-18 * @desc description * @param [array] $params [输入参数] */ public static function ArticleDelete($params = []) { // 请求参数 $p = [ [ 'checked_type' => 'empty', 'key_name' => 'id', 'error_msg' => '操作id有误', ], ]; $ret = ParamsChecked($params, $p); if($ret !== true) { return DataReturn($ret, -1); } // 删除操作 if(Db::name('Article')->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 [输入参数] */ public static function ArticleStatusUpdate($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' => '状态有误', ], ]; $ret = ParamsChecked($params, $p); if($ret !== true) { return DataReturn($ret, -1); } // 数据更新 if(Db::name('Article')->where(['id'=>intval($params['id'])])->update([$params['field']=>intval($params['state'])])) { return DataReturn('编辑成功'); } return DataReturn('编辑失败或数据未改变', -100); } /** * 获取文章分类节点数据 * @author Devil * @blog http://gong.gg/ * @version 1.0.0 * @datetime 2018-12-16T23:54:46+0800 * @param [array] $params [输入参数] */ public static function ArticleCategoryNodeSon($params = []) { // id $id = isset($params['id']) ? intval($params['id']) : 0; // 获取数据 $field = '*'; $data = Db::name('ArticleCategory')->field($field)->where(['pid'=>$id])->order('sort asc')->select(); if(!empty($data)) { foreach($data as &$v) { $v['is_son'] = (Db::name('ArticleCategory')->where(['pid'=>$v['id']])->count() > 0) ? 'ok' : 'no'; $v['ajax_url'] = MyUrl('admin/articlecategory/getnodeson', array('id'=>$v['id'])); $v['delete_url'] = MyUrl('admin/articlecategory/delete'); $v['json'] = json_encode($v); } return DataReturn('操作成功', 0, $data); } return DataReturn('没有相关数据', -100); } /** * 文章分类保存 * @author Devil * @blog http://gong.gg/ * @version 1.0.0 * @datetime 2018-12-17T01:04:03+0800 * @param [array] $params [输入参数] */ public static function ArticleCategorySave($params = []) { // 请求参数 $p = [ [ 'checked_type' => 'length', 'key_name' => 'name', 'checked_data' => '2,16', 'error_msg' => '名称格式 2~16 个字符', ], ]; $ret = ParamsChecked($params, $p); if($ret !== true) { return DataReturn($ret, -1); } // 数据 $data = [ 'name' => $params['name'], 'pid' => isset($params['pid']) ? intval($params['pid']) : 0, 'sort' => isset($params['sort']) ? intval($params['sort']) : 0, 'is_enable' => isset($params['is_enable']) ? intval($params['is_enable']) : 0, ]; // 添加 if(empty($params['id'])) { $data['add_time'] = time(); if(Db::name('ArticleCategory')->insertGetId($data) > 0) { return DataReturn('添加成功', 0); } return DataReturn('添加失败', -100); } else { $data['upd_time'] = time(); if(Db::name('ArticleCategory')->where(['id'=>intval($params['id'])])->update($data)) { return DataReturn('编辑成功', 0); } return DataReturn('编辑失败', -100); } } /** * 文章分类删除 * @author Devil * @blog http://gong.gg/ * @version 1.0.0 * @datetime 2018-12-17T02:40:29+0800 * @param [array] $params [输入参数] */ public static function ArticleCategoryDelete($params = []) { // 请求参数 $p = [ [ 'checked_type' => 'empty', 'key_name' => 'id', 'error_msg' => '删除数据id有误', ], [ 'checked_type' => 'empty', 'key_name' => 'admin', 'error_msg' => '用户信息有误', ], ]; $ret = ParamsChecked($params, $p); if($ret !== true) { return DataReturn($ret, -1); } // 开始删除 if(Db::name('ArticleCategory')->where(['id'=>intval($params['id'])])->delete()) { return DataReturn('删除成功', 0); } return DataReturn('删除失败', -100); } } ?>