index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. that.loadStoreListData()
  43. }
  44. })
  45. }else {
  46. // 2. 用户未授权的情况下, 打开授权界面, 引导用户授权.
  47. wx.authorize({
  48. scope: "scope.userLocation",
  49. success(res) {
  50. wx.getLocation({
  51. success(res){
  52. // 2.2 获取用户位置成功后,将会返回 latitude, longitude 两个字段,代表用户的经纬度位置
  53. // 2.3 将获取到的 经纬度传值给 getAddress 解析出 具体的地址
  54. that.setData({
  55. latitude: res.latitude,
  56. longitude: res.longitude,
  57. })
  58. that.loadStoreListData()
  59. },
  60. fail() {
  61. that.loadStoreListData()
  62. }
  63. })
  64. }
  65. })
  66. }
  67. }
  68. })
  69. },
  70. qqMapToBMap(lng, lat) {
  71. if (lng == null || lng == '' || lat == null || lat == '')
  72. return [lng, lat];
  73. var x_pi = 3.14159265358979324;
  74. var x = parseFloat(lng);
  75. var y = parseFloat(lat);
  76. var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
  77. var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
  78. var lng = (z * Math.cos(theta) + 0.0065).toFixed(5);
  79. var lat = (z * Math.sin(theta) + 0.006).toFixed(5);
  80. return [lng, lat];
  81. },
  82. bMapToQQMap(lng, lat) {
  83. if (lng == null || lng == '' || lat == null || lat == '')
  84. return [lng, lat];
  85. var x_pi = 3.14159265358979324;
  86. var x = parseFloat(lng) - 0.0065;
  87. var y = parseFloat(lat) - 0.006;
  88. var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  89. var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  90. var lng = (z * Math.cos(theta)).toFixed(7);
  91. var lat = (z * Math.sin(theta)).toFixed(7);
  92. return [lng, lat];
  93. },
  94. /**
  95. * 生命周期函数--监听页面初次渲染完成
  96. */
  97. onReady: function () {
  98. },
  99. /**
  100. * 生命周期函数--监听页面显示
  101. */
  102. onShow: function () {
  103. },
  104. /**
  105. * 生命周期函数--监听页面隐藏
  106. */
  107. onHide: function () {
  108. },
  109. onConfirm(e) {
  110. this.setData({
  111. page: 1,
  112. keyword: e.detail.value,
  113. noMore: false,
  114. noResult: false,
  115. storeList: []
  116. })
  117. this.loadStoreListData()
  118. },
  119. /**
  120. * 生命周期函数--监听页面卸载
  121. */
  122. onUnload: function () {
  123. },
  124. /**
  125. * 页面相关事件处理函数--监听用户下拉动作
  126. */
  127. onPullDownRefresh: function () {
  128. },
  129. onReachBottom: function () {
  130. if (this.data.lock || this.data.noMore) {
  131. return
  132. }
  133. this.data.lock = true
  134. this.data.page++
  135. this.loadStoreListData();
  136. },
  137. /**
  138. * 用户点击右上角分享
  139. */
  140. onShareAppMessage: function () {
  141. },
  142. loadStoreListData: function() {
  143. activity.getStoreListByCoupon({
  144. couponId: this.data.couponId,
  145. latitude: this.data.latitude || null,
  146. longitude: this.data.longitude || null,
  147. page: this.data.page,
  148. keyword: this.data.keyword,
  149. pageSize: 10,
  150. }).then(data => {
  151. if (data.code == 200) {
  152. this.dataMapToView(data.data)
  153. }
  154. this.data.lock = false
  155. }).catch(_ => {
  156. this.data.lock = false;
  157. console.log(_)
  158. })
  159. },
  160. dataMapToView(list) {
  161. if (!Array.isArray(list) || list.length == 0) {
  162. if (this.data.page == 1) {
  163. this.setData({
  164. noResult: true
  165. })
  166. } else {
  167. this.setData({
  168. noMore: true
  169. })
  170. }
  171. return
  172. }
  173. list.forEach(v => {
  174. v.distance = parseInt(v.distance * 100) / 100
  175. if (v.distance > 1) {
  176. v.distanceStr = "距离" + v.distance + "km"
  177. } else {
  178. v.distanceStr = "距离" + (v.distance * 100) + "m"
  179. }
  180. })
  181. this.data.storeList = this.data.storeList.concat(list)
  182. this.setData({
  183. storeList: this.data.storeList
  184. })
  185. },
  186. callPhone(e) {
  187. const phone = e.currentTarget.dataset.phone
  188. if (phone) {
  189. wx.makePhoneCall({
  190. phoneNumber: phone,
  191. })
  192. }
  193. }
  194. })