# WXYHNetworking **Repository Path**: liuyonghu/WXYHNetworking ## Basic Information - **Project Name**: WXYHNetworking - **Description**: 微信集成化网络框架 - **Primary Language**: JavaScript - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 14 - **Forks**: 0 - **Created**: 2019-01-18 - **Last Updated**: 2025-08-18 ## Categories & Tags **Categories**: weixin-dev **Tags**: wx ## README # 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); }); ``` 2. 回调风格 ``` 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}); ``` 2. 配置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; ``` 2. 配置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}); ```