list.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const util = require('../../utils/util.js');
  2. const app = getApp();
  3. Page({
  4. data: {
  5. list:[],
  6. query: { isSelectCity:false},
  7. alpha: '',
  8. windowHeight: '',
  9. addBg: false
  10. },
  11. onReady(){
  12. var res = wx.getSystemInfoSync();
  13. this.setData({ windowHeight: res.windowHeight })
  14. },
  15. onLoad: function (option) {
  16. var self = this;
  17. if (option.query){
  18. self.setData({ query: { isSelectCity:true,url:option.url} });
  19. }
  20. // util.loadCity(function(res){
  21. // self.setData({ city: res.data.city });
  22. // }, 1);
  23. self.getCityList();
  24. },
  25. getCityList:function(){
  26. var that = this;
  27. util.ajax({
  28. func:"city_list"
  29. },function(res){
  30. if (res.code == 0) {
  31. that.setData({ list: res.data.pinyin });
  32. var query = wx.createSelectorQuery();
  33. query.select('#selector').boundingClientRect();
  34. query.exec(function (res) {
  35. that.apHeight = Math.ceil(res[0].top + res[0].height) / 20;
  36. that.apTop = res[0].top;
  37. });
  38. }
  39. });
  40. },
  41. handlerAlphaTap(e) {
  42. let { ap } = e.target.dataset;
  43. this.setData({ alpha: ap });
  44. },
  45. //滑动
  46. handlerMove(e) {
  47. let { list } = this.data;
  48. this.setData({ addBg: true });
  49. let rY = e.touches[0].clientY;//竖向滑动的距离
  50. if (rY >= 0) {
  51. let index = Math.ceil(Math.ceil(rY - this.apTop - this.apHeight) / this.apHeight);
  52. if (0 <= index < list.length) {
  53. let nonwAp = list[index];
  54. nonwAp && this.setData({ alpha: nonwAp.letter.toUpperCase() });
  55. }
  56. }
  57. },
  58. //滑动结束
  59. handlerEnd(e) {
  60. this.setData({ addBg: false });
  61. },
  62. setCity: function (event){
  63. var city = app.globalData.city;
  64. if (util.isEmpty(city)){
  65. city = {
  66. city:''
  67. };
  68. }
  69. app.globalData.previous = city.city;
  70. city.city = event.currentTarget.dataset.city;
  71. getApp().globalData.city = city;
  72. wx.setStorageSync("city", city);
  73. if (!this.data.query.isSelectCity){
  74. wx.navigateBack();
  75. }else{
  76. wx.reLaunch({
  77. url: this.data.query.url
  78. });
  79. }
  80. }
  81. })