Thinkcmf 二次开发
一、 创建模板 demo
1 Tpl下创建demo文件-----后台启用新的模板 (网站信息--模板方案)
2 在模板在tpl/demo目录下创建Portal目录,然后在Portal目录下创建index.html
首页基本分为以下部分head,header,content,footer,scripts这五大部分
<<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{$site_seo_title} {$site_name}</title>
<meta name="keywords" content="{$site_seo_keywords}" />
<meta name="description" content="{$site_seo_description}">
<tc_include file="Public:head"/><!--head部分 加载公共CSS和js等 -->
</head>
<body>
<tc_include file="Public:header"/><!--header部分 加载导航等 -->
<!-- content 开始 -->这是ThinkCMF首页哟!!<!-- content 结束 -->
<tc_include file="Public:footer"/><!--footer部分 加载公共底部,友情链接等 -->
<tc_include file="Public:scripts"/><!--scripts部分 加载公共js等 -->
</body> </html>
3 引入公共文件
首先在模板目录demo下创建公共文件目录Public,这个目录主要是用来存资源文件,公共文件等;接着在Public下创建第1步里的四个文件head.html,header.html,footer.
html,scripts.html
file只支持:1. Public:文件名 模板Public目录下的文件;
2. :文件名 模板根目录下的文件;
Head.html:
<tc_include file=":config"/>
<meta name="author" content="滴滴好活"> <meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<!-- Set render engine for 360 browser -->
<meta name="renderer" content="webkit">
<!-- No Baidu Siteapp-->
<meta http-equiv="Cache-Control" content="no-siteapp"/>
<link href="__TMPL__Public/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="__TMPL__Public/css/bootstrap.css">
<link rel="stylesheet" href="__TMPL__Public/css/app.css">
<link rel="icon" href="__TMPL__Public/img/logo.png">
<style>
@-webkit-keyframes animations{ 0%{height: 100%;opacity: 0.3;padding-top:0;}/**透明度有浅到深,从加载到加载完**/
100%{height: 40px;opacity: 0.8;} } </style>
Script.html
<script src="__TMPL__Public/js/jquery-1.11.3.js"></script>
<script src="__TMPL__Public/js/bootstrap.js"></script>
<script src="__TMPL__Public/js/html5shiv.min.js"></script>
<script src="__TMPL__Public/js/respond.min.js"></script>
<script src="__TMPL__Public/js/app.js"></script>
<script src="__TMPL__Public/js/bootlint.js"></script>
<!--[endif]-->
4 使用全局变量
{$site_name} /站点名称
{$site_host} /站点域名
{$site_root} /安装目录
{$site_icp} /备案信息
{$site_admin_email} /管理员邮箱
{$site_tongji} /页面统计代码
{$site_seo_title} /SEO标题
{$site_seo_keywords} /SEO关键字
{$site_seo_description} /SEO描述
5 加载模板资源
1.加载模板Public下的图片
<img src="__TMPL__Public/images/prv/human-img-3.png"/>
__TMPL__ 这是一个模板常量,表示当前模板根目录
2.加载模板Public下的css,js
<link href="__TMPL__Public/css/demo.css" rel="stylesheet" type="text/css" /><script type="text/javascript" src="__TMPL__Public/js/jquery.js" ></script>_
二、制作首页内容
1 制作幻灯片
<php> $home_slides=sp_getslide($top_slides); $home_slides=empty($home_slides)?$default_home_slides:$home_slides; $home_slides=($home_slides); $result=array_pop($home_slides); </php> <div class="carousel slide" id="ad4" data-ride="carousel" data-interval="3000"> <div class="carousel-inner"> <div class="item active"> <a href="{$result.slide_url}"><img src="{$result['slide_pic']}" alt=""></a> </div> <foreach name="home_slides" item="vo"> <div class="item"> <a href="{$vo.slide_url}"><img src="{$vo['slide_pic']}" alt=""></a> </div> </foreach> </div> |
配置默认幻灯片 config.html
$default_home_slides=array(
array(
"slide_name"=>"ThinkCMFX1.6.0发布啦!",
"slide_pic"=>$tmpl."Public/images/demo/1.jpg", "slide_url"=>"", ),
array(aaaa
"slide_name"=>"ThinkCMFX1.6.0发布啦!",
"slide_pic"=>$tmpl."Public/images/demo/2.jpg", "slide_url"=>"", ) )
2 制作文章内页--分页文章显示
<php> $posts=sp_sql_posts('cid:6;field:post_title,post_content;order:listorder asc'); Print($posts); //可以打印出结果 field 查询范围 cid 文章管理中心的id号 order排序 </php> <foreach name="posts" item="vo"> /* 遍历数组 */ {$vo.term_id }<br> 文章分类id {$vo.post_author }<br> 文章作者id,后台管理员,对应于表users里的ID; {$vo.post_keywords }<br> {$vo.post_date }<br> 文章发布日期 格式2014-01-01 00:00:00 {$vo.post_content }<br> 文章内容 {$vo.post_title }<br> 文章标题 {$vo.post_excerpt }<br> 文章摘要 {$vo.post_modified}<br> 文章更新日期 {$vo.user_nicename }<br> 管理员名称 {$vo.user_email }<br> 管理员邮箱 {$vo.post_source}<br> 文章来源 <php> $smeta=json_decode($vo['smeta'],true);/* 把smeta转化成数组 */ </php> <img src="{:sp_get_asset_upload_path($smeta['thumb'])}"/> </foreach> |
<php> $photo=$Portal_index['Cat']['Photo']; $contentPh=sp_sql_posts_paged("cid:$photo;field:post_title,term_id,smeta;order:post_date desc;",'4'); </php> <foreach name="contentPh['posts']" item="vo"> <php> $smeta=json_decode($vo['smeta'],true);/* 把smeta转化成数组 */ </php> <div class="col-md-3 col-xs-12"> <div class="drop"> <a href="{:leuu('article/index',array('id'=>$vo['id']))}" target="_blank"> <img src="{:sp_get_asset_upload_path($smeta['thumb'])}" alt="" class="img-responsive"> </a><a href="{:leuu('article/index',array('id'=>$vo['id']))}"> <p class="drop-u" target="_black">{$vo.post_title}</p> </a> </div> </div> </foreach> |
- Config.html
// 首页配置 $Portal_index= array( 'Article'=>array(), 'Page'=>array(), 'Cat'=>array( 'Photo'=>5, //合作伙伴 'Newscenter'=>8, //新闻中心 'Advantage'=>2, //产品优势 'Industry'=>7 , //行业动态 'Question'=>9 //疑难解答 ), ); $top_slides='top_slides'; //幻灯片设置 $default_home_slides=array( array( "slide_name"=>"广告1", "slide_pic"=>"banner-bg.png", "slide_url"=>"",),); |
三、创建登录页面 <a href="{:u('user/login/index')}">
1 框架 User/login.html
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>{$site_seo_title} {$site_name}</title> <meta name="keywords" content="{$site_seo_keywords}" /> <meta name="description" content="{$site_seo_description}"> <tc_include file="Public:head"/><!--head部分 加载公共CSS和js等 --> </head> <body> <tc_include file="Public:header" /><!--header部分 加载导航等 --> {content} <tc_include file="public:footer"/><!--footer部分 加载公共底部,友情链接等 --> <tc_include file="Public:scripts"/><!--scripts部分 加载公共js等 --> </body> </html> |
2 内容
<form class="J_ajaxForms" action="{:U('user/login/dologin')}" method="post"> <label for="input_username">账号</label> <input type="text" id="input_username" name="username" placeholder="请输入用户名或者邮箱" class="span3"> <label for="input_password">密码</label> <input type="password" id="input_password" name="password" placeholder="请输入密码" class="span3">
<label for="input_verify">验证码</label> <input type="text" id="input_verify" name="verify" placeholder="请输入验证码" class="span3"> {:sp_verifycode_img('length=4&font_size=15&width=100&height=35&charset=1234567890')} <label for="input_repassword"></label> <label class="checkbox persistent"><input type="checkbox" name="terms" value="1">我同意<a href="#">网站内容服务条款</a></label> <button class="btn btn-primary J_ajax_submit_btn" type="submit">确定</button> </form> |
3 控制器
namespace User\Controller; use Common\Controller\HomeBaseController; class LoginController extends HomeBaseController { function index(){ if(sp_is_user_login()){ //已经登录时直接跳到首页 redirect(__ROOT__."/"); }else{ $this->display(":login"); } } function active(){ $this->check_login(); $this->display(":active"); } function doactive(){ $this->check_login(); $this->_send_to_active(); $this->success('激活邮件发送成功,激活请重新登录!',U("user/index/logout")); } function forgot_password(){ $this->display(":forgot_password"); } function doforgot_password(){ if(IS_POST){ if(!sp_check_verify_code()){ $this->error("验证码错误!"); }else{ $users_model=M("Users"); $rules = array( //array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间) array('email', 'require', '邮箱不能为空!', 1 ), array('email','email','邮箱格式不正确!',1), // 验证email字段格式是否正确 ); if($users_model->validate($rules)->create()===false){ $this->error($users_model->getError()); }else{ $email=I("post.email"); $find_user=$users_model->where(array("user_email"=>$email))->find(); if($find_user){ $this->_send_to_resetpass($find_user); $this->success("密码重置邮件发送成功!",__ROOT__."/"); }else { $this->error("账号不存在!"); } } } } } //登录验证 function dologin(){ if(!sp_check_verify_code()){ $this->error("验证码错误!"); } $users_model=M("Users"); $rules = array( //array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间) array('username', 'require', '用户名或者邮箱不能为空!', 1 ), array('password','require','密码不能为空!',1), ); if($users_model->validate($rules)->create()===false){ $this->error($users_model->getError()); } extract($_POST); if(strpos($username,"@")>0){//邮箱登陆 $where['user_email']=$username; }else{ $where['user_login']=$username; } $users_model=M('Users'); $result = $users_model->where($where)->find(); $ucenter_syn=C("UCENTER_ENABLED"); $ucenter_old_user_login=false; $ucenter_login_ok=false; if($ucenter_syn){ setcookie("thinkcmf_auth",""); include UC_CLIENT_ROOT."client.php"; list($uc_uid, $username, $password, $email)=uc_user_login($username, $password); if($uc_uid>0){ if(!$result){ $data=array( 'user_login' => $username, 'user_email' => $email, 'user_pass' => sp_password($password), 'last_login_ip' => get_client_ip(), 'create_time' => time(), 'last_login_time' => time(), 'user_status' => '1',); $id= $users_model->add($data); $data['id']=$id; $result=$data; } }else{ switch ($uc_uid){ case "-1"://用户不存在,或者被删除 if($result){//本应用已经有这个用户 if($result['user_pass'] == sp_password($password)){//本应用已经有这个用户,且密码正确,同步用户 $uc_uid2=uc_user_register($username, $password, $result['user_email']); if($uc_uid2<0){ $uc_register_errors=array( "-1"=>"用户名不合法", "-2"=>"包含不允许注册的词语", "-3"=>"用户名已经存在", "-4"=>"Email格式有误", "-5"=>"Email不允许注册", "-6"=>"该Email已经被注册", ); $this->error("同步用户失败--".$uc_register_errors[$uc_uid2]); } $uc_uid=$uc_uid2; }else{ $this->error("密码错误!"); } } break; case -2://密码错 if($result){//本应用已经有这个用户 if($result['user_pass'] == sp_password($password)){//本应用已经有这个用户,且密码正确,同步用户 $uc_user_edit_status=uc_user_edit($username,"",$password,"",1); if($uc_user_edit_status<=0){ $this->error("登陆错误!"); } list($uc_uid2)=uc_get_user($username); $uc_uid=$uc_uid2; $ucenter_old_user_login=true; }else{ $this->error("密码错误!"); } }else{ $this->error("密码错误!"); }break; } } $ucenter_login_ok=true; echo uc_user_synlogin($uc_uid); } //exit(); if($result != null) { if($result['user_pass'] == sp_password($password)|| $ucenter_login_ok){ $_SESSION["user"]=$result; //写入此次登录信息 $data = array( 'last_login_time' => date("Y-m-d H:i:s"), 'last_login_ip' => get_client_ip(), ); $users_model->where("id=".$result["id"])->save($data); $redirect=empty($_SESSION['login_http_referer'])?__ROOT__."/":$_SESSION['login_http_referer']; $_SESSION['login_http_referer']=""; $ucenter_old_user_login_msg=""; if($ucenter_old_user_login){ //$ucenter_old_user_login_msg="老用户请在跳转后,再次登陆";} $this->success("登录验证成功!", $redirect); }else{ $this->error("密码错误!"); } }else{ $this->error("用户名不存在!"); } } 。。。。。。 |
四、创建注册页面 <a href="{:U('user/register/index')}">
1 框架同登录页
2 注册页面 User/register.html
<form class="form-horizontal J_ajaxForm" action="{:U('user/register/doregister')}" method="post"> <label class="control-label" for="input_username">账号</label> <input type="text" id="input_username" name="username" placeholder="请输入账号" class="span3"> <label class="control-label" for="input_email">邮箱</label> <input type="text" id="input_email" name="email" placeholder="请输入邮箱" class="span3"> <label class="control-label" for="input_password">密码</label> <input type="password" id="input_password" name="password" placeholder="请输入密码" class="span3"> <label class="control-label" for="input_repassword">重复密码</label> <input type="password" id="input_repassword" name="repassword" placeholder="请输入重复密码" class="span3"> <label class="control-label" for="input_verify">验证码</label> <input type="text" id="input_verify" name="verify" placeholder="请输入验证码" class="span3"> {:sp_verifycode_img('length=4&font_size=15&width=100&height=35&charset=1234567890')} <label class="control-label" for="input_repassword"></label> <label class="checkbox persistent"><input type="checkbox" name="terms" value="1">我同意<a href="#">网站内容服务条款</a></label> <button class="btn btn-primary J_ajax_submit_btn" type="submit" data-wait="1500">确定注册</button> </form> |
五、 联系我们
1 listercontroller.class.php
class ListController extends HomeBaseController { public function index() { $term=sp_get_term($_GET['id']); //文章内页 if(empty($term)){ header('HTTP/1.1 404 Not Found'); header('Status:404 Not Found'); if(sp_template_file_exists(MODULE_NAME."/404")){ $this->display(":404"); } return ; } var_dump($term); $tplname=$term["list_tpl"]; $tplname=sp_get_apphome_tpl($tplname, "list"); $this->assign($term); $this->assign('cat_id', intval($_GET['id'])); $this->display(":$tplname"); } |
$term=sp_get_term($_GET['id']);打印结果 array(15) { ["term_id"]=> string(1) "4" ["name"]=> string(12) "联系我们" ["slug"]=> string(0) "" ["taxonomy"]=> string(7) "article" ["description"]=> string(0) "" ["parent"]=> string(1) "0" ["count"]=> string(1) "0" ["path"]=> string(3) "0-4" ["seo_title"]=> string(0) "" ["seo_keywords"]=> string(0) "" ["seo_description"]=> string(0) "" ["list_tpl"]=> string(5) "index" ["one_tpl"]=> string(7) "article" ["listorder"]=> string(1) "0" ["status"]=> string(1) "1" } |
2 关于我们
<php $contentAb=sp_sql_posts_paged("cid:$Portal_about;field:post_title,post_content,post_excerpt;order:post_date desc;"; </php>
<foreach name="contentAb['posts']" item="vo">
<div class="jianjie"><p>{$vo[post_title]}</p>
<span>{$vo.post_excerpt }</span> <span>{$vo.post_content }</span>
</div>
</foreach>
六、创建个人中心
1 登陆后验证 LoginController.class.php---function dologin()
$users_model=M("Users");
$rules = array( //array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
array('username', 'require', '用户名或者邮箱不能为空!', 1 ),
array('password','require','密码不能为空!',1),);
if($users_model->validate($rules)->create()===false){
$this->error($users_model->getError()); }
extract($_POST);
if(strpos($username,"@")>0){//邮箱登陆 $where['user_email']=$username;
}else{ $where['user_login']=$username; }
If (strpos($username,"@")>0){//邮箱登陆 $where['user_email']=$username;
}else{ $where['user_login']=$username; }
2 登录后显示登陆信息 header.html script.html
<div id="main-menu-user"> <div class='userout user' > <!--还未登陆--> <ul class="nav navbar-nav navbar-right"> <li><a href="{:u('user/register/index')}" data-toggle="dropdown">注册</a> </li></ul> <ul class="nav navbar-nav navbar-right"> <li><a href="{:u('user/login/index')}" data-toggle="dropdown">登陆</a></li></ul> <ul class="nav navbar-nav navbar-right" style="margin-top:10px"> <li > <img src="__TMPL__/Public/img/headicon.png" class="headicon"/> </li> </ul> </div> <div class="userlogin user" ><!--已经登陆--> <ul class="nav navbar-nav navbar-right"> <li><a href="{:u('user/index/logout')}" data-toggle="dropdown">退出</a></li></ul> <ul class="nav navbar-nav navbar-right"><li> <a href="{:u('user/center/index')}" data-toggle="dropdown" class="user-nicename"></a> </li></ul> <ul class="nav navbar-nav navbar-right" style="margin-top:10px"> <li ><img src="" class="headicon"/> </li></ul> </div> </div> <script> $(function () { /*控制导航栏显示登陆还是个人中心*/ $.post("{:U('user/index/is_login')}",{},function(data){ if(data.status==1){ if(data.user.avatar){ $("#main-menu-user ul li .headicong").attr("src",data.user. avatar.indexOf("http")==0?data.user.avatar:"__UPLOAD__avatar/"+data.user.avatar); } $("#main-menu-user ul li .user-nicename").text(data.user.user_nicename!=""?data.use r.user_nicename:data.user.user_login); $("#main-menu-user .userlogin").show(); $("#main-menu-user .userout").hide(); } if(data.status==0){ $("#main-menu-user .userout").show(); $("#main-menu-user .userlogin").hide(); } }); }); </script> |
同时与之对应的传值IndexController.class.php--function is_login()
function is_login(){ if(sp_is_user_login()){ $this->ajaxReturn(array("status"=>1,"user"=>sp_get_current_user())); }else{ $this->ajaxReturn(array("status"=>0,"info"=>"此用户未登录!")); } } |