cshenfu
2023-06-14 de93aa62c83d6afb9f70cd5a22838375db0a8cbc
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
/**
 * 微信支付相关函数
 */
var wxpay = {
    async getOpenid(data) {
        let {
            provider_pay_type
        } = data;
        if (provider_pay_type === "jsapi") {
            return this._getJsOpenid(data);
        } else {
            return this._getMpOpenid(data);
        }
    },
    async _getMpOpenid(data) {
        let {
            config = {},
                code,
                provider_pay_type
        } = data;
        if (!config.appId) throw new Error("uni-pay配置wxpay.mp节点下的appId不能为空");
        if (!config.secret) throw new Error("uni-pay配置wxpay.mp节点下的secret不能为空");
        let res = await uniCloud.httpclient.request("https://api.weixin.qq.com/sns/jscode2session", {
            method: 'GET',
            data: {
                appid: config.appId,
                secret: config.secret,
                js_code: code,
                grant_type: "authorization_code"
            },
            contentType: 'json', // 指定以application/json发送data内的数据
            dataType: 'json' // 指定返回值为json格式,自动进行parse
        });
        if (res.data && !res.data.errcode && res.data.openid) {
            return {
                errCode: 0,
                errMsg: 'ok',
                openid: res.data.openid,
                unionid: res.data.unionid,
            }
        } else {
            return {
                errCode: -1,
                errMsg: res.data.errmsg
            }
        }
    },
    async _getJsOpenid(data) {
        let {
            config = {},
                code,
                provider_pay_type
        } = data;
        if (!config.appId) throw new Error("uni-pay配置wxpay.jsapi节点下的appId不能为空");
        if (!config.secret) throw new Error("uni-pay配置wxpay.jsapi节点下的secret不能为空");
        let res = await uniCloud.httpclient.request("https://api.weixin.qq.com/sns/oauth2/access_token", {
            method: 'GET',
            data: {
                appid: config.appId,
                secret: config.secret,
                code: code,
                grant_type: "authorization_code"
            },
            contentType: 'json', // 指定以application/json发送data内的数据
            dataType: 'json' // 指定返回值为json格式,自动进行parse
        });
        if (res.data && !res.data.errcode && res.data.openid) {
            return {
                errCode: 0,
                errMsg: 'ok',
                openid: res.data.openid,
                unionid: res.data.unionid,
            }
        } else {
            return {
                errCode: -1,
                errMsg: res.data.errmsg
            }
        }
    }
};
module.exports = wxpay;