addUserAddress.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div >
  3. <el-form ref="form" :model="form" :rules="rules" label-width="120px">
  4. <el-form-item label="收货人姓名" prop="realName">
  5. <el-input v-model="form.realName" placeholder="请输入收货人姓名" />
  6. </el-form-item>
  7. <el-form-item label="收货人电话" prop="phone">
  8. <el-input v-model="form.phone" placeholder="请输入收货人电话" />
  9. </el-form-item>
  10. <el-form-item label="收货地址" prop="district">
  11. <el-row :gutter="20">
  12. <el-col :span="6">
  13. <el-select @change="provinceChange" v-model="form.province" placeholder="请选择">
  14. <el-option
  15. v-for="item in province"
  16. :key="item.cityId"
  17. :label="item.name"
  18. :value="item.cityId">
  19. </el-option>
  20. </el-select>
  21. </el-col>
  22. <el-col :span="6">
  23. <el-select @change="cityChange" v-model="form.city" placeholder="请选择">
  24. <el-option
  25. v-for="item in city"
  26. :key="item.cityId"
  27. :label="item.name"
  28. :value="item.cityId">
  29. </el-option>
  30. </el-select>
  31. </el-col>
  32. <el-col :span="6">
  33. <el-select @change="districtChange" v-model="form.district" placeholder="请选择">
  34. <el-option
  35. v-for="item in district"
  36. :key="item.cityId"
  37. :label="item.name"
  38. :value="item.cityId">
  39. </el-option>
  40. </el-select>
  41. </el-col>
  42. </el-row>
  43. </el-form-item>
  44. <el-form-item label="详细地址" prop="detail">
  45. <el-input v-model="form.detail" placeholder="请输入收货人详细地址" />
  46. </el-form-item>
  47. </el-form>
  48. <div style="text-align:right;">
  49. <el-button type="primary" @click="submitForm">确 定</el-button>
  50. </div>
  51. </div>
  52. </template>
  53. <script>
  54. import { listUserAddress, getUserAddress, delUserAddress, addUserAddress, updateUserAddress, exportUserAddress } from "@/api/users/userAddress";
  55. import {getCitys} from "@/api/hisStore/city";
  56. export default {
  57. name: "add",
  58. data() {
  59. return {
  60. citys:[],
  61. province:[],
  62. city:[],
  63. district:[],
  64. // 表单参数
  65. form: {
  66. province:null,
  67. city:null,
  68. district:null,
  69. cityId:null,
  70. },
  71. // 表单校验
  72. rules: {
  73. userId: [
  74. { required: true, message: "会员不能为空", trigger: "blur" }
  75. ],
  76. realName: [
  77. { required: true, message: "姓名不能为空", trigger: "blur" }
  78. ],
  79. phone: [
  80. { required: true, message: "电话不能为空", trigger: "blur" }
  81. ],
  82. detail: [
  83. { required: true, message: "详细地址不能为空", trigger: "blur" }
  84. ],
  85. district: [
  86. { required: true, message: "收货地址不能为空", trigger: "blur" }
  87. ],
  88. }
  89. };
  90. },
  91. created(){
  92. this.getCityList();
  93. },
  94. methods: {
  95. districtChange(val){
  96. const item = this.district.find(i => i.cityId === val)
  97. this.form.district=item.name;
  98. },
  99. cityChange(val){
  100. const item = this.city.find(i => i.cityId === val)
  101. this.district = item.children
  102. this.form.district=null;
  103. this.form.city=item.name;
  104. this.form.cityId=val;
  105. },
  106. provinceChange(val){
  107. const item = this.citys.find(i => i.cityId === val)
  108. this.city = item.children
  109. this.district=[];
  110. this.form.city=null;
  111. this.form.district=null;
  112. this.form.province=item.name;
  113. },
  114. getCityList(){
  115. getCitys().then(res => {
  116. this.loading = false;
  117. this.citys = this.convertCityData(res.data)
  118. this.province=this.citys.filter(item => item.parentId===0 )
  119. })
  120. },
  121. convertCityData(array) {
  122. return array.map(item => {
  123. return {
  124. 'cityId': item.value,
  125. 'name': item.label,
  126. 'parentId': item.pid,
  127. 'children': item.children && this.convertCityData(item.children)
  128. }
  129. });
  130. },
  131. init(userId){
  132. this.form.userId=userId;
  133. },
  134. submitForm() {
  135. this.$refs["form"].validate(valid => {
  136. if (valid) {
  137. addUserAddress(this.form).then(response => {
  138. if (response.code === 200) {
  139. this.msgSuccess("新增成功");
  140. this.$emit("addUserAddress")
  141. }
  142. });
  143. }
  144. });
  145. },
  146. }
  147. };
  148. </script>
  149. <style scoped>
  150. </style>