瀑布流

燃烧的冰2021-03-25  5.0K+

API 利用之一,前后端完全分离和无刷新瀑布流

 

wellcms 前台的所有页面,均内置了 API,支持异步 GET 和 POST,可以做到完全的前后端分离。通过 API 可制作小程序和APP,现在的APP、小程序多是无刷新瀑布流,那么 wellcms 也是可以做到。

 

根据个人习惯,自行挑选前端框架制作小程序和APP。

 

1.开启 API,conf/conf.php

'api_on' => 1,

2.内置 js 获取数据

<script>
    /*
    * 通过 API 获取数据,格式为 Object 无需再JSON转换,直接使用
    * 获取路径http://www.x.com/list-1.html
    * 格式化链接 xn.url('list-1')
    * 列表翻页加载 xn.url('list-1-2') 最后一个 2 为翻页码,1为第1页,2为第2页,3为第3页
    * Object 返回数据中
    * "page": 1, 为当前页码
    * "num": "2", 为当前版块主题数量,根据此数量计算分页数量
    * list 为列表页
    * 1为fid
    * 其他页面同样如此,再浏览器看到的链接都可以直接使用
    *
    * 链接有携带其他参数
    * 如http://www.x.com/list-1.html?default=1&orderby=lastpid
    * 格式化链接
    * xn.url('list-1', {'default':'1', 'orderby':'lastpid'})
    * code = 0 返回正常数据, message 为返回的数据
    * code > 0 数据错误,message 为错误提示
    * code < 0 数据不存在,message 为错误提示
    * code 返回非数字错误,表单提交的错误参数,message 为错误提示
    * 不需要显示错误,可删除
    * 学会使用 console.log(message); 打印查看数据
    * JS 代码请勿使用双斜杠 // 注释
    *  */
    $.xget(xn.url('list-1'), function (code, message) {
        /*console.log(code);*/
        if (0 == code) {
            /*
            返回的数据,直接JS for遍历 或 JQ 渲染页面 $.each 遍历,复制列表最后一个元素,进行替换即可,wellcms 非前端渲染可达到 0.00x秒打开页面,前端渲染只会更出色。
            console.log(message);
            */
        } else if (1 == code) {

        } else {
            /* 弹窗提示错误,根据需要可删除此处或修改message.htm */
            $.alert(message);
        }
    });
</script>

API GET 返回 Object 格式数据:

{
    "forum": {
        "fid": "1",
        "fup": "0",
        "son": "0",
        "type": "1",
        "model": "0",
        "category": "0",
        "name": "新闻",
        "rank": "0",
        "threads": "2",
        "tops": "7",
        "todayposts": "0",
        "todaythreads": "1",
        "accesson": "0",
        "orderby": "0",
        "icon": "0",
        "display": "1",
        "nav_display": "1",
        "index_new": "10",
        "channel_new": "10",
        "comment": "2",
        "pagesize": "20",
        "publish": "0",
        "flags": "1",
        "create_date": "1616507053",
        "flagstr": "1",
        "thumbnail": {
            "width": 170,
            "height": 113
        },
        "moduids": "",
        "seo_title": "",
        "seo_keywords": "",
        "brief": "",
        "announcement": "",
        "well_publish": "0",
        "create_date_fmt": "2021-3-23",
        "icon_url": "view/img/forum.png",
        "accesslist": [],
        "modlist": [],
        "flagstr_fmt": {
            "1": {
                "flagid": "1",
                "name": "节点",
                "fid": "1",
                "rank": "0",
                "number": "10",
                "count": "0",
                "icon": "0",
                "display": "1",
                "create_date": "1612705005",
                "i": 1,
                "forum_name": "新闻",
                "display_fmt": "是",
                "forum_url": "list-1.html",
                "url": "flag-1.html",
                "create_date_fmt": "2021-02-07",
                "icon_fmt": "view/img/nopic.png"
            }
        },
        "url": "list-1.html"
    },
    "page": 1,
    "num": "2",
    "arrlist": {
        "threadlist": {
            "9": {
                "tid": "9",
                "fid": "1",
                "type": "0",
                "sticky": "0",
                "uid": "1",
                "icon": "0",
                "create_date": "1616642455",
                "views": "0",
                "posts": "0",
                "images": "0",
                "files": "0",
                "mods": "0",
                "status": "0",
                "closed": "0",
                "lastuid": 0,
                "last_date": "1616642455",
                "attach_on": "0",
                "flags": "0",
                "subject": "书中自有黄金屋,书中自有颜如玉",
                "tag": "",
                "brief": "",
                "keyword": "",
                "description": "",
                "image_url": "",
                "well_topicid": "0",
                "well_last_time": "0",
                "well_downloads": "0",
                "i": 1,
                "create_date_fmt": "8分钟前",
                "last_date_fmt": "",
                "create_date_fmt_ymd": "2021-03-25",
                "last_date_fmt_ymd": "2021-03-25",
                "username": "admin",
                "user_avatar_url": "view/img/avatar.png",
                "user": {
                    "uid": "1",
                    "gid": "1",
                    "username": "admin",
                    "articles": "1",
                    "comments": "48",
                    "avatar": "0",
                    "well_comment_likes": "0",
                    "well_publishs": "0",
                    "well_publish_latest": "0",
                    "well_comments": "0",
                    "well_comment_latest": "0",
                    "well_verify_threads": "0",
                    "well_verify_comments": "0",
                    "well_publish_rejects": "0",
                    "well_topics": "0",
                    "well_topic_manages": "0",
                    "well_topic_subscribes": "0",
                    "well_topic_joins": "0",
                    "well_softwares": "0",
                    "well_downloads": "0",
                    "groupname": "管理员组",
                    "avatar_url": "view/img/avatar.png",
                    "online_status": 1
                },
                "forum_name": "新闻",
                "forum_url": "list-1.html",
                "lastusername": "",
                "url": "read-9.html",
                "user_url": "user-1.html",
                "sticky_class": "",
                "icon_fmt": "view/img/nopic.png",
                "pages": 0,
                "tag_fmt": "",
                "allowupdate": true,
                "allowdelete": true,
                "allowtop": true,
                "topic_title": "",
                "topic_url": ""
            }
        },
        "flaglist": {
            "1": {
                "flagid": "1",
                "name": "节点",
                "fid": "1",
                "rank": "0",
                "number": "10",
                "count": "0",
                "icon": "0",
                "display": "1",
                "create_date": "1612705005",
                "i": 1,
                "forum_name": "新闻",
                "display_fmt": "是",
                "forum_url": "list-1.html",
                "url": "flag-1.html",
                "create_date_fmt": "2021-02-07",
                "icon_fmt": "view/img/nopic.png"
            }
        }
    },
    "extra": {
        "fid": 1
    },
    "header": {
        "title": "新闻-Website",
        "mobile_title": "",
        "mobile_link": "list-1.html",
        "keywords": "新闻",
        "description": "",
        "navs": []
    },
    "active": "default"
}
0