index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. isLocation: false,
  21. pageNo: 1,
  22. pageSum: 0
  23. },
  24. onLoad: async function () {
  25. const self = this
  26. const tokenData = await commonApi.getToken();
  27. if (tokenData.token) {
  28. wx.setStorageSync('token', tokenData.token)
  29. requestApi.HEAD.token = tokenData.token
  30. }
  31. wx.getSetting({
  32. success: (res) => {
  33. let authSetting = res.authSetting
  34. if (authSetting['scope.userLocation']) {
  35. console.log('已授权地理位置')
  36. // 已授权
  37. self.getLocation();
  38. } else {
  39. // 未授权
  40. console.log('未授权地理位置');
  41. wx.getLocation({
  42. type: 'wgs84',
  43. async success(res) {
  44. console.log(res)
  45. const latitude = res.latitude
  46. const longitude = res.longitude
  47. const rs = await storeApi.getNearStore(latitude, longitude)
  48. console.log(rs.info);
  49. self.setData({
  50. locationFlag: true,
  51. storeInfo: rs.info
  52. })
  53. }
  54. })
  55. }
  56. }
  57. })
  58. // this.getProductHeight()
  59. },
  60. onShow: async function () {
  61. var tabs = await this.getProductList();
  62. this.setData({
  63. tabs
  64. });
  65. },
  66. getProductList: async function () {
  67. const self = this
  68. let pageNo = this.data.pageNo;
  69. let productList = this.data.productList;
  70. const proList = await productApi.getProductList(pageNo, 4)
  71. let list = proList.list;
  72. list.forEach((item)=>{
  73. productList.push(item)
  74. })
  75. self.setData({
  76. productList,
  77. pageSum: proList.pageCount
  78. })
  79. },
  80. requestAll(url, data, header = {}, method) {
  81. const _self = this
  82. wx.showLoading()
  83. return new Promise((resolve, reject) => {
  84. wx.request({
  85. url: url,
  86. data: data,
  87. header: header,
  88. dataType: 'json',
  89. method: method,
  90. success: (res => {
  91. wx.hideLoading()
  92. if (res.data.code === 1) {
  93. //200: 服务端业务处理正常结束
  94. resolve(res.data)
  95. } else {
  96. if (res.data.code === 0) {
  97. wx.showToast({
  98. title: res.data.message,
  99. })
  100. }
  101. if (res.data.code === 901) {
  102. console.log(res.data)
  103. }
  104. //其它错误,提示用户错误信息
  105. if (this._errorHandler != null) {
  106. //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
  107. this._errorHandler(res)
  108. }
  109. reject(res)
  110. }
  111. }),
  112. fail: (res => {
  113. if (this._errorHandler != null) {
  114. this._errorHandler(res)
  115. }
  116. wx.showToast({
  117. title: '网络异常请,稍后再试~',
  118. })
  119. reject(res)
  120. })
  121. })
  122. })
  123. },
  124. onTabClick(e) {
  125. const index = e.detail.index
  126. this.setData({
  127. activeTab: index
  128. })
  129. },
  130. onChange(e) {
  131. const index = e.detail.index
  132. this.setData({
  133. activeTab: index
  134. })
  135. },
  136. handleClick(e) {
  137. },
  138. getLocation: async function () {
  139. console.log('getlocation')
  140. const self = this
  141. wx.getLocation({
  142. type: 'wgs84',
  143. async success(res) {
  144. console.log(res)
  145. const latitude = res.latitude
  146. const longitude = res.longitude
  147. const speed = res.speed
  148. const accuracy = res.accuracy
  149. const rs = await storeApi.getNearStore(latitude, longitude)
  150. console.log(rs.info);
  151. self.setData({
  152. locationFlag: true,
  153. storeInfo: rs.info
  154. })
  155. },
  156. fail(err) {
  157. console.log(err)
  158. }
  159. })
  160. },
  161. /**
  162. * 页面上拉触底事件的处理函数
  163. */
  164. onReachBottom: function () {
  165. console.log(1213131)
  166. let pageNo = this.data.pageNo;
  167. let pageSum = this.data.pageSum;
  168. pageNo++;
  169. this.setData({
  170. pageNo
  171. })
  172. if (this.data.pageNo <= pageSum) {
  173. this.getProductList()
  174. }
  175. },
  176. /**
  177. * 页面相关事件处理函数--监听用户下拉动作
  178. */
  179. onPullDownRefresh: function () {
  180. console.log(111)
  181. },
  182. showProduct: async function (e) {
  183. const self = this
  184. console.log(e.currentTarget.dataset.productid)
  185. var productId = e.currentTarget.dataset.productid;
  186. if (this.data.storeInfo) {
  187. var shopId = this.data.storeInfo.id;
  188. wx.navigateTo({
  189. url: '../product/product?shopId=' + shopId + '&productId=' + productId,
  190. })
  191. return
  192. }
  193. wx.getLocation({
  194. type: 'wgs84',
  195. async success(res) {
  196. console.log(res)
  197. const latitude = res.latitude
  198. const longitude = res.longitude
  199. const speed = res.speed
  200. const accuracy = res.accuracy
  201. const rs = await storeApi.getNearStore(latitude, longitude)
  202. console.log(rs.info);
  203. self.setData({
  204. locationFlag: true,
  205. storeInfo: rs.info
  206. })
  207. var shopId = self.data.storeInfo.id;
  208. wx.navigateTo({
  209. url: '../product/product?shopId=' + shopId + '&productId=' + productId,
  210. })
  211. }
  212. })
  213. },
  214. })