# Mini Program Manual Access Security Gateway
In general, it is recommended to useOne Click Access, support a variety of grayscale, one key disconnect, preheating and other configuration issued, more flexible and safe. In some special situations, such as PC side, Web, need to be compatible with the lower version), you can use the following manual access program.
important: Need to be in WeChat Official Platform The gateway domain name (a****-********.sh.wxcloudrun.com ) Manually add to the request Legitimate domain name.
# Low version coverage (no frame)
- in app.json In, add a file that uses the cloud: true The ability to cover the low version base library is as follows:
// Apagejson, if it is Uniapp in manifest.json in mp - weixin Field, if it is Taro in app.config.js
{
"cloud": true,
"cloudVersion": "alpha"
}
After the configuration is in effect, it is strongly recommended to test on the real machine for exceptions
cloud: true Ability not supportedOne Click AccessAutomatic rollbackChinese URL parameterThe implementation of corresponding capabilities needs to be completed manually. Can be accessed through the app.js to hit the target onLaunch Add the following code to hijack wx.request Achieve a similar effect of one-click access
// Reference implementation, by hijacking wx.request Achieve a one-click access effect
App({
onLaunch() {
const gateway = wx.cloud.services.Gateway({
// Access domain name: that is, the domain name generated in the gateway access layer, please copy the access domain name of the gateway in the "domain name configuration."
domain: 'a****-********.sh.wxcloudrun.com',
})
// Assignment to cloud Object to facilitate subsequent calls
wx.cloud.gateway = gateway
const __origin_req = wx.request
const gatewayDomainList = [
"https://domain "
] // url Prefix Match Hit gw _Domain_list Will be accessed using cloudsdk
function checkGateway(url) {
for (const element of gatewayDomainList) {
if (url.startsWith(element)) {
return true
}
}
return false
}
// Rewrite wx.request
wx.request = function (option) {
if (checkGateway(option.url)) {
option.header['X-WX-HTTP-MODE'] = "REROUTE"
option.header['X-WX-CONF-VERSION'] = "0"
return gateway.call({
// Fill out the complete domain name (including the agreement header and host Partially, such as https://api.example.com/path? query=xxx)
// if URL Contains Chinese, requires manual Encode
path: option.url,
...option
}).then(option.success). catch(option.fail)
}
return __origin_req(option)
}
}
})
# Taro Third Party Framework
Some third-party frameworks do not support rewriting Wx.request, you can use the official interceptor program access, the specific operation is as follows:
- create Gateway.js, which reads as follows
Taro Example:
import Taro from '@tarojs /taro'
// Gateway domain name whitelist, starting with the following address URL Will be accessed through a gateway.
const GATEWAY_INTERCEPT_URLS = [
'https://example.com'
]
if (Taro.getEnv() === Taro.ENV_TYPE.WEAPP) {
const gateway = wx.cloud.services.Gateway({
// Access domain name: that is, the domain name generated in the gateway access layer, please copy the access domain name of the gateway in the "domain name configuration."
domain: '***your-domain***.sh.wxcloudrun.com',
})
const gatewayInterceptor = (Chain) => {
const params = chain.requestParams
if (GATEWAY_INTERCEPT_URLS.some(url => params.url.startsWith(url))) {
return gateway.call({
...params,
header: {
...params.header,
'X-WX-HTTP-MODE': 'REROUTE',
'X-WX-CONF-VERSION': '0',
},
path: params.url,
})
} else {
return chain.proceed(params)
}
}
Taro.addInterceptor(gatewayInterceptor)
}
Uniapp Example:
// #ifdef MP-WEIXIN
// Gateway domain name whitelist, starting with the following address URL Will be accessed through a gateway.
const GATEWAY_INTERCEPT_URLS = [
'https://example.com'
]
const gateway = wx.cloud.services.Gateway({
// Access domain name: that is, the domain name generated in the gateway access layer, please copy the access domain name of the gateway in the "domain name configuration."
domain: '****.sh.wxcloudrun.com',
})
uni.addInterceptor('request', {
invoke(params) {
if (GATEWAY_INTERCEPT_URLS.some(url => params.url.startsWith(url))) {
return gateway.call({
...params,
header: {
...params.header,
'X-WX-HTTP-MODE': 'REROUTE',
'X-WX-CONF-VERSION': '0',
},
path: params.url,
})
}
},
})
// #endif
- in app.js In, introducing the above JS
import './gateway.js'
- Test if the request is normal.
# API to access
If you need to use a more fine-grained access scheme, you can use API Forms of Access:
Interface GatewayCallParam {
path: string
header: Record<string, string>
data?: string | ArrayBuffer | any
method?: string
success?: (res: GatewayCallResult) => void
fail?: (err: Error) => void
}
Interface GatewayCallResult {
data: any
statusCode: number
header: Record<string, any>
callID: string
}
Interface GatewayInstance {
call: (param: GatewayCallParam ) => Promise<GatewayCallResult>
}
declare Interface WxCloud {
services: {
Gateway: (opts: { domain: string }) => GatewayInstance
}
gateway: GatewayInstance
}
declare Interface Wx {
cloud: WxCloud
}
Examples of use are as follows
const gateway = wx.cloud.services.Gateway({
// Access domain names, which can be obtained in the gateway console
domain: 'a******.sh.wxcloudrun.com',
})
// Assignment to cloud Object to facilitate subsequent calls
wx.cloud.gateway = gateway
const result = wx.cloud.gateway.call({
// Fill out the complete domain name (including the agreement header and host Partially, such as https://api.example.com/path? query=xxx)
// if URL Contains Chinese, requires manual Encode
path: 'https://httpbin.org/get?query=xxx',
// Request method
method: 'GET',
// request header
header: {
//! Important: Need to carry this Header to enable multi-domain support capabilities
'X-WX-HTTP-MODE': 'REROUTE',
//! Manual access to special fields that need to be carried, skipping the version number check
'X-WX-CONF-VERSION': '0',
}
// The remaining parameters are wx.request identical
}).then(result => {
console.log(result)
})
#
# FAQ
Q: What are the limitations of manual access?
- Manual Access Be sure to add the gateway domain name to the MP Valid domain name, or the gateway will trigger a link switch, causing the request to slow down or
- Manual access can not use the following gateway capabilities: link degradation, native encryption and decryption optimization, link warm-up, etc. Gateway performance will have a certain impact according to business scenarios.
- When manually plugged in, it does not automatically Data Transform of parameters to GET URLSearchParam, need to convert yourself
- When manually plugged in, if URL In Chinese, you need to do it yourself. Encodeuricomponent, otherwise the request will report an error
- If the gateway expires or exceeds the traffic limit, it enters the fail Callbacks, which need to be handled manually by the developer
Q: How does the manual access gray traffic?
- When manually plugged in, you can use the API Form of access, at its sole discretion to use the gateway.call still wx.request Make a request
Q: What version is supported for manual access?
- WeChat: 2.14.1 Base library version and above
- Corporate WeChat: 2.20.3 Base library version and above
- Web SDK:2.0.3 SDK Version and above Specific circumstances to the real machine shall prevail, in the opening cloud:true Of the Mini Program, will prompt using cloud: true Information on the