index.js 5.8 KB

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