addressEdit.vue 7.6 KB

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