model.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // model.js
  2. var area = require('../utils/area.js')
  3. var areaInfo = []; //所有省市区县数据
  4. var provinces = []; //省
  5. var citys = []; //城市
  6. var countys = []; //区县
  7. var value = [0, 0, 0]; //数据位置下标
  8. var info = {};
  9. function updateAreaData(that, status, e) {
  10. //获取省份数据
  11. var getProvinceData = function () {
  12. var s;
  13. provinces = [];
  14. var num = 0;
  15. for (var i = 0; i < areaInfo.length; i++) {
  16. s = areaInfo[i];
  17. if (s.di == "00" && s.xian == "00") {
  18. provinces[num] = s;
  19. num++;
  20. }
  21. }
  22. //初始化调一次
  23. //获取地级市数据
  24. getCityArr();
  25. //获取区县数据
  26. getCountyInfo();
  27. //模型赋值
  28. info = {
  29. item: {
  30. provinces: provinces,
  31. citys: citys,
  32. countys: countys,
  33. value: value
  34. }
  35. }
  36. animationEvents(that, 200, false, 0);
  37. }
  38. // 获取地级市数据
  39. var getCityArr = function (count = 0) {
  40. var c;
  41. citys = [];
  42. var num = 0;
  43. for (var i = 0; i < areaInfo.length; i++) {
  44. c = areaInfo[i];
  45. if (c.xian == "00" && c.sheng == provinces[count].sheng && c.di != "00") {
  46. citys[num] = c;
  47. num++;
  48. }
  49. }
  50. if (citys.length == 0) {
  51. citys[0] = {
  52. name: ''
  53. };
  54. }
  55. }
  56. // 获取区县数据
  57. var getCountyInfo = function (column0 = 0, column1 = 0) {
  58. var c;
  59. countys = [];
  60. var num = 0;
  61. for (var i = 0; i < areaInfo.length; i++) {
  62. c = areaInfo[i];
  63. if (c.xian != "00" && c.sheng == provinces[column0].sheng && c.di == citys[column1].di) {
  64. countys[num] = c;
  65. num++;
  66. }
  67. }
  68. if (countys.length == 0) {
  69. countys[0] = {
  70. name: ''
  71. };
  72. }
  73. value = [column0, column1, 0];
  74. }
  75. //滑动事件
  76. var valueChange = function (e, that) {
  77. var val = e.detail.value
  78. console.log(e)
  79. //判断滑动的是第几个column
  80. //若省份column做了滑动则定位到地级市和区县第一位
  81. if (value[0] != val[0]) {
  82. val[1] = 0;
  83. val[2] = 0;
  84. getCityArr(val[0]); //获取地级市数据
  85. getCountyInfo(val[0], val[1]); //获取区县数据
  86. } else { //若省份column未做滑动,地级市做了滑动则定位区县第一位
  87. if (value[1] != val[1]) {
  88. val[2] = 0;
  89. getCountyInfo(val[0], val[1]); //获取区县数据
  90. }
  91. }
  92. value = val;
  93. assignmentData(that, that.data.item.show)
  94. console.log(val);
  95. //回调
  96. //callBack(val);
  97. }
  98. if (status == 0) {
  99. area.getAreaInfo(function (arr) {
  100. areaInfo = arr;
  101. //获取省份数据
  102. getProvinceData();
  103. });
  104. }
  105. //滑动事件
  106. else {
  107. valueChange(e, that);
  108. }
  109. }
  110. //动画事件
  111. function animationEvents(that, moveY, show, duration) {
  112. that.animation = wx.createAnimation({
  113. transformOrigin: "50% 50%",
  114. duration: duration,
  115. timingFunction: "ease",
  116. delay: 0
  117. })
  118. that.animation.translateY(moveY + 'vh').step()
  119. //赋值
  120. assignmentData(that, show)
  121. }
  122. //赋值
  123. function assignmentData(that, show) {
  124. that.setData({
  125. item: {
  126. animation: that.animation.export(),
  127. show: show,
  128. provinces: provinces,
  129. citys: citys,
  130. countys: countys,
  131. value: value
  132. }
  133. })
  134. }
  135. module.exports = {
  136. updateAreaData: updateAreaData,
  137. animationEvents: animationEvents
  138. }