拓恒河湖长制全民护河平台WEB端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
7.6KB

  1. // pages/login/login.js
  2. import {getCityTree} from '../../api/feeddback.js'
  3. import {getCityNameCode} from '../../api/login.js'
  4. import {getLocatonPermission} from '../../utils/getUserLocation.js'
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. // 区域选项列表
  11. provinceOptions: [],
  12. cityOptions: [],
  13. districtOptions: [],
  14. provinceCurrent: -1, // 当前选择省
  15. cityCurrent: -1,
  16. districtCurrent: -1,
  17. params: {}, // 筛选条件
  18. obj: {},
  19. path: '/pages/FirstPage/index',
  20. fromType: 'tabbar'
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad: function (options) {
  26. if(options) {
  27. this.setData({path: options.path, fromType: options.fromType})
  28. }
  29. },
  30. /**
  31. * 生命周期函数--监听页面显示
  32. */
  33. onShow: function () {
  34. // 隐藏返回图标
  35. wx.hideHomeButton();
  36. let positionParams = wx.getStorageSync('positionParams')
  37. let options = this.data.provinceOptions
  38. if(options.length === 0) {
  39. let provinceOptions = wx.getStorageSync('provinceOptions')
  40. if(provinceOptions.length) {
  41. this.setData({provinceOptions})
  42. if(Object.keys(positionParams).length) {
  43. let params = this.data.params
  44. params.provinceCode = positionParams.provinceCode
  45. params.cityCode = positionParams.cityCode
  46. params.districtCode = positionParams.districtCode
  47. this.setData({
  48. params,
  49. provinceCurrent: positionParams.provinceCurrent,
  50. cityCurrent: positionParams.cityCurrent,
  51. cityOptions: positionParams.cityOptions,
  52. districtCurrent: positionParams.districtCurrent,
  53. districtOptions: positionParams.districtOptions
  54. })
  55. } else {
  56. this.getUserLocation()
  57. }
  58. } else {
  59. this.getCityTreeList().then(res=> {
  60. if(Object.keys(positionParams).length) {
  61. let params = this.data.params
  62. params.provinceCode = positionParams.provinceCode
  63. params.cityCode = positionParams.cityCode
  64. params.districtCode = positionParams.districtCode
  65. this.setData({
  66. params,
  67. provinceCurrent: positionParams.provinceCurrent,
  68. cityCurrent: positionParams.cityCurrent,
  69. cityOptions: positionParams.cityOptions,
  70. districtCurrent: positionParams.districtCurrent,
  71. districtOptions: positionParams.districtOptions
  72. })
  73. } else {
  74. this.getUserLocation()
  75. }
  76. })
  77. }
  78. }
  79. },
  80. /**
  81. * 根据name查找list
  82. */
  83. getListByName(obj) {
  84. let provinceOptions = this.data.provinceOptions
  85. let provinceCurrent, cityCurrent, districtCurrent, cityOptions, districtOptions
  86. let params = this.data.params
  87. params.provinceCode = obj.provinceCode
  88. params.cityCode = obj.cityCode
  89. params.districtCode = obj.districtCode
  90. if(provinceOptions.length) {
  91. provinceOptions.forEach((item,index)=> {
  92. if(item.citycode === obj.provinceCode) {
  93. provinceCurrent = index
  94. cityOptions = item?.itemList || []
  95. if(cityOptions.length) {
  96. cityOptions.forEach((i, j)=> {
  97. if(i.citycode === obj.cityCode) {
  98. cityCurrent = j
  99. districtOptions = i?.itemList || []
  100. if (districtOptions.length) {
  101. districtOptions.forEach((m, n) => {
  102. if(m.citycode === obj.districtCode) {
  103. districtCurrent = n
  104. }
  105. })
  106. }
  107. }
  108. })
  109. }
  110. }
  111. })
  112. }
  113. this.setData({provinceCurrent, cityCurrent, districtCurrent, cityOptions, districtOptions, params})
  114. },
  115. /* 获取省市区 */
  116. getCityTreeList() {
  117. return new Promise((resolve)=> {
  118. getCityTree().then(res=> {
  119. wx.setStorage({
  120. key: 'provinceOptions',
  121. data: res.data
  122. })
  123. this.setData({
  124. provinceOptions: res.data
  125. })
  126. resolve(res)
  127. })
  128. })
  129. },
  130. /* 筛选 */
  131. updateAreaChange(e) {
  132. const type = e.target.dataset.type
  133. const current = e.detail.current
  134. let params = this.data.params
  135. let code = this.data[type+'Options'][current].citycode
  136. params[type+'Code'] = code
  137. let itemList = this.data[type+'Options'][current].itemList || []
  138. if(type === 'province') {
  139. this.setData({
  140. provinceCurrent: current,
  141. cityOptions: itemList,
  142. cityCurrent: -1,
  143. districtCurrent: -1
  144. })
  145. params.cityCode = ""
  146. params.districtCode = ""
  147. } else if(type === 'city') {
  148. this.setData({
  149. districtOptions: itemList,
  150. cityCurrent: current,
  151. districtCurrent: -1
  152. })
  153. params.districtCode = ""
  154. } else if(type === 'district') {
  155. this.setData({districtCurrent: current})
  156. }
  157. this.setData({
  158. params: params
  159. })
  160. },
  161. /* 根据current获取当前选中的值 */
  162. getValueByCurrent(current,type) {
  163. let code = this.data[type+'Options'][current].value
  164. return code
  165. },
  166. getUserLocation(e) {
  167. let cityObj = wx.getStorageSync('cityObj')
  168. if(!Object.keys(cityObj).length) {
  169. getLocatonPermission().then(res=> {
  170. wx.setStorage({
  171. key: 'location',
  172. data: res
  173. })
  174. let latlng = {}
  175. latlng.lat = res.latitude
  176. latlng.lng = res.longitude
  177. getCityNameCode(latlng).then(res=> {
  178. let cityObj = res.data
  179. this.setData({cityObj})
  180. this.getListByName(cityObj)
  181. })
  182. })
  183. }
  184. },
  185. /**
  186. * 进入首页
  187. */
  188. goHome() {
  189. let params = this.data.params
  190. let positionParams = Object.assign({}, params)
  191. positionParams.provinceCurrent = this.data.provinceCurrent
  192. positionParams.cityCurrent = this.data.cityCurrent
  193. positionParams.districtCurrent = this.data.districtCurrent
  194. positionParams.cityOptions = this.data.cityOptions
  195. positionParams.districtOptions = this.data.districtOptions
  196. wx.setStorage({
  197. key: 'positionParams',
  198. data: positionParams
  199. })
  200. const path = this.data.path
  201. const fromType = this.data.fromType
  202. if(fromType === 'page') {
  203. wx.redirectTo({
  204. url: path,
  205. })
  206. } else {
  207. wx.switchTab({
  208. url: path,
  209. })
  210. }
  211. }
  212. })