微信小程序----编写后台服务接口配置文件

编写后台服务接口配置文件

const basePath = 'http://test.cdbhbc.com/wbgapi'; 
const urlList = {
  // 刷新token
  refreshTokeUrl: basePath + '/refreshToke',//token
  // 登录和首页轮播
  loginUrl: basePath + '/wxappLogin',//登录
  advertPicListUrl: basePath + '/advertPicList',//轮播列表
  // 网吧
  shopSortListUrl: basePath + '/shopSortList',//网吧分类列表
  shopListUrl: basePath + '/shopList',//网吧列表
  shopDetailUrl: basePath + '/shopDetail',//网吧详情
  // 商品
  goodsSortListUrl: basePath + '/goodsSortList',//商品分类列表
  goodsListUrl: basePath + '/goodsList',//商品列表
  goodsDetailUrl: basePath + '/goodsDetail',//商品详情
  // 订单
  orderListUrl: basePath + '/orderList',//订单列表
  orderDetailUrl: basePath + '/orderDetail',//订单详情
  payBeforeInfoUrl: basePath + '/payBeforeInfo',//订单支付前信息
  submitOrderUrl: basePath + '/submitOrder',//提交订单
  // 购物车
  emptyShopCartUrl: basePath + '/emptyShopCart',//清空购物车
  addShopCartUrl: basePath + '/addShopCart',//添加到购物车
  shopCartListUrl: basePath + '/shopCartList',//购物车列表
  // 修改商品数量
  shopCartBuyCountOperateUrl: basePath + '/shopCartBuyCountOperate',//修改商品数量
  // 支付
  wxPayUrl: basePath + '/wxPay/payConfig',//支付信息接口
  // 特殊商品网费
  goodsSpecialDetailUrl: basePath + '/goodsSpecialDetail',//网费充值列表
  submitSpecialOrderUrl: basePath + '/submitSpecialOrder',//网费充值提交
  // 我的网吧
  shopMyListUrl: basePath + '/shopMyList',//我的网吧列表
  shopMyAddUrl: basePath + '/shopMyAdd',//添加我的网吧
}
module.exports = urlList;

接口配置文件的使用

// 引入配置文件config
const urlList = require('../../utils/config.js');
// request请求的时候使用,实例:更新token接口的使用
App({
    refreshToke(){
        // 小程序登录接口获取code
        wx.login({
            success: res => {
                // console.log(res)
                if(res.errMsg == 'login:ok'){
                    //调用refreshTokeUrl接口刷新token
                    wx.request({
                        url: urlList.refreshTokeUrl,
                        data: { code: res.code },
                        success: res => {
                            // console.log(res)
                            if(res.data.state == 'true'){
                                // token本地存储并且进行全局变量的赋值
                                wx.setStorageSync('token', res.data.data.token);
                                this.globalData.token = res.data.data.token;
                            }
                        }
                    })
                }
            }
        })
    }
})

接口配置文件的作用

  1. 在项目开发时能够在固定位置很快查到需要的接口;
  2. 服务器地址改变时,只用替换 basePath 的值,就能全部替换,不用去单独页面替换;
  3. 方便接口的集中管理,方便后期的维护。

DEMO下载

我的博客,欢迎交流!

我的CSDN博客,欢迎交流!

微信小程序专栏

前端笔记专栏

微信小程序实现部分高德地图功能的DEMO下载

微信小程序实现MUI的部分效果的DEMO下载

微信小程序实现MUI的GIT项目地址

微信小程序实例列表

前端笔记列表

游戏列表

原文地址:https://www.cnblogs.com/linewman/p/9918442.html