zoukankan      html  css  js  c++  java
  • 使用PbootCMS做微信小程序

    如何用PbootCMS做微信小程序呢,看完这篇文章你也可以学会。

    第一步:准备md5加密文件

    PbootCMS的签名参数signature是经过双层md5加密的,当然如果后台WebAPI没有开启强制认证的,这一步可以跳过。 

    第二步:封装API请求方法

    //声明一个API请求函数
    function api_request(url, method, parm){
        var apiurl    = 'https://www.pbootcms.com/api.php';    //域名 + api.php
        var appid     = 'wx12345678910';    //后台WebAPI中设置的AppID
        var appsecret = '12334356465765867878943';    //后台WebAPI中设置的API认证密钥
        var timestamp = Date.parse(new Date());
            timestamp = timestamp / 1000;    //获取当前时间戳
        var signature = md5.hex_md5(md5.hex_md5(appid + appsecret + timestamp));    //构建signature
        var data = {    //因为搜索、留言和表单需要用到POST方法,要通过data传递参数,所以先声明data对象,方便后面添加
            appid: appid,
            timestamp: timestamp,
            signature: signature,
        };
        //根据请求方法构建Header
        if (method == 'POST') {
            //POST方法,设置请求头部
            var header = { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' };
            //添加POST参数到data
            for (var i in parm) {
                data[i] = parm[i];
            }
        } else {
            //GET方法,设置请求头部
            method = 'GET';
            var header = { 'Content-Type': 'application/json' };
        }
        //通过promise返回数据
        var promise = new Promise(function(resolve,reject){
            //小程序自带的wx.request请求
            wx.request({
                method: method,
                url: apiurl + url,
                header: header,
                data: data,
                //请求成功
                success: function(res){
                    var api_data = res.data;
                    if (api_data.code != 1 && api_data.code != 0){
                        reject({ error: '数据请求失败,请稍后重试', code: 500 });
                        return;
                    } else {
                        resolve(api_data);
                    }
                } 
            })
        })
        //返回数据
        return promise;
    }

    第三步:封装官方API模块

    //官方Api模块进行封装
    module.exports = {
        //获取公司信息
        companyInfos: function(){
            var url = '/cms/company';
            return api_request(url);
        },
        //获取单个公司信息
        companyInfo: function(name){
            var url = '/cms/company/name/' + name;
            return api_request(url);
        },
        //获取网站信息
        siteInfos: function(){
            var url = '/cms/site';
            return api_request(url);
        },
        //获取单个网站信息
        siteInfo: function(name){
            var url = '/cms/site/name/' + name;
            return api_request(name);
        },
        //获取自定义标签
        getLabels: function(){
            var url = '/cms/label';
            return api_request(url);
        },
        //获取单个自定义标签
        getLabel: function(){
            var url = '/cms/label/name/' + name;
            return api_request(url);
        },
        //获取幻灯片
        getSlide: function(gid,num){
            var url = '/cms/slide/gid/' + gid + '/num/' + num;
            return api_request(url);
        },
        //获取友情链接
        getLinks: function(gid,num){
            var url = '/cms/link/gid/' + gid + '/num/' + num;
            return api_request(url);
        },
        //获取栏目列表
        getNavs: function(){
            var url = '/cms/nav';
            return api_request(url);
        },
        //获取指定栏目列表
        getNav: function(scode){
            var url = '/cms/nav/scode/' + scode;
            return api_request(url);
        },
        //获取当前栏目
        getCurrentCat: function(scode){
            var url = '/cms/sort/scode/' + scode;
            return api_request(url);
        },
        //获取内容列表
        getList: function(scode,num,order){
            if( ( num != '' ) && !order ){
                var url = '/list/' + scode + '/num/' + num;
            } else if( !num && ( order != '' ) ){
                var url = '/list/' + scode + '/order/' + order;
            } else if ( (num != '') && (order != '') ){
                var url = '/list/' + scode + '/num/' + num + '/order/' + order;
            } else {
                var url = '/list/' + scode;
            }
            return api_request(url);
        },
        //获取内容列表第*页
        getListPaged: function (scode, paged, num, order) {
            if ((num != '') && (order == '')) {
                var url = '/list/' + scode + '/num/' + num + '/page/' + paged;
            } else if ((num == '') && (order != '')) {
                var url = '/list/' + scode + '/order/' + order + '/page/' + paged;
            } else if ((num != '') && (order != '')) {
                var url = '/list/' + scode + '/num/' + num + '/order/' + order + '/page/' + paged;
            } else {
                var url = '/list/' + scode + '/page/' + paged;
            }
            return api_request(url);
        },
        //获取指定内容
        getContent: function(id){
            var url = '/content/' + id;
            return api_request(url);
        },
        //获取指定内容
        getContent: function(scode){
            var url = '/about/' + scode;
            return api_request(url);
        },
        //获取指定内容多图
        getContent_pics: function(id){
            var url = '/cms/pics/id/' + id;
            return api_request(url);
        },
        //获取搜索结果
        getSearchResault: function(parm,num,order){ 
            if ((num != '') && (order == '')) {
                var url = '/cms/search/' + 'num/' + num;
            } else if ((num == '') && (order != '')) {
                var url = '/cms/search/' + 'oder/' + order;
            } else if ((num != '') && (order != '')) {
                var url = '/cms/search/num/' + num + '/order/' + order;
            } else {
                var url = '/cms/search/';
            }
            return api_request(url, 'POST', parm);
        },
        //提交留言
        addMessage: function(parm){
            var url = '/cms/addmsg';
            return api_request(url, 'POST', parm);
        },
        //获取留言内容
        getMessage: function(num){
            var url = '/cms/msg/num/' + num;
            return api_request(url);
        },
        //获取留言内容第*页
        getMessagePaged: function(num,paged){
            var url = '/cms/msg/num/' + num + '/page/' + paged;
            return api_request(url);
        },
        //提交表单
        addForm: function(fcode,parm){
            var url = '/cms/addform/fcode/' + fcode;
            return api_request(url, 'POST', parm);
        },
        //获取表单内容
        getForm: function(fcode){
            var url = '/cms/form/fcode/' + fcode;
            return api_request(url);
        },
        //获取表单内容第*页
        getFormPaged: function(fcode,num,paged){
            var url = '/cms/form/fcode/' + fcode + '/num/' + num + '/page/' + paged;
            return api_request(url);
        },
    }

    本文来源:小郭博客(AndyGuo) ,原文链接:http://www.d163.net/html/web2/40.html

    学无止境
  • 相关阅读:
    hdu2818 Building Block
    struct2面试准备
    Spring mvc 面试
    Spring 面试详解
    Java面试必备Springioc上
    redis高级命令4 持久化机制 、事务
    redis高级命令3哨兵模式
    redis高级命令2
    redis高级命令1
    redis基础二----操作set数据类型
  • 原文地址:https://www.cnblogs.com/andywenming/p/12671465.html
Copyright © 2011-2022 走看看