index.js 11 KB

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