|
- // pages/login/login.js
- import {getCityTree} from '../../api/feeddback.js'
- import {getCityNameCode} from '../../api/login.js'
- import {getLocatonPermission} from '../../utils/getUserLocation.js'
- Page({
-
- /**
- * 页面的初始数据
- */
- data: {
- // 区域选项列表
- provinceOptions: [],
- cityOptions: [],
- districtOptions: [],
- provinceCurrent: -1, // 当前选择省
- cityCurrent: -1,
- districtCurrent: -1,
- params: {}, // 筛选条件
- obj: {},
- path: '/pages/FirstPage/index',
- fromType: 'tabbar'
- },
-
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- if(options) {
- this.setData({path: options.path, fromType: options.fromType})
- }
- },
-
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- // 隐藏返回图标
- wx.hideHomeButton();
- let positionParams = wx.getStorageSync('positionParams')
- let options = this.data.provinceOptions
- if(options.length === 0) {
- let provinceOptions = wx.getStorageSync('provinceOptions')
- if(provinceOptions.length) {
- this.setData({provinceOptions})
- if(Object.keys(positionParams).length) {
- let params = this.data.params
- params.provinceCode = positionParams.provinceCode
- params.cityCode = positionParams.cityCode
- params.districtCode = positionParams.districtCode
- this.setData({
- params,
- provinceCurrent: positionParams.provinceCurrent,
- cityCurrent: positionParams.cityCurrent,
- cityOptions: positionParams.cityOptions,
- districtCurrent: positionParams.districtCurrent,
- districtOptions: positionParams.districtOptions
- })
- } else {
- this.getUserLocation()
- }
- } else {
- this.getCityTreeList().then(res=> {
- if(Object.keys(positionParams).length) {
- let params = this.data.params
- params.provinceCode = positionParams.provinceCode
- params.cityCode = positionParams.cityCode
- params.districtCode = positionParams.districtCode
- this.setData({
- params,
- provinceCurrent: positionParams.provinceCurrent,
- cityCurrent: positionParams.cityCurrent,
- cityOptions: positionParams.cityOptions,
- districtCurrent: positionParams.districtCurrent,
- districtOptions: positionParams.districtOptions
- })
- } else {
- this.getUserLocation()
- }
- })
- }
- }
- },
- /**
- * 根据name查找list
- */
- getListByName(obj) {
- let provinceOptions = this.data.provinceOptions
- let provinceCurrent, cityCurrent, districtCurrent, cityOptions, districtOptions
- let params = this.data.params
- params.provinceCode = obj.provinceCode
- params.cityCode = obj.cityCode
- params.districtCode = obj.districtCode
- if(provinceOptions.length) {
- provinceOptions.forEach((item,index)=> {
- if(item.citycode === obj.provinceCode) {
- provinceCurrent = index
- cityOptions = item?.itemList || []
- if(cityOptions.length) {
- cityOptions.forEach((i, j)=> {
- if(i.citycode === obj.cityCode) {
- cityCurrent = j
- districtOptions = i?.itemList || []
- if (districtOptions.length) {
- districtOptions.forEach((m, n) => {
- if(m.citycode === obj.districtCode) {
- districtCurrent = n
- }
- })
- }
- }
- })
- }
- }
- })
- }
- this.setData({provinceCurrent, cityCurrent, districtCurrent, cityOptions, districtOptions, params})
- },
-
- /* 获取省市区 */
- getCityTreeList() {
- return new Promise((resolve)=> {
- getCityTree().then(res=> {
- wx.setStorage({
- key: 'provinceOptions',
- data: res.data
- })
- this.setData({
- provinceOptions: res.data
- })
- resolve(res)
- })
- })
- },
- /* 筛选 */
- updateAreaChange(e) {
- const type = e.target.dataset.type
- const current = e.detail.current
- let params = this.data.params
- let code = this.data[type+'Options'][current].citycode
- params[type+'Code'] = code
- let itemList = this.data[type+'Options'][current].itemList || []
- if(type === 'province') {
- this.setData({
- provinceCurrent: current,
- cityOptions: itemList,
- cityCurrent: -1,
- districtCurrent: -1
- })
- params.cityCode = ""
- params.districtCode = ""
- } else if(type === 'city') {
- this.setData({
- districtOptions: itemList,
- cityCurrent: current,
- districtCurrent: -1
- })
- params.districtCode = ""
- } else if(type === 'district') {
- this.setData({districtCurrent: current})
- }
- this.setData({
- params: params
- })
- },
- /* 根据current获取当前选中的值 */
- getValueByCurrent(current,type) {
- let code = this.data[type+'Options'][current].value
- return code
- },
- getUserLocation(e) {
- let cityObj = wx.getStorageSync('cityObj')
- if(!Object.keys(cityObj).length) {
- getLocatonPermission().then(res=> {
- wx.setStorage({
- key: 'location',
- data: res
- })
- let latlng = {}
- latlng.lat = res.latitude
- latlng.lng = res.longitude
- getCityNameCode(latlng).then(res=> {
- let cityObj = res.data
- this.setData({cityObj})
- this.getListByName(cityObj)
- })
- })
- }
- },
- /**
- * 进入首页
- */
- goHome() {
- let params = this.data.params
- let positionParams = Object.assign({}, params)
- positionParams.provinceCurrent = this.data.provinceCurrent
- positionParams.cityCurrent = this.data.cityCurrent
- positionParams.districtCurrent = this.data.districtCurrent
- positionParams.cityOptions = this.data.cityOptions
- positionParams.districtOptions = this.data.districtOptions
- wx.setStorage({
- key: 'positionParams',
- data: positionParams
- })
- const path = this.data.path
- const fromType = this.data.fromType
- if(fromType === 'page') {
- wx.redirectTo({
- url: path,
- })
- } else {
- wx.switchTab({
- url: path,
- })
- }
- }
- })
|