index.js 7.9 KB
let app = getApp();

// import {
//   mobile_list
// } from '/const/index';


// import {
//   areas
// } from '/const/area-new';

Page({

  data: {
    queryForm: {
      query: "",
    },

    formVisible: false,
    succVisible: false,
    shortCutVisible: false,

    tagIndex: 0,
    tagList: [{
        name: "推荐",
        val: "",
      },
      {
        name: "AAA+",
        val: "AAA+",
      },
      {
        name: "AABB+",
        val: "AABB+",
      },
      {
        name: "ABAB+",
        val: "ABAB+",
      },
      {
        name: "ABBA",
        val: "ABBA",
      },
      {
        name: "ABCAB",
        val: "ABCAB",
      },
      {
        name: "ABCD",
        val: "ABCD",
      },
      {
        name: "尾号6/8/9",
        val: "LAST689",
        fontSize: 26
      },
    ],

    page: 1,
    size: 10,
    totalPages: 0,

    // 总号码池
    numberPool: [],
    // 命中池
    targetNumberList: [],
    // 号码列表,当前展示号码
    numberList: [],
    // 当前选择号码
    curNumberItem: null,
  },
  onLoad(query) {

    this.initData();

    // let area = {};
    // let province_list = {};
    // let city_list = {};
    // let county_list = {};
    // areas.forEach(element => {
    //   province_list[element[0]] = element[1]
    //   city_list[element[2]] = element[3]
    //   county_list[element[4]] = element[5]
    // });
    // area.province_list = province_list;
    // area.city_list = city_list;
    // area.county_list = county_list;
    // console.log(area);

  },
  async initData() {
    let numberPool = await this.getSelectNumberList();
    app.func.sortByLength(numberPool)
    this.data.targetNumberList = numberPool;
    this.data.page = 1;
    this.refreshPage();


    // let beautyResAAA = app.func.checkBeautyNumberAAA('15014443959');
    // console.log("beautyResAAA:", beautyResAAA);

    // let beautyResAABB = app.func.checkBeautyNumberAABB('15011337986');
    // console.log("beautyResAABB:", beautyResAABB);

    // let beautyResABAB = app.func.checkBeautyNumberABAB('15056563959');
    // console.log("beautyResABAB:", beautyResABAB);

    // let beautyResABBA = app.func.checkBeautyNumberABBA('15056653959');
    // console.log("beautyResABBA:", beautyResABBA);

    // let beautyResABCAB = app.func.checkBeautyNumberABCAB('15056756959');
    // console.log("beautyResABCAB:", beautyResABCAB);

    // let beautyResABCD = app.func.checkBeautyNumberABCD('15054323959');
    // console.log("beautyResABCD:", beautyResABCD);

    // let beautyResLast = app.func.checkBeautyNumberLast('15054323959');
    // console.log("beautyResLast:", beautyResLast);
  },

  // 页面滚动
  onPageScroll(e) {
    let shortCutVisible = e.scrollTop > 700;
    this.setData({
      shortCutVisible
    });
  },

  toTop() {
    console.log("toTop");
    my.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },

  async onSearch() {
    let numberPool = await this.getSelectNumberList();
    this.setData({
      tagIndex: 0
    })
    // numberPool = app.util.shuffleArray(numberPool);
    this.data.targetNumberList = numberPool;
    this.data.page = 1;
    this.refreshPage();
  },

  onSearchConfirm() {
    this.onSearch();
  },

  /**
   * 获取
   */
  async getSelectNumberList() {
    let res = await app.get({
      url: app.api.selectNum,
      data: this.data.queryForm,
      loading: true,
    });
    // console.log(res);
    let funcList = [
      app.func.checkBeautyNumberAAA,
      app.func.checkBeautyNumberAABB,
      app.func.checkBeautyNumberABAB,
      app.func.checkBeautyNumberABBA,
      app.func.checkBeautyNumberABCAB,
      app.func.checkBeautyNumberABCD,
      app.func.checkBeautyNumberLast,
    ]

    res.forEach(element => {
      element.beautyList = []
      element.beautyObj = {}
      funcList.forEach(funElement => {
        let beautyRes = funElement(element.mobile);
        if (beautyRes.beautyNum) {
          element.beautyList.push(beautyRes);
          element.beautyObj[beautyRes.type] = beautyRes;
        }
      });
    });

    let numberPool = res.concat(this.data.numberPool);
    numberPool = app.util.uniqueArray(numberPool);
    this.data.numberPool = numberPool;
    return numberPool;
  },

  // 点击标签
  onTagItem(evt) {
    let item = app.tapData(evt);
    let index = app.tapData(evt, "index");
    this.setData({
      queryForm: {
        query: "",
      },
      tagIndex: index,
    })

    let numberPool = this.data.numberPool;
    numberPool = app.util.shuffleArray(numberPool);

    if (item.val) {
      let targetNumberList = []
      numberPool.forEach(element => {
        if (element.beautyObj[item.val]) {
          targetNumberList.push(element);
        }
      });

      targetNumberList = targetNumberList.concat(numberPool);
      targetNumberList = app.util.uniqueArray(targetNumberList);
      this.data.targetNumberList = targetNumberList;
    } else {

      // app.func.sortByLength(numberPool)
      this.data.targetNumberList = numberPool;
    }

    this.data.page = 1;
    this.refreshPage();
  },

  /**
   * 点击号码
   */
  onNumberItem(evt) {
    let item = app.tapData(evt);
    item.featureName = app.func.getFeatureName(item.mobile)
    this.setData({
      curNumberItem: item,
      formVisible: true,
    })
  },


  // 上一页
  onPagePrev() {
    this.data.page--;
    if (this.data.page < 1) {
      this.data.page = this.data.totalPages;
      // return;
    };
    this.refreshPage();
  },
  // 下一页
  onPageNext() {
    this.data.page++;

    if (this.data.page > this.data.totalPages) {
      this.data.page = 1;
      // return;
    }
    this.refreshPage();
  },

  refreshPage() {
    let listObj = app.util.paginateArray(this.data.targetNumberList, this.data.page, this.data.pageSize);
    let curTag = this.data.tagList[this.data.tagIndex];
    let curTagVal = curTag.val;
    let list = listObj.list;

    let query = this.data.queryForm.query;
    /**
     * 高亮数字
     * 
     * 1、如果query中有值,按query的值
     * 2、如果标签有值,按标签
     * 3、如果都没有,随机显示靓号
     */
    list.forEach(element => {

      let beautyObj = null;

      if (query) {
        beautyObj = {
          beautyNum: query,
          index: element.mobile.indexOf(query),
          len: query.length,
          type: "query",
        }
      } else if (element.beautyObj[curTagVal]) {
        beautyObj = element.beautyObj[curTagVal];
      } else {
        // 默认靓号提示
        let len = element.beautyList.length;
        if (len) {
          beautyObj = element.beautyList[Math.floor(Math.random() * len)] || null
        }
      }

      let numberDisplayList = []
      element.mobile.split("").forEach((ele, index) => {
        let numberObj = {
          number: ele,
          highlight: beautyObj && index >= beautyObj.index && index <= beautyObj.index + beautyObj.len - 1,
        }
        numberDisplayList.push(numberObj);
      });
      element.numberDisplayList = numberDisplayList;
    });

    this.setData({
      numberList: list,
      totalPages: listObj.totalPages,
    })
  },


  /**
   * input绑定
   * 多输入文本绑定
   * 在input/texarea中设置 data-name="属性值即可"
   */
  inputEdit(e) {
    let dataset = e.currentTarget.dataset;
    let value = e.detail.value;
    let name = dataset.name;
    this.setData({
      [name]: value
    });
  },


  evtcomp(evt) {
    console.log("evtcomp evt:", evt)
    let {
      name,
      data
    } = evt;
    // console.log("name:", name)
    switch (name) {
      case "_evt_hide":
        this.hideMask();
        break;
      case "_evt_submit_complete":
        this.setData({
          formVisible: false,
          succVisible: true,
        })
        break;
    }
  },


  // 关闭蒙层
  onHideMask() {
    this.setData({
      formVisible: false,
      succVisible: false,
    })
  },

  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: '',
      desc: '',
      path: '',
    };
  },
});