address-edit.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. <template>
  2. <view class="container">
  3. <view class="form-wrap">
  4. <!-- 收货人 -->
  5. <view class="form-row">
  6. <text class="form-label">收货人</text>
  7. <input
  8. class="form-input"
  9. v-model="form.receiverName"
  10. placeholder="请输入收货人姓名"
  11. placeholder-style="color: #ccc; font-size: 28rpx;"
  12. />
  13. </view>
  14. <!-- 手机号 -->
  15. <view class="form-row">
  16. <text class="form-label">手机号</text>
  17. <input
  18. class="form-input"
  19. v-model="form.phone"
  20. placeholder="请输入手机号"
  21. type="number"
  22. maxlength="11"
  23. placeholder-style="color: #ccc; font-size: 28rpx;"
  24. />
  25. </view>
  26. <!-- 区域选择 (省市区三级联动) -->
  27. <view class="form-row address-row" @click="openRegionPicker">
  28. <text class="form-label">所在地区</text>
  29. <view class="address-value-wrap">
  30. <text v-if="form.fullAddress" class="address-value">{{ form.fullAddress }}</text>
  31. <text v-else class="address-placeholder">请选择省市区</text>
  32. <text class="address-arrow">›</text>
  33. </view>
  34. </view>
  35. <!-- 详细地址 -->
  36. <view class="form-row">
  37. <text class="form-label">详细地址</text>
  38. <input
  39. class="form-input"
  40. v-model="form.street"
  41. placeholder="请输入详细地址"
  42. placeholder-style="color: #ccc; font-size: 28rpx;"
  43. />
  44. </view>
  45. <!-- 设为默认 -->
  46. <view class="form-row switch-row">
  47. <text class="form-label">设为默认地址</text>
  48. <view
  49. :class="['switch', form.isDefault === 1 ? 'switch-on' : '']"
  50. @click="toggleDefault"
  51. >
  52. <view :class="['switch-knob', form.isDefault === 1 ? 'knob-on' : '']"></view>
  53. </view>
  54. </view>
  55. <!-- 区域选择弹窗 -->
  56. <view class="region-mask" v-if="showRegionPicker" @click="cancelRegion">
  57. <view class="region-picker" @click.stop>
  58. <view class="region-header">
  59. <text class="region-header-btn" @click="cancelRegion">取消</text>
  60. <text class="region-header-title">选择地区</text>
  61. <text class="region-header-btn region-confirm" @click="confirmRegion">确定</text>
  62. </view>
  63. <view class="region-breadcrumb">
  64. <text
  65. v-for="(name, i) in regionBreadcrumb"
  66. :key="i"
  67. :class="['crumb-item', i === currentRegionLevel ? 'crumb-active' : '']"
  68. @click="jumpRegionLevel(i)"
  69. >{{ name || '请选择' }}</text>
  70. <text class="crumb-arrow" v-if="currentRegionLevel < 2">›</text>
  71. </view>
  72. <scroll-view class="region-list" scroll-y>
  73. <view
  74. v-for="(item, idx) in currentRegionItems"
  75. :key="idx"
  76. :class="['region-item', isRegionSelected(item) ? 'region-item-selected' : '']"
  77. @click="selectRegionItem(item)"
  78. >
  79. <text class="region-item-text">{{ getRegionName(item) }}</text>
  80. <text class="region-item-check" v-if="isRegionSelected(item)">✓</text>
  81. </view>
  82. <view v-if="regionLoading" class="region-loading">
  83. <text>加载中...</text>
  84. </view>
  85. </scroll-view>
  86. </view>
  87. </view>
  88. <!-- Save Button -->
  89. <button
  90. :class="['save-btn', saving ? 'disabled' : '']"
  91. :disabled="saving"
  92. @click="onSave"
  93. >
  94. {{ saving ? '保存中...' : '保存地址' }}
  95. </button>
  96. </view>
  97. </view>
  98. </template>
  99. <script>
  100. import config from '@/config.js'
  101. export default {
  102. data() {
  103. return {
  104. addressId: null,
  105. saving: false,
  106. form: {
  107. receiverName: '',
  108. phone: '',
  109. province: '',
  110. city: '',
  111. district: '',
  112. street: '',
  113. isDefault: 0,
  114. fullAddress: ''
  115. },
  116. // 区域选择弹窗
  117. showRegionPicker: false,
  118. currentRegionLevel: 0, // 0=province, 1=city, 2=district
  119. regionLoading: false,
  120. provinceList: [],
  121. cityList: [],
  122. districtList: [],
  123. tempProvince: null,
  124. tempCity: null,
  125. tempDistrict: null
  126. }
  127. },
  128. onLoad(options) {
  129. if (options.addressId) {
  130. this.addressId = parseInt(options.addressId)
  131. uni.setNavigationBarTitle({ title: '编辑地址' })
  132. this.loadAddress()
  133. } else {
  134. uni.setNavigationBarTitle({ title: '新增地址' })
  135. }
  136. },
  137. computed: {
  138. regionBreadcrumb() {
  139. var names = []
  140. names.push(this.tempProvince ? this.getRegionName(this.tempProvince) : '省份')
  141. names.push(this.tempCity ? this.getRegionName(this.tempCity) : '城市')
  142. names.push(this.tempDistrict ? this.getRegionName(this.tempDistrict) : '区县')
  143. return names
  144. },
  145. currentRegionItems() {
  146. if (this.currentRegionLevel === 0) return this.provinceList
  147. if (this.currentRegionLevel === 1) return this.cityList
  148. if (this.currentRegionLevel === 2) return this.districtList
  149. return []
  150. }
  151. },
  152. methods: {
  153. loadAddress() {
  154. var that = this
  155. uni.request({
  156. url: config.api('/api/user/address/get'),
  157. method: 'POST',
  158. data: { id: this.addressId },
  159. header: {
  160. 'Content-Type': 'application/json',
  161. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  162. },
  163. success: function(res) {
  164. if (res.data && res.data.code === 200) {
  165. var data = res.data.data || {}
  166. that.form.receiverName = data.receiverName || ''
  167. that.form.phone = data.phone || ''
  168. that.form.province = data.province || ''
  169. that.form.city = data.city || ''
  170. that.form.district = data.district || ''
  171. that.form.street = data.street || ''
  172. that.form.isDefault = data.isDefault || 0
  173. that.form.fullAddress = (data.province || '') + (data.city || '') + (data.district || '') + (data.street || '')
  174. }
  175. }
  176. })
  177. },
  178. // ===== 区域选择弹窗 =====
  179. openRegionPicker() {
  180. this.tempProvince = this.form.province ? { name: this.form.province, id: null } : null
  181. this.tempCity = this.form.city ? { name: this.form.city, id: null } : null
  182. this.tempDistrict = this.form.district ? { name: this.form.district, id: null } : null
  183. this.currentRegionLevel = 0
  184. this.showRegionPicker = true
  185. this.loadRegionProvinces()
  186. },
  187. loadRegionProvinces() {
  188. var that = this
  189. that.regionLoading = true
  190. uni.request({
  191. url: config.api('/api/region/provinces'),
  192. method: 'POST',
  193. header: {
  194. 'Content-Type': 'application/json',
  195. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  196. },
  197. success: function(res) {
  198. that.regionLoading = false
  199. if (res.data && res.data.code === 200) {
  200. that.provinceList = res.data.data || []
  201. }
  202. },
  203. fail: function() {
  204. that.regionLoading = false
  205. }
  206. })
  207. },
  208. selectRegionItem(item) {
  209. if (this.currentRegionLevel === 0) {
  210. this.tempProvince = item
  211. this.tempCity = null
  212. this.tempDistrict = null
  213. this.cityList = []
  214. this.districtList = []
  215. this.currentRegionLevel = 1
  216. this.loadRegionCities(this.getRegionId(item))
  217. } else if (this.currentRegionLevel === 1) {
  218. this.tempCity = item
  219. this.tempDistrict = null
  220. this.districtList = []
  221. this.currentRegionLevel = 2
  222. this.loadRegionDistricts(this.getRegionId(item))
  223. } else if (this.currentRegionLevel === 2) {
  224. this.tempDistrict = item
  225. this.confirmRegion()
  226. }
  227. },
  228. loadRegionCities(parentId) {
  229. if (!parentId) return
  230. var that = this
  231. that.regionLoading = true
  232. uni.request({
  233. url: config.api('/api/region/cities'),
  234. method: 'POST',
  235. data: { parentId: parentId },
  236. header: {
  237. 'Content-Type': 'application/json',
  238. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  239. },
  240. success: function(res) {
  241. that.regionLoading = false
  242. if (res.data && res.data.code === 200) {
  243. that.cityList = res.data.data || []
  244. }
  245. },
  246. fail: function() {
  247. that.regionLoading = false
  248. }
  249. })
  250. },
  251. loadRegionDistricts(parentId) {
  252. if (!parentId) return
  253. var that = this
  254. that.regionLoading = true
  255. uni.request({
  256. url: config.api('/api/region/districts'),
  257. method: 'POST',
  258. data: { parentId: parentId },
  259. header: {
  260. 'Content-Type': 'application/json',
  261. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  262. },
  263. success: function(res) {
  264. that.regionLoading = false
  265. if (res.data && res.data.code === 200) {
  266. that.districtList = res.data.data || []
  267. }
  268. },
  269. fail: function() {
  270. that.regionLoading = false
  271. }
  272. })
  273. },
  274. confirmRegion() {
  275. if (this.tempProvince) {
  276. this.form.province = this.getRegionName(this.tempProvince)
  277. }
  278. if (this.tempCity) {
  279. this.form.city = this.getRegionName(this.tempCity)
  280. }
  281. if (this.tempDistrict) {
  282. this.form.district = this.getRegionName(this.tempDistrict)
  283. }
  284. this.updateFullAddress()
  285. this.showRegionPicker = false
  286. },
  287. cancelRegion() {
  288. this.showRegionPicker = false
  289. },
  290. jumpRegionLevel(level) {
  291. if (level < this.currentRegionLevel) {
  292. this.currentRegionLevel = level
  293. }
  294. },
  295. getRegionName(item) {
  296. return item && (item.name || item.province || item.city || item.district || '')
  297. },
  298. getRegionId(item) {
  299. return item && (item.id || item.code)
  300. },
  301. isRegionSelected(item) {
  302. var name = this.getRegionName(item)
  303. if (this.currentRegionLevel === 0) return this.tempProvince && this.getRegionName(this.tempProvince) === name
  304. if (this.currentRegionLevel === 1) return this.tempCity && this.getRegionName(this.tempCity) === name
  305. if (this.currentRegionLevel === 2) return this.tempDistrict && this.getRegionName(this.tempDistrict) === name
  306. return false
  307. },
  308. updateFullAddress() {
  309. this.form.fullAddress = (this.form.province || '') + (this.form.city || '') + (this.form.district || '') + (this.form.street || '')
  310. },
  311. toggleDefault() {
  312. this.form.isDefault = this.form.isDefault === 1 ? 0 : 1
  313. },
  314. onSave() {
  315. // Validate
  316. if (!this.form.receiverName.trim()) {
  317. uni.showToast({ title: '请输入收货人姓名', icon: 'none' })
  318. return
  319. }
  320. if (!this.form.phone.trim() || this.form.phone.trim().length !== 11) {
  321. uni.showToast({ title: '请输入正确的手机号', icon: 'none' })
  322. return
  323. }
  324. if (!this.form.province && !this.form.fullAddress) {
  325. uni.showToast({ title: '请选择所在地区', icon: 'none' })
  326. return
  327. }
  328. this.saving = true
  329. var data = {
  330. receiverName: this.form.receiverName.trim(),
  331. phone: this.form.phone.trim(),
  332. province: this.form.province,
  333. city: this.form.city,
  334. district: this.form.district,
  335. street: this.form.street,
  336. isDefault: this.form.isDefault
  337. }
  338. if (this.addressId) {
  339. data.id = this.addressId
  340. }
  341. var that = this
  342. uni.request({
  343. url: this.addressId
  344. ? config.api('/api/user/address/update')
  345. : config.api('/api/user/address/create'),
  346. method: 'POST',
  347. data: data,
  348. header: {
  349. 'Content-Type': 'application/json',
  350. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  351. },
  352. success: function(res) {
  353. that.saving = false
  354. if (res.data && res.data.code === 200) {
  355. uni.showToast({ title: '保存成功', icon: 'success' })
  356. setTimeout(function() {
  357. uni.navigateBack()
  358. }, 1500)
  359. } else {
  360. uni.showToast({ title: res.data.message || '保存失败', icon: 'none' })
  361. }
  362. },
  363. fail: function() {
  364. that.saving = false
  365. uni.showToast({ title: '网络请求失败', icon: 'none' })
  366. }
  367. })
  368. }
  369. }
  370. }
  371. </script>
  372. <style scoped>
  373. .container {
  374. min-height: 100vh;
  375. background: #f5f5f5;
  376. }
  377. .form-wrap {
  378. padding: 20rpx;
  379. }
  380. .form-row {
  381. background: #fff;
  382. border-radius: 16rpx;
  383. padding: 24rpx 20rpx;
  384. margin-bottom: 20rpx;
  385. display: flex;
  386. align-items: center;
  387. }
  388. .form-label {
  389. font-size: 28rpx;
  390. color: #333;
  391. width: 160rpx;
  392. flex-shrink: 0;
  393. }
  394. .form-input {
  395. flex: 1;
  396. font-size: 28rpx;
  397. color: #333;
  398. text-align: right;
  399. height: 48rpx;
  400. line-height: 48rpx;
  401. }
  402. .address-row {
  403. cursor: pointer;
  404. }
  405. .address-value-wrap {
  406. flex: 1;
  407. display: flex;
  408. align-items: center;
  409. justify-content: flex-end;
  410. }
  411. .address-value {
  412. font-size: 28rpx;
  413. color: #333;
  414. text-align: right;
  415. max-width: 400rpx;
  416. overflow: hidden;
  417. text-overflow: ellipsis;
  418. white-space: nowrap;
  419. }
  420. .address-placeholder {
  421. font-size: 28rpx;
  422. color: #ccc;
  423. text-align: right;
  424. }
  425. .address-arrow {
  426. font-size: 36rpx;
  427. color: #ccc;
  428. margin-left: 8rpx;
  429. }
  430. .switch-row {
  431. justify-content: space-between;
  432. }
  433. .switch {
  434. width: 88rpx;
  435. height: 48rpx;
  436. background: #ddd;
  437. border-radius: 24rpx;
  438. position: relative;
  439. transition: background 0.3s;
  440. padding: 2rpx;
  441. }
  442. .switch.switch-on {
  443. background: #F97316;
  444. }
  445. .switch-knob {
  446. width: 44rpx;
  447. height: 44rpx;
  448. background: #fff;
  449. border-radius: 50%;
  450. position: absolute;
  451. left: 2rpx;
  452. top: 2rpx;
  453. transition: left 0.3s;
  454. box-shadow: 0 2rpx 4rpx rgba(0,0,0,0.2);
  455. }
  456. .switch-knob.knob-on {
  457. left: 42rpx;
  458. }
  459. .save-btn {
  460. margin-top: 40rpx;
  461. width: 100%;
  462. height: 88rpx;
  463. line-height: 88rpx;
  464. background: linear-gradient(135deg, #F97316, #FB923C);
  465. color: #fff;
  466. font-size: 32rpx;
  467. font-weight: bold;
  468. border-radius: 44rpx;
  469. border: none;
  470. text-align: center;
  471. }
  472. .save-btn::after {
  473. border: none;
  474. }
  475. .save-btn.disabled {
  476. background: #ddd;
  477. color: #999;
  478. }
  479. /* ===== 区域选择弹窗 ===== */
  480. .region-mask {
  481. position: fixed;
  482. top: 0;
  483. left: 0;
  484. right: 0;
  485. bottom: 0;
  486. background: rgba(0, 0, 0, 0.5);
  487. z-index: 999;
  488. display: flex;
  489. align-items: flex-end;
  490. }
  491. .region-picker {
  492. background: #fff;
  493. border-radius: 20rpx 20rpx 0 0;
  494. width: 100%;
  495. max-height: 70vh;
  496. display: flex;
  497. flex-direction: column;
  498. }
  499. .region-header {
  500. display: flex;
  501. justify-content: space-between;
  502. align-items: center;
  503. padding: 24rpx 30rpx;
  504. border-bottom: 1rpx solid #f0f0f0;
  505. }
  506. .region-header-btn {
  507. font-size: 28rpx;
  508. color: #999;
  509. }
  510. .region-confirm {
  511. color: #F97316;
  512. font-weight: bold;
  513. }
  514. .region-header-title {
  515. font-size: 30rpx;
  516. font-weight: bold;
  517. color: #333;
  518. }
  519. .region-breadcrumb {
  520. display: flex;
  521. align-items: center;
  522. padding: 20rpx 30rpx;
  523. border-bottom: 1rpx solid #f5f5f5;
  524. }
  525. .crumb-item {
  526. font-size: 26rpx;
  527. color: #999;
  528. padding: 8rpx 12rpx;
  529. border-radius: 8rpx;
  530. }
  531. .crumb-item.crumb-active {
  532. color: #F97316;
  533. font-weight: bold;
  534. }
  535. .crumb-arrow {
  536. font-size: 24rpx;
  537. color: #ccc;
  538. margin: 0 8rpx;
  539. }
  540. .region-list {
  541. max-height: 50vh;
  542. overflow-y: auto;
  543. }
  544. .region-item {
  545. display: flex;
  546. justify-content: space-between;
  547. align-items: center;
  548. padding: 28rpx 30rpx;
  549. border-bottom: 1rpx solid #f5f5f5;
  550. }
  551. .region-item:active {
  552. background: #FFF7ED;
  553. }
  554. .region-item-text {
  555. font-size: 28rpx;
  556. color: #333;
  557. }
  558. .region-item-check {
  559. font-size: 28rpx;
  560. color: #F97316;
  561. font-weight: bold;
  562. }
  563. .region-item-selected {
  564. background: #FFF7ED;
  565. }
  566. .region-item-selected .region-item-text {
  567. color: #F97316;
  568. font-weight: bold;
  569. }
  570. .region-loading {
  571. padding: 40rpx;
  572. text-align: center;
  573. font-size: 26rpx;
  574. color: #999;
  575. }
  576. </style>