github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/app/script/app.js (about)

     1  /*
     2   * app JavaScript Library
     3   * Copyright (c) 2015-2018 xinsongkeji
     4   */
     5  // 上传图片
     6  // url:上传的url地址
     7  // method: GET/POST/DELETE/PUT
     8  // payload:post body
     9  function myajax(path, method, payload, callback){
    10  	//user_id = $api.getStorage("user_id");
    11  	
    12  	token = $api.getStorage("token");
    13  	console.log("token is " + token);
    14  	
    15  	if(method == 'GET'){
    16  		if(token == null || token == undefined || token == ''){
    17  			url = 'http://123.56.159.76/v1' + path;
    18  		} else if(path.indexOf("?") > -1) {
    19  			url = 'http://123.56.159.76/v1' + path + '&' + 'token=' + token;
    20  			//url = 'http://123.56.159.76/v1' + path
    21  		} else {
    22  			url = 'http://123.56.159.76/v1' + path + '?' + 'token=' + token;
    23  		}
    24  	}else{
    25  		url = 'http://123.56.159.76/v1' + path
    26  	}
    27  	if(method == 'GET'){
    28  		console.log(url + ' ' + method + ' ');
    29  		
    30  		api.ajax({
    31  			url : url,
    32  			method : 'get',
    33  			timeout : 30,
    34  			dataType : 'json',
    35  			returnAll : true
    36  		}, function(ret, err) {
    37  			//if token is timeout,login again
    38  			callback(ret,err);
    39  		});
    40  	} else if(method == "POST"){
    41  		mybody = JSON.stringify(payload);
    42  		console.log(url + ' ' + method + ' ' + mybody);
    43  		
    44      	api.ajax({
    45  	        url:url,
    46  	        method: 'post',
    47  		    timeout: 30,
    48  		    dataType: 'json',
    49  		    returnAll:true,
    50  		    headers:{
    51  		    	'Content-Type': 'application/json',
    52  		    	'token': token
    53  		    },
    54  		    data:{
    55  		    	body: mybody
    56  		    }
    57          }, function(ret, err) {
    58              //api.hideProgress();
    59              callback(ret, err);
    60          });
    61  	}
    62  }
    63  
    64  
    65  // 上传图片
    66  // url:上传的url地址
    67  // filepath:上传的文件
    68  // callback:上传成功返回地址
    69  function myajax_upload(path,myfiles, callback) {
    70  	post_id = $api.getStorage("user_id");
    71  	token = $api.getStorage("token");
    72  	//http_url = "http://123.56.159.76/v1/ay/lost/" + post_id + "/upload_imgs" ;
    73  	http_url = "http://123.56.159.76/v1/ay/upload_imgs?token=" + token;
    74  	api.ajax({
    75  		url : http_url,
    76  		method : 'post',
    77  		timeout : 30,
    78  		dataType : 'json',
    79  		returnAll : true,
    80  		data : {
    81  			files : {"file": myfiles}
    82  		}
    83  	}, function(ret, err) {
    84  		callback(ret, err);
    85  		/*if (ret) {
    86  	        	console.log(JSON.stringify(ret));
    87  	        	console.log(JSON.stringify(ret.body))
    88  				callback(ret.body.img_urls);
    89  	        	
    90  		} else {
    91  			api.alert({msg : ('错误码:' + err.code + ';错误信息:' + err.msg + ';网络状态码:' + err.statusCode)});
    92  		}*/
    93  	});
    94  }
    95  
    96  
    97  
    98  // 时间戳转时间
    99  function _formatDate(timespan) {
   100  	var now = new Date(timespan);
   101  	var year = now.getFullYear();
   102  	var month = now.getMonth() + 1;
   103  	var date = now.getDate();
   104  	var hour = now.getHours();
   105  	var minute = now.getMinutes();
   106  	var second = now.getSeconds();
   107  	return year + "-" + month + "-" + date + "   " + hour + ":" + minute + ":" + second;
   108  }
   109  
   110  // 滚动到底部
   111  function _scrollToEnd() {
   112  	document.getElementsByTagName('body')[0].scrollTop = document.getElementsByTagName('body')[0].scrollHeight;
   113  }
   114  
   115  // 广播事件
   116  function _sendEvent(eventName, extra) {
   117  	var _extra = ( typeof arguments[1] == "undefined" || arguments[1] == null) ? {} : arguments[1];
   118  	api.sendEvent({
   119  		name : eventName,
   120  		extra : _extra
   121  	});
   122  }
   123  
   124  // 监听事件
   125  function _addEventListener(eventName, _call, extra) {
   126  	var _extra = ( typeof arguments[2] == "undefined" || arguments[2] == null) ? {} : arguments[2];
   127  	api.addEventListener({
   128  		name : eventName,
   129  		extra : _extra
   130  	}, function(ret, err) {
   131  		if ( typeof _call == "function") {
   132  			_call(ret);
   133  		}
   134  	});
   135  }
   136  
   137  // 打开相册或相机
   138  //library            //图片库
   139  //camera            //相机
   140  //album            //相册
   141  //pic            //图片
   142  //video        //视频
   143  function _getPicture(_call, encodingType, mediaValue, destinationType, quality, targetWidth, targetHeight, saveToPhotoAlbum, allowEdit) {
   144  	var _encodingType = ( typeof arguments[1] == "undefined" || arguments[1] == null) ? "jpg" : arguments[1];
   145  	var _mediaValue = ( typeof arguments[2] == "undefined" || arguments[2] == null) ? "pic" : arguments[2];
   146  	var _destinationType = ( typeof arguments[3] == "undefined" || arguments[3] == null) ? "url" : arguments[3];
   147  	var _quality = ( typeof arguments[4] == "undefined" || arguments[4] == null) ? 50 : arguments[4];
   148  	var _targetWidth = ( typeof arguments[5] == "undefined" || arguments[5] == null) ? 320 : arguments[5];
   149  	var _targetHeight = ( typeof arguments[6] == "undefined" || arguments[6] == null) ? 320 : arguments[6];
   150  	var _saveToPhotoAlbum = ( typeof arguments[7] == "undefined" || arguments[7] == null) ? true : arguments[7];
   151  
   152  	api.actionSheet({
   153  		title : '您想要从哪里选取图片?',
   154  		cancelTitle : '取消',
   155  		buttons : ["优雅自拍", "相册收藏"]
   156  	}, function(ret, err) {
   157  		if (ret.buttonIndex == 3) {
   158  			return;
   159  		}
   160  		var sourceType = "album";
   161  		if (ret.buttonIndex == 1) {
   162  			sourceType = "camera";
   163  		}
   164  
   165  		api.getPicture({
   166  			sourceType : sourceType,
   167  			encodingType : _encodingType,
   168  			destinationType : _destinationType,
   169  			mediaValue : _mediaValue,
   170  			quality : _quality,
   171  			targetWidth : _targetWidth,
   172  			targetHeight : _targetHeight,
   173  			saveToPhotoAlbum : _saveToPhotoAlbum
   174  		}, function(ret, err) {
   175  			if (ret) {
   176  				if ( typeof _call == "function") {
   177  					_call(ret.data);
   178  				}
   179  			} else {
   180  				api.alert({
   181  					msg : err.msg
   182  				});
   183  			}
   184  		});
   185  	});
   186  
   187  }
   188  // 根据出生日期算年龄
   189  function ages(str) {
   190  	var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
   191  	if (r == null)
   192  		return false;
   193  	var d = new Date(r[1], r[3] - 1, r[4]);
   194  	if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {
   195  		var Y = new Date().getFullYear();
   196  		return (Y - r[1]);
   197  	}
   198  	return 0;
   199  }
   200  
   201  // 获取文件拓展名
   202  function _getExt(fileName) {
   203  	return fileName.substring(fileName.lastIndexOf('.') + 1);
   204  }
   205  
   206  // PHP时间戳转时间
   207  function _trans_php_time_to_str(timestamp, n) {
   208  	update = new Date(timestamp * 1000);
   209  	//时间戳要乘1000
   210  	year = update.getFullYear();
   211  	month = (update.getMonth() + 1 < 10) ? ('0' + (update.getMonth() + 1)) : (update.getMonth() + 1);
   212  	day = (update.getDate() < 10) ? ('0' + update.getDate()) : (update.getDate());
   213  	hour = (update.getHours() < 10) ? ('0' + update.getHours()) : (update.getHours());
   214  	minute = (update.getMinutes() < 10) ? ('0' + update.getMinutes()) : (update.getMinutes());
   215  	second = (update.getSeconds() < 10) ? ('0' + update.getSeconds()) : (update.getSeconds());
   216  	if (n == 1) {
   217  		return (year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second);
   218  	} else if (n == 2) {
   219  		return (year + '-' + month + '-' + day);
   220  	} else {
   221  		return 0;
   222  	}
   223  }
   224  
   225  function _uuid() {
   226      var s = [];
   227      var hexDigits = "0123456789abcdef";
   228      for (var i = 0; i < 32; i++) {
   229          s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
   230      }
   231      //s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
   232      //s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
   233      //s[8] = s[13] = s[18] = s[23] = "-";
   234  
   235      var uuid = s.join("");
   236      return uuid;
   237  }
   238  
   239  // 生成guid,主要用于生成随机文件名
   240  function _newGuid() {
   241  	function S4() {
   242  		return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
   243  	}
   244  
   245  	return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
   246  }
   247  
   248  // 获取当前的时间,拼接成2015-11-09这样的格式,主要用于对图片进行时间分类
   249  function _getNowFormatDate() {
   250  	var date = new Date();
   251  	var seperator1 = "-";
   252  	var year = date.getFullYear();
   253  	var month = date.getMonth() + 1;
   254  	var strDate = date.getDate();
   255  	if (month >= 1 && month <= 9) {
   256  		month = "0" + month;
   257  	}
   258  	if (strDate >= 0 && strDate <= 9) {
   259  		strDate = "0" + strDate;
   260  	}
   261  	var currentdate = year + seperator1 + month + seperator1 + strDate
   262  	return currentdate;
   263  }
   264  
   265  // 判断是否是IOS
   266  function _isIOS() {
   267  	return api.systemType == "ios" ? true : false;
   268  }
   269  
   270  // 图片压缩
   271  // imageFilter:压缩对象
   272  // imgsrc:源图片的路径
   273  // quality:图片压缩质量,一般建议0.5
   274  // scale:图片压缩比例,也是建议0.5
   275  // ext:源图片拓展名
   276  // callback:转换成功之后回调函数
   277  function _imgCompress(imageFilter, imgsrc, quality, scale, ext, callback) {
   278  	// 压缩文件的保存目录
   279  	var savePath = api.cacheDir + "/" + _getNowFormatDate() + "/";
   280  	// 压缩文件生成的随机文件名称
   281  	var savename = _newGuid() + "." + ext;
   282  	imageFilter.compress({
   283  		img : imgsrc,
   284  		quality : quality,
   285  		scale : quality,
   286  		save : {
   287  			album : false,
   288  			imgPath : savePath,
   289  			imgName : savename
   290  		}
   291  	}, function(ret, err) {
   292  		if (ret) {
   293  			callback(savePath + savename);
   294  		} else {
   295  			alert(JSON.stringify(err));
   296  		}
   297  	});
   298  }		
   299  // 解决状态栏重合
   300  function _fixStatusBar(headerid, callback) {
   301  	var header = $api.byId(headerid);
   302  	var systemType = api.systemType;
   303  	var systemVersion = parseFloat(api.systemVersion);
   304  	if (systemType == "ios" || (systemType == "android" && systemVersion >= 4.4)) {
   305  		if (systemType == "android") {
   306  			header.style.paddingTop = '25px';
   307  		}
   308  		$api.fixStatusBar(header);
   309  	} else {
   310  		$api.fixIos7Bar(header);
   311  	}
   312  	var headerPos = $api.offset(header);
   313  	if ( typeof callback == "function") {
   314  		callback(headerPos);
   315  	}
   316  }
   317  
   318  
   319  
   320  // 下拉刷新
   321  function _setRefreshHeaderInfo(_call, bgColor, textColor) {
   322  	var _bgColor = ( typeof arguments[1] == "undefined" || arguments[1] == null) ? '#f1f1f1' : arguments[1];
   323  	var _textColor = ( typeof arguments[2] == "undefined" || arguments[2] == null) ? '#999' : arguments[2];
   324  	api.setRefreshHeaderInfo({
   325  		visible : true,
   326  		loadingImg : 'widget://image/refresh.png',
   327  		bgColor : _bgColor,
   328  		textColor : _textColor,
   329  		textDown : '下拉刷新...',
   330  		textUp : '松开刷新...',
   331  		showTime : true
   332  	}, function(ret, err) {
   333  		if ( typeof _call == "function") {
   334  			_call();
   335  		}
   336  	});
   337  }
   338  
   339  // 上拉加载
   340  function _scrolltobottom(_call, threshold) {
   341  	var _threshold = ( typeof arguments[1] == "undefined" || arguments[1] == null) ? 0 : arguments[1];
   342  	_addEventListener('scrolltobottom', _call, {
   343  		threshold : _threshold
   344  	});
   345  }
   346  
   347  
   348  // 打开新框架
   349  function _openFrame(name, url, rect, pageParam, bgColor, bounces, reload, showProgress) {
   350  	var __rect = {
   351  		x : 0,
   352  		y : 0,
   353  		w : api.winWidth,
   354  		y : api.winHeight,
   355  		l : 0,
   356  		t : 0,
   357  		b : 0,
   358  		r : 0
   359  	};
   360  	var _rect = ( typeof arguments[2] == "undefined" || arguments[2] == null || typeof arguments[2] != "object") ? __rect : arguments[2];
   361  	var _pageParam = ( typeof arguments[3] == "undefined" || arguments[3] == null) ? api.pageParam : arguments[3];
   362  	var _bgColor = ( typeof arguments[4] == "undefined" || arguments[4] == null) ? 'rgba(0,0,0,0)' : arguments[4];
   363  	var _bounces = ( typeof arguments[5] == "undefined" || arguments[5] == null) ? false : arguments[5];
   364  	var _reload = ( typeof arguments[6] == "undefined" || arguments[6] == null) ? false : arguments[6];
   365  	var _showProgress = ( typeof arguments[7] == "undefined" || arguments[7] == null) ? false : arguments[7];
   366  	api.openFrame({
   367  		name : name,
   368  		url : url,
   369  		rect : {
   370  			x : typeof _rect.x != "number" ? __rect.x : _rect.x,
   371  			y : typeof _rect.y != "number" ? __rect.y : _rect.y,
   372  			w : typeof _rect.w != "number" ? __rect.w : _rect.w,
   373  			h : typeof _rect.h != "number" ? __rect.h : _rect.h,
   374  			marginLeft : typeof _rect.l != "number" ? __rect.l : _rect.l,
   375  			marginTop : typeof _rect.t != "number" ? __rect.t : _rect.t,
   376  			marginBottom : typeof _rect.b != "number" ? __rect.b : _rect.b,
   377  			marginRight : typeof _rect.r != "number" ? __rect.r : _rect.r
   378  		},
   379  		bgColor : _bgColor,
   380  		pageParam : _pageParam,
   381  		scrollToTop : true,
   382  		bounces : _bounces,
   383  		showProgress : _showProgress,
   384  		reload : _reload,
   385  		vScrollBarEnabled : false,
   386  		hScrollBarEnabled : false,
   387  		allowEdit : true
   388  	});
   389  }
   390  // 小提示
   391  function _toast(msg, duration, location, _call) {
   392  	var _duration = ( typeof arguments[1] == "undefined" || arguments[1] == null) ? window.toastDuration : arguments[1];
   393  	var _location = ( typeof arguments[2] == "undefined" || arguments[2] == null) ? window.toastLocation : arguments[2];
   394  	api.toast({
   395  		msg : msg,
   396  		duration : _duration,
   397  		location : _location
   398  	});
   399  	if ( typeof _call == "function") {
   400  		setTimeout(_call, duration);
   401  	}
   402  }
   403  
   404  // 加载框
   405  function _showProgress(title, text, modal, animationType) {
   406  	var _title = ( typeof arguments[0] == "undefined" || arguments[0] == null) ? window.showProgressTitle : arguments[0];
   407  	var _text = ( typeof arguments[1] == "undefined" || arguments[1] == null) ? window.showProgressText : arguments[1];
   408  	var _modal = ( typeof arguments[2] == "undefined" || arguments[2] == null) ? true : arguments[2];
   409  	var _animationType = ( typeof arguments[3] == "undefined" || arguments[3] == null) ? window.showProgressAnimationType : arguments[3];
   410  	api.showProgress({
   411  		style : 'default',
   412  		animationType : _animationType,
   413  		title : _title,
   414  		text : _text,
   415  		modal : _modal
   416  	});
   417  }