homeNoLogin2.js 5.4 KB
import {
  request
} from '../../request/index.js'
var QQMapWX = require('../../qqmap/qqmap-wx-jssdk.min.js');
var qqmapsdk;
const a = getApp()
Page({
  data: {
    imagesUrl: a.globalData.baseUrl,
    //下面是字母排序
    letter: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
    cityListId: '',
    //下面是城市列表信息,这里只是模拟数据
    citylist: [],
    //下面是热门城市数据,模拟数据
    newcity: ['北京', '上海', '广州', '深圳', '成都', '杭州'],
    // citySel: '全国',
    locateCity: '',
    hotCity: [],
    cityInput: [],
    letterCITY: [],
    kw: '',
    city_id: '',
    dwcity_name: '',
    dwcity: '',
  },
  onLoad() {
    qqmapsdk = new QQMapWX({
      key: 'B4MBZ-3VICV-THBPQ-UCLHB-SLCEK-B7FIK'
    });
  },
  onShow() {
    this.getPromotion()
    this.getletter()
    // this.getDingWei()
    this.getCitied()
    let that = this,
      cityOrTime = wx.getStorageSync('locatecity') || {},
      time = new Date().getTime(),
      city = '';
    if (!cityOrTime.time || (time - cityOrTime.time > 1800000)) { //每隔30分钟请求一次定位
      // this.getLocate();
    } else { //如果未满30分钟,那么直接从本地缓存里取值
      that.setData({
        locateCity: cityOrTime.city
      })
    }
  },
  async getPromotion() { // 获取热门城市
    let that = this;
    try {
      const {
        data: {
          data
        }
      } = await
      request({
        url: 'api/citys/hotCity',
      })
      that.setData({
        hotCity: data
      })
      console.log(that.data.hotCity, "城市");

    } catch (err) {
      console.log(err);
      that.setData({
        msg: err.msg
      })
      that.popMaskTest()
    }
  },
  async getDingWei() { //推荐位置
    let that = this;
    try {
      const {
        data: {
          data
        }
      } = await
      request({
        url: 'api/citys/tuiCity',
      })
      that.setData({
        DingWei: data
      })
      // console.log(that.data.DingWei, "推荐位置");

    } catch (err) {
      console.log(err);
      that.setData({
        msg: err.msg
      })
      that.popMaskTest()
    }
  },
  getCitied() { // 获取城市信息
    let that = this
    wx.getLocation({
      type: 'wgs84', //默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标 
      success: function (res) {
        qqmapsdk.reverseGeocoder({
          //位置坐标,默认获取当前位置,非必须参数
          //Object格式
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (res) { //成功后的回调
            console.log(res);
            var res = res.result;
            that.setData({
              dwcity: res.ad_info.city,
              dwcity_name: res.ad_info.province
            })
          },
          fail: function (error) {
            console.error(error);
          },
          complete: function (res) {
            // console.log(res);
          }
        })
      },
      fail: res => {}
    })
    return
  },
  async getletter() { // 获取城市a-z
    let that = this;
    try {
      const {
        data: {
          data
        }
      } = await
      request({
        url: 'api/citys/allCity',
      })
      that.setData({
        letterCITY: data
      })
      // console.log(that.data.letterCITY, "城市a-z");

    } catch (err) {
      console.log(err);
      that.setData({
        msg: err.msg
      })
      that.popMaskTest()
    }
  },
  userNameInput() {
    wx.navigateTo({
      url: '/pages/addVehicle/addVehicle'
    })

  },
  //点击城市
  async cityTap(e) {
    const val = e.currentTarget.dataset.val || '',
      that = this;
    try {
      const {
        data
      } = await request({
        url: 'api/citys/updateCity',
        data: {
          id: val
        }
      })
      that.setData({
        msg: data.msg
      });
      // console.log(data);
      that.popMaskTest()
      setTimeout(() => {
        let pages = getCurrentPages(); //获取当前页面pages里的所有信息。
        let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。-2 是上一个页面,-3是上上个页面以此类推。                                                           
        prevPage.setData({ // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
          city_id: val,
        }) //上一个页面内执行setData操作,将我们想要的信息保存住。当我们返回去的时候,页面已经处理完毕。
        //最后就是返回上一个页面。
        wx.navigateBack({
          delta: 1, // 返回上一级页面。
          success: function () {
            console.log('成功!')
          }
        })
      }, 500);

    } catch (err) {
      that.setData({
        msg: err.msg
      })
      that.popMaskTest()
    }

  },

  //点击城市字母
  letterTap(e) {
    const Item = e.currentTarget.dataset.item;
    this.setData({
      cityListId: Item
    });
  },
  popMaskTest() {
    wx.showToast({
      title: this.data.msg,
      duration: 2000,
      icon: '',
      mask: true //是否有透明蒙层,默认为false 
      //如果有透明蒙层,弹窗的期间不能点击文档内容 
    })
  },

})