企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 前端代码 引入相关文件在此省略,注意回调函数的对象(var that = $(this);)即可 ``` {volist name="addonslist" id="vo"} <tr> <td> {switch name="vo.status"} {case value="0"}<a class="addonmanage" href="{:url('addonmanage',['id'=>$vo.id])}" title="安装">安装</a>&nbsp;&nbsp;{/case} {case value="1"}<a class="addonmanage" href="{:url('addonmanage',['id'=>$vo.id])}" title="卸载">卸载</a>&nbsp;&nbsp;{/case} {/switch} </td> </tr> {/volist} ``` ``` <script> $(document).ready(function() { $(".addonmanage").click(function() { var url = $(this).attr('href'); var title = $(this).attr('title'); var that = $(this); $.ajax({ type: "POST", dataType:"json", url:url, data:{}, success:function(obj){ if(obj.status==200){ layer.open({ content: obj.msg, btn: ['确定'], shade: 0.1, icon: 1, end: function(){ // 修改状态显示 if(title=='安装'){ that.attr('title','卸载'); that.text('卸载'); }else{ that.attr('title','安装'); that.text('安装'); } } }); }else{ layer.open({ content: obj.msg, btn: ['确定'], shade: 0.1, icon: 2 }); } } }); return false; //终止a标签打开href }); }); </script> ``` ## 后台处理 ``` /** * * 插件安装和卸载 * @return bool */ public function addonmanage(){ if(request()->isPost()){ $id = input('id'); // 获取status值,0为安装,1为卸载 $addons = Db::name('addons')->where('id',$id)->find(); $class = \think\Loader::parseName($addons['name'],1); //首字母大写 $namespace = "\\addons\\" . $addons['name'] . "\\" . $class; $book = new $namespace(); if($addons['status']){ // 已安装则卸载 $result = $book->uninstall(); if($result){ $data['status'] = 200; $data['msg'] = '已卸载!'; }else{ $data['status'] = 202; $data['msg'] = '卸载失败!'; } }else{ $result = $book->install(); if($result){ $data['status'] = 200; $data['msg'] = '已安装!'; }else{ $data['status'] = 202; $data['msg'] = '安装失败!'; } } return json($data); } } ``` ## 安装方法 ``` // 插件安装方法(测试) public function install(){ $result = Db::name('addons')->where('name','book')->setField('status',1); return $result; } ``` ## 卸载方法 ``` // 插件卸载方法 public function uninstall(){ $result = Db::name('addons')->where('name','book')->setField('status',0); return $result; } ```