宁波企业网站搭建特点,mip手机网站模板,成都网站建设 哪家比较好,制作网站找哪家好原因是#xff1a;需要登陆微信公众平台在开发管理 中设置 相应的 服务器域名 中的 request合法域名
// index.jsPage({data: {products:[],cardLayout: grid, // 默认卡片布局为网格模式isGrid: true, // 默认为网格布局page: 0, // 当前页码size: 10, // 每页大小hasMore… 原因是需要登陆微信公众平台在开发管理 中设置 相应的 服务器域名 中的 request合法域名
// index.jsPage({data: {products:[],cardLayout: grid, // 默认卡片布局为网格模式isGrid: true, // 默认为网格布局page: 0, // 当前页码size: 10, // 每页大小hasMore: true, // 是否还有更多数据loading:true,hasMore: true, // 是否还有更多数据showBottomImage: false, // 控制底部图片的显示状态searchValue: ,currentFilter: all, // 默认选中 all},// 处理网格视图按钮点击事件handleGridViewClick: function() {const currentLayout this.data.cardLayout;const newLayout currentLayout grid ? list : grid;this.setData({cardLayout: newLayout,isGrid: !this.data.isGrid});},onLoad: function () {this.fetchData();},// 发送请求获取数据async fetchData(page 0, size 10) {console.log(Fetching data, page:, page, size:, size); // 添加日志输出记录传入的参数try {const token wx.getStorageSync(token)console.log(获取商品数据前需要携带token token);if (!token) {wx.showToast({title: 获取 token 失败请重试,icon: none});return;}const response await new Promise((resolve, reject) {wx.request({url: https://api.crossbiog.com/product/admin/list, // 使用配置文件中的URLmethod: GET,data: { page, size }, // 分页参数header: { token: token,Cache-Control: max-age60 // 设置缓存时间为60秒},success: resolve,fail: reject});});if (response.statusCode 200) {const products response.data.data.content || [];const formattedProducts products.map(product ({...product,image: https://www.crossbiog.com/${product.image}}));const filteredProducts formattedProducts.filter(product product.status 1 product.editAuth 1);this.setData({products: [...this.data.products, ...filteredProducts],loading: false, // 如果有加载指示器设置为falsehasMore: filteredProducts.length size, // 是否还有更多数据page:page //更新页面数据中的page值});console.log(Updated page to:, page); // 添加日志输出记录更新后的 page 值if (filteredProducts.length size) {wx.showToast({title: 没有更多数据了,icon: none});}} else {wx.showToast({title: 数据加载失败,icon: none});}} catch (error) {wx.showToast({title: error.message || 请求失败,icon: none});}},//监听页面触底事件如用于加载更多数据。onReachBottom: function() {console.log(Current page before fetching more data:, this.data.page); // 添加日志输出记录当前 page 值if (this.data.hasMore) {const nextPage this.data.page 1;this.fetchData(this.data.page 1, this.data.size);console.log(Fetching data for page:, nextPage); // 添加日志输出方便调试} else {wx.showToast({title: 没有更多数据了,icon: none});}// 用户滑动到页面底部时触发this.setData({showBottomImage: true});},// 扫描二维码scanQrcode: function() {wx.scanCode({onlyFromCamera: false, // 允许从相机和相册中选择图片success: (res) {const jancode res.result;console.log(扫描结果:, jancode);this.getProductByJancode(jancode);},fail: (err) {wx.showToast({title: 扫描失败请重试,icon: none});}});},// 获取 tokengetToken: function() {return new Promise((resolve,reject){const token wx.getStorageSync(token)console.log(Token:, token);resolve(token)});},// 根据条码查询产品信息getProductByJancode: function(jancode) {this.getToken().then((token) {if (!token) {wx.showToast({title: 获取 token 失败请重试,icon: none});return;}wx.request({url: https://api.crossbiog.com/product/admin/detailByJancode, // 使用配置文件中的URLmethod: GET,data: {jancode: jancode},header: {token: ${token}},success: (res) {console.log(res res);console.log(后端返回的数据:, res.data); // 添加日志输出if (res.statusCode 200 res.data res.data.data) {const product res.data.data;if (product) {// 显示产品信息this.setData({products: [product],showNoResultsImage: false // 如果有结果隐藏无结果图片});} else {// 没有找到产品wx.showToast({title: 未找到该条码对应的产品,icon: none});this.setData({showNoResultsImage: true // 如果没有结果显示无结果图片});}} else {wx.showToast({title: 数据加载失败,icon: none});}},fail: (err) {wx.showToast({title: 请求失败,icon: none});}});}).catch((err) {wx.showToast({title: err.message,icon: none});});},// 点击商品卡片后跳转到详情页navigateToDetail(event) {const productId event.currentTarget.dataset.id;console.log(跳转到详情页产品ID:, productId);wx.navigateTo({url: /pages/productDetail/productDetail?id${productId},});},// 处理输入框的输入事件handleSearchInput: function (e) {this.setData({searchValue: e.detail.value // 更新输入框的值});},// 处理搜索事件按下回车时handleSearch: function () {const value this.data.searchValue; // 获取输入的值if (!value) {wx.showToast({title: 请输入搜索内容,icon: none});return;}// 获取 token 并跳转到结果页面this.getToken().then((token) {if(!token){wx.showToast({title: 获取 token 失败请重试,icon: none});return;}// 跳转到另一个页面并传递搜索内容和 tokenwx.navigateTo({url: /pages/searchResults/searchResults?value${value}token${token}}); }).catch((err){// 获取 token 失败时在这里处理错误wx.showToast({title: 获取 token 失败请重试,icon: none});})},handleFilterClick: function(e) {const filterType e.target.dataset.type;this.setData({currentFilter: filterType,page: 0, // 重置页码hasMore: true, // 重置是否有更多数据标志products: [] // 清空当前商品列表});this.fetchData();},
})