• Welcome to Journal web site.

我是 PHP 程序员

- 开发无止境 -

Next
Prev

微信小程序--网络通讯

Data: 2021-09-01 07:22:06Form: JournalClick: 0

# 微信小程序--网络通讯


# 一、服务器域名配置

  • 每个微信小程序需要事先设置通讯域名,小程序只可以跟指定的域名进行网络通信
  • 服务器域名请在 「小程序后台-开发-开发设置-服务器域名」 中进行配置
    • 域名只支持 https (wx.request、wx.uploadFile、wx.downloadFile) 和 wss (wx.connectSocket) 协议;
    • 域名不能使用 IP 地址(小程序的局域网 IP 除外)或 localhost;
    • 可以配置端口,如 https://myserver.com:8080,但是配置后只能向 https://myserver.com:8080 发起请求。如果向 https://myserver.com、https://myserver.com:9091 等 URL 请求则会失败。
    • 如果不配置端口。如 https://myserver.com,那么请求的 URL 中也不能包含端口,甚至是默认的 443 端口也不可以。如果向 https://myserver.com:443 请求则会失败。
    • 域名必须经过 ICP 备案
    • 出于安全考虑,api.weixin.qq.com 不能被配置为服务器域名,相关 API 也不能在小程序内调用。 开发者应将 AppSecret 保存到后台服务器中,通过服务器使用 getAccessToken 接口获取 access_token,并调用相关 API
    • 对于每个接口,分别可以配置最多 20 个域名

小程序管理后台1小程序管理后台2


# 二、接口

  • 聚合数据:https://www.juhe.cn/docs/api/id/73
    • 有很多第三方接口,供大家使用
    • 不同语言可以互通数据
  • 接口地址:http://apis.juhe.cn/simpleWeather/query
  • key:abc4b64ae7656b460723402175a5650b

请求参数

编号名称必填类型说明
1citystring要查询的城市名称/id,城市名称如:温州、上海、北京,需要 utf8 urlencode
2keytring在个人中心->我的数据,接口名称上方查看

返回参数

编号名称类型说明
1error_codeint返回码,0 为查询成功
2reasonstring返回说明
3resultstring返回结果集
----
4realtime-当前天气详情情况
5infostring天气情况,如:晴、多云
6widstring天气标识 id,可参考小接口 2
7temperaturestring温度,可能为空
8humiditystring湿度,可能为空
9directstring风向,可能为空
10powerstring风力,可能为空
11aqistring空气质量指数,可能为空
----
12future-近 5 天天气情况
13datestring日期
14temperaturestring温度,最低温/最高温
15weatherstring天气情况
16directstring风向

JSON 返回示例

{
	"error_code": 0,
	"reason": "查询成功",
	"result": {
		"city": "苏州",
		"realtime": {
			"info": "阴",
			"wid": "02",
			"temperature": "4",
			"humidity": "82",
			"direct": "西北风",
			"power": "3级",
			"aqi": "80"
		},
		"future": [
			{
				"date": "2019-02-22",
				"temperature": "1/7℃",
				"weather": "小雨转多云",
				"wid": {
					"day": "07",
					"night": "01"
				},
				"direct": "北风转西北风"
			},
			{
				"date": "2019-02-23",
				"temperature": "2/11℃",
				"weather": "多云转阴",
				"wid": {
					"day": "01",
					"night": "02"
				},
				"direct": "北风转东北风"
			},
			{
				"date": "2019-02-24",
				"temperature": "6/12℃",
				"weather": "多云",
				"wid": {
					"day": "01",
					"night": "01"
				},
				"direct": "东北风转北风"
			},
			{
				"date": "2019-02-25",
				"temperature": "5/12℃",
				"weather": "小雨转多云",
				"wid": {
					"day": "07",
					"night": "01"
				},
				"direct": "东北风"
			},
			{
				"date": "2019-02-26",
				"temperature": "5/11℃",
				"weather": "多云转小雨",
				"wid": {
					"day": "01",
					"night": "07"
				},
				"direct": "东北风"
			}
		]
	}
}

# 三、wx.request

  • 发起 HTTPS 网络请求
编号属性类型默认值必填说明
1urlstring开发者服务器接口地址
2datastring/object/ArrayBuffer请求的参数
3headerObject设置请求的 header,header 中不能设置 Referer。content-type 默认为 application/json
4timeoutnumber超时时间,单位为毫秒
5methodstringGETHTTP 请求方法
6dataTypestringjson返回的数据格式
7responseTypestringtext响应的数据类型
8successfunction接口调用成功的回调函数
9failfunction接口调用失败的回调函数
10completefunction接口调用结束的回调函数(调用成功、失败都会执行)

# 1、返回值:回包

编号属性类型说明
1datastring/Object/Arraybuffer开发者服务器返回的数据
2statusCodenumber开发者服务器返回的 HTTP 状态码
3headerObject开发者服务器返回的 HTTP Response Header
4cookiesArray.<string>开发者服务器返回的 cookies,格式为字符串数组
Page({
	onLoad() {
		wx.request({
			url: "http://apis.juhe.cn/simpleWeather/query",
			data: {
				city: "合肥",
				key: "abc4b64ae7656b460723402175a5650b",
			},
			success(res) {
				console.log(res);
			},
		});
	},
});

# 2、参数

method 的合法值

编号说明
1GETHTTP 请求 GET
2POSTHTTP 请求 POST
3HEADHTTP 请求 HEAD
4PUTHTTP 请求 PUT
5DELETEHTTP 请求 DELETE
6TRACEHTTP 请求 TRACE
7CONNECTHTTP 请求 CONNECT
8OPTIONSHTTP 请求 OPTIONS

dataType 的合法值

编号说明
1json返回的数据为 JSON,返回后会对返回的数据进行一次 JSON.parse
2其他不对返回的内容进行 JSON.parse

responseType 的合法值

编号说明
1text响应的数据为文本
2arraybuffer响应的数据为 ArrayBuffer
Page({
	onShow() {
		wx.request({
			url: "http://apis.juhe.cn/simpleWeather/query",
			data: {
				city: "合肥",
				key: "abc4b64ae7656b460723402175a5650b",
			},
			header: {
				// 'content-type': 'application/json'
				"content-type": "application/x-www-form-urlencoded",
			},
			method: "POST",
			dataType: "json",
			responseType: "text",
			success(res) {
				console.log(res);
			},
		});
	},
});
  • data 参数:最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String
    • 对于 GET 方法的数据,会将数据转换成 query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
    • 对于 POST 方法且 header['content-type'] 为 application/json 的数据,会对数据进行 JSON 序列化
    • 对于 POST 方法且 header['content-type'] 为 application/x-www-form-urlencoded 的数据,会将数据转换成 query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)

# 四、理论

# 1、超时时间

  • 默认超时时间和最大超时时间都是 60s;
  • 超时时间可以在 app.json 中通过 networktimeout 配置。

# 2、使用限制

  • wx.request、wx.uploadFile、wx.downloadFile 的最大并发限制是 10 个
  • wx.connectSockt 的最大并发限制是 5 个。
  • 小程序进入后台运行后,如果 5s 内网络请求没有结束,会回调错误信息 fail interrupted;在回到前台之前,网络请求接口调用都会无法调用。

# 3、返回值编码

  • 建议服务器返回值使用 UTF-8 编码。对于非 UTF-8 编码,小程序会尝试进行转换,但是会有转换失败的可能。
  • 小程序会自动对 BOM 头进行过滤(只过滤一个 BOM 头)。

# 4、回调函数

  • 只要成功接收到服务器返回,无论 statusCode 是多少,都会进入 success 回调。请开发者根据业务逻辑对返回值进行判断
Name:
<提交>