index.js 7.8 KB

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