common.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*+++++通用函数脚本++++++++*/
  2. var colorBackground = "#3255ee";
  3. var colorTitle = "#FFFFFF";
  4. var colorLine = "#323af1";
  5. /**
  6. * APP 获取GPS位置
  7. * @param {Object} callback 成功获取后回调
  8. */
  9. function plusGetLocation(callback) {
  10. mui.plusReady(function() {
  11. //获取位置信息
  12. plus.geolocation.getCurrentPosition(function(position) {
  13. var lng = position.coords.longitude;
  14. var lat = position.coords.latitude;
  15. callback(lat, lng);
  16. }, function(e) {
  17. mui.alert(e.message);
  18. }, {
  19. provider: 'system',
  20. coordsType: 'wgs84',
  21. geocode: false,
  22. enableHighAccuracy: true, //这个布尔值用来表明应用是否使用其最高精度来表示结果。如果值为 true ,同时设备能够提供一个更精确的位置,那么设备就会使用这个位置
  23. maximumAge: 0, //它表明可以返回多长时间(即最长年龄,单位毫秒)内的可获取的缓存位置。如果设置为 0, 说明设备不能使用一个缓存位置
  24. });
  25. });
  26. }
  27. /**
  28. * 加载顶部导航栏
  29. * @param {Object} title 导航栏标题
  30. * @param {Object} showBackButton 导航栏是否具备返回按钮
  31. */
  32. function plusSetNavbar(title, showBackButton) {
  33. var ws = plus.webview.currentWebview();
  34. //设置导航条
  35. ws.setStyle({
  36. "titleNView": {
  37. backgroundColor: colorBackground,
  38. titleText: title,
  39. titleColor: colorTitle,
  40. autoBackButton: showBackButton,
  41. splitLine: {
  42. color: colorLine
  43. }
  44. }
  45. });
  46. }
  47. /**
  48. * 加载顶部导航栏
  49. * @param {Object} title 导航栏标题
  50. * @param {Object} showBackButton 导航栏是否具备返回按钮
  51. */
  52. function plusSetNavbarRef(title, showBackButton, callback) {
  53. var ws = plus.webview.currentWebview();
  54. //设置导航条
  55. ws.setStyle({
  56. "titleNView": {
  57. backgroundColor: colorBackground,
  58. titleText: title,
  59. titleColor: colorTitle,
  60. autoBackButton: showBackButton,
  61. splitLine: {
  62. color: colorLine
  63. },
  64. buttons: [{
  65. 'float': 'right',
  66. fontSrc: '../fonts/iconfont.ttf',
  67. text: '\ueb73',
  68. onclick: callback,
  69. }]
  70. }
  71. });
  72. }
  73. /**
  74. * UIWebView加载完成 大多数方法建议放到此处运行
  75. * @param {Object} callback
  76. */
  77. function UIView_DidLoad(callback) {
  78. mui.plusReady(function() {
  79. // var self = plus.webview.currentWebview();
  80. // //添加显示监听
  81. // self.addEventListener('show', function() {
  82. callback();
  83. // });
  84. });
  85. }
  86. /**
  87. * 输出控制台消息
  88. * @param {Object} msg 消息内容
  89. */
  90. function printToConsole(msg) {
  91. console.log(msg);
  92. }
  93. /**
  94. * 日期格式化
  95. * @param {Object} fmt 格式化字符串
  96. */
  97. Date.prototype.Format = function(fmt) {
  98. var o = {
  99. "M+": this.getMonth() + 1, //月份
  100. "d+": this.getDate(), //日
  101. "H+": this.getHours(), //小时
  102. "m+": this.getMinutes(), //分
  103. "s+": this.getSeconds(), //秒
  104. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  105. "S": this.getMilliseconds() //毫秒
  106. };
  107. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  108. for (var k in o)
  109. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((
  110. "00" + o[
  111. k]).substr(("" + o[k]).length)));
  112. return fmt;
  113. }
  114. /**
  115. * @param {JSON} options 图片路径
  116. * path{string}:图片路径
  117. * lable{string}:文字描述
  118. * click{function}:回调函数
  119. */
  120. function createBigMenu(options) {
  121. var li = document.createElement("li");
  122. li.className = 'mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3';
  123. var liHtml = "";
  124. liHtml += "<div class='cr-menu-panel'>";
  125. liHtml += "<img src='" + options.path + "'>";
  126. liHtml += "<span>" + options.label + "</span>";
  127. liHtml += "</div>";
  128. li.innerHTML = liHtml;
  129. if (options.click != undefined) li.onclick = function() {
  130. options.click();
  131. };
  132. return li;
  133. }
  134. /**
  135. * @param {JSON} options 图片路径
  136. * path{string}:图片路径
  137. * lable{string}:文字描述
  138. * click{function}:回调函数
  139. */
  140. function createSmartMenu(options) {
  141. var li = document.createElement("li");
  142. li.className = 'mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3';
  143. var liHtml = "";
  144. liHtml += "<div class='cr-menu-smart-panel'>";
  145. liHtml += "<img src='" + options.path + "'>";
  146. liHtml += "<span>" + options.label + "</span>";
  147. liHtml += "</div>";
  148. li.innerHTML = liHtml;
  149. if (options.click != undefined) {
  150. li.onclick = function() {
  151. options.click('options');
  152. };
  153. }
  154. return li;
  155. }
  156. /**
  157. * 创建菜单Html
  158. * @param {JSON} options 图片路径
  159. * path{string}:图片路径
  160. * lable{string}:文字描述
  161. * click{function}:回调函数
  162. */
  163. function createSmartMenuHtml(options) {
  164. var menuHtml = "<li class='mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3'";
  165. if(options.idx != undefined){
  166. menuHtml += " idx='" + options.idx + "'";
  167. }
  168. menuHtml += " onclick='" + options.click + "()'>";
  169. menuHtml += "<div class='cr-menu-smart-panel'>";
  170. menuHtml += "<img src='" + options.path + "'>";
  171. menuHtml += "<span>" + options.label + "</span>";
  172. menuHtml += "</div>";
  173. menuHtml += "</li>";
  174. return menuHtml;
  175. }
  176. /**
  177. * 创建并打开页面
  178. * @param {JSON} options 配置项
  179. * htmlPath{string}:页面路径
  180. * top{number/undefined 可选项}:窗口距离顶端高度
  181. * bottom{number/undefined 可选项}:窗口距离底部高度
  182. * backgroundColor{color 可选项}:导航栏背景色
  183. * lineColor{color 可选项}:导航栏底框颜色
  184. * title{string}:导航栏标题
  185. * statusbar:{boolean 可选项}:状态栏是否透明
  186. * extras{JSON 可选项}:传递参数
  187. * showSlide{string 可选项}:动画方式 slide-in-bottom
  188. * isShow{boolean}:是否默认显示
  189. */
  190. function createAndOpenPage(options) {
  191. var viewId = options.id == undefined ? options.htmlPath : options.id;
  192. UIView_DidLoad(function() {
  193. var pageConfig = {
  194. "titleNView": {
  195. backgroundColor: options.backgroundColor == undefined ? colorBackground : options
  196. .backgroundColor,
  197. titleColor: options.titleColor == undefined ? colorTitle : options.titleColor,
  198. autoBackButton: options.autoback == undefined ? false : options.autoback,
  199. titleText: options.title,
  200. splitLine: {
  201. color: options.lineColor == undefined ? colorLine : options.lineColor,
  202. }
  203. },
  204. top: options.top == undefined ? '0' : options.top,
  205. bottom: options.bottom == undefined ? '0' : options.bottom,
  206. contentAdjust: false
  207. }
  208. /* 判断是否增加了按钮配置 */
  209. if (options.buttons != undefined) {
  210. pageConfig.titleNView.buttons = options.buttons;
  211. }
  212. /* 配置sttusbar 主要是为了配置任务栏的背景色 */
  213. if (options.statusbar != undefined && !options.statusbar) {
  214. pageConfig.statusbar = {
  215. background: colorBackground,
  216. }
  217. }
  218. /* 判断是否设置了高度 */
  219. if (options.height != undefined) {
  220. pageConfig.height = options.height;
  221. }
  222. /* 根据是否传参决定采用何种方式加载 */
  223. var subPage = undefined;
  224. if (options.extras != undefined) {
  225. subPage = plus.webview.create(options.htmlPath, viewId, pageConfig, options.extras);
  226. } else {
  227. subPage = plus.webview.create(options.htmlPath, viewId, pageConfig);
  228. }
  229. subPage.onloaded = function(e) {
  230. if (options.onloaded != undefined) options.onloaded(subPage);
  231. }
  232. /* 是否显示 */
  233. if (options.isShow == undefined || options.isShow == true) {
  234. /* 显示动画 */
  235. if (options.showSlide == undefined) {
  236. subPage.show('slide-in-right', 300);
  237. } else {
  238. subPage.show(options.showSlide, 300);
  239. }
  240. } else {
  241. subPage.hide();
  242. }
  243. });
  244. }
  245. /**
  246. * 获取值 对0值进行特殊处理
  247. * @param {string} strValue 字符串或其他
  248. */
  249. function getEChartsValue(strValue) {
  250. var value = parseFloat(strValue)
  251. if (value == 0) {
  252. return 0.1;
  253. } else {
  254. return value;
  255. }
  256. }
  257. /**
  258. * 验证Ip地址
  259. * @param {string} ip 输入的ip地址
  260. */
  261. function isValidIP(ip) {
  262. var reg =
  263. /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
  264. return reg.test(ip);
  265. }
  266. /**
  267. * 验证Com端口号
  268. * @param {string} com 输入的com端口号
  269. */
  270. function isValidCom(com) {
  271. var reg =
  272. /^(\d{4,4})$/
  273. return reg.test(com);
  274. }
  275. /**
  276. * 验证电话号码
  277. * @param {string} phoneNumber 输入的电话号码
  278. */
  279. function isValidPhone(phoneNumber) {
  280. var reg =
  281. /^(\d{11,11})$/
  282. return reg.test(phoneNumber);
  283. }