addressEdit.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. // 自动填充邮编
  110. // let zipcode = '';
  111. // if (e.value[2] && (e.value[2].code || e.value[2].zipcode || e.value[2].postalCode)) {
  112. // zipcode = e.value[2].code || e.value[2].zipcode || e.value[2].postalCode;
  113. // }
  114. // this.form.zipcode = zipcode;
  115. this.showPicker = false;
  116. },
  117. // 校验方法
  118. validateForm() {
  119. console.log(this.form ,"填写的数据");
  120. // 姓名
  121. if (!this.form.name.trim()) {
  122. uni.showToast({ title: "请填写收货人姓名", icon: "none" });
  123. return false;
  124. }
  125. // 手机号
  126. if (!/^1[3-9]\d{9}$/.test(this.form.mobile.trim())) {
  127. uni.showToast({ title: "请填写正确的手机号", icon: "none" });
  128. return false;
  129. }
  130. // 地区
  131. if (!this.form.region.trim()) {
  132. uni.showToast({ title: "请选择所在地区", icon: "none" });
  133. return false;
  134. }
  135. // 详细地址
  136. if (!this.form.detail.trim()) {
  137. uni.showToast({ title: "请填写详细地址", icon: "none" });
  138. return false;
  139. }
  140. // 邮编(可选,填写时校验)
  141. if (this.form.zipcode && !/^\d{6}$/.test(this.form.zipcode.trim())) {
  142. uni.showToast({ title: "请填写正确的邮政编码", icon: "none" });
  143. return false;
  144. }
  145. return true;
  146. },
  147. save() {
  148. if (!this.validateForm()) return;
  149. // 组装参数
  150. const params = {
  151. realname: this.form.name,
  152. mobile: this.form.mobile,
  153. area: this.form.region.trim(),
  154. address: this.form.detail.trim(),
  155. is_default: this.form.isDefault ? 'yes' : 'no',
  156. uuid: getApp().globalData.uuid,
  157. skey: getApp().globalData.skey,
  158. };
  159. // 保存逻辑
  160. uni.request({
  161. url: this.$apiHost + '/Address/submitaddress',
  162. method: 'GET',
  163. data: params,
  164. success: (res) => {
  165. if (res.data && res.data.success === 'yes') {
  166. uni.showToast({ title: this.isEdit ? '编辑成功' : '添加成功', icon: 'success' });
  167. setTimeout(() => {
  168. uni.navigateBack();
  169. }, 800);
  170. } else {
  171. uni.showToast({ title: res.data.str || '保存失败', icon: 'none' });
  172. }
  173. },
  174. fail: () => {
  175. uni.showToast({ title: '网络错误', icon: 'none' });
  176. }
  177. });
  178. },
  179. },
  180. };
  181. </script>
  182. <style lang="scss">
  183. page{
  184. background: #f2f6f2;
  185. }
  186. .address-edit-page {
  187. min-height: 100vh;
  188. background: #f2f6f2;
  189. ::v-deep .uni-input-placeholder{
  190. color: #bbb !important;
  191. }
  192. .form-card {
  193. background: #fff;
  194. border-radius: 18rpx;
  195. margin: 32rpx;
  196. padding: 32rpx 0;
  197. .form-row {
  198. display: flex;
  199. align-items: center;
  200. border-bottom: 1rpx solid #f2f6f2;
  201. padding: 24rpx 32rpx;
  202. .label {
  203. width: 180rpx;
  204. font-size: 28rpx;
  205. color: #222;
  206. }
  207. .input {
  208. flex: 1;
  209. font-size: 28rpx;
  210. color: #222;
  211. border: none;
  212. background: transparent;
  213. }
  214. switch {
  215. margin-left: auto;
  216. }
  217. }
  218. .form-row:last-child {
  219. border-bottom: none;
  220. }
  221. }
  222. .save-btn {
  223. width: 80vw;
  224. height: 88rpx;
  225. background: #1f1f1f;
  226. color: #acf934;
  227. border-radius: 44rpx;
  228. margin: 48rpx auto 0 auto;
  229. text-align: center;
  230. line-height: 88rpx;
  231. font-size: 32rpx;
  232. font-weight: bold;
  233. }
  234. .region-input {
  235. min-height: 48rpx;
  236. line-height: 48rpx;
  237. color: #222;
  238. border-bottom: 1rpx solid #eee;
  239. padding: 0 16rpx;
  240. .placeholder {
  241. color: #bbb;
  242. }
  243. }
  244. }
  245. </style>