request.js
4.1 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* @Descripttion: 定义一个Request类,用于封装请求高德地图API的方法
* @version: 1.0.0
* @Author: limin
* @Date: 2023-07-04 17:25:31
* @LastEditors: limin
* @LastEditTime: 2023-07-05 08:48:04
* @FilePath: \glsx-map-sdk\src\request.js
*/
import md5 from 'md5'
import qs from 'qs'
import { MAP_OPTIONS } from './constants'
import { isMiniprogram } from './helper'
class Request {
// 构造函数,接收key和secret作为参数,如果没有传入,则使用默认值
constructor(key = MAP_OPTIONS.KEY, secret = MAP_OPTIONS.SECRET) {
// 将key和secret赋值给实例属性
this.key = key
this.secret = secret
}
/**对参数进行排序:按参数名升序 */
setLocaleCompare(params) {
// 使用Object.assign和展开运算符,将key添加到参数对象中,并返回一个新的对象
return Object.fromEntries(Object.entries(Object.assign({ key: this.key }, params)).sort(([a], [b]) => a.localeCompare(b)))
}
/**签名计算(sig) */
getSign(params) {
return md5(decodeURIComponent(qs.stringify(this.setLocaleCompare(params))) + this.secret)
}
/**封装的GET方法 */
get(method, data) {
// 调用request方法,传入请求方式为GET,请求路径为method,请求数据为data
return this.request('GET', method, data)
}
/**定义一个异步方法,用于发送请求到高德地图API */
async request(RequestMethods, method, data = {}) {
// 调用getSign方法,传入请求数据和请求路径,得到签名
const sig = this.getSign(data, method),
// 将签名添加到请求数据中,并对请求数据进行排序
obj = {
...this.setLocaleCompare(data),
sig
}
// 判断是否是小程序环境
if (isMiniprogram) {
// 显示导航栏加载动画
wx.showNavigationBarLoading()
try {
// 使用Promise封装wx.request方法,返回一个异步结果
const res = await new Promise((resolve, reject) => {
wx.request({
url: `${MAP_OPTIONS.domain}${method}`,
// 设置请求数据
data: obj,
// 设置请求方式
method: RequestMethods,
// 设置请求头,如果是POST请求,则设置content-type为application/json
header:
RequestMethods === 'POST'
? {
'content-type': 'application/json'
}
: {},
dataType: 'json',
responseType: 'text',
success: resolve,
fail: reject
})
})
// 解构响应结果中的状态码,数据和错误信息
const { statusCode, data, errMsg } = res
if (statusCode == '200') {
if (data.status == '1') {
return data
} else {
wx.showToast({
title: data.info,
icon: 'none'
})
}
} else {
wx.showToast({
title: errMsg,
icon: 'error'
})
}
} catch (error) {
wx.hideLoading()
console.error(`接口异常:`, error)
wx.showToast({
title: '系统异常',
icon: 'error'
})
throw error
} finally {
// 最后,隐藏导航栏加载动画
wx.hideNavigationBarLoading()
}
} else {
// 如果不是小程序环境,则使用window.fetch方法发送请求,并传入完整的请求地址和请求方式
try {
// 使用qs.stringify将请求数据转换为查询字符串,并使用encodeURIComponent进行编码,然后拼接到请求地址后面
const url = `${MAP_OPTIONS.domain}${method}?${encodeURIComponent(qs.stringify(obj))}`
// 使用await等待异步结果,并解构响应对象
const res = await window.fetch(url, {
method: RequestMethods
})
const data = await res.json()
if (data.status == '1') return data
else throw data.info
} catch (error) {
console.error(`接口异常:`, error)
throw error
}
}
}
}
export default Request