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, productHeight: 0, productList: [], isLocation: false, pageNo: 1, pageSum: 0 }, onLoad: async function () { const self = this const tokenData = await commonApi.getToken(); if (tokenData.token && !requestApi.HEAD.token) { wx.setStorageSync('token', tokenData.token) requestApi.HEAD.token = tokenData.token } wx.getSetting({ success: (res) => { let authSetting = res.authSetting if (authSetting['scope.userLocation']) { console.log('已授权地理位置') // 已授权 self.getLocation(); } else { // 未授权 console.log('未授权地理位置'); 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 rs = await storeApi.getNearStore(latitude, longitude) console.log(rs.info); self.setData({ locationFlag: true, storeInfo: rs.info }) } }) } } }) // this.getProductHeight() }, onShow: async function () { const self = this const tokenData = await commonApi.getToken(); if (tokenData.token && !requestApi.HEAD.token) { wx.setStorageSync('token', tokenData.token) requestApi.HEAD.token = tokenData.token } var tabs = await this.getProductList(true); this.setData({ tabs }); }, getProductList: async function (refresh = false) { const self = this let pageNo = 1 if (refresh) { pageNo = 1 this.setData({ pageNo }) }else { pageNo = this.data.pageNo; } let productList = this.data.productList; const proList = await productApi.getProductList(pageNo, 4) let list = proList.list; if (pageNo == 1) { productList = list }else { list.forEach((item)=>{ productList.push(item) }) } self.setData({ productList, pageSum: proList.pageCount }) }, requestAll(url, data, header = {}, method) { const _self = this wx.showLoading() return new Promise((resolve, reject) => { wx.request({ url: url, data: data, header: header, dataType: 'json', method: method, success: (res => { wx.hideLoading() if (res.data.code === 1) { //200: 服务端业务处理正常结束 resolve(res.data) } else { if (res.data.code === 0) { wx.showToast({ title: res.data.message, }) } if (res.data.code === 901) { console.log(res.data) } //其它错误,提示用户错误信息 if (this._errorHandler != null) { //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理 this._errorHandler(res) } reject(res) } }), fail: (res => { if (this._errorHandler != null) { this._errorHandler(res) } wx.showToast({ title: '网络异常请,稍后再试~', }) reject(res) }) }) }) }, onTabClick(e) { const index = e.detail.index this.setData({ activeTab: index }) }, onChange(e) { const index = e.detail.index this.setData({ activeTab: index }) }, 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) console.log(rs.info); self.setData({ locationFlag: true, storeInfo: rs.info }) }, fail(err) { console.log(err) } }) }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { console.log(1213131) let pageNo = this.data.pageNo; let pageSum = this.data.pageSum; pageNo++; if (this.data.pageNo <= pageSum) { this.setData({ pageNo }) 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 }) var shopId = self.data.storeInfo.id; wx.navigateTo({ url: '../product/product?shopId=' + shopId + '&productId=' + productId, }) } }) }, })