addAddress.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <view class="switch-roles">
  3. <nav-bar title="添加地址" color="#000"></nav-bar>
  4. <view class="roles-content">
  5. <view class="content">
  6. <!-- 地图选点入口(辅助填充) -->
  7. <view class="map-selector" @click="openMapSelector">
  8. <u-icon name="map" size="36rpx" color="#007AFF" marginRight="16rpx"></u-icon>
  9. <text class="map-text">通过地图选择地址(可选)</text>
  10. </view>
  11. <!-- 省/市/区三级地址(手动填写为主) -->
  12. <view class="form-item">
  13. <view class="item-label"> 所在省份 <text class="required">*</text></view>
  14. <u-input
  15. v-model="province"
  16. placeholder="请输入省份,例:广东省"
  17. clearable
  18. class="custom-input"
  19. >
  20. <template #prefix>
  21. <u-icon name="location" size="36rpx" color="#999" marginRight="20rpx"></u-icon>
  22. </template>
  23. </u-input>
  24. </view>
  25. <view class="form-item">
  26. <view class="item-label"> 所在城市 <text class="required">*</text></view>
  27. <u-input
  28. v-model="city"
  29. placeholder="请输入城市,例:深圳市"
  30. clearable
  31. class="custom-input"
  32. >
  33. <template #prefix>
  34. <u-icon name="map-pin" size="36rpx" color="#999" marginRight="20rpx"></u-icon>
  35. </template>
  36. </u-input>
  37. </view>
  38. <view class="form-item">
  39. <view class="item-label"> 所在区域 <text class="required">*</text></view>
  40. <u-input
  41. v-model="district"
  42. placeholder="请输入区域,例:南山区"
  43. clearable
  44. class="custom-input"
  45. >
  46. <template #prefix>
  47. <u-icon name="map-marker" size="36rpx" color="#999" marginRight="20rpx"></u-icon>
  48. </template>
  49. </u-input>
  50. </view>
  51. <!-- 详细地址信息 -->
  52. <view class="form-item">
  53. <view class="item-label"> 街道/楼宇 <text class="required">*</text></view>
  54. <u-input
  55. placeholder="请输入街道或楼宇名称,例:科技园路1号"
  56. v-model="address"
  57. clearable
  58. class="custom-input"
  59. >
  60. <template #prefix>
  61. <u-icon name="map" size="36rpx" color="#999" marginRight="20rpx"></u-icon>
  62. </template>
  63. </u-input>
  64. </view>
  65. <view class="form-item">
  66. <view class="item-label"> 楼层/单元室 </view>
  67. <u-input
  68. placeholder="例:3层302室(选填)"
  69. v-model="addressDetail"
  70. clearable
  71. class="custom-input"
  72. >
  73. <template #prefix>
  74. <u-icon name="home" size="36rpx" color="#999" marginRight="20rpx"></u-icon>
  75. </template>
  76. </u-input>
  77. </view>
  78. <!-- 地图选点后显示经纬度(可选) -->
  79. <view class="location-info" v-if="latitude && longitude">
  80. <text>经纬度:{{ latitude }}, {{ longitude }}</text>
  81. </view>
  82. </view>
  83. </view>
  84. <view class="submit-btn" @click="confirmAddress">确定</view>
  85. </view>
  86. </template>
  87. <script>
  88. import navBar from "@/components/nav-bar/index.vue";
  89. export default {
  90. data() {
  91. return {
  92. province: "", // 省份(必填)
  93. city: "", // 城市(必填)
  94. district: "", // 区域(必填)
  95. address: "", // 街道/楼宇(必填)
  96. addressDetail: '', // 楼层/单元室(选填)
  97. latitude: '', // 纬度(地图选点获取)
  98. longitude: '' // 经度(地图选点获取)
  99. };
  100. },
  101. components: {
  102. navBar,
  103. },
  104. onLoad(options) {
  105. // 回显上一页传递的地址信息
  106. if (options.province) this.province = decodeURIComponent(options.province);
  107. if (options.city) this.city = decodeURIComponent(options.city);
  108. if (options.district) this.district = decodeURIComponent(options.district);
  109. if (options.address) this.address = decodeURIComponent(options.address);
  110. if (options.addressDetail) this.addressDetail = decodeURIComponent(options.addressDetail);
  111. if (options.latitude) this.latitude = options.latitude;
  112. if (options.longitude) this.longitude = options.longitude;
  113. },
  114. methods: {
  115. // 打开地图选择地址(辅助填充详细地址)
  116. openMapSelector() {
  117. uni.chooseLocation({
  118. success: (res) => {
  119. // 地图选点仅填充街道/楼宇信息,省市区留给用户手动确认
  120. this.address = res.name || res.address;
  121. this.latitude = res.latitude;
  122. this.longitude = res.longitude;
  123. // 提示用户补充省市区信息
  124. uni.showToast({
  125. title: '请确认并补充省/市/区信息',
  126. icon: 'none',
  127. duration: 2000
  128. });
  129. },
  130. fail: (err) => {
  131. console.error('地图选择失败:', err);
  132. if (err.errMsg.includes('auth')) {
  133. uni.showToast({ title: '请开启位置权限', icon: 'none' });
  134. }
  135. }
  136. });
  137. },
  138. // 确认地址并回传(严格验证三级地址)
  139. confirmAddress() {
  140. // 验证必填项
  141. if (!this.province.trim()) {
  142. return uni.showToast({ title: '请输入所在省份', icon: 'none' });
  143. }
  144. if (!this.city.trim()) {
  145. return uni.showToast({ title: '请输入所在城市', icon: 'none' });
  146. }
  147. if (!this.district.trim()) {
  148. return uni.showToast({ title: '请输入所在区域', icon: 'none' });
  149. }
  150. if (!this.address.trim()) {
  151. return uni.showToast({ title: '请输入街道或楼宇名称', icon: 'none' });
  152. }
  153. // 合并完整地址信息
  154. const fullAddress = {
  155. province: this.province.trim(),
  156. city: this.city.trim(),
  157. district: this.district.trim(),
  158. address: this.address.trim(),
  159. addressDetail: this.addressDetail.trim(),
  160. latitude: this.latitude || '',
  161. longitude: this.longitude || '',
  162. fullText: `${this.province.trim()}${this.city.trim()}${this.district.trim()}${this.address.trim()}${this.addressDetail.trim() ? ' ' + this.addressDetail.trim() : ''}`
  163. };
  164. // 回传上一页
  165. uni.$emit('addressData', fullAddress);
  166. uni.navigateBack();
  167. }
  168. }
  169. };
  170. </script>
  171. <style lang="scss" scoped>
  172. .switch-roles {
  173. background-color: #fff;
  174. position: absolute;
  175. left: 0;
  176. right: 0;
  177. top: 0;
  178. bottom: 0;
  179. display: flex;
  180. flex-direction: column;
  181. .roles-content {
  182. width: 100%;
  183. flex: 1;
  184. overflow-y: auto;
  185. .content {
  186. padding: 40rpx;
  187. box-sizing: border-box;
  188. .map-selector {
  189. display: flex;
  190. align-items: center;
  191. padding: 20rpx 24rpx;
  192. background-color: #f5f7fa;
  193. border-radius: 16rpx;
  194. margin-bottom: 30rpx;
  195. color: #007AFF;
  196. font-size: 28rpx;
  197. cursor: pointer;
  198. transition: background-color 0.2s;
  199. &:active {
  200. background-color: #e8ebf0;
  201. }
  202. }
  203. .location-info {
  204. margin-top: 10rpx;
  205. font-size: 24rpx;
  206. color: #999;
  207. padding: 10rpx 0;
  208. }
  209. .required {
  210. color: #ff4d4f; /* 红色必填标记 */
  211. font-size: 32rpx;
  212. margin-left: 4rpx;
  213. }
  214. }
  215. }
  216. .submit-btn {
  217. margin: 60rpx 20rpx;
  218. height: 96rpx;
  219. border-radius: 999px;
  220. background: linear-gradient(90deg, #0d27f7, #13c1ea);
  221. color: #fff;
  222. font-size: 32rpx;
  223. display: flex;
  224. justify-content: center;
  225. align-items: center;
  226. transition: all 0.2s ease;
  227. &:active {
  228. transform: scale(0.98);
  229. opacity: 0.9;
  230. }
  231. }
  232. }
  233. .custom-input {
  234. box-sizing: border-box;
  235. border: 2rpx solid #9ea1a8;
  236. border-radius: 100rpx;
  237. /* 增加左右内边距,让内容更舒展 */
  238. padding: 8rpx 36rpx;
  239. height: 96rpx;
  240. }
  241. /* 调整图标与输入文字的间距 */
  242. ::v-deep .u-input__prefix {
  243. margin-right: 24rpx !important; /* 增加图标右侧间距 */
  244. }
  245. /* 输入框文字区域增加左侧间距(配合图标) */
  246. ::v-deep .u-input input {
  247. padding-left: 32rpx; /* 避免文字紧贴图标 */
  248. }
  249. .form-item {
  250. margin-bottom: 26rpx;
  251. width: 100%;
  252. }
  253. .item-label {
  254. color: #121a2c;
  255. font-size: 32rpx;
  256. padding: 10rpx 0;
  257. display: flex;
  258. align-items: center;
  259. }
  260. .custom-input {
  261. box-sizing: border-box;
  262. border: 2rpx solid #9ea1a8;
  263. border-radius: 100rpx;
  264. padding: 8rpx 24rpx;
  265. height: 96rpx;
  266. }
  267. /* 输入框聚焦样式 */
  268. ::v-deep .u-input input:focus {
  269. border-color: #007AFF !important;
  270. outline: none;
  271. }
  272. ::v-deep .u-input {
  273. text-align: left !important;
  274. }
  275. </style>