TeachingSet.vue 8.8 KB

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