address-edit.vue 9.1 KB

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