location.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <template>
  2. <view>
  3. <u-navbar :border-bottom="false" :is-back="false" title="选择城市"></u-navbar>
  4. <view class="selected">
  5. <view class="title">
  6. 已选 :
  7. <text style="color: #2979ff;margin-left: 12rpx;">
  8. {{ vuex_city == '' ? '请选择城市' : vuex_city }}
  9. </text>
  10. </view>
  11. </view>
  12. <view class="location" @click="setLocation">
  13. <view class="title">当前定位</view>
  14. <view class="body">
  15. <view class="left">
  16. <image class="img" mode="widthFix" src="../../static/navigate.png"></image>
  17. {{ locationCity }}
  18. </view>
  19. <view class="right">切换城市</view>
  20. </view>
  21. </view>
  22. <view class="hot">
  23. <view class="title">热门城市</view>
  24. <view class="body">
  25. <view v-for="(item,index) in hotList" :key="index" class="tag" @click="clickCity(item)">{{ item }}</view>
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. // import wxGetAddress from '@/common/utils/wxGetAddress.js'
  32. export default {
  33. data() {
  34. return {
  35. locationCity: '',
  36. hotList: ['杭州市', '郑州市', '北京市', '上海市', '广州市', '深圳市']
  37. }
  38. },
  39. onLoad() {
  40. this.findLocation()
  41. },
  42. methods: {
  43. clickCity(item) {
  44. this.$u.vuex('vuex_city', item);
  45. return uni.reLaunch({
  46. url: '../index/index'
  47. })
  48. },
  49. setLocation() {
  50. this.$u.vuex('vuex_city', this.locationCity);
  51. return uni.reLaunch({
  52. url: '../index/index'
  53. })
  54. },
  55. findLocation() {
  56. let that = this
  57. uni.showLoading({title: "定位中....", mask: true})
  58. this.$u.get("/api/profile/getRealCity").then(obj => {
  59. let cityName = obj.msg
  60. if (cityName) {
  61. this.locationCity = cityName
  62. let lifeData = uni.getStorageSync('lifeData');
  63. let vuex_city = lifeData.vuex_city
  64. if (!vuex_city) {
  65. this.$u.vuex('vuex_city', cityName);
  66. }
  67. uni.hideLoading();
  68. } else {
  69. uni.request({
  70. // url:'https://pv.sohu.com/cityjson?ie=utf-8',
  71. url: 'https://tianqiapi.com/ip?version=v9&appid=23035354&appsecret=8YvlPNrz',
  72. success(resp) {
  73. let ip = resp.data.ip
  74. that.$u.get("/api/profile/getRealCityByIP?ip=" + ip).then(obj => {
  75. let cityName = obj.msg
  76. if (cityName) {
  77. that.locationCity = cityName
  78. let lifeData = uni.getStorageSync('lifeData');
  79. let vuex_city = lifeData.vuex_city
  80. if (!vuex_city) {
  81. that.$u.vuex('vuex_city', cityName);
  82. }
  83. uni.hideLoading();
  84. } else {
  85. that.$mytip.toast('定位失败')
  86. }
  87. uni.hideLoading();
  88. });
  89. }
  90. })
  91. }
  92. });
  93. }
  94. // 微信小程序定位审核太严厉,不使用了。扩展后端接口,使用ip定位
  95. // findLocation(){
  96. // uni.showLoading({title:"定位中....",mask:true})
  97. // uni.getLocation({
  98. // type: 'gcj02',
  99. // success: async res => {
  100. // let { longitude, latitude } = res;
  101. // let result = await wxGetAddress({ longitude, latitude });
  102. // let province = result.regeocodeData.addressComponent.province
  103. // let cityName = result.regeocodeData.addressComponent.city
  104. // this.locationCity = cityName
  105. // let lifeData = uni.getStorageSync('lifeData');
  106. // let vuex_city = lifeData.vuex_city
  107. // if(!vuex_city){
  108. // this.$u.vuex('vuex_city', cityName);
  109. // }
  110. // uni.hideLoading();
  111. // }
  112. // });
  113. // }
  114. }
  115. }
  116. </script>
  117. <style lang="scss" scoped>
  118. .selected {
  119. .title {
  120. font-size: 30 rpx;
  121. color: $u-main-color;
  122. margin: 30 rpx 20 rpx;
  123. font-weight: 600;
  124. margin-left: 30 rpx;
  125. }
  126. }
  127. .location {
  128. background: #FFFFFF;
  129. border-radius: 18 rpx;
  130. padding: 3 rpx 0;
  131. margin: 20 rpx;
  132. .title {
  133. font-size: 22 rpx;
  134. color: $u-tips-color;
  135. margin: 30 rpx 20 rpx;
  136. }
  137. .body {
  138. margin: 30 rpx 20 rpx;
  139. display: flex;
  140. justify-content: space-between;
  141. .left {
  142. font-size: 32 rpx;
  143. font-weight: 800;
  144. display: flex;
  145. justify-content: center;
  146. align-items: center;
  147. .img {
  148. width: 35 rpx;
  149. margin-right: 12 rpx;
  150. }
  151. }
  152. .right {
  153. color: #2979ff;
  154. font-weight: 600;
  155. margin-right: 10 rpx;
  156. }
  157. ;
  158. }
  159. }
  160. .hot {
  161. .title {
  162. font-size: 30 rpx;
  163. color: $u-main-color;
  164. margin: 30 rpx 20 rpx;
  165. font-weight: 600;
  166. margin-left: 30 rpx;
  167. }
  168. .body {
  169. background: #FFFFFF;
  170. padding-bottom: 15 rpx;
  171. border-radius: 18 rpx;
  172. margin: 20 rpx;
  173. .tag {
  174. display: inline-block;
  175. width: 137 rpx;
  176. height: 75 rpx;
  177. line-height: 75 rpx;
  178. font-size: 26 rpx;
  179. color: $u-content-color;
  180. margin: 20 rpx 20 rpx 5 rpx 20 rpx;
  181. padding: 5 rpx 10 rpx;
  182. text-align: center;
  183. background-color: $u-bg-color;
  184. border-radius: 12 rpx;
  185. }
  186. }
  187. }
  188. </style>