index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. var city = require('../../data/city');
  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. if (showLetter === '#' || showLetter === '热') {
  147. wx.pageScrollTo({
  148. scrollTop: 0,
  149. duration: 300,
  150. })
  151. }
  152. this.setData({
  153. showLetter,
  154. isShowLetter: true,
  155. })
  156. },
  157. searchEnd: function(e) {
  158. // console.log(e);
  159. // var showLetter=e.currentTarget.dataset.letter;
  160. var that = this;
  161. setTimeout(function() {
  162. that.setData({
  163. isShowLetter: false
  164. })
  165. }, 1000)
  166. },
  167. nowLetter: function(pageY, that) { //当前选中的信息
  168. var letterData = this.data.searchLetter;
  169. var bHeight = 0;
  170. var tHeight = 0;
  171. var showLetter = "";
  172. for (var i = 0; i < letterData.length; i++) {
  173. if (letterData[i].tHeight <= pageY && pageY <= letterData[i].bHeight) {
  174. bHeight = letterData[i].bHeight;
  175. tHeight = letterData[i].tHeight;
  176. showLetter = letterData[i].name;
  177. break;
  178. }
  179. }
  180. this.setScrollTop(that, showLetter);
  181. that.setData({
  182. bHeight: bHeight,
  183. tHeight: tHeight,
  184. showLetter: showLetter,
  185. startPageY: pageY
  186. })
  187. },
  188. bindScroll: function(e) {
  189. // console.log(e.detail)
  190. },
  191. setScrollTop: function(that, showLetter) {
  192. var scrollTop = 0;
  193. var cityList = that.data.cityList;
  194. var cityCount = 0;
  195. var initialCount = 0;
  196. for (var i = 0; i < cityList.length; i++) {
  197. if (showLetter == cityList[i].firstNameLetter) {
  198. scrollTop = initialCount * 30 + cityCount * 41;
  199. break;
  200. } else {
  201. initialCount++;
  202. cityCount += cityList[i].cityInfo.length;
  203. }
  204. }
  205. that.setData({
  206. scrollTop: scrollTop - 1558
  207. })
  208. },
  209. bindCity: function(e) {
  210. var city = e.currentTarget.dataset.city;
  211. this.setData({
  212. city: city
  213. })
  214. },
  215. chooseHotCity(e) {
  216. const areaList = this.data.hotList[e.currentTarget.dataset.index].areaList;
  217. this.wxSortPickerViewItemTap(e, areaList)
  218. },
  219. chooseCity(e) {
  220. const areaList = this.data.cityList[e.currentTarget.dataset.index].cityInfo[e.currentTarget.dataset.idx].areaList;
  221. this.wxSortPickerViewItemTap(e, areaList)
  222. },
  223. chooseSearchCity(e) {
  224. const areaList = this.data.searchList[e.currentTarget.dataset.index].areaList;
  225. this.wxSortPickerViewItemTap(e, areaList)
  226. },
  227. wxSortPickerViewItemTap(e, areaList) {
  228. var city = e.target.dataset.text;
  229. var cityCode = e.target.dataset.code;
  230. if (city.indexOf("市") != -1) {
  231. city = city.substring(0, city.lastIndexOf('市'));
  232. }
  233. if (city.indexOf("地区") != -1) {
  234. city = city.substring(0, city.lastIndexOf('地区'));
  235. }
  236. if (city.indexOf("自治州") != -1) {
  237. city = city.substring(0, city.lastIndexOf('自治州'));
  238. }
  239. if (city.indexOf("特别行政区") != -1) {
  240. city = city.substring(0, city.lastIndexOf('特别行政区'));
  241. }
  242. wx.setStorageSync('city', city)
  243. wx.setStorageSync('cityCode', cityCode)
  244. wx.setStorageSync('areaList', [{
  245. countryName: '不限',
  246. countryCode: ''
  247. }].concat(areaList))
  248. // wx.setStorageSync('countryCode', areaList[0].countryCode)
  249. this.setData({
  250. city: city,
  251. cityCode: cityCode
  252. })
  253. var pages = getCurrentPages();
  254. var currPage = pages[pages.length - 1]; //当前页面
  255. var prevPage = pages[pages.length - 2]; //上一个页面
  256. if (prevPage) {
  257. prevPage.setData({
  258. hasLocation1: true
  259. })
  260. wx.navigateBack()
  261. }
  262. },
  263. cxgps: function(e) {
  264. var that = this;
  265. wx.showLoading({
  266. title: 'loading',
  267. mask: true
  268. });
  269. wx.getSetting({
  270. success: function(res) {
  271. if (!res.authSetting['scope.userLocation']) {
  272. wx.hideLoading();
  273. wx.showModal({
  274. title: '授权提示',
  275. content: '当前服务需要获取您的位置信息,请确认授权',
  276. confirmText: '去授权',
  277. cancelText: '暂不授权',
  278. success: function(res) {
  279. if (res.confirm) {
  280. wx.openSetting();
  281. } else if (res.cancel) {
  282. }
  283. }
  284. })
  285. } else {
  286. $.map.getRegeo({
  287. success(data) {
  288. wx.hideLoading();
  289. var data = data[0],
  290. cityNmae = data.regeocodeData.addressComponent.city || "无锡",
  291. cityCode = data.regeocodeData.addressComponent.adcode.substring(0, 4) + "00";
  292. if (cityNmae.indexOf("市") != -1) {
  293. cityNmae = cityNmae.substring(0, cityNmae.lastIndexOf('市'));
  294. }
  295. let areaList = city.getcityList([cityNmae])[0].areaList;
  296. that.setData({
  297. currcity: cityNmae,
  298. currcityCode: cityCode,
  299. cityName: cityNmae
  300. })
  301. wx.setStorageSync('areaList', [{
  302. countryName: '不限',
  303. countryCode: ''
  304. }].concat(areaList))
  305. // wx.setStorageSync('countryCode', areaList[0].countryCode)
  306. wx.setStorageSync('city', cityNmae);
  307. wx.setStorageSync('cityCode', cityCode);
  308. wx.setStorageSync('currcity', cityNmae)
  309. wx.setStorageSync('currcityCode', cityCode)
  310. }
  311. })
  312. }
  313. }
  314. });
  315. },
  316. bindKeyInput(e) {
  317. var value = e.detail.value.trim()
  318. if (value == "") {
  319. this.setData({
  320. inputValue: value,
  321. searchList: []
  322. })
  323. return '';
  324. }
  325. this.setData({
  326. inputValue: value
  327. })
  328. this.serach(value)
  329. },
  330. serach(text) {
  331. var searchList = this.data.cityNameList.filter(function(name) {
  332. // console.log(name)
  333. return name.indexOf(text) !== -1
  334. })
  335. var cityList = this.data.cityList;
  336. var resultList = [];
  337. for (var k = 0; k < searchList.length; k++) {
  338. for (var i = 0; i < cityList.length; i++) {
  339. for (var j = 0; j < cityList[i].cityInfo.length; j++) {
  340. var resultArr = {};
  341. if (searchList[k] == cityList[i].cityInfo[j].cityName) {
  342. // resultArr.cityCode = cityList[i].cityInfo[j].cityCode
  343. // resultArr.cityName = cityList[i].cityInfo[j].cityName
  344. resultArr = cityList[i].cityInfo[j]
  345. resultList.push(resultArr)
  346. }
  347. }
  348. }
  349. }
  350. this.setData({
  351. searchList: resultList
  352. })
  353. },
  354. onError(err) {
  355. app.aldstat.sendEvent('报错', {
  356. 'err': err
  357. });
  358. },
  359. })
  360. //经纬度定位获取站点
  361. function ajaxGes(lat, lng) {
  362. //TODO:自己的定位接口
  363. var url = '';
  364. return new Promise(function(resolve, reject) {
  365. wx.request({
  366. url: url,
  367. header: {
  368. 'content-type': 'application/json'
  369. },
  370. success: function(res) {
  371. resolve(res.data);
  372. },
  373. fail: function(err) {
  374. reject(err);
  375. }
  376. })
  377. })
  378. }