address-edit.vue 11 KB

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