Blame view

common/requestServer.js 1.63 KB
4b045f7c   刘淇   江阴初始化项目
1
  import common from "./common.js";
e133a83d   chenbiao   add 接口文档更新
2
  export const myRequest = (options) => {
e6c7d1db   刘淇   登陆
3
4
5
6
7
8
9
10
11
12
13
14
15
    // 调接口加载
    uni.showLoading({
      title: "加载中",
      mask: true,
    });
    return new Promise((resolve, reject) => {
      uni.request({
        url: options.url,
        //默认参数
        data: options.data || {},
        // 配置请求头参数-例如token
        header: {
          'content-type': 'application/json',
e6c7d1db   刘淇   登陆
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
          // Accept: 'application/json',
          // 'Content-Type': 'application/json',
          // 'X-Requested-With': 'XMLHttpRequest'
        },
        dataType: "json",
        method: options.method || 'GET',
        // sslVerify: true,
        // 接口请求成功
        success: (res) => {
          // 关闭加载
          uni.hideLoading();
          // 调用成功且有数据 返回数据  组件内通过 .then() 或者async await 接受异步返回数据
          //resolve(res.data)
          //在接口200 调用成功后 才能进行判断接口内的状态码 return_code 以此判定作何操作和提示
          let result = res.data
          console.log(result);
f7e9351e   chenbiao   add 接口文档更新
32
         
e6c7d1db   刘淇   登陆
33
34
35
36
37
          if (result.code == 0) {
            resolve(res.data)
          } else {
            uni.hideLoading();
            uni.showToast({
f7e9351e   chenbiao   add 接口文档更新
38
39
              title: result.message,
              icon: 'error',
e6c7d1db   刘淇   登陆
40
41
42
              duration: 2000
            });
          }
f7e9351e   chenbiao   add 接口文档更新
43
         
e6c7d1db   刘淇   登陆
44
45
46
47
48
        },
        // 接口接口失败
        fail: (error) => {
          // 关闭加载
          uni.hideLoading();
f7e9351e   chenbiao   add 接口文档更新
49
          console.log("请求失败", error);
e6c7d1db   刘淇   登陆
50
          uni.showToast({
f7e9351e   chenbiao   add 接口文档更新
51
52
53
            title: error.message,
            icon: 'error',
            duration: 2000
e133a83d   chenbiao   add 接口文档更新
54
          })
e6c7d1db   刘淇   登陆
55
56
57
          // 失败数据
          reject(error)
        }
e133a83d   chenbiao   add 接口文档更新
58
      })
e6c7d1db   刘淇   登陆
59
    })
e133a83d   chenbiao   add 接口文档更新
60
  }