chooseArea.js 1.48 KB
import wx from '../../utils/wx';
import event from '../../common/event';
import accountModel from '../../models/account/index';
import Yas from '../../common/yas';

const AREA_CACHE_KEY = 'area_cache_key';
let yas;
let app = getApp();

Page({
    data: {
        list: []
    },
    getArea: function() {
        accountModel.getArea()
            .then(res => {
                if (res && res.code === 200) {
                    let dataList = res.data;

                    if (dataList && dataList.length > 0) {
                        this.setData({
                            list: dataList
                        });

                        wx.setStorage({
                            key: AREA_CACHE_KEY,
                            data: dataList,
                        });
                    }
                } else {
                    this.useCache();
                }
            });
    },
    useCache: function() {
        let dataList = wx.getStorageSync(AREA_CACHE_KEY);

        this.setData({
            list: dataList
        });
    },
    chooseArea: function(e) {
        const code = e.currentTarget.dataset.code;
        const name = e.currentTarget.dataset.name;

        event.emit('choose-area', {
            code,
            name
        });
        wx.navigateBack({
            delta: 1
        });
    },
    onLoad: function() {
        yas = new Yas(app); // 实例化埋点
        yas.pageOpenReport(); // 进入页面后 上报
        this.getArea();
    }
});