| | |
| | | import Taro from '@tarojs/taro'; |
| | | import Qs from 'qs'; |
| | | import { Tools } from '@components/common/Tools'; |
| | | import { $hostBoot } from '@components/bases/HostBoot'; |
| | | import project from '@project'; |
| | | |
| | | export class Fetcher { |
| | |
| | | constructor(options = {}) { |
| | | this._data = { |
| | | urlPrefix: options.urlPrefix || ['/api/common/', '/api/common/'], |
| | | mock: Tools.getUrlParam('mock') || project.host.mock, |
| | | }; |
| | | if (this._data.mock === 'on') { |
| | | if (project.host.assetsPath.indexOf('..') === 0) { |
| | | this._defaultConfig.url = Fetcher.host + '/' + project.host.assetsPath.replace('/assets', '/mocks'); |
| | | } else { |
| | | this._defaultConfig.url = Fetcher.host + project.host.assetsPath.replace('/assets', '/mocks'); |
| | | } |
| | | } else { |
| | | this._defaultConfig.url = Fetcher.host; |
| | | // mock 模式转换地址 |
| | | if ($hostBoot.isOnMock()) { |
| | | this._defaultConfig.url += (project.host.assetsPath.indexOf('..') === 0 ? '/' : '') + |
| | | project.host.assetsPath.replace('/assets', '/mocks'); |
| | | } |
| | | // 正常模式 |
| | | else { |
| | | this._defaultConfig.url = ''; |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | spellURL(devSuffix, serSuffix) { |
| | | let url = ''; |
| | | // mock 模式 |
| | | if (this._data.mock === 'on') { |
| | | // mock地址 |
| | | if ($hostBoot.isOnMock()) { |
| | | url = this._data.urlPrefix[0].replace('api/', '') + devSuffix + '.json'; |
| | | } |
| | | // 强制实际请求模式 |
| | | else if (this._data.mock === 'real') { |
| | | url = this._data.urlPrefix[1] + (serSuffix || devSuffix); |
| | | } |
| | | // 正常模式 |
| | | // 正常地址 |
| | | else { |
| | | // 开发环境地址 |
| | | if (Fetcher.inDevMod) { |
| | | url = this._data.urlPrefix[0] + devSuffix; |
| | | } |
| | | // 生产环境地址 |
| | | else { |
| | | url = this._data.urlPrefix[1] + (serSuffix || devSuffix); |
| | | } |
| | | url = this._data.urlPrefix[1] + (serSuffix || devSuffix); |
| | | } |
| | | const fixReg = /[a-zA-Z0-9]+\/\.\.\//; |
| | | while (url.indexOf('../') >= 0) { |
| | |
| | | * @return {Promise<any>} |
| | | */ |
| | | post(url, data, options = {}) { |
| | | if (this._data.mock === 'on') { |
| | | // mock 模式转换为 get 请求 |
| | | if ($hostBoot.isOnMock()) { |
| | | return this.get(url, data, options); |
| | | } |
| | | const params = Qs.stringify(data); |
| | |
| | | if (process.env.TARO_ENV === 'weapp') { |
| | | header['Cookie'] = this._getCookies(); |
| | | } |
| | | // 如果指定了主机名,使用固定主机地址,否则使用默认地址 |
| | | let urlPrefix = ''; |
| | | if (options.hostName) { |
| | | urlPrefix = $hostBoot.getHost(options.hostName) + this._defaultConfig.url; |
| | | } else { |
| | | urlPrefix = $hostBoot.getHost() + this._defaultConfig.url; |
| | | } |
| | | Taro.request({ |
| | | ...this._defaultConfig, |
| | | header, |
| | | url: this._defaultConfig.url + url, |
| | | url: urlPrefix + url, |
| | | method: type.toUpperCase(), |
| | | data, |
| | | success: response => { |
| | |
| | | msg += '解析通讯数据异常!'; |
| | | } |
| | | setTimeout(() => { |
| | | this.message('fail', msg); |
| | | this._message('fail', msg); |
| | | }, 20); |
| | | } |
| | | |
| | |
| | | } else if (response.state.code === 2001) { |
| | | setTimeout(() => { |
| | | if (typeof options.silence === 'undefined' || !options.silence) { |
| | | this.message('info', response.state.msg); |
| | | this._message('info', response.state.msg); |
| | | } |
| | | }, 20); |
| | | return null; |
| | |
| | | } else { |
| | | setTimeout(() => { |
| | | if (typeof options.silence === 'undefined' || !options.silence) { |
| | | this.message('error', response.state.msg); |
| | | this._message('error', response.state.msg); |
| | | } |
| | | }, 20); |
| | | return null; |
| | |
| | | } |
| | | // 绝对路径 |
| | | if (/^(\/upload|\/static|\/mini|\/assets)/.test(path)) { |
| | | return Fetcher.host + path; |
| | | return $hostBoot.getHost() + path; |
| | | } |
| | | // 部分路径 |
| | | else { |
| | | return Fetcher.host + '/upload/' + path; |
| | | return $hostBoot.getHost() + '/upload/' + path; |
| | | } |
| | | } |
| | | // 裁剪多余部分 |
| | |
| | | * @param type |
| | | * @param msg |
| | | */ |
| | | message(type, msg) { |
| | | _message(type, msg) { |
| | | Taro.showToast({ |
| | | title: msg, |
| | | icon: 'none', |
| | | mask: true, |
| | | mask: false, |
| | | duration: type === 'fail' ? 3000 : 2000, |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 记录是否为本地开发模式 |
| | | * @type {Boolean} |
| | | */ |
| | | static inDevMod = (() => { |
| | | // 网页 |
| | | if (process.env.TARO_ENV === 'h5') { |
| | | // http协议访问 |
| | | if (window.location.protocol.indexOf('http') > 0) { |
| | | // 当内网 ip 且使用 33**/35** 的端口号时,视为本地开发模式 |
| | | return /^(192|127|localhost).*?:3[35]\d{2}$/i.test(window.location.host); |
| | | } |
| | | // 非http协议访问 |
| | | else { |
| | | // 开发编译 |
| | | if (process.env.NODE_ENV === 'development') { |
| | | return true; |
| | | } |
| | | // 生产编译 |
| | | else if (process.env.NODE_ENV === 'production') { |
| | | return false; |
| | | } |
| | | } |
| | | } |
| | | // 小程序 |
| | | else if (process.env.TARO_ENV === 'weapp') { |
| | | // 开发编译 |
| | | if (process.env.NODE_ENV === 'development') { |
| | | return true; |
| | | } |
| | | // 生产编译 |
| | | else if (process.env.NODE_ENV === 'production') { |
| | | return false; |
| | | } |
| | | } |
| | | })(); |
| | | |
| | | /** |
| | | * 当前服务器主机地址 |
| | | * @type {String} |
| | | */ |
| | | static host = (() => { |
| | | // 网页模式 |
| | | if (process.env.TARO_ENV === 'h5') { |
| | | // 如果网址参数有指定服务器 |
| | | const server = Tools.getUrlParam('server'); |
| | | if (server) { |
| | | // 如果是完整网址,使用网址对应的域名 |
| | | if (server.indexOf('http') >= 0) { |
| | | const portal = server.split('//')[0]; |
| | | const domain = server.split('//')[1].split('/')[0]; |
| | | return portal + '//' + domain; |
| | | } |
| | | // 如果有匹配服务器,使用指定的服务器地址 |
| | | if (typeof project.host.hosts[server] !== 'undefined') { |
| | | return project.host.hosts[server]; |
| | | } |
| | | // 否则使用本地 |
| | | else { |
| | | return project.host.hosts.lc; |
| | | } |
| | | } |
| | | // 网页域名提取服务器地址 |
| | | else if (window.location.protocol.indexOf('http') >= 0) { |
| | | return window.location.protocol + '//' + window.location.host; |
| | | } |
| | | // 既不指定server也不是域名访问,使用设置的服务器地址 |
| | | else { |
| | | // 开发 |
| | | if (Fetcher.inDevMod) { |
| | | return project.host.hosts[project.host.devType]; |
| | | } |
| | | // 生产 |
| | | else { |
| | | return project.host.hosts[project.host.serverType]; |
| | | } |
| | | } |
| | | } |
| | | // 小程序模式 |
| | | else if (process.env.TARO_ENV === 'weapp') { |
| | | // 开发 |
| | | if (Fetcher.inDevMod) { |
| | | return project.host.hosts[project.host.devType]; |
| | | } |
| | | // 生产 |
| | | else { |
| | | return project.host.hosts[project.host.serverType]; |
| | | } |
| | | } |
| | | })(); |
| | | |
| | | } |