Просмотр исходного кода

小程序食堂当图片不存在时显示默认图片

wrh 2 лет назад
Родитель
Сommit
6561a37d6c

+ 7 - 0
nngkxxdp/src/main/resources/static/nnzwminiapp/pages/commentList/commentList.js

@@ -39,6 +39,13 @@ Page({
 			})
 		})
 	},
+	imageError(e) {
+		let temp = this.data.commentList;
+		temp[e.currentTarget.dataset.index].imgUrl = '';
+		this.setData({
+			commentList: temp
+		})
+	},
 
 	/**
 	 * 生命周期函数--监听页面初次渲染完成

+ 1 - 1
nngkxxdp/src/main/resources/static/nnzwminiapp/pages/commentList/commentList.wxml

@@ -3,7 +3,7 @@
 	<view class="top-cont">此菜品上周共获得<span>{{compliment}}</span>次点赞,获得<span>{{commentCount}}</span>次评论</view>
 	<view class="content">
 		<view wx:for="{{commentList}}" wx:key="index" class="cont-width">
-			<image class="logo" src="{{item.imgUrl ? item.imgUrl :'/pages/images/user_logo.png'}}" alt=""></image>
+			<image class="logo" src="{{item.imgUrl ? item.imgUrl :'/pages/images/user_logo.png'}}" alt="" binderror="imageError" data-index="{{index}}"></image>
 			<view class="right">
 				<view class="name">
 					<view class="left">

+ 16 - 0
nngkxxdp/src/main/resources/static/nnzwminiapp/pages/deliciousList/deliciousList.js

@@ -20,10 +20,26 @@ Page({
 		canteenList: []
 	},
 	getDeliciousList() {
+		var that = this;
 		request({
 			url: '/mini/canteen/getDeliciousList',
 			method: 'GET'
 		}).then(res => {
+			res.data.forEach(element => {
+				element.deliciousList.forEach(item => {
+					if (item.dishesPic) {
+						wx.getImageInfo({
+							src: this.data.imgUrl + item.dishesPic,
+							fail() {
+								item.dishesPic = '';
+								that.setData({
+									canteenList: res.data
+								})
+							}
+						});
+					}
+				});
+			});
 			this.setData({
 				canteenList: res.data
 			})

+ 1 - 1
nngkxxdp/src/main/resources/static/nnzwminiapp/pages/ourCanteen/canteen.wxml

@@ -2,7 +2,7 @@
     <view class="cont-width first" bindtap="toMenu" wx:for="{{canteenList}}" wx:for-item="item" wx:for-index="index" wx:key="index" data-name="{{item.canteenName}}" data-item="{{item.id}}">
         <view class="cont-bg">
             <image style="height: 100%;width:100%" mode="scaleToFill" 
-			src="{{item.canteenPhotoPath ? baseUrl + item.canteenPhotoPath : imgUrl + 'cont_bg.png'}}"></image>
+			src="{{item.canteenPhotoPath ? baseUrl + item.canteenPhotoPath : imgUrl + 'cont_bg.png'}}" binderror="imageError" data-index="{{index}}"></image>
         </view>
         <view class="cont-name">
             <view class="title">{{item.canteenName}}</view>

+ 168 - 161
nngkxxdp/src/main/resources/static/nnzwminiapp/pages/ourCanteen/ourCanteen.js

@@ -1,185 +1,192 @@
 // pages/ourCanteen/index.js
 import {
-  imgUrl,
-  baseUrl
+	imgUrl,
+	baseUrl
 } from "../api/request"
 
 import {
-  getCanteenList
+	getCanteenList
 } from "../api/user-api";
 Page({
 
-  /**
-   * 页面的初始数据
-   */
-  data: {
-	imgUrl: imgUrl,
-	baseUrl: baseUrl + '/',
-    tab: 0,
-    item: '',
-    canteenList: [],
-  },
-
-  /**
-   * 生命周期函数--监听页面加载
-   */
-  onLoad: function (options) {
-    // this.islogin();
+	/**
+	 * 页面的初始数据
+	 */
+	data: {
+		imgUrl: imgUrl,
+		baseUrl: baseUrl + '/',
+		tab: 0,
+		item: '',
+		canteenList: [],
+	},
+
+	/**
+	 * 生命周期函数--监听页面加载
+	 */
+	onLoad: function (options) {
+		// this.islogin();
 		this.canteenList();
-  },
-
-  /**
-   * 生命周期函数--监听页面初次渲染完成
-   */
-  onReady: function () {
-
-  },
-
-  /**
-   * 生命周期函数--监听页面显示
-   */
-  onShow: function () {
-    this.setData({
-      item: 0
-    })
-  },
-
-  /**
-   * 生命周期函数--监听页面隐藏
-   */
-  onHide: function () {
-
-  },
-
-  /**
-   * 生命周期函数--监听页面卸载
-   */
-  onUnload: function () {
-
-  },
-
-  /**
-   * 页面相关事件处理函数--监听用户下拉动作
-   */
-  onPullDownRefresh: function () {
-
-  },
-
-  /**
-   * 页面上拉触底事件的处理函数
-   */
-  onReachBottom: function () {
-
-  },
-
-  /**
-   * 用户点击右上角分享
-   */
-  onShareAppMessage: function () {
-
-  },
-
-  changeItem: function (e) {
-    if (e.currentTarget.dataset.item == 1) {
-      wx.navigateTo({
-        url: '/pages/deliciousList/deliciousList'
-      })
-    } else {
-      this.setData({
-        item: e.currentTarget.dataset.item
-      })
-    }
-  },
-  changeTab: function (e) {
-    this.setData({
-      tab: e.detail.current
-    })
-  },
-
-  toMenu: function (e) {
+	},
+
+	/**
+	 * 生命周期函数--监听页面初次渲染完成
+	 */
+	onReady: function () {
+
+	},
+
+	/**
+	 * 生命周期函数--监听页面显示
+	 */
+	onShow: function () {
+		this.setData({
+			item: 0
+		})
+	},
+
+	/**
+	 * 生命周期函数--监听页面隐藏
+	 */
+	onHide: function () {
+
+	},
+
+	/**
+	 * 生命周期函数--监听页面卸载
+	 */
+	onUnload: function () {
+
+	},
+
+	/**
+	 * 页面相关事件处理函数--监听用户下拉动作
+	 */
+	onPullDownRefresh: function () {
+
+	},
+
+	/**
+	 * 页面上拉触底事件的处理函数
+	 */
+	onReachBottom: function () {
+
+	},
+
+	/**
+	 * 用户点击右上角分享
+	 */
+	onShareAppMessage: function () {
+
+	},
+	imageError(e) {
+		let temp = this.data.canteenList;
+		temp[e.currentTarget.dataset.index].canteenPhotoPath = '';
+		this.setData({
+			canteenList: temp
+		})
+	},
+
+	changeItem: function (e) {
+		if (e.currentTarget.dataset.item == 1) {
+			wx.navigateTo({
+				url: '/pages/deliciousList/deliciousList'
+			})
+		} else {
+			this.setData({
+				item: e.currentTarget.dataset.item
+			})
+		}
+	},
+	changeTab: function (e) {
+		this.setData({
+			tab: e.detail.current
+		})
+	},
+
+	toMenu: function (e) {
 		var canteenId = e.currentTarget.dataset.item;
 		var canteenName = e.currentTarget.dataset.name;
-		if (canteenId && canteenId !='') {
+		if (canteenId && canteenId != '') {
 			wx.navigateTo({
 				url: `/pages/menu/menu?canteenId=${canteenId}&canteenName=${canteenName}`
 			})
 		}
-    
-  },
-
-  // 阻止手动拖动
-  catchTouchMove: function (res) {
-    return true
-  },
-  islogin() {
-    let userid = wx.getStorageSync('userId');
-    if (userid === null || userid === '') {
-      this.showDialog();
-    }
-  },
-  showDialog() {
-    wx.showModal({
-      title: '未授权',
-      content: '您登录授权已过期,请重新登陆授权',
-      showCancel: false,
-      confirmText: '登陆',
-      success: function (res) {
-        wx.navigateTo({
-          url: '/pages/login/login',
-        });
-      },
-    });
+
+	},
+
+	// 阻止手动拖动
+	catchTouchMove: function (res) {
+		return true
+	},
+	islogin() {
+		let userid = wx.getStorageSync('userId');
+		if (userid === null || userid === '') {
+			this.showDialog();
+		}
+	},
+	showDialog() {
+		wx.showModal({
+			title: '未授权',
+			content: '您登录授权已过期,请重新登陆授权',
+			showCancel: false,
+			confirmText: '登陆',
+			success: function (res) {
+				wx.navigateTo({
+					url: '/pages/login/login',
+				});
+			},
+		});
 	},
 	back() {
 		wx.navigateBack({
 			delta: 1,
 		})
 	},
-  canteenList() {
-    getCanteenList().then(res => {
-      if (res.result) {
-        let data = res.data
-        for (let i = 0; i < data.length; i++) {
-          const element = data[i];
-          var timeFrame = "";
-          var timeFrameinfo = [];
-          var supplyTime= element.supplyTimeMaps.sort((a,b)=>{
-            return a.timeNode - b.timeNode
-          })
-          for (let j = 0; j < supplyTime.length; j++) {
-            const childElement = supplyTime[j];
-            switch (childElement.timeNode) {
-              case 1:
-                timeFrame+="早";
-                childElement.timeNode = "早上";
-                break
-              case 2:
-                timeFrame+="中";
-                childElement.timeNode = "中午";
-                break
-              case 3:
-                timeFrame+="晚";
-                childElement.timeNode = "晚上";
-                break
-              case 4:
-                timeFrame+="外卖";
-                childElement.timeNode = "外卖";
-                break
-            }
-            if(j!=supplyTime.length-1){
-              timeFrame+="/"
-            }
-            timeFrameinfo.push(childElement)
-          }
-          
-          data[i].timeFrame = timeFrame;
-          data[i].timeFrameinfo = timeFrameinfo;
-        }
-        this.setData({
-          canteenList:data
+	canteenList() {
+		getCanteenList().then(res => {
+			if (res.result) {
+				let data = res.data
+				for (let i = 0; i < data.length; i++) {
+					const element = data[i];
+					var timeFrame = "";
+					var timeFrameinfo = [];
+					var supplyTime = element.supplyTimeMaps.sort((a, b) => {
+						return a.timeNode - b.timeNode
+					})
+					for (let j = 0; j < supplyTime.length; j++) {
+						const childElement = supplyTime[j];
+						switch (childElement.timeNode) {
+							case 1:
+								timeFrame += "早";
+								childElement.timeNode = "早上";
+								break
+							case 2:
+								timeFrame += "中";
+								childElement.timeNode = "中午";
+								break
+							case 3:
+								timeFrame += "晚";
+								childElement.timeNode = "晚上";
+								break
+							case 4:
+								timeFrame += "外卖";
+								childElement.timeNode = "外卖";
+								break
+						}
+						if (j != supplyTime.length - 1) {
+							timeFrame += "/"
+						}
+						timeFrameinfo.push(childElement)
+					}
+
+					data[i].timeFrame = timeFrame;
+					data[i].timeFrameinfo = timeFrameinfo;
+				}
+				this.setData({
+					canteenList: data
 				})
-      }
-    })
-  }
+			}
+		})
+	}
 
 })