linzq-citySelect.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <template>
  2. <div class="wrapper" :style="'top:'+statusBarHeight+'px'">
  3. <div class="header">
  4. <view class="back_div">
  5. <image class="back_img" @click="back_city()" src="../../static/back_img.png" mode=""></image>
  6. </view>
  7. <input class="input" @input="onInput" placeholder="中文/拼音/首字母" v-model="searchValue" />
  8. </div>
  9. <scroll-view class="calendar-list" scroll-y="true" :scroll-into-view="scrollIntoId">
  10. <view v-if="disdingwei" id="hot">
  11. <!-- 定位模块 -->
  12. <view class="dingwei">
  13. <view class="dingwei_Tips">
  14. 当前定位
  15. </view>
  16. <view class="dingwei_city">
  17. <view class="dingwei_city_one">
  18. {{position}}
  19. </view>
  20. <view class="dingweis_div" @click="getWarpweft">
  21. <image class="dingweis" src="../../static/dingweis.png" mode=""></image>
  22. <text>{{po_tips}}</text>
  23. </view>
  24. </view>
  25. </view>
  26. <!-- 最近模块 -->
  27. <view class="dingwei" v-if="Visit.length>=0">
  28. <view class="dingwei_Tips">
  29. 最近访问
  30. </view>
  31. <view class="dingwei_city dingwei_city_zuijin">
  32. <view class="dingwei_city_one toright" v-for="(item,index) in Visit" v-if="index<2" @click="back_city(item)">
  33. {{item.cityName}}
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. <!-- 城市列表 -->
  39. <view v-if="searchValue == ''" v-for="(item, index) in list" :id="getId(index)" :key="index">
  40. <view class="letter-header">{{ getId(index) }}</view>
  41. <view class="city-div" v-for="(city, i) in item" :key="i" @click="back_city(city)">
  42. <text class="city">{{ city.cityName }}</text>
  43. </view>
  44. </view>
  45. <!-- 搜索结果 -->
  46. <view class="city-div" v-for="(item, index) in searchList" @click="back_city(item)">
  47. <text class="city">{{ item.cityName }}</text>
  48. </view>
  49. </scroll-view>
  50. <!-- 右侧字母 -->
  51. <view class="letters" v-if="searchValue == ''">
  52. <view class="letters-item" @click="scrollTo('hot')">最近</view>
  53. <view class="letters-item" v-for="item in letter" :key="item" @click="scrollTo(item)">{{ item }}</view>
  54. </view>
  55. <!-- 选中之后字母 -->
  56. <view class="mask" v-if="showMask">
  57. <view class="mask-r">{{selectLetter}}</view>
  58. </view>
  59. </div>
  60. </template>
  61. <script>
  62. import Citys from '../city.js';
  63. export default {
  64. components: {},
  65. props: {},
  66. computed: {
  67. hotCity() {
  68. return Citys.hotCity;
  69. },
  70. citys() {
  71. return Citys.cities;
  72. }
  73. },
  74. data() {
  75. return {
  76. statusBarHeight: this.statusBarHeight,
  77. ImgUrl: this.ImgUrl,
  78. letter: [],
  79. selectLetter: '',
  80. searchValue: '',
  81. scrollIntoId: '',
  82. list: [],
  83. tId: null,
  84. searchList: [],
  85. showMask: false,
  86. disdingwei: true,
  87. Visit: [], //最近访问
  88. position: '青岛',
  89. longitude: '', //经度
  90. latitude: '', //纬度
  91. seconds: 3,
  92. po_tips: '重新定位',
  93. }
  94. },
  95. created() {
  96. //获取存储的最近访问
  97. var that = this
  98. uni.getStorage({
  99. key: 'Visit_key',
  100. success: function(res) {
  101. that.Visit = res.data
  102. }
  103. });
  104. //获取定位 经度纬度
  105. that.getWarpweft()
  106. //获取city.js 的程序字母
  107. var mu = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'w', 'x', 'y',
  108. 'z'
  109. ];
  110. var tmp = [];
  111. for (var i = 0; i < mu.length; i++) {
  112. var item = mu[i];
  113. for (var j = 0; j < this.citys.length; j++) {
  114. var py = this.citys[j].py;
  115. if (py.substring(0, 1) == item) {
  116. if (tmp.indexOf(item) == -1) {
  117. this.list[i] = [this.citys[j]];
  118. tmp.push(item);
  119. this.letter.push(item.toUpperCase());
  120. } else {
  121. this.list[i].push(this.citys[j]);
  122. }
  123. }
  124. }
  125. }
  126. },
  127. methods: {
  128. getId(index) {
  129. return this.letter[index];
  130. },
  131. scrollTo(letter) {
  132. this.showMask = true
  133. this.selectLetter = letter == 'hot' ? '最' : letter
  134. setTimeout(() => {
  135. this.showMask = false
  136. }, 300);
  137. this.scrollIntoId = letter;
  138. },
  139. query(source, text) {
  140. let res = [];
  141. var self = this;
  142. res = source.filter(item => {
  143. const arr = [];
  144. let isHave = false;
  145. Object.keys(item).forEach(prop => {
  146. const itemStr = item[prop];
  147. self.isString(itemStr) &&
  148. itemStr.split(',').forEach(val => {
  149. arr.push(val);
  150. });
  151. });
  152. arr.some(val => {
  153. isHave = new RegExp('^' + text).test(val);
  154. return isHave;
  155. });
  156. return isHave;
  157. });
  158. console.log(JSON.stringify(res));
  159. return res;
  160. },
  161. isString(obj) {
  162. return typeof obj === 'string';
  163. },
  164. onInput(e) {
  165. const value = e.target.value;
  166. console.log(value);
  167. if (value !== '' && this.citys && this.citys.length > 0) {
  168. const queryData = this.query(this.citys, String(value).trim());
  169. this.searchList = queryData;
  170. this.disdingwei = false
  171. } else {
  172. this.searchList = [];
  173. this.disdingwei = true
  174. }
  175. },
  176. back_city(item) {
  177. if (item) {
  178. this.$emit('back_city', item);
  179. //unshift 把数据插入到首位,与push相反
  180. this.Visit.unshift(item)
  181. this.searchValue = "";
  182. this.disdingwei = true
  183. var arr = this.Visit
  184. //数组去重
  185. function distinct(arr) {
  186. let newArr = []
  187. for (let i = 0; i < arr.length; i++) {
  188. if (newArr.indexOf(arr[i]) < 0) {
  189. newArr.push(arr[i])
  190. }
  191. }
  192. return newArr
  193. }
  194. this.Visit = distinct(arr)
  195. console.log(this.Visit, "---最近访问")
  196. uni.setStorage({
  197. key: 'Visit_key',
  198. data: this.Visit
  199. });
  200. } else {
  201. this.$emit('back_city', 'no');
  202. }
  203. },
  204. getWarpweft() {
  205. var that = this
  206. that.po_tips = '定位中...'
  207. let countdown = setInterval(() => {
  208. that.seconds--;
  209. uni.getLocation({
  210. type: 'wgs84',
  211. success: function(res) {
  212. console.log('当前位置的经度:' + res.longitude);
  213. console.log('当前位置的纬度:' + res.latitude);
  214. that.longitude = res.longitude
  215. that.latitude = res.latitude
  216. }
  217. });
  218. if (that.seconds <= 0) {
  219. that.seconds = 3
  220. that.po_tips = '重新定位'
  221. clearInterval(countdown);
  222. }
  223. }, 1000);
  224. }
  225. }
  226. };
  227. </script>
  228. <style scoped>
  229. .wrapper {
  230. position: fixed;
  231. z-index: 999999;
  232. background: #ffffff;
  233. height: 100%;
  234. width: 100%;
  235. top: 0px;
  236. left: 0px;
  237. }
  238. .mask {
  239. position: absolute;
  240. bottom: 0upx;
  241. top: 83upx;
  242. left: 0upx;
  243. right: 0upx;
  244. width: 750upx;
  245. display: flex;
  246. justify-content: center;
  247. align-items: center;
  248. background: rgba(0, 0, 0, 0);
  249. }
  250. .mask-r {
  251. height: 120upx;
  252. width: 120upx;
  253. border-radius: 60upx;
  254. display: flex;
  255. background: rgba(0, 0, 0, 0.5);
  256. justify-content: center;
  257. align-items: center;
  258. font-size: 40upx;
  259. color: #FFFFFF
  260. }
  261. .content {
  262. height: 100%;
  263. width: 100%;
  264. background-color: #ffffff;
  265. }
  266. .header {
  267. height: 85upx;
  268. display: flex;
  269. justify-content: flex-start;
  270. align-items: center;
  271. }
  272. .back_div {
  273. width: 65upx;
  274. height: 100%;
  275. display: flex;
  276. justify-content: center;
  277. align-items: center;
  278. }
  279. .back_img {
  280. width: 35upx;
  281. height: 35upx;
  282. }
  283. .input {
  284. font-size: 28upx;
  285. width: 620upx;
  286. height: 55upx;
  287. border-radius: 40upx;
  288. background-color: #F5F5F5;
  289. padding-left: 20upx;
  290. padding-right: 20upx;
  291. box-sizing: border-box;
  292. }
  293. .title {
  294. font-size: 30upx;
  295. color: white;
  296. }
  297. .show {
  298. left: 0;
  299. width: 100%;
  300. transition: left 0.3s ease;
  301. }
  302. .hide {
  303. left: 100%;
  304. width: 100%;
  305. transition: left 0.3s ease;
  306. }
  307. .title {
  308. font-size: 30upx;
  309. color: white;
  310. }
  311. .calendar-list {
  312. position: absolute;
  313. top: 83upx;
  314. bottom: 0upx;
  315. width: 100%;
  316. background-color: #FFFFFF;
  317. }
  318. .letters {
  319. position: absolute;
  320. right: 30upx;
  321. bottom: 0px;
  322. width: 50upx;
  323. top: 260upx;
  324. color: #2f9bfe;
  325. text-align: center;
  326. font-size: 24upx;
  327. }
  328. .letters-item {
  329. margin-bottom: 5upx;
  330. }
  331. .letter-header {
  332. height: 45upx;
  333. font-size: 22upx;
  334. color: #333333;
  335. padding-left: 24upx;
  336. box-sizing: border-box;
  337. display: flex;
  338. align-items: center;
  339. background-color: #ebedef;
  340. }
  341. .city-div {
  342. width: 660upx;
  343. height: 85upx;
  344. margin-left: 24upx;
  345. border-bottom-width: 0.5upx;
  346. border-bottom-color: #ebedef;
  347. border-bottom-style: solid;
  348. display: flex;
  349. align-items: center;
  350. margin-right: 35upx;
  351. }
  352. .city {
  353. font-size: 28upx;
  354. color: #000000;
  355. padding-left: 30upx;
  356. }
  357. .dingwei {
  358. width: 100%;
  359. padding-top: 25upx;
  360. box-sizing: border-box;
  361. margin-bottom: 26upx;
  362. }
  363. .dingwei_Tips {
  364. margin-left: 24upx;
  365. margin-bottom: 24upx;
  366. font-size: 24upx;
  367. color: #A5A5A5;
  368. }
  369. .dingwei_city {
  370. width: 100%;
  371. height: 60upx;
  372. padding-left: 55upx;
  373. padding-right: 70upx;
  374. box-sizing: border-box;
  375. display: flex;
  376. justify-content: space-between;
  377. }
  378. .dingwei_city_one {
  379. width: 185upx;
  380. height: 60upx;
  381. background-color: #F5F5F5;
  382. border-radius: 10upx;
  383. font-size: 32upx;
  384. color: #333333;
  385. display: flex;
  386. justify-content: center;
  387. align-items: center;
  388. }
  389. .dingweis_div {
  390. display: flex;
  391. align-content: flex-end;
  392. align-items: center;
  393. font-size: 24upx;
  394. color: #FD5745;
  395. }
  396. .dingweis {
  397. width: 32upx;
  398. height: 32upx;
  399. }
  400. .dingwei_city_zuijin {
  401. display: flex;
  402. justify-content: flex-start;
  403. }
  404. .toright {
  405. margin-right: 25upx;
  406. }
  407. </style>