index.js 6.7 KB

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