Array.prototype.remove = function (val) { var index = this.indexOf(val); while (index > -1) { this.splice(index, 1); index = this.indexOf(val); } } Array.prototype.clear = function () { //this.length = 0; this.splice(0, this.length) } Array.prototype.addAll = function ($array) { if ($array == null || $array.length == 0) return; for (var $i = 0; $i < $array.length; $i++) this.push($array[$i]); } Array.prototype.insertAll = function ($array) { if ($array == null || $array.length == 0) return; $array = $array.reverse() for (var $i = 0; $i < $array.length; $i++) this.splice(0, 0, $array[$i]); } // 对Date的扩展,将 Date 转化为指定格式的String // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 // (new Date(timestamp)).Format("yyyy/M/d h:m") 毫秒转日期 Date.prototype.Format = function (fmt) { //author: meizz if(!fmt) fmt = 'yyyy-MM-dd hh:mm:ss' var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } function getCookie(name) { var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); if (arr = document.cookie.match(reg)) return unescape(arr[2]); else return null; } function setCookie(name, value, days) { days = days || 90; var exp = new Date(); exp.setTime(exp.getTime() + days * 86400 * 1000); document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString(); } function isEmpty(str) { return str == null || str == undefined || str == '' } function locationPage(url, check_returnurl) { if (check_returnurl) { var returnurl = getUrlQueryVal('returnurl') if (returnurl) url = returnurl } location.href = url } function localReplace(url) { location.replace(url) } function locationBack(isRefresh) { if(history.length <= 1) locationPage('/h5/views/qbtask/list.html?type=1') else { if(isRefresh && document.referrer) location.href = document.referrer else history.back() } } function showWeuiDialog($selector, clickFun) { $($selector).show() $($selector + ' .weui-mask').addClass('weui-mask--visible') $($selector + ' .weui-dialog').addClass('weui-dialog--visible') $($selector + ' .weui-mask').unbind('click').click(function () { hideWeuiDialog($selector) }) $($selector + ' .weui-dialog__btn').unbind('click').click(function () { var index = $(this).index() if (clickFun) clickFun(index) }) $($selector + ' .weui-dialog-close').unbind('click').click(function () { hideWeuiDialog($selector) }) } function hideWeuiDialog($selector) { $($selector + ' .weui-mask--visible').removeClass('weui-mask--visible') $($selector + ' .weui-dialog--visible').removeClass('weui-dialog--visible') } function showLoading(text) { //myApp.hideIndicator(); //myApp.showIndicator(); if (text == null) text = '' $.showLoading(text) } function hideLoading() { //$('.preloader-indicator-overlay, .preloader-indicator-modal').fadeOut(400, function () { // myApp.hideIndicator(); //}) // $('.preloader-indicator-overlay, .preloader-indicator-modal').remove(); $.hideLoading() } function showTip(msg, cbk, delay) { if (!delay) delay = 1500 $.toast.prototype.defaults.duration = delay $.toast(msg, 'text', cbk) } function showTopTip(msg, delay) { if (!delay) delay = 1500 $.toptip(msg, delay, 'success'); } function showTopTipError(msg, delay) { if (!delay) delay = 1500 $.toptip(msg, delay, 'error'); } function successConfirmBack(ret) { var msg = '提交成功' if (ret && ret.info) msg = ret.info $.alert(msg, undefined, function () { history.back(); }); } function successConfirmLoad(ret, url) { var msg = '提交成功' if (ret && ret.info) msg = ret.info $.alert(msg, undefined, function () { locationPage(url); }); } function apiGet(url, data, callback, loadingstyle, nohint) { apiRequest(url, data, callback, 'get', loadingstyle, nohint) } function apiPost(url, data, callback, loadingstyle, nohint, isformdata) { apiRequest(url, data, callback, 'post', loadingstyle, nohint, isformdata) } /** * * @param url * @param data * @param callback * @param loadingstyle 0 none 1 preloader 2 indicator * @param nohint */ function apiRequest(url, data, callback, method, loadingstyle, nohint, isformdata) { if (url.indexOf('http') != 0 && url.indexOf('/') != 0) { // url = HOST + url } if (!method) method = 'get' if (!data) data = {} // data['openid'] = getOpenId() // data['timestamp'] = parseInt(new Date().valueOf() / 1000) //data['openid'] = getOpenId() if (loadingstyle == undefined || loadingstyle == null) { //loadingstyle = 1 } if (loadingstyle == 1) showLoading(); else if (loadingstyle == 2) showLoading(); if (isformdata) { var formdata = new FormData() for (var k in data) { formdata.append(k, data[k]) } data = formdata } var options = { type: method, url: url, data: data, dataType: 'json', error: function (res) { if (!nohint) $.alert("请求失败..."); // 加载完毕需要重置 hideLoading(); if (callback) callback(null, -1); }, success: function (data) { if (data.code != 0) { if (!nohint) $.alert(data.msg); } hideLoading() if (callback) callback(data, data.code); } } if (isformdata) { options['processData'] = false options['contentType'] = false } $.ajax(options); } function getUrlQueryVal(name, def) { var reg = new RegExp("(^|&|\\?)" + name + "=([^&]*)(&|$)"); var url = window.location.href var value = null var r = url.substr(1).match(reg); if (r != null) value = decodeURIComponent(r[2]); if (value == null && def) value = def return value; } // function getOpenId() { // return getCookie('openid') // } AM = { initWeixin: function (jsApiList, callback) { AM.getWxSignPackage(function (data) { wx.config({ "appId": data["appId"], "timestamp": data["timestamp"] + '', "nonceStr": data["nonceStr"], "signature": data["signature"], // "debug": true, "jsApiList": jsApiList }); if (callback) callback() }) }, getWxSignPackage: function (callback) { if (!navigator.userAgent.match(/MicroMessenger/i)) { return } apiGet("api/user/get_wx_sign_package", { "url": location.href }, function (result) { // alert(JSON.stringify(result)) callback(result.data); }); }, // 会话缓存 expire 秒 sessionCache: function (key, val, expire) { var timestamp = parseInt(new Date().getTime() / 1000); var data if (val == undefined || val == null) { data = sessionStorage.getItem(key) if (!data) return null data = JSON.parse(data) if (data.expire > 0 && timestamp - data.t > data.expire) { sessionStorage.removeItem(key) return null } return data.val } if (!expire) expire = 0 data = { t: timestamp, expire: expire, val: val } sessionStorage.setItem(key, JSON.stringify(data)) }, getSessionCache: function (key, defaultval) { var val = this.sessionCache(key) if (val == null) val = defaultval return val } } function initWxConfig(callback) { var title = document.title var link = location.href var icon = '' if (typeof wx == 'undefined') return AM.initWeixin([ "onMenuShareTimeline", "onMenuShareAppMessage", "onMenuShareQQ", "onMenuShareQZone", 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'getLocalImgData', 'scanQRCode', // 'startRecord', 'stopRecord', 'onVoiceRecordEnd', 'playVoice', // 'pauseVoice', 'stopVoice', 'onVoicePlayEnd', 'uploadVoice', 'downloadVoice' ], function () { //alert('share init...') if (callback) callback() }) } // 微信JDK选择图片 function wxChooseImage(callback, count) { wx.chooseImage({ count: count || 9, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success: function (res) { // res.localIds.toString(); 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片 setTimeout(function () { callback(res['localIds']); }, 100); } }); } function wxImageSrc(localId, cbk) { var imgsrc = localId var u = navigator.userAgent var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //alert(wx.getLocalImgData) if (isIOS && wx.getLocalImgData) { wx.getLocalImgData({ localId: localId, success: function (res) { var imgsrc = res.localData; //alert(imgsrc) imgsrc = imgsrc.replace(/\r|\n/g, '').replace('data:image/jgp', 'data:image/jpeg') cbk(imgsrc) } }); } else { cbk(imgsrc) } } function wxUploadImage(callback, count) { count = count || 1 var uploadcount = 0 var mediaids = [] var localsrc = [] var localIds = [] wxChooseImage(function (_localIds) { localIds = _localIds.toString().split(',') uploadNext() }, count); function uploadNext() { var localid = localIds[uploadcount] //alert(localid) wxImageSrc(localid, function (src) { localsrc.push(src) wx.uploadImage({ localId: localid, isShowProgressTips: 1, success: function (res) { uploadcount++ mediaids.push(res['serverId']) if (uploadcount < localIds.length) { uploadNext() } else { if (callback) callback(mediaids, localsrc) } } }); }) } } // 上传头像 function wxUploadAvatar(element, callback) { wxChooseImage(function (localIds) { wx.uploadImage({ localId: localIds.toString(), isShowProgressTips: 1, success: function (res) { var url = $(element).data("url") if (!url) url = '/api/user/upload_avatar' apiGet(url, { wxServerIds: res['serverId'] }, function (ret, err) { //if(err) return // $(element).attr("src", ret.data.urls[0]); if (callback) callback(ret, err) }); } }); }, 1); } function transformToJson(form){ var formData = $(form).serializeArray() var obj={} for (var i in formData) { if(formData[i].name != '') obj[formData[i].name]=formData[i]['value']; } return obj; }