🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
1\.插件目录结构 2\.插件控制器继承父类 3\.模板开发 4\.插件实例: 控制前端控制后端 #### 1.插件目录结构 ![Image](https://box.kancloud.cn/8b3510eefbd9abb74e2b0927221b82b2_298x456.jpeg) ![Image](https://box.kancloud.cn/8b3510eefbd9abb74e2b0927221b82b2_298x456.jpeg) 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.kancloud.cn/)\_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.kancloud.cn/)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.kancloud.cn/) 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 -