2 Star 1 Fork 0

Coder / WXYHNetworking

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 3.67 KB
一键复制 编辑 原始数据 按行查看 历史
刘勇虎 提交于 2021-08-12 17:09 . update

WXYHNetworking

介绍

微信集成化网络框架

软件架构

软件架构说明

安装教程

  1. git submodule init
  2. git submodule add https://gitee.com/liuyonghu/WXYHNetworking.git

使用说明

  1. promise 风格
    netTaskByConf({
        host:"https://wenwen.sogou.com/z/q721011130.htm",
        method:"GET"
        }).then((res, fail) => {
         console.log(res);
         console.error(fail);
    });
  1. 回调风格
    netTaskByConf({
        host:"https://wenwen.sogou.com/z/q721011130.htm",
        method:"GET",
        success:(res) => {
         console.log(res);

        },
        error:(fail)=>{
         console.error(fail);
     }});

API 参数 说明

注意 : 标 * 为必填参数

普通请求:


   {
        config:{
            host:       "* string (服务器地址)",
            method:     "* string (接口方式)",
            headers:    "object headers",
            sync:       "bool (同名请求,是否同步)",
            path:       "string (接口路径)",
            port:       "number  (接口端口)"
        },
        data:           "参数",
        timeout:        "number (超时时间,默认5000)",
        resInterceptor: "function 响应拦截 ",
        requestInterceptor: "function 请求拦截 "
   }

文件上传:

    {
        config:{
            host:       "* string (服务器地址)",
            headers:    "headers",
            sync:       "bool (同名请求,是否同步)",
            path:       "string (接口路径)",
            port:       "number  (接口端口)"
        },
        filePath:       "* string 需要上传的文件路径",
        name:           "* string 文件对应的 key,开发者在服务端可以通过这个 key 获取文件的二进制内容",
        data:           "object 额外的参数",
        timeout:        "number (超时时间,默认5000)",
        resInterceptor: "function 响应拦截 ",
        requestInterceptor: "function 请求拦截 "
   }

eg:

  1. 实例化工具
// request.js

const {WXYHNetworking} = require("./submodule/index");

const requestInterceptor = function(options){
    return options;
}
const resInterceptor = (res) => {
  return res;
};
module.exports = new WXYHNetworking({ resInterceptor ,requestInterceptor});
  1. 配置api文件

const host = getApp().config.host;
const port = getApp().config.port;
const netWork = require("../utils/request");

module.exports.getPersonDetail = function (params) {
  return netWork.netTaskByConf({
    params,
    config: {
      host,
      path: "personnel/person/(id)",
      port,
      method: "get",
    },
    timeout: 8000,
  });
};

云开发请求工具

eg:

  1. 实例化工具
// request.js

const {env} = require("./envList");
const traceUser = true;
const {YHCNetWoking} = require("./submodule/index");
const mrequest = new YHCNetWoking({env,traceUser});
module.exports = mrequest;
  1. 配置api文件
// api.js
const net = require("../request");

module.exports.login = function (data) {
    return net.request({
        name: "user",
        data: {
            type: "login",
            ...data
        }
    })
}

进阶

配置拦截器

// request.js

const requestInterceptor = function(options){
    wx.showLoading({
      title: 'none'
    })
    return options;
}

const resInterceptor = function(res){
    const {result} = res;
    const {code,msg} = result;
    if (code != 200) {
        wx.showToast({
          title: msg,
          icon:"error"
        })
    }
    wx.hideLoading()
    return result;
}
const mrequest = new YHCNetWoking({env,traceUser,resInterceptor,requestInterceptor});
JavaScript
1
https://gitee.com/liuyonghu/WXYHNetworking.git
git@gitee.com:liuyonghu/WXYHNetworking.git
liuyonghu
WXYHNetworking
WXYHNetworking
master

搜索帮助