Title1

Title2

Title3

4-1-1 商品資料處理

取得所有商品

let rows = get_rows_prod_main();

取得「啟用」的商品


/**====================================
  得到所有啟用的商品 
=====================================*/
function get_rows_prod_main_enable(){
  let rows = get_rows_prod_main();
  let prods = rows.filter(function(row){
    if(row.enable === '是')return true;
  })
  console.log(prods);
  return prods;
}

字串轉陣列


function test1(){
  let a = '+1 商品1';
  let a_arr = a.split(' ');
  console.log(a_arr)
}


function test2(){
  let a = '+1|+2|+5';
  let a_arr = a.split('|');
  console.log(a_arr)
}

套用「carouselTemplate()」


/**======================================
  商品展示 樣版
=======================================*/
function prodCarouselTemplate() {
  let prods = get_rows_prod_main_enable();

  let messages = [];
  
  for (let i in prods) {
    let title = prods[i]['title'];

    let content = `商品單位: ${prods[i]['unit']}
商品單價: ${prods[i]['price']} 元
${prods[i]['content']}`;

    let amount = prods[i]['amount'].split('|');
    let buttons = [];
    for (let j in amount) {
      buttons.push({
        "type": "button",
        "style": "link",
        "height": "sm",
        "action": {
          "type": "message",
          "label": amount[j],                     //訊息標題
          "text": amount[j] + ' ' + title //  訊息內容
        }
      });
    }
    messages.push({
      "type": "bubble",
      "size": "micro",
      "hero": {
        "type": "image",
        "url": prods[i]['pic'],//圖片
        "size": "full",
        "aspectMode": "cover",
        "aspectRatio": "320:213"
      },
      "body": {
        "type": "box",
        "layout": "vertical",
        "spacing": "sm",
        "paddingAll": "13px",
        "contents": [
          {
            "type": "text",
            "text": title,//標題
            "weight": "bold",
            "size": "sm",
            "wrap": true
          },
          {
            "type": "text",
            "text": content,//內容
            "color": "#8C8C8C",
            "size": "sm",
            "wrap": true
          },
          {
            "type": "separator",
            "margin": "xxl"
          },
          {
            "type": "box",
            "layout": "vertical",
            "contents": buttons
          }
        ]
      }
    });

  }
  return messages;
}

判斷「訂購商品」關鍵字


    if (botMessageText === '商品訂購') {
      //---------------------------------- carousel(商品展示)
      let messages = [{
        "type": "flex",
        "altText": "商品展示",//訊息副標題
        "contents": {
          "type": "carousel",
          "contents": prodCarouselTemplate()
        }
      }];

      lineReplyMessage(replyToken, messages);

    } else {

    }