select.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const WXAPI = require('apifm-wxapi')
  2. const AUTH = require('../../utils/auth')
  3. const APP = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. },
  10. /**
  11. * 生命周期函数--监听页面加载
  12. */
  13. onLoad: function (options) {
  14. },
  15. /**
  16. * 生命周期函数--监听页面初次渲染完成
  17. */
  18. onReady: function () {
  19. },
  20. /**
  21. * 生命周期函数--监听页面显示
  22. */
  23. onShow: function () {
  24. wx.getLocation({
  25. type: 'wgs84', //wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
  26. success: (res) => {
  27. this.data.latitude = res.latitude
  28. this.data.longitude = res.longitude
  29. this.fetchShops(res.latitude, res.longitude, '')
  30. },
  31. fail(e){
  32. console.error(e)
  33. AUTH.checkAndAuthorize('scope.userLocation')
  34. }
  35. })
  36. },
  37. async fetchShops(latitude, longitude, kw){
  38. const res = await WXAPI.fetchShops({
  39. curlatitude: latitude,
  40. curlongitude: longitude,
  41. nameLike: kw
  42. })
  43. if (res.code == 0) {
  44. res.data.forEach(ele => {
  45. ele.distance = ele.distance.toFixed(3) // 距离保留3位小数
  46. })
  47. this.setData({
  48. shops: res.data
  49. })
  50. } else {
  51. this.setData({
  52. shops: null
  53. })
  54. }
  55. },
  56. /**
  57. * 生命周期函数--监听页面隐藏
  58. */
  59. onHide: function () {
  60. },
  61. /**
  62. * 生命周期函数--监听页面卸载
  63. */
  64. onUnload: function () {
  65. },
  66. /**
  67. * 页面相关事件处理函数--监听用户下拉动作
  68. */
  69. onPullDownRefresh: function () {
  70. },
  71. /**
  72. * 页面上拉触底事件的处理函数
  73. */
  74. onReachBottom: function () {
  75. },
  76. searchChange(event){
  77. this.setData({
  78. searchValue: event.detail.value
  79. })
  80. },
  81. search(event){
  82. console.log('search')
  83. this.setData({
  84. searchValue: event.detail.value
  85. })
  86. this.fetchShops(this.data.latitude, this.data.longitude, event.detail.value)
  87. },
  88. goShop(e){
  89. const idx = e.currentTarget.dataset.idx
  90. wx.setStorageSync('shopInfo', this.data.shops[idx])
  91. wx.setStorageSync('shopIds', this.data.shops[idx].id)
  92. wx.switchTab({
  93. url: '/pages/index/index'
  94. })
  95. }
  96. })