Blame view

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