# Hook xhr access WeChat gateway

# I. Access guidelines

# 1. Using ajax-hook
<script src="https://unpkg.com/ajax-hook@3.0.3/dist/ajaxhook.min.js"></script>
# 2. Create a new gwpatch.js file

As follows, modify theGATEWAY_DOMAINfield as your gateway domain, andresoureceAppid(Fill out anyAppIDwithin the Weixin Mini Program whitelist, which is just as a source, not validated, and includesRESOLVE_GATEWAY_DOMAINis the domain name that needs to be added to gateway forwarding.

const c1 = new cloud.Cloud({
identityless: true,
resourceAppid: '***', // appid,填入接入的小程序 appid
config: {
    customDomain: 'https://a1d5ad78d-***.sh.wxcloudrun.com' // 网关接入节点域名
}
})
c1.init() // 初始化实例
const gateway = c1.services.Gateway({ domain: 'a1d5ad78d-****.sh.wxcloudrun.com' }) // 网关接入节点域名,不包含协议头

const RESOLVE_GATEWAY_DOMAIN = ["https://domain/"]
const GWFALLBACK=true

const UNPARSE_JSON = false //控制是否强制解json,true 时为不解json,返回为 string 格式
const UNPARES_URL_PERFIX = RESOLVE_GATEWAY_DOMAIN //配置是否解 json的路径


function checkDomain(ob1,ob2){
    for (var i=0;i<ob1.length;i++)
    { 
        if(ob2.startsWith(ob1[i])>0){
            return true
        }
    }
    return false
} 

ah.proxy({
    onRequest: (config, handler) => {
        //config = sigcrypto(config) //加签逻辑
        if (checkDomain(RESOLVE_GATEWAY_DOMAIN,config.url) ) {
            let nheader = config.headers
            nheader["X-WX-HTTP-MODE"]= 'REROUTE'
            gateway.call({
            method:config.method,
            url:config.url,
            header:nheader,
            data:config.body,
            apiVersion:3,
            }
            ).then(res => {
                if (UNPARSE_JSON && checkDomain(UNPARES_URL_PERFIX,config.url) && res.header['content-type']=='application/json'){
                    res.data = JSON.stringify(res.data)
                }
                if (res.errMsg=="gateway.call:ok"){
                    handler.resolve(
                        {
                        config:config,
                        status:res.statusCode,
                        response:res.data,
                        headers:res.header
                        }
                    )
                }else{
                    if (GWFALLBACK){
                        handler.next(config)
                    }else{
                        handler.resolve(
                            {
                            config:config,
                            status:res.statusCode,
                            response:res.data,
                            headers:res.header
                            }
                        )
                    }
                }
            }).catch((error)=>{
                if (GWFALLBACK){
                    handler.next(config)
                }else{
                    handler.resolve(
                        {
                        config:config,
                        status:error.errCode,
                        response:error.errMsg
                        }
                    )
                }
            })
        } else {
        handler.next(config);
        }
        
    }
})
# 3. Introduction of the WeChat gateway web SDK and hook SDK in HTML
<script src="https://该地址隐藏,请前往控制台获取/cloud.js" importance="VeryHigh"></script>
<script src="./ajaxhook.min.js"></script>
<script src="gwpatch.js"></script>
# 4. After successful introduction, all xhr requests will be automatically towed to the websdk
# 5. Just test whether the request is normal and whether the requests are encrypted. Note that the SDK for 2.0.4 does not allow the DevTools debugging tool to be opened, otherwise it will be intercepted.

# II. FAQ

# 1. Can the demotion capability be customized?

You can customize the conditions for entering the downgrade via the catch section of the wxadapter above.

# 2. How can I close it quickly?

Step 3 Cancel the application of the adapter at the entrance.

# 3. Does the service side support proactive downgrading?

In the above code, by default any error will be degraded, or you can force all requests to be degraded by shutting down the gateway's Web access capability by the console.