sunshibo il y a 1 semaine
Parent
commit
604e5a4d7a
1 fichiers modifiés avec 116 ajouts et 84 suppressions
  1. 116 84
      HBuilderProjects/secondShareGoods.html

+ 116 - 84
HBuilderProjects/secondShareGoods.html

@@ -668,10 +668,12 @@
 		'use strict';
 		'use strict';
 
 
 		/**
 		/**
-		 * 商品详情:POST https://test.ailien.shop/alienSecond/recommend/querySecondGoodsDetail
-		 *   body: application/x-www-form-urlencoded(「表单数据」);键 goodsId、longitude、latitude,值均为字符串;经纬度未传时用默认 121.662567 / 38.925757
-		 * 留言:GET {API_BASE}/commonComment/getListBySourceType — 成功时 data 常为数组;项含 parentId、childCommonComments、headName、headImg、createdTime、content、likeCount
-		 * URL 常用:goodsId、userId、longitude/lon/jingdu、latitude/lat/weidu、pageNum、pageSize、sourceType(含 hash 内 ? 参数)
+		 * 进入页先校验商品是否存在:
+		 *   POST {API_BASE_SECOND}/recommend/querySecondGoodsDetailWithOutJWT
+		 *   body(x-www-form-urlencoded):goodsId、longitude、latitude、phoneId(均为字符串)
+		 * 无有效 data 或已下架/删除 → shareUndefined.html;有 data → renderGoodsDetail 再拉留言
+		 * 留言:GET {API_BASE}/commonComment/getListBySourceType
+		 * URL:goodsId、userId、phoneId、longitude/lon/jingdu、latitude/lat/weidu、alienSecondBase(可选覆盖二手接口前缀)
 		 */
 		 */
 		/**
 		/**
 		 * 唤起 App 落地:二手商品详情(与 pages.json 路径一致)
 		 * 唤起 App 落地:二手商品详情(与 pages.json 路径一致)
@@ -1319,13 +1321,20 @@
 		}
 		}
 
 
 		var DEFAULT_HERO = '';
 		var DEFAULT_HERO = '';
-		/** 二手商品等业务接口前缀(与 alienStore 分离) */
-		var API_BASE_SECOND = 'https://test.ailien.shop/alienSecond';
-		/** 与接口调试「表单数据」一致;优先取地址栏 longitude/latitude,未带时用默认坐标 */
+		/** 二手商品接口前缀;可用 URL 参数 alienSecondBase / secondApiBase 覆盖 */
+		function resolveSecondApiBase() {
+			var custom = (q('alienSecondBase') || q('secondApiBase') || '').trim();
+			if (custom) {
+				return custom.replace(/\/+$/, '');
+			}
+			return 'http://120.26.186.130:8000/alienSecond';
+		}
+		var API_BASE_SECOND = resolveSecondApiBase();
+		/** 与 querySecondGoodsDetailWithOutJWT 表单一致;优先地址栏,否则默认坐标 */
 		var DEFAULT_REQUEST_LONGITUDE =
 		var DEFAULT_REQUEST_LONGITUDE =
-			(q('longitude') || q('lon') || q('jingdu') || '').trim() || '121.662567';
+			(q('longitude') || q('lon') || q('jingdu') || '').trim() || '121.662527';
 		var DEFAULT_REQUEST_LATITUDE =
 		var DEFAULT_REQUEST_LATITUDE =
-			(q('latitude') || q('lat') || q('weidu') || '').trim() || '38.925757';
+			(q('latitude') || q('lat') || q('weidu') || '').trim() || '38.925754';
 		var COMMENT_SOURCE_TYPE = 4;
 		var COMMENT_SOURCE_TYPE = 4;
 
 
 		function apiGet(path) {
 		function apiGet(path) {
@@ -1721,34 +1730,61 @@
 			});
 			});
 		}
 		}
 
 
-		function pickGoodsStatusFromStatusApi(res) {
-			if (!res || typeof res !== 'object') return null;
-			var d = isApiOk(res) ? (res.data != null ? res.data : res.result) : null;
-			if (d != null && typeof d === 'object' && d.goodsStatus != null) {
-				return d.goodsStatus;
+		/** 2=已卖出 3/4/5=下架或删除等不可用态 */
+		function isGoodsStatusUnavailable(gs) {
+			if (gs == null) return false;
+			var n = Number(gs);
+			if (!isNaN(n) && (n === 2 || n === 3 || n === 4 || n === 5)) return true;
+			var s = String(gs).trim();
+			return s === '2' || s === '3' || s === '4' || s === '5';
+		}
+
+		function buildShareUndefinedRedirectQuery(gs, ctx) {
+			ctx = ctx || {};
+			var uq = new URLSearchParams();
+			uq.set('goodsUnavailable', '1');
+			if (gs != null && String(gs).trim() !== '') {
+				uq.set('goodsStatus', String(gs).trim());
 			}
 			}
-			if (res.goodsStatus != null) return res.goodsStatus;
-			return null;
+			if (ctx.userId) uq.set('userId', String(ctx.userId));
+			if (ctx.longitude) uq.set('longitude', String(ctx.longitude));
+			if (ctx.latitude) uq.set('latitude', String(ctx.latitude));
+			if (ctx.userCity) uq.set('userCity', String(ctx.userCity));
+			var gid = ctx.goodsId || '';
+			if (gid) {
+				uq.set('goodsId', String(gid));
+				uq.set('id', String(gid));
+			}
+			return uq;
 		}
 		}
 
 
-		/** 打开页时先拉取商品状态;query 参数名为 id */
-		function fetchGoodsStatusById(id) {
-			var sid = String(id != null ? id : '').trim();
-			if (!sid) return Promise.resolve(null);
-			var path =
-				'/secondGoods/getGoodsStatusById?id=' + encodeURIComponent(sid);
-			return fetch(API_BASE_SECOND + path, {
-				method: 'GET',
-				mode: 'cors',
-				credentials: 'omit',
-				headers: { Accept: 'application/json' }
-			}).then(function (res) {
-				if (!res.ok) throw new Error('HTTP ' + res.status);
-				return res.json();
-			});
+		function redirectToShareUndefined(gs, ctx) {
+			window.location.replace(
+				'shareUndefined.html?' + buildShareUndefinedRedirectQuery(gs, ctx).toString()
+			);
+		}
+
+		function isDetailResponseGoodsUnavailable(res) {
+			if (!res || typeof res !== 'object') return true;
+			var d = res.data != null ? res.data : res.result;
+			if (d && typeof d === 'object' && d.goodsStatus != null) {
+				return isGoodsStatusUnavailable(d.goodsStatus);
+			}
+			var msg = res.msg != null ? String(res.msg) : '';
+			if (/已删除|已下架|已卖出|不存在|不可用|下架|卖出|删除/.test(msg)) {
+				return true;
+			}
+			if (!isApiOk(res)) {
+				return !d || (typeof d === 'object' && !Object.keys(d).length);
+			}
+			return !d || (typeof d !== 'object');
 		}
 		}
 
 
-		function fetchSecondGoodsDetail(goodsId, lon, lat, phoneId) {
+		/**
+		 * 载入时校验商品是否存在(querySecondGoodsDetailWithOutJWT)
+		 * @returns {Promise<{redirected:boolean, exists:boolean}>}
+		 */
+		function fetchSecondGoodsDetail(goodsId, lon, lat, phoneId, redirectCtx) {
 			var lonStr =
 			var lonStr =
 				lon != null && String(lon).trim() !== ''
 				lon != null && String(lon).trim() !== ''
 					? String(lon).trim()
 					? String(lon).trim()
@@ -1783,17 +1819,32 @@
 					'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
 					'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
 				},
 				},
 				body: form.toString()
 				body: form.toString()
-			}).then(function (res) {
-				if (!res.ok) throw new Error('HTTP ' + res.status);
-				return res.json();
-			}).then(function (res) {
-				var d = isApiOk(res) ? res.data || res.result : null;
-				if (d && typeof d === 'object') {
-					renderGoodsDetail(d);
-				} else if (res && res.msg) {
-					console.warn('[querySecondGoodsDetail]', res.msg);
-				}
-			});
+			})
+				.then(function (res) {
+					if (!res.ok) throw new Error('HTTP ' + res.status);
+					return res.json();
+				})
+				.then(function (res) {
+					redirectCtx = redirectCtx || {};
+					if (isDetailResponseGoodsUnavailable(res)) {
+						var gsPick =
+							res && res.data && res.data.goodsStatus != null
+								? res.data.goodsStatus
+								: null;
+						redirectToShareUndefined(gsPick, redirectCtx);
+						return { redirected: true, exists: false };
+					}
+					var d = isApiOk(res) ? res.data || res.result : null;
+					if (d && typeof d === 'object') {
+						renderGoodsDetail(d);
+						return { redirected: false, exists: true };
+					}
+					if (res && res.msg) {
+						console.warn('[querySecondGoodsDetailWithOutJWT]', res.msg);
+					}
+					redirectToShareUndefined(null, redirectCtx);
+					return { redirected: true, exists: false };
+				});
 		}
 		}
 
 
 		function fetchCommentList(goodsId, userId, pageNum, pageSize) {
 		function fetchCommentList(goodsId, userId, pageNum, pageSize) {
@@ -1828,59 +1879,40 @@
 			var latResolved = lat || DEFAULT_REQUEST_LATITUDE;
 			var latResolved = lat || DEFAULT_REQUEST_LATITUDE;
 			var pageNum = (q('pageNum') || '1').trim();
 			var pageNum = (q('pageNum') || '1').trim();
 			var pageSize = (q('pageSize') || '10').trim();
 			var pageSize = (q('pageSize') || '10').trim();
+			var redirectCtx = {
+				goodsId: goodsId,
+				userId: userId,
+				longitude: lonResolved,
+				latitude: latResolved,
+				userCity: userCity
+			};
 
 
 			bindCommentThreadDelegation();
 			bindCommentThreadDelegation();
 
 
+			if (String(q('goodsUnavailable') || '').trim() === '1') {
+				redirectToShareUndefined(q('goodsStatus'), redirectCtx);
+				return;
+			}
+
 			if (!goodsId) {
 			if (!goodsId) {
 				applyFromUrl();
 				applyFromUrl();
 				return;
 				return;
 			}
 			}
 
 
-			fetchGoodsStatusById(goodsId)
+			fetchSecondGoodsDetail(goodsId, lon, lat, phoneId, redirectCtx)
 				.catch(function (e) {
 				.catch(function (e) {
-					console.warn('[getGoodsStatusById]', e);
-					return null;
+					console.warn('[querySecondGoodsDetailWithOutJWT]', e);
+					redirectToShareUndefined(null, redirectCtx);
+					return { redirected: true, exists: false };
 				})
 				})
-				.then(function (statusRes) {
-					var gs = pickGoodsStatusFromStatusApi(statusRes);
-					var gsNum = gs != null ? Number(gs) : NaN;
-					var gsStr = gs != null ? String(gs).trim() : '';
-					var gsGotoUndefined =
-						gs != null &&
-						((!isNaN(gsNum) && (gsNum === 2 || gsNum === 4 || gsNum === 5)) ||
-							gsStr === '2' || gsStr === '4' || gsStr === '5');
-					if (gsGotoUndefined) {
-						var uq = new URLSearchParams();
-						uq.set('goodsUnavailable', '1');
-						if (gs != null && String(gs).trim() !== '') {
-							uq.set('goodsStatus', String(gs).trim());
-						}
-						if (userId) {
-							uq.set('userId', userId);
-						}
-						uq.set('longitude', lonResolved);
-						uq.set('latitude', latResolved);
-						if (userCity) {
-							uq.set('userCity', userCity);
-						}
-						window.location.replace('shareUndefined.html?' + uq.toString());
-						return { skipRest: true };
+				.then(function (detailResult) {
+					if (!detailResult || detailResult.redirected) {
+						return;
 					}
 					}
-					return Promise.all([
-						fetchSecondGoodsDetail(goodsId, lon, lat, phoneId).catch(function (e) {
-							console.warn('[querySecondGoodsDetail]', e);
-						}),
-						fetchCommentList(goodsId, userId, pageNum, pageSize).catch(function (e) {
-							console.warn('[getListBySourceType]', e);
-							renderCommentsList([], 0);
-						})
-					]).then(function () {
-						return { skipRest: false };
+					return fetchCommentList(goodsId, userId, pageNum, pageSize).catch(function (e) {
+						console.warn('[getListBySourceType]', e);
+						renderCommentsList([], 0);
 					});
 					});
-				})
-				.then(function (flag) {
-					if (flag && flag.skipRest) return;
-					applyFromUrl();
 				});
 				});
 		}
 		}