讯睿CMS通过ID获取数据的方式

有的时候我们需要通过AJAX获取某ID数据,通过分析

public function index() {
		// 共享模块通过id查找内容
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		$row = \Phpcmf\Service::M()->table(SITE_ID.'_share_index')->get($id);
		$mid = $row['mid'];
		!$mid && exit($this->goto_404_page(dr_lang('无法通过id找到共享模块的模块目录')));
		// 初始化模块
		$this->_module_init($mid);
		// 调用内容方法
		$this->_Show($id, null, max(1, (int)\Phpcmf\Service::L('input')->get('page')));
	}
 // 模块内容页
    // $param 自定义字段检索
    protected function _Show($id = 0, $param = [], $page = 1, $rt = 0) {

        // 启用页面缓存
        if (SYS_CACHE && SYS_CACHE_PAGE && !defined('SC_HTML_FILE')) {
            $this->cachePage(SYS_CACHE_PAGE * 3600);
        }

        // 通过自定义字段查找id
        $is_id = 1;
        if (!$id && isset($param['field']) && $this->module['field'][$param['field']]['ismain']) {
            $id = md5($param['field'].$param['value']);
            $is_id = 0;
        }

        $name = 'module_'.$this->module['dirname'].'_show_id_'.$id.($page > 1 ? $page : '');
        $data = \Phpcmf\Service::L('cache')->get_data($name);
        if (!$data) {
            $data = $this->content_model->get_data($is_id ? $id : 0, 0, $param);
            if (!$data) {
                $this->goto_404_page(dr_lang('%s内容(#%s)不存在', $this->module['name'], $id));
                return;
            }

            // 检测转向字段
            foreach ($this->module['field'] as $t) {
                if ($t['fieldtype'] == 'Redirect' && $data[$t['fieldname']]) {
                    // 存在转向字段时的情况
                    \Phpcmf\Service::M()->db->table(SITE_ID.'_'.$this->module['dirname'])->where('id', $id)->set('hits', 'hits+1', FALSE)->update();
                    \Phpcmf\Service::V()->assign('goto_url', $data[$t['fieldname']]);
                    \Phpcmf\Service::V()->display('goto_url');
                    return;
                }
            }

            // 处理关键字标签
            $data['tag'] = $data['keywords'];
            $data['tags'] = [];
            if ($data['keywords']) {
                $tag = explode(',', $data['keywords']);
                foreach ($tag as $t) {
                    $t = trim($t);
                    $t && $data['tags'][$t] = $this->content_model->get_tag_url($t);
                }
            }

            // 关闭插件嵌入
            $is_fstatus = dr_is_app('fstatus') && isset($this->module['field']['fstatus']) && $this->module['field']['fstatus']['ismain'] ? 1 : 0;

            // 上一篇文章
            $builder = \Phpcmf\Service::M()->db->table($this->content_model->mytable);
            $builder->where('catid', (int)$data['catid'])->where('status', 9);
            $is_fstatus && $builder->where('fstatus', 1);
            $builder->where('id<', $id)->orderBy('id desc');
            $data['prev_page'] = $builder->limit(1)->get()->getRowArray();

            // 下一篇文章
            $builder = \Phpcmf\Service::M()->db->table($this->content_model->mytable);
            $builder->where('catid', (int)$data['catid'])->where('status', 9);
            $is_fstatus && $builder->where('fstatus', 1);
            $builder->where('id>', $id)->orderBy('id asc');
            $data['next_page'] = $builder->limit(1)->get()->getRowArray();

            // 格式化输出自定义字段
            $fields = $this->module['category'][$data['catid']]['field'] ? array_merge($this->module['field'], $this->module['category'][$data['catid']]['field']) : $this->module['field'];
            $fields['inputtime'] = ['fieldtype' => 'Date'];
            $fields['updatetime'] = ['fieldtype' => 'Date'];

            // 格式化字段
            $data = \Phpcmf\Service::L('Field')->app($this->module['dirname'])->format_value($fields, $data, $page);

            // 模块的回调处理
            $data = $this->content_model->_call_show($data);

            // 缓存结果 
            if ($data['uid'] != $this->uid && SYS_CACHE) {
                if ($this->member && $this->member['is_admin']) {
                    // 管理员时不进行缓存
                    \Phpcmf\Service::L('cache')->init()->delete($name);
                } else {
                    \Phpcmf\Service::L('cache')->set_data($name, $data, SYS_CACHE_SHOW * 3600);
                    if (!$is_id) {
                        // 表示自定义查询,再缓存一次ID
                        \Phpcmf\Service::L('cache')->set_data(str_replace($id, $data['id'], $name), $data, SYS_CACHE_SHOW * 3600);
                    }
                }
            }
        }

        // 挂钩点 内容读取之后
        \Phpcmf\Hooks::trigger('module_show_read_data', $data);

        // 状态判断
        if ($data['status'] == 10 && !($this->uid == $data['uid'] || $this->member['is_admin'])) {
            $this->goto_404_page(dr_lang('内容被删除,暂时无法访问'));
            return;
        }

        $catid = $data['catid'];

        if ($this->is_hcategory) {
            $parent = $related = [];
            $this->content_model->_hcategory_member_show_auth();
        } else {
            // 无权限访问栏目内容
            if (!dr_member_auth($this->member_authid, $this->member_cache['auth_module'][SITE_ID][$this->module['dirname']]['category'][$catid]['show'])) {
                $this->_msg(0, dr_lang('您的用户组无权限访问栏目'), $this->uid || !defined('SC_HTML_FILE') ? '' : dr_member_url('login/index'));
                return;
            }
            // 判断是否同步栏目
            if ($data['link_id'] && $data['link_id'] > 0) {
                \Phpcmf\Service::V()->assign('gotu_url', dr_url_prefix($data['url'], $this->module['dirname']));
                \Phpcmf\Service::V()->display('go.html', 'admin');
                return;
            }
            // 获取同级栏目及父级栏目
            list($parent, $related) = dr_related_cat(
                !$this->module['share'] ? $this->module['category'] : \Phpcmf\Service::L('cache')->get('module-'.SITE_ID.'-share', 'category'),
                $data['catid']
            );
        }


        // 判断分页
        if ($page && $data['content_page'] && !$data['content_page'][$page]) {
            $this->goto_404_page(dr_lang('该分页不存在'));
            return;
        }

        // 判断内容唯一性
        !$rt && \Phpcmf\Service::L('Router')->is_redirect_url(dr_url_prefix($data['url'], $this->module['dirname']));

        // 传入模板
        \Phpcmf\Service::V()->assign($data);
        \Phpcmf\Service::V()->assign($this->content_model->_format_show_seo($this->module, $data, $page));
        \Phpcmf\Service::V()->assign([
            'cat' => $this->module['category'][$catid],
            'top' => $this->module['category'][$catid]['topid'] ? $this->module['category'][$this->module['category'][$catid]['topid']] : $this->module['category'][$catid],
            'page' => $page,
            'params' => ['catid' => $catid],
            'parent' => $parent,
            'markid' => 'module-'.$this->module['dirname'].'-'.$catid,
            'related' => $related,
            'urlrule' => \Phpcmf\Service::L('Router')->show_url($this->module, $data, '[page]'),
            'fix_html_now_url' => defined('SC_HTML_FILE') ? dr_url_prefix(\Phpcmf\Service::L('Router')->show_url($this->module, $data, $page), $this->module['dirname'], SITE_ID, \Phpcmf\Service::V()->_is_mobile == 'mobile') : '', // 修复静态下的当前url变量
        ]);
        \Phpcmf\Service::V()->module($this->module['dirname']);
        !$rt && (\Phpcmf\Service::V()->display(isset($data['template']) && strpos($data['template'], '.html') !== FALSE && is_file(\Phpcmf\Service::V()->get_dir().$data['template']) ? $data['template'] : ($this->module['category'][$data['catid']]['setting']['template']['show'] ? $this->module['category'][$data['catid']]['setting']['template']['show'] : 'show.html')));
        return $data;
    }

分析如上代码我们可以写出自己的AJAX请求到某ID数据:

           // 共享模块通过id查找内容
            $get=\Phpcmf\Service::L('input')->get('', true);
            $id=$get['id'];
            $row = \Phpcmf\Service::M()->table(SITE_ID.'_share_index')->get($id);
            $mid = $row['mid'];
            !$mid && exit($this->goto_404_page(dr_lang('无法通过id找到共享模块的模块目录')));
            // 初始化模块
            $this->_module_init($mid);
            $page=1;
            $name = 'module_'.$this->module['dirname'].'_show_id_'.$id.($page > 1 ? $page : '');
            $data = \Phpcmf\Service::L('cache')->get_data($name);
            if(!$data){
                $data = $this->content_model->get_data($id);
            }
            echo json_encode($data);exit();

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ymwlchina

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值