util.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. const getToday = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return [year, month, day].map(formatNumber).join('-');
  9. }
  10. const formatNumber = n => {
  11. n = n.toString()
  12. //通过判断第二个数字是否存在在来判断是否大于9
  13. return n[1] ? n : '0' + n
  14. }
  15. function checkTime(i) {
  16. if (i < 10) {
  17. i = "0" + i
  18. }
  19. return i;
  20. }
  21. // 中国标准时间格式化为yyyy-MM-dd HH:MM格式
  22. function formatDate(value) {
  23. var date = new Date(value);
  24. return date.getFullYear() + '-' + checkTime((date.getMonth() + 1)) + '-' + checkTime(date.getDate()) + ' ' + checkTime((date.getHours())) + ':' + checkTime((date.getMinutes()));// + ':' + checkTime((date.getSeconds()))
  25. //最后显示yyyy-MM-dd HH:MM:ss
  26. }
  27. // 时间戳格式化为yyyy-MM-dd格式
  28. function formatDateTime(value) {
  29. var date = new Date(parseInt(value));
  30. return date.getFullYear() + '-' + checkTime((date.getMonth() + 1)) + '-' + checkTime(date.getDate());
  31. //最后显示yyyy-MM-dd
  32. }
  33. // 获取当前周几转化为数字返回
  34. function getWeek() {
  35. var date = new Date();
  36. let week = date.getDay();
  37. if (week == 0) week = 7;
  38. return week;
  39. }
  40. /**
  41. * 弹窗提示
  42. */
  43. function checkForm(warn) {
  44. wx.showToast({
  45. title: warn,
  46. icon: 'none',
  47. duration: 2000,
  48. mask:true,
  49. })
  50. return
  51. }
  52. /**
  53. *
  54. * @param {Number} AddDayCount 数值 0为今天,-1为昨天,1为明天
  55. */
  56. function GetDateStr(AddDayCount) {
  57. var dd = new Date();
  58. dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期
  59. var y = dd.getFullYear();
  60. var m = dd.getMonth() + 1; //获取当前月份的日期
  61. m = m > 9 ? m : '0' + m;
  62. var d = dd.getDate();
  63. d = d > 9 ? d : '0' + d;
  64. return y + "-" + m + "-" + d;
  65. }
  66. /**
  67. * 下拉框选择
  68. * that [obj] this
  69. * setindex [string] 索引存放字段
  70. * e [obj] event
  71. */
  72. function getPickerIndex(that, index, e) { //弹窗提示
  73. that.setData({
  74. [index]: e.detail.value
  75. })
  76. }
  77. /**
  78. * 找到字典对应下标
  79. */
  80. function getDicIndex(v_data, value, codeName) {
  81. var n = '';
  82. for (var i = 0; i < v_data.length; i++) {
  83. if (v_data[i][codeName] == value) {
  84. n = i;
  85. }
  86. }
  87. return n;
  88. }
  89. /**
  90. * 找到字典对应text,用于详情显示
  91. * @param {*} v_data 数组
  92. * @param {*} value 数据库返回的值
  93. * @param {*} codeName 用于匹配数组中的字段名
  94. * @param {*} textCode 返回的字段名
  95. */
  96. function getDicName(v_data, value, codeName,textCode) {
  97. var n = '';
  98. if (v_data){
  99. for (var i = 0; i < v_data.length; i++) {
  100. if (v_data[i][codeName] == value) {
  101. n = v_data[i][textCode];
  102. }
  103. }
  104. return n;
  105. }
  106. }
  107. /**
  108. * 选项卡的点击事件
  109. */
  110. function tabClickFun(e, that, currentTab) {
  111. if (that.data[currentTab] === e.target.dataset.current) {
  112. return false;
  113. } else {
  114. that.setData({
  115. [currentTab]: e.target.dataset.current
  116. })
  117. }
  118. }
  119. /**
  120. * 选项卡的滑动事件
  121. */
  122. function tabBindFun(e, that, currentTab) {
  123. that.setData({
  124. [currentTab]: e.detail.current
  125. });
  126. }
  127. /**
  128. * 字符串截取
  129. * @string value 需要截取的字符串
  130. * @Number start 截取起始位置
  131. * @Number end 截取结束位置
  132. */
  133. function tosubStr(value, start, end) {
  134. if (value) {
  135. return value.slice(start, end);
  136. } else {
  137. return ''
  138. }
  139. }
  140. /**
  141. * 显示数值输入,最多只能输入2位小数
  142. * @param {*} value 数值
  143. */
  144. function digitLength(value) {
  145. var digit;
  146. if (/^(\d?)+(\.\d{0,2})?$/.test(value)) { //正则验证,提现金额小数点后不能大于两位数字
  147. digit = value;
  148. } else {
  149. digit = value.substring(0, value.length - 1);
  150. }
  151. return digit
  152. }
  153. module.exports = {
  154. getToday,
  155. formatDate, //中国标准时间格式化为yyyy-MM-dd格式
  156. formatDateTime,//时间戳转为日期格式
  157. checkTime,
  158. checkForm,
  159. tabClickFun,
  160. tabBindFun,
  161. GetDateStr, //获取日期(今天,昨天,明天等前后日期字符串)
  162. getPickerIndex,
  163. getWeek, // 获取当前周几数字
  164. getDicIndex, //获取select对应下标
  165. getDicName,//返回对应code的text
  166. tosubStr,
  167. digitLength, //输入框限制输入小数位数
  168. }