addressEdit.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <view class="address-edit-page">
  3. <view class="form-card">
  4. <view class="form-row" v-for="item in formList" :key="item.key">
  5. <text class="label">{{ item.label }}</text>
  6. <input
  7. v-if="item.key !== 'region'"
  8. class="input"
  9. :placeholder="item.placeholder"
  10. v-model="form[item.key]"
  11. />
  12. <view v-else class="input region-input" @click="openPicker()">
  13. <text :class="{ placeholder: !regionText }">
  14. {{ regionText || "请选择省/市/区" }}
  15. </text>
  16. </view>
  17. </view>
  18. <view class="form-row " style="display: flex;align-items: center; justify-content: space-between;">
  19. <text class="label">设为默认地址</text>
  20. <uv-switch activeColor="#acf934" inactiveColor="#eee" v-model="form.isDefault" size="28"></uv-switch>
  21. </view>
  22. </view>
  23. <view class="save-btn" @click="save">保存</view>
  24. <uv-picker
  25. ref="picker"
  26. :columns="addressList"
  27. keyName="name"
  28. @change="change"
  29. @confirm="confirm"
  30. />
  31. </view>
  32. </template>
  33. <script>
  34. import addressData from "@/static/address/province.json";
  35. export default {
  36. data() {
  37. return {
  38. isEdit: false,
  39. form: {
  40. name: "",
  41. mobile: "",
  42. region: "",
  43. detail: "",
  44. zipcode: "",
  45. isDefault: false,
  46. },
  47. formList: [
  48. { key: "name", label: "收货人", placeholder: "填写收货人姓名" },
  49. { key: "mobile", label: "手机号", placeholder: "填写手机号" },
  50. { key: "region", label: "所在地区", placeholder: "省/市/区" },
  51. {
  52. key: "detail",
  53. label: "详细地址",
  54. placeholder: "填写详细地址和门牌号",
  55. },
  56. { key: "zipcode", label: "邮政编码", placeholder: "填写邮政编码" },
  57. ],
  58. pickerValue: [0, 0, 0],
  59. provinces: [],
  60. citys: [],
  61. areas: [],
  62. regionText: "",
  63. };
  64. },
  65. computed: {
  66. addressList() {
  67. return [
  68. Array.isArray(this.provinces) ? this.provinces : [],
  69. Array.isArray(this.citys) ? this.citys : [],
  70. Array.isArray(this.areas) ? this.areas : []
  71. ];
  72. },
  73. },
  74. onLoad(options) {
  75. this.initAddressData();
  76. if (options.id) {
  77. this.isEdit = true;
  78. uni.setNavigationBarTitle({ title: "编辑地址" });
  79. } else {
  80. uni.setNavigationBarTitle({ title: "添加新地址" });
  81. }
  82. },
  83. methods: {
  84. openPicker() {
  85. this.$refs.picker.open();
  86. },
  87. goBack() {
  88. uni.navigateBack();
  89. },
  90. initAddressData() {
  91. this.provinces = Array.isArray(addressData) ? addressData : [];
  92. this.citys = this.provinces[0]?.children || [];
  93. this.areas = this.citys[0]?.children || [];
  94. },
  95. change(e) {
  96. const { columnIndex, index, indexs } = e;
  97. if (columnIndex === 0) {
  98. this.citys = this.provinces[index]?.children || [];
  99. this.areas = this.citys[0]?.children || [];
  100. this.$refs.picker.setIndexs([index, 0, 0], true);
  101. } else if (columnIndex === 1) {
  102. this.areas = this.citys[index]?.children || [];
  103. this.$refs.picker.setIndexs(indexs, true);
  104. }
  105. },
  106. confirm(e) {
  107. this.regionText = `${e.value[0].name}/${e.value[1].name}/${e.value[2].name}`;
  108. this.form.region = this.regionText;
  109. this.showPicker = false;
  110. uni.showToast({
  111. icon: "none",
  112. title: this.regionText,
  113. });
  114. },
  115. // 校验方法
  116. validateForm() {
  117. // 姓名
  118. if (!this.form.name.trim()) {
  119. uni.showToast({ title: "请填写收货人姓名", icon: "none" });
  120. return false;
  121. }
  122. // 手机号
  123. if (!/^1[3-9]\\d{9}$/.test(this.form.mobile)) {
  124. uni.showToast({ title: "请填写正确的手机号", icon: "none" });
  125. return false;
  126. }
  127. // 地区
  128. if (!this.form.region.trim()) {
  129. uni.showToast({ title: "请选择所在地区", icon: "none" });
  130. return false;
  131. }
  132. // 详细地址
  133. if (!this.form.detail.trim()) {
  134. uni.showToast({ title: "请填写详细地址", icon: "none" });
  135. return false;
  136. }
  137. // 邮编(可选,填写时校验)
  138. if (this.form.zipcode && !/^\\d{6}$/.test(this.form.zipcode)) {
  139. uni.showToast({ title: "请填写正确的邮政编码", icon: "none" });
  140. return false;
  141. }
  142. return true;
  143. },
  144. save() {
  145. if (!this.validateForm()) return;
  146. // 保存逻辑
  147. if (this.isEdit) {
  148. // 编辑地址
  149. // TODO: 调用编辑接口
  150. uni.showToast({ title: "编辑成功", icon: "success" });
  151. } else {
  152. // 新增地址
  153. // TODO: 调用新增接口
  154. uni.showToast({ title: "添加成功", icon: "success" });
  155. }
  156. setTimeout(() => {
  157. uni.navigateBack();
  158. }, 800);
  159. },
  160. },
  161. };
  162. </script>
  163. <style lang="scss">
  164. page{
  165. background: #f2f6f2;
  166. }
  167. .address-edit-page {
  168. min-height: 100vh;
  169. background: #f2f6f2;
  170. ::v-deep .uni-input-placeholder{
  171. color: #bbb !important;
  172. }
  173. .form-card {
  174. background: #fff;
  175. border-radius: 18rpx;
  176. margin: 32rpx;
  177. padding: 32rpx 0;
  178. .form-row {
  179. display: flex;
  180. align-items: center;
  181. border-bottom: 1rpx solid #f2f6f2;
  182. padding: 24rpx 32rpx;
  183. .label {
  184. width: 180rpx;
  185. font-size: 28rpx;
  186. color: #222;
  187. }
  188. .input {
  189. flex: 1;
  190. font-size: 28rpx;
  191. color: #222;
  192. border: none;
  193. background: transparent;
  194. }
  195. switch {
  196. margin-left: auto;
  197. }
  198. }
  199. .form-row:last-child {
  200. border-bottom: none;
  201. }
  202. }
  203. .save-btn {
  204. width: 80vw;
  205. height: 88rpx;
  206. background: #1f1f1f;
  207. color: #acf934;
  208. border-radius: 44rpx;
  209. margin: 48rpx auto 0 auto;
  210. text-align: center;
  211. line-height: 88rpx;
  212. font-size: 32rpx;
  213. font-weight: bold;
  214. }
  215. .region-input {
  216. min-height: 48rpx;
  217. line-height: 48rpx;
  218. color: #222;
  219. border-bottom: 1rpx solid #eee;
  220. padding: 0 16rpx;
  221. .placeholder {
  222. color: #bbb;
  223. }
  224. }
  225. }
  226. </style>