editAddressModal.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <a-modal
  3. centered
  4. class="editAddress-modal"
  5. :footer="null"
  6. :maskClosable="false"
  7. title="收货地址"
  8. v-model="isShow"
  9. @cancle="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="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="11"
  32. v-model="form.consigneeTel"
  33. placeholder="请输入手机号码(最多11个字符)"
  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" @change="getCityList" v-model="form.provinceSn" placeholder="请选择省">
  42. <a-select-option v-for="item in addrProvinceList" :value="item.id" :key="item.id + '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" @change="getAreaList" v-model="form.citySn" placeholder="请选择市">
  49. <a-select-option v-for="item in addrCityList" :value="item.id" :key="item.id + '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" @change="areaCharged" v-model="form.countySn" placeholder="请选择区/县">
  56. <a-select-option v-for="item in addrDistrictList" :value="item.id" :key="item.id + '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="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 { getArea } from '@/api/data'
  80. import { shippingAddressSave } from '@/api/shippingAddress.js'
  81. export default {
  82. name: 'EditAddressModal',
  83. props: {
  84. openModal: { // 弹框显示状态
  85. type: Boolean,
  86. default: false
  87. }
  88. },
  89. data () {
  90. return {
  91. spinning: false,
  92. isShow: this.openModal, // 是否打开弹框
  93. isEdit: false,
  94. formItemLayout: {
  95. labelCol: { span: 4 },
  96. wrapperCol: { span: 18 }
  97. },
  98. form: {
  99. consigneeName: '',
  100. consigneeTel: '',
  101. provinceSn: undefined,
  102. provinceName: '',
  103. citySn: undefined,
  104. cityName: '',
  105. countySn: undefined,
  106. countyName: '',
  107. addr: ''
  108. },
  109. rules: {
  110. consigneeName: [
  111. { required: true, message: '请输入收货人', trigger: 'change' }
  112. ],
  113. consigneeTel: [
  114. { required: true, message: '请输入手机号', trigger: 'change' },
  115. { pattern: /^\d{11}$/, message: '请输入正确的手机号' }
  116. ],
  117. provinceSn: [
  118. { required: true, message: '请选择省', trigger: 'change' }
  119. ],
  120. citySn: [
  121. { required: true, message: '请选择市', trigger: 'change' }
  122. ],
  123. countySn: [
  124. { required: true, message: '请选择区', trigger: 'change' }
  125. ],
  126. addr: [
  127. { required: true, message: '请输入详细地址', trigger: 'change' }
  128. ]
  129. },
  130. addrProvinceList: [], // 省下拉
  131. addrCityList: [], // 市下拉
  132. addrDistrictList: [] // 区下拉
  133. }
  134. },
  135. methods: {
  136. // 保存
  137. handleSubmit () {
  138. this.$refs.ruleForm.validate(valid => {
  139. if (valid) {
  140. this.spinning = true
  141. shippingAddressSave(this.form).then(res => {
  142. if (res.status == 200) {
  143. this.isShow = false
  144. this.$emit('ok')
  145. this.$message.success(res.message)
  146. this.spinning = false
  147. } else {
  148. this.spinning = false
  149. }
  150. })
  151. } else {
  152. console.log('error submit!!')
  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. this.form.provinceName = this.addrProvinceList.find(item => item.id == val).name
  164. this.getArea('city', val)
  165. },
  166. // 获取区县列表
  167. getAreaList (val) {
  168. this.addrDistrictList = []
  169. this.form.countySn = undefined
  170. this.form.cityName = this.addrCityList.find(item => item.id == val).name
  171. this.getArea('district', val)
  172. },
  173. // 区县变更
  174. areaCharged (val) {
  175. this.form.countyName = this.addrDistrictList.find(item => item.id == val).name
  176. },
  177. // 省/市/区
  178. getArea (leve, sn) {
  179. let params
  180. if (leve == 'province') {
  181. params = { type: '2' }
  182. } else {
  183. params = { parentId: sn, type: leve == 'city' ? '3' : '4' }
  184. }
  185. getArea(params).then(res => {
  186. if (res.status == 200) {
  187. if (leve == 'province') {
  188. this.addrProvinceList = res.data || []
  189. } else if (leve == 'city') {
  190. this.addrCityList = res.data || []
  191. } else if (leve == 'district') {
  192. this.addrDistrictList = res.data || []
  193. }
  194. } else {
  195. if (leve == 'province') {
  196. this.addrProvinceList = []
  197. } else if (leve == 'city') {
  198. this.addrCityList = []
  199. } else if (leve == 'district') {
  200. this.addrDistrictList = []
  201. }
  202. }
  203. })
  204. },
  205. // 编辑数据
  206. setData (data) {
  207. this.isEdit = true
  208. Promise.all([getArea({ type: '2' }), getArea({ parentId: data.provinceSn, type: '3' }), getArea({ parentId: data.citySn, type: '4' })]).then(res => {
  209. console.log(res)
  210. this.addrProvinceList = res[0].data
  211. const provinceName = this.addrProvinceList.find(item => item.id == data.provinceSn)
  212. if (provinceName && provinceName.name) {
  213. this.form.provinceName = provinceName.name
  214. }
  215. this.addrCityList = res[1].data
  216. const cityName = this.addrCityList.find(item => item.id == data.citySn)
  217. if (cityName && cityName.name) {
  218. this.form.cityName = cityName.name
  219. }
  220. this.addrDistrictList = res[2].data
  221. const countyName = this.addrDistrictList.find(item => item.id == data.countySn)
  222. if (countyName && countyName.name) {
  223. this.form.countyName = countyName.name
  224. }
  225. this.form = Object.assign({}, this.form, data)
  226. })
  227. }
  228. },
  229. watch: {
  230. // 父页面传过来的弹框状态
  231. openModal (newValue, oldValue) {
  232. this.isShow = newValue
  233. },
  234. // 重定义的弹框状态
  235. isShow (newValue, oldValue) {
  236. if (!newValue) {
  237. this.$emit('close')
  238. this.$refs.ruleForm.resetFields()
  239. this.addrCityList = []
  240. this.addrDistrictList = []
  241. this.isEdit = false
  242. delete this.form.id
  243. } else {
  244. if (!this.isEdit) {
  245. this.getArea('province')
  246. }
  247. }
  248. }
  249. }
  250. }
  251. </script>