1\.插件目錄結構
2\.插件控制器繼承父類
3\.模板開發
4\.插件實例:
控制前端控制后端
#### 1.插件目錄結構


Controller文件夾存放插件控制器類
View文件夾存放各個控制器的視圖模板和其他模板開發一致,模板渲染使用$this -> \_display('模板文件名');
#### 2.插件控制器繼承父類
前端插件繼承前端插件父類(\\Home\\Controller\\AddonController) 實例:
class IndexController extends \\Home\\Controller\\AddonController{}
后端插件繼承后端插件父類(\\Admin\\Controller\\AddonController) 實例:
class AdminController extends \\Admin\\Controller\\AddonController{}
#### 3.模板開發
插件模板開發和普通模板開發一致
使用{:ADDON\_CSS}引入插件公共資源(Public)css樣式文件使用{:A[DDON](http://www.hmoore.net/)\_IMG}引入插件公共資源(Public)img圖片文件
使用{:ADDON\_js}引入插件公共資源(Public)javascript文件
4\.插件實例:
#### 控制前端
<?php
namespace Addon\\LeaveMessage\\Controller;
/\*引入微信SDK\*/
use Com\\WechatAuth;
/\*\*
\* 在線留言插件控制器
\*/
class IndexController extends \\Home\\Controller\\AddonController{ private $leaveMessageTable = null; // LeaveMessage留言數據表對象
private $wechatAuth = null; //微信WechatAuth public function construct(){
parent:: construct();
$firstId = I('get.firstId', 0);
$this -> leaveMessageTable = M('leaveMessage'); if((boolean) $firstId){
$res = M('Wechat') -> find($firstId);
//AppId
$appid = $res\['appid'\];
//Secret
$secret = $res\['secret'\];
//EncodingAESKey
$encodingAESkey = $res\['encodingAESkey'\];
//獲取被訪問公眾號access\_token
$token = session("token\_ed");
if($token){//access\_token未過期
$this -> wechatAuth = new WechatAuth($appid, $secret, $token);
} else {//access\_token已過期
$this -> wechatAuth = new WechatAuth($appid, $secret);
$token = $this -> wechatAuth -> getAccessToken();
//將被訪問的公眾號access\_token寫入session并設置過期時間session(array('expire' => $token\['expires\_in'\])); session("token\_ed", $token\['access\_token'\]);
}
//將被訪問的公眾號EncodingAESKey,id,wechatAuth類寫入session便于后期調用session('EncodingAESKey', $encodingAESkey);
session('wechatID', $firstId); session('wechatAuth', $this -> wechatAuth);
}else{
$this -> wechatAuth = session('wechatAuth');
}
}
public function index(){
$redirect = C('DOMAIN').U('Weixin/Index/auth/addon/LeaveMessage');
$redirect = $this -> wechatAuth -> getRequestCodeURL($redirect, $state); header("Location:".$redirect);
}
public function auth($code = ''){
// 獲取網頁授權access\_token數組try{
$accessTokenArray = $this -> wechatAuth -> getAccessToken('code', $code);
}catch(\\Exception $e){
$this -> redirect('Weixin/Index/index/firstId/'.session('wechatID').'/addon/LeaveMessage');exit;
}
// 獲取信息
$accessToken = $accessTokenArray\['access\_token'\]; // 網頁授權access\_token
$expiresIn = $accessTokenArray\['expires\_in'\]; // 網頁授權access\_token 有效時間
$refreshToken = $accessTokenArray\['refresh\_token'\]; // 網頁授權用戶刷新access\_token
$openId = $accessTokenArray\['openid'\]; // 網頁授權用戶唯一標識
// 緩存信息
// 獲取用戶信息
$userInfo = $this -> wechatAuth -> getUserInfo($openId); session('userInfo', $userInfo);
$this -> redirect('Weixin/Index/leaveMessage/addon/LeaveMessage');
}
public function leaveMessage(){
// 獲取當前用戶信息
$userInfo = session('userInfo'); if(!(boolean) $userInfo)
redirect('https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe71bdd9ed0bd39b 9&redirect\_uri=http%3A%2F%2Fyineng.uicp.cn%2Fwww%2Fweixin.php%3Fs%3D%2FWeixin%2FIndex
%2Fauth%2Faddon%2FLeaveMessage.html&response\_type=code&scope=snsapi\_userinfo&connect\_r edirect=1#wechat\_redirect');
if(IS\_POST){
// 組織入庫數據
$userInfo\['question'\] = I('post.question');
$userInfo\['question\_time'\] = time();
// 入庫
if($this -> leaveMessageTable -> field('openid,nickname,sex,province,city,country,headimgurl, question,question\_time') -> add($userInfo) === false){
$msg = '留言失敗!';
}else{
$msg = '留言成功,請等待管理員回復!';
}
$this -> ajaxReturn($msg);
}else{
// 獲取留言數據表已經查看了的留言
$condition = array('is\_view' => 1);
$[ord](http://www.hmoore.net/)er = 'question\_time DESC';
$list = $this -> leaveMessageTable -> where($condition) -> order($order) -> select(); 本文檔使用 看云 構建
\- 58 -
// 獲取管理員回復當前用戶的留言但未查看的留言條數
$map\['openid'\] = array('eq', $userInfo\['openid'\]);
$map\['is\_view'\] = array('eq', 0);
$map\['answer'\] = array('neq', '');
$count = $this -> leaveMessageTable -> where($map) -> count();
// 模板賦值
$this -> assign('userInfo', $userInfo);
$this -> assign('list', $list);
$this -> assign('count', $count);
// 模板渲染
$this -> \_display('leaveMessage');
}
}
public function showMe(){
// 獲取當前用戶信息
$userInfo = session('userInfo'); if(!(boolean) $userInfo)
redirect('https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe71bdd9ed0bd39b 9&redirect\_uri=http%3A%2F%2Fyineng.uicp.cn%2Fwww%2Fweixin.php%3Fs%3D%2FWeixin%2FIndex
%2Fauth%2Faddon%2FLeaveMessage.html&response\_type=code&scope=snsapi\_userinfo&connect\_r edirect=1#wechat\_redirect');
// 獲取當前用戶留言
$condition = array('openid' => $userInfo\['openid'\]);
$order = 'question\_time DESC';
$list = $this -> leaveMessageTable -> where($condition) -> order($order) -> select();
// 更新留言是否查看狀態,管理員已經回復但是當前用戶并未查看foreach ($list as $key => $value) {
if(!empty($value\['answer'\]) && !(boolean) $value\['is\_view'\]){
$data\['is\_view'\] = 1;
$data\['id'\] = $value\['id'\];
$this -> leaveMessageTable -> save($data);
}
}
// 模板賦值
$this -> assign('list', $list);
// 模板渲染
$this -> \_display('showMe');
}
}
?>
#### 控制后端
<?php
namespace Addon\\LeaveMessage\\Controller;
<?php
namespace Addon\\LeaveMessage\\Controller;
本文檔使用 看云 構建 - 59 -
/\*\*
\* 在線留言后臺插件控制器
\*/
class AdminController extends \\Admin\\Controller\\AddonController{ private $leaveMessageTable = null; // 在線留言數據表對象
public function construct(){ parent:: construct();
$this -> leaveMessageTable = M('LeaveMessage');
}
// 在線留言列表
public function index(){
//獲取操作按鈕
$button = get\_action\_button('/Admin/Admin/index/addon/LeaveMessage');
// 獲取留言列表
$list = $this -> leaveMessageTable -> field('\*') -> order('question\_time DESC') -> select();
// 模板賦值
$this -> assign('button', $button);
$this -> assign('list', $list);
// 模板渲染
$this -> \_display('index');
}
// 留言回復
public function revert($id = null){ if(empty($id)) $this -> error('操作不合法');
$answer = I('post.answer'); if(empty($answer)){
$this -> error('請填寫回復內容');
}
// 組織回復數據
$user = session('user');
$data\['id'\] = $id;
$data\['username'\] = $user\['username'\];
$data\['answer'\] = $answer;
$data\['answer\_time'\] = time();
// 數據入庫
if($this -> leaveMessageTable -> save($data) === false){
$this -> error('回復失敗');
}else{
$this -> success('回復成功');
}
}
/\*\*
\* \[delete 刪除留言\]
\* @pa[ram](http://www.hmoore.net/) integer $id \[留言id\]
\* @return \[type\] \[description\] 本文檔使用 看云 構建
\- 60 -
\*/
public function delete($id = null){ if(empty($id)) $this -> error('操作不合法');
//刪除條件
$map\['id'\] = array('in',$id);
//刪除留言記錄
if($this -> leaveMessageTable -> where($map) -> delete() === false){
$this -> error('刪除留言失敗');
}else{
$this -> success('刪除留言成功');
}
}
/\*批量刪除留言\*/
public function batchDelete($id = null){ if(empty($id)) $this -> error('操作不合法');
$this -> delete($id);
}
}
?>
\*/
public function delete($id = null){ if(empty($id)) $this -> error('操作不合法');
//刪除條件
$map\['id'\] = array('in',$id);
//刪除留言記錄
if($this -> leaveMessageTable -> where($map) -> delete() === false){
$this -> error('刪除留言失敗');
}else{
$this -> success('刪除留言成功');
}
}
/\*批量刪除留言\*/
public function batchDelete($id = null){ if(empty($id)) $this -> error('操作不合法');
$this -> delete($id);
}
}
?>
本文檔使用 看云 構建 - 61 -
- ThinkPHP模板
- 變量輸出
- 系統變量
- 系統變量輸出
- 常量輸出
- 配置輸出
- 語言變量
- 使用函數
- 默認值輸出
- 使用運算符
- 標簽庫
- 導入標簽庫
- 內置標簽
- 標簽庫預加載
- 模板繼承
- 修改定界符
- 普通標簽
- XML標簽
- 三元運算符
- 包含文件
- 使用模版表達式
- 使用模版文件
- 傳入參數
- 內置標簽
- Volist標簽
- Foreach標簽
- For標簽
- Switch標簽
- 比較標簽
- 范圍判斷標簽
- IN和NOTIN
- BETWEEN 和 NOTBETWEEN
- RANGE
- IF標簽
- Present標簽
- Empty標簽
- Defined標簽
- Assign標簽
- Define標簽
- 標簽嵌套
- import標簽
- 使用PHP代碼
- 使用php標簽
- 使用原生php代碼
- 原樣輸出
- 模板注釋
- 單行注釋
- 多行注釋
- 模板布局
- 第一種方式:全局配置方式
- 第二種方式:模板標簽方式
- 第三種方式:使用layout控制模板布局
- 模板替換
- 調用導航
- 調用欄目信息
- 根據欄目Id獲取欄目信息
- 首頁&封面調用信息列表
- 調用新聞列表
- 當前位置
- 當前位置
- 列表頁分頁
- 熱門信息
- 獲取熱門信息
- 上一篇&下一篇
- YNCMS函數
- YNCMS插件開發
- 開始開發