util.js 5.3 KB

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