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