address-edit.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. <!-- 区域选择弹窗:统一使用 components/address-picker.vue(3 级省市区) -->
  56. <address-picker
  57. ref="addressPicker"
  58. :levels="3"
  59. :show-trigger="false"
  60. :initial="regionInitial"
  61. @change="onRegionChange"
  62. />
  63. <!-- Save Button -->
  64. <button
  65. :class="['save-btn', saving ? 'disabled' : '']"
  66. :disabled="saving"
  67. @click="onSave"
  68. >
  69. {{ saving ? '保存中...' : '保存地址' }}
  70. </button>
  71. </view>
  72. </view>
  73. </template>
  74. <script>
  75. import config from '@/config.js'
  76. import AddressPicker from '@/components/address-picker.vue'
  77. export default {
  78. components: { AddressPicker },
  79. data() {
  80. return {
  81. addressId: null,
  82. saving: false,
  83. form: {
  84. receiverName: '',
  85. phone: '',
  86. province: '',
  87. city: '',
  88. district: '',
  89. street: '',
  90. isDefault: 0,
  91. fullAddress: ''
  92. }
  93. }
  94. },
  95. onLoad(options) {
  96. if (options.addressId) {
  97. this.addressId = parseInt(options.addressId)
  98. uni.setNavigationBarTitle({ title: '编辑地址' })
  99. this.loadAddress()
  100. } else {
  101. uni.setNavigationBarTitle({ title: '新增地址' })
  102. }
  103. },
  104. computed: {
  105. // 编辑回显:把已保存的省/市/区县名称传给统一地址组件,打开时按名称回选
  106. regionInitial() {
  107. return {
  108. province: this.form.province || '',
  109. city: this.form.city || '',
  110. district: this.form.district || ''
  111. }
  112. }
  113. },
  114. methods: {
  115. loadAddress() {
  116. var that = this
  117. uni.request({
  118. url: config.api('/api/user/address/get'),
  119. method: 'POST',
  120. data: { id: this.addressId },
  121. header: {
  122. 'Content-Type': 'application/json',
  123. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  124. },
  125. success: function(res) {
  126. if (res.data && res.data.code === 200) {
  127. var data = res.data.data || {}
  128. that.form.receiverName = data.receiverName || ''
  129. that.form.phone = data.phone || ''
  130. that.form.province = data.province || ''
  131. that.form.city = data.city || ''
  132. that.form.district = data.district || ''
  133. that.form.street = data.street || ''
  134. that.form.isDefault = data.isDefault || 0
  135. that.form.fullAddress = (data.province || '') + (data.city || '') + (data.district || '') + (data.street || '')
  136. }
  137. }
  138. })
  139. },
  140. // ===== 区域选择(统一组件 components/address-picker.vue) =====
  141. openRegionPicker() {
  142. if (this.$refs.addressPicker) {
  143. this.$refs.addressPicker.open()
  144. }
  145. },
  146. onRegionChange(data) {
  147. this.form.province = data.province || ''
  148. this.form.city = data.city || ''
  149. this.form.district = data.district || ''
  150. this.updateFullAddress()
  151. },
  152. updateFullAddress() {
  153. this.form.fullAddress = (this.form.province || '') + (this.form.city || '') + (this.form.district || '') + (this.form.street || '')
  154. },
  155. toggleDefault() {
  156. this.form.isDefault = this.form.isDefault === 1 ? 0 : 1
  157. },
  158. onSave() {
  159. // Validate
  160. if (!this.form.receiverName.trim()) {
  161. uni.showToast({ title: '请输入收货人姓名', icon: 'none' })
  162. return
  163. }
  164. if (!this.form.phone.trim() || this.form.phone.trim().length !== 11) {
  165. uni.showToast({ title: '请输入正确的手机号', icon: 'none' })
  166. return
  167. }
  168. if (!this.form.province && !this.form.fullAddress) {
  169. uni.showToast({ title: '请选择所在地区', icon: 'none' })
  170. return
  171. }
  172. this.saving = true
  173. var data = {
  174. receiverName: this.form.receiverName.trim(),
  175. phone: this.form.phone.trim(),
  176. province: this.form.province,
  177. city: this.form.city,
  178. district: this.form.district,
  179. street: this.form.street,
  180. isDefault: this.form.isDefault
  181. }
  182. if (this.addressId) {
  183. data.id = this.addressId
  184. }
  185. var that = this
  186. uni.request({
  187. url: this.addressId
  188. ? config.api('/api/user/address/update')
  189. : config.api('/api/user/address/create'),
  190. method: 'POST',
  191. data: data,
  192. header: {
  193. 'Content-Type': 'application/json',
  194. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  195. },
  196. success: function(res) {
  197. that.saving = false
  198. if (res.data && res.data.code === 200) {
  199. uni.showToast({ title: '保存成功', icon: 'success' })
  200. setTimeout(function() {
  201. uni.navigateBack()
  202. }, 1500)
  203. } else {
  204. uni.showToast({ title: res.data.message || '保存失败', icon: 'none' })
  205. }
  206. },
  207. fail: function() {
  208. that.saving = false
  209. uni.showToast({ title: '网络请求失败', icon: 'none' })
  210. }
  211. })
  212. }
  213. }
  214. }
  215. </script>
  216. <style scoped>
  217. .container {
  218. min-height: 100vh;
  219. background: #f5f5f5;
  220. }
  221. .form-wrap {
  222. padding: 20rpx;
  223. }
  224. .form-row {
  225. background: #fff;
  226. border-radius: 16rpx;
  227. padding: 24rpx 20rpx;
  228. margin-bottom: 20rpx;
  229. display: flex;
  230. align-items: center;
  231. }
  232. .form-label {
  233. font-size: 28rpx;
  234. color: #333;
  235. width: 160rpx;
  236. flex-shrink: 0;
  237. }
  238. .form-input {
  239. flex: 1;
  240. font-size: 28rpx;
  241. color: #333;
  242. text-align: right;
  243. height: 48rpx;
  244. line-height: 48rpx;
  245. }
  246. .address-row {
  247. cursor: pointer;
  248. }
  249. .address-value-wrap {
  250. flex: 1;
  251. display: flex;
  252. align-items: center;
  253. justify-content: flex-end;
  254. }
  255. .address-value {
  256. font-size: 28rpx;
  257. color: #333;
  258. text-align: right;
  259. max-width: 400rpx;
  260. overflow: hidden;
  261. text-overflow: ellipsis;
  262. white-space: nowrap;
  263. }
  264. .address-placeholder {
  265. font-size: 28rpx;
  266. color: #ccc;
  267. text-align: right;
  268. }
  269. .address-arrow {
  270. font-size: 36rpx;
  271. color: #ccc;
  272. margin-left: 8rpx;
  273. }
  274. .switch-row {
  275. justify-content: space-between;
  276. }
  277. .switch {
  278. width: 88rpx;
  279. height: 48rpx;
  280. background: #ddd;
  281. border-radius: 24rpx;
  282. position: relative;
  283. transition: background 0.3s;
  284. padding: 2rpx;
  285. }
  286. .switch.switch-on {
  287. background: #F97316;
  288. }
  289. .switch-knob {
  290. width: 44rpx;
  291. height: 44rpx;
  292. background: #fff;
  293. border-radius: 50%;
  294. position: absolute;
  295. left: 2rpx;
  296. top: 2rpx;
  297. transition: left 0.3s;
  298. box-shadow: 0 2rpx 4rpx rgba(0,0,0,0.2);
  299. }
  300. .switch-knob.knob-on {
  301. left: 42rpx;
  302. }
  303. .save-btn {
  304. margin-top: 40rpx;
  305. width: 100%;
  306. height: 88rpx;
  307. line-height: 88rpx;
  308. background: linear-gradient(135deg, #F97316, #FB923C);
  309. color: #fff;
  310. font-size: 32rpx;
  311. font-weight: bold;
  312. border-radius: 44rpx;
  313. border: none;
  314. text-align: center;
  315. }
  316. .save-btn::after {
  317. border: none;
  318. }
  319. .save-btn.disabled {
  320. background: #ddd;
  321. color: #999;
  322. }
  323. </style>