UNPKG

h5-cli

Version:

hello

578 lines (528 loc) 17.7 kB
"use strict"; define("libs/util", function (require, exports, module) { var alerts = null; var timeout = null; var config = window.config; var url = require("./url"); var nihao = module.exports = { pad: function pad(string, length, _pad) { var len = length - String(string).length; if (len < 0) { return string; } var arr = new Array(length - String(string).length || 0); arr.push(string); return arr.join(_pad || '0'); }, flat: function flat(param) { var str = ""; for (var key in param) { str += key + "=" + (param[key] != undefined ? encodeURIComponent(param[key]) : "") + "&"; } return str.slice(0, -1); }, https: function https(a) { return String(a).replace("http://", "//"); }, //\u53ef\u89c6\u5316object\u628a\u5047\u503c\u8f6c\u6362\u4e3a "" vis: function vis(json) { for (var key in json) { // || isNaN(json[key]) if (json[key] === undefined || json[key] === null) { json[key] = ""; } } return json; }, forEach: function forEach(object, callback) { var list = []; for (var key in object) { list.push(callback(object[key], key)); } return list; }, flatStr: function flatStr(str, param) { return str + this.flat(param); }, is: function is(_is, str) { return _is ? str : ""; }, rmb: function rmb(_rmb, dot) { dot = dot == undefined ? 4 : dot; if (_rmb == 0) { return 0; } if (dot == 2) { return _rmb.toFixed(2); } if (dot == 4 && _rmb.toFixed(2) * 10000 == _rmb.toFixed(4) * 10000) { return _rmb.toFixed(2); } return _rmb.toFixed(dot); }, getSID: function getSID() { if (this.isLogin()) { var cookie = document.cookie; var sids = cookie.match(/sid=(\w+)/); if (sids && sids[1]) { return sids[1]; } } return null; }, getUID: function getUID() { if (this.isLogin()) { var cookie = document.cookie; var sids = cookie.match(/[^u]uid=(\d+)/); if (sids && sids[1]) { return sids[1]; } } return null; }, getTID: function getTID() { if (this.isLogin()) { var cookie = document.cookie; var sids = cookie.match(/tid=([\-\w]+)/); if (sids && sids[1]) { return sids[1]; } } return null; }, dateFormatGMT: function dateFormatGMT(source, pattern) { var time = new Date(source); time.setUTCHours(time.getUTCHours() + (time.getTimezoneOffset() + 8 * 60) / 60); return this.dateFormat(time, pattern); }, dateFormat: function dateFormat(source, pattern) { // Jun.com.format(new Date(),"yyyy-MM-dd hh:mm:ss"); //Jun.com.format(new Date(),"yyyy\u5e74MM\u6708dd\u65e5 hh\u65f6:mm\u5206:ss\u79d2"); if (!source) { return ""; } source = new Date(source); var pad = nihao.pad, date = { yy: String(source.getFullYear()).slice(-2), yyyy: source.getFullYear(), M: source.getMonth() + 1, MM: pad(source.getMonth() + 1, 2, '0'), d: source.getDate(), dd: pad(source.getDate(), 2, '0'), h: source.getHours(), hh: pad(source.getHours(), 2, '0'), m: source.getMinutes(), mm: pad(source.getMinutes(), 2, '0'), s: source.getSeconds(), ss: pad(source.getSeconds(), 2, '0') }; return (pattern || "yyyy-MM-dd hh:mm:ss").replace(/yyyy|yy|MM|M|dd|d|hh|h|mm|m|ss|s/g, function (v) { return date[v]; }); }, //\u5c06\u91d1\u989d*100\u7684\u6574\u6570\u8f6c\u6362\u6210 \u5343\u5206\u4f4d\u91d1\u989d 1,234.00 moneyFormat: function moneyFormat(money, fixed) { if (!fixed) { fixed = 2; } if (!money || Number(money) == 0) { return 0; //(0).toFixed(fixed) } var negative = false; var mStr = money.toString().split("."); mStr[1] = Number('0.00' + (mStr[1] || "")); if (mStr[0].indexOf('-') == 0) { negative = true; mStr[0] = mStr[0].replace("-", ''); } var len = mStr[0].length; if (len <= 5) { if (negative) { return '-' + (mStr[0] / 100 + mStr[1]).toFixed(fixed); } else { return (mStr[0] / 100 + mStr[1]).toFixed(fixed); } } var decimal = (Number('0.' + mStr[0].slice(-2)) + mStr[1]).toFixed(fixed).replace("0.", ""); var num = []; for (var i = -5; i > -len - 3; i = i - 3) { var part = []; part[0] = mStr[0].slice(i, i + 3); num = part.concat(num); } var round = num.join(","); //return (round + '.' + decimal ); if (negative) { return '-' + round + '.' + decimal; } else { return round + '.' + decimal; } }, //\u83b7\u53d6\u6807\u51c6\u65f6\u5dee getTimezoneOffset: function getTimezoneOffset() { var now = new Date(); return now.getTimezoneOffset() * 60 * 1000; }, isLogin: function isLogin() { return (/sid\=\w+/.test(document.cookie) ); }, isInYuantuApp: function isInYuantuApp() { //\u4e34\u65f6 //return navigator.userAgent.indexOf("MicroMessage") != -1; return navigator.userAgent.indexOf("YuanTu(") != -1; // return navigator.userAgent.indexOf("YuanTu(") != -1; }, //\u5728\u5fae\u4fe1\u4e2d isInMicroMessenger: function isInMicroMessenger() { return navigator.userAgent.indexOf("MicroMessenger") != -1; }, isInAliPay: function isInAliPay() { return navigator.userAgent.indexOf("AliPay") != -1; }, isInZhifubao: function isInZhifubao() { return navigator.userAgent.indexOf("AliApp(AP") != -1; }, isInIOS: function isInIOS() { //ios return (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent) ); }, waitAlert: function waitAlert(text, delay) { /** 700 \u6beb\u79d2\u4ee5\u4e0a\u624d\u5f39\u51fa\u52a0\u8f7d\u52a8\u753b */ var self = this; this.waitHide(); timeout = setTimeout(function () { self.alertDialog({ text: text, time: 120000, isWait: true }); }, delay === undefined ? 700 : delay); }, waitHide: function waitHide() { timeout && clearTimeout(timeout); if (alerts) { alerts.remove(); } }, alert: function alert(text, callback) { this.alertDialog({ text: text, time: Math.max(String(text).length * 250, 2000) }, callback); }, /** * text * param { text:"", time:"", //\u663e\u793a\u65f6\u95f4 isWait:true //\u662f\u5426\u663e\u793a\u7b49\u5f85 } */ alertDialog: function alertDialog(param, callback) { timeout && clearTimeout(timeout); var text = param.text; var time = param.time; var isWait = param.isWait; // console.log( param ) if (alerts) { alerts.remove(); alerts = null; } alerts = $('<div class="ui-alert"><div class="text">' + (isWait ? '<div class="loading-icon"></div>' : '') + text + '</div></div>'); alerts.appendTo(document.body); setTimeout(function () { callback && callback(); alerts && alerts.remove(); }, time || text.length * 200); return alerts; }, dialog: function dialog(content, callback, option) { option = $.extend({ cancel: true, cancelText: "\u53d6\u6d88", ok: true, okText: "\u786e\u5b9a" }, option || {}); var tmpl = '<div class="ui-dialog show">' + '<div class="ui-dialog-cnt">' + '<div class="ui-dialog-bd">' + '<div style="text-align:center;">' + content + '</div>' + '</div>' + '<div class="ui-dialog-ft">' + (option.cancel ? '<button type="button" class="J_Btn" data-role="0">' + option.cancelText + '</button>' : '') + (option.ok ? '<button type="button" class="J_Btn" data-role="1">' + option.okText + '</button>' : '') + '</div>' + '</div>' + '</div>'; var dialog = $(tmpl); dialog.appendTo(document.body).find(".J_Btn").click(function () { var role = $(this).attr("data-role"); dialog.remove(); callback && callback(role == "1"); }); }, //\u8c03\u7528jsbrige\u65b9\u6cd5 brige: function brige(name, param, callback, fail, time) { if (this.isInYuantuApp()) { lib.windvane.call("jsbrige", name, param || {}, callback || function () {}, fail || function () {}, time); } else { // \u53ea\u5728YuantuApp\u8c03\u7528jsBrige return false; } }, //\u8df3\u8f6c\u5230\u767b\u5f55\u9875\u9762 history \u662f\u5426\u6e05\u695a\u5f53\u524d\u9875\u9762\u7684\u5386\u53f2\u8bb0\u5f55 goLogin: function goLogin(clearHistory) { // var h5_domain = config.h5Domain; var query = url.parse(window.location.href, true).query || {}; var unionId = query.unionId || ''; var corpId = query.corpId || ''; var loginUrl = this.h5URL("/sign-in.html"); //"https://"+h5_domain+"/tms/h5/login.php"; if (this.isInYuantuApp()) { loginUrl = this.h5URL("pages/logo2.html"); loginUrl += "?backview=1"; //\u767b\u5f55\u5b8c\u6210\u8df3\u8f6c\u5230\u4e0a\u4e00\u4e2a\u9875\u9762\u5e76\u5237\u65b0 this.brige("openView", { url: loginUrl, animation: "bottom-in" }); } else { if (history) { /*window.location.replace(this.flatStr(loginUrl + "?", { redirecturl: encodeURIComponent(window.location.href), unionId: query.unionId, }));*/ var flag = '?'; if (loginUrl.indexOf('?') > -1) { flag = '&'; } window.location.replace(loginUrl + flag + "redirecturl=" + encodeURIComponent(window.location.href) + "&unionId=" + unionId + (unionId ? '' : '&corpId=' + corpId)); } else { //window.location.href = this.flatStr(loginUrl + "?", {unionId: query.unionId}); var _flag = '?'; if (loginUrl.indexOf('?') > -1) { _flag = '&'; } window.location.href = loginUrl + _flag + "unionId=" + unionId + (unionId ? '' : '&corpId=' + corpId);; } } }, //\u8fd4\u56de\u4e0a\u4e00\u4e2awebview goBack: function goBack(isReload, url) { // lib.windvane.call("jsbrige","backView", {isReload:isReload}); if (this.isInYuantuApp()) { this.brige("backView", { isReload: isReload }); } else { //\u76f4\u63a5back\u65e0\u6cd5\u5237\u65b0\u4e0a\u4e00\u4e2a\u9875\u9762\uff0c replace \u4f1a\u5bfc\u81f4\u9700\u8981\u8fd4\u56de\u4e24\u6b21 // if(isReload && document.referrer){ // window.location.replace(document.referrer); // }else{ window.history.back(); // } } }, //\u652f\u4ed8\u5b9d\u652f\u4ed8 pay: function pay(feeChannel, param, callback, fail) { this.brige("pay", { feeChannel: feeChannel, //1 \u652f\u4ed8\u5b9d\uff0c 2\u5fae\u4fe1\u652f\u4ed8 payData: param }, callback, fail); }, getYuanTuVersion: function getYuanTuVersion() { //getPlatform // > gt // < lt this.getPlatform(); }, setNativeTitle: function setNativeTitle(text, callback, fail) { if (this.isInYuantuApp()) { this.brige("setTitle", { text: text }, callback, fail); } else { document.title = text; } }, getDeviceToke: function getDeviceToke(callback, fail) { if (this.isInYuantuApp()) { this.brige("getDeviceToken", {}, callback, fail); } else { fail(); } }, getPlatform: function getPlatform() { var ua = navigator.userAgent; if (ua.indexOf("iPhone") != -1 || ua.indexOf("iPad") != -1) { return "ios"; } if (ua.indexOf("Android") != -1) { return "android"; } return null; }, h5URL: function h5URL(path) { return location.protocol + "//" + config.h5Domain + "/yuantu/h5-cli/" + config.version + path; }, //\u5f53\u524d\u5ba2\u6237\u7aef\u7248\u672c\u6bd4\u8f83 version: { v: function () { var versions = navigator.userAgent.match(/YuanTu\((?:QY|YY)\/([\d.]+)\)/); if (versions && versions.length) { return { x: parseInt(versions[1].split(".")[0]), y: parseInt(versions[1].split(".")[1]), z: parseInt(versions[1].split(".")[2]) }; } return { x: 0, y: 0, z: 0 }; }(), // > gt: function gt(x, y, z) { var aa = [this.v.x, this.v.y, this.v.z]; var ab = [x, y, z]; var i = 0; var la = aa.length, lb = ab.length; while (la > lb) { ab.push(0); ++lb; } while (la < lb) { aa.push(0); ++la; } while (i < la && i < lb) { var ai = parseInt(aa[i], 10); var bi = parseInt(ab[i], 10); if (ai > bi) { return true; } else if (ai < bi) { return false; } ++i; } return false; }, // < lt: function lt(x, y, z) { // \u76f8\u7b49\u7684\u65f6\u5019\u4e0d\u5c0f\u4e8e if (this.v.x == x && this.v.y == y && this.v.z == z) { return false; } return !this.gt(x, y, z); }, test: function test() { var v = { v: { x: 2, y: 1, z: 19 } }; console.log(nihao.version.gt.apply(v, [2, 1, 6]) == true); console.log(nihao.version.gt.apply(v, [3, 1, 6]) == false); console.log(nihao.version.gt.apply(v, [2, 2, 6]) == false); console.log(nihao.version.gt.apply(v, [2, 1, 19]) == false); // console.log( nihao.version.test() ) return [this.v.x, this.v.y, this.v.z].join(".") > [x, y, z].join("."); } }, h5Version: { v: function () { var versions = window.location.href.match(/\/h5\-cli\/([\d\.]+)\/pages/); if (versions && versions.length) { return { x: parseInt(versions[1].split(".")[0]), y: parseInt(versions[1].split(".")[1]), z: parseInt(versions[1].split(".")[2]) }; } return { x: 100, y: 100, z: 100 }; }(), // > gt: function gt(x, y, z) { var aa = [this.v.x, this.v.y, this.v.z]; var ab = [x, y, z]; var i = 0; var la = aa.length, lb = ab.length; while (la > lb) { ab.push(0); ++lb; } while (la < lb) { aa.push(0); ++la; } while (i < la && i < lb) { var ai = parseInt(aa[i], 10); var bi = parseInt(ab[i], 10); if (ai > bi) { return true; } else if (ai < bi) { return false; } ++i; } return false; }, // < lt: function lt(x, y, z) { // \u76f8\u7b49\u7684\u65f6\u5019\u4e0d\u5c0f\u4e8e if (this.v.x == x && this.v.y == y && this.v.z == z) { return false; } return !this.gt(x, y, z); } }, IsPC: function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"); var flag = true; for (var v = 0; v < Agents.length; v++) { if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; } } return flag; }, keys: function keys(obj) { var keys = []; for (var key in obj) { keys.push(key); } return keys; } }; //test if (!Object.assign) { // \u5b9a\u4e49assign\u65b9\u6cd5 Object.defineProperty(Object, 'assign', { enumerable: false, configurable: true, writable: true, value: function value(target) { // assign\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570 'use strict'; // \u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u7a7a\uff0c\u5219\u629b\u9519 if (target === undefined || target === null) { throw new TypeError('Cannot convert first argument to object'); } var to = Object(target); // \u904d\u5386\u5269\u4f59\u6240\u6709\u53c2\u6570 for (var i = 1; i < arguments.length; i++) { var nextSource = arguments[i]; // \u53c2\u6570\u4e3a\u7a7a\uff0c\u5219\u8df3\u8fc7\uff0c\u7ee7\u7eed\u4e0b\u4e00\u4e2a if (nextSource === undefined || nextSource === null) { continue; } nextSource = Object(nextSource); // \u83b7\u53d6\u6539\u53c2\u6570\u7684\u6240\u6709key\u503c\uff0c\u5e76\u904d\u5386 var keysArray = Object.keys(nextSource); for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) { var nextKey = keysArray[nextIndex]; var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); // \u5982\u679c\u4e0d\u4e3a\u7a7a\u4e14\u53ef\u679a\u4e3e\uff0c\u5219\u76f4\u63a5\u6d45\u62f7\u8d1d\u8d4b\u503c if (desc !== undefined && desc.enumerable) { to[nextKey] = nextSource[nextKey]; } } } return to; } }); } window.nihao = nihao; });