123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- const md5 = require("./md5.js");
- const util = require("./util.js");
- /**
- * 网络请求对象
- */
- const net = {
- commonParams: () => {
- let systemInfo = util.getStorageSync("systemInfo");
- if (!systemInfo) {
- systemInfo = wx.getSystemInfoSync();
- if (!systemInfo) {
- wx.setStorageSync("systemInfo", systemInfo);
- }
- }
- let deviceId = "weapp";
- let systemVersion = "weapp";
- if (systemInfo) {
- let { brand, model, system } = systemInfo;
- if (brand || model) {
- deviceId = brand + model;
- }
- if (system && "string" === typeof system) {
- systemVersion = system;
- }
- }
- let params = {
- language: "cn", //语言参数
- deviceType: "weapp", //weapp 接口调用来源参数
- deviceId: deviceId, //设备唯一标识码
- apiVersion: "1.0.0", //api版本号
- systemVersion: systemVersion, //操作系统及版本号
- };
- let loginUserInfo = util.getStorageSync("USERINFO");
- if (loginUserInfo) {
- let userToken = loginUserInfo.token;
- if (userToken) {
- params.token = userToken; //用户登录凭证token
- }
- }
- return params;
- },
- paramsSign: function (params, url) {
- /*参数签名*/
- let key = "d9ce9fe030776727e85442d033d1be84";
- if (url && url.indexOf("/common/") !== -1) {
- key = "b15681f74461af9642ab5356fba7962b";
- }
- let allKeys = [],
- paramsLower = {},
- sArr = [],
- res = "";
- for (let k in params) {
- let kLower = k;
- allKeys.push(kLower);
- paramsLower[kLower] = params[k];
- }
- allKeys.sort();
- allKeys.forEach((v, i, a) => {
- sArr.push(`${v}=${paramsLower[v]}`);
- });
- sArr.push(`key=${key}`);
- res = sArr.join("&");
- res = md5(res);
- return res.toLowerCase();
- },
- //isDealFail是否手动处理异常,true or false
- req: function (options, isDealFail) {
- //get或post请求
- const params = {
- ...options,
- };
- if (!params.url) {
- wx.showToast({
- title: "请设置请求url",
- icon: "none",
- duration: 2000,
- });
- return Promise.reject();
- }
- // 过滤空值
- for (const key in params.data) {
- if (util.isUndef(params.data[key])) {
- delete params.data[key];
- }
- }
- return new Promise((resolve, reject) => {
- params.data = Object.assign({}, net.commonParams(), params.data);
- params.data.signToken = net.paramsSign(params.data, params.url);
- wx.request({
- url: params.url,
- data: params.data || null,
- header: {
- "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
- },
- method: params.method || "POST",
- dataType: params.dataType || "json",
- success: function (res) {
- const { code, msg, data } = res.data;
- if (0 === code) {
- //ok
- return resolve(data);
- } else if (3 === code) {
- //未登录
- wx.clearStorage();
- wx.navigateTo({
- url: "/pages/login/login",
- });
- return reject(res.data);
- } else {
- if (isDealFail !== true && msg) {
- wx.showToast({
- title: msg || "",
- icon: "none",
- duration: 2000,
- });
- }
- return reject(res.data);
- }
- },
- fail: function (e) {
- if (isDealFail !== true) {
- wx.showToast({
- title: (e && e.errMsg) || "",
- icon: "none",
- duration: 2000,
- });
- }
- reject(e);
- },
- complete: function (res) {},
- });
- });
- },
- uploadFile: function (params, isDealFail) {
- //get或post请求
- if (!params) {
- wx.showToast({
- title: "请设置请求参数",
- icon: "none",
- duration: 2000,
- });
- return new Promise((resolve, reject) => {
- reject();
- });
- }
- if (!params.url) {
- wx.showToast({
- title: "请设置请求url",
- icon: "none",
- duration: 2000,
- });
- return new Promise((resolve, reject) => {
- reject();
- });
- }
- if (!params.filePath || !params.filePath.length) {
- wx.showToast({
- title: "请设置文件资源的路径",
- icon: "none",
- duration: 2000,
- });
- return new Promise((resolve, reject) => {
- reject();
- });
- }
- let promise = new Promise((resolve, reject) => {
- params.data = Object.assign({}, net.commonParams(), params.data);
- params.data.signToken = net.paramsSign(params.data, params.url);
- wx.uploadFile({
- url: params.url,
- filePath: params.filePath,
- name: "files[]",
- formData: params.data,
- header: {
- "content-type": "multipart/form-data",
- },
- method: "POST",
- success: function (res) {
- let data = res.data;
- if (typeof data == "string" && data.constructor == String) {
- data = JSON.parse(data);
- }
- let { code, msg } = data;
- if (0 === parseInt(code)) {
- //ok
- resolve(data);
- } else if (3 === code) {
- //未登录
- //未登录
- wx.clearStorage();
- wx.navigateTo({
- url: "/pages/login/login",
- });
- return reject(res.data);
- } else {
- if (isDealFail) {
- reject(data);
- } else {
- wx.showToast({
- title: msg || "error",
- icon: "none",
- duration: 2000,
- });
- }
- }
- },
- fail: function (e) {
- if (isDealFail) {
- reject(e);
- } else {
- wx.showToast({
- title: (e && e.errMsg) || "",
- icon: "none",
- duration: 2000,
- });
- }
- },
- complete: function (res) {},
- });
- });
- return promise;
- },
- };
- module.exports = net;
|