wangzhibo
4 天以前 10595d8632f959d0954d1939243196f4eed02372
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
import { service, useWx } from "/@/cool";
 
export function useOrder() {
    const wx = useWx();
 
    const payTypes = [
        {
            label: "微信支付",
            value: 1,
            key: "wxpay",
            icon: "/static/icon/wxpay.png",
        },
        // {
        //     label: "支付宝支付",
        //     value: 2,
        //     key: "alipay",
        //     icon: "/static/icon/alipay.png",
        // },
        {
            label: "碳积分支付",
            value: 3,
            key: "carbonpay",
            icon: "/static/icon/carbonpay.png",
        },
    ];
 
    function resolvePayType(type?: string | number) {
        return (
            payTypes.find((e) => e.key === type || e.value == type) || payTypes[0]
        );
    }
 
    async function toPay(orderId: number, type: string | number = "wxpay") {
        const pay = resolvePayType(type);
 
        if (pay?.key === "carbonpay") {
            return service.order.pay.carbonPay({ orderId });
        }
 
        // #ifdef MP-WEIXIN
        return service.order.pay.wxMiniPay({ orderId }).then((res) => {
            return wx.miniPay(res.data);
        });
        // #endif
 
        // #ifdef H5
        if (wx.isWxBrowser()) {
            return service.order.pay.wxMpPay({ orderId }).then((res) => {
                return wx.mpPay(res.data);
            });
        }
        // #endif
 
        // #ifdef APP
        return service.order.pay.wxAppPay({ orderId }).then((res) => {
            return wx.appPay(res.data);
        });
        // #endif
    }
 
    return {
        toPay,
        payTypes,
    };
}