util.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import amap from '@/components/amap-wx/lib/amap-wx.js';
  2. // 地铁颜色图
  3. const line = {
  4. '1号线': '#C43B33',
  5. '2号线': '#016299',
  6. '4号线/大兴线': '#008E9C',
  7. '5号线': '#A42380',
  8. '6号线': '#D09900',
  9. '7号线': '#F2C172',
  10. '8号线': '#009D6A',
  11. '9号线': '#8FC41E',
  12. '10号线': '#009DBE',
  13. '13号线': '#F9E701',
  14. '14号线东段': '#D4A7A2',
  15. '14号线西段': '#D4A7A2',
  16. '15号线': '#5D2D69',
  17. '八通线': '#C33A32',
  18. '昌平线': '#DE82B1',
  19. '亦庄线': '#E40177',
  20. '房山线': '#E66021',
  21. '机场线': '#A29BBC',
  22. }
  23. // 150500:地铁站 ,150700:公交站 , 190700:地名地址
  24. const typecode = [{
  25. id: '150500',
  26. icon: 'icon-ditie'
  27. }, {
  28. id: '150700',
  29. icon: 'icon-gongjiao'
  30. }, {
  31. id: '190700',
  32. icon: 'icon-gonglu'
  33. }];
  34. const util = {
  35. key:'b526b09b86cd2996e7732be8ab8c4430',
  36. /**
  37. * 初始化高德地图api
  38. */
  39. mapInit() {
  40. return new amap.AMapWX({
  41. key: this.key
  42. });
  43. },
  44. // 服务状态吗
  45. typecode,
  46. /**
  47. * 获取地图颜色
  48. */
  49. lineColor(name) {
  50. if (line[name]) {
  51. return line[name];
  52. } else {
  53. return '#ccc';
  54. }
  55. },
  56. /**
  57. * 关键字颜色变化
  58. */
  59. serachNmme(val, name) {
  60. let namestr = new RegExp(val);
  61. let nameresult =
  62. `<div style="font-size: 14px;color: #333;line-height: 1.5;">
  63. ${name.replace(namestr, "<span style='color:#66ccff;'>" + val + '</span>')}
  64. </div>`
  65. .trim();
  66. return nameresult;
  67. },
  68. /**
  69. * 地址转地铁线路
  70. */
  71. addressToLine(address, type) {
  72. let addr = address.split(';');
  73. let dt = '';
  74. addr.forEach(elm => {
  75. let color = '#cccccc';
  76. if (type === typecode[0].id) {
  77. color = this.lineColor(elm)
  78. } else if (type === typecode[1].id) {
  79. color = '#4075cb'
  80. }
  81. let style = 'margin:5px 0;margin-right:5px;padding:0 5px;background:' + color +
  82. ';font-size:12px;color:#fff;border-radius:3px;';
  83. dt += `<div style=\'${style}\'>${elm}</div>`;
  84. });
  85. return `<div style="display:flex;flex-wrap: wrap;">${dt}</div>`;
  86. },
  87. /**
  88. * 数据处理
  89. */
  90. dataHandle(item, val) {
  91. // 改变字体颜色
  92. if (val) {
  93. item.nameNodes = util.serachNmme(val, item.name);
  94. } else {
  95. item.nameNodes = `<div style="font-size: 14px;color: #333;line-height: 1.5;">${item.name}</div>`;
  96. }
  97. // 地址解析 地铁
  98. if (
  99. item.typecode === util.typecode[0].id ||
  100. item.typecode === util.typecode[1].id
  101. ) {
  102. item.addressNodes = util.addressToLine(item.address, item.typecode);
  103. if (item.typecode === util.typecode[0].id) {
  104. item.icon = util.typecode[0].icon;
  105. } else if (item.typecode === util.typecode[1].id) {
  106. item.icon = util.typecode[1].icon;
  107. }
  108. } else {
  109. item.addressNodes = `<span>${item.district}${
  110. item.address.length > 0 ? '·' + item.address : ''
  111. }</span>`.trim();
  112. item.icon = 'icon-weizhi';
  113. }
  114. if (item.location && item.location.length === 0) {
  115. item.icon = 'icon-sousuo';
  116. }
  117. return item;
  118. },
  119. /**
  120. * 存储历史数据
  121. * val [string | object]需要存储的内容
  122. */
  123. setHistory(val) {
  124. let searchHistory = uni.getStorageSync('search:history');
  125. if (!searchHistory) searchHistory = [];
  126. let serachData = {};
  127. if (typeof(val) === 'string') {
  128. serachData = {
  129. adcode: [],
  130. address: [],
  131. city: [],
  132. district: [],
  133. id: [],
  134. location: [],
  135. name: val,
  136. typecode: []
  137. };
  138. } else {
  139. serachData = val
  140. }
  141. // 判断数组是否存在,如果存在,那么将放到最前面
  142. for (var i = 0; i < searchHistory.length; i++) {
  143. if (searchHistory[i].name === serachData.name) {
  144. searchHistory.splice(i, 1);
  145. break;
  146. }
  147. }
  148. searchHistory.unshift(util.dataHandle(serachData));
  149. uni.setStorage({
  150. key: 'search:history',
  151. data: searchHistory,
  152. success: function() {
  153. // console.log('success');
  154. }
  155. });
  156. },
  157. getHistory() {
  158. },
  159. removeHistory() {
  160. uni.removeStorage({
  161. key: 'search:history',
  162. success: function(res) {
  163. console.log('success');
  164. }
  165. });
  166. return []
  167. }
  168. }
  169. export default util;