import { default as storeApi } from "../../api/store" import { default as commonApi } from "../../api/commonApi" import { default as requestApi } from "../../utils/request" import { default as productApi } from "../../api/product" const app = getApp(); Page({ data: { tabs: [], activeTab: 0, tabHeight: 500, productList: [], isLocation: false, pageNo: [1, 1, 1], pageSum: [], activeIndex: 0, productHeight: 0, isShowFood:true, }, onLoad: async function (options) { console.log(options) const self = this const tokenData = await commonApi.getToken(); if (tokenData.token && !requestApi.HEAD.token) { wx.setStorageSync('token', tokenData.token) requestApi.HEAD.token = tokenData.token } if (!options.storeId) { wx.getSetting({ success: (res) => { let authSetting = res.authSetting if (authSetting['scope.userLocation']) { console.log('已授权地理位置') // 已授权 self.getLocation(); } else { // 未授权 console.log('未授权地理位置'); } } }) } else { const rs = await storeApi.getStoreById(options.storeId) self.setData({ storeInfo: rs.info }) self.setData({ pageNo: [1, 1, 1,1,1,1,1], }) await this.productTypeList() await this.getProductList() } //根据机型设置现有的tab-content盒子高度 let windowHeight = parseInt(wx.getSystemInfoSync().windowHeight) * parseInt(750 / wx.getSystemInfoSync().windowWidth) - 185 self.setData({ tabHeight: windowHeight }) }, onShow: async function (op) { const self = this if (self.data.chooseStoreId) { console.log('选择了其他门店') const rs = await storeApi.getStoreById(self.data.chooseStoreId) self.setData({ storeInfo: rs.info }) self.setData({ pageNo: [1, 1, 1], }) await self.productTypeList() await self.getProductList() } if (self.data.isShow == 1) { return } const tokenData = await commonApi.getToken(); if (tokenData.token && !requestApi.HEAD.token) { wx.setStorageSync('token', tokenData.token) requestApi.HEAD.token = tokenData.token } self.setData({ isShow: 1 }) }, productTypeList: async function(){ const self = this let storeInfo = self.data.storeInfo; console.log(storeInfo) let storeId = 0 if (!storeInfo) { storeId = 0 }else { storeId = storeInfo.id } const rs = await productApi.getProductTypeList(1,10,1,storeId) if (rs.count == 0) { wx.showModal({ title: "提示", content: "该门店暂无商品哦" }).then(() => { wx.navigateTo({ url: '/pages/shop/shop', }) }) } self.setData({ tabs: rs.list }) }, getProductList: async function (refresh = false) { const self = this let activeTab = self.data.activeTab; let tabs = self.data.tabs; let pageSum = self.data.pageSum; let pageNo = self.data.pageNo; console.log(self.data.pageNo) console.log(activeTab) let storeInfo = self.data.storeInfo; console.log(storeInfo) let storeId = 1 if (!storeInfo) { storeId = 1 }else { storeId = storeInfo.id } console.log(storeInfo['id']) let prolist = await productApi.getProductList(tabs[activeTab].id, pageNo[activeTab],10,1,storeInfo['id']); pageSum[activeTab] = prolist.pageCount; let list = prolist.list; if (!tabs[activeTab].list) { tabs[activeTab].list = []; } if (pageNo[activeTab] == 1) { tabs[activeTab].list = list; } else { list.forEach((item) => { let oldList = tabs[activeTab].list; oldList.push(item); tabs[activeTab].list = oldList; }) } let defaultH = '100%' if (tabs[activeTab].list.length) { defaultH = tabs[activeTab].list.length * 170 } let productHeight = defaultH self.setData({ tabs, pageSum, productHeight }) }, onTabClick(e) { const index = e.detail.index let tabs = this.data.tabs let defaultH = 600 if (tabs[index].list && tabs[index].list.length) { defaultH = tabs[index].list.length * 170 } let productHeight = defaultH this.setData({ activeTab: index, productHeight }) }, async onChange(e) { const index = e.detail.index let pageNo = this.data.pageNo; this.setData({ activeTab: index, // pageNo: [1, 1, 1, 1] }), await this.productTypeList() await this.getProductList() }, handleClick(e) { }, getLocation: async function () { console.log('getlocation') const self = this wx.getLocation({ type: 'wgs84', async success(res) { console.log(res) const latitude = res.latitude const longitude = res.longitude wx.setStorageSync('lon', longitude) wx.setStorageSync('lat', latitude) const speed = res.speed const accuracy = res.accuracy const rs = await storeApi.getNearStore(latitude, longitude) self.setData({ locationFlag: true, storeInfo: rs.info }) await self.productTypeList() await self.getProductList() self.setData({ pageNo: [1, 1, 1], }) }, fail(err) { console.log(err) wx.showModal({ title: "提示", content:"请授权获取位置功能", }) } }) }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { let activeTab = this.data.activeTab; let pageNo = this.data.pageNo; let pageSum = this.data.pageSum; pageNo[activeTab]++; this.setData({ pageNo }) if (pageNo[activeTab] <= pageSum[activeTab]) { this.getProductList() } }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { console.log(111) }, showProduct: async function (e) { const self = this console.log(e.currentTarget.dataset.productid) var productId = e.currentTarget.dataset.productid; if (this.data.storeInfo) { var shopId = this.data.storeInfo.id; wx.navigateTo({ url: '../product/product?shopId=' + shopId + '&productId=' + productId, }) return } wx.getLocation({ type: 'wgs84', async success(res) { console.log(res) const latitude = res.latitude const longitude = res.longitude const speed = res.speed const accuracy = res.accuracy const rs = await storeApi.getNearStore(latitude, longitude) console.log(rs.info); self.setData({ locationFlag: true, storeInfo: rs.info }) if (!rs.info.distance) { wx.showModal({ title: '提示', content: '该门店打样了哦', }) } var shopId = self.data.storeInfo.id; wx.navigateTo({ url: '../product/product?shopId=' + shopId + '&productId=' + productId, }) } }) }, gotoAppointment: async function (e) { const self = this console.log(e.currentTarget.dataset.productid) let productId = e.currentTarget.dataset.productid; let shopId = this.data.storeInfo.id; let address = self.data.storeInfo.address; let shopName = self.data.storeInfo.storeName; const isAuth = await app.isAuth() if (!isAuth) { // wx.redirectTo({ // url: '/pages/prompt/prompt?page=' + this.route + `&shopId=${shopId}&productId=${productId}&address=${address}&shopName=${shopName}`, // }) // return } wx.navigateTo({ url: '../appointment/appointment?' + 'shopId=' + shopId + '&productId=' + productId + '&address=' + address + "&shopName=" + shopName, }) }, goSmdd: async function () { wx.scanCode({ // onlyFromCamera: true, success: (res) => { console.log(res); var temp1 = res.result.split('?'); var pram = temp1[1]; var keyValue = pram.split('&'); var obj = {}; for (var i = 0; i