1 Star 0 Fork 0

0x12fd16b/IJiechaoImageMagician

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
util.js 3.42 KB
一键复制 编辑 原始数据 按行查看 历史
0x12fd16b 提交于 2016-12-29 23:15 . update capture config reference
var path = require('path');
var set = require('./config/set');
var uuid = require('uuid/v4');
var logger = require('./config/logger').getLogger("util.js");
var Image = require('canvas').Image;
var fs = require('fs');
var queryString = require("query-string");
var http = require("http-request");
var getPath = function () {
var outPath = "";
if (arguments.length == 0) {
return outPath;
}
outPath = outPath.concat(__dirname);
for (var i = 0; i < arguments.length; i++) {
outPath = path.join(outPath, arguments[i]);
}
return outPath;
};
var platforms = ["pc", "mobile", "wechat", "qq"];
var stockCities = ["北京,", "上海,", "广州,", "成都,", "深圳,", "南宁,", "武汉,", "天津,", "南京,", "西安"];
var HttpStatus = {
OK: 200,
LOGIC_INTERRUPT: 422,
NOT_FOUND: 404,
ERROR: 500
};
var currency = {
CN: "¥#",
US: "$#",
JP: "¥#"
};
var getImageName = function() {
return getPath(set.imageOutPath, set.tempResourcePath, uuid() + '.jpeg');
};
var getSelfUrl = function (vendor, id) {
return set.settings.selfUrlFormat.replace("$vendor", vendor).replace("$id", id);
};
var getSourceImageByName = function (imageName) {
var sourceImage = new Image;
sourceImage.src = fs.readFileSync(path.isAbsolute(imageName) ? imageName : getPath(set.sourcePath, imageName));
return sourceImage;
};
var httpRender = function (code, message, data, res) {
console.log(code, message, data, res);
res.send({
code: code,
message: message,
data: data
});
};
var formatPriceWithCurrency = function (price, locale) {
if (locale) {
return currency[locale].replace("#", (price / 100.0).toFixed(2));
} else {
return (price / 100.0).toFixed(2);
}
};
var getCheapestPlatformAndPrice = function (platformPrices, locale) {
if (!platformPrices || platformPrices.length == 0) {
return {};
}
var minPricePlatform = platformPrices[0];
var minPrice = minPricePlatform.price;
for (var i = 1; i < platformPrices.length; i++) {
if (platformPrices[i].price < minPrice) {
minPricePlatform = platformPrices[i];
}
}
return minPricePlatform;
};
var getPlatformIcons = function (minPricePlatform) {
var outImages = [];
for (var i = 0; i < platforms.length; i++) {
var nameAfterfix = platforms[i] === minPricePlatform.toLowerCase() ? "_suff.png" : "_lack.png";
outImages.push(getSourceImageByName(platforms[i].concat(nameAfterfix)));
}
return outImages;
};
var getMinPriceOptions = function (minPrice, promotionStr, vendorId) {
var reqBody = {
price: minPrice,
promotionStr: promotionStr,
vendorId: vendorId
};
reqBody = queryString.stringify(reqBody);
return {
url: "http://rest.ijiechao.com:6060/product/minPrice.do",
reqBody: new Buffer(reqBody),
headers: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
}
};
};
exports.getImageName = getImageName;
exports.getSelfUrl = getSelfUrl;
exports.getSourceImageByName = getSourceImageByName;
exports.HttpStatus = HttpStatus;
exports.httpRender = httpRender;
exports.getCheapestPlatformAndPrice = getCheapestPlatformAndPrice;
exports.getPlatformIcons = getPlatformIcons;
exports.getMinPriceOptions = getMinPriceOptions;
exports.formatPriceWithCurrency = formatPriceWithCurrency;
exports.inStockCities = stockCities;
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/hex12fd16b/IJiechaoImageMagician.git
git@gitee.com:hex12fd16b/IJiechaoImageMagician.git
hex12fd16b
IJiechaoImageMagician
IJiechaoImageMagician
master

搜索帮助