linzq-citySelect.vue 9.9 KB

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