内置JS方法

内置JS方法,后台前台模板基本通用,后台不用单独加载JS,前台需要单独加载一下public/web/static/ljs.js文件,当然,前台JS你也可以完全自己写。

前后端通用方法

url - 全局URL变量

LCMS.url变量中包含了与PHP中$_L['url']中一模一样的参数,可参考全局变量文档查看具体参数说明。

loading - 加载动画

//开始loading
LCMS.util.loading();
//结束loading
LCMS.util.loading("close");

notify - 消息提示

LCMS.util.notify({
    type: "success", //success、error、warning、info、desk,desk桌面级通知需要https
    title: "通知标题", //标题可省略
    content: "通知内容",
    icon: "", //type=desk桌面通知,可以设置图标
});

load - 加载文件

动态加载css、js文件
LCMS.util.load({
    type: "js", //js、css
    src: "文件链接",
    async: true, //JS文件可设置是否异步加载
});

ajax - 请求

LCMS.util.ajax({}); //参数同jQuery的ajax方法
//所有ajax请求会自动组成队列按序执行,async为false时,会等待这个请求完成后,再执行后续其它ajax请求
//另外多一个参数 loading,true/有加载动画,false/无加载动画

EventSource - SSE流式请求

LCMS.util.EventSource(url, {
    method: "GET",
    headers: [],
    onmessage: function(msg) {
        //流式输出数据
        console.log(msg);
    },
    onclose: function() {
        //当请求完成关闭后
    },
    onerror: function(error) {
        //当请求出现错误时
    }
});
//使用fetch模拟的EventSource方法,所以fetch的参数都支持。

randStr - 随机字符串

LCMS.util.randStr(len, type);
//len 长度
//type 默认字母+数据、num数字、let字母

iframe - 打开内置弹窗

//打开iframe弹窗
LCMS.util.iframe({
    title: "弹窗标题",
    url: "弹窗链接",
    area: [], //可选,弹窗尺寸
});

//iframe内的页面关闭iframe弹窗自己
LCMS.util.iframe({
    type: "close"
});

getQuery - 获取url参数

LCMS.util.getQuery(key, url);
//key 参数名称
//url 链接地址,不传值默认当前链接

changeUrl - 替换URL中某个参数

LCMS.util.changeUrl(url, key, value);
//url 链接地址
//key 要替换的参数名
//value 替换后的参数值

getDate - 获取时间

LCMS.util.getDate(format, date);
//format 对齐PHP的时间格式,例如 Y:m:d 或 H:i:s 自己随意组合
//date 不传值默认为new Date()

play - 播放音频

LCMS.util.play({
    type: "audio",
    src: "文件链接",
    loop: 0, //是否循环播放
});

speak - 文字朗读

LCMS.util.speak(text);
//text 要朗读的内容,pc端使用

copy - 复制内容

LCMS.util.copy(text);
//复制内容到剪贴板,必须写在用户点击事件中

getFun - 执行某个方法

LCMS.util.getFun(fname);
//fname为方法名,如果方法存在才会执行。

crypto - 字符串加密

LCMS.util.crypto(type, options);
//加密字符串
LCMS.util.crypto("encode", {
    key: "密钥",
    text: "要加密的字符串"
});
//解密字符串
LCMS.util.crypto("decode", {
    key: "密钥",
    text: "要解密的字符串"
});
//对应后端加解密算法PHP为例
$key = md5("密钥");
//加密
openssl_encrypt("要加密的字符串","AES-256-CBC", $key, 0, $key);
//解密
openssl_decrypt("要解密的字符串","AES-256-CBC", $key, 0, $key);

//获取字符串md5
LCMS.util.crypto("md5", "字符串");
//获取字符串hash值
LCMS.util.crypto("hash", "字符串");

后端独有方法

setMenu - 设置后台右上角菜单

//支持同时设置多个菜单
LCMS.util.setMenu([{
    title: "按钮名称",
    icon: "home", //参考layui图标
    color: "#FFFFFF", //按钮颜色
    bgcolor: "#000000", //不需要设置为none
    url: "", //点击的链接,支持链接地址,或者function方法
    targer: "", //如果url是链接地址,此项可以填_blank或_self
}, {
    title: ''' //第二个菜单
}]);

JS文件上传与操作接口

// 通过JS上传图片或文件
LCMS.plugin.upload.direct({
    type: "image", // 或者file、url
    file: "", // type为image和file可以传入File对象、或base64Data格式的文件,type为url时传入图片链接
    local: 0, // 如果开启云存储,是否强制存储到本地服务器
    success: function (res) {
        res.data.dir; //文件目录
        res.data.filename; //文件名称
        res.data.src; //文件完整链接含CDN
        res.data.original; //文件相对链接../开头
    }, // 成功回调,返回图片链接等信息
    error: function (error) {}, // 失败回调
    complete: function () {}, // 完成回调
});

// 给upload组件添加图片
// id为upload组件的ID,image为base64Data格式的图片
LCMS.plugin.upload.add(id, image);

// 移除upload组件图片
// id为upload组件的ID,index为要移除图片的位置,index不传的话移除所有图片
LCMS.plugin.upload.remove(id, index);

获取当前焦点编辑器ID

// 相关编辑器操作,可查看UEditor文档
// http://fex.baidu.com/ueditor/
top.LCMS.plugin.editor.focusid;

TAGS组件操作

//添加TAGS
LCMS.plugin.tags.add("组件ID", "TAGS1|TAGS2|TAGS3");

//清空TAGS
LCMS.plugin.tags.clear("组件ID");

AI能力调用

//静默调用
LCMS.plugin.aimodel.chat({
    system: "你是我的AI助手", //系统提示词
    messages: [{
        role: "user", //依次为 user、assistant
        content: "你好"
    }],
    onmessage: function(txt, html) {
        //流式输出数据
        //txt、流式内容
        //html、自动html格式化的内容
    },
    onclose: function(content, html) {
        //请求完成后执行
        //content、所有回答内容txt格式
        //html、所有回答内容html格式
    },
    onerror: function(error) {
        //请求错误
    }
});

//弹出聊天窗口调用
LCMS.plugin.aimodel.chat({
    window: "AI助手", //聊天窗标题
    system: "你是我的AI助手", //系统提示词
    messages: [{
        role: "user", //依次为 user、assistant
        content: "你好"
    }], //可选,打开窗口自动开始的对话
    plugins: [{
        title: "按钮标题",
        tips: "按钮提示,鼠标指在按钮上显示",
        onclick: function(chatFunc, chat) {
            //按钮点击后操作
            //chatFunc、对话操作
            //chat、历史对话内容
            
            //清除历史对话,false 只清除请求中的历史会话,true 清除请求和页面显示的历史对话
            chatFunc.clear(false);
            //发送一句话
            chatFunc.chat("讲个笑话吧");
        }
    }], //可选,输入框上方按钮
    chatbtns: function(){
        title: "按钮标题",
        tips: "按钮提示",
        onclick: function (chatFunc, chat) {
            //chatFunc、同plugins
            //chat、返回content 回答原始内容、和html 回答html内容,两个参数
        },
    }//全局AI每个回复后的按钮,此方法可写在plugins每个按钮里,这样不同回复可显示不同按钮。
});
大纲