如何找出 SAP電商雲產品明細頁面讀取後臺數據的代碼具體位置

以以下這個產品明細頁面爲例:git

http://localhost:4200/electro...json

觀察瀏覽器 url 的格式:/product/553637/nv10api

在 default-routing-config.ts 裏,得知 product/ 後的 553637 表明 productCode,而 nv10 則是 product name.
瀏覽器

由於我使用的是默認的 CSR 即客戶端渲染模式,因此首先看到一個只包含 loading 的 HTML 頁面。electron

而後看到第一個和該產品信息相關的 HTTP 請求:/occ/v2/electronics-spa/orgProducts/553637?fields=name,categories,purchasable,baseOptions(DEFAULT),baseProduct,variantOptions(DEFAULT),variantType&lang=en&curr=USDide

返回的請求響應,從中能夠看到,PDP - Product Detail Page 內的不少數據,都不包含在這個響應裏。工具

接下來這個請求才是咱們要查找的 PDP 明細數據請求:
ui

{
   "availableForPickup" : true,
   "averageRating" : 4.541666666666667,
   "categories" : [ {
      "code" : "576",
      "name" : "Digital Compacts",
      "url" : "/Open-Catalogue/Cameras/Digital-Cameras/Digital-Compacts/c/576"
   }, {
      "code" : "brand_26",
      "name" : "Samsung",
      "url" : "/Brands/Samsung/c/brand_26"
   } ],
   "code" : "553637",
   "configurable" : false,
   "description" : "<b>10.1 mega-pixel 1/1.8\"\"CCD high resolution</b><br/>The NV10 comes in an 18.5mm thin black aluminium body with Samsung's unique blue ring on its expendable lens barrel, and a pop-up flash. A 10.1 mega pixels, 1/1.8\"\" CCD, not normally used in slim cameras, is used to give better detail.<br/><br/><b>Smart Touch + Large 2.5\"\" LCD</b><br/>The new Smart Touch user interface makes it possible to quickly navigate and set menu items or view pictures. The unique makeup of the Smart Touch buttons with large 2.5 inch LCD on the back of the camera is expected to be great fun for users, giving them an experience unlike anything they have tried before.<br/><br/><b>The superior Schneider-KREUZNACH lens</b><br/>The superior Schneider-KREUZNACH lens produces crystal clear images. <br/>Bright lens that start at f2.8 delivers even resolution from the centre to the edges. For more natural-looking pictures, each lens surface is multi-coated to minimize the \"\"ghost\"\" or \"\"flare\"\" phenomenon.<br/><br/><b>Pop-Up Flash</b><br/>The most distinctive feature of the new NV Series is the built-in pop-up flash. The use of a pop-up mechanism made the overall design simple yet luxurious, and its location, right above the lens, hugely improved light diffusion angles and transmission rates. The lens location is also ideal for delivering the flashlight as intended, since it prevents accidental blocking of the flash by a hand.<br/><br/><b>Advanced Shake Reduction</b><br/>The ASR technology prevents the degradation of image clarity and colour common to flash photography. With ASR the effects of camera shake are reduced in lower light conditions. You can even take well exposed, sharper pictures in low light without using a flash at all. It guarantees brighter and more natural pictures.<br/><br/><b>ISO1000 Standards in Action</b><br/>NV series has an auto sensitivity feature that automatically adjusts sensitivity according to the ambient exposure conditions up to ISO 1000. The high sensitivity setting of ISO 1000 enables you to take clear indoor pictures in the dark without camera shake and the resulting image blurring.<br/><br/><b>Photo Gallery function</b><br/>This is an upgraded version of the photo album, which used to let you access the Menu only from Play Back. Now, this new UI lets you go directly to the album. Pictures are grouped and stored by date for easier access.<br/><br/><b>Multiple ways to Recharge</b><br/>Recharging can be done using a cradle or a 24 pin USB cable, as well as by using a detachable cable. Connect the camera to a PC using a 24 pin USB cable, use the adaptor cable to plug it into a power supply, or use any other convenient method to recharge your camera. <br/><br/><b>Advanced Movie Function</b><br/>The NV series supports MPEG-4 VGA (640x480, 30fps) and TVD (720x480, 20fps - NV3) size for an enhanced video shooting experience. You can even edit your movies on the camera itself, making these clever models brilliantly portable but powerful movie-makers.<br/><br/><b>Scene Mode</b><br/>11 Scene Modes and Text Recognition capability NV series have various scene modes to obtain the best picture result in a variety of settings.<br/>The scene modes provided are Night Scene, Portrait, Children, Landscape, Text Recognition, Sunset, Dawn, Backlight, Fireworks, Beach and Snow.",
   "images" : [ {
      "altText" : "NV10",
      "format" : "zoom",
      "imageType" : "PRIMARY",
      "url" : "/medias/?context=bWFzdGVyfGltYWdlc3wyNjQ0MHxpbWFnZS9qcGVnfGg4OC9oNzQvODc5NzQzMTY5MzM0Mnw2ZTBiODI5M2Q0MmVkNDA3NzU2MGRiMmFkNmVjYzA3ZjQzYmUzYWU4YTM5ZGFlMWU0NTRjYjY0ZTg1OTE5NTBj"
   }, {
      "altText" : "NV10",
      "format" : "product",
      "imageType" : "PRIMARY",
      "url" : "/medias/?context=bWFzdGVyfGltYWdlc3wxMjA0MHxpbWFnZS9qcGVnfGg5Yy9oNTMvODc5NzQ1Nzg3NDk3NHxjNTc2ZmRjYzJkODZjNGRmZjM1NTM5Zjc5OGE1NWE2MWYyZThiMjg0NmQ1OGI5ZDg0ZTY0M2YxM2Q5YWYxY2Vh"
   }, {
      "altText" : "NV10",
      "format" : "thumbnail",
      "imageType" : "PRIMARY",
      "url" : "/medias/?context=bWFzdGVyfGltYWdlc3wyNjEzfGltYWdlL2pwZWd8aGU3L2hjOS84Nzk3NDg0Mjg1OTgyfDQxODAyMWNjMWYwOGQwYWM2NzU0ZGNjZjkxNDhlNmQ2MTk2NTlmNWU2ZTZlOTcxYTlmZWQ5ODUwZWQ1ZDNkMTU"
   }, {
      "altText" : "NV10",
      "format" : "cartIcon",
      "imageType" : "PRIMARY",
      "url" : "/medias/?context=bWFzdGVyfGltYWdlc3wxNjk2fGltYWdlL2pwZWd8aDViL2hhYS84Nzk3NTEwNjY0MjIyfGZiNzIxZDAzMzcwYWE5MzJlYzZhYWMwZGY3NWI0MDFjNzg1YTEyODk1ZmQwZjQ5MGNmNzAyMmMzOTVlNWZhY2M"
   }, {
      "altText" : "NV10",
      "format" : "zoom",
      "galleryIndex" : 0,
      "imageType" : "GALLERY",
      "url" : "/medias/?context=bWFzdGVyfGltYWdlc3wyNjQ0MHxpbWFnZS9qcGVnfGg4OC9oNzQvODc5NzQzMTY5MzM0Mnw2ZTBiODI5M2Q0MmVkNDA3NzU2MGRiMmFkNmVjYzA3ZjQzYmUzYWU4YTM5ZGFlMWU0NTRjYjY0ZTg1OTE5NTBj"
   }, {
      "altText" : "NV10",
      "format" : "product",
      "galleryIndex" : 0,
      "imageType" : "GALLERY",
      "url" : "/medias/?context=bWFzdGVyfGltYWdlc3wxMjA0MHxpbWFnZS9qcGVnfGg5Yy9oNTMvODc5NzQ1Nzg3NDk3NHxjNTc2ZmRjYzJkODZjNGRmZjM1NTM5Zjc5OGE1NWE2MWYyZThiMjg0NmQ1OGI5ZDg0ZTY0M2YxM2Q5YWYxY2Vh"
   }, {
      "altText" : "NV10",
      "format" : "thumbnail",
      "galleryIndex" : 0,
      "imageType" : "GALLERY",
      "url" : "/medias/?context=bWFzdGVyfGltYWdlc3wyNjEzfGltYWdlL2pwZWd8aGU3L2hjOS84Nzk3NDg0Mjg1OTgyfDQxODAyMWNjMWYwOGQwYWM2NzU0ZGNjZjkxNDhlNmQ2MTk2NTlmNWU2ZTZlOTcxYTlmZWQ5ODUwZWQ1ZDNkMTU"
   } ],
   "manufacturer" : "Samsung",
   "name" : "NV10",
   "numberOfReviews" : 24,
   "price" : {
      "currencyIso" : "USD",
      "formattedValue" : "$264.69",
      "priceType" : "BUY",
      "value" : 264.69
   },
   "priceRange" : {
   },
   "stock" : {
      "isValueRounded" : false,
      "stockLevel" : 240,
      "stockLevelStatus" : "inStock"
   },
   "summary" : "The NV10 comes in an 18.5mm thin black aluminium body with Samsung's unique blue ring on its extendable lens barrel, and a pop-up flash.",
   "url" : "/electronics-spa/products/553637"
}

url: /occ/v2/electronics-spa/products/553637?fields=code,configurable,configuratorType,name,summary,price(formattedValue,DEFAULT),images(galleryIndex,FULL),baseProduct,averageRating,stock(DEFAULT),description,availableForPickup,url,numberOfReviews,manufacturer,categories(FULL),priceRange,multidimensional,tags&lang=en&curr=USDthis

在 Chrome 開發者工具 network 標籤頁裏選中這個 HTTP 請求,把鼠標放在 Initiator colume 上,會彈出調用棧: url

耐心往下瀏覽調用棧列表,直至找到 product-loading.service.ts:

單擊該文件,自動跳轉到第116行,設置一個斷點,按 F5 刷新, 斷點觸發。第 117 行代碼,發送了一個新的 action 到 ngrx Store,類型爲:ProductActions.LoadProduct:

斷點觸發:

根據關鍵字 ProductActions.LoadProduct 搜索,便可找處處理這個 action 的 effect 實現:product.effect.ts 文件:

在下圖第 40 行設置斷點:

下圖就是咱們最終要找到代碼,SAP Spartacus 最終仍是經過 http client, 請求對應 url 表明的資源:

url :

"https://api.cg79x9wuu9-eccommerc1-p5-public.model-t.myhybris.cloud/occ/v2/electronics-spa/orgProducts/553637?fields=name,categories,purchasable,baseOptions(DEFAULT),baseProduct,variantOptions(DEFAULT),variantType"

更多細節,請查看下列源代碼:

projects\core\src\product\connectors\product\product.connector.ts