index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. var city = require('./city.js');
  2. var $ = require('../../libs/gdconf.js');
  3. var app = getApp()
  4. Page({
  5. data: {
  6. searchLetter: [],
  7. showLetter: "",
  8. winHeight: 0,
  9. tHeight: 0,
  10. bHeight: 0,
  11. startPageY: 0,
  12. cityList: [],
  13. cityNameList: [],
  14. isShowLetter: false,
  15. scrollTop: 0,
  16. city: "",
  17. hotList: [],
  18. inputValue: "",
  19. searchList: []
  20. },
  21. onLoad: function(options) {
  22. var pcity = wx.getStorageSync('city') || '无锡'
  23. var hotList = ['北京', '上海', '南京', '杭州', '厦门', '南昌', '武汉']
  24. this.setData({
  25. city: pcity,
  26. hotList: hotList
  27. })
  28. // 生命周期函数--监听页面加载
  29. var searchLetter = city.searchLetter;
  30. var cityList = city.cityList();
  31. this.data.cityNameList = city.cityNameList();
  32. // console.log(cityInfo);
  33. var sysInfo = wx.getSystemInfoSync();
  34. console.log(sysInfo);
  35. var winHeight = sysInfo.windowHeight;
  36. //添加要匹配的字母范围值
  37. //1、更加屏幕高度设置子元素的高度
  38. var itemH = (winHeight - 50) / searchLetter.length;
  39. var tempObj = [];
  40. for (var i = 0; i < searchLetter.length; i++) {
  41. var temp = {};
  42. temp.name = searchLetter[i];
  43. temp.tHeight = i * itemH;
  44. temp.bHeight = (i + 1) * itemH;
  45. tempObj.push(temp)
  46. }
  47. this.setData({
  48. winHeight: winHeight,
  49. itemH: itemH,
  50. searchLetter: tempObj,
  51. cityList: cityList,
  52. })
  53. },
  54. searchStart: function(e) {
  55. var showLetter = e.currentTarget.dataset.letter;
  56. var pageY = e.touches[0].pageY;
  57. this.setScrollTop(this, showLetter);
  58. this.nowLetter(pageY, this);
  59. this.setData({
  60. showLetter: showLetter,
  61. startPageY: pageY,
  62. isShowLetter: true,
  63. })
  64. },
  65. searchMove: function(e) {
  66. var pageY = e.touches[0].pageY;
  67. var startPageY = this.data.startPageY;
  68. var tHeight = this.data.tHeight;
  69. var bHeight = this.data.bHeight;
  70. var showLetter = 0;
  71. // console.log(pageY);
  72. if (startPageY - pageY > 0) { //向上移动
  73. if (pageY < tHeight) {
  74. // showLetter=this.mateLetter(pageY,this);
  75. this.nowLetter(pageY, this);
  76. }
  77. } else { //向下移动
  78. if (pageY > bHeight) {
  79. // showLetter=this.mateLetter(pageY,this);
  80. this.nowLetter(pageY, this);
  81. }
  82. }
  83. },
  84. searchEnd: function(e) {
  85. // console.log(e);
  86. // var showLetter=e.currentTarget.dataset.letter;
  87. var that = this;
  88. setTimeout(function() {
  89. that.setData({
  90. isShowLetter: false
  91. })
  92. }, 1000)
  93. },
  94. nowLetter: function(pageY, that) { //当前选中的信息
  95. var letterData = this.data.searchLetter;
  96. var bHeight = 0;
  97. var tHeight = 0;
  98. var showLetter = "";
  99. for (var i = 0; i < letterData.length; i++) {
  100. if (letterData[i].tHeight <= pageY && pageY <= letterData[i].bHeight) {
  101. bHeight = letterData[i].bHeight;
  102. tHeight = letterData[i].tHeight;
  103. showLetter = letterData[i].name;
  104. break;
  105. }
  106. }
  107. this.setScrollTop(that, showLetter);
  108. that.setData({
  109. bHeight: bHeight,
  110. tHeight: tHeight,
  111. showLetter: showLetter,
  112. startPageY: pageY
  113. })
  114. },
  115. bindScroll: function(e) {
  116. // console.log(e.detail)
  117. },
  118. setScrollTop: function(that, showLetter) {
  119. var scrollTop = 0;
  120. var cityList = that.data.cityList;
  121. var cityCount = 0;
  122. var initialCount = 0;
  123. for (var i = 0; i < cityList.length; i++) {
  124. if (showLetter == cityList[i].initial) {
  125. scrollTop = initialCount * 30 + cityCount * 41;
  126. break;
  127. } else {
  128. initialCount++;
  129. cityCount += cityList[i].cityInfo.length;
  130. }
  131. }
  132. that.setData({
  133. scrollTop: scrollTop - 1558
  134. })
  135. },
  136. bindCity: function(e) {
  137. var city = e.currentTarget.dataset.city;
  138. this.setData({
  139. city: city
  140. })
  141. },
  142. wxSortPickerViewItemTap: function(e) {
  143. var city = e.target.dataset.text;
  144. console.log('选择了城市:', city);
  145. wx.setStorageSync('city', city)
  146. this.setData({
  147. city: city
  148. })
  149. wx.navigateBack()
  150. },
  151. cxgps: function(e) {
  152. var that = this;
  153. wx.showLoading({
  154. title: 'loading',
  155. mask: true
  156. });
  157. wx.getSetting({
  158. success: function(res) {
  159. if (!res.authSetting['scope.userInfo'] || !res.authSetting['scope.userLocation']) {
  160. wx.hideLoading();
  161. wx.showModal({
  162. title: '授权提示',
  163. content: '当前服务需要获取您的位置信息,请确认授权',
  164. confirmText: '去授权',
  165. cancelText: '暂不授权',
  166. success: function (res) {
  167. if (res.confirm) {
  168. wx.openSetting();
  169. } else if (res.cancel) {
  170. }
  171. }
  172. })
  173. } else {
  174. $.map.getRegeo({
  175. success(data) {
  176. wx.hideLoading();
  177. var data = data[0],
  178. city = data.regeocodeData.addressComponent.city || "无锡";
  179. that.setData({
  180. city: city
  181. })
  182. wx.setStorageSync('city', city)
  183. }
  184. })
  185. }
  186. }
  187. });
  188. },
  189. bindKeyInput(e) {
  190. var value = e.detail.value.trim()
  191. if (value == "") {
  192. this.setData({
  193. inputValue: value,
  194. searchList: []
  195. })
  196. return '';
  197. }
  198. this.setData({
  199. inputValue: value
  200. })
  201. this.serach(value)
  202. },
  203. serach(text) {
  204. var searchList = this.data.cityNameList.filter(function(name) {
  205. // console.log(name)
  206. return name.indexOf(text) !== -1
  207. })
  208. console.log(searchList)
  209. this.setData({
  210. searchList: searchList
  211. })
  212. },
  213. onError(err) {
  214. app.aldstat.sendEvent('报错', {
  215. 'err': err
  216. });
  217. },
  218. })
  219. //经纬度定位获取站点
  220. function ajaxGes(lat, lng) {
  221. //TODO:自己的定位接口
  222. var url = '';
  223. return new Promise(function(resolve, reject) {
  224. wx.request({
  225. url: url,
  226. header: {
  227. 'content-type': 'application/json'
  228. },
  229. success: function(res) {
  230. resolve(res.data);
  231. },
  232. fail: function(err) {
  233. reject(err);
  234. }
  235. })
  236. })
  237. }