editAddressModal.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <a-modal
  3. centered
  4. class="editAddress-modal"
  5. :footer="null"
  6. :maskClosable="false"
  7. title="收货地址"
  8. v-model="isShow"
  9. @cancel="isShow=false"
  10. :width="800">
  11. <a-spin :spinning="spinning" tip="Loading...">
  12. <a-form-model
  13. id="editAddress-form"
  14. ref="ruleForm"
  15. :model="form"
  16. :rules="rules"
  17. :label-col="formItemLayout.labelCol"
  18. :wrapper-col="formItemLayout.wrapperCol"
  19. >
  20. <a-form-model-item label="收货人" prop="consigneeName">
  21. <a-input
  22. id="editAddress-name"
  23. :maxLength="30"
  24. v-model.trim="form.consigneeName"
  25. placeholder="请输入收货人(最多30个字符)"
  26. allowClear />
  27. </a-form-model-item>
  28. <a-form-model-item label="联系电话" prop="consigneeTel">
  29. <a-input
  30. id="editAddress-phone"
  31. :maxLength="30"
  32. v-model.trim="form.consigneeTel"
  33. placeholder="请输入联系电话(最多30个字符)"
  34. allowClear />
  35. </a-form-model-item>
  36. <a-form-model-item label="所在地区" required style="margin: 0;">
  37. <a-row :gutter="20">
  38. <!-- 所在地区 -->
  39. <a-col span="8">
  40. <a-form-model-item prop="provinceSn">
  41. <a-select id="editAddress-provinceSn" allowClear @change="getCityList" v-model="form.provinceSn" placeholder="请选择省">
  42. <a-select-option v-for="item in addrProvinceList" :id="item.areaSn" :value="item.areaSn" :key="item.areaSn + 'a'">{{ item.name }}</a-select-option>
  43. </a-select>
  44. </a-form-model-item>
  45. </a-col>
  46. <a-col span="8">
  47. <a-form-model-item prop="citySn">
  48. <a-select id="editAddress-citySn" allowClear @change="getAreaList" v-model="form.citySn" placeholder="请选择市">
  49. <a-select-option v-for="item in addrCityList" :id="item.areaSn" :value="item.areaSn" :key="item.areaSn + 'b'">{{ item.name }}</a-select-option>
  50. </a-select>
  51. </a-form-model-item>
  52. </a-col>
  53. <a-col span="8">
  54. <a-form-model-item prop="countySn">
  55. <a-select id="editAddress-countySn" allowClear @change="areaCharged" v-model="form.countySn" placeholder="请选择区/县">
  56. <a-select-option v-for="item in addrDistrictList" :id="item.areaSn" :value="item.areaSn" :key="item.areaSn + 'c'">{{ item.name }}</a-select-option>
  57. </a-select>
  58. </a-form-model-item>
  59. </a-col>
  60. </a-row>
  61. </a-form-model-item>
  62. <a-form-model-item label="详细地址" prop="addr">
  63. <a-input
  64. id="editAddress-address"
  65. :maxLength="60"
  66. v-model.trim="form.addr"
  67. placeholder="请输入详细地址(最多60个字符)"
  68. allowClear />
  69. </a-form-model-item>
  70. <a-form-model-item :wrapper-col="{ span: 12, offset: 6 }" style="text-align: center;">
  71. <a-button type="primary" @click="handleSubmit" id="editAddress-btn-submit">保存</a-button>
  72. <a-button @click="isShow=false" style="margin-left: 15px" id="editAddress-btn-back">返回</a-button>
  73. </a-form-model-item>
  74. </a-form-model>
  75. </a-spin>
  76. </a-modal>
  77. </template>
  78. <script>
  79. import { commonMixin } from '@/utils/mixin'
  80. import { getArea } from '@/api/data'
  81. import { shippingAddressSave } from '@/api/shippingAddress.js'
  82. export default {
  83. name: 'EditAddressModal',
  84. mixins: [commonMixin],
  85. props: {
  86. openModal: { // 弹框显示状态
  87. type: Boolean,
  88. default: false
  89. }
  90. },
  91. data () {
  92. return {
  93. spinning: false,
  94. isShow: this.openModal, // 是否打开弹框
  95. isEdit: false,
  96. formItemLayout: {
  97. labelCol: { span: 4 },
  98. wrapperCol: { span: 18 }
  99. },
  100. form: {
  101. consigneeName: '', // 收货人
  102. consigneeTel: '', // 联系电话
  103. provinceSn: undefined, // 省
  104. provinceName: '',
  105. citySn: undefined, // 市
  106. cityName: '',
  107. countySn: undefined, // 区
  108. countyName: '',
  109. addr: ''// 详细地址
  110. },
  111. rules: {
  112. consigneeName: [
  113. { required: true, message: '请输入收货人', trigger: 'change' }
  114. ],
  115. consigneeTel: [
  116. { required: true, message: '请输入联系电话', trigger: 'change' }
  117. ],
  118. provinceSn: [
  119. { required: true, message: '请选择省', trigger: 'change' }
  120. ],
  121. citySn: [
  122. { required: true, message: '请选择市', trigger: 'change' }
  123. ],
  124. countySn: [
  125. { required: true, message: '请选择区', trigger: 'change' }
  126. ],
  127. addr: [
  128. { required: true, message: '请输入详细地址', trigger: 'change' }
  129. ]
  130. },
  131. addrProvinceList: [], // 省下拉
  132. addrCityList: [], // 市下拉
  133. addrDistrictList: [] // 区下拉
  134. }
  135. },
  136. methods: {
  137. // 保存
  138. handleSubmit () {
  139. this.$refs.ruleForm.validate(valid => {
  140. if (valid) {
  141. this.spinning = true
  142. shippingAddressSave(this.form).then(res => {
  143. if (res.status == 200) {
  144. this.isShow = false
  145. this.$emit('ok')
  146. this.$message.success(res.message)
  147. this.spinning = false
  148. } else {
  149. this.spinning = false
  150. }
  151. })
  152. } else {
  153. return false
  154. }
  155. })
  156. },
  157. // 获取城市列表
  158. getCityList (val) {
  159. this.addrCityList = []
  160. this.addrDistrictList = []
  161. this.form.citySn = undefined
  162. this.form.countySn = undefined
  163. if (val) {
  164. this.getArea('city', val)
  165. this.form.provinceName = this.addrProvinceList.find(item => item.areaSn == val).name
  166. } else {
  167. this.form.provinceName = ''
  168. }
  169. },
  170. // 获取区县列表
  171. getAreaList (val) {
  172. this.addrDistrictList = []
  173. this.form.countySn = undefined
  174. if (val) {
  175. this.getArea('district', val)
  176. this.form.cityName = this.addrCityList.find(item => item.areaSn == val).name
  177. } else {
  178. this.form.cityName = ''
  179. }
  180. },
  181. // 区县变更
  182. areaCharged (val) {
  183. if (val) {
  184. this.form.countyName = this.addrDistrictList.find(item => item.areaSn == val).name
  185. } else {
  186. this.form.countyName = ''
  187. }
  188. },
  189. // 省/市/区
  190. getArea (leve, sn) {
  191. let params
  192. if (leve == 'province') {
  193. params = { type: '2' }
  194. } else {
  195. params = { parentId: sn, type: leve == 'city' ? '3' : '4' }
  196. }
  197. getArea(params).then(res => {
  198. if (res.status == 200) {
  199. if (leve == 'province') {
  200. this.addrProvinceList = res.data || []
  201. } else if (leve == 'city') {
  202. this.addrCityList = res.data || []
  203. } else if (leve == 'district') {
  204. this.addrDistrictList = res.data || []
  205. }
  206. } else {
  207. if (leve == 'province') {
  208. this.addrProvinceList = []
  209. } else if (leve == 'city') {
  210. this.addrCityList = []
  211. } else if (leve == 'district') {
  212. this.addrDistrictList = []
  213. }
  214. }
  215. })
  216. },
  217. // 编辑数据
  218. setData (data) {
  219. this.isEdit = true
  220. Promise.all([getArea({ type: '2' }), getArea({ parentId: data.provinceSn, type: '3' }), getArea({ parentId: data.citySn, type: '4' })]).then(res => {
  221. this.addrProvinceList = res[0].data
  222. const provinceName = this.addrProvinceList.find(item => item.id == data.provinceSn)
  223. if (provinceName && provinceName.name) {
  224. this.form.provinceName = provinceName.name
  225. }
  226. this.addrCityList = res[1].data
  227. const cityName = this.addrCityList.find(item => item.id == data.citySn)
  228. if (cityName && cityName.name) {
  229. this.form.cityName = cityName.name
  230. }
  231. this.addrDistrictList = res[2].data
  232. const countyName = this.addrDistrictList.find(item => item.id == data.countySn)
  233. if (countyName && countyName.name) {
  234. this.form.countyName = countyName.name
  235. }
  236. this.form = Object.assign({}, this.form, data)
  237. })
  238. }
  239. },
  240. watch: {
  241. // 父页面传过来的弹框状态
  242. openModal (newValue, oldValue) {
  243. this.isShow = newValue
  244. },
  245. // 重定义的弹框状态
  246. isShow (newValue, oldValue) {
  247. if (!newValue) {
  248. this.$emit('close')
  249. this.$refs.ruleForm.resetFields()
  250. this.addrCityList = []
  251. this.addrDistrictList = []
  252. this.isEdit = false
  253. delete this.form.id
  254. } else {
  255. if (!this.isEdit) {
  256. this.getArea('province')
  257. }
  258. }
  259. }
  260. }
  261. }
  262. </script>