index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import {
  2. default as storeApi
  3. } from "../../api/store"
  4. import {
  5. default as commonApi
  6. } from "../../api/commonApi"
  7. import {
  8. default as requestApi
  9. } from "../../utils/request"
  10. import {
  11. default as productApi
  12. } from "../../api/product"
  13. const app = getApp();
  14. Page({
  15. data: {
  16. tabs: [],
  17. activeTab: 0,
  18. productHeight: 0,
  19. productList: [{
  20. id: "1",
  21. title: "葛优躺(北京躺)电影足疗",
  22. price: "299",
  23. desc: "全店通用",
  24. open: true,
  25. imgurl: "../../images/product01.jpg"
  26. },
  27. {
  28. id: "2",
  29. title: "中式古法推拿",
  30. price: "299",
  31. desc: "暂未开放",
  32. open: false,
  33. imgurl: "../../images/product02.jpg"
  34. },
  35. {
  36. id: "3",
  37. title: "中式古法推拿",
  38. price: "299",
  39. desc: "暂未开放",
  40. open: false,
  41. imgurl: "../../images/product02.jpg"
  42. },
  43. {
  44. id: "4",
  45. title: "葛优躺(北京躺)电影足疗",
  46. price: "299",
  47. desc: "暂未开放",
  48. open: false,
  49. imgurl: "../../images/product01.jpg"
  50. }
  51. ],
  52. isLocation: false,
  53. },
  54. onLoad: async function () {
  55. const self = this
  56. const tokenData = await commonApi.getToken();
  57. if (tokenData.token) {
  58. wx.setStorageSync('token', tokenData.token)
  59. requestApi.HEAD.token = tokenData.token
  60. }
  61. wx.getSetting({
  62. success: (res) => {
  63. let authSetting = res.authSetting
  64. if (authSetting['scope.userLocation']) {
  65. console.log('已授权地理位置')
  66. // 已授权
  67. self.getLocation();
  68. } else {
  69. // 未授权
  70. console.log('未授权地理位置');
  71. }
  72. }
  73. })
  74. var tabs = await this.getProductList();
  75. this.setData({
  76. tabs
  77. });
  78. this.getProductHeight()
  79. },
  80. //动态生成产品的父盒子高度
  81. getProductHeight: function () {
  82. var h = wx.getSystemInfoSync().windowHeight * (750 / wx.getSystemInfoSync().windowWidth);
  83. var productHeight = h - 430;
  84. this.setData({
  85. productHeight
  86. })
  87. },
  88. getProductList: async function () {
  89. const self = this
  90. const productList = await productApi.getProductList(1, 4)
  91. self.setData({
  92. productList: productList,
  93. page: 1,
  94. pageSize: 10
  95. })
  96. },
  97. requestAll(url, data, header = {}, method) {
  98. const _self = this
  99. wx.showLoading()
  100. return new Promise((resolve, reject) => {
  101. wx.request({
  102. url: url,
  103. data: data,
  104. header: header,
  105. dataType: 'json',
  106. method: method,
  107. success: (res => {
  108. wx.hideLoading()
  109. if (res.data.code === 1) {
  110. //200: 服务端业务处理正常结束
  111. resolve(res.data)
  112. } else {
  113. if (res.data.code === 0) {
  114. wx.showToast({
  115. title: res.data.message,
  116. })
  117. }
  118. if (res.data.code === 901) {
  119. console.log(res.data)
  120. }
  121. //其它错误,提示用户错误信息
  122. if (this._errorHandler != null) {
  123. //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
  124. this._errorHandler(res)
  125. }
  126. reject(res)
  127. }
  128. }),
  129. fail: (res => {
  130. if (this._errorHandler != null) {
  131. this._errorHandler(res)
  132. }
  133. wx.showToast({
  134. title: '网络异常请,稍后再试~',
  135. })
  136. reject(res)
  137. })
  138. })
  139. })
  140. },
  141. onTabClick(e) {
  142. const index = e.detail.index
  143. this.setData({
  144. activeTab: index
  145. })
  146. },
  147. onChange(e) {
  148. const index = e.detail.index
  149. this.setData({
  150. activeTab: index
  151. })
  152. },
  153. handleClick(e) {
  154. },
  155. getLocation: async function () {
  156. console.log('getlocation')
  157. const self = this
  158. wx.getLocation({
  159. type: 'wgs84',
  160. async success(res) {
  161. console.log(res)
  162. const latitude = res.latitude
  163. const longitude = res.longitude
  164. const speed = res.speed
  165. const accuracy = res.accuracy
  166. const rs = await storeApi.getNearStore(latitude, longitude)
  167. console.log(rs.info);
  168. self.setData({
  169. locationFlag: true,
  170. storeInfo: rs.info
  171. })
  172. },fail(err){
  173. console.log(err)
  174. }
  175. })
  176. },
  177. showProduct:async function (e) {
  178. const self = this
  179. console.log(e.currentTarget.dataset.productid)
  180. var productId = e.currentTarget.dataset.productid;
  181. if (this.data.storeInfo) {
  182. var shopId = this.data.storeInfo.id;
  183. wx.navigateTo({
  184. url: '../product/product?shopId=' + shopId + '&productId=' + productId,
  185. })
  186. return
  187. }
  188. wx.getLocation({
  189. type: 'wgs84',
  190. async success(res) {
  191. console.log(res)
  192. const latitude = res.latitude
  193. const longitude = res.longitude
  194. const speed = res.speed
  195. const accuracy = res.accuracy
  196. const rs = await storeApi.getNearStore(latitude, longitude)
  197. console.log(rs.info);
  198. self.setData({
  199. locationFlag: true,
  200. storeInfo: rs.info
  201. })
  202. var shopId = self.data.storeInfo.id;
  203. wx.navigateTo({
  204. url: '../product/product?shopId=' + shopId + '&productId=' + productId,
  205. })
  206. }
  207. })
  208. },
  209. /**
  210. * 页面上拉触底事件的处理函数
  211. */
  212. onReachBottom: function () {
  213. console.log('2222')
  214. },
  215. /**
  216. * 页面相关事件处理函数--监听用户下拉动作
  217. */
  218. onPullDownRefresh: function () {
  219. console.log(111)
  220. },
  221. })