index.js 10.0 KB

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