index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. var city = require('./city.js');
  2. var $ = require('../../libs/gdconf.js');
  3. import {
  4. imgServerUrl
  5. } from '../../config/config.js'
  6. var app = getApp()
  7. Page({
  8. data: {
  9. imgServerUrl,
  10. searchLetter: [],
  11. showLetter: "",
  12. winHeight: 0,
  13. tHeight: 0,
  14. bHeight: 0,
  15. startPageY: 0,
  16. cityList: [],
  17. cityNameList: [],
  18. isShowLetter: false,
  19. scrollTop: 0,
  20. city: "",
  21. cityCode: "",
  22. currcity: "",
  23. currcityCode: "",
  24. hotList: [],
  25. hotCodeList: [],
  26. inputValue: "",
  27. searchList: [],
  28. letter: ''
  29. },
  30. onLoad: function(options) {
  31. var currcity = wx.getStorageSync('currcity') || '无锡'
  32. var currcityCode = wx.getStorageSync('currcityCode') || '320200';
  33. var pcity = wx.getStorageSync('city') || '无锡'
  34. var pcityCode = wx.getStorageSync('cityCode') || '320200';
  35. // var hotList = ['北京', '上海', '南京', '杭州', '厦门', '南昌', '武汉']
  36. // var hotCodeList = ['110000', '310000', '320100', '330100', '350200', '360100', '420100'];
  37. var searchLetter = city.searchLetter;
  38. var cityList = city.cityList();
  39. let cityNameList = city.cityNameList();
  40. this.setData({
  41. currcity: currcity,
  42. currcityCode: currcityCode,
  43. city: pcity,
  44. cityCode: pcityCode,
  45. hotList: city.hotCity(),
  46. // hotCodeList: hotCodeList
  47. })
  48. // console.log("cityinfo", cityList);
  49. var sysInfo = wx.getSystemInfoSync();
  50. // console.log(sysInfo);
  51. var winHeight = sysInfo.windowHeight;
  52. //添加要匹配的字母范围值
  53. //1、更加屏幕高度设置子元素的高度
  54. var itemH = (winHeight - 50) / searchLetter.length;
  55. var tempObj = [];
  56. for (var i = 0; i < searchLetter.length; i++) {
  57. var temp = {};
  58. temp.name = searchLetter[i];
  59. temp.tHeight = i * itemH;
  60. temp.bHeight = (i + 1) * itemH;
  61. tempObj.push(temp)
  62. }
  63. this.setData({
  64. winHeight: winHeight,
  65. itemH: itemH,
  66. searchLetter: [{
  67. name: '#'
  68. }, {
  69. name: '热'
  70. }].concat(tempObj),
  71. cityList: cityList,
  72. cityNameList: cityNameList
  73. })
  74. },
  75. onShow() {
  76. let cityName = wx.getStorageSync('city') || '无锡';
  77. let cityCode = wx.getStorageSync('cityCode') || '320200';
  78. this.setData({
  79. cityName: cityName,
  80. cityCode: cityCode
  81. })
  82. },
  83. toView(e) {
  84. var showLetter = e.currentTarget.dataset.letter;
  85. let letter = showLetter;
  86. if (showLetter === '#' || showLetter === '热') {
  87. this.setData({
  88. showLetter,
  89. letter: 'A',
  90. isShowLetter: true,
  91. })
  92. } else {
  93. this.setData({
  94. showLetter,
  95. letter,
  96. isShowLetter: true,
  97. })
  98. }
  99. setTimeout(() => {
  100. this.setData({
  101. showLetter,
  102. isShowLetter: false,
  103. })
  104. }, 1000)
  105. },
  106. searchStart: function(e) {
  107. var showLetter = e.currentTarget.dataset.letter;
  108. // var pageY = e.touches[0].pageY;
  109. // this.setScrollTop(this, showLetter);
  110. // this.nowLetter(pageY, this);
  111. // this.setData({
  112. // showLetter: showLetter,
  113. // startPageY: pageY,
  114. // isShowLetter: true,
  115. // })
  116. if (showLetter === '#' || showLetter === '热') {
  117. wx.pageScrollTo({
  118. scrollTop: 0,
  119. duration: 300,
  120. })
  121. }
  122. this.setData({
  123. showLetter,
  124. isShowLetter: true,
  125. })
  126. },
  127. searchMove: function(e) {
  128. // var pageY = e.touches[0].pageY;
  129. // var startPageY = this.data.startPageY;
  130. // var tHeight = this.data.tHeight;
  131. // var bHeight = this.data.bHeight;
  132. // var showLetter = 0;
  133. // // console.log(pageY);
  134. // if (startPageY - pageY > 0) { //向上移动
  135. // if (pageY < tHeight) {
  136. // // showLetter=this.mateLetter(pageY,this);
  137. // this.nowLetter(pageY, this);
  138. // }
  139. // } else { //向下移动
  140. // if (pageY > bHeight) {
  141. // // showLetter=this.mateLetter(pageY,this);
  142. // this.nowLetter(pageY, this);
  143. // }
  144. // }
  145. var showLetter = e.currentTarget.dataset.letter;
  146. console.log(showLetter)
  147. if (showLetter === '#' || showLetter === '热') {
  148. wx.pageScrollTo({
  149. scrollTop: 0,
  150. duration: 300,
  151. })
  152. }
  153. this.setData({
  154. showLetter,
  155. isShowLetter: true,
  156. })
  157. },
  158. searchEnd: function(e) {
  159. // console.log(e);
  160. // var showLetter=e.currentTarget.dataset.letter;
  161. var that = this;
  162. setTimeout(function() {
  163. that.setData({
  164. isShowLetter: false
  165. })
  166. }, 1000)
  167. },
  168. nowLetter: function(pageY, that) { //当前选中的信息
  169. var letterData = this.data.searchLetter;
  170. var bHeight = 0;
  171. var tHeight = 0;
  172. var showLetter = "";
  173. for (var i = 0; i < letterData.length; i++) {
  174. if (letterData[i].tHeight <= pageY && pageY <= letterData[i].bHeight) {
  175. bHeight = letterData[i].bHeight;
  176. tHeight = letterData[i].tHeight;
  177. showLetter = letterData[i].name;
  178. break;
  179. }
  180. }
  181. this.setScrollTop(that, showLetter);
  182. that.setData({
  183. bHeight: bHeight,
  184. tHeight: tHeight,
  185. showLetter: showLetter,
  186. startPageY: pageY
  187. })
  188. },
  189. bindScroll: function(e) {
  190. // console.log(e.detail)
  191. },
  192. setScrollTop: function(that, showLetter) {
  193. var scrollTop = 0;
  194. var cityList = that.data.cityList;
  195. var cityCount = 0;
  196. var initialCount = 0;
  197. for (var i = 0; i < cityList.length; i++) {
  198. if (showLetter == cityList[i].firstNameLetter) {
  199. scrollTop = initialCount * 30 + cityCount * 41;
  200. break;
  201. } else {
  202. initialCount++;
  203. cityCount += cityList[i].cityInfo.length;
  204. }
  205. }
  206. that.setData({
  207. scrollTop: scrollTop - 1558
  208. })
  209. },
  210. bindCity: function(e) {
  211. var city = e.currentTarget.dataset.city;
  212. this.setData({
  213. city: city
  214. })
  215. },
  216. chooseHotCity(e) {
  217. const areaList = this.data.hotList[e.currentTarget.dataset.index].areaList;
  218. this.wxSortPickerViewItemTap(e, areaList)
  219. },
  220. chooseCity(e) {
  221. const areaList = this.data.cityList[e.currentTarget.dataset.index].cityInfo[e.currentTarget.dataset.idx].areaList;
  222. this.wxSortPickerViewItemTap(e, areaList)
  223. },
  224. chooseSearchCity(e) {
  225. const areaList = this.data.searchList[e.currentTarget.dataset.index].areaList;
  226. this.wxSortPickerViewItemTap(e, areaList)
  227. },
  228. wxSortPickerViewItemTap(e, areaList) {
  229. console.log(areaList)
  230. var city = e.target.dataset.text;
  231. var cityCode = e.target.dataset.code;
  232. if (city.indexOf("市") != -1) {
  233. city = city.substring(0, city.lastIndexOf('市'));
  234. }
  235. if (city.indexOf("地区") != -1) {
  236. city = city.substring(0, city.lastIndexOf('地区'));
  237. }
  238. if (city.indexOf("自治州") != -1) {
  239. city = city.substring(0, city.lastIndexOf('自治州'));
  240. }
  241. if (city.indexOf("特别行政区") != -1) {
  242. city = city.substring(0, city.lastIndexOf('特别行政区'));
  243. }
  244. wx.setStorageSync('city', city)
  245. wx.setStorageSync('cityCode', cityCode)
  246. wx.setStorageSync('areaList', [{
  247. countryName: '不限',
  248. countryCode: ''
  249. }].concat(areaList))
  250. // wx.setStorageSync('countryCode', areaList[0].countryCode)
  251. this.setData({
  252. city: city,
  253. cityCode: cityCode
  254. })
  255. var pages = getCurrentPages();
  256. var currPage = pages[pages.length - 1]; //当前页面
  257. var prevPage = pages[pages.length - 2]; //上一个页面
  258. if (prevPage) {
  259. prevPage.setData({
  260. hasLocation1: true
  261. })
  262. wx.navigateBack()
  263. }
  264. },
  265. cxgps: function(e) {
  266. var that = this;
  267. wx.showLoading({
  268. title: 'loading',
  269. mask: true
  270. });
  271. wx.getSetting({
  272. success: function(res) {
  273. if (!res.authSetting['scope.userLocation']) {
  274. wx.hideLoading();
  275. wx.showModal({
  276. title: '授权提示',
  277. content: '当前服务需要获取您的位置信息,请确认授权',
  278. confirmText: '去授权',
  279. cancelText: '暂不授权',
  280. success: function(res) {
  281. if (res.confirm) {
  282. wx.openSetting();
  283. } else if (res.cancel) {
  284. }
  285. }
  286. })
  287. } else {
  288. $.map.getRegeo({
  289. success(data) {
  290. wx.hideLoading();
  291. var data = data[0],
  292. cityNmae = data.regeocodeData.addressComponent.city || "无锡",
  293. cityCode = data.regeocodeData.addressComponent.adcode.substring(0, 4) + "00";
  294. if (cityNmae.indexOf("市") != -1) {
  295. cityNmae = cityNmae.substring(0, cityNmae.lastIndexOf('市'));
  296. }
  297. let areaList = city.getcityList([cityNmae])[0].areaList;
  298. that.setData({
  299. currcity: cityNmae,
  300. currcityCode: cityCode,
  301. cityName: cityNmae
  302. })
  303. wx.setStorageSync('areaList', [{
  304. countryName: '不限',
  305. countryCode: ''
  306. }].concat(areaList))
  307. // wx.setStorageSync('countryCode', areaList[0].countryCode)
  308. wx.setStorageSync('city', cityNmae);
  309. wx.setStorageSync('cityCode', cityCode);
  310. wx.setStorageSync('currcity', cityNmae)
  311. wx.setStorageSync('currcityCode', cityCode)
  312. }
  313. })
  314. }
  315. }
  316. });
  317. },
  318. bindKeyInput(e) {
  319. var value = e.detail.value.trim()
  320. if (value == "") {
  321. this.setData({
  322. inputValue: value,
  323. searchList: []
  324. })
  325. return '';
  326. }
  327. this.setData({
  328. inputValue: value
  329. })
  330. this.serach(value)
  331. },
  332. serach(text) {
  333. var searchList = this.data.cityNameList.filter(function(name) {
  334. // console.log(name)
  335. return name.indexOf(text) !== -1
  336. })
  337. var cityList = this.data.cityList;
  338. var resultList = [];
  339. for (var k = 0; k < searchList.length; k++) {
  340. for (var i = 0; i < cityList.length; i++) {
  341. for (var j = 0; j < cityList[i].cityInfo.length; j++) {
  342. var resultArr = {};
  343. if (searchList[k] == cityList[i].cityInfo[j].cityName) {
  344. console.log(cityList[i].cityInfo[j])
  345. // resultArr.cityCode = cityList[i].cityInfo[j].cityCode
  346. // resultArr.cityName = cityList[i].cityInfo[j].cityName
  347. resultArr = cityList[i].cityInfo[j]
  348. resultList.push(resultArr)
  349. }
  350. }
  351. }
  352. }
  353. console.log("searchList", resultList)
  354. this.setData({
  355. searchList: resultList
  356. })
  357. },
  358. onError(err) {
  359. app.aldstat.sendEvent('报错', {
  360. 'err': err
  361. });
  362. },
  363. })
  364. //经纬度定位获取站点
  365. function ajaxGes(lat, lng) {
  366. //TODO:自己的定位接口
  367. var url = '';
  368. return new Promise(function(resolve, reject) {
  369. wx.request({
  370. url: url,
  371. header: {
  372. 'content-type': 'application/json'
  373. },
  374. success: function(res) {
  375. resolve(res.data);
  376. },
  377. fail: function(err) {
  378. reject(err);
  379. }
  380. })
  381. })
  382. }