index.js 4.6 KB

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