You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

420 lines
13 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
  1. <template>
  2. <view class="container">
  3. <view class="address-info ">
  4. <u--form labelPosition="left" ref="uForm">
  5. <u-form-item required label="联系人" prop="addressInfo.name" labelWidth="120" borderBottom>
  6. <u--input v-model="addressInfo.name" placeholder="请输入联系人姓名" placeholderStyle="text-align:right;color:#AAA" border="none" inputAlign="right"></u--input>
  7. </u-form-item>
  8. <u-form-item required label="手机号码" prop="addressInfo.phone" labelWidth="120" borderBottom>
  9. <u--input v-model="addressInfo.phone" placeholder="请输入手机号码" placeholderStyle="text-align:right;color:#AAA" border="none" inputAlign="right"></u--input>
  10. </u-form-item>
  11. <u-form-item label="应急联络" prop="addressInfo.emergencyPhone" labelWidth="120" borderBottom>
  12. <u--input v-model="addressInfo.emergencyPhone" placeholder="请输入应急联络号码" placeholderStyle="text-align:right;color:#AAA" border="none" inputAlign="right"></u--input>
  13. </u-form-item>
  14. <!-- <u-form-item label="所在城市" prop="addressInfo.city" labelWidth="120" borderBottom>
  15. <picker mode="multiSelector" @columnchange="columnchange" @change="bindMultiPickerColumnChange" :value="multiIndex"
  16. :range="multiArray">
  17. <view style="display: flex;justify-content: space-between;">
  18. <u--input :value="addressInfo.city? (addressInfo.city +' '+ addressInfo.district):''"
  19. disabled disabledColor="#ffffff" placeholder="请选择所在城市"
  20. placeholderStyle="text-align:right;color:#AAA" border="none" inputAlign="right">
  21. </u--input>
  22. <u-icon slot="right" name="arrow-right" color="#AAA"></u-icon>
  23. </view>
  24. </picker>
  25. </u-form-item> -->
  26. <u-form-item label="所在地区" prop="addressInfo.city" labelWidth="120" borderBottom>
  27. <view style="display: flex;justify-content: space-between;" @click="selectAddress">
  28. <u--input :value="addressInfo.selectAddress"
  29. disabled disabledColor="#ffffff" placeholder="请选择所在地区"
  30. placeholderStyle="text-align:right;color:#AAA" border="none" inputAlign="right">
  31. </u--input>
  32. <u-icon slot="right" name="arrow-right" color="#AAA"></u-icon>
  33. </view>
  34. </u-form-item>
  35. <u-form-item label="详细地址" prop="addressInfo.detailAddress" labelWidth="120" borderBottom>
  36. <u--input v-model="addressInfo.detailAddress" placeholder="如楼栋、门牌号" placeholderStyle="text-align:right;color:#AAA;" border="none" inputAlign="right"></u--input>
  37. </u-form-item>
  38. <u-form-item label="设为默认地址" prop="addressInfo.isDefault" labelWidth="120">
  39. <view>
  40. <u-radio-group :value="addressInfo.isDefault?'默认':''" placement="column" @change="groupChange">
  41. <u-radio name="默认" activeColor="#ffbf60" labelColor="#ffbf60" label="默认"></u-radio>
  42. </u-radio-group>
  43. </view>
  44. </u-form-item>
  45. </u--form>
  46. </view>
  47. <view class="address-save">
  48. <button class="address-save-btn" @click="save" :loading="loading">保存并使用</button>
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. import {
  54. getCity
  55. } from '@/api/system/user.js'
  56. import {
  57. getAddressDetails,addAddress,updateAddress
  58. } from '@/api/system/address.js'
  59. export default{
  60. data(){
  61. return{
  62. loading:false,
  63. needRelocate: false, // 是否需要重新定位
  64. addressInfo:{
  65. name:"",
  66. phone:"",
  67. emergencyPhone:"",
  68. province:"",
  69. city:"",
  70. district:'',
  71. detailAddress:"",
  72. openId:"",
  73. isDefault:0,
  74. latitude:"",
  75. longitude:"",
  76. selectAddress:""
  77. },
  78. multiArray: [],
  79. cityList:[],
  80. multiIndex: [0, 0],
  81. addressId:'',
  82. }
  83. },
  84. onLoad(option) {
  85. this.optionType=option.optionType;
  86. this.needRelocate = option.needRelocate === 'true'; // 是否需要重新定位
  87. if(this.optionType=='edit'){
  88. this.addressId=option.addressId;
  89. this.getAddressDetails(option.addressId);
  90. }
  91. },
  92. mounted() {
  93. this.getCityList()
  94. },
  95. methods:{
  96. getAddressDetails(id){
  97. getAddressDetails(id).then(res=>{
  98. if(res&& res.id){
  99. const {
  100. name,
  101. phone,
  102. emergencyPhone,
  103. province,
  104. city,
  105. district,
  106. detailAddress,
  107. openId,
  108. isDefault,
  109. latitude,
  110. longitude,
  111. } = res;
  112. // 检查是否需要重新定位或缺少经纬度信息
  113. const needClearLocation = this.needRelocate || !latitude || !longitude;
  114. this.addressInfo = {
  115. name,
  116. phone,
  117. emergencyPhone,
  118. province: needClearLocation ? '' : province,
  119. city: needClearLocation ? '' : city,
  120. district: needClearLocation ? '' : district,
  121. detailAddress,
  122. openId,
  123. isDefault,
  124. latitude: needClearLocation ? '' : latitude,
  125. longitude: needClearLocation ? '' : longitude,
  126. selectAddress: needClearLocation ? '' : `${province}${city}${district}`,
  127. };
  128. // 如果需要重新定位,提示用户
  129. if(needClearLocation) {
  130. setTimeout(() => {
  131. this.$modal.showToast('请重新选择所在地区进行定位');
  132. }, 500);
  133. }
  134. }else{
  135. this.$modal.showToast('获取地址详情失败')
  136. }
  137. })
  138. },
  139. // 选择地区
  140. selectAddress(){
  141. uni.chooseLocation({
  142. success: res => {
  143. console.log(res);
  144. this.addressInfo.latitude = res.latitude
  145. this.addressInfo.longitude = res.longitude
  146. // 获取完整地址
  147. let address = ''
  148. if (!res.address && res.name) {
  149. address = res.name
  150. } else if (res.address || res.name) {
  151. address = (res.address || '') + (res.name || '')
  152. }
  153. if(!address){
  154. return
  155. }
  156. // 解析地址信息
  157. const addressInfo = this.parseAddress(address);
  158. this.addressInfo.province = addressInfo.province
  159. this.addressInfo.city = addressInfo.city
  160. this.addressInfo.district = addressInfo.district
  161. this.addressInfo.detailAddress = addressInfo.detailAddress
  162. this.addressInfo.selectAddress = addressInfo.selectAddress
  163. },
  164. fail(e) {
  165. console.log("获取位置信息失败!", e)
  166. }
  167. })
  168. },
  169. save(){
  170. let params = this.addressInfo
  171. if(params.phone){
  172. if(!(/^1[3456789]\d{9}$/.test(params.phone))){
  173. this.$modal.showToast('手机号码有误')
  174. return false;
  175. }
  176. } else {
  177. this.$modal.showToast('请输入手机号')
  178. return false;
  179. }
  180. if(params.emergencyPhone){
  181. if(!(/^1[3456789]\d{9}$/.test(params.emergencyPhone))){
  182. this.$modal.showToast('手机号码有误')
  183. return false;
  184. }
  185. }
  186. if(!params.name){
  187. this.$modal.showToast('请输入联系人')
  188. return false;
  189. }
  190. this.loading=true
  191. if(this.optionType=='edit'){
  192. params.id = this.addressId;
  193. updateAddress(params).then(res=>{
  194. if(res&&res==1){
  195. this.$globalData.newOrderData.currentAddress={
  196. id:params.id,
  197. name:params.name,
  198. phone:params.phone,
  199. province:params.province,
  200. city:params.city,
  201. district:params.district,
  202. detailAddress:params.detailAddress,
  203. latitude:params.latitude,
  204. longitude:params.longitude,
  205. }
  206. this.loading=false
  207. uni.redirectTo({
  208. url: `/pages/newOrder/serviceNew`
  209. });
  210. }else {
  211. this.loading=false
  212. this.$modal.showToast('更新地址失败')
  213. }
  214. })
  215. } else if(this.optionType=='add'){
  216. addAddress(params).then(res=>{
  217. if(res&&res>0){
  218. this.$globalData.newOrderData.currentAddress={
  219. id:res,
  220. name:params.name,
  221. phone:params.phone,
  222. province:params.province,
  223. city:params.city,
  224. district:params.district,
  225. detailAddress:params.detailAddress,
  226. latitude:params.latitude,
  227. longitude:params.longitude,
  228. }
  229. this.loading=false
  230. uni.redirectTo({
  231. url: `/pages/newOrder/serviceNew`
  232. });
  233. }else {
  234. this.loading=false
  235. this.$modal.showToast('新增地址失败')
  236. }
  237. })
  238. }
  239. },
  240. getCityList() {
  241. getCity().then(res => {
  242. if (res.code == 200) {
  243. console.log('city', JSON.parse(res.msg));
  244. this.cityList = JSON.parse(res.msg)
  245. const cityLabels = this.cityList.map(item =>item.city)
  246. this.multiArray[0] = cityLabels
  247. this.multiArray[1] = this.cityList[0].region.map(e=> e.region)
  248. } else {
  249. this.$modal.showToast('获取城市失败,请联系系统管理员!');
  250. }
  251. })
  252. },
  253. columnchange(e){
  254. console.log(e)
  255. // 当滚动切换一级分类时,为当前的一级分类添加它的子类
  256. if (e.detail.column == 0) {
  257. const currentCity = this.cityList[e.detail.value]
  258. // #ifdef H5
  259. // 在小程序中直接赋值无效 H5 可直接赋值
  260. this.multiArray[1] = currentCity.region.map(e=>e.region)
  261. // #endif
  262. // #ifdef MP-WEIXIN
  263. // 在 H5 环境下 $set 会导致一级分类无法滚动, 小程序正常运行
  264. this.$set(this.multiArray, 1, currentCity.region.map(e=>e.region))
  265. // #endif
  266. this.multiIndex=[e.detail.value,0]
  267. }
  268. },
  269. bindMultiPickerColumnChange(e) {
  270. console.log('值为:' + e.detail.value)
  271. this.multiIndex = e.detail.value
  272. this.addressInfo.province = this.multiArray[0][this.multiIndex[0]]
  273. this.addressInfo.city = this.multiArray[0][this.multiIndex[0]]
  274. this.addressInfo.district = this.multiArray[1][this.multiIndex[1]]
  275. this.isSelected = true
  276. this.$forceUpdate()
  277. },
  278. groupChange(n) {
  279. console.log('groupChange', n);
  280. this.addressInfo.isDefault=+(!this.addressInfo.isDefault)
  281. },
  282. // 智能解析地址信息
  283. parseAddress(address) {
  284. if (!address) {
  285. return {
  286. province: '',
  287. city: '',
  288. district: '',
  289. detailAddress: '',
  290. selectAddress: ''
  291. };
  292. }
  293. // 直辖市列表
  294. const municipalities = ['北京市', '上海市', '天津市', '重庆市'];
  295. // 省级行政区正则
  296. const provinceReg = /(.*?(?:省|自治区|特别行政区))/;
  297. // 市级行政区正则
  298. const cityReg = /(.*?(?:市|自治州|地区|盟))/;
  299. // 区县级行政区正则
  300. const districtReg = /(.*?(?:区|县|市|旗|自治县|自治旗))/;
  301. let province = '';
  302. let city = '';
  303. let district = '';
  304. let detailAddress = '';
  305. let remainingAddress = address;
  306. // 检查是否为直辖市
  307. const municipality = municipalities.find(m => address.includes(m));
  308. if (municipality) {
  309. province = municipality;
  310. city = municipality;
  311. remainingAddress = address.substring(address.indexOf(municipality) + municipality.length);
  312. // 解析区县
  313. const districtMatch = remainingAddress.match(districtReg);
  314. if (districtMatch) {
  315. district = districtMatch[1];
  316. detailAddress = remainingAddress.substring(districtMatch[0].length).trim();
  317. } else {
  318. detailAddress = remainingAddress.trim();
  319. }
  320. } else {
  321. // 非直辖市,按省市区县顺序解析
  322. // 解析省份
  323. const provinceMatch = remainingAddress.match(provinceReg);
  324. if (provinceMatch) {
  325. province = provinceMatch[1];
  326. remainingAddress = remainingAddress.substring(provinceMatch[0].length);
  327. }
  328. // 解析城市
  329. const cityMatch = remainingAddress.match(cityReg);
  330. if (cityMatch) {
  331. city = cityMatch[1];
  332. remainingAddress = remainingAddress.substring(cityMatch[0].length);
  333. }
  334. // 解析区县
  335. const districtMatch = remainingAddress.match(districtReg);
  336. if (districtMatch) {
  337. district = districtMatch[1];
  338. remainingAddress = remainingAddress.substring(districtMatch[0].length);
  339. }
  340. detailAddress = remainingAddress.trim();
  341. }
  342. // 如果没有解析到省份,但有城市,可能是省级市
  343. if (!province && city) {
  344. province = city;
  345. }
  346. const selectAddress = `${province}${city !== province ? city : ''}${district}`;
  347. return {
  348. province,
  349. city,
  350. district,
  351. detailAddress,
  352. selectAddress
  353. };
  354. }
  355. }
  356. }
  357. </script>
  358. <style lang="scss">
  359. .container {
  360. position: relative;
  361. height: 100%;
  362. padding-bottom: 90px;
  363. .address-save {
  364. background-color: #FFFFFF;
  365. padding: 10px 20px 40px;
  366. width: 100%;
  367. height: 90px;
  368. position: fixed;
  369. bottom: 0;
  370. z-index: 100;
  371. .address-save-btn {
  372. width: 100%;
  373. border-radius: 6px;
  374. background: #FFB13F;
  375. font-size: 16px;
  376. color: #FFFFFF;
  377. }
  378. }
  379. }
  380. .address-info{
  381. background-color: #fff;
  382. padding: 10px 20px;
  383. .u-radio-group{
  384. float: right;
  385. }
  386. }
  387. </style>