countryPhoneCode.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const app = getApp();
  2. const api = require('../../api/api');
  3. import countryCodeData from '../../mocks/countryCode';
  4. import {
  5. throttle
  6. } from '../../utils/util';
  7. Page({
  8. data: {
  9. navbarData: {
  10. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  11. title: '区号列表', //导航栏 中间的标题
  12. capsuleMode: 'navBack', //显示模式(navBack:返回上一页;navHome:返回首页)
  13. },
  14. countryCodeList: [],
  15. allCode: [],
  16. intoPosition: '',
  17. clickIndex: 0,
  18. topVal: '',
  19. titleHeight: 0,
  20. contentHeight: 0,
  21. height: 0
  22. },
  23. onLoad() {
  24. this.setData({
  25. height: app.globalData.height
  26. })
  27. let newCountryData = countryCodeData.map(item => {
  28. if (item.data && item.data.length) {
  29. return {
  30. ...item
  31. }
  32. }
  33. }).filter(item => item);
  34. this.setData({
  35. countryCodeList: newCountryData,
  36. allCode: newCountryData.map(item => item.key)
  37. })
  38. },
  39. onReady() {
  40. let totalTop = 0;
  41. const query = wx.createSelectorQuery()
  42. query.select('.title').boundingClientRect()
  43. query.select('.ind_content').boundingClientRect()
  44. query.selectViewport().scrollOffset()
  45. query.exec((res) => {
  46. let titleHeight = res[0].height;
  47. let contentHeight = res[1].height;
  48. this.data.countryCodeList.map(item => {
  49. let height = (item.data.length * contentHeight) + titleHeight;
  50. item.distance = totalTop;
  51. totalTop += height;
  52. return {
  53. ...item
  54. }
  55. })
  56. this.setData({
  57. countryCodeList: this.data.countryCodeList
  58. })
  59. })
  60. },
  61. jumpItemPosition(e) {
  62. let tip = e.currentTarget.dataset.info;
  63. let one = tip.split("-")[0];
  64. let two = tip.split("-")[1];
  65. this.setData({
  66. intoPosition: one,
  67. clickIndex: two,
  68. topVal: one
  69. })
  70. },
  71. handleScroll: throttle(function (e) {
  72. for (let i = 0; i < this.data.countryCodeList.length; i++) {
  73. if (i > 0 && e.detail.scrollTop < this.data.countryCodeList[i].distance) {
  74. this.setData({
  75. topVal: this.data.countryCodeList[i - 1].key,
  76. clickIndex: i - 1,
  77. })
  78. return;
  79. }
  80. }
  81. }),
  82. backInputPhone(e) {
  83. let code = e.currentTarget.dataset.code;
  84. let pages = getCurrentPages();
  85. let prevPage = pages[pages.length - 2]; //上一个页面
  86. wx.navigateBack({
  87. delta: 1
  88. })
  89. prevPage.setData({
  90. phoneCode: code
  91. })
  92. }
  93. })