index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. title: '足浴'
  18. },
  19. {
  20. title: '美甲'
  21. },
  22. {
  23. title: '附加服务'
  24. }
  25. ],
  26. activeTab: 0,
  27. tabHeight: 500,
  28. productList: [],
  29. isLocation: false,
  30. pageNo: [1, 1, 1],
  31. pageSum: [],
  32. activeIndex: 0,
  33. productHeight: 0
  34. },
  35. onLoad: async function (options) {
  36. console.log(options)
  37. const self = this
  38. const tokenData = await commonApi.getToken();
  39. if (tokenData.token && !requestApi.HEAD.token) {
  40. console.log('设置token')
  41. wx.setStorageSync('token', tokenData.token)
  42. requestApi.HEAD.token = tokenData.token
  43. }
  44. if (!options.storeId) {
  45. wx.getSetting({
  46. success: (res) => {
  47. let authSetting = res.authSetting
  48. if (authSetting['scope.userLocation']) {
  49. console.log('已授权地理位置')
  50. // 已授权
  51. self.getLocation();
  52. } else {
  53. // 未授权
  54. console.log('未授权地理位置');
  55. wx.getLocation({
  56. type: 'wgs84',
  57. async success(res) {
  58. console.log(res)
  59. const latitude = res.latitude
  60. const longitude = res.longitude
  61. wx.setStorageSync('lon', longitude)
  62. wx.setStorageSync('lat', latitude)
  63. const rs = await storeApi.getNearStore(latitude, longitude)
  64. console.log(rs.info);
  65. self.setData({
  66. locationFlag: true,
  67. storeInfo: rs.info
  68. })
  69. }
  70. })
  71. }
  72. }
  73. })
  74. } else {
  75. const rs = await storeApi.getStoreByI(options.storeId)
  76. self.setData({
  77. storeInfo: rs.info
  78. })
  79. }
  80. //根据机型设置现有的tab-content盒子高度
  81. let windowHeight = parseInt(wx.getSystemInfoSync().windowHeight) * parseInt(750 / wx.getSystemInfoSync().windowWidth) - 185
  82. await this.getProductList();
  83. let tabs = this.data.tabs;
  84. let activeTab = this.data.activeTab;
  85. console.log(tabs[activeTab].list.length)
  86. let productHeight = tabs[activeTab].list.length * 170
  87. this.setData({
  88. tabHeight: windowHeight,
  89. productHeight
  90. })
  91. },
  92. onShow: async function (op) {
  93. console.log(op)
  94. const self = this
  95. const tokenData = await commonApi.getToken();
  96. if (tokenData.token && !requestApi.HEAD.token) {
  97. wx.setStorageSync('token', tokenData.token)
  98. requestApi.HEAD.token = tokenData.token
  99. }
  100. if (self.data.chooseStoreId) {
  101. const rs = await storeApi.getStoreById(self.data.chooseStoreId)
  102. self.setData({
  103. storeInfo: rs.info,
  104. chooseStoreId: 0
  105. })
  106. }
  107. await this.getProductList();
  108. },
  109. getProductList: async function (refresh = false) {
  110. const self = this
  111. let activeTab = self.data.activeTab;
  112. let tabs = self.data.tabs;
  113. let pageSum = self.data.pageSum;
  114. let pageNo = self.data.pageNo;
  115. let prolist = await productApi.getProductList(activeTab + 1, pageNo[activeTab],10);
  116. pageSum[activeTab] = prolist.pageCount;
  117. let list = prolist.list;
  118. if (!tabs[activeTab].list) {
  119. tabs[activeTab].list = [];
  120. }
  121. if (pageNo[activeTab] == 1) {
  122. tabs[activeTab].list = list;
  123. } else {
  124. list.forEach((item) => {
  125. let oldList = tabs[activeTab].list;
  126. oldList.push(item);
  127. tabs[activeTab].list = oldList;
  128. })
  129. }
  130. self.setData({
  131. tabs,
  132. pageSum
  133. })
  134. },
  135. requestAll(url, data, header = {}, method) {
  136. const _self = this
  137. wx.showLoading()
  138. return new Promise((resolve, reject) => {
  139. wx.request({
  140. url: url,
  141. data: data,
  142. header: header,
  143. dataType: 'json',
  144. method: method,
  145. success: (res => {
  146. wx.hideLoading()
  147. if (res.data.code === 1) {
  148. //200: 服务端业务处理正常结束
  149. resolve(res.data)
  150. } else {
  151. if (res.data.code === 0) {
  152. wx.showToast({
  153. title: res.data.message,
  154. })
  155. }
  156. if (res.data.code === 901) {
  157. console.log(res.data)
  158. }
  159. //其它错误,提示用户错误信息
  160. if (this._errorHandler != null) {
  161. //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
  162. this._errorHandler(res)
  163. }
  164. reject(res)
  165. }
  166. }),
  167. fail: (res => {
  168. if (this._errorHandler != null) {
  169. this._errorHandler(res)
  170. }
  171. wx.showToast({
  172. title: '网络异常请,稍后再试~',
  173. })
  174. reject(res)
  175. })
  176. })
  177. })
  178. },
  179. onTabClick(e) {
  180. const index = e.detail.index
  181. this.setData({
  182. activeTab: index
  183. })
  184. },
  185. onChange(e) {
  186. const index = e.detail.index
  187. let pageNo = this.data.pageNo;
  188. this.setData({
  189. activeTab: index,
  190. // pageNo: [1, 1, 1, 1]
  191. }),
  192. this.getProductList()
  193. },
  194. handleClick(e) {
  195. },
  196. getLocation: async function () {
  197. console.log('getlocation')
  198. const self = this
  199. wx.getLocation({
  200. type: 'wgs84',
  201. async success(res) {
  202. console.log(res)
  203. const latitude = res.latitude
  204. const longitude = res.longitude
  205. wx.setStorageSync('lon', longitude)
  206. wx.setStorageSync('lat', latitude)
  207. const speed = res.speed
  208. const accuracy = res.accuracy
  209. const rs = await storeApi.getNearStore(latitude, longitude)
  210. console.log(rs.info);
  211. self.setData({
  212. locationFlag: true,
  213. storeInfo: rs.info
  214. })
  215. },
  216. fail(err) {
  217. console.log(err)
  218. }
  219. })
  220. },
  221. /**
  222. * 页面上拉触底事件的处理函数
  223. */
  224. onReachBottom: function () {
  225. let activeTab = this.data.activeTab;
  226. let pageNo = this.data.pageNo;
  227. let pageSum = this.data.pageSum;
  228. pageNo[activeTab]++;
  229. console.log('触底了')
  230. this.setData({
  231. pageNo
  232. })
  233. if (pageNo[activeTab] <= pageSum[activeTab]) {
  234. console.log(pageNo)
  235. this.getProductList()
  236. }
  237. },
  238. /**
  239. * 页面相关事件处理函数--监听用户下拉动作
  240. */
  241. onPullDownRefresh: function () {
  242. console.log(111)
  243. },
  244. showProduct: async function (e) {
  245. const self = this
  246. console.log(e.currentTarget.dataset.productid)
  247. var productId = e.currentTarget.dataset.productid;
  248. if (this.data.storeInfo) {
  249. var shopId = this.data.storeInfo.id;
  250. wx.navigateTo({
  251. url: '../product/product?shopId=' + shopId + '&productId=' + productId,
  252. })
  253. return
  254. }
  255. wx.getLocation({
  256. type: 'wgs84',
  257. async success(res) {
  258. console.log(res)
  259. const latitude = res.latitude
  260. const longitude = res.longitude
  261. const speed = res.speed
  262. const accuracy = res.accuracy
  263. const rs = await storeApi.getNearStore(latitude, longitude)
  264. console.log(rs.info);
  265. self.setData({
  266. locationFlag: true,
  267. storeInfo: rs.info
  268. })
  269. var shopId = self.data.storeInfo.id;
  270. wx.navigateTo({
  271. url: '../product/product?shopId=' + shopId + '&productId=' + productId,
  272. })
  273. }
  274. })
  275. },
  276. gotoAppointment: async function (e) {
  277. const self = this
  278. console.log(e.currentTarget.dataset.productid)
  279. let productId = e.currentTarget.dataset.productid;
  280. let shopId = this.data.storeInfo.id;
  281. let address = self.data.storeInfo.address;
  282. let shopName = self.data.storeInfo.storeName;
  283. const isAuth = await app.isAuth()
  284. if (!isAuth) {
  285. // wx.redirectTo({
  286. // url: '/pages/prompt/prompt?page=' + this.route + `&shopId=${shopId}&productId=${productId}&address=${address}&shopName=${shopName}`,
  287. // })
  288. // return
  289. }
  290. wx.navigateTo({
  291. url: '../appointment/appointment?' + 'shopId=' + shopId + '&productId=' + productId + '&address=' + address + "&shopName=" + shopName,
  292. })
  293. }
  294. })