selectArea.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. export default {
  27. data() {
  28. return {
  29. // 顶部分段器选项数组
  30. items: [],
  31. // 分段器当前索引
  32. current: 0,
  33. // 校区ID
  34. schoolId: 0,
  35. // 楼栋Id
  36. buildId: null,
  37. // 地址Id
  38. addressId: null,
  39. // 左边分类数组
  40. leftList: [],
  41. // 左边分类数组当前索引
  42. active_left: 0,
  43. // 右边分类数组
  44. rightList: [],
  45. // 右边分类数组当前索引
  46. active_right: null,
  47. // 报修区域数组
  48. areaTreeList: [],
  49. // 校区数组
  50. schoolList: [],
  51. // 密匙
  52. key: 'ee7d39a2cc41bd2ffca08d3aa3fe5fe5'
  53. }
  54. },
  55. onLoad(options) {
  56. if (options.schoolId) {
  57. this.schoolId = options.schoolId
  58. this.buildId = options.buildId
  59. this.addressId = options.addressId
  60. }
  61. this.getSchoolList()
  62. },
  63. methods: {
  64. // 获取当前定位位置信息
  65. getLocationData() {
  66. uni.showLoading({
  67. title: '定位中,请稍后',
  68. mask: true
  69. })
  70. uni.getLocation({
  71. type: 'gcj02',
  72. success: (res) => {
  73. // 获取详细地址
  74. uni.request({
  75. url: `https://api.tianditu.gov.cn/geocoder?postStr={'lon':${res.longitude},'lat':${res.latitude},'ver':1}&type=geocode&tk=${this.key}`,
  76. success: (res) => {
  77. // console.log(res)
  78. let address = res.data.result.formatted_address
  79. if (address.indexOf('南昌市') !== -1) {
  80. this.current = 0
  81. } else {
  82. this.current = 1
  83. }
  84. this.schoolId = this.schoolList[this.current].id
  85. this.getRepairAreaTree()
  86. },
  87. fail: () => {
  88. uni.showModal({
  89. title: '提示',
  90. content: '定位失败,请手动选择校区',
  91. showCancel: false,
  92. confirmText: '确定',
  93. success: (res) => {
  94. if (res.confirm) {
  95. this.current = 0
  96. this.schoolId = this.schoolList[this.current].id
  97. this.getRepairAreaTree()
  98. }
  99. }
  100. })
  101. },
  102. complete: () => {
  103. uni.hideLoading()
  104. }
  105. })
  106. }
  107. })
  108. },
  109. // 获取校区数据
  110. async getSchoolList() {
  111. const res = await this.$myRequest_repairs({
  112. url: '/repairArea/queryRepairSchools'
  113. })
  114. // console.log(res)
  115. if (res.code === '200') {
  116. this.schoolList = res.data
  117. // 获取之前选择过的校区
  118. this.schoolList.forEach((ele, index) => {
  119. if (ele.id == this.schoolId) {
  120. this.current = index
  121. }
  122. })
  123. this.items = this.schoolList.map((ele) => ele.name)
  124. let schoolAddress = uni.getStorageSync('schoolAddress')
  125. if (schoolAddress) {
  126. this.items.forEach((ele, index) => {
  127. if (ele === schoolAddress) {
  128. this.current = index
  129. }
  130. })
  131. this.schoolId = this.schoolList[this.current].id
  132. this.getRepairAreaTree()
  133. } else {
  134. this.getLocationData()
  135. }
  136. }
  137. },
  138. // 获取区域数据
  139. async getRepairAreaTree() {
  140. const res = await this.$myRequest_repairs({
  141. url: '/repairArea/queryRepairAreaTree',
  142. data: {
  143. schoolId: this.schoolId
  144. }
  145. })
  146. // console.log(res)
  147. if (res.code === '200') {
  148. this.areaTreeList = res.data
  149. // 获取之前选择过的楼栋
  150. this.areaTreeList.forEach((ele, index) => {
  151. if (ele.id == this.buildId) {
  152. this.active_left = index
  153. }
  154. })
  155. this.leftList = this.areaTreeList.map((ele) => ele.name)
  156. this.rightList = this.areaTreeList[this.active_left].children || []
  157. // 获取之前选择过的地址
  158. this.rightList.forEach((ele, index) => {
  159. if (ele.id == this.addressId) {
  160. this.active_right = index
  161. }
  162. })
  163. }
  164. },
  165. // 顶部分段器切换选项回调
  166. onClickItem(e) {
  167. this.active_left = 0
  168. this.active_right = null
  169. this.buildId = null
  170. this.addressId = null
  171. if (this.current != e.currentIndex) {
  172. this.current = e.currentIndex
  173. this.schoolId = this.schoolList[this.current].id
  174. this.getRepairAreaTree()
  175. }
  176. },
  177. // 左边数组切换回调
  178. handleChange(index) {
  179. this.active_left = index
  180. this.active_right = null
  181. this.rightList = this.areaTreeList[this.active_left].children || []
  182. },
  183. // 右边数组切换回调
  184. handleChange2(index) {
  185. this.active_right = index
  186. const repairsArea = this.items[this.current] + this.leftList[this.active_left] + this.rightList[this.active_right].name
  187. uni.setStorageSync('schoolAddress', this.items[this.current])
  188. uni.$emit('addRepairsArea', {
  189. data: repairsArea,
  190. schoolId: this.schoolId,
  191. buildId: this.areaTreeList[this.active_left].id,
  192. addressId: this.rightList[this.active_right].id
  193. })
  194. uni.navigateBack(1)
  195. }
  196. }
  197. }
  198. </script>
  199. <style lang="scss" scoped>
  200. .container {
  201. width: 100vw;
  202. height: 100vh;
  203. .top {
  204. height: 100rpx;
  205. }
  206. .gap {
  207. height: 10rpx;
  208. background-color: #f2f2f2;
  209. }
  210. .body {
  211. display: flex;
  212. height: calc(100vh - 110rpx);
  213. .body_left {
  214. box-sizing: border-box;
  215. padding: 15rpx;
  216. width: 198rpx;
  217. border-right: 1rpx solid #ccc;
  218. overflow-y: auto;
  219. .body_left_item {
  220. width: 100%;
  221. height: 80rpx;
  222. line-height: 80rpx;
  223. text-align: center;
  224. font-size: 28rpx;
  225. overflow: hidden;
  226. white-space: nowrap;
  227. text-overflow: ellipsis;
  228. }
  229. .active {
  230. color: #6fb6b8;
  231. }
  232. }
  233. .body_right {
  234. box-sizing: border-box;
  235. padding: 30rpx 40rpx;
  236. width: 552rpx;
  237. overflow-y: auto;
  238. .body_right_item {
  239. float: left;
  240. box-sizing: border-box;
  241. margin: 0 12rpx 28rpx 0;
  242. padding: 10rpx 30rpx;
  243. height: 50rpx;
  244. line-height: 30rpx;
  245. text-align: center;
  246. color: #808080;
  247. font-size: 28rpx;
  248. border-radius: 53rpx;
  249. background-color: #e6e6e6;
  250. overflow: hidden;
  251. white-space: nowrap;
  252. text-overflow: ellipsis;
  253. }
  254. .active {
  255. color: #fff;
  256. background-color: #6fb6b8;
  257. }
  258. }
  259. }
  260. }
  261. ::v-deep .segmented-control {
  262. height: 100rpx;
  263. }
  264. </style>