Commit e79c5054bdf701aa760ec686ade5f9603476cf9d
1 parent
5f08568d
增加unit.js
Showing
1 changed file
with
500 additions
and
0 deletions
resource/js/unit.js
0 → 100644
| 1 | +/** | |
| 2 | + * Created by mac on 17/5/8. | |
| 3 | + */ | |
| 4 | +sysComm = { | |
| 5 | + sysCode: '1001',//企业云系统编码 | |
| 6 | + gvnSysCode: '1002',//政府系统编码 | |
| 7 | + grantType: 'PASSWORD',//授权模式 | |
| 8 | + clientType: 'WEB'//客户端类型 | |
| 9 | +} | |
| 10 | + | |
| 11 | +//阿里云测试环境,别删 | |
| 12 | +// | |
| 13 | +// var baseUrl = 'http://192.168.1.196:8088/';// url公用地址 | |
| 14 | +// var parkcloudbaseUrl = 'http://192.168.1.196:8093/';// url公用地址 | |
| 15 | +// var authUrl = 'http://192.168.1.196:8088/';// 鉴权URL公用地址s | |
| 16 | + | |
| 17 | + | |
| 18 | + var parkcloudbaseUrl = 'http://localhost:8100/';// url公用地址 | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | +var dataUrl = dataUrl || {}; | |
| 26 | + | |
| 27 | +// dataUrl.util = { | |
| 28 | +// | |
| 29 | +// | |
| 30 | +// | |
| 31 | +// } | |
| 32 | + | |
| 33 | +dataUrl.util = { | |
| 34 | + | |
| 35 | + | |
| 36 | + queryChargerIncomeByPlNos: function () { | |
| 37 | + return parkcloudbaseUrl + 'index/new/queryChargerIncomeByPlNos'; | |
| 38 | + }, | |
| 39 | + | |
| 40 | + | |
| 41 | + //获取普通API鉴权token | |
| 42 | + getToken: function () { | |
| 43 | + var token = sessionStorage.getItem("token"); | |
| 44 | + if (StrUtil.isEmpty(token)) { | |
| 45 | + token = ''; | |
| 46 | + } | |
| 47 | + return token; | |
| 48 | + }, | |
| 49 | + //设置普通API鉴权token | |
| 50 | + setToken: function (token) { | |
| 51 | + sessionStorage.setItem("token", token); | |
| 52 | + }, | |
| 53 | + getUserAccount: function () { | |
| 54 | + return sessionStorage.getItem("userAccount"); | |
| 55 | + }, | |
| 56 | + setUserAccount: function (userAccount) { | |
| 57 | + sessionStorage.setItem("userAccount", userAccount); | |
| 58 | + }, | |
| 59 | + getUserName: function () { | |
| 60 | + return sessionStorage.getItem("userName"); | |
| 61 | + }, | |
| 62 | + setUserName: function (userName) { | |
| 63 | + sessionStorage.setItem("userName", userName); | |
| 64 | + }, | |
| 65 | + getOpImgPath: function () { | |
| 66 | + return sessionStorage.getItem("imgPath"); | |
| 67 | + }, | |
| 68 | + setOpImgPath: function (imgPath) { | |
| 69 | + sessionStorage.setItem("imgPath", imgPath); | |
| 70 | + }, | |
| 71 | + getUserId: function () { | |
| 72 | + return sessionStorage.getItem("userId"); | |
| 73 | + }, | |
| 74 | + setUserId: function (userId) { | |
| 75 | + sessionStorage.setItem("userId", userId); | |
| 76 | + }, | |
| 77 | + getOrgId: function () { | |
| 78 | + return sessionStorage.getItem("orgId"); | |
| 79 | + }, | |
| 80 | + setOrgId: function (orgId) { | |
| 81 | + sessionStorage.setItem("orgId", orgId); | |
| 82 | + }, | |
| 83 | + getSysShowName: function () { | |
| 84 | + return sessionStorage.getItem("sysShowName"); | |
| 85 | + }, | |
| 86 | + setSysShowName: function (sysShowName) { | |
| 87 | + sessionStorage.setItem("sysShowName", sysShowName); | |
| 88 | + }, | |
| 89 | + //跳转到登录界面 | |
| 90 | + toLoginPage: function () { | |
| 91 | + sessionStorage.setItem("sys_parkLot", ''); | |
| 92 | + window.location.href = '../../login/login.html' | |
| 93 | + }, | |
| 94 | + //登出系统 | |
| 95 | + loginOut: function () { | |
| 96 | + sessionStorage.clear() | |
| 97 | + var token = fn.getToken(); | |
| 98 | + fn.setToken(''); | |
| 99 | + if (StrUtil.isNotEmpty()) { | |
| 100 | + var req = {'token': token, 'url': dataUrl.util.getLoginOut()} | |
| 101 | + sysAjax(req); | |
| 102 | + | |
| 103 | + } | |
| 104 | + fn.toLoginPage(); | |
| 105 | + } | |
| 106 | + | |
| 107 | +} | |
| 108 | + | |
| 109 | +DateUtil = { | |
| 110 | + dataFormat: function (longTime, formatStr) { | |
| 111 | + //格式化时间 | |
| 112 | + //longTime 要进行格式化的long类型的时间 | |
| 113 | + //formatStr 要格式化成的字符串类型 | |
| 114 | + var data = new Date(longTime); | |
| 115 | + return data.Format(formatStr); | |
| 116 | + }, | |
| 117 | + isLongTime: function (longTime) { | |
| 118 | + //判断是否是longTime时间类型 | |
| 119 | + | |
| 120 | + } | |
| 121 | +} | |
| 122 | + | |
| 123 | +//cookie操作工具类 | |
| 124 | +CkUtil = { | |
| 125 | + //获取cookie | |
| 126 | + getCookie: function (name) { | |
| 127 | + var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); | |
| 128 | + if (arr = document.cookie.match(reg)) | |
| 129 | + return unescape(arr[2]); | |
| 130 | + else | |
| 131 | + return null; | |
| 132 | + }, | |
| 133 | + //删除cookie | |
| 134 | + delCookie: function (name) { | |
| 135 | + var exp = new Date(); | |
| 136 | + exp.setTime(exp.getTime() - 1); | |
| 137 | + var cval = CkUtil.getCookie(name); | |
| 138 | + if (cval != null) | |
| 139 | + document.cookie = name + "=" + cval + ";expires=" + exp.toUTCString(); | |
| 140 | + }, | |
| 141 | + //设置cookie,time单位毫秒 | |
| 142 | + setCookie: function (name, value, time) { | |
| 143 | + var exp = new Date(); | |
| 144 | + exp.setTime(exp.getTime() + time); | |
| 145 | + document.cookie = name + "=" + escape(value) + ";expires=" + exp.toUTCString(); | |
| 146 | + }, | |
| 147 | + //获取毫秒数 1s的毫秒数为 getSec('s1') 1小时为 getSec('h1') | |
| 148 | + getSec: function (str) { | |
| 149 | + var str1 = str.substring(1, str.length) * 1; | |
| 150 | + var str2 = str.substring(0, 1); | |
| 151 | + if (str2 == "s") { | |
| 152 | + return str1 * 1000; | |
| 153 | + } | |
| 154 | + else if (str2 == "h") { | |
| 155 | + return str1 * 60 * 60 * 1000; | |
| 156 | + } | |
| 157 | + else if (str2 == "d") { | |
| 158 | + return str1 * 24 * 60 * 60 * 1000; | |
| 159 | + } | |
| 160 | + } | |
| 161 | + | |
| 162 | +} | |
| 163 | + | |
| 164 | +ArrayUtils = { | |
| 165 | + /**判断属性是否已经在数组中存在*/ | |
| 166 | + contains: function (array, obj) { | |
| 167 | + var i = array.length; | |
| 168 | + while (i--) { | |
| 169 | + if (array[i] === obj) { | |
| 170 | + return true; | |
| 171 | + } | |
| 172 | + } | |
| 173 | + return false; | |
| 174 | + } | |
| 175 | + | |
| 176 | +} | |
| 177 | + | |
| 178 | + | |
| 179 | +StrUtil = { | |
| 180 | + isEmpty: function (str) { | |
| 181 | + if (str == undefined || null == str || StrUtil.trim(str).length == 0) { | |
| 182 | + return true; | |
| 183 | + } | |
| 184 | + return false; | |
| 185 | + }, | |
| 186 | + isNotEmpty: function (str) { | |
| 187 | + return !StrUtil.isEmpty(str); | |
| 188 | + }, | |
| 189 | + trim: function (str) { | |
| 190 | + //删除左右两端的空格 | |
| 191 | + return str.replace(/(^\s*)|(\s*$)/g, ""); | |
| 192 | + }, | |
| 193 | + ltrim: function (str) { | |
| 194 | + //删除左边的空格 | |
| 195 | + return str.replace(/(^\s*)/g, ""); | |
| 196 | + }, | |
| 197 | + rtrim: function (str) { | |
| 198 | + //删除右边的空格 | |
| 199 | + return str.replace(/(\s*$)/g, ""); | |
| 200 | + } | |
| 201 | +} | |
| 202 | + | |
| 203 | + | |
| 204 | +// 对Date的扩展,将 Date 转化为指定格式的String | |
| 205 | +// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, | |
| 206 | +// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) | |
| 207 | +// 例子: | |
| 208 | +// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 | |
| 209 | +// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 | |
| 210 | +Date.prototype.Format = function (fmt) { //author: meizz | |
| 211 | + var o = { | |
| 212 | + "M+": this.getMonth() + 1, //月份 | |
| 213 | + "d+": this.getDate(), //日 | |
| 214 | + "h+": this.getHours(), //小时 | |
| 215 | + "m+": this.getMinutes(), //分 | |
| 216 | + "s+": this.getSeconds(), //秒 | |
| 217 | + "q+": Math.floor((this.getMonth() + 3) / 3), //季度 | |
| 218 | + "S": this.getMilliseconds() //毫秒 | |
| 219 | + }; | |
| 220 | + if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); | |
| 221 | + for (var k in o) | |
| 222 | + if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); | |
| 223 | + return fmt; | |
| 224 | +} | |
| 225 | + | |
| 226 | + | |
| 227 | +//公用接口调用 | |
| 228 | + | |
| 229 | +function sysAjax(opts) { | |
| 230 | + | |
| 231 | + //一.设置默认参数 | |
| 232 | + var defaults = { | |
| 233 | + method: 'GET', | |
| 234 | + url: '', | |
| 235 | + dataType: 'json', | |
| 236 | + data: '', | |
| 237 | + async: true, | |
| 238 | + cache: false, | |
| 239 | + contentType: 'application/json; charset=utf-8', | |
| 240 | + success: function () { | |
| 241 | + }, | |
| 242 | + error: function () { | |
| 243 | + } | |
| 244 | + }; | |
| 245 | + | |
| 246 | + | |
| 247 | + //二.用户参数覆盖默认参数 | |
| 248 | + for (var key in opts) { | |
| 249 | + defaults[key] = opts[key]; | |
| 250 | + } | |
| 251 | + // console.log("请求相关参数:"); | |
| 252 | + // console.log(defaults) | |
| 253 | + $.ajax({ | |
| 254 | + type: defaults.method, | |
| 255 | + url: defaults.url, | |
| 256 | + dataType: defaults.dataType, | |
| 257 | + contentType: defaults.contentType, | |
| 258 | + data: defaults.data, | |
| 259 | + async: defaults.async, | |
| 260 | + beforeSend: function (xhr) { | |
| 261 | + //设置请求头 | |
| 262 | + //xhr.setRequestHeader("User-Agent", "headertest"); | |
| 263 | + //console.log(JSON.stringify(sysComm)); | |
| 264 | + xhr.setRequestHeader("x-auth-token", fn.getToken()); | |
| 265 | + }, | |
| 266 | + success: function (res, status, xhr) { | |
| 267 | + //console.log('获取header:'+xhr.getAllResponseHeaders()); | |
| 268 | + // console.log("服务器响应信息:"); | |
| 269 | + // console.log(res) | |
| 270 | + var msg = (typeof res.msg == 'string') && (res.msg.constructor == String) ? res.msg : res.errMsg; | |
| 271 | + //成功 | |
| 272 | + if (res.code == "2003") { | |
| 273 | + //授权令牌不存在或已失效,请重新登录后在尝试 | |
| 274 | + window.location.href = '../login.html' | |
| 275 | + } else if (res.code == "2004") { | |
| 276 | + //授权令牌不合法,请重新登录收尝试 | |
| 277 | + //alert(res.msg); | |
| 278 | + window.location.href = '../login.html' | |
| 279 | + } else if (res.code == "2005") { | |
| 280 | + //无此权限 | |
| 281 | + alert(msg); | |
| 282 | + } else { | |
| 283 | + defaults.success(res, status, xhr); | |
| 284 | + } | |
| 285 | + }, | |
| 286 | + error: function (XMLHttpRequest, textStatus, errorThrown) { | |
| 287 | + if (textStatus == "timeout") { | |
| 288 | + //alert('请求超时,请重试'); | |
| 289 | + } else { | |
| 290 | + //alert("请求报错") | |
| 291 | + console.log(errorThrown); | |
| 292 | + } | |
| 293 | + | |
| 294 | + } | |
| 295 | + }); | |
| 296 | +} | |
| 297 | + | |
| 298 | + | |
| 299 | +function ajaxCommon(opts) { | |
| 300 | + //一.设置默认参数 | |
| 301 | + var defaults = { | |
| 302 | + method: 'GET', | |
| 303 | + url: '', | |
| 304 | + data: '', | |
| 305 | + async: true, | |
| 306 | + cache: false, | |
| 307 | + contentType: 'application/json; charset=utf-8', | |
| 308 | + beforeSend: function (xhr) { | |
| 309 | + //设置请求头 | |
| 310 | + //xhr.setRequestHeader("User-Agent", "headertest"); | |
| 311 | + //console.log(JSON.stringify(sysComm)); | |
| 312 | + xhr.setRequestHeader("x-auth-token", fn.getToken()); | |
| 313 | + }, | |
| 314 | + success: function () { | |
| 315 | + }, | |
| 316 | + error: function () { | |
| 317 | + } | |
| 318 | + }; | |
| 319 | + | |
| 320 | + //二.用户参数覆盖默认参数 | |
| 321 | + for (var key in opts) { | |
| 322 | + defaults[key] = opts[key]; | |
| 323 | + } | |
| 324 | + | |
| 325 | + defaults.method = defaults.method.toUpperCase(); //处理 method | |
| 326 | + | |
| 327 | + //三.对数据进行处理 | |
| 328 | + //如果数据是对象,并且 方法为 get方法时,将data拼接到url后面 | |
| 329 | + if (typeof defaults.data === 'object' && defaults.method === 'GET') { //处理 data | |
| 330 | + var str = ''; | |
| 331 | + for (var key in defaults.data) { | |
| 332 | + str += key + '=' + defaults.data[key] + '&'; | |
| 333 | + } | |
| 334 | + defaults.data = str.substring(0, str.length - 1); | |
| 335 | + } | |
| 336 | + | |
| 337 | + defaults.cache = defaults.cache ? '' : '&' + new Date().getTime();//处理 cache | |
| 338 | + | |
| 339 | + if (defaults.method === 'GET' && (defaults.data || defaults.cache)) defaults.url += '?' + defaults.data + defaults.cache; //处理 url | |
| 340 | + | |
| 341 | + //四.开始编写ajax | |
| 342 | + //1.创建ajax对象 | |
| 343 | + var oXhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); | |
| 344 | + //2.和服务器建立联系,告诉服务器你要取什么文件 | |
| 345 | + oXhr.open(defaults.method, defaults.url, defaults.async); | |
| 346 | + //3.发送请求 | |
| 347 | + if (defaults.method === 'GET') | |
| 348 | + oXhr.send(null); | |
| 349 | + else { | |
| 350 | + oXhr.setRequestHeader("Content-type", defaults.contentType); | |
| 351 | + oXhr.send(defaults.data); | |
| 352 | + } | |
| 353 | + //4.等待服务器回应 | |
| 354 | + oXhr.onreadystatechange = function () { | |
| 355 | + if (oXhr.readyState === 4) { | |
| 356 | + if (oXhr.status === 200) | |
| 357 | + defaults.success.call(oXhr, oXhr.responseText); | |
| 358 | + else { | |
| 359 | + defaults.error(); | |
| 360 | + } | |
| 361 | + } | |
| 362 | + }; | |
| 363 | +} | |
| 364 | + | |
| 365 | +/** | |
| 366 | + * jquery 解绑绑定函数.<br/> | |
| 367 | + * wangfs 2018-01-11.<br/> | |
| 368 | + */ | |
| 369 | +var documentBindFunc ={ | |
| 370 | + /** | |
| 371 | + * @param event 事件名称如:click | |
| 372 | + * @param element 元素 如:id,class 元素等 | |
| 373 | + * @param func 函数 | |
| 374 | + * 例子:documentBindFunc.on('click','#berthmanage-addBtn',function () {}); | |
| 375 | + */ | |
| 376 | + on: function (event, element, func) { | |
| 377 | + $(document).off(event, element); | |
| 378 | + $(document).on(event, element, func); | |
| 379 | + } | |
| 380 | + | |
| 381 | +}; | |
| 382 | +/* | |
| 383 | + @ songcxa | |
| 384 | + @ 2018-1-3 | |
| 385 | + @ 暂时公共方法 | |
| 386 | +*/ | |
| 387 | +var commonClass = { | |
| 388 | + /* | |
| 389 | + @切割字符串 | |
| 390 | + @str原字符串 | |
| 391 | + @num字符位数 | |
| 392 | + */ | |
| 393 | + cutString: function (str, num) { | |
| 394 | + var _str = ""; | |
| 395 | + if (str.length >= num) { | |
| 396 | + var strN = str.substring(0, num); | |
| 397 | + strN += "..."; | |
| 398 | + _str = strN; | |
| 399 | + } else { | |
| 400 | + _str = str; | |
| 401 | + } | |
| 402 | + return _str; | |
| 403 | + }, | |
| 404 | + | |
| 405 | + /* | |
| 406 | + @ popupObject:pop-plug | |
| 407 | + @ inputID:输入框id | |
| 408 | + @ popID:提示框id | |
| 409 | + @ queryOption(url、queryParames请求参数post、itemsSelectCallback提示框选择事件) | |
| 410 | + */ | |
| 411 | + popupObject: function (inputID, popID, queryOption) { | |
| 412 | + var _self=this; | |
| 413 | + var _inputID = $("#" + inputID), _popID = $("#" + popID); | |
| 414 | + _self._popID = _popID; | |
| 415 | + _self._tmpXhr = null; | |
| 416 | + var url = queryOption.url; | |
| 417 | + var queryParames = queryOption.queryParames; | |
| 418 | + var itemsSelectCallback = queryOption.itemsSelectCallback || "undefined"; | |
| 419 | + //注册输入事件 | |
| 420 | + _inputID.keyup(function () { | |
| 421 | + var req = queryParames(); | |
| 422 | + req.baseRequest.pageSize = 5;//前5条记录 | |
| 423 | + var self = _self; | |
| 424 | + if (req.parkNameOrAddress != "") { | |
| 425 | + $(".cancelBtn").css("display", "block"); | |
| 426 | + self._tmpXhr = $.ajax({ | |
| 427 | + type: 'POST', | |
| 428 | + url: url,//处理页面的url地址 | |
| 429 | + cache: false, | |
| 430 | + data: JSON.stringify(req),//要传递参数 | |
| 431 | + contentType: 'application/json; charset=utf-8', | |
| 432 | + //dataType: 'json', | |
| 433 | + success: function (data) { | |
| 434 | + if (data != '') { | |
| 435 | + dataCallBack(data); | |
| 436 | + } else { | |
| 437 | + commonClass.setPopupVisible(false); | |
| 438 | + } | |
| 439 | + }, | |
| 440 | + error: function (par1, par2) { | |
| 441 | + //alert("错误:"+dataUrl.util.queryParkLotByCountryAndParkName()); | |
| 442 | + } | |
| 443 | + }); | |
| 444 | + } else { | |
| 445 | + commonClass.setPopupVisible(false); | |
| 446 | + _inputID.val(""); _inputID.focus(); | |
| 447 | + } | |
| 448 | + }); | |
| 449 | + //格式化数据 | |
| 450 | + var dataCallBack = function (res) { | |
| 451 | + if (res.code == '8888') { | |
| 452 | + var tmpArr = res.data.dataList; | |
| 453 | + if (tmpArr == null) { commonClass.setPopupVisible(false); return }; | |
| 454 | + var lists = "<ul class='ser-resPanel-items'>"; | |
| 455 | + $.each(tmpArr, function (index, obj) { | |
| 456 | + //小问题:后台问题【字段应该做统一】 | |
| 457 | + //PDA和停车场用plName | |
| 458 | + if (typeof (obj.plName) != "undefined") { | |
| 459 | + lists += "<li>" + obj.plName + "</li>"; | |
| 460 | + } else {//诱导屏用guideName | |
| 461 | + lists += "<li>" + obj.guideName + "</li>"; | |
| 462 | + } | |
| 463 | + }); | |
| 464 | + lists += "</ul>"; | |
| 465 | + _popID.html(lists).show();//将搜索到的结果展示出来 | |
| 466 | + $(".ser-resPanel-items li").click(function (ev) { | |
| 467 | + //$("#ser-input").val($(this).text());//点击某个li就会获取当前的值 | |
| 468 | + if (typeof (itemsSelectCallback) != "undefined") { | |
| 469 | + itemsSelectCallback($(this).text()); | |
| 470 | + } | |
| 471 | + commonClass.setPopupVisible(false); | |
| 472 | + }); | |
| 473 | + } else { | |
| 474 | + commonClass.setPopupVisible(false); | |
| 475 | + } | |
| 476 | + }; | |
| 477 | + }, | |
| 478 | + /* | |
| 479 | + @ setPopupVisible | |
| 480 | + @ state:true||false | |
| 481 | + */ | |
| 482 | + setPopupVisible: function (state) { | |
| 483 | + if (this._tmpXhr != null) { | |
| 484 | + this._tmpXhr.abort(); this._tmpXhr = null; | |
| 485 | + } | |
| 486 | + if (typeof (this._popID) == "undefined") return; | |
| 487 | + var obj = this._popID; | |
| 488 | + if (obj.css('display') == "none") return; | |
| 489 | + obj.empty(); | |
| 490 | + if (state) { | |
| 491 | + obj.show(); | |
| 492 | + } else { | |
| 493 | + obj.hide(); | |
| 494 | + } | |
| 495 | + } | |
| 496 | +} | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | ... | ... |