index.js 8.8 KB

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