getLbsAmapRegion.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import { lbsAmapRegion } from '@/api/common.js'
  2. import geographicalPosi from '@/components/geographicalPosi/index.vue'
  3. export default {
  4. data() {
  5. return {
  6. provinceList: [],
  7. cityList: [],
  8. areaList: [],
  9. streetList: [],
  10. }
  11. },
  12. computed: {
  13. amapRegion() {
  14. return [
  15. {
  16. name: 'el-select',
  17. md: 6,
  18. formItemAttributes: {
  19. label: '客户地址', //省
  20. prop: 'provinceId',
  21. rules: [],
  22. errLabel: '省'
  23. },
  24. options: this.provinceList.map((v, i) => ({ value: v.id, label: v.name })),
  25. attributes: { disabled: true, clearable: true, filterable: true, placeholder: '请选择' },
  26. events: {
  27. change: (val) => {
  28. // 获取省名称
  29. this.formData.province = this.provinceList.find(item => item.id === val)?.name || ''
  30. // 清除市区街道以及详细地址数据
  31. this.delDataK(1)
  32. lbsAmapRegion({ pid: val }).then(res => {
  33. this.cityList = res.data
  34. })
  35. }
  36. }
  37. },
  38. {
  39. name: 'el-select',
  40. md: 5,
  41. formItemAttributes: {
  42. 'label-width': '0px',
  43. label: '', //市
  44. prop: 'cityId',
  45. rules: [],
  46. errLabel: '市'
  47. },
  48. options: this.cityList.map((v, i) => ({ value: v.id, label: v.name })),
  49. attributes: { disabled: true, clearable: true, filterable: true, placeholder: '请选择' },
  50. events: {
  51. change: (val) => {
  52. // 获取市名称
  53. this.formData.city = this.cityList.find(item => item.id === val).name
  54. // 清除区街道以及详细地址数据
  55. this.delDataK(2)
  56. lbsAmapRegion({ pid: val }).then(res => {
  57. this.areaList = res.data
  58. })
  59. }
  60. }
  61. },
  62. {
  63. name: 'el-select',
  64. md: 5,
  65. formItemAttributes: {
  66. 'label-width': '0px',
  67. label: '', //区
  68. prop: 'areaId',
  69. rules: [],
  70. errLabel: '区'
  71. },
  72. options: this.areaList.map((v, i) => ({ value: v.id, label: v.name })),
  73. attributes: { disabled: true, clearable: true, filterable: true, placeholder: '请选择' },
  74. events: {
  75. change: (val) => {
  76. // 获取区名称
  77. this.formData.area = this.areaList.find(item => item.id === val).name
  78. // 清除街道以及详细地址数据
  79. this.delDataK(3)
  80. lbsAmapRegion({ pid: val }).then(res => {
  81. this.streetList = res.data
  82. })
  83. }
  84. }
  85. },
  86. {
  87. name: 'el-select',
  88. md: 5,
  89. formItemAttributes: {
  90. 'label-width': '0px',
  91. label: '', //街道
  92. prop: 'streetId',
  93. rules: [],
  94. errLabel: '街道'
  95. },
  96. options: this.streetList.map((v, i) => ({ value: v.id, label: v.name })),
  97. attributes: { disabled: true, clearable: true, filterable: true, placeholder: '请选择' },
  98. events: {
  99. change: (val) => {
  100. // 获取街道名称
  101. this.formData.street = this.streetList.find(item => item.id === val).name
  102. // 清除详细地址数据
  103. this.delDataK(4)
  104. }
  105. }
  106. },
  107. {
  108. name: 'slot-component',
  109. md: 2,
  110. formItemAttributes: {
  111. 'label-width': '0px',
  112. prop: '',
  113. },
  114. render: (h, { props }) => {
  115. return (
  116. <geographicalPosi
  117. disabled={false}
  118. formData={this.formData}
  119. onSelectPosi={data => {
  120. // 获取经纬度
  121. this.formData.lng = data.center[0]
  122. this.formData.lat = data.center[1]
  123. // 获取定位的省市区街道
  124. var { province, city, district, township } = data.data.addressComponent
  125. // 获取选中省名称id
  126. var { id, name } = this.provinceList.find(item => item.name === province)
  127. this.formData.provinceId = id
  128. this.formData.province = name
  129. // 请求市选项
  130. lbsAmapRegion({ pid: this.formData.provinceId }).then(res => {
  131. // 赋值市选项
  132. this.cityList = res.data
  133. // 获取选中市名称id
  134. var { id, name } = res.data.find(item => item.name === city)
  135. this.formData.cityId = id
  136. this.formData.city = name
  137. // 请求区选项
  138. lbsAmapRegion({ pid: this.formData.cityId }).then(res => {
  139. // 赋值区选项
  140. this.areaList = res.data
  141. // 获取选中区名称id
  142. var { id, name } = res.data.find(item => item.name === district)
  143. this.formData.areaId = id
  144. this.formData.area = name
  145. // 请求街道选项
  146. lbsAmapRegion({ pid: this.formData.areaId }).then(res => {
  147. // 赋值街道选项
  148. this.streetList = res.data
  149. // 获取选中街道名称id
  150. var { id, name } = res.data.find(item => item.name === township)
  151. this.formData.streetId = id
  152. this.formData.street = name
  153. // 赋值GPS详细地址
  154. this.formData.gpsAddress = data.name
  155. if (!this.formData.address) {
  156. this.formData.address = data.name
  157. }
  158. })
  159. })
  160. })
  161. }}
  162. />
  163. )
  164. }
  165. },
  166. {
  167. name: 'el-input',
  168. md: 24,
  169. formItemAttributes: { label: '', prop: 'address', rules: [] },
  170. attributes: { disabled: false, placeholder: '详细地址' }
  171. },
  172. ]
  173. }
  174. },
  175. created(){
  176. this.getinitlbslist()
  177. },
  178. methods: {
  179. getinitlbslist() {
  180. // 初始化请求省市区街道下拉选项数据
  181. lbsAmapRegion({ pid: 0 }).then(res => {
  182. this.provinceList = res.data
  183. // 创建工单时获取ip地址定位赋值
  184. if (!this.id && !!~[1, 2, 3].indexOf(this?.workOrderType) && this.$IpAdd.province) {
  185. var item = this.provinceList.find(item => item.name === this.$IpAdd.province)
  186. if (item) {
  187. this.formData.provinceId = item.id
  188. this.formData.province = item.name
  189. }
  190. }
  191. if (this.formData.provinceId) {
  192. lbsAmapRegion({ pid: this.formData.provinceId }).then(res => {
  193. this.cityList = res.data
  194. // 创建工单时获取ip地址定位赋值
  195. if (!this.id && this.$IpAdd.city) {
  196. var item2 = this.cityList.find(item => item.name === this.$IpAdd.city)
  197. if (item2) {
  198. this.formData.cityId = item2.id
  199. this.formData.city = item2.name
  200. }
  201. }
  202. if (this.formData.cityId) {
  203. lbsAmapRegion({ pid: this.formData.cityId }).then(res => {
  204. this.areaList = res.data
  205. })
  206. }
  207. if (this.formData.areaId) {
  208. lbsAmapRegion({ pid: this.formData.areaId }).then(res => {
  209. this.streetList = res.data
  210. })
  211. }
  212. })
  213. }
  214. })
  215. },
  216. delDataK(num) {
  217. if (num <= 1) {
  218. // 删除市
  219. this.formData.cityId = ''
  220. this.formData.city = ''
  221. // 删除市选项
  222. this.cityList = []
  223. }
  224. if (num <= 2) {
  225. // 删除区
  226. this.formData.areaId = ''
  227. this.formData.area = ''
  228. // 删除区选项
  229. this.areaList = []
  230. }
  231. if (num <= 3) {
  232. // 删除街道
  233. this.formData.streetId = ''
  234. this.formData.street = ''
  235. // 删除街道选项
  236. this.streetList = []
  237. }
  238. // 删除gps地址
  239. this.formData.gpsAddress = ''
  240. }
  241. }
  242. }