index.js 10 KB

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