123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- angular.module('push')
- .factory('UtilService', function ($rootScope, $timeout, $http, ConfigService, $q) {
- var utilService = {};
- var toastflg = 0;
- //TODO 公共弹窗,提示信息用
- utilService.showMess = function (mess) {
- console.log(1);
- window.plugins.toast.show(mess, 'short', 'center', function (a) {
- }, function (b) {
- });
- // if (toastflg == 0) {
- // toastflg = 1;
- // console.log(2);
- // window.plugins.toast.show(mess, 'short', 'center', function (a) {
- // }, function (b) {
- // });
- // $timeout(function () {
- // toastflg = 0;
- // }, 200);
- // }
- };
- //Android:true,IOS:false
- utilService.checkPlatform = function () {
- return device.platform == "Android";
- };
- //格式化时间、时间戳
- utilService.formatDate = function () {
- var tempdate = new Date();
- return {
- timestamp: (Date.parse(tempdate) / 1000),
- formattime: tempdate.Format("yyyy-MM-dd hh:mm:ss"),
- formattime2: tempdate.Format("yyyy-MM-dd hh:mm:ss.S"),
- formattime3: tempdate.Format("yyyy年MM月dd日 hh:mm")
- };
- };
- //时间转时间戳
- utilService.reversalDate = function (str) {
- if (device.platform != "Android") {
- str = str.replace("-", "/");
- str = str.replace("-", "/");
- }
- var timestamp = Date.parse(new Date(str));
- timestamp = timestamp / 1000;
- return timestamp;
- };
- //时间戳转时间
- utilService.reversalTimestamp = function (str) {
- return new Date(parseInt(str) * 1000).Format("yyyy-MM-dd hh:mm:ss");
- };
- //yyyy年MM月dd日 转 yyyy-MM-dd
- utilService.formatTime = function (tempstr) {
- tempstr = tempstr.replace("年", "-");
- tempstr = tempstr.replace("月", "-");
- tempstr = tempstr.replace("日", "");
- return tempstr;
- };
- //获取当前日期{年,月,日,格式化日期,周}
- utilService.getNowDate = function () {
- //日期、星期初始化
- var week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"];
- //选中日期初始化
- var _date = new Date();
- var weekind = (_date.getDay() == 0) ? 6 : _date.getDay() - 1;
- var month = _date.getMonth() + 1;
- var day = _date.getDate();
- var tempn = _date.getFullYear() + "-";
- if (month < 10) {
- tempn = tempn + "0" + month + "-";
- } else {
- tempn = tempn + month + "-";
- }
- if (day < 10) {
- tempn = tempn + "0" + day;
- } else {
- tempn = tempn + day;
- }
- return {
- year: _date.getFullYear(),
- month: month,
- day: day,
- date: tempn,
- week: week[weekind]
- };
- };
- //时间比较 true:第一个时间更近 false:第二个时间更近
- utilService.compareDate = function (date1, date2) {
- var oDate1 = new Date(date1);
- var oDate2 = new Date(date2);
- return oDate1.getTime() > oDate2.getTime();
- };
- //格式化链接(提取链接中参数组成json)
- utilService.formatUrlToJson = function (url) {
- var temp1 = url.substring(url.lastIndexOf("?") + 1, url.length);
- var temp2 = temp1.replace(/=/g, "\":\"");
- var temp3 = "{\"" + temp2.replace(/&/g, "\",\"") + "\"}";
- return angular.fromJson(temp3);
- };
- //检测网络状态
- utilService.checkNetWork = function () {
- var networkState = navigator.connection.type;
- var states = {};
- states[Connection.UNKNOWN] = 'Unknown connection';
- states[Connection.ETHERNET] = 'Ethernet connection';
- states[Connection.WIFI] = 'WiFi connection';
- states[Connection.CELL_2G] = 'Cell 2G connection';
- states[Connection.CELL_3G] = 'Cell 3G connection';
- states[Connection.CELL_4G] = 'Cell 4G connection';
- states[Connection.CELL] = 'Cell generic connection';
- states[Connection.NONE] = 'No network connection';
- if (states[networkState] == 'WiFi connection') {
- return "WiFi";
- } else if (states[networkState] == 'No network connection') {
- return "None";
- } else {
- return "Other";
- }
- };
- var content = "";
- utilService.tempRep = function (type, str) {
- if (type == 0) {
- content = str.replace("\n", "<br>");
- if (content.indexOf("\n") != -1) {
- this.tempRep(0, content);
- }
- } else {
- content = str.replace("<br>", "\n");
- if (content.indexOf("<br>") != -1) {
- this.tempRep(1, content);
- }
- }
- };
- // 合并2个json
- utilService.concatJson = function (obj1, obj2) {
- var str1 = angular.toJson(obj1);
- str1 = str1.substr(0, str1.length - 1) + ",";
- var str2 = angular.toJson(obj2);
- str2 = str1 + str2.substr(1, str2.length);
- return angular.fromJson(str2);
- };
- //textarea中换行转<br>
- utilService.replaceEnterTag = function (str) {
- this.tempRep(0, str);
- return content;
- };
- //<br>转换行
- utilService.replaceHTMLTag = function (str) {
- this.tempRep(1, str);
- return content;
- };
- //检测6~20位数字字母组合密码
- utilService.checkPassword = function (str) {
- return (/^[0-9a-zA-Z]{6,20}$/.test(str));
- };
- //检查字符是否为空
- utilService.isDefined = function (str) {
- return (angular.isDefined(str) && str != null && str != "" && str != "null");
- };
- //检查手机号码
- utilService.isMobilePhone = function (str) {
- return ((/^[+0-9-()()]{7,18}$/.test(str)));
- // return ((/^1(3|4|5|7|8)\d{9}$/.test(str)));
- };
- //检查固定电话
- utilService.isTelePhone = function (tel) {
- return (/^0\d{2,3}-?\d{7,8}$/.test(tel));
- };
- //检测15/18位身份证号码
- utilService.isIdentityNumber = function (num) {
- return (/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/.test(num));
- };
- //检测邮箱
- utilService.isEmail = function (email) {
- return (/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(email));
- };
- //批量上传文件
- utilService.uploadFile = function (urls, filetype, mimeType) {
- //控制ajax请求异步变同步
- var deferred = $q.defer();
- var result = [];
- var upload = function (index) {
- if (index < urls.length) {
- var params = {};
- params.type = filetype;
- var options = new FileUploadOptions();
- options.mimeType = mimeType;
- options.fileKey = "file";
- options.fileName = urls[index].substr(urls[index].lastIndexOf('/') + 1);
- options.chunkedMode = false;
- options.params = params;
- var ft = new FileTransfer();
- // ft.upload(urls[index], ConfigService.server + "appUploadFile.action?", function win(r) {
- ft.upload(urls[index], ConfigService.server + "common/upload/attachment?", function win(r) {
- if (r.response == "null" || r.response == null || angular.isUndefined(r.response)) {
- deferred.reject();
- } else {
- var json = JSON.parse(r.response);
- result.push(json);
- upload(index + 1);
- }
- }, function fail(error) {
- console.log(error);
- result.push({photoName: "", originalName: ""});
- upload(index + 1);
- }, options);
- } else {
- deferred.resolve(result);
- }
- };
- upload(0);
- return deferred.promise;
- };
- //拍照、提取单张照片
- utilService.getPicture = function (srcType) {
- var deferred = $q.defer();
- var options = {
- quality: 50,
- destinationType: Camera.DestinationType.FILE_URI,
- sourceType: srcType, //0:相册 1:相机
- encodingType: Camera.EncodingType.JPEG,
- mediaType: Camera.MediaType.PICTURE,
- allowEdit: false,
- correctOrientation: true,
- saveToPhotoAlbum: true
- };
- navigator.camera.getPicture(function (imageData) {
- deferred.resolve(imageData);
- }, function (message) {
- deferred.reject(message);
- }, options);
- return deferred.promise;
- };
- //批量提取照片
- utilService.getPictureList = function (maxcount) {
- var deferred = $q.defer();
- window.imagePicker.getPictures(
- function (results) {
- deferred.resolve(results);
- }, function (error) {
- deferred.reject(error);
- }, {
- maximumImagesCount: maxcount,
- quality: 50
- }
- );
- return deferred.promise;
- };
- //http get请求
- utilService.urlget=function(url){
- return $http({
- method: "get",
- url: url
- });
- };
- //http get请求
- utilService.get = function (url, data) {
- return this._http(url, "get", data);
- };
- //http post请求
- utilService.post = function (url, data) {
- return this._http(url, "post", data);
- };
- utilService._http = function (url, method, data) {
- var json = {comefrom: ConfigService.comefrom, filterInputKey: 1, callback: "JSON_CALLBACK"};
- var jsondata = angular.merge(data, json);
- if (angular.equals(method, "get")) {
- return $http({
- method: "get",
- url: url,
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
- },
- params: jsondata,
- // timeout: 15000
- });
- } else if (angular.equals(method, "post")) {
- return $http({
- method: "post",
- url: url,
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
- },
- // timeout: 15000,
- transformRequest: function (jsondata) {
- /** * The workhorse; converts an object to x-www-form-urlencoded serialization. * @param {Object} obj * @return {String} */
- var param = function (obj) {
- var query = '';
- var name, value;
- for (name in obj) {
- value = obj[name];
- query += name + '=' + value + '&';
- }
- query = query.substr(0, query.length - 1);
- // console.log(url + query);
- // query = encodeURIComponent(query);
- return query.length ? query.substr(0, query.length - 1) : query;
- };
- return angular.isObject(jsondata) && String(jsondata) !== '[object File]' ? param(jsondata) : jsondata;
- },
- data: jsondata
- });
- }
- };
- //http get请求(IM服务器)
- utilService.imget = function (url, data,key) {
- return this._imhttp(url, "get", data,key);
- };
- // utilService.keyget=function(url,data,key){
- // return this._keyhttp(url,'get',data,key);
- // }
- //http post请求(IM服务器)
- utilService.impost = function (url, data) {
- return this._imhttp(url, "post", data);
- };
- utilService.getparams = function (url, method, data) {
- //发起请求
- return $http({
- method: get,
- url: url,
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
- },
- params: data,
- });
- };
- utilService._imhttp = function (url, method, data,keys) {
- //服务地址后拼接参数
- var jsondata = '';
- for (var key in data) {
- jsondata += '/' + data[key];
- }
- if(keys){
- jsondata +='?=key'+keys;
- }
- //发起请求
- return $http({
- method: method,
- url: url + jsondata,
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
- },
- // cache:true,
- // ifModified :true ,
- params: {},
- // timeout: 20000
- });
- };
- utilService.getparams = function (url, params) {
- // var jsondata = '';
- // angular.forEach(params,function (res,key){
- // if(!jsondata){
- // jsondata='?'+key+'='+res;
- // }else {
- // jsondata +='&'+key+'='+res;
- // }
- //
- // })
- return $http({
- method: 'get',
- url: url,
- headers: {
- 'Content-Type': 'application/json; charset=UTF-8'
- },
- params: params,
- // timeout: 20000
- });
- };
- // utilService._keyhttp = function (url, method, data,keys) {
- // //服务地址后拼接参数
- // var jsondata = '';
- // for (var key in data) {
- // jsondata += '/' + data[key];
- // }
- // if(keys){
- // jsondata+='?key='+keys;
- // }
- // //发起请求
- // return $http({
- // method: method,
- // url: url + jsondata,
- // headers: {
- // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
- // },
- // params: {},
- // timeout: 20000
- // });
- // };
- // 请求使用data
- utilService.imbodyhttp = function (url, data) {
- //发起请求
- return $http({
- method: "POST",
- url: url,
- dataType: 'json',
- headers: {
- 'Content-Type': 'application/json; charset=UTF-8'
- },
- params: {},
- data: data,
- });
- };
- // 请求使用data
- //上传
- utilService.Uploadhttp = function (url, data) {
- //发起请求
- return $http({
- method: "POST",
- url: url,
- data: data,
- headers: {'Content-Type': undefined},
- transformRequest: angular.identity,
- });
- };
- // 文件流下载
- utilService.downloadhttp = function (url, data) {
- //发起请求
- return $http({
- method: "POST",
- url: url,
- dataType: 'json',
- responseType: 'arraybuffer',
- headers: {
- 'Content-Type': 'application/json; charset=UTF-8'
- },
- params: {},
- data: data,
- // timeout: 15000
- });
- };
- // 文件流下载
- utilService.getdownload = function (url, data) {
- //发起请求
- return $http({
- method: "get",
- url: url,
- dataType: 'json',
- responseType: 'arraybuffer',
- headers: {
- 'Content-Type': 'application/json; charset=UTF-8'
- },
- params: {},
- data: data,
- });
- };
- return utilService;
- });
|