コード例 #1
0
ファイル: SqDataModel.class.php プロジェクト: anLl/ybirds
 /**
  * 获取指定社区栏目的资讯列表数据.
  * 
  * @access public
  * @param integer $catId 栏目 ID.
  * @param array $where where 条件.
  * @param integer $page 请求的页码.
  * @param integer $pageSize 每页显示的条数.
  * @return array lists: 包含 id, title, create_time, picurl, content; totalRows: 总条数.
  */
 public function fetchListByCategory($catId, array $where = [], $page = 1, $pageSize = 10)
 {
     $joins = ["LEFT JOIN __SQ_FDATA__ AS b ON a.id=b.dataID"];
     $map = ['a.sid' => $catId, 'a.verify' => 1];
     $map = array_merge($map, $where);
     $order = ['a.id DESC'];
     $field = ['a.id' => 'id', 'a.title' => 'title', 'a.postTime' => 'create_time', 'a.picUrl' => 'picurl', 'a.thumb' => 'thumb', 'b.content' => 'content', '(@app_is_top:=0)' => 'app_is_top'];
     // 获取置顶数据.
     $modelAppTopInfo = new AppTopInfoModel();
     $topList = $modelAppTopInfo->fetchListOfSq($catId, C('app_top_info_num'));
     $ids = [];
     // 在列表要排除的 dataID.
     foreach ($topList['lists'] as $r) {
         $ids[] = (int) $r['id'];
     }
     if (!empty($ids)) {
         $map['a.id'] = ['NOT IN', $ids];
     }
     $totalRows = $this->alias('a')->join($joins)->where($map)->count();
     $result = $this->alias('a')->join($joins)->field($field)->where($map)->order($order)->page($page, $pageSize)->select();
     $result = FALSE === $result || NULL === $result ? [] : $result;
     // 合并置顶数据到顶表, 只有第一页要合并.
     if ($page === 1) {
         foreach ($topList['lists'] as $r) {
             $r['app_is_top'] = (int) $r['app_is_top'] === 0 ? 1 : 0;
             array_unshift($result, $r);
         }
     }
     return ['lists' => $result, 'totalRows' => $totalRows];
 }
コード例 #2
0
 /**
  * 指定栏目的分类信息列表.
  * 
  * @access public
  * @return void
  */
 public function getListByCategory()
 {
     $pageConfig = C('page');
     // 分页参数变量名配置项.
     $catId = (int) I('post.catid', 0);
     // 栏目 ID.
     $sortby = I('post.sortby', []);
     // 排序方式, 有排序时, 此参数为数组, 如 ['name' => id, 'value' => 'ASC'].
     $ispic = (bool) I('post.ispic', 0);
     // 是否有图.
     $page = (int) I('post.' . $pageConfig['var_page'], 1);
     // page.
     $pageSize = (int) I('post.' . $pageConfig['var_page_size'], 10);
     // rows.
     unset($pageConfig);
     if ($catId < 1) {
         $this->setAjaxData(Message::PARAM_ERROR, Message::get(Message::PARAM_ERROR))->myAjaxReturn();
     }
     // 实例化栏目模型.
     $modelCtgClass = new CtgClassModel();
     $categoryInfo = $modelCtgClass->fetchInfo($catId);
     if (empty($categoryInfo)) {
         $this->setAjaxData(Message::PARAM_ERROR, Message::get(Message::PARAM_ERROR))->myAjaxReturn();
     }
     unset($modelCtgClass);
     $moduleConfig = C('module_config')[$categoryInfo['module_id']];
     // 模型配置项.
     $moduleListPageConfig = $moduleConfig['listPage'];
     // 模型列表页配置.
     // ----------------------------------- 查询条件 ------------------------------------
     $whereMap = [];
     // 查询条件.
     // 组装查询条件.
     $whereMap = ['a.verify' => 1, 'a.ctgClassID' => $catId, 'a.cityID' => $this->getLocateCity()['id']];
     // 是否有图, 且请求有图.
     if ($moduleConfig['ispic'] && $ispic) {
         $whereMap['a.picUrl'] = ['neq', ''];
         // picUrl 不为空的表示有图.
     }
     // ---------------------------------- 排序方式 ------------------------------------
     $arrSort = [];
     // 排序项数组.
     $sortByConfig = $moduleListPageConfig['sortby'];
     // 模型排序项配置.
     $_allowSortValues = ['ASC', 'DESC'];
     // 有效的排序值.
     $_sortField = $_sortValue = $_sortName = '';
     if (isset($sortby['name']) && isset($sortby['value']) && isset($sortByConfig['field'][$sortby['name']])) {
         // 请求的排序是有效的排序项.
         $_sortName = strtolower($sortby['name']);
         $_sortValue = strtoupper($sortby['value']);
     } elseif (!empty($sortByConfig['default'])) {
         // 默认排序.
         $_sortName = strtolower($sortByConfig['default']['name']);
         $_sortValue = $sortByConfig['default']['value'];
     }
     $_sortValue = in_array($_sortValue, $_allowSortValues, TRUE) ? $_sortValue : 'DESC';
     // 默认 DESC.
     $sortNumFieldConf = C('module_config')['sortByNumField'];
     // 数字排序字段.
     $_sortField = $sortByConfig['field'][$_sortName];
     if (in_array($_sortName, $sortNumFieldConf, TRUE)) {
         $_sortField .= '+0';
     }
     $arrSort[$_sortField] = $_sortValue;
     unset($sortByConfig);
     // ---------------------------------- 查询结果的字段 -------------------------------
     $showField = $moduleListPageConfig['listField'];
     // 列出的字段.
     if (!$moduleConfig['ispic']) {
         // 无图, 删除 pic 字段.
         unset($showField['picurl']);
     }
     $arrField = array_flip($showField);
     // 将配置项字段反转就是 tp 中的别名.
     unset($moduleConfig, $moduleListPageConfig, $_allowSortValues, $_sortField, $_sortValue);
     // 释放变量.
     // 获取置顶数据.
     $modelAppTopInfo = new AppTopInfoModel();
     $topList = $modelAppTopInfo->fetchListOfClassified($catId, $categoryInfo['module_id'], C('app_top_info_num'), ['field' => $showField]);
     $ids = [];
     // 在列表要排除的 dataID.
     foreach ($topList['lists'] as $r) {
         $ids[] = (int) $r['id'];
     }
     if (!empty($ids)) {
         $whereMap['a.dataID'] = ['NOT IN', $ids];
     }
     // 实例化分类信息模型.
     $modelCtgData = new CtgDataModel();
     $lists = $modelCtgData->fetchListByCategory($catId, $categoryInfo['module_id'], $page, $pageSize, ['sortby' => $arrSort, 'where' => $whereMap, 'field' => $arrField]);
     // 合并置顶数据到顶表, 只有第一页要合并.
     if ($page === 1) {
         foreach ($topList['lists'] as $r) {
             $r['app_is_top'] = (int) $r['app_is_top'] === 0 ? 1 : 0;
             array_unshift($lists['lists'], $r);
         }
     }
     // picurl: 组装成完整的 url, 且有值的只返回一张.
     $lists['lists'] = $this->resolveImgUrlPath($lists['lists'], 'picurl', TRUE, '|', TRUE);
     $lists['lists'] = $this->resolveImgUrlPath($lists['lists'], 'thumb', TRUE, '|', TRUE);
     // 将 NULL 转换成 '' 以方便 app 处理.
     Util::resolveDbNull($lists['lists']);
     $this->setAjaxData(Message::SUCCESS, Message::get(Message::SUCCESS), $lists)->myAjaxReturn();
 }