Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Development configuration weight

Json ->h5->devServer in uni-app actually corresponds to devServer in Webpack. Json ->h5->devServer node. Funciton and other complex types are not supported at present.

Page routing mode is returned depending on the backend configuration

{
  "h5": {
  "router": {
    "mode":"history" // hash uses the hash of a URL to simulate a full URL, so that the page does not reload when the URL changes. || history
  },
  "devServer": {  
    "https": false.// Whether to use HTTPS
    "host": "0.0.0.0"."hotOnly": true."port": 8000."disableHostCheck": true."proxy": {  
      "/api": {  
        "target": "https://xxx.xxx.cn".// Address of the API server
        "changeOrigin": true."secure": true.// Set a proxy that supports HTTPS
        "ws": true./ / agency web sockets
        "pathRewrite": {  // Rewrite paths such as '/ API /user/info' to '/user/info', target+pathRewrite
          "^/api": ""}}}}},}Copy the code

Json proxy pathRewrite in the manifest.json proxy in the development environment replaces “/ API “with empty, and below is the request interface

let baseUrl = ""

// Set the interface address according to the environment variable
switch(process.env.NODE_ENV){
    case "development":
        baseUrl = '/api';
        break;
    case "production":
        baseUrl = 'xxx.xxx.com';
        break;
    default:
        baseUrl = "Other environments"
}

uni.requset({
    url: baseUrl+"/api/user/info"
    method:"post",})Copy the code

Promisic utility function for UNIApp

The uniApp interface uses the callback function as the return value, so here is a utility function that returns the success and fail of the UNIApp API as a promise

Promisic (wx.Request)({url:””}).then(res).catch(err); Let res = await promisic(wx.request)({url:””});

 /** Convert the applet callback to the promise form *@description: 
  * @param {Function} func
  * @return {Promise}* /	
function promisic (func) {
    return function (params = {}) {
        return new Promise((resolve, reject) = > {
            const args = Object.assign(params, {
                success: (res) = > {
                    resolve(res);
                },
                fail: (error) = >{ reject(error); }}); func(args); }); }; }Copy the code