1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| const { develop, trial, release } = require('./url.js');
/** * GET请求封装 */ function get(url, headers = {}, data = {}) { return request(url, 'GET', headers, data) }
/** * POST请求封装 */ function post(url, headers = {}, data = {}) { return request(url, 'POST', headers, data) } /** * @description 上传文件 * @param {*} url 请求地址 * @param {*} data 参数 */ function upLoad(url, headers = {}, data = {}) { return uploadFile(url, 'POST', headers, data) }
/** */ function request(url, method = "GET", headers = {}, data = {}) { var contentType = method == 'GET' ? 'application/json' : 'application/x-www-form-urlencoded' wx.showLoading({ title: '努力加载中' }) return new Promise(function (resolve, reject) { wx.request({ url: develop.BASE_URL + url, data: data, method: method, header: { 'content-Type': contentType, 'token': wx.getStorageSync('token') }, success: function (res) { wx.hideLoading() // console.log(res) switch (res.statusCode) { case 200: //请求成功 resolve(res.data); break; default: //请求失败 reject(res.data) wx.clearStorage(); wx.navigateTo({ url: '/pages/login/login', }) break; } }, fail: function (err) { //服务器连接异常 wx.hideLoading() reject(err, "服务器连接异常,请检查网络再试") } }) }); } /** * 微信的uploadFile */ function uploadFile(url, method = "POST", data = {}) { return new Promise(function (resolve, reject) { wx.uploadFile({ url: develop.BASE_URL + url, filePath: data.filePath, file: data.filePath, name: 'file', header: { 'token': wx.getStorageSync('token'), }, formData: { "userNo": wx.getStorageSync('userno'), "organId": wx.getStorageSync('organId'), }, success: function (res) { // console.log(res) if (res.statusCode == 200) { try { var resString = res.data var res = JSON.parse(resString); var data = res.data; console.log(res.code); console.log(1); switch (res.code) { case 20000: // 请求成功处理 resolve(data); break; // case 20001: //参数为空 // case 20002: //数据验证错误 // case 20005: //没有可用的数据库 // case 20006: //该用户未登录 // case 20007: //上传视频不存在 // case 20008: //请确认文件完整性 // case 20009: //程序异常 case 20003: // 20003 //您没有权限访问,请获取系统权限 case 20004: // 20004 权限失效或token过期 wx.removeStorageSync('token') wx.removeStorageSync('userno') wx.navigateTo({ url: '/pages/login/login', }) reject(data) break; default: reject(data) break; } } catch (error) { reject("数据获取失败") } } else { reject("请求失败:", res) } }, fail: function (err) { //服务器连接异常 reject(err, "服务器连接异常,请检查网络再试") } }) }); }
module.exports = { request, get, post, upLoad, }
|