index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. wx.getLocation({
  72. type: 'wgs84',
  73. async success (res) {
  74. console.log(res)
  75. const latitude = res.latitude
  76. const longitude = res.longitude
  77. const rs = await storeApi.getNearStore(latitude,longitude)
  78. console.log(rs.info);
  79. self.setData({
  80. locationFlag: true,
  81. storeInfo: rs.info
  82. })
  83. }
  84. })
  85. }
  86. }
  87. })
  88. var tabs = await this.getProductList();
  89. this.setData({
  90. tabs
  91. });
  92. this.getProductHeight()
  93. },
  94. //动态生成产品的父盒子高度
  95. getProductHeight: function () {
  96. var h = wx.getSystemInfoSync().windowHeight * (750 / wx.getSystemInfoSync().windowWidth);
  97. var productHeight = h - 430;
  98. this.setData({
  99. productHeight
  100. })
  101. },
  102. getProductList: async function () {
  103. const self = this
  104. const productList = await productApi.getProductList(1, 4)
  105. self.setData({
  106. productList: productList,
  107. page: 1,
  108. pageSize: 10
  109. })
  110. },
  111. requestAll(url, data, header = {}, method) {
  112. const _self = this
  113. wx.showLoading()
  114. return new Promise((resolve, reject) => {
  115. wx.request({
  116. url: url,
  117. data: data,
  118. header: header,
  119. dataType: 'json',
  120. method: method,
  121. success: (res => {
  122. wx.hideLoading()
  123. if (res.data.code === 1) {
  124. //200: 服务端业务处理正常结束
  125. resolve(res.data)
  126. } else {
  127. if (res.data.code === 0) {
  128. wx.showToast({
  129. title: res.data.message,
  130. })
  131. }
  132. if (res.data.code === 901) {
  133. console.log(res.data)
  134. }
  135. //其它错误,提示用户错误信息
  136. if (this._errorHandler != null) {
  137. //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
  138. this._errorHandler(res)
  139. }
  140. reject(res)
  141. }
  142. }),
  143. fail: (res => {
  144. if (this._errorHandler != null) {
  145. this._errorHandler(res)
  146. }
  147. wx.showToast({
  148. title: '网络异常请,稍后再试~',
  149. })
  150. reject(res)
  151. })
  152. })
  153. })
  154. },
  155. onTabClick(e) {
  156. const index = e.detail.index
  157. this.setData({
  158. activeTab: index
  159. })
  160. },
  161. onChange(e) {
  162. const index = e.detail.index
  163. this.setData({
  164. activeTab: index
  165. })
  166. },
  167. handleClick(e) {
  168. },
  169. getLocation: async function () {
  170. console.log('getlocation')
  171. const self = this
  172. wx.getLocation({
  173. type: 'wgs84',
  174. async success(res) {
  175. console.log(res)
  176. const latitude = res.latitude
  177. const longitude = res.longitude
  178. const speed = res.speed
  179. const accuracy = res.accuracy
  180. const rs = await storeApi.getNearStore(latitude,longitude)
  181. console.log(rs.info);
  182. self.setData({
  183. locationFlag: true,
  184. storeInfo: rs.info
  185. })
  186. },fail(err){
  187. console.log(err)
  188. }
  189. })
  190. },
  191. /**
  192. * 页面上拉触底事件的处理函数
  193. */
  194. onReachBottom: function () {
  195. console.log('2222')
  196. },
  197. /**
  198. * 页面相关事件处理函数--监听用户下拉动作
  199. */
  200. onPullDownRefresh: function () {
  201. console.log(111)
  202. },
  203. showProduct:async function (e) {
  204. const self = this
  205. console.log(e.currentTarget.dataset.productid)
  206. var productId = e.currentTarget.dataset.productid;
  207. if (this.data.storeInfo) {
  208. var shopId = this.data.storeInfo.id;
  209. wx.navigateTo({
  210. url: '../product/product?shopId=' + shopId + '&productId=' + productId,
  211. })
  212. return
  213. }
  214. wx.getLocation({
  215. type: 'wgs84',
  216. async success(res) {
  217. console.log(res)
  218. const latitude = res.latitude
  219. const longitude = res.longitude
  220. const speed = res.speed
  221. const accuracy = res.accuracy
  222. const rs = await storeApi.getNearStore(latitude, longitude)
  223. console.log(rs.info);
  224. self.setData({
  225. locationFlag: true,
  226. storeInfo: rs.info
  227. })
  228. var shopId = self.data.storeInfo.id;
  229. wx.navigateTo({
  230. url: '../product/product?shopId=' + shopId + '&productId=' + productId,
  231. })
  232. }
  233. })
  234. },
  235. })