123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- /**
- * 日期格式化
- * @param {Object} fmt 格式化字符串
- */
- Date.prototype.Format = function(fmt) {
- 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;
- }
- let watchGpsId = undefined;
- /**
- * 开启GPS位置变化监听
- * @param {Function} callChange GPS位置变化回调callChange(纬度,经度);
- */
- function plusGPSListenerOn(callChange) {
- mui.plusReady(function() {
- watchGpsId = plus.geolocation.watchPosition(function(p) {
- if (callChange) callChange(p.coords.latitude, p.coords.longitude);
- }, function(err) {
- mui.alert('定位错误 = ' + err.message);
- }, {
- provider: 'system',
- coordsType: 'wgs84',
- geocode: false,
- enableHighAccuracy: true, //这个布尔值用来表明应用是否使用其最高精度来表示结果。如果值为 true ,同时设备能够提供一个更精确的位置,那么设备就会使用这个位置
- maximumAge: 0, //它表明可以返回多长时间(即最长年龄,单位毫秒)内的可获取的缓存位置。如果设置为 0, 说明设备不能使用一个缓存位置
- });
- })
- }
- /**
- * 关闭GPS位置监听
- * @param {Function} callCompletion 完成回到callCompletion()
- */
- function plusGPSListenerOff(callCompletion) {
- if (watchGpsId) {
- plus.geolocation.clearWatch(watchGpsId);
- watchGpsId = undefined;
- if (callCompletion) callCompletion('GPS定位关闭成功!');
- } else {
- if (callCompletion) callCompletion('GPS定位尚未开启!');
- }
- }
- /**
- * 调用相机拍照
- * @param {JSON} options 配置项
- * @param {String} options.name 照片名称,不允许重复
- * @param {Function} callSuccess 成功回调callSuccess({data:path:})
- * @param {Function} callError 失败回调callError(message)
- */
- function plusCamera(options, callSuccess, callError) {
- let _self = this;
- let strDate = new Date().Format('yyyyMMddHHmmss');
- /* 如果未传递,则自定义照片名称 */
- let photoName = options && options.name ? options.name : 'plus' + '_' + strDate;
- let tName = photoName + '_temp_' + '.jpg';
- photoName += '.jpg';
- /* 获取相机 */
- let camera = plus.camera.getCamera();
- camera.captureImage(
- function(path) {
- /* 存储一个照片 */
- plus.gallery.save(path, function() {
- /* 压缩一个缩略图 给展示照片用 */
- plus.zip.compressImage({
- src: path,
- dst: '_doc/camera/' + tName,
- quality: 20
- },
- function() {
- plus.io.resolveLocalFileSystemURL(
- '_doc/camera/' + tName,
- function(entry) {
- entry.file(function(file) {
- let reader = new plus.io.FileReader();
- reader.onload = function(e) {
- if (callSuccess) callSuccess({
- data: e.target.result,
- path: path,
- })
- };
- reader.readAsDataURL(file);
- });
- },
- function(e) {
- if (callError) callError("路径转换失败!" + e.message);
- }
- );
- },
- function(e) {
- if (callError) callError("照片压缩失败!" + e.message);
- }
- );
- },
- function(e) {
- if (callError) callError("照片存储失败!" + e.message);
- }
- );
- },
- function(e) {
- if (callError) callError('取消拍照!');
- }, {
- filename: '_doc/camera/' + photoName,
- index: 1
- }
- );
- }
- /**
- * 浏览本地照片
- * @param {String} path 照片路径
- * @param {Function} callError 错误回调callError(message)
- */
- function plusPreviewImage(path, callError) {
- plus.io.resolveLocalFileSystemURL(path, function(entry) {
- plus.nativeUI.previewImage([entry.toLocalURL()], {
- indicator: 'number',
- loop: true,
- });
- }, function(e) {
- if (callError) callError("路径转换失败!" + e.message);
- });
- }
- /**
- * 存储键值
- * @param {JSON} options 配置项
- * @param {String} options.key 键
- * @param {String} options.value 值
- * @param {Function} options.success 成功回调
- * @param {Function} options.error 错误回调error(String)
- */
- function plusSaveStorage(options) {
- // mui.plusReady(function() {
- // plus.storage.setItemAsync(options.key, options.value, function(obj) {
- // if (options.success) options.success();
- // }, function(err) {
- // if (options.error) options.error(err.message);
- // });
- // });
- localStorage.setItem(options.key, JSON.stringify({
- data: options.value
- }));
- if (options.success) options.success();
- }
- /**
- * 获取键值
- * @param {JSON} options 配置项
- * @param {String} options.key 键
- * @param {Function} options.success 成功回调success(String)
- * @param {Function} options.error 错误回调error(String)
- */
- function plusGetStorage(options) {
- // mui.plusReady(function() {
- // plus.storage.getItemAsync(options.key, function(obj) {
- // /* 成功获取 JSON data中存储值 */
- // if (options.success) options.success(obj.data);
- // }, function(err) {
- // if (options.error) options.error(err.message);
- // });
- // });
- let obj = localStorage.getItem(options.key);
- if (obj) {
- if (options.success) options.success(JSON.parse(obj).data);
- } else {
- if (options.error) options.error("未找到对应键值!");
- }
- }
- /**
- * 设置Ip和端口
- * @param {JSON} options 配置项
- * @param {String} options.ip IP地址
- * @param {String} options.com 端口
- * @param {Function} options.success 成功回调success()
- * @param {Function} options.error 错误回调error(String)
- */
- function plusSetIpAndCom(options) {
- let ipAndCom = {
- ip: options.ip,
- com: options.com
- }
- plusSaveStorage({
- key: 'ipandcom',
- value: JSON.stringify(ipAndCom),
- success: options.success,
- error: options.error
- })
- }
- /**
- * @param {JSON} options 配置项
- * @param {Function} options.success 成功回调success(JSON)
- * @param {Function} options.error 错误回调error(String)
- */
- function plusGetIpAndCom(options) {
- plusGetStorage({
- key: 'ipandcom',
- success: function(res) {
- let icJSON = JSON.parse(res);
- if (options.success) options.success({
- ip: icJSON.ip,
- com: icJSON.com
- })
- },
- error: options.error
- })
- }
- let NET_ERR = "RES_ERR";
- let NET_SUCCESS = "RES_OK";
- let NET_NO = "RES_NO";
- /* 测试版虚拟目录 */
- let publishURL = 'LyLSQInterface_war_exploded';
- publishURL = 'LyLSQInterface';
- /**
- * 获取服务器URL地址
- * @param {String} ashxName 接口名称
- * @param {Function} callSuccess 成功回调callSuccess(url)
- * @param {Function} callError 失败回调callError(String)
- */
- function _getServerURL(ashxName, callSuccess, callError) {
- plusGetIpAndCom({
- success: function(res) {
- var url = "http://" + res.ip + ":" + res.com + "/" + publishURL + "/" + ashxName;
- if (callSuccess) callSuccess(url)
- },
- error: function(err) {
- if (callError) callError("尚未设置IP地址和端口!");
- }
- });
- }
- /**
- * 分析错误代码
- * @param {String} code 错误代码
- * @return {String} 错误描述
- */
- function _analyzingErrorCode(code) {
- var res = "未知错误!";
- switch (code) {
- case 0:
- res = "服务连接异常!";
- break;
- case 203:
- res = "非权威性信息!";
- break;
- case 401:
- res = "访问被拒绝!";
- break;
- case 401.1:
- res = "登录失败!";
- break;
- default:
- res = "其他错误!" + code;
- break;
- }
- return res;
- }
- /**
- * 通过ajax获取信息
- * @param {JSON} options 配置项
- * @param {String} options.ashxName 接口名称
- * @param {JSON} options.data 提交数据{JSON}
- * @param {Function} options.success 成功回调success(JSON)
- * @param {Function} options.error 失败回调error(message)
- * @param {Function} options.nodata 没有数据回调nodata(message)
- */
- function sendAjax(options) {
- /* 统一方式 */
- _getServerURL(options.ashxName, function(url) {
- /* 如果设置了等待消息 则显示 */
- // if (options.waitMessage) plus.nativeUI.showWaiting(options.waitMessage);
- if (options.waitMessage && options.loading) options.loading(options.waitMessage);
- mui.ajax(url, {
- data: options.data == undefined ? {} : options.data,
- dataType: 'json',
- type: options.type == undefined ? "post" : options.type,
- timeout: 45000,
- async: true,
- success: function(resData) {
- if (options.complete) options.complete();
- if (resData == null) {
- if (options.error) options.error("连接失败...");
- return;
- }
- if (resData.res == 'RES_OK') {
- if (options.success) options.success(resData.data);
- } else if (resData.res == 'RES_NO') {
- if (options.nodata) options.nodata(resData.data[0].msg);
- } else {
- if (options.error) options.error(resData.data[0].msg);
- }
- },
- error: function(xhr, type, errorThrown) {
- // plus.nativeUI.closeWaiting();
- if (options.complete) options.complete();
- if (options.error) options.error(_analyzingErrorCode(xhr.status));
- }
- });
- }, function(err) {
- if (options.error) options.error(err);
- });
- }
|