selectArea.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <template>
  2. <view class="container">
  3. <!-- 顶部选择校区区域 -->
  4. <view class="top">
  5. <uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="text" activeColor="#6FB6B8"></uni-segmented-control>
  6. </view>
  7. <view class="gap"></view>
  8. <!-- 主体内容区域 -->
  9. <view class="body">
  10. <!-- 左边区域 -->
  11. <view class="body_left">
  12. <view class="body_left_item" :class="{ active: active_left === index }" v-for="(item, index) in leftList" :key="index" @click="handleChange(index)">
  13. {{ item }}
  14. </view>
  15. </view>
  16. <!-- 右边区域 -->
  17. <view class="body_right">
  18. <view class="body_right_item" :class="{ active: active_right === index }" v-for="(item, index) in rightList" :key="index" @click="handleChange2(index)">
  19. {{ item.name }}
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. var QQMapWX = require('../util/qqmap-wx-jssdk1.1/qqmap-wx-jssdk')
  27. var qqmapsdk
  28. export default {
  29. data() {
  30. return {
  31. // 顶部分段器选项数组
  32. items: [],
  33. // 分段器当前索引
  34. current: 0,
  35. // 校区ID
  36. schoolId: 0,
  37. // 楼栋Id
  38. buildId: null,
  39. // 地址Id
  40. addressId: null,
  41. // 左边分类数组
  42. leftList: [],
  43. // 左边分类数组当前索引
  44. active_left: 0,
  45. // 右边分类数组
  46. rightList: [],
  47. // 右边分类数组当前索引
  48. active_right: null,
  49. // 报修区域数组
  50. areaTreeList: [],
  51. // 校区数组
  52. schoolList: [],
  53. // 用户定位经度
  54. myLng: 0,
  55. // 用户定位纬度
  56. myLat: 0
  57. }
  58. },
  59. onLoad(options) {
  60. // 实例化API核心类
  61. qqmapsdk = new QQMapWX({
  62. // 申请的key
  63. key: 'X57BZ-ZISE3-KTN3O-3P45H-C3J7Q-D5B67'
  64. })
  65. if (options.schoolId) {
  66. this.schoolId = options.schoolId
  67. this.buildId = options.buildId
  68. this.addressId = options.addressId
  69. }
  70. this.getSchoolList()
  71. },
  72. methods: {
  73. // 获取当前定位位置信息
  74. getLocationData() {
  75. uni.showLoading({
  76. title: '定位中,请稍后',
  77. mask: true
  78. })
  79. qqmapsdk.reverseGeocoder({
  80. success: (res) => {
  81. // console.log(res)
  82. if (res.status === 0) {
  83. let address = res.result.address
  84. // console.log(address)
  85. if (address.indexOf('南昌市') !== -1) {
  86. this.current = 0
  87. } else {
  88. this.current = 1
  89. }
  90. this.schoolId = this.schoolList[this.current].id
  91. this.getRepairAreaTree()
  92. } else {
  93. uni.showToast({
  94. title: '请求定位失败',
  95. icon: 'none'
  96. })
  97. }
  98. },
  99. fail: (error) => {
  100. uni.showModal({
  101. title: '提示',
  102. content: '定位失败,请手动选择校区',
  103. showCancel: false,
  104. confirmText: '确定',
  105. success: (res) => {
  106. if (res.confirm) {
  107. this.current = 0
  108. this.schoolId = this.schoolList[this.current].id
  109. this.getRepairAreaTree()
  110. }
  111. }
  112. })
  113. },
  114. complete: () => {
  115. uni.hideLoading()
  116. }
  117. })
  118. },
  119. // 获取校区数据
  120. async getSchoolList() {
  121. const res = await this.$myRequest_repairs({
  122. url: '/repairArea/queryRepairSchools'
  123. })
  124. // console.log(res)
  125. if (res.code === '200') {
  126. this.schoolList = res.data
  127. // 获取之前选择过的校区
  128. this.schoolList.forEach((ele, index) => {
  129. if (ele.id == this.schoolId) {
  130. this.current = index
  131. }
  132. })
  133. this.items = this.schoolList.map((ele) => ele.name)
  134. let schoolAddress = uni.getStorageSync('schoolAddress')
  135. if (schoolAddress) {
  136. this.items.forEach((ele, index) => {
  137. if (ele === schoolAddress) {
  138. this.current = index
  139. }
  140. })
  141. this.schoolId = this.schoolList[this.current].id
  142. this.getRepairAreaTree()
  143. } else {
  144. this.getLocationData()
  145. }
  146. }
  147. },
  148. // 获取区域数据
  149. async getRepairAreaTree() {
  150. const res = await this.$myRequest_repairs({
  151. url: '/repairArea/queryRepairAreaTree',
  152. data: {
  153. schoolId: this.schoolId
  154. }
  155. })
  156. // console.log(res)
  157. if (res.code === '200') {
  158. this.areaTreeList = res.data
  159. // 获取之前选择过的楼栋
  160. this.areaTreeList.forEach((ele, index) => {
  161. if (ele.id == this.buildId) {
  162. this.active_left = index
  163. }
  164. })
  165. this.leftList = this.areaTreeList.map((ele) => ele.name)
  166. this.rightList = this.areaTreeList[this.active_left].children || []
  167. // 获取之前选择过的地址
  168. this.rightList.forEach((ele, index) => {
  169. if (ele.id == this.addressId) {
  170. this.active_right = index
  171. }
  172. })
  173. }
  174. },
  175. // 顶部分段器切换选项回调
  176. onClickItem(e) {
  177. this.active_left = 0
  178. this.active_right = null
  179. this.buildId = null
  180. this.addressId = null
  181. if (this.current != e.currentIndex) {
  182. this.current = e.currentIndex
  183. this.schoolId = this.schoolList[this.current].id
  184. this.getRepairAreaTree()
  185. }
  186. },
  187. // 左边数组切换回调
  188. handleChange(index) {
  189. this.active_left = index
  190. this.active_right = null
  191. this.rightList = this.areaTreeList[this.active_left].children || []
  192. },
  193. // 右边数组切换回调
  194. handleChange2(index) {
  195. this.active_right = index
  196. const repairsArea = this.items[this.current] + this.leftList[this.active_left] + this.rightList[this.active_right].name
  197. uni.setStorageSync('schoolAddress', this.items[this.current])
  198. uni.$emit('addRepairsArea', {
  199. data: repairsArea,
  200. schoolId: this.schoolId,
  201. buildId: this.areaTreeList[this.active_left].id,
  202. addressId: this.rightList[this.active_right].id
  203. })
  204. uni.navigateBack(1)
  205. }
  206. }
  207. }
  208. </script>
  209. <style lang="scss" scoped>
  210. .container {
  211. width: 100vw;
  212. height: 100vh;
  213. .top {
  214. height: 100rpx;
  215. }
  216. .gap {
  217. height: 10rpx;
  218. background-color: #f2f2f2;
  219. }
  220. .body {
  221. display: flex;
  222. height: calc(100vh - 110rpx);
  223. .body_left {
  224. box-sizing: border-box;
  225. padding: 15rpx;
  226. width: 198rpx;
  227. border-right: 1rpx solid #ccc;
  228. overflow-y: auto;
  229. .body_left_item {
  230. width: 100%;
  231. height: 80rpx;
  232. line-height: 80rpx;
  233. text-align: center;
  234. font-size: 28rpx;
  235. overflow: hidden;
  236. white-space: nowrap;
  237. text-overflow: ellipsis;
  238. }
  239. .active {
  240. color: #6fb6b8;
  241. }
  242. }
  243. .body_right {
  244. box-sizing: border-box;
  245. padding: 30rpx 40rpx;
  246. width: 552rpx;
  247. overflow-y: auto;
  248. .body_right_item {
  249. float: left;
  250. box-sizing: border-box;
  251. margin: 0 12rpx 28rpx 0;
  252. padding: 10rpx 30rpx;
  253. height: 50rpx;
  254. line-height: 30rpx;
  255. text-align: center;
  256. color: #808080;
  257. font-size: 28rpx;
  258. border-radius: 53rpx;
  259. background-color: #e6e6e6;
  260. overflow: hidden;
  261. white-space: nowrap;
  262. text-overflow: ellipsis;
  263. }
  264. .active {
  265. color: #fff;
  266. background-color: #6fb6b8;
  267. }
  268. }
  269. }
  270. }
  271. ::v-deep .segmented-control {
  272. height: 100rpx;
  273. }
  274. </style>