util.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. var regChar = { //非特殊字符作为值
  2. '?':'qweqwqjoijweq',
  3. '&':'asdauihdasfsdas',
  4. '=':'fqwebwfubwefqwf'
  5. }
  6. function formatTime(date) {
  7. var year = date.getFullYear();
  8. var month = date.getMonth() + 1;
  9. var day = date.getDate();
  10. var hour = date.getHours();
  11. var minute = date.getMinutes();
  12. var second = date.getSeconds();
  13. return (
  14. [year, month, day].map(formatNumber).join('/') +
  15. ' ' +
  16. [hour, minute, second].map(formatNumber).join(':')
  17. );
  18. }
  19. function formatTimeTxt(year, month, day) {
  20. return [year, month, day].map(formatNumber).join('');
  21. }
  22. function formatNumber(n) {
  23. n = n.toString();
  24. return n[1] ? n : '0' + n;
  25. }
  26. function makeNumToHour(n) {
  27. return formatNumber(n) + ':00';
  28. }
  29. function makeHourToNum(t) {
  30. return parseInt(t.toString().split(':')[0]);
  31. }
  32. function makeNumToDay(n) {
  33. return '周' + '日一二三四五六'[n];
  34. }
  35. function makeNumToFullTimeArr(n) {
  36. // 20171010
  37. n = n.toString();
  38. let year = n.substring(0, 4);
  39. let month = n.substring(4, 6);
  40. let date = n.substring(6, 8);
  41. return [year, month, date];
  42. }
  43. function isPhoneNum(txt) {
  44. return /^1[34578]\d{9}$/.test(txt);
  45. }
  46. function arrUniqueFilter(arr) {
  47. return arr.filter((item, pos, array) => array.indexOf(item) === pos);
  48. }
  49. function bindInput(event) {
  50. var obj = {},
  51. key = event.target.dataset['key'];
  52. // Toast.showToast('success', event.detail.value);
  53. obj[key] = event.detail.value;
  54. this.setData(obj);
  55. // console.log(obj[key])
  56. }
  57. function encodeParam(str){
  58. var encode = [];
  59. var tempSwitch = false
  60. for (let i = 0; i < str.length; i++) {
  61. Object.keys(regChar).forEach(function(key){
  62. if (str.charAt(i) === key){
  63. encode.push(regChar[key]);
  64. tempSwitch = true
  65. return;
  66. }
  67. })
  68. if (!tempSwitch){
  69. encode.push(str.charAt(i))
  70. }
  71. tempSwitch = false
  72. }
  73. return encode.join('')
  74. }
  75. function decodeParam(str) {
  76. var keyReg = ''
  77. Object.keys(regChar).forEach(function (key) {
  78. keyReg = new RegExp(regChar[key],'g')
  79. // console.log(regChar[key])
  80. str = str.replace(keyReg, key)
  81. })
  82. return str
  83. }
  84. function removeArrItem(arr, item) {
  85. var newarr = [];
  86. for (var i = 0; i < arr.length; i++) {
  87. if (arr[i] != item) {
  88. newarr.push(arr[i]);
  89. }
  90. }
  91. return newarr;
  92. }
  93. class Timer {
  94. constructor({ max, delay = 1000 }) {
  95. console.log(max, delay);
  96. this.timer = null;
  97. this.max = max;
  98. this.delay = delay;
  99. }
  100. run(cb) {
  101. this.timer = setInterval(() => {
  102. if (this.max >= 0) {
  103. cb && cb(this.max--);
  104. } else {
  105. this.cancel();
  106. }
  107. }, this.delay);
  108. }
  109. cancel() {
  110. this.timer && clearInterval(this.timer);
  111. }
  112. }
  113. class Toast {
  114. constructor() {
  115. // this.successImage = 'images/icon-success.png';
  116. // this.warnImage = '../../../images/icon-warn.png';
  117. // this.loadingImage = 'images/icon-loading.png';
  118. this.image = {
  119. success: '../../../images/icon-success.png',
  120. warn: '../../../images/icon-warn.png',
  121. loading: '../../../images/icon-loading.png'
  122. };
  123. }
  124. showToast(type = 'success', title, success = () => { }) {
  125. let imgUrl = '';
  126. let t = '';
  127. switch (type) {
  128. case 'success':
  129. case 'tip':
  130. wx.showModal({
  131. title: '提示',
  132. content: title,
  133. showCancel: false,
  134. confirmColor: '#e83828',
  135. success
  136. });
  137. break;
  138. case 'warn':
  139. // imgUrl = this.warnImage;
  140. t = type == 'success' ? '成功' : '提示';
  141. wx.showModal({
  142. title: t,
  143. content: title,
  144. showCancel: false,
  145. confirmColor: '#e83828',
  146. success
  147. });
  148. break;
  149. case 'loading':
  150. wx.showToast({
  151. //title: '手机号码输入错误',
  152. title: title || '加载中...',
  153. icon: type,
  154. mask: true
  155. });
  156. break;
  157. }
  158. }
  159. showToast2(type = 'success', title = '') {
  160. switch (type) {
  161. case 'loading':
  162. wx.showLoading({
  163. mask: true,
  164. title: title || '加载中...'
  165. });
  166. break;
  167. case 'success':
  168. case 'warn':
  169. default:
  170. wx.showToast({
  171. //title: '手机号码输入错误',
  172. title: title,
  173. icon: type,
  174. image: this.image[type],
  175. mask: true
  176. });
  177. break;
  178. }
  179. }
  180. hideLoading() {
  181. wx.hideLoading();
  182. }
  183. }
  184. module.exports = {
  185. formatTime,
  186. removeArrItem,
  187. formatTimeTxt,
  188. formatNumber,
  189. isPhoneNum,
  190. makeNumToHour,
  191. makeHourToNum,
  192. makeNumToDay,
  193. makeNumToFullTimeArr,
  194. arrUniqueFilter,
  195. Timer,
  196. bindInput,
  197. encodeParam,
  198. decodeParam,
  199. Toast: new Toast(),
  200. };