index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // pages/welfareMall/couponFitStore/index.js
  2. import activity from '../../../api/activity'
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. couponId: '',
  9. keyword: '',
  10. page: 1,
  11. pageSize: 10,
  12. storeList: []
  13. },
  14. /**
  15. * 生命周期函数--监听页面加载
  16. */
  17. onLoad: function (options) {
  18. if (!options.couponId) {
  19. wx.reLaunch({
  20. url: '/pages/welfareMall/index/index',
  21. })
  22. }
  23. this.data.couponId = options.couponId
  24. const that = this
  25. wx.getSetting({
  26. success(res) {
  27. // 1. scope.userLocation 为真, 代表用户已经授权
  28. if (res.authSetting['scope.userLocation']) {
  29. // 1.1 使用 getlocation 获取用户 经纬度位置
  30. wx.getLocation({
  31. success(res){
  32. // 1.2 获取用户位置成功后,将会返回 latitude, longitude 两个字段,代表用户的经纬度位置
  33. console.log(res)
  34. const l = that.qqMapToBMap(res.longitude, res.latitude)
  35. that.setData({
  36. latitude: l[1],
  37. longitude: l[0],
  38. })
  39. that.loadStoreListData()
  40. },
  41. fail() {
  42. wx.navigateBack({
  43. delta: -1,
  44. })
  45. }
  46. })
  47. }else {
  48. // 2. 用户未授权的情况下, 打开授权界面, 引导用户授权.
  49. wx.openSetting({
  50. success(res) {
  51. // 2.1 如果二次授权允许了 userLocation 权限, 就再次执行获取位置的接口
  52. if (res.authSetting["scope.userLocation"]) {
  53. wx.getLocation({
  54. success(res){
  55. // 2.2 获取用户位置成功后,将会返回 latitude, longitude 两个字段,代表用户的经纬度位置
  56. // 2.3 将获取到的 经纬度传值给 getAddress 解析出 具体的地址
  57. that.setData({
  58. latitude: res.latitude,
  59. longitude: res.longitude,
  60. })
  61. that.loadStoreListData()
  62. },
  63. fail() {
  64. wx.navigateBack({
  65. delta: -1,
  66. })
  67. }
  68. })
  69. }
  70. }
  71. })
  72. }
  73. }
  74. })
  75. },
  76. qqMapToBMap(lng, lat) {
  77. if (lng == null || lng == '' || lat == null || lat == '')
  78. return [lng, lat];
  79. var x_pi = 3.14159265358979324;
  80. var x = parseFloat(lng);
  81. var y = parseFloat(lat);
  82. var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
  83. var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
  84. var lng = (z * Math.cos(theta) + 0.0065).toFixed(5);
  85. var lat = (z * Math.sin(theta) + 0.006).toFixed(5);
  86. return [lng, lat];
  87. },
  88. bMapToQQMap(lng, lat) {
  89. if (lng == null || lng == '' || lat == null || lat == '')
  90. return [lng, lat];
  91. var x_pi = 3.14159265358979324;
  92. var x = parseFloat(lng) - 0.0065;
  93. var y = parseFloat(lat) - 0.006;
  94. var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  95. var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  96. var lng = (z * Math.cos(theta)).toFixed(7);
  97. var lat = (z * Math.sin(theta)).toFixed(7);
  98. return [lng, lat];
  99. },
  100. /**
  101. * 生命周期函数--监听页面初次渲染完成
  102. */
  103. onReady: function () {
  104. },
  105. /**
  106. * 生命周期函数--监听页面显示
  107. */
  108. onShow: function () {
  109. },
  110. /**
  111. * 生命周期函数--监听页面隐藏
  112. */
  113. onHide: function () {
  114. },
  115. onConfirm(e) {
  116. this.setData({
  117. page: 1,
  118. keyword: e.detail.value,
  119. noMore: false,
  120. noResult: false,
  121. storeList: []
  122. })
  123. this.loadStoreListData()
  124. },
  125. /**
  126. * 生命周期函数--监听页面卸载
  127. */
  128. onUnload: function () {
  129. },
  130. /**
  131. * 页面相关事件处理函数--监听用户下拉动作
  132. */
  133. onPullDownRefresh: function () {
  134. },
  135. onReachBottom: function () {
  136. if (this.data.lock || this.data.noMore) {
  137. return
  138. }
  139. this.data.lock = true
  140. this.data.page++
  141. this.loadStoreListData();
  142. },
  143. /**
  144. * 用户点击右上角分享
  145. */
  146. onShareAppMessage: function () {
  147. },
  148. loadStoreListData: function() {
  149. activity.getStoreListByCoupon({
  150. couponId: this.data.couponId,
  151. latitude: this.data.latitude,
  152. longitude: this.data.longitude,
  153. page: this.data.page,
  154. keyword: this.data.keyword,
  155. pageSize: 10,
  156. }).then(data => {
  157. if (data.code == 200) {
  158. this.dataMapToView(data.data)
  159. }
  160. this.data.lock = false
  161. }).catch(_ => {
  162. this.data.lock = false;
  163. console.log(_)
  164. })
  165. },
  166. dataMapToView(list) {
  167. if (!Array.isArray(list) || list.length == 0) {
  168. if (this.data.page == 1) {
  169. this.setData({
  170. noResult: true
  171. })
  172. } else {
  173. this.setData({
  174. noMore: true
  175. })
  176. }
  177. return
  178. }
  179. list.forEach(v => {
  180. v.distance = parseInt(v.distance * 100) / 100
  181. if (v.distance > 1) {
  182. v.distanceStr = "距离" + v.distance + "km"
  183. } else {
  184. v.distanceStr = "距离" + (v.distance * 100) + "m"
  185. }
  186. })
  187. this.data.storeList = this.data.storeList.concat(list)
  188. this.setData({
  189. storeList: list
  190. })
  191. },
  192. callPhone(e) {
  193. const phone = e.currentTarget.dataset.phone
  194. if (phone) {
  195. wx.makePhoneCall({
  196. phoneNumber: phone,
  197. })
  198. }
  199. }
  200. })