cshenfu
2023-06-07 292a07615b42b193351b39d887eff7d9affdff39
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var util = {};
/*
 * 此方法不支持微信公众号
util.getWeixinCode().then((code) => {
 
});
*/
util.getWeixinCode = function() {
    return new Promise((resolve, reject) => {
        // #ifdef MP-WEIXIN
        uni.login({
            provider: 'weixin',
            success(res) {
                resolve(res.code)
            },
            fail(err) {
                reject(new Error('获取微信code失败'))
            }
        })
        // #endif
        // #ifdef APP-PLUS
        plus.oauth.getServices((services) => {
            let weixinAuthService = services.find((service) => {
                return service.id === 'weixin';
            });
            if (weixinAuthService) {
                weixinAuthService.authorize(function(res) {
                    resolve(res.code);
                }, function(err) {
                    console.log(err);
                    reject(new Error('获取微信code失败'));
                });
            }
        });
        // #endif
    })
};
 
util.getAlipayCode = function() {
    // #ifdef APP-PLUS || MP-ALIPAY
    return new Promise((resolve, reject) => {
        uni.login({
            provider: 'alipay',
            success(res) {
                resolve(res.code);
            },
            fail(err) {
                reject(new Error('获取支付宝code失败,可能是没有关联appid或你的支付宝开发者工具还没有登录'));
            }
        });
    });
    // #endif
};
 
util.checkPlatform = function() {
    // #ifdef H5
    let system = {
        win: false,
        mac: false,
        xll: false
    };
    let p = navigator.platform;
    system.win = p.indexOf("Win") == 0;
    system.mac = p.indexOf("Mac") == 0;
    system.x11 = p == "X11" || p.indexOf("Linux") == 0;
    if (system.win || system.mac || system.xll) {
        let ua = navigator.userAgent.toLowerCase();
        if (ua.indexOf("micromessenger") > -1) {
            // 微信开发者工具下访问(注意微信开发者工具下无法唤起微信公众号支付)
            return "pc-weixin";
        } else {
            return "pc";
        }
    } else {
        if (p.indexOf("iPhone") > -1 || p.indexOf("iPad") > -1) {
            return "ios";
        } else {
            return "android";
        }
    }
    // #endif
};
 
/**
 * 获取当前H5所在的环境
 */
util.getH5Env = function() {
    let ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == 'micromessenger' && (ua.match(/miniprogram/i) == 'miniprogram')) {
        // 微信小程序
        return "mp-weixin";
    }
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        // 微信公众号
        return "h5-weixin";
    }
    if (ua.match(/alipay/i) == 'alipay' && ua.match(/miniprogram/i) == 'miniprogram') {
        return "mp-alipay";
    }
    if (ua.match(/alipay/i) == 'alipay') {
        return "h5-alipay";
    }
    // 外部 H5
    return "h5";
};
 
 
/**
 * 日期格式化
 * @params {Date || Number} date 需要格式化的时间
 * timeFormat(new Date(),"yyyy-MM-dd hh:mm:ss");
 */
util.timeFormat = function(time, fmt = 'yyyy-MM-dd hh:mm:ss', targetTimezone = 8) {
    try {
        if (!time) {
            return "";
        }
        if (typeof time === "string" && !isNaN(time)) time = Number(time);
        // 其他更多是格式化有如下:
        // yyyy-MM-dd hh:mm:ss|yyyy年MM月dd日 hh时MM分等,可自定义组合
        let date;
        if (typeof time === "number") {
            if (time.toString().length == 10) time *= 1000;
            date = new Date(time);
        } else {
            date = time;
        }
 
        const dif = date.getTimezoneOffset();
        const timeDif = dif * 60 * 1000 + (targetTimezone * 60 * 60 * 1000);
        const east8time = date.getTime() + timeDif;
 
        date = new Date(east8time);
        let opt = {
            "M+": date.getMonth() + 1, //月份
            "d+": date.getDate(), //日
            "h+": date.getHours(), //小时
            "m+": date.getMinutes(), //分
            "s+": date.getSeconds(), //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (let k in opt) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (opt[k]) : (("00" + opt[k]).substr(("" + opt[k]).length)));
            }
        }
        return fmt;
    } catch (err) {
        // 若格式错误,则原值显示
        return time;
    }
};
 
export default util;