123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- import { Config } from "./config.js";
- import { Token } from "./token.js";
- class Base {
- constructor() {
- this.baseRequestUrl = Config.userUrl;
- this.serviceRequestUrl = Config.serviceUrl;
- this.jgRequestUrl = Config.jgUrl;
- }
-
-
- // 文件上传
- uploadFileByServer(params, noRefetch,name = "image") {
- var that = this;
- var url = this.serviceRequestUrl + params.url;
- if (!params.type) {
- params.type = "GET";
- }
- if (params.queryString) {
- params.queryString = params.queryString;
- } else {
- params.queryString = "";
- }
- uni.uploadFile({
- url: url + params.queryString,
- filePath: params.filePath,
- name: name,
- header: {
- "Authorization": uni.getStorageSync("tokenHead") + uni.getStorageSync("token"),
- "content-type": "application/json"
- },
- success: function(res) {
- if (res.data.code == "401") {
- //token过期
- if (!noRefetch) {
- that._refetch(params);
- }
- }
- //根据http状态码判断本次http请求调用是否成功
- var code = res.statusCode.toString();
- var startChar = code.charAt(0);
-
- if (startChar == "2") { //http调用是成功的
- params.sCallBack && params.sCallBack(res);
- } else {
- //处理请求错误 原因:token可能是失效的
-
- //但是这种情况只能发生一次
- if (code == "500") { //因为token失效服务器返回的状态码
- //1.获取新的令牌 2.获取完成以后继续调用本接口 3.只能请求一次 不能反复校验
- if (!noRefetch) {
- that._refetch(params);
- }
- }
- params.sCallBack && params.sCallBack(res);
- }
- },
- fail: function(err) {
- //网络中断或者网络异常导致的错误
- console.log(err);
- console.log("服务器走神了...");
- }
- });
- }
-
- // 文件上传
- uploadFile(params, noRefetch) {
- var that = this;
- var url = this.baseRequestUrl + params.url;
- if (!params.type) {
- params.type = "GET";
- }
- if (params.queryString) {
- params.queryString = params.queryString;
- } else {
- params.queryString = "";
- }
- uni.uploadFile({
- url: url + params.queryString,
- filePath: params.filePath,
- name: "photo",
- header: {
- "Authorization": uni.getStorageSync("tokenHead") + uni.getStorageSync("token"),
- "content-type": "application/json"
- },
- success: function(res) {
- if (res.data.code == "401") {
- //token过期
- if (!noRefetch) {
- that._refetch(params);
- }
- }
- //根据http状态码判断本次http请求调用是否成功
- var code = res.statusCode.toString();
- var startChar = code.charAt(0);
- if (startChar == "2") { //http调用是成功的
- params.sCallBack && params.sCallBack(res);
- } else {
- //处理请求错误 原因:token可能是失效的
- //但是这种情况只能发生一次
- if (code == "500") { //因为token失效服务器返回的状态码
- //1.获取新的令牌 2.获取完成以后继续调用本接口 3.只能请求一次 不能反复校验
- if (!noRefetch) {
- that._refetch(params);
- }
- }
- params.sCallBack && params.sCallBack(res);
- }
- },
- fail: function(err) {
- //网络中断或者网络异常导致的错误
- console.log(err);
- console.log("服务器走神了...");
- // reject(err);
- }
- });
- }
- // 服务端请求
- serviceRequest(params, noRefetch) {
- var that = this;
- var url = params.url.includes("http") ? params.url : (this.serviceRequestUrl + params.url);
- if (!params.type) {
- params.type = "GET";
- }
- if (params.queryString) {
- params.queryString = params.queryString;
- } else {
- params.queryString = "";
- }
- uni.request({
- url: url + params.queryString,
- data: params.data,
- method: params.type,
- header: {
- "Authorization": uni.getStorageSync("tokenHead") + uni.getStorageSync("token"),
- "content-type": "application/json",
- ...params.header
- },
- success: function(res) {
- if (res.data.code == "401") {
- console.log("登录过期");
- // 过期跳转登录页
- wx.redirectTo({
- url: "/pages/login/index"
- });
- //token过期
- if (!noRefetch) {
- that._refetch(params);
- }
- }
- //根据http状态码判断本次http请求调用是否成功
- var code = res.statusCode.toString();
- var startChar = code.charAt(0);
- if (startChar == "2") { //http调用是成功的
- params.sCallBack && params.sCallBack(res);
- } else {
- if (code == "500") { //因为token失效服务器返回的状态码
- //1.获取新的令牌 2.获取完成以后继续调用本接口 3.只能请求一次 不能反复校验
- if (!noRefetch) {
- that._refetch(params);
- }
- }
- params.sCallBack && params.sCallBack(res);
- }
- },
- fail: function(err) {
- //网络中断或者网络异常导致的错误
- console.log(err);
- console.log("服务器走神了...");
- }
- });
- }
- //用户端请求
- //当noRefetch为true时,不做未授权重制机制
- request(params, noRefetch) {
- var that = this;
- var url = params.url.includes("http") ? params.url : (this.baseRequestUrl + params.url);
- if (!params.type) {
- params.type = "GET";
- }
- if (params.queryString) {
- params.queryString = params.queryString;
- } else {
- params.queryString = "";
- }
- uni.request({
- url: url + params.queryString,
- data: params.data,
- method: params.type,
- header: {
- "Authorization": uni.getStorageSync("tokenHead") + uni.getStorageSync("token"),
- "content-type": "application/json",
- ...params.header
- },
- success: function(res) {
- if (res.data.code == "401") {
- // 过期跳转登录页
- wx.redirectTo({
- url: "pages/login/index"
- });
- //token过期
- if (!noRefetch) {
- that._refetch(params);
- }
- }
- //根据http状态码判断本次http请求调用是否成功
- var code = res.statusCode.toString();
- var startChar = code.charAt(0);
- if (startChar == "2") { //http调用是成功的
- params.sCallBack && params.sCallBack(res);
- } else {
- if (code == "500") { //因为token失效服务器返回的状态码
- //1.获取新的令牌 2.获取完成以后继续调用本接口 3.只能请求一次 不能反复校验
- if (!noRefetch) {
- that._refetch(params);
- }
- }
- params.sCallBack && params.sCallBack(res);
- }
- },
- fail: function(err) {
- //网络中断或者网络异常导致的错误
- console.log(err);
- console.log("服务器走神了...");
- }
- });
- }
- jgRequest(params, noRefetch) {
- var that = this;
- var url = params.url.includes("http") ? params.url : (this.jgRequestUrl + params.url);
- if (!params.type) {
- params.type = "GET";
- }
- if (params.queryString) {
- params.queryString = params.queryString;
- } else {
- params.queryString = "";
- }
- uni.request({
- url: url + params.queryString,
- data: params.data,
- method: params.type,
- header: {
- "Authorization": uni.getStorageSync("tokenHead") + uni.getStorageSync("token"),
- "content-type": "application/json",
- ...params.header
- },
- success: function(res) {
- if (res.data.code == "401") {
- //token过期
- if (!noRefetch) {
- that._refetch(params);
- }
- }
- //根据http状态码判断本次http请求调用是否成功
- var code = res.statusCode.toString();
- var startChar = code.charAt(0);
- if (startChar == "2") { //http调用是成功的
- params.sCallBack && params.sCallBack(res.data);
- } else {
- if (code == "500") { //因为token失效服务器返回的状态码
- //1.获取新的令牌 2.获取完成以后继续调用本接口 3.只能请求一次 不能反复校验
- if (!noRefetch) {
- that._refetch(params);
- }
- }
- params.sCallBack && params.sCallBack(res.data);
- }
- },
- fail: function(err) {
- //网络中断或者网络异常导致的错误
- console.log(err);
- console.log("服务器走神了...");
- }
- });
- }
- //token失效的时候,重新刷一下token
- _refetch(params) {
- var token = new Token();
- token.getTokenFromServer((token) => {
- this.request(params, true);
- });
- }
- }
- export { Base };
|