icson.zhougmail.com / 豆果展示

// ==UserScript==
// @name        豆果展示
// @namespace   cn.dietcare
// @include     http://www.douguo.com/cookbook/*
// @version     1
// @require 	http://120.24.216.10:8888/dietmanage/JS/dg/jStarbox-master/jstarbox.js
// @grant       none
// @run-at		document-end
// ==/UserScript==
$(function () {
  function addTR(table, trs) {
    for (var i = 0; i <= trs.length - 1; i++) {
      table.append('<tr><td class="lirre" style="padding-top:5px;padding-bottom:5px;vertical-align:middle"><span><label class="fcbm inblok">' + trs[i].name + '</label></span></td><td title="' + trs[i].title + '" id ="' + trs[i].id + '" class="lirre" style="padding-top:5px;padding-bottom:5px;padding-right:113px;vertical-align:middle;border-right:0px"></td></tr>')
      var $div = $('<div></div>')
      $('#' + trs[i].id).append($div)
      $div.starbox({
        average: trs[i].value,
        changeable: false,
        ghosting: true,
        stars: 5
      })
    }
  }
  function addSP(div, sps) {
    for (var i = 0; i <= sps.length - 1; i++) {
      var $span = $('<span></span>')
      var $a = $('<a></a>')
      $a.html(sps[i].name)
      switch (sps[i].level) {
        case '禁食':
          $a.addClass('btnta ajs');
          $a.attr('title', sps[i].name + '患者不建议食用\r' + sps[i].ingredients[0].name + ':' + sps[i].ingredients[0].reason)
          $a.attr('href', 'http://www.douguo.com/caipu/' + sps[i].name)
          break;
        case '慎食':
          $a.addClass('btnta ass');
          $a.attr('title', sps[i].name + '患者少量食用\r' + sps[i].ingredients[0].name + ':' + sps[i].ingredients[0].reason)
          $a.attr('href', 'http://www.douguo.com/caipu/' + sps[i].name)
          break;
        case '宜食':
          $a.addClass('btnta ays');
          $a.attr('title', sps[i].name + '患者适当食用\r' + sps[i].ingredients[0].name + ':' + sps[i].ingredients[0].reason)
          $a.attr('href', 'http://www.douguo.com/caipu/' + sps[i].name)
          break;
        case '推荐':
          $a.addClass('btnta ays');
          $a.attr('title', sps[i].name + '患者推荐食用\r' + sps[i].ingredients[0].name + ':' + sps[i].ingredients[0].reason)
          $a.attr('href', 'http://www.douguo.com/caipu/' + sps[i].name)
          break;
        default:
          $a.addClass('btnta')
      }
      div.append($span.append($a))
    }
  }
  //获取数据

  var name = new Array(),
  weight = new Array()
  $('.lirre').each(function (idx, me) {
    var obj1 = {
    },
    obj2 = {
    }
    if ($(me).children('span').children().length > 0) {
      name.push($(me).children('span').children().html())
      weight.push($($(me).children('span') [1]).html())
      if ($($(me).next()).has('span').length > 0) {
        name.push($($(me).next().children('span').children() [0]).html())
        weight.push($($(me).next().children('span') [1]).html())
      }
    }
  })
  $.ajax({
    type: 'get',
    async: true,
    url: 'http://dietcare.cn:16168/dietservice/rest/queryNutrientElement',
    data: 'token=c8a6aed59bc175af999261f64e0e02aaa2d60476d6bd2194ac5241ba6f34513193fb01dc3e43d662&name=' + name.join('|') + '&weight=' + weight.join('|'),
    dataType: 'jsonp', //数据类型为jsonp  
    jsonp: 'jsonpCallback', //服务端用于接收callback调用的function名的参数  
    success: function (resp) {
      console.log(resp)
      if (!resp.respCode) {
        addEl(resp)
      } else {
        alert('error:' + resp.respMsg)
      }
    },
    error: function (resp) {
      alert('error:' + resp.respMsg)
    }
  })
  function addEl(data) {
    var $css = $('<link></link>').attr({
      rel: 'stylesheet',
      type: 'text/css',
      href: 'http://120.24.216.10:8888/dietmanage/JS/dg/jStarbox-master/css/jstarbox.css'
    })
    $('head').append($css)
    if (Object.getOwnPropertyNames(data.data.nutrient).length > 0) {
      $('.retamr').append('<tr class="mtim">' +
      '<td><h3 class="zfliao">营养成分</h3></td>' +
      '<td style="border-left:0px"><span style="float:right;color:#999"><a id="more" href="#detail">更多</a></span></td>' +
      '</tr>')
      var e = data.data.nutrient.protein * 4 + data.data.nutrient.cho * 4 + data.data.nutrient.fat * 9
      addTR($('.retamr'), [
        {
          name: '蛋白质',
          id: 'dbz',
          title: data.data.nutrient.protein + 'g蛋白质,能量占比' + ((data.data.nutrient.protein * 4 / e) * 100).toFixed(1) + '%',
          value: (data.data.nutrient.protein * 4 / e)
        },
        {
          name: '脂肪',
          id: 'zf',
          title: data.data.nutrient.fat + 'g脂肪,能量占比' + ((data.data.nutrient.fat * 9 / e) * 100).toFixed(1) + '%',
          value: (data.data.nutrient.fat * 9 / e)
        },
        {
          name: '碳水化合物',
          id: 'tshhw',
          title: data.data.nutrient.cho + 'g碳水化合物,能量占比' + ((data.data.nutrient.cho * 4 / e) * 100).toFixed(1) + '%',
          value: (data.data.nutrient.cho * 4 / e)
        }
      ])
      //增加营养元素明细框
      var $detail = $('<div style="display:none"></div>')
      $detail.html('<div id="detail">' +
      '<table width="500" cellspacing="0" cellpadding="0" border="0" class="poptable">' +
      '<tbody><tr><td class="lirre"><span><label class="fcbm inblok">能量(大卡)</label></span><span class="right">' + data.data.nutrient.energy + '</span></td>' +
      '<td><span><label class="fcbm inblok">蛋白质(克)</label></span><span class="right">' + data.data.nutrient.protein + '</span></td>' +
      '</tr><tr><td><span><label class="fcbm inblok">脂肪(克)</label></span><span class="right">' + data.data.nutrient.fat + '</span></td>' +
      '<td><span><label class="fcbm inblok">碳水化合物(克)</label></span><span class="right">' + data.data.nutrient.cho + '</span></td>' +
      '</tr><tr><td><span><label class="fcbm inblok">膳食纤维(克)</label></span><span class="right">' + data.data.nutrient.fiberTotal + '</span></td>' +
      '<td><span><label class="fcbm inblok">胆固醇(毫克)</label></span><span class="right">' + data.data.nutrient.cholesterol + '</span></td>' +
      '</tr><tr><td><span><label class="fcbm inblok">灰分(克)</label></span><span class="right">' + data.data.nutrient.ash + '</span></td>' +
      '<td><span><label class="fcbm inblok">维生素A(微克RE)</label></span><span class="right">' + data.data.nutrient.vitaminaTotal + '</span></td>' +
      '</tr><tr><td><span><label class="fcbm inblok">胡萝卜素(微克)</label></span><span class="right">' + data.data.nutrient.carotene + '</span></td>' +
      '<td><span><label class="fcbm inblok">视黄醇(微克)</label></span><span class="right">' + data.data.nutrient.retinol + '</span></td>' +
      '</tr><tr><td><span><label class="fcbm inblok">硫胺素(毫克)</label></span><span class="right">' + data.data.nutrient.thiamin + '</span></td>' +
      '<td><span><label class="fcbm inblok">核黄素(毫克)</label></span><span class="right">' + data.data.nutrient.riboflav + '</span></td>' +
      '</tr><tr><td><span><label class="fcbm inblok">尼克酸(毫克)</label></span><span class="right">' + data.data.nutrient.niacin + '</span></td>' +
      '<td><span><label class="fcbm inblok">维生素C(毫克)</label></span><span class="right">' + data.data.nutrient.vitaminC + '</span></td>' +
      '</tr><tr><td><span><label class="fcbm inblok">维生素E(毫克)</label></span><span class="right">' + data.data.nutrient.vitamineTotal + '</span></td>' +
      '<td><span><label class="fcbm inblok">钙(毫克)</label></span><span class="right">' + data.data.nutrient.ca + '</span></td>' +
      '</tr><tr><td><span><label class="fcbm inblok">磷(毫克)</label></span><span class="right">' + data.data.nutrient.p + '</span></td>' +
      '<td><span><label class="fcbm inblok">钾(毫克)</label></span><span class="right">' + data.data.nutrient.k + '</span></td>' +
      '</tr><tr><td><span><label class="fcbm inblok">钠(毫克)</label></span><span class="right">' + data.data.nutrient.na + '</span></td>' +
      '<td><span><label class="fcbm inblok">镁(毫克)</label></span><span class="right">' + data.data.nutrient.mg + '</span></td>' +
      '</tr><tr><td><span><label class="fcbm inblok">铁(毫克)</label></span><span class="right">' + data.data.nutrient.fe + '</span></td>' +
      '<td><span><label class="fcbm inblok">锌(毫克)</label></span><span class="right">' + data.data.nutrient.zn + '</span></td>' +
      '</tr><tr><td><span><label class="fcbm inblok">硒(微克)</label></span><span class="right">' + data.data.nutrient.se + '</span></td>' +
      '<td><span><label class="fcbm inblok">铜(毫克)</label></span><span class="right">' + data.data.nutrient.cu + '</span></td>' +
      '</tr><tr><td><span><label class="fcbm inblok">锰(毫克)</label></span><span class="right">' + data.data.nutrient.mn + '</span></td>' +
      '<td><span><label class="fcbm inblok">碘(微克)</label></span><span class="right">' + data.data.nutrient.i + '</span></td>' +
      '</tr>' +
      '</tbody></table></div>');
      $('body').append($detail);
      $('#more').fancybox({
        openEffect: 'fade',
        closeEffect: 'fade',
        scrolling: 'no',
        cyclic: true
      });
    }
    if (data.data.disease.length > 0) {
      $('body').append($('<div id="ygtg" class="mortips mb20 mll"></div>'))
      $('#ygtg').append($('<h4>宜忌:</h4>'))
      if ($('#displaytag').length > 0) {
        $('#displaytag').after($('#ygtg'))
      } else {
        $('div[class=\'sdquan\']').after($('#ygtg'))
      }
      var diseases = new Array()
      $.each(data.data.disease, function (idx, value) {
        diseases.push({
          name: value.name.split('(') [0],
          level: value.level,
          ingredients: value.ingredients
        })
      })
      addSP($('#ygtg'), diseases)
    }
  }
})