每日一弹:微信小程序+mockjs开发

写完再总结

1.    下载mock.js

git clone https://github.com/nuysoft/Mock

2.    配置文件

模块文件

3.api.js中写模拟数据和后台数据接口

1.    注意: 通过 DEBUG = true;//切换数据入口

let API_HOST = "http://xxx.com/xxx";

let DEBUG = true;//切换数据入口

var Mock = require('mock.js')

function ajax(data = '', fn, method = "get", header = {}) {

    if (!DEBUG) {

        wx.request({

            url: config.API_HOST + data,

            method: method ? method : 'get',

            data: {},

            header: header ? header : { "Content-Type": "application/json" },

            success: function (res) {

                fn(res);

            }

        });

    } else {

        // 模拟数据

        var res = Mock.mock({

            'error_code': '',

            'error_msg': '',

            'data|10': [{

                'id|+1': 1,

                'img': "@image('200x100', '#4A7BF7','#fff','pic')",

                'title': '@ctitle(3,8)',

                'city': "@county(true)",

                'stock_num': '@integer(0,100)',//库存数量 

                'marketing_start': '@datetime()',

                'marketing_stop': '@now()',

                'price': '@integer(100,2000)',//现价,单位:分 

                'original_price': '@integer(100,3000)'

            }] 

        })

        // 输出结果

      // console.log(JSON.stringify(res, null, 2))

        fn(res);

    }

}

module.exports = {

    ajax: ajax

}

你可能感兴趣的:(每日一弹:微信小程序+mockjs开发)