util.js 5.3 KB

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