123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import util from './util.js'
- class request {
- //本地的
- // static BASE_URL = 'http://www.lx.com:81/'
- // pre环境的
- // static BASE_URL = 'https://oapi.shpr.top/'
- // 正式的
- static BASE_URL = 'https://vapi.hsayi.com/'
- // @todo需要修改正式的域名
- static HEAD = {
- "Content-Type": "application/json",
- "SIGN":'',
- "NONCE":'',
- "TIMESTAMP":'',
- }
- static constructor() {
- }
- /**
- * 设置统一的异常处理
- */
- static setErrorHandler(handler) {
- this._errorHandler = handler
- }
- /**
- * GET类型的网络请求
- */
- static getRequest(url, data ) {
- let headerSign = this.getSignHead();
- return this.requestAll(url, data, headerSign, 'GET')
- }
-
- /**
- * POST类型的网络请求
- */
- static postRequest(url, data) {
- let headerSign = this.getSignHead();
- return this.requestAll(url, data, headerSign, 'POST')
- }
- static upload(url, name, path, ortherData, header = this.HEAD){
-
- return new Promise((resolve, reject) => {
- wx.uploadFile({
- url: url,
- header: header,
- filePath: path,
- name: name,
- formData: ortherData,
- success: (res => {
- res.data = JSON.parse(res.data);
- if (res.data.code === 200) {
- //200: 服务端业务处理正常结束
- resolve(res.data)
- } else {
- wx.showToast({
- title: res.data.msg,
- })
- //其它错误,提示用户错误信息
- if (this._errorHandler != null) {
- //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
- this._errorHandler(res)
- }
- reject(res)
- }
- }),
- fail: (res => {
- if (this._errorHandler != null) {
- this._errorHandler(res)
- }
- wx.showToast({
- title: '网络异常请,稍后再试~',
- })
- reject(res)
- })
- })
- })
- }
- static getSignHead(){
- let timestamp = parseInt(Date.now()/1000);
- let nonce = Math.floor(Math.random() * 1000).toString()
- let signStr = timestamp+nonce+""+"U6Watb875eCiX4Lq";
- let sign = util.sha1(signStr).toString();
-
- let signHeadInfo = {
- "SIGN":sign,
- "NONCE":nonce,
- "TIMESTAMP":timestamp,
- "Content-Type": "application/json"
- }
- return signHeadInfo;
- }
- /**
- * 网络请求
- */
- static requestAll(url, data, header, method) {
-
- wx.showLoading()
- return new Promise((resolve, reject) => {
- wx.request({
- url: url,
- data: data,
- header: header,
- method: method,
- success: (res => {
- wx.hideLoading()
- if (res?.data.code === 200) {
- //200: 服务端业务处理正常结束
- resolve(res?.data)
- } else {
- wx.showToast({
- title: res?.data?.msg,
- })
- //其它错误,提示用户错误信息
- if (this._errorHandler != null) {
- //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
- this._errorHandler(res)
- }
- reject(res)
- }
- }),
- fail: (res => {
- if (this._errorHandler != null) {
- this._errorHandler(res)
- }
- wx.showToast({
- title: '网络异常请稍后',
- })
- reject(res)
- })
- })
- })
- }
- }
- export default request
|