index.js 9.1 KB

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