Comm.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. import IDCard from "../vendor/IDCard.js";
  2. const dialog=uni.requireNativePlugin("CL-Dialog");
  3. /**
  4. * @param {string} text 提示内容
  5. * @param {int} time 多少时间消失
  6. * @return {function} fn 函数
  7. *
  8. */
  9. const Tip = (text, time = 2000, fn = null) => {
  10. uni.showToast({
  11. 'title' : text,
  12. duration : time,
  13. icon:'none'
  14. });
  15. if (fn != null) setTimeout(() => {fn();}, time);
  16. }
  17. /**
  18. * 加载数据
  19. */
  20. const loadIng = (title = false,mask = false) => {
  21. uni.showLoading({
  22. title: title ? title : "加载中...",
  23. mask : mask
  24. });
  25. }
  26. //询问框
  27. const showModal = (title,success = null,fail = null) =>{
  28. if(dialog != null){
  29. let options = {
  30. title: "系统提示", //标题 (可选)
  31. con: title, //内容(可选)但是标题和内容至少选择一个
  32. okTitle: "确认", //确认按钮文字(可选)
  33. cancleTitle: "取消", //取消按钮文字(可选)
  34. okTextColor: "#f1851e", //确认按钮颜色(可选)
  35. cancleTextColor: "#999999", //取消按钮颜色(可选)
  36. singer: false, //是否只显示确认按钮,默认false(可选
  37. textAlign: "center", //对齐方式 //left居左,center居中 ,right 居右 默认居中
  38. conColor: "",
  39. bgColor: "#ffffff", //自定义弹框颜色
  40. titleColor: "#3d3d3d" //自定义title颜色
  41. //提示框内容颜色;
  42. }
  43. dialog.show(options,()=>{
  44. if(success != null) success();
  45. },()=>{
  46. if(fail != null) fail();
  47. });
  48. } else {
  49. uni.showModal({
  50. title: "系统提示",
  51. content: title,
  52. confirmText: "确定",
  53. cancelText: "取消",
  54. success:function(res){
  55. if(res.confirm && success != null){
  56. success();
  57. return;
  58. }
  59. if(!res.confirm && fail != null){
  60. fail();
  61. return;
  62. }
  63. }
  64. })
  65. }
  66. }
  67. /**
  68. * 提示框
  69. */
  70. const showAlert = (ob,success = null) =>{
  71. var oAr = {};
  72. if(ob instanceof Object){
  73. oAr = ob;
  74. if(oAr['title'] == null || oAr['title'] == '') {
  75. oAr['title'] = "系统提示";
  76. }
  77. } else {
  78. oAr['title'] = "系统提示";
  79. oAr['content'] = ob;
  80. }
  81. if(dialog != null){
  82. let options = {
  83. title: oAr['title'], //标题 (可选)
  84. con: oAr['content'], //内容(可选)但是标题和内容至少选择一个
  85. okTitle: oAr['btn'] == null ? '确认' : oAr['btn'], //确认按钮文字(可选)
  86. okTextColor: "#f1851e", //确认按钮颜色(可选)
  87. singer: true, //是否只显示确认按钮,默认false(可选
  88. textAlign: "center", //对齐方 式 //left居左,center居中 ,right 居右 默认居中
  89. conColor: "",
  90. bgColor: "#ffffff", //自定义弹框颜色
  91. titleColor: "#3d3d3d" //自定义title颜色
  92. //提示框内容颜色;
  93. }
  94. console.log(dialog);
  95. dialog.show(options,()=>{
  96. if(success != null) success();
  97. });
  98. } else {
  99. uni.showModal({
  100. title: oAr['title'],
  101. content: oAr['content'],
  102. confirmText:oAr['btn'] == null ? '确认' : oAr['btn'],
  103. showCancel: false,
  104. success:function(){
  105. if(success != null) success();
  106. }
  107. });
  108. }
  109. }
  110. /**
  111. * 判断是否微信
  112. * @return {bool} 是否微信
  113. */
  114. function isWeiXin(){
  115. var ua = window.navigator.userAgent.toLowerCase();
  116. if(ua.match(/MicroMessenger/i) == 'micromessenger'){
  117. return true;
  118. }else{
  119. return false;
  120. }
  121. }
  122. /**
  123. * 判断是否存在
  124. * @param {Object} value
  125. */
  126. function isDefine(value) {
  127. if (value == null || value == "" || value == "undefined" || value == undefined || value == "null" || value == "(null)" || value == 'NULL' || typeof (value) == 'undefined') {
  128. return false;
  129. }
  130. else {
  131. value = value + "";
  132. value = value.replace(/\s/g, "");
  133. if (value == "") {
  134. return false;
  135. }
  136. return true;
  137. }
  138. }
  139. /**
  140. * 时间戳11位
  141. */
  142. function timest() {
  143. var tmp = Date.parse( new Date() ).toString();
  144. tmp = tmp.substr(0,10);
  145. return tmp;
  146. }
  147. /**
  148. * 时间戳格式化函数
  149. * @param {string} format 格式
  150. * @param {int} timestamp 要格式化的时间 默认为当前时间
  151. * @return {string} 格式化的时间字符串
  152. */
  153. function date(format, timestamp) {
  154. var a, jsdate = ((timestamp) ? new Date(timestamp * 1000) : new Date());
  155. var pad = function (n, c) {
  156. n = n + "";
  157. if (n.length < c) {
  158. var i = c - n.length;
  159. var v = '';
  160. for (var j = 0; j < i; j++) {
  161. v += '0';
  162. }
  163. return v + n;
  164. } else {
  165. return n;
  166. }
  167. };
  168. var txt_weekdays = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  169. var txt_ordin = { "1": "st", "2": "nd", "3": "rd", "21": "st", "22": "nd", "23": "rd", "31": "st" };
  170. var txt_months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  171. var f = {
  172. // Day
  173. d: function () { return pad(f.j(), 2) },
  174. D: function () { return f.l().substr(0, 3) },
  175. j: function () { return jsdate.getDate() },
  176. l: function () { return txt_weekdays[f.w()] },
  177. N: function () { return f.w() + 1 },
  178. S: function () { return txt_ordin[f.j()] ? txt_ordin[f.j()] : 'th' },
  179. w: function () { return jsdate.getDay() }
  180. ,
  181. // Week
  182. W: function () {
  183. var a = f.z(), b = 364 + f.L() - a;
  184. var nd2, nd = (new Date(jsdate.getFullYear() + "/1/1").getDay() || 7) - 1;
  185. if (b <= 2 && ((jsdate.getDay() || 7) - 1) <= 2 - b) {
  186. return 1;
  187. } else {
  188. if (a <= 2 && nd >= 4 && a >= (6 - nd)) {
  189. nd2 = new Date(jsdate.getFullYear() - 1 + "/12/31");
  190. return date("W", Math.round(nd2.getTime() / 1000));
  191. } else {
  192. return (1 + (nd <= 3 ? ((a + nd) / 7) : (a - (7 - nd)) / 7) >> 0);
  193. }
  194. }
  195. },
  196. // Month
  197. F: function () { return txt_months[f.n()] },
  198. m: function () { return pad(f.n(), 2) },
  199. M: function () { return f.F().substr(0, 3) },
  200. n: function () { return jsdate.getMonth() + 1 },
  201. t: function () {
  202. var n;
  203. if ((n = jsdate.getMonth() + 1) == 2) {
  204. return 28 + f.L();
  205. } else {
  206. if (n & 1 && n < 8 || !(n & 1) && n > 7) {
  207. return 31;
  208. } else {
  209. return 30;
  210. }
  211. }
  212. },
  213. //o not supported yet
  214. Y: function () { return jsdate.getFullYear() },
  215. y: function () { return (jsdate.getFullYear() + "") },
  216. // Time
  217. a: function () { return jsdate.getHours() > 11 ? "pm" : "am" },
  218. A: function () { return f.a().toUpperCase() },
  219. B: function () {
  220. // peter paul koch:
  221. var off = (jsdate.getTimezoneOffset() + 60) * 60;
  222. var theSeconds = (jsdate.getHours() * 3600) + (jsdate.getMinutes() * 60) + jsdate.getSeconds() + off;
  223. var beat = Math.floor(theSeconds / 86.4);
  224. if (beat > 1000) beat -= 1000;
  225. if (beat < 0) beat += 1000;
  226. if ((String(beat)).length == 1) beat = "00" + beat;
  227. if ((String(beat)).length == 2) beat = "0" + beat;
  228. return beat;
  229. },
  230. g: function () { return jsdate.getHours() % 12 || 12 },
  231. G: function () { return jsdate.getHours() },
  232. h: function () { return pad(f.g(), 2) },
  233. H: function () { return pad(jsdate.getHours(), 2) },
  234. i: function () { return pad(jsdate.getMinutes(), 2) },
  235. s: function () { return pad(jsdate.getSeconds(), 2) },
  236. //u not supported yet
  237. // Timezone
  238. //e not supported yet
  239. //I not supported yet
  240. O: function () {
  241. var t = pad(Math.abs(jsdate.getTimezoneOffset() / 60 * 100), 4);
  242. if (jsdate.getTimezoneOffset() > 0) t = "-" + t; else t = "+" + t;
  243. return t;
  244. },
  245. P: function () { var O = f.O(); return (O.substr(0, 3) + ":" + O.substr(3, 2)) },
  246. //T not supported yet
  247. //Z not supported yet
  248. // Full Date/Time
  249. c: function () { return f.Y() + "-" + f.m() + "-" + f.d() + "T" + f.h() + ":" + f.i() + ":" + f.s() + f.P() },
  250. //r not supported yet
  251. U: function () { return Math.round(jsdate.getTime() / 1000) }
  252. };
  253. return format.replace(/[\\]?([a-zA-Z])/g, function (t, s) {
  254. var ret = '';
  255. if (t != s) {
  256. ret = s;
  257. } else if (f[s]) {
  258. ret = f[s]();
  259. } else {
  260. ret = s;
  261. }
  262. return ret;
  263. });
  264. }
  265. /**
  266. * strtotime()函数
  267. * strtotime('2012-07-27 12:43:43') OR strtotime('2012-07-27')
  268. * @return 时间戳
  269. */
  270. function strtotime(str = '') {
  271. if(str == '') {
  272. var _temp = new Date();
  273. return parseInt(_temp.getTime() / 1000);
  274. }
  275. var _arr = str.split(' ');
  276. var _day = _arr[0].split('-');
  277. _arr[1] = (_arr[1] == null) ? '0:0:0' : _arr[1];
  278. var _time = _arr[1].split(':');
  279. for (var i = _day.length - 1; i >= 0; i--) {
  280. _day[i] = isNaN(parseInt(_day[i])) ? 0 : parseInt(_day[i]);
  281. };
  282. for (var i = _time.length - 1; i >= 0; i--) {
  283. _time[i] = isNaN(parseInt(_time[i])) ? 0 : parseInt(_time[i]);
  284. };
  285. var _temp = new Date(_day[0], _day[1] - 1, _day[2], _time[0], _time[1], _time[2]);
  286. return parseInt(_temp.getTime() / 1000);
  287. }
  288. /**
  289. * 小数保留默认2位
  290. */
  291. function returnFloat(value) {
  292. var value = Math.round(parseFloat(value) * 100) / 100;
  293. var xsd = value.toString().split(".");
  294. if (xsd.length == 1) {
  295. value = value.toString() + ".00";
  296. return value;
  297. }
  298. if (xsd.length > 1) {
  299. if (xsd[1].length < 2) {
  300. value = value.toString() + "0";
  301. }
  302. return value;
  303. }
  304. }
  305. /**
  306. * 百分比
  307. */
  308. function percentage(value,value2) {
  309. return returnFloat((value / value2) * 100);
  310. }
  311. /**
  312. * 倒计时【验证码】
  313. * @time 倒计时 秒
  314. * @结束
  315. */
  316. function CountDowm(time, fe = null) {
  317. var id = setInterval(() => {
  318. time--;
  319. if (fe != null) fe(time, 0);
  320. if (time <= 0) {
  321. clearInterval(id);
  322. fe(0, 1);
  323. }
  324. }, 1000);
  325. }
  326. /**
  327. * 远程获取[GET同步返回]
  328. * @parm url 地址
  329. * @parm data get数据
  330. * @parm fn 异步返回
  331. */
  332. const httpGet = (url, data, fn, fe) => {
  333. // if (fe == null) { fe = () => { Tip("网络错误,请稍等尝试..."); wx.hideLoading() } };
  334. if (data == null) data = {time:Math.round(new Date().getTime() / 1000)};
  335. uni.request({
  336. url: url,
  337. data: post,
  338. success: res => {
  339. if (res.statusCode == 200) if (fn != null) fn(res.data); else fe();
  340. },
  341. fail: () => { fe(); }});
  342. }
  343. /**
  344. * 远程获取[GET异步返回]
  345. * @parm url 地址
  346. * @parm data get数据
  347. */
  348. const httpGetAsy = (url, data) => {
  349. return new Promise((resolve, reject) => {
  350. if (data == null) data = {time:Math.round(new Date().getTime() / 1000)};
  351. uni.request({
  352. url : url,
  353. data : data,
  354. success : res => {if (res.statusCode == 200) resolve(res.data); else reject();},
  355. fail: ()=> {reject();}
  356. });
  357. });
  358. };
  359. /**
  360. * 判断手机号码是否准确
  361. */
  362. const isPoneAvailable = (tel) => {
  363. var myreg=/^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  364. if (!myreg.test(tel)) {
  365. return false;
  366. } else {
  367. return true;
  368. }
  369. };
  370. /**
  371. * 远程获取[POST异步操作]
  372. * @parm url 地址
  373. * @parm post post数据
  374. */
  375. const httpPostAsy = (url, post) => {
  376. return new Promise((resolve, reject) => {
  377. uni.request({
  378. url : url,
  379. method : 'POST',
  380. data : post,
  381. header : {
  382. "content-type" : "application/x-www-form-urlencoded"
  383. },
  384. success: res => { if (res.statusCode == 200) resolve(res.data); else reject(); },
  385. fail: () => { reject(); }
  386. });
  387. });
  388. };
  389. /**
  390. * 广告跳转页面
  391. * @param {Object} url
  392. */
  393. function navigateTo(url) {
  394. if (url == '#' || url == '') {
  395. return;
  396. }
  397. var switchAr = [
  398. '/pages/cart/index'
  399. ];
  400. for(var i in switchAr) {
  401. if(switchAr[i] == url) {
  402. uni.switchTab({
  403. url : url
  404. });
  405. }
  406. }
  407. if(url.indexOf("http://") >= 0 || url.indexOf("https://") >= 0) {
  408. location.href = url;
  409. return;
  410. }
  411. uni.navigateTo({
  412. url: url
  413. });
  414. }
  415. /**
  416. * 阿拉伯数字转中文数字
  417. * @param {Object} num
  418. */
  419. function noToChinese(num) {
  420. if (!/^\d*(\.\d*)?$/.test(num)) {
  421. alert("Number is wrong!");
  422. return "Number is wrong!";
  423. }
  424. var AA = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九");
  425. var BB = new Array("", "十", "百", "千", "万", "亿", "点", "");
  426. var a = ("" + num).replace(/(^0*)/g, "").split("."),
  427. k = 0,
  428. re = "";
  429. for (var i = a[0].length - 1; i >= 0; i--) {
  430. switch (k) {
  431. case 0:
  432. re = BB[7] + re;
  433. break;
  434. case 4:
  435. if (!new RegExp("0{4}\\d{" + (a[0].length - i - 1) + "}$").test(a[0]))
  436. re = BB[4] + re;
  437. break;
  438. case 8:
  439. re = BB[5] + re;
  440. BB[7] = BB[5];
  441. k = 0;
  442. break;
  443. }
  444. if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0) re = AA[0] + re;
  445. if (a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k % 4] + re;
  446. k++;
  447. }
  448. if (a.length > 1) //加上小数部分(如果有小数部分)
  449. {
  450. re += BB[6];
  451. for (var i = 0; i < a[1].length; i++) re += AA[a[1].charAt(i)];
  452. }
  453. return re;
  454. };
  455. /**
  456. * 字节转换
  457. */
  458. function bytesToSize(bytes) {
  459. if (bytes === 0) return '0 B';
  460. var k = 1024;
  461. var sizes = ['B', 'KB', 'M', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  462. var i = Math.floor(Math.log(bytes) / Math.log(k));
  463. return toDecimal(bytes / Math.pow(k, i)) + ' ' + sizes[i];
  464. }
  465. /**
  466. * 星座
  467. */
  468. function constellation(strBirthday) {
  469. if (strBirthday == '0') return '无';
  470. var strBirthdayArr = strBirthday.split("-");
  471. var month = strBirthdayArr[1];
  472. var date = strBirthdayArr[2];
  473. if (month == 1 && date >= 20 || month == 2 && date <= 18) { return "水瓶座"; }
  474. if (month == 2 && date >= 19 || month == 3 && date <= 20) { return "双鱼座"; }
  475. if (month == 3 && date >= 21 || month == 4 && date <= 19) { return "白羊座"; }
  476. if (month == 4 && date >= 20 || month == 5 && date <= 20) { return "金牛座"; }
  477. if (month == 5 && date >= 21 || month == 6 && date <= 21) { return "双子座"; }
  478. if (month == 6 && date >= 22 || month == 7 && date <= 22) { return "巨蟹座"; }
  479. if (month == 7 && date >= 23 || month == 8 && date <= 22) { return "狮子座"; }
  480. if (month == 8 && date >= 23 || month == 9 && date <= 22) { return "室女座"; }
  481. if (month == 9 && date >= 23 || month == 10 && date <= 22) { return "天秤座"; }
  482. if (month == 10 && date >= 23 || month == 11 && date <= 21) { return "天蝎座"; }
  483. if (month == 11 && date >= 22 || month == 12 && date <= 21) { return "人马座"; }
  484. if (month == 12 && date >= 22 || month == 1 && date <= 19) { return "摩羯座"; }
  485. }
  486. /**
  487. * 消息时间值转化
  488. * @param {int} timestamp 要格式化的时间 默认为当前时间
  489. * @return {string} 格式化的时间字符串
  490. */
  491. function timeLastdate(timestamp,isWeekTime = false){
  492. //判断是否今天
  493. var stoday = strtotime(date('Y-m-d 00:00:00'));
  494. if (timestamp > stoday){
  495. var h = date('H');
  496. if(h <= 12)
  497. return '早上' + date('H:i', timestamp);
  498. else
  499. return '下午' + date('H:i', timestamp);
  500. }
  501. //昨天
  502. var day = new Date();
  503. day.setTime(day.getTime() - 24 * 60 * 60 * 1000);
  504. var ytoday = day.getTime() / 1000;
  505. var s_yesterday = strtotime(date('Y-m-d 0:0:0', ytoday));
  506. var e_yesterday = strtotime(date('Y-m-d 23:59:59', ytoday));
  507. if (timestamp > s_yesterday && timestamp < e_yesterday){
  508. return '昨天' + date('H:i', timestamp);
  509. }
  510. //获取周一时间
  511. var dayInWeek = day.getDay();
  512. dayInWeek == 0 && (dayInWeek = 7);
  513. var beginWeek = strtotime(date('Y-m-d 0:0:0',(day.getTime() - (dayInWeek - 1) * 86400000) / 1000));
  514. if (timestamp >= beginWeek){
  515. return date('l', timestamp) + (isWeekTime == true ? date('h:i',timestamp) : '');
  516. }
  517. //判断是否今年
  518. var y = day.getFullYear() ;
  519. var y1 = date('Y', timestamp);
  520. if(y == y1){
  521. return isWeekTime ? date('m月d日 H:i', timestamp) : date('m月d日', timestamp);
  522. }
  523. return date('Y年m月d日 H:i', timestamp);
  524. }
  525. function countdown(time,isAr) {
  526. var str = "";
  527. var day = parseInt(time / (3600 * 24));
  528. var day_yusu = time % (3600 * 24);
  529. var housr = parseInt(day_yusu / 3600);
  530. var housr_yusu = day_yusu % 3600;
  531. var minutes = parseInt(housr_yusu / 60);
  532. var seconds = housr_yusu % 60;
  533. if(isAr) {
  534. return [day,
  535. (housr > 9 ? housr : ('0' + housr) ),
  536. (minutes > 9 ? minutes : ('0' + minutes) ),
  537. (seconds > 9 ? seconds : ('0' + seconds))];
  538. }
  539. if(day > 0){
  540. str = day + '天';
  541. }
  542. console.log(seconds);
  543. str += (housr > 9 ? housr : ('0' + housr) ) + ':'
  544. + (minutes > 9 ? minutes : ('0' + minutes) ) + ':' +
  545. (seconds > 9 ? seconds : ('0' + seconds) );
  546. return str;
  547. }
  548. /**
  549. * 金钱单位模拟
  550. * @param {Object} str
  551. */
  552. function moneyUnit(str){
  553. let money = parseFloat(str);
  554. if(money < 1000) {
  555. return money + "元";
  556. }
  557. if(money >= 1000) {
  558. return Math.floor(money / 1000) + "K";
  559. }
  560. }
  561. /**
  562. * 返回薪水
  563. * @param {Object} min_price 最小金额
  564. * @param {Object} max_price 最大金额
  565. * @param {Object} salary_type 薪水类型
  566. */
  567. function nxMoney(min_price,max_price,salary_type) {
  568. var tls = "";
  569. if(min_price == max_price && min_price == 0) {
  570. return '面议';
  571. }
  572. if(min_price == max_price && min_price > 0) {
  573. tls = (min_price) +"元";
  574. }
  575. //以上
  576. if(min_price > 0 && max_price == 0) {
  577. tls = (min_price) + "元以上";
  578. }
  579. //区间
  580. if(min_price > 0 && max_price > 0) {
  581. tls = (min_price) + "元-" + (max_price)+"元";
  582. }
  583. //小时制
  584. if(salary_type == 2) {
  585. tls += "/每小时";
  586. }
  587. //天制
  588. if(salary_type == 3) {
  589. tls += "/每天";
  590. }
  591. return tls;
  592. }
  593. /**
  594. * 最小年龄 | 最大年龄
  595. * @param {Object} min_age
  596. * @param {Object} max_age
  597. */
  598. function nxAge(min_age,max_age) {
  599. if(min_age > 0 && max_age > 0 && max_age != min_age) {
  600. return min_age + "岁-" + max_age + "岁";
  601. }
  602. if(min_age > 0 && max_age > 0 && max_age == min_age) {
  603. return min_age + "岁";
  604. }
  605. if(min_age > 0 && max_age == 0) {
  606. return min_age + "岁以上";
  607. }
  608. if(min_age == 0 && max_age > 0) {
  609. return max_age + "岁以下";
  610. }
  611. if(min_age == 0 && max_age == 0) {
  612. return "不限年龄";
  613. }
  614. }
  615. function toH5ml(str) {
  616. if(str == null || str.length == 0)
  617. return '';
  618. //#ifdef H5
  619. var newStr = str.replace(/\n/g, '_@').replace(/\r/g, '_#');
  620. newStr = newStr.replace(/_#_@/g, '<br/>');//IE7-8
  621. newStr = newStr.replace(/_@/g, '<br/>');//IE9、FF、chrome
  622. newStr = newStr.replace(/\s/g, '&nbsp;');//空格处理
  623. return newStr;
  624. //#endif
  625. //#ifndef H5
  626. str = str.replace(/\n/g, '<br/>');//替换HTML空格
  627. //#endif
  628. return str;
  629. }
  630. function bankCode(code) {
  631. if(code.length <= 12) {
  632. return code;
  633. }
  634. var len = parseInt(code.length / 4) + (code.length % 4 > 0 ? 1 : 0);
  635. var reStr = "";
  636. for(var i = 0;i<len;i++) {
  637. var llen = ((i * 4) + 4) >= code.length ? code.length : (i * 4);
  638. reStr += code.substr((i * 4),4) + " ";
  639. }
  640. return reStr;
  641. }
  642. /**
  643. * 标题搜索突出关键词
  644. * @param {Object} title1
  645. * @param {Object} title2
  646. */
  647. function titleTag(title1,title2) {
  648. var dAr = [];
  649. for(var i in title2) {
  650. if(title1.indexOf(title2[i]) >= 0) {
  651. dAr.push({ name : title2[i], type : 's'});
  652. } else {
  653. dAr.push({ name : title2[i], type : 'e'});
  654. }
  655. }
  656. return dAr;
  657. }
  658. /**
  659. * 检验18位身份证号码(15位号码可以只检测生日是否正确即可)
  660. * @author wolfchen
  661. * @param cid 18为的身份证号码
  662. * @return Boolean 是否合法
  663. **/
  664. function isCnNewID(cid){
  665. return IDCard(cid);
  666. }
  667. /**
  668. * 自动计算高度 | 宽度
  669. * @param width 原始
  670. * @param height 原始
  671. * @param mWidth 现在 0不参与计算
  672. * @param mHeight 现在
  673. */
  674. function getAotuWidth(width,height,mWidth,mHeight = 0) {
  675. if(mWidth > 0 && mHeight == 0) {
  676. return (height / width) * mWidth;
  677. }
  678. if(mWidth == 0 && mHeight > 0) {
  679. return (width / height) * mHeight;
  680. }
  681. return 0;
  682. }
  683. function getAvatar(img,type){
  684. if(isDefine(img) && img != "") {
  685. return img;
  686. }
  687. return type == 'chat' ? '/static/chat/user-avatar.png' : '/static/chat/group_chat.png';
  688. }
  689. /**
  690. * 销售量
  691. * @param {Object} count
  692. */
  693. function sales(count){
  694. if(isNaN(count)) return 0;
  695. if(count < 100) return count + "";
  696. if(count > 100 && count < 1000) return parseInt(count / 100) * 100 + "+";
  697. if(count >= 1000 && count < 10000) return parseInt(count / 1000) * 1000 + "+";
  698. if(count >= 10000 ) return parseInt(count / 10000) + "万+";
  699. }
  700. /**
  701. * 数字转化文字
  702. * @param {Object} num
  703. */
  704. function numberToChinese(num) {
  705. var cnNums = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  706. var cnIntRadice = ["", "十", "百", "千"];
  707. var cnIntUnits = ["", "万", "亿", "兆"];
  708. var integerNum = Math.floor(num);
  709. var decimalNum = Math.round((num - integerNum) * 100);
  710. function convertToChinese(num) {
  711. var cnStr = "";
  712. var numStr = num.toString();
  713. var length = numStr.length;
  714. var lastNonZero = false; // 用于处理连续的零
  715. for (var i = 0; i < length; i++) {
  716. var digit = numStr.charAt(i);
  717. if (digit === "0") {
  718. lastNonZero = false;
  719. if (i === length - 1 || numStr.charAt(i + 1) !== "0") {
  720. cnStr += cnNums[parseInt(digit)];
  721. }
  722. } else {
  723. cnStr += cnNums[parseInt(digit)] + cnIntRadice[length - i - 1];
  724. lastNonZero = true;
  725. }
  726. }
  727. return cnStr;
  728. }
  729. var chineseStr = "";
  730. var zeroCount = 0;
  731. while (integerNum > 0) {
  732. var section = integerNum % 10000;
  733. if (zeroCount > 0) {
  734. chineseStr = cnNums[0] + chineseStr; // 处理连续的零
  735. }
  736. chineseStr = convertToChinese(section) + cnIntUnits[zeroCount] + chineseStr;
  737. integerNum = Math.floor(integerNum / 10000);
  738. zeroCount++;
  739. }
  740. chineseStr += convertToChinese(decimalNum);
  741. chineseStr = chineseStr.replace(/(零.)*零$/, "").replace(/^$/, "零");
  742. if(chineseStr[chineseStr.length - 1] == '零'){
  743. chineseStr = chineseStr.substring(0,chineseStr.length - 1);
  744. }
  745. return chineseStr;
  746. }
  747. /**
  748. * 取小数位0
  749. * @param {Object} num
  750. */
  751. function removeTrailingZeros(num) {
  752. let str = num.toString();
  753. if (str.indexOf('.') !== -1) {
  754. let parts = str.split('.');
  755. let decimalPart = parts[1].replace(/0+$/, '');
  756. if (decimalPart.length === 0) {
  757. return parseInt(parts[0]);
  758. } else {
  759. return parts[0] + '.' + decimalPart;
  760. }
  761. } else {
  762. return str;
  763. }
  764. }
  765. function isJSON(str) {
  766. if (typeof str == 'string') {
  767. try {
  768. var obj=JSON.parse(str);
  769. if(typeof obj == 'object' && obj ){
  770. return true;
  771. }else{
  772. return false;
  773. }
  774. } catch(e) {
  775. return false;
  776. }
  777. }
  778. }
  779. export default {
  780. Tip : Tip,//
  781. sales : sales,//销售数量
  782. loadIng : loadIng,//加载效果
  783. showModal : showModal,//确认框
  784. showAlert : showAlert,//提示框
  785. date : date,//时间戳解析时间
  786. strtotime : strtotime,//日期解析时间戳
  787. returnFloat : returnFloat,//小数保留二位
  788. CountDowm : CountDowm,//倒计时
  789. isDefine : isDefine,//判断是否空
  790. httpGet : httpGet,//同步获取GET
  791. timest : timest,//获取11位时间戳
  792. httpGetAsy : httpGetAsy,//异步GET
  793. httpPostAsy : httpPostAsy,//异步POST
  794. isPoneAvailable : isPoneAvailable,//判断手机号码
  795. navigateTo : navigateTo,//页面跳转
  796. noToChinese : noToChinese,//数字转为中文数字
  797. timeLastdate : timeLastdate,
  798. moneyUnit : moneyUnit,//金钱单位
  799. bankCode : bankCode,//银行卡hide
  800. toH5ml : toH5ml,//格式化
  801. nxAge : nxAge,//年龄格式化
  802. nxMoney : nxMoney,//金钱格式化
  803. titleTag : titleTag,//标题tag
  804. countdown : countdown,
  805. isCnNewID : isCnNewID,
  806. getAotuWidth : getAotuWidth,
  807. percentage : percentage,
  808. getAvatar : getAvatar,
  809. isJSON : isJSON,
  810. numberToChinese : numberToChinese,//数字转化中文
  811. removeTrailingZeros : removeTrailingZeros,
  812. navigateBack : ()=>{
  813. const pages = getCurrentPages();
  814. pages.length > 1 ? uni.navigateBack() : uni.reLaunch({
  815. url:"/pages/index/home"
  816. });
  817. }
  818. }