plus.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * 日期格式化
  3. * @param {Object} fmt 格式化字符串
  4. */
  5. Date.prototype.Format = function(fmt) {
  6. var o = {
  7. "M+": this.getMonth() + 1, //月份
  8. "d+": this.getDate(), //日
  9. "H+": this.getHours(), //小时
  10. "m+": this.getMinutes(), //分
  11. "s+": this.getSeconds(), //秒
  12. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  13. "S": this.getMilliseconds() //毫秒
  14. };
  15. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  16. for (var k in o)
  17. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((
  18. "00" + o[
  19. k]).substr(("" + o[k]).length)));
  20. return fmt;
  21. }
  22. let watchGpsId = undefined;
  23. /**
  24. * 开启GPS位置变化监听
  25. * @param {Function} callChange GPS位置变化回调callChange(纬度,经度);
  26. */
  27. function plusGPSListenerOn(callChange) {
  28. mui.plusReady(function() {
  29. watchGpsId = plus.geolocation.watchPosition(function(p) {
  30. if (callChange) callChange(p.coords.latitude, p.coords.longitude);
  31. }, function(err) {
  32. mui.alert('定位错误 = ' + err.message);
  33. }, {
  34. provider: 'system',
  35. coordsType: 'wgs84',
  36. geocode: false,
  37. enableHighAccuracy: true, //这个布尔值用来表明应用是否使用其最高精度来表示结果。如果值为 true ,同时设备能够提供一个更精确的位置,那么设备就会使用这个位置
  38. maximumAge: 0, //它表明可以返回多长时间(即最长年龄,单位毫秒)内的可获取的缓存位置。如果设置为 0, 说明设备不能使用一个缓存位置
  39. });
  40. })
  41. }
  42. /**
  43. * 关闭GPS位置监听
  44. * @param {Function} callCompletion 完成回到callCompletion()
  45. */
  46. function plusGPSListenerOff(callCompletion) {
  47. if (watchGpsId) {
  48. plus.geolocation.clearWatch(watchGpsId);
  49. watchGpsId = undefined;
  50. if (callCompletion) callCompletion('GPS定位关闭成功!');
  51. } else {
  52. if (callCompletion) callCompletion('GPS定位尚未开启!');
  53. }
  54. }
  55. /**
  56. * 调用相机拍照
  57. * @param {JSON} options 配置项
  58. * @param {String} options.name 照片名称,不允许重复
  59. * @param {Function} callSuccess 成功回调callSuccess({data:path:})
  60. * @param {Function} callError 失败回调callError(message)
  61. */
  62. function plusCamera(options, callSuccess, callError) {
  63. let _self = this;
  64. let strDate = new Date().Format('yyyyMMddHHmmss');
  65. /* 如果未传递,则自定义照片名称 */
  66. let photoName = options && options.name ? options.name : 'plus' + '_' + strDate;
  67. let tName = photoName + '_temp_' + '.jpg';
  68. photoName += '.jpg';
  69. /* 获取相机 */
  70. let camera = plus.camera.getCamera();
  71. camera.captureImage(
  72. function(path) {
  73. /* 存储一个照片 */
  74. plus.gallery.save(path, function() {
  75. /* 压缩一个缩略图 给展示照片用 */
  76. plus.zip.compressImage({
  77. src: path,
  78. dst: '_doc/camera/' + tName,
  79. quality: 20
  80. },
  81. function() {
  82. plus.io.resolveLocalFileSystemURL(
  83. '_doc/camera/' + tName,
  84. function(entry) {
  85. entry.file(function(file) {
  86. let reader = new plus.io.FileReader();
  87. reader.onload = function(e) {
  88. if (callSuccess) callSuccess({
  89. data: e.target.result,
  90. path: path,
  91. })
  92. };
  93. reader.readAsDataURL(file);
  94. });
  95. },
  96. function(e) {
  97. if (callError) callError("路径转换失败!" + e.message);
  98. }
  99. );
  100. },
  101. function(e) {
  102. if (callError) callError("照片压缩失败!" + e.message);
  103. }
  104. );
  105. },
  106. function(e) {
  107. if (callError) callError("照片存储失败!" + e.message);
  108. }
  109. );
  110. },
  111. function(e) {
  112. if (callError) callError('取消拍照!');
  113. }, {
  114. filename: '_doc/camera/' + photoName,
  115. index: 1
  116. }
  117. );
  118. }
  119. /**
  120. * 浏览本地照片
  121. * @param {String} path 照片路径
  122. * @param {Function} callError 错误回调callError(message)
  123. */
  124. function plusPreviewImage(path, callError) {
  125. plus.io.resolveLocalFileSystemURL(path, function(entry) {
  126. plus.nativeUI.previewImage([entry.toLocalURL()], {
  127. indicator: 'number',
  128. loop: true,
  129. });
  130. }, function(e) {
  131. if (callError) callError("路径转换失败!" + e.message);
  132. });
  133. }
  134. /**
  135. * 存储键值
  136. * @param {JSON} options 配置项
  137. * @param {String} options.key 键
  138. * @param {String} options.value 值
  139. * @param {Function} options.success 成功回调
  140. * @param {Function} options.error 错误回调error(String)
  141. */
  142. function plusSaveStorage(options) {
  143. // mui.plusReady(function() {
  144. // plus.storage.setItemAsync(options.key, options.value, function(obj) {
  145. // if (options.success) options.success();
  146. // }, function(err) {
  147. // if (options.error) options.error(err.message);
  148. // });
  149. // });
  150. localStorage.setItem(options.key, JSON.stringify({
  151. data: options.value
  152. }));
  153. if (options.success) options.success();
  154. }
  155. /**
  156. * 获取键值
  157. * @param {JSON} options 配置项
  158. * @param {String} options.key 键
  159. * @param {Function} options.success 成功回调success(String)
  160. * @param {Function} options.error 错误回调error(String)
  161. */
  162. function plusGetStorage(options) {
  163. // mui.plusReady(function() {
  164. // plus.storage.getItemAsync(options.key, function(obj) {
  165. // /* 成功获取 JSON data中存储值 */
  166. // if (options.success) options.success(obj.data);
  167. // }, function(err) {
  168. // if (options.error) options.error(err.message);
  169. // });
  170. // });
  171. let obj = localStorage.getItem(options.key);
  172. if (obj) {
  173. if (options.success) options.success(JSON.parse(obj).data);
  174. } else {
  175. if (options.error) options.error("未找到对应键值!");
  176. }
  177. }
  178. /**
  179. * 设置Ip和端口
  180. * @param {JSON} options 配置项
  181. * @param {String} options.ip IP地址
  182. * @param {String} options.com 端口
  183. * @param {Function} options.success 成功回调success()
  184. * @param {Function} options.error 错误回调error(String)
  185. */
  186. function plusSetIpAndCom(options) {
  187. let ipAndCom = {
  188. ip: options.ip,
  189. com: options.com
  190. }
  191. plusSaveStorage({
  192. key: 'ipandcom',
  193. value: JSON.stringify(ipAndCom),
  194. success: options.success,
  195. error: options.error
  196. })
  197. }
  198. /**
  199. * @param {JSON} options 配置项
  200. * @param {Function} options.success 成功回调success(JSON)
  201. * @param {Function} options.error 错误回调error(String)
  202. */
  203. function plusGetIpAndCom(options) {
  204. plusGetStorage({
  205. key: 'ipandcom',
  206. success: function(res) {
  207. let icJSON = JSON.parse(res);
  208. if (options.success) options.success({
  209. ip: icJSON.ip,
  210. com: icJSON.com
  211. })
  212. },
  213. error: options.error
  214. })
  215. }
  216. let NET_ERR = "RES_ERR";
  217. let NET_SUCCESS = "RES_OK";
  218. let NET_NO = "RES_NO";
  219. /* 测试版虚拟目录 */
  220. let publishURL = 'LyLSQInterface_war_exploded';
  221. publishURL = 'LyLSQInterface';
  222. /**
  223. * 获取服务器URL地址
  224. * @param {String} ashxName 接口名称
  225. * @param {Function} callSuccess 成功回调callSuccess(url)
  226. * @param {Function} callError 失败回调callError(String)
  227. */
  228. function _getServerURL(ashxName, callSuccess, callError) {
  229. plusGetIpAndCom({
  230. success: function(res) {
  231. var url = "http://" + res.ip + ":" + res.com + "/" + publishURL + "/" + ashxName;
  232. if (callSuccess) callSuccess(url)
  233. },
  234. error: function(err) {
  235. if (callError) callError("尚未设置IP地址和端口!");
  236. }
  237. });
  238. }
  239. /**
  240. * 分析错误代码
  241. * @param {String} code 错误代码
  242. * @return {String} 错误描述
  243. */
  244. function _analyzingErrorCode(code) {
  245. var res = "未知错误!";
  246. switch (code) {
  247. case 0:
  248. res = "服务连接异常!";
  249. break;
  250. case 203:
  251. res = "非权威性信息!";
  252. break;
  253. case 401:
  254. res = "访问被拒绝!";
  255. break;
  256. case 401.1:
  257. res = "登录失败!";
  258. break;
  259. default:
  260. res = "其他错误!" + code;
  261. break;
  262. }
  263. return res;
  264. }
  265. /**
  266. * 通过ajax获取信息
  267. * @param {JSON} options 配置项
  268. * @param {String} options.ashxName 接口名称
  269. * @param {JSON} options.data 提交数据{JSON}
  270. * @param {Function} options.success 成功回调success(JSON)
  271. * @param {Function} options.error 失败回调error(message)
  272. * @param {Function} options.nodata 没有数据回调nodata(message)
  273. */
  274. function sendAjax(options) {
  275. /* 统一方式 */
  276. _getServerURL(options.ashxName, function(url) {
  277. /* 如果设置了等待消息 则显示 */
  278. // if (options.waitMessage) plus.nativeUI.showWaiting(options.waitMessage);
  279. if (options.waitMessage && options.loading) options.loading(options.waitMessage);
  280. mui.ajax(url, {
  281. data: options.data == undefined ? {} : options.data,
  282. dataType: 'json',
  283. type: options.type == undefined ? "post" : options.type,
  284. timeout: 45000,
  285. async: true,
  286. success: function(resData) {
  287. if (options.complete) options.complete();
  288. if (resData == null) {
  289. if (options.error) options.error("连接失败...");
  290. return;
  291. }
  292. if (resData.res == 'RES_OK') {
  293. if (options.success) options.success(resData.data);
  294. } else if (resData.res == 'RES_NO') {
  295. if (options.nodata) options.nodata(resData.data[0].msg);
  296. } else {
  297. if (options.error) options.error(resData.data[0].msg);
  298. }
  299. },
  300. error: function(xhr, type, errorThrown) {
  301. // plus.nativeUI.closeWaiting();
  302. if (options.complete) options.complete();
  303. if (options.error) options.error(_analyzingErrorCode(xhr.status));
  304. }
  305. });
  306. }, function(err) {
  307. if (options.error) options.error(err);
  308. });
  309. }