TeachingSet.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <div class="teachingset">
  3. <m-header />
  4. <van-search
  5. v-model="search_value"
  6. shape="round"
  7. placeholder="搜小区、学校"
  8. background="transparent"
  9. show-action
  10. @search="onSearch">
  11. <div slot="action" @click="onSearch">搜索</div>
  12. </van-search>
  13. <el-amap :zoom="zoom" :events="amapEvents()" :plugin="plugin" :center="center">
  14. <el-amap-marker :clickable="true"
  15. :events="marker.events"
  16. :position="marker.position"
  17. v-for="(marker, index) in markers"
  18. :key="index" ></el-amap-marker>
  19. </el-amap>
  20. <van-cell-group>
  21. <van-field
  22. type="textarea"
  23. rows="1"
  24. autosize
  25. v-model="addressDetail"
  26. :disabled="true"
  27. label="教学地址"
  28. placeholder="详情地址" />
  29. <van-field
  30. type="text"
  31. v-model="teachingSchool"
  32. label="教学点名称"
  33. placeholder="请输入名称" />
  34. </van-cell-group>
  35. <div class="button-group">
  36. <van-button type="primary" @click="onSubmit" round size="large">确认</van-button>
  37. </div>
  38. </div>
  39. </template>
  40. <script>
  41. // import AMap from 'vue-amap'
  42. // AMap.initAMapApiLoader({
  43. // key: 'c7856e7c812d299cff150e74d60ea608',
  44. // plugin: ['Geolocation', 'PlaceSearch', 'Geocoder', 'ToolBar'],
  45. // v: '1.4.4'
  46. // })
  47. import MHeader from '@/components/MHeader'
  48. import { schoolAdd, schoolUpdate } from '@/api/teacher'
  49. export default {
  50. name: 'teachingset',
  51. components: { MHeader },
  52. data() {
  53. let self = this
  54. return {
  55. type: this.$route.query.type,
  56. search_value: '', // 搜索地址
  57. zoom: 12,
  58. center: [114.34371, 30.55939],
  59. markers: [],
  60. searchResult: [], // 搜索出来的数据
  61. plugin: [
  62. {
  63. pName: 'Geolocation',
  64. events: {
  65. init(o) {
  66. if(self.type == 'create') {
  67. o.getCurrentPosition((status, result) => {
  68. if(result && result.position) {
  69. self.lng = result.position.lng
  70. self.lat = result.position.lat
  71. self.center = [self.lng, self.lat]
  72. self.loaded = true
  73. self.$nextTick()
  74. }
  75. })
  76. }
  77. }
  78. }
  79. },
  80. {
  81. pName: 'ToolBar',
  82. events: {
  83. init() {
  84. // console.log(instrance)
  85. }
  86. }
  87. }],
  88. searchOption: {
  89. pageSize: 1, // 单页显示结果条数
  90. pageIndex: 1, // 页码
  91. autoFitView: true // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
  92. },
  93. addressDetail: null, // 输入详情地址
  94. teachingSchool: null, // 教学点
  95. lnglat: null, // 教学点经纬度
  96. }
  97. },
  98. mounted() {
  99. document.title = '教学点设置'
  100. let params = this.$route.query
  101. if(params.Authorization) {
  102. localStorage.setItem('Authorization', decodeURI(params.Authorization))
  103. localStorage.setItem('userInfo', decodeURI(params.Authorization))
  104. }
  105. if(params.type == 'update') {
  106. this.addressDetail = params.address
  107. this.teachingSchool = params.name
  108. this.lnglat = params.longitudeLatitude
  109. let tempLnglat = params.longitudeLatitude.split(',')
  110. this.center = [tempLnglat[0], tempLnglat[1]]
  111. this.markers.push({
  112. position: [tempLnglat[0], tempLnglat[1]],
  113. events: this.markerEvents()
  114. })
  115. }
  116. },
  117. methods: {
  118. amapEvents() {
  119. return {
  120. complete: () => {
  121. // this.$toast('加载完成')
  122. }
  123. }
  124. },
  125. onSubmit() { // 确定提交
  126. if(!this.addressDetail) {
  127. this.$toast('请选择教学地点')
  128. return
  129. }
  130. if(!this.teachingSchool) {
  131. this.$toast('请输入教学点名称')
  132. return
  133. }
  134. // 添加教学点
  135. if(this.type == 'create') {
  136. schoolAdd({
  137. name: this.teachingSchool,
  138. address: this.addressDetail,
  139. longitudeLatitude: this.lnglat
  140. }).then(res => {
  141. let result = res.data
  142. if(result.code == 200) {
  143. this.$toast('添加成功')
  144. this.$router.push('/teachingSchool')
  145. } else {
  146. this.$toast(result.msg)
  147. }
  148. })
  149. } else if(this.type == 'update') {
  150. schoolUpdate({
  151. id: this.$route.query.id,
  152. name: this.teachingSchool,
  153. address: this.addressDetail,
  154. longitudeLatitude: this.lnglat
  155. }).then(res => {
  156. let result = res.data
  157. if(result.code == 200) {
  158. this.$toast('修改成功')
  159. this.$router.push('/teachingSchool')
  160. } else {
  161. this.$toast(result.msg)
  162. }
  163. })
  164. }
  165. },
  166. onSearch() {
  167. if(!this.search_value) return
  168. this.markers = [] // 重置位置
  169. let mapSearch = new AMap.PlaceSearch(this.searchOption)
  170. // 目前只需要搜索第一条数据
  171. mapSearch.search(this.search_value, (status, sr) => {
  172. if(sr && sr.poiList && sr.poiList.count) {
  173. let pois = sr.poiList.pois[0]
  174. this.searchResult.push(pois)
  175. this.markers.push({
  176. position: [pois.location.lng, pois.location.lat],
  177. events: this.markerEvents(pois)
  178. })
  179. this.center = [pois.location.lng, pois.location.lat]
  180. } else if (sr.poiList === undefined) {
  181. throw new Error(sr);
  182. }
  183. })
  184. },
  185. markerEvents() {
  186. // marker 事件列表
  187. let that = this
  188. return {
  189. click: (e) => {
  190. let geocoder = new AMap.Geocoder()
  191. geocoder.getAddress(e.lnglat, function(status, result) {
  192. if(status === 'complete' && result.regeocode) {
  193. that.lnglat = e.lnglat.lng + ',' + e.lnglat.lat
  194. that.addressDetail = result.regeocode.formattedAddress
  195. } else {
  196. that.$toast('请重新选择地址')
  197. }
  198. })
  199. }
  200. }
  201. }
  202. }
  203. }
  204. </script>
  205. <style lang='less' scoped>
  206. @import url("../../assets/commonLess/variable.less");
  207. .teachingset {
  208. min-height: 100vh;
  209. }
  210. .el-vue-amap-container {
  211. height: 60vh;
  212. }
  213. /deep/.van-sticky--fixed {
  214. top: .44rem !important;
  215. }
  216. /deep/.van-search {
  217. position: fixed;
  218. top: .44rem !important;
  219. right: 0;
  220. left: 0;
  221. z-index: 99;
  222. padding: .1rem .23rem;
  223. .van-cell {
  224. padding: .12rem;
  225. }
  226. .van-field__control {
  227. font-size: .16rem;
  228. }
  229. .van-search__content {
  230. border-radius: .5rem;
  231. background: @whiteColor;
  232. box-shadow:0px 4px 8px 0px rgba(0,0,0,0.08),0px 0px 1px 0px rgba(0,0,0,0.08);
  233. }
  234. .van-icon-search {
  235. font-size: .2rem;
  236. font-weight: bold;
  237. color: @mColor;
  238. }
  239. .van-search__action {
  240. background-color: #89C8C4;
  241. color: #fff;
  242. border-radius: 0.15rem;
  243. position: absolute;
  244. right: 0.2rem;
  245. height: .48rem;
  246. display: inline-block;
  247. line-height: .5rem;
  248. border-radius: 2rem;
  249. border-top-left-radius: 0;
  250. border-bottom-left-radius: 0;
  251. padding: 0 .2rem;
  252. font-size: .16rem;
  253. }
  254. }
  255. .button-group {
  256. margin: .3rem .26rem .2rem;
  257. .van-button--primary {
  258. background: @mColor;
  259. border-color: @mColor;
  260. font-size: .18rem;
  261. }
  262. }
  263. </style>