拓恒河湖长制全民护河平台WEB端
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

317 lines
11KB

  1. // pages/login/login.js
  2. import {getCityTree} from '../../api/feeddback.js'
  3. import {getCityNameCode, getTenantData, getUserData, enterMini} from '../../api/login.js'
  4. import {getOpenidData, getUserMessage} from '../../utils/getUserInfo.js'
  5. import {getLocatonPermission} from '../../utils/getUserLocation.js'
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. // 区域选项列表
  12. provinceOptions: [],
  13. cityOptions: [],
  14. districtOptions: [],
  15. provinceCurrent: -1, // 当前选择省
  16. cityCurrent: -1,
  17. districtCurrent: -1,
  18. params: {}, // 筛选条件
  19. obj: {},
  20. path: '/pages/FirstPage/index',
  21. fromType: 'tabbar',
  22. tenantId: '',
  23. tenantCurrent: -1, // 当前选择的租户数据index
  24. tenantOptions: [], // 租户列表
  25. formRules: {
  26. tenantId: {
  27. validator: function (value) {
  28. return value;
  29. },
  30. warning: false
  31. }
  32. }
  33. },
  34. /**
  35. * 生命周期函数--监听页面加载
  36. */
  37. onLoad: function (options) {
  38. if(options) {
  39. this.setData({path: options.path, fromType: options.fromType})
  40. }
  41. },
  42. /**
  43. * 生命周期函数--监听页面显示
  44. */
  45. onShow: function () {
  46. // 隐藏返回图标
  47. wx.hideHomeButton();
  48. let positionParams = wx.getStorageSync('positionParams')
  49. let options = this.data.provinceOptions
  50. if(options.length === 0) {
  51. let provinceOptions = wx.getStorageSync('provinceOptions')
  52. if(provinceOptions.length) {
  53. this.setData({provinceOptions})
  54. if(Object.keys(positionParams).length) {
  55. let params = this.data.params
  56. params.provinceCode = positionParams.provinceCode
  57. params.cityCode = positionParams.cityCode
  58. params.districtCode = positionParams.districtCode
  59. this.setData({
  60. params,
  61. provinceCurrent: positionParams.provinceCurrent,
  62. cityCurrent: positionParams.cityCurrent,
  63. cityOptions: positionParams.cityOptions,
  64. districtCurrent: positionParams.districtCurrent,
  65. districtOptions: positionParams.districtOptions
  66. })
  67. // 获取租户信息
  68. this.getTenantByParams(params)
  69. } else {
  70. this.getUserLocation()
  71. }
  72. } else {
  73. this.getCityTreeList().then(res=> {
  74. if(Object.keys(positionParams).length) {
  75. let params = this.data.params
  76. params.provinceCode = positionParams.provinceCode
  77. params.cityCode = positionParams.cityCode
  78. params.districtCode = positionParams.districtCode
  79. this.setData({
  80. params,
  81. provinceCurrent: positionParams.provinceCurrent,
  82. cityCurrent: positionParams.cityCurrent,
  83. cityOptions: positionParams.cityOptions,
  84. districtCurrent: positionParams.districtCurrent,
  85. districtOptions: positionParams.districtOptions
  86. })
  87. wx.setStorage({
  88. key: 'positionParams',
  89. data: positionParams
  90. })
  91. } else {
  92. this.getUserLocation()
  93. }
  94. })
  95. }
  96. }
  97. const tenantObj = wx.getStorageSync('tenant')
  98. if(tenantObj) {
  99. const {tenantCurrent, tenantId} = tenantObj
  100. this.setData({tenantCurrent, tenantId})
  101. }
  102. const openid = wx.getStorageSync('openid')
  103. if(openid) {
  104. this.setData({openid})
  105. } else {
  106. getOpenidData().then(res=> {
  107. this.setData({openid: res.openid})
  108. })
  109. }
  110. },
  111. validate(name) {
  112. let formRules = this.data.formRules;
  113. let validator = formRules[name].validator
  114. let result = validator ? !validator(this.data[name]) : false;
  115. formRules[name].warning = result
  116. this.setData({formRules})
  117. return result
  118. },
  119. validateForm() {
  120. return new Promise((resolve, reject) => {
  121. try {
  122. let formRules = this.data.formRules;
  123. let result = false;
  124. for (let key in formRules) {
  125. let temp = this.validate(key)
  126. if (temp) {
  127. result = temp
  128. }
  129. }
  130. resolve(!result)
  131. } catch (e) {
  132. reject(e)
  133. }
  134. })
  135. },
  136. /**
  137. * 根据省市区查询已有租户
  138. */
  139. getTenantByParams(params) {
  140. getTenantData(params).then(res=> {
  141. if(res.code === 0) {
  142. this.setData({
  143. tenantOptions: res.data
  144. })
  145. }
  146. })
  147. },
  148. /**
  149. * 选择租户
  150. */
  151. selectTenant(e) {
  152. const tenantCurrent = e.detail.value
  153. let tenantId = this.data.tenantOptions[tenantCurrent].id
  154. this.setData({
  155. tenantCurrent,
  156. tenantId
  157. })
  158. this.validate('tenantId')
  159. },
  160. /**
  161. * 根据name查找list
  162. */
  163. getListByName(obj) {
  164. let provinceOptions = this.data.provinceOptions
  165. let provinceCurrent, cityCurrent, districtCurrent, cityOptions, districtOptions
  166. let params = this.data.params
  167. params.provinceCode = obj.provinceCode
  168. params.cityCode = obj.cityCode
  169. params.districtCode = obj.districtCode
  170. if(provinceOptions.length) {
  171. provinceOptions.forEach((item,index)=> {
  172. if(item.citycode === obj.provinceCode) {
  173. provinceCurrent = index
  174. cityOptions = item?.itemList || []
  175. if(cityOptions.length) {
  176. cityOptions.forEach((i, j)=> {
  177. if(i.citycode === obj.cityCode) {
  178. cityCurrent = j
  179. districtOptions = i?.itemList || []
  180. if (districtOptions.length) {
  181. districtOptions.forEach((m, n) => {
  182. if(m.citycode === obj.districtCode) {
  183. districtCurrent = n
  184. }
  185. })
  186. }
  187. }
  188. })
  189. }
  190. }
  191. })
  192. }
  193. this.setData({provinceCurrent, cityCurrent, districtCurrent, cityOptions, districtOptions, params})
  194. },
  195. /* 获取省市区 */
  196. getCityTreeList() {
  197. return new Promise((resolve)=> {
  198. getCityTree().then(res=> {
  199. wx.setStorage({
  200. key: 'provinceOptions',
  201. data: res.data
  202. })
  203. this.setData({
  204. provinceOptions: res.data
  205. })
  206. resolve(res)
  207. })
  208. })
  209. },
  210. /* 筛选 */
  211. updateAreaChange(e) {
  212. // 清空租户信息
  213. this.setData({
  214. tenantId: '',
  215. tenantCurrent: -1,
  216. tenantOptions: [],
  217. })
  218. const type = e.target.dataset.type
  219. const current = e.detail.current
  220. let params = this.data.params
  221. let code = this.data[type+'Options'][current].citycode
  222. params[type+'Code'] = code
  223. let itemList = this.data[type+'Options'][current].itemList || []
  224. if(type === 'province') {
  225. this.setData({
  226. provinceCurrent: current,
  227. cityOptions: itemList,
  228. cityCurrent: -1,
  229. districtCurrent: -1
  230. })
  231. params.cityCode = ""
  232. params.districtCode = ""
  233. } else if(type === 'city') {
  234. this.setData({
  235. districtOptions: itemList,
  236. cityCurrent: current,
  237. districtCurrent: -1
  238. })
  239. params.districtCode = ""
  240. } else if(type === 'district') {
  241. this.setData({districtCurrent: current})
  242. }
  243. this.setData({
  244. params: params
  245. })
  246. this.getTenantByParams(params)
  247. },
  248. /* 根据current获取当前选中的值 */
  249. getValueByCurrent(current,type) {
  250. let code = this.data[type+'Options'][current].value
  251. return code
  252. },
  253. // 获取用户位置信息
  254. getUserLocation(e) {
  255. let cityObj = wx.getStorageSync('cityObj')
  256. if(!Object.keys(cityObj).length) {
  257. getLocatonPermission().then(res=> {
  258. wx.setStorage({
  259. key: 'location',
  260. data: res
  261. })
  262. let latlng = {}
  263. latlng.lat = res.latitude
  264. latlng.lng = res.longitude
  265. getCityNameCode(latlng).then(res=> {
  266. let cityObj = res.data
  267. const params = {
  268. provinceCode: cityObj.provinceCode,
  269. cityCode: cityObj.cityCode,
  270. districtCode: cityObj.districtCode
  271. }
  272. this.setData({cityObj})
  273. this.getListByName(cityObj)
  274. this.getTenantByParams(params)
  275. })
  276. })
  277. }
  278. },
  279. /**
  280. * 进入首页
  281. */
  282. goHome() {
  283. this.validateForm().then(res => {
  284. if(res) {
  285. enterMini({openid: this.data.openid, tenantId: this.data.tenantId}).then(item=> {
  286. if(item.code === 0) {
  287. const tenantObj = {
  288. tenantCurrent: this.data.tenantCurrent,
  289. tenantId: this.data.tenantId
  290. }
  291. wx.setStorage({
  292. key: 'tenant',
  293. data: tenantObj
  294. })
  295. // 进入首页
  296. const path = this.data.path
  297. const fromType = this.data.fromType
  298. if(fromType === 'page') {
  299. wx.redirectTo({
  300. url: path,
  301. })
  302. } else {
  303. wx.switchTab({
  304. url: path,
  305. })
  306. }
  307. }
  308. })
  309. }
  310. })
  311. }
  312. })