riku
2024-11-07 5a678cce1b157411f20fbddfaed49c7bc8d9fba7
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
import { basePicUrl, baseUrl, baseFileUrl, baseIconUrl, mode } from '../config/index';
 
function request(fun, hostUrl) {
  const bUrl = hostUrl ? hostUrl : baseUrl;
  if (fun.params != undefined) {
    var param = '';
    Object.keys(fun.params).forEach(key => {
      var value = fun.params[key];
      if (param == '') {
        param += key + '=' + value;
      } else {
        param += '&' + key + '=' + value;
      }
    });
    var url = fun.url;
    fun.url = bUrl + url + '?' + param;
  } else {
    var url = fun.url;
    fun.url = bUrl + url;
  }
 
  return new Promise((resolve, reject) => {
    wx.request({
      url: fun.url,
      data: fun.data,
      method: fun.method,
      success(res) {
        if (mode == 'debug') {
          console.log(
            '|------------------------------------------------------------------------------------------------------------',
          );
          console.log('|--访问: ', fun.url);
          console.log('|--数据: ', fun.data);
          console.log('|--结果: ', res);
        }
        if (res.statusCode == 200) {
          resolve(res);
        } else {
          reject(res.statusCode);
        }
      },
      fail(err) {
        if (mode == 'debug') {
          console.log('--------------请求错误----------------' + fun.url);
          console.log(err);
        }
        // wx.showToast({
        //   title: '请求失败',
        //   icon: 'none',
        //   duration: 2000,
        // });
        reject(err);
      },
    });
  });
}
 
export function get(fun, hostUrl) {
  fun['method'] = 'GET';
  return request(fun, hostUrl);
}
export function post(fun, hostUrl) {
  fun['method'] = 'POST';
  return request(fun, hostUrl);
}
 
export function put(fun, hostUrl) {
  fun['method'] = 'PUT';
  return request(fun, hostUrl);
}