choosecountry.js 1.39 KB
import { API_HOST } from '../../../libs/config';
import { GET } from '../../../libs/request';
import { getYHStorageSync } from '../../../utils/util';

let CACH_KEY = 'country_cach';
let CHOOSED_COUNTRY_ID = 'CHOOSED_COUNTRY_ID'
let CHOOSED_COUNTRY_NAME = 'CHOOSED_COUNTRY_NAME'


Page({
  data: {
    list:[]
  },
  getArea: function () {
    let param={
      method: 'app.passport.getArea'
    }
    GET(API_HOST,param)
    .then(json=>{
      if(json && json.code == 200){
        let datalist = json.data;
        if(datalist && datalist.length>0){
          this.setData({
            list: datalist
          })
          wx.setStorage({
            key: CACH_KEY,
            data: datalist,
          })
        }
      }else{
        this.useCache();
      }
    })
    .catch(error=>{
      this.useCache();
    })
  },
  useCache:function(){
    let datalist = getYHStorageSync(CACH_KEY,'choosecountry');
    this.setData({
      list: datalist
    })
  },
  chooseCountry:function(e){
    let country = e.currentTarget.dataset.country;
    let country_area = e.currentTarget.dataset.area;
    let country_id = e.currentTarget.dataset.id;

    wx.setStorage({
      key: CHOOSED_COUNTRY_ID,
      data: country_area,
    })

    wx.setStorage({
      key: CHOOSED_COUNTRY_NAME,
      data: country,
    })
    wx.navigateBack({
      delta: 1
    })

  },
  onLoad: function (options) {
    this.getArea();
  }
})