address-edit.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. <!-- 省市区街道 (4级地址选择) -->
  27. <view class="form-row address-row" @click="showAddressPicker">
  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. <!-- Save Button -->
  56. <button
  57. :class="['save-btn', saving ? 'disabled' : '']"
  58. :disabled="saving"
  59. @click="onSave"
  60. >
  61. {{ saving ? '保存中...' : '保存地址' }}
  62. </button>
  63. </view>
  64. </view>
  65. </template>
  66. <script>
  67. import config from '@/config.js'
  68. export default {
  69. data() {
  70. return {
  71. addressId: null,
  72. userId: null,
  73. saving: false,
  74. form: {
  75. receiverName: '',
  76. phone: '',
  77. province: '',
  78. city: '',
  79. district: '',
  80. street: '',
  81. isDefault: 0,
  82. fullAddress: ''
  83. }
  84. }
  85. },
  86. onLoad(options) {
  87. this.userId = uni.getStorageSync('userId')
  88. if (options.addressId) {
  89. this.addressId = parseInt(options.addressId)
  90. uni.setNavigationBarTitle({ title: '编辑地址' })
  91. this.loadAddress()
  92. } else {
  93. uni.setNavigationBarTitle({ title: '新增地址' })
  94. }
  95. },
  96. methods: {
  97. loadAddress() {
  98. var that = this
  99. uni.request({
  100. url: config.danshop('/danshop/address/get'),
  101. method: 'POST',
  102. data: { id: this.addressId },
  103. header: {
  104. 'Content-Type': 'application/json',
  105. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  106. },
  107. success: function(res) {
  108. if (res.data && res.data.code === 200) {
  109. var data = res.data.data || {}
  110. that.form.receiverName = data.receiverName || ''
  111. that.form.phone = data.phone || ''
  112. that.form.province = data.province || ''
  113. that.form.city = data.city || ''
  114. that.form.district = data.district || ''
  115. that.form.street = data.street || ''
  116. that.form.isDefault = data.isDefault || 0
  117. that.form.fullAddress = (data.province || '') + (data.city || '') + (data.district || '') + (data.street || '')
  118. }
  119. }
  120. })
  121. },
  122. showAddressPicker() {
  123. // Use multi-column picker for region selection
  124. var that = this
  125. // Load provinces first
  126. uni.showLoading({ title: '加载地址...' })
  127. uni.request({
  128. url: config.danshop('/danshop/region/provinces'),
  129. method: 'POST',
  130. header: {
  131. 'Content-Type': 'application/json',
  132. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  133. },
  134. success: function(res) {
  135. uni.hideLoading()
  136. if (res.data && res.data.code === 200) {
  137. var provinces = res.data.data || []
  138. var provinceList = provinces.map(function(p) { return p.name || p.province })
  139. uni.showActionSheet({
  140. itemList: provinceList,
  141. success: function(pRes) {
  142. var selectedProvince = provinces[pRes.tapIndex]
  143. that.form.province = selectedProvince.name || selectedProvince.province
  144. var provinceId = selectedProvince.id || selectedProvince.code
  145. that.loadCities(provinceId)
  146. }
  147. })
  148. } else {
  149. // Fallback: simple text input for full address
  150. that.showSimpleAddressInput()
  151. }
  152. },
  153. fail: function() {
  154. uni.hideLoading()
  155. that.showSimpleAddressInput()
  156. }
  157. })
  158. },
  159. loadCities(provinceId) {
  160. var that = this
  161. uni.showLoading({ title: '加载城市...' })
  162. uni.request({
  163. url: config.danshop('/danshop/region/cities'),
  164. method: 'POST',
  165. data: { parentId: provinceId },
  166. header: {
  167. 'Content-Type': 'application/json',
  168. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  169. },
  170. success: function(res) {
  171. uni.hideLoading()
  172. if (res.data && res.data.code === 200) {
  173. var cities = res.data.data || []
  174. var cityList = cities.map(function(c) { return c.name || c.city })
  175. uni.showActionSheet({
  176. itemList: cityList,
  177. success: function(cRes) {
  178. var selectedCity = cities[cRes.tapIndex]
  179. that.form.city = selectedCity.name || selectedCity.city
  180. var cityId = selectedCity.id || selectedCity.code
  181. that.loadDistricts(cityId)
  182. }
  183. })
  184. }
  185. },
  186. fail: function() {
  187. uni.hideLoading()
  188. }
  189. })
  190. },
  191. loadDistricts(cityId) {
  192. var that = this
  193. uni.showLoading({ title: '加载区县...' })
  194. uni.request({
  195. url: config.danshop('/danshop/region/districts'),
  196. method: 'POST',
  197. data: { parentId: cityId },
  198. header: {
  199. 'Content-Type': 'application/json',
  200. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  201. },
  202. success: function(res) {
  203. uni.hideLoading()
  204. if (res.data && res.data.code === 200) {
  205. var districts = res.data.data || []
  206. var districtList = districts.map(function(d) { return d.name || d.district })
  207. uni.showActionSheet({
  208. itemList: districtList,
  209. success: function(dRes) {
  210. var selectedDistrict = districts[dRes.tapIndex]
  211. that.form.district = selectedDistrict.name || selectedDistrict.district
  212. that.updateFullAddress()
  213. }
  214. })
  215. }
  216. },
  217. fail: function() {
  218. uni.hideLoading()
  219. }
  220. })
  221. },
  222. showSimpleAddressInput() {
  223. var that = this
  224. uni.showModal({
  225. title: '请输入地址',
  226. content: '请输入省市区街道信息',
  227. editable: true,
  228. placeholderText: '如: 北京市朝阳区XX街道',
  229. success: function(res) {
  230. if (res.confirm && res.content) {
  231. that.form.fullAddress = res.content
  232. that.form.province = res.content
  233. }
  234. }
  235. })
  236. },
  237. updateFullAddress() {
  238. this.form.fullAddress = (this.form.province || '') + (this.form.city || '') + (this.form.district || '') + (this.form.street || '')
  239. },
  240. toggleDefault() {
  241. this.form.isDefault = this.form.isDefault === 1 ? 0 : 1
  242. },
  243. onSave() {
  244. // Validate
  245. if (!this.form.receiverName.trim()) {
  246. uni.showToast({ title: '请输入收货人姓名', icon: 'none' })
  247. return
  248. }
  249. if (!this.form.phone.trim() || this.form.phone.trim().length !== 11) {
  250. uni.showToast({ title: '请输入正确的手机号', icon: 'none' })
  251. return
  252. }
  253. if (!this.form.province && !this.form.fullAddress) {
  254. uni.showToast({ title: '请选择所在地区', icon: 'none' })
  255. return
  256. }
  257. this.saving = true
  258. var data = {
  259. userId: this.userId,
  260. receiverName: this.form.receiverName.trim(),
  261. phone: this.form.phone.trim(),
  262. province: this.form.province,
  263. city: this.form.city,
  264. district: this.form.district,
  265. street: this.form.street,
  266. isDefault: this.form.isDefault
  267. }
  268. if (this.addressId) {
  269. data.id = this.addressId
  270. }
  271. var that = this
  272. uni.request({
  273. url: config.danshop('/danshop/address/save'),
  274. method: 'POST',
  275. data: data,
  276. header: {
  277. 'Content-Type': 'application/json',
  278. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  279. },
  280. success: function(res) {
  281. that.saving = false
  282. if (res.data && res.data.code === 200) {
  283. uni.showToast({ title: '保存成功', icon: 'success' })
  284. setTimeout(function() {
  285. uni.navigateBack()
  286. }, 1500)
  287. } else {
  288. uni.showToast({ title: res.data.message || '保存失败', icon: 'none' })
  289. }
  290. },
  291. fail: function() {
  292. that.saving = false
  293. uni.showToast({ title: '网络请求失败', icon: 'none' })
  294. }
  295. })
  296. }
  297. }
  298. }
  299. </script>
  300. <style scoped>
  301. .container {
  302. min-height: 100vh;
  303. background: #f5f5f5;
  304. }
  305. .form-wrap {
  306. padding: 20rpx;
  307. }
  308. .form-row {
  309. background: #fff;
  310. border-radius: 16rpx;
  311. padding: 24rpx 20rpx;
  312. margin-bottom: 20rpx;
  313. display: flex;
  314. align-items: center;
  315. }
  316. .form-label {
  317. font-size: 28rpx;
  318. color: #333;
  319. width: 160rpx;
  320. flex-shrink: 0;
  321. }
  322. .form-input {
  323. flex: 1;
  324. font-size: 28rpx;
  325. color: #333;
  326. text-align: right;
  327. height: 48rpx;
  328. line-height: 48rpx;
  329. }
  330. .address-row {
  331. cursor: pointer;
  332. }
  333. .address-value-wrap {
  334. flex: 1;
  335. display: flex;
  336. align-items: center;
  337. justify-content: flex-end;
  338. }
  339. .address-value {
  340. font-size: 28rpx;
  341. color: #333;
  342. text-align: right;
  343. max-width: 400rpx;
  344. overflow: hidden;
  345. text-overflow: ellipsis;
  346. white-space: nowrap;
  347. }
  348. .address-placeholder {
  349. font-size: 28rpx;
  350. color: #ccc;
  351. text-align: right;
  352. }
  353. .address-arrow {
  354. font-size: 36rpx;
  355. color: #ccc;
  356. margin-left: 8rpx;
  357. }
  358. .switch-row {
  359. justify-content: space-between;
  360. }
  361. .switch {
  362. width: 88rpx;
  363. height: 48rpx;
  364. background: #ddd;
  365. border-radius: 24rpx;
  366. position: relative;
  367. transition: background 0.3s;
  368. padding: 2rpx;
  369. }
  370. .switch.switch-on {
  371. background: #F97316;
  372. }
  373. .switch-knob {
  374. width: 44rpx;
  375. height: 44rpx;
  376. background: #fff;
  377. border-radius: 50%;
  378. position: absolute;
  379. left: 2rpx;
  380. top: 2rpx;
  381. transition: left 0.3s;
  382. box-shadow: 0 2rpx 4rpx rgba(0,0,0,0.2);
  383. }
  384. .switch-knob.knob-on {
  385. left: 42rpx;
  386. }
  387. .save-btn {
  388. margin-top: 40rpx;
  389. width: 100%;
  390. height: 88rpx;
  391. line-height: 88rpx;
  392. background: linear-gradient(135deg, #F97316, #FB923C);
  393. color: #fff;
  394. font-size: 32rpx;
  395. font-weight: bold;
  396. border-radius: 44rpx;
  397. border: none;
  398. text-align: center;
  399. }
  400. .save-btn::after {
  401. border: none;
  402. }
  403. .save-btn.disabled {
  404. background: #ddd;
  405. color: #999;
  406. }
  407. </style>