UNPKG

minif_node

Version:

各类平台的对接的一个sdk

562 lines (561 loc) 30.9 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.tik_tok = void 0; /*** * 关于抖音移动和网站应用的 */ var base_1 = require("./base"); var AccountApiPool_1 = require("./common/AccountApiPool"); var util_1 = require("./common/util"); var tik_tok_live_url = 'https://open.douyin.com'; var tik_tok = /** @class */ (function (_super) { __extends(tik_tok, _super); /** * 构造器 * @param obj object类型 包括下面参数 * @param appid 抖音生活的appid * @param appSecret 抖音生活的appSecret * @param redis_options redis的配置信息 */ function tik_tok(obj) { var _this = _super.call(this) || this; _this.appid = obj.appid; _this.appSecret = obj.appSecret; _this.redisconfig = obj.redis_options; return _this; } /*** * 获取这次的请求的token信息 * 文档位置:https://partner.open-douyin.com/docs/resource/zh-CN/dop/develop/openapi/account-permission/client-token */ tik_tok.prototype.getAccessToken = function () { return __awaiter(this, void 0, void 0, function () { var token_key, url, AccessToken, redisred, redisredgetna, redisredget, gettokenres, redisredsetres, redisredtime, tokenCache, tokenCacheres, gettokenres, tokenCacheset; return __generator(this, function (_a) { switch (_a.label) { case 0: token_key = "".concat(this.appid, "-tik_tok_AccessToken"); url = "".concat(tik_tok_live_url, "/oauth/client_token/"); if (!this.redisconfig) return [3 /*break*/, 8]; if (!this.redisconfig.host && !this.redisconfig.port) throw new Error("\u4F60\u662F\u9700\u8981\u914D\u7F6Eredis\u7F13\u5B58\uFF1F\u4F46\u662F\u770B\u4F60\u914D\u7F6E\u7684\u53C2\u6570\u5C11\u4E86\u4E3B\u8981\u7684host\u548Cport\u53C2\u6570"); return [4 /*yield*/, (0, AccountApiPool_1.createredis)(this.redisconfig || { host: "127.0.0.1", port: 3306, password: "", db: 0 })]; case 1: redisred = _a.sent(); return [4 /*yield*/, redisred.get(token_key)]; case 2: redisredgetna = _a.sent(); if (!redisredgetna) return [3 /*break*/, 3]; redisredget = JSON.parse(redisredgetna); AccessToken = redisredget; return [3 /*break*/, 7]; case 3: return [4 /*yield*/, this.postRequest(url, { client_key: this.appid, client_secret: this.appSecret, grant_type: 'client_credential' })]; case 4: gettokenres = _a.sent(); // 说明有问题 if (gettokenres.message == 'error') { throw new Error("code:".concat(gettokenres.data.error_code, ",\u8FD4\u56DE\u6D88\u606F:").concat(gettokenres.data.description)); } return [4 /*yield*/, redisred.set(token_key, JSON.stringify(gettokenres.data.access_token))]; case 5: redisredsetres = _a.sent(); return [4 /*yield*/, redisred.expire(token_key, gettokenres.data.expires_in - 600)]; case 6: redisredtime = _a.sent(); if (!redisredsetres) throw new Error("\u8BBE\u7F6Eredis\u503C\u9519\u8BEF~"); AccessToken = gettokenres.data.access_token; _a.label = 7; case 7: return [3 /*break*/, 11]; case 8: tokenCache = (0, AccountApiPool_1.createNodeCache)(this.appid); tokenCacheres = tokenCache.get(token_key); if (!tokenCacheres) return [3 /*break*/, 9]; AccessToken = tokenCacheres; return [3 /*break*/, 11]; case 9: return [4 /*yield*/, this.postRequest(url, { client_key: this.appid, client_secret: this.appSecret, grant_type: 'client_credential' })]; case 10: gettokenres = _a.sent(); // 说明有问题 if (gettokenres.message == 'error') { throw new Error("code:".concat(gettokenres.data.error_code, ",\u8FD4\u56DE\u6D88\u606F:").concat(gettokenres.data.description)); } tokenCacheset = tokenCache.set(token_key, gettokenres.data.access_token, gettokenres.data.expires_in - 600); AccessToken = gettokenres.data.access_token; _a.label = 11; case 11: return [2 /*return*/, AccessToken]; } }); }); }; // ------------------ 团购核销 start ---------------------- /** * 验券准备 * 文档位置:https://partner.open-douyin.com/docs/resource/zh-CN/local-life/develop/OpenAPI/life.capacity.fulfilment/certificate.prepare * @param encrypted_data 从二维码解析出来的标识(传参前需要先进行URL编码)(encrypted_data/code必须二选一) * @param code 原始的抖音团购券码 (encrypted_data/code必须二选一) * */ tik_tok.prototype.check_ticket_start = function (data) { return __awaiter(this, void 0, void 0, function () { var prreq, getshoturlres, queryURLParamsres, getAccessTokenres, url, _params, querystring, o_url, get_check_ticket_start; return __generator(this, function (_a) { switch (_a.label) { case 0: prreq = {}; if (!(data.code || data.short_chain)) return [3 /*break*/, 6]; if (!data.short_chain) return [3 /*break*/, 2]; if (!data.short_chain) throw new Error("\u5F53\u524D\u662F\u626B\u7801\u540E\u89E3\u6790\u4F46\u662F\u7F3A\u5C11\u4E86\u8FD9\u4E2A\u77ED\u94FE~"); return [4 /*yield*/, this.getshotreq(data.short_chain)]; case 1: getshoturlres = _a.sent(); queryURLParamsres = (0, util_1.queryURLParams)(getshoturlres.url); if (!queryURLParamsres.object_id) throw new Error("\u89E3\u6790\u626B\u7801\u7684object_id\u9519\u8BEF~"); prreq["encrypted_data"] = queryURLParamsres.object_id; return [3 /*break*/, 3]; case 2: if (data.code) { prreq['code'] = data.code; } else { throw new Error("\u5F53\u524D\u5728\u9A8C\u5238\u51C6\u5907\u6B65\u9AA4\u9047\u89C1\u4E86\u9519\u8BEF~"); } _a.label = 3; case 3: return [4 /*yield*/, this.getAccessToken()]; case 4: getAccessTokenres = _a.sent(); url = "".concat(tik_tok_live_url, "/goodlife/v1/fulfilment/certificate/prepare"); _params = __assign({}, prreq); querystring = (0, util_1.querystringfun)(_params); o_url = url + "?".concat(querystring); return [4 /*yield*/, this.getRequest(o_url, { "access-token": getAccessTokenres })]; case 5: get_check_ticket_start = _a.sent(); return [2 /*return*/, get_check_ticket_start]; case 6: throw new Error("\u5F53\u524D\u9700\u8981\u4F20\u5165\u6838\u9500code\u6216\u8005\u626B\u63CF\u77ED\u94FE\u7684\u94FE\u63A5"); } }); }); }; /** * 验券 * 文档位置:https://partner.open-douyin.com/docs/resource/zh-CN/local-life/develop/OpenAPI/life.capacity.fulfilment/certificate.verify * @param data 包含一下的信息 * @param verify_token 一次验券的标识 在验券准备中获取~ * @param poi_id 核销的抖音门店id * 还有一些信息可以看文档 * */ tik_tok.prototype.check_ticket_open = function (data) { return __awaiter(this, void 0, void 0, function () { var getAccessTokenres, url, get_check_ticket_start; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getAccessToken()]; case 1: getAccessTokenres = _a.sent(); url = "".concat(tik_tok_live_url, "/goodlife/v1/fulfilment/certificate/verify"); return [4 /*yield*/, this.postRequest(url, __assign({}, data), { "access-token": getAccessTokenres })]; case 2: get_check_ticket_start = _a.sent(); return [2 /*return*/, get_check_ticket_start]; } }); }); }; /** * 撤销核销 * 文档位置:https://partner.open-douyin.com/docs/resource/zh-CN/local-life/develop/OpenAPI/life.capacity.fulfilment/certificate.cancel * @param data 包含一下的信息 * @param verify_id 代表券码一次核销的唯一标识(验券时返回)(次卡撤销多次时请填0) * @param certificate_id 代表一张券码的标识(验券时返回) * 还有一些信息可以看文档 */ tik_tok.prototype.check_ticket_cancel = function (data) { return __awaiter(this, void 0, void 0, function () { var getAccessTokenres, url, get_check_ticket_cancel; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!data.verify_id || !data.certificate_id) throw new Error("\u5F53\u524Dverify_id\u548Ccertificate_id\u662F\u5FC5\u4F20\u7684~"); return [4 /*yield*/, this.getAccessToken()]; case 1: getAccessTokenres = _a.sent(); url = "".concat(tik_tok_live_url, "/goodlife/v1/fulfilment/certificate/cancel"); return [4 /*yield*/, this.postRequest(url, __assign({}, data), { "access-token": getAccessTokenres })]; case 2: get_check_ticket_cancel = _a.sent(); return [2 /*return*/, get_check_ticket_cancel]; } }); }); }; /** * 券状态查询 * 文档位置:https://partner.open-douyin.com/docs/resource/zh-CN/local-life/develop/OpenAPI/life.capacity.fulfilment/certificate.get * @param encrypted_code 验券准备接口返回的加密券码(传参前需要先进行URL编码) */ tik_tok.prototype.check_ticket_certificateget = function (encrypted_code) { return __awaiter(this, void 0, void 0, function () { var getAccessTokenres, url, _params, querystring, o_url, get_check_ticket_cancel; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!encrypted_code) throw new Error("\u5F53\u524Dencrypted_code\u53C2\u6570\u662F\u5FC5\u4F20\u7684~"); return [4 /*yield*/, this.getAccessToken()]; case 1: getAccessTokenres = _a.sent(); url = "".concat(tik_tok_live_url, "/goodlife/v1/fulfilment/certificate/get"); encrypted_code = encodeURIComponent(encrypted_code); _params = { encrypted_code: encrypted_code }; querystring = (0, util_1.querystringfun)(_params); o_url = url + "?".concat(querystring) //encodeURIComponent(); //decodeURI(decodeURI(url + `?${querystring}`)); ; console.log("o_url======>", o_url); return [4 /*yield*/, this.getRequest(o_url, { "access-token": getAccessTokenres })]; case 2: get_check_ticket_cancel = _a.sent(); return [2 /*return*/, get_check_ticket_cancel]; } }); }); }; // ------------------ 团购核销 end---------------------- // ------------------团购对账 start ---------------------- /** * 验券历史查询 * 文档位置:https://partner.open-douyin.com/docs/resource/zh-CN/local-life/develop/OpenAPI/life.capacity.billing/certificate.verifyrecord.query * @param data 参数 */ tik_tok.prototype.check_ticket_history_list = function (data) { return __awaiter(this, void 0, void 0, function () { var getAccessTokenres, url, _params, querystring, o_url, get_check_ticket_cancel; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!data.account_id) throw new Error("\u5F53\u524D\u6709\u9700\u8981\u5FC5\u586B\u7684\u53C2\u6570\uFF0C\u4F46\u662F\u4F60\u6CA1\u6709\u4F20\u6216\u8005\u6709\u95EE\u9898\u3002\u8BF7\u68C0\u67E5~"); return [4 /*yield*/, this.getAccessToken()]; case 1: getAccessTokenres = _a.sent(); url = "".concat(tik_tok_live_url, "/goodlife/v1/fulfilment/certificate/verify_record/query"); _params = __assign({}, data); querystring = (0, util_1.querystringfun)(_params); o_url = url + "?".concat(querystring); console.log("o_url======>", o_url); return [4 /*yield*/, this.getRequest(o_url, { "access-token": getAccessTokenres })]; case 2: get_check_ticket_cancel = _a.sent(); return [2 /*return*/, get_check_ticket_cancel]; } }); }); }; /** * 账单详细查询 * 文档位置:https://partner.open-douyin.com/docs/resource/zh-CN/local-life/develop/OpenAPI/life.capacity.billing/detailedquery * @param data 参数 */ tik_tok.prototype.check_ticket_detailed_query = function (data) { return __awaiter(this, void 0, void 0, function () { var getAccessTokenres, url, _params, querystring, o_url, get_check_ticket_cancel; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!data.account_id) throw new Error("\u5F53\u524D\u6709\u9700\u8981\u5FC5\u586B\u7684\u53C2\u6570\uFF0C\u4F46\u662F\u4F60\u6CA1\u6709\u4F20\u6216\u8005\u6709\u95EE\u9898\u3002\u8BF7\u68C0\u67E5~"); return [4 /*yield*/, this.getAccessToken()]; case 1: getAccessTokenres = _a.sent(); url = "".concat(tik_tok_live_url, "/goodlife/v1/settle/ledger/detailed_query"); _params = __assign({}, data); querystring = (0, util_1.querystringfun)(_params); o_url = url + "?".concat(querystring); console.log("o_url======>", o_url); return [4 /*yield*/, this.getRequest(o_url, { "access-token": getAccessTokenres })]; case 2: get_check_ticket_cancel = _a.sent(); return [2 /*return*/, get_check_ticket_cancel]; } }); }); }; /** * 账单查询 * 文档位置:https://partner.open-douyin.com/docs/resource/zh-CN/local-life/develop/OpenAPI/life.capacity.billing/merchantquery * @param data 参数 */ tik_tok.prototype.check_ticket_merchantquery = function (data) { return __awaiter(this, void 0, void 0, function () { var getAccessTokenres, url, _params, querystring, o_url, get_check_ticket_cancel; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!data.account_id) throw new Error("\u5F53\u524D\u6709\u9700\u8981\u5FC5\u586B\u7684\u53C2\u6570\uFF0C\u4F46\u662F\u4F60\u6CA1\u6709\u4F20\u6216\u8005\u6709\u95EE\u9898\u3002\u8BF7\u68C0\u67E5~"); return [4 /*yield*/, this.getAccessToken()]; case 1: getAccessTokenres = _a.sent(); url = "".concat(tik_tok_live_url, "/goodlife/v1/settle/ledger/query"); _params = __assign({}, data); querystring = (0, util_1.querystringfun)(_params); o_url = url + "?".concat(querystring); console.log("o_url======>", o_url); return [4 /*yield*/, this.getRequest(o_url, { "access-token": getAccessTokenres })]; case 2: get_check_ticket_cancel = _a.sent(); return [2 /*return*/, get_check_ticket_cancel]; } }); }); }; /** * 分账明细查询 * 文档位置:https://partner.open-douyin.com/docs/resource/zh-CN/local-life/develop/OpenAPI/life.capacity.billing/ledger.query-record-by-cert * @param data 参数 */ tik_tok.prototype.query_record_by_cert = function (data) { return __awaiter(this, void 0, void 0, function () { var getAccessTokenres, url, _params, querystring, o_url, get_query_record_by_cert; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getAccessToken()]; case 1: getAccessTokenres = _a.sent(); url = "".concat(tik_tok_live_url, "/goodlife/v1/settle/ledger/query_record_by_cert"); _params = __assign({}, data); querystring = (0, util_1.querystringfun)(_params); o_url = url + "?".concat(querystring); return [4 /*yield*/, this.getRequest(o_url, { "access-token": getAccessTokenres })]; case 2: get_query_record_by_cert = _a.sent(); return [2 /*return*/, get_query_record_by_cert]; } }); }); }; // ------------------团购对账 end ---------------------- // ------------------门店的 start ---------------------- /** * 获取门店的信息 * 文档地址:https://partner.open-douyin.com/docs/resource/zh-CN/local-life/develop/OpenAPI/life.capacity.shop/shop.query * @param poi_id poi_id 抖音门店POI_ID * @param third_id 三方ID * @param account_id 本地生活账户ID inter_get_poi_query */ tik_tok.prototype.get_poi_query = function (data) { return __awaiter(this, void 0, void 0, function () { var getAccessTokenres, url, _params, querystring, o_url, get_poi_queryres; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(data.account_id || data.poi_id || data.third_id)) return [3 /*break*/, 3]; return [4 /*yield*/, this.getAccessToken()]; case 1: getAccessTokenres = _a.sent(); url = "".concat(tik_tok_live_url, "/goodlife/v1/shop/poi/query"); _params = __assign({}, data); querystring = (0, util_1.querystringfun)(_params); o_url = url + "?".concat(querystring); return [4 /*yield*/, this.getRequest(o_url, { "access-token": getAccessTokenres })]; case 2: get_poi_queryres = _a.sent(); return [2 /*return*/, get_poi_queryres]; case 3: throw new Error("\u5FC5\u987B\u8981\u4F20:\u6296\u97F3\u95E8\u5E97POI_ID ||\u4E09\u65B9ID || \u672C\u5730\u751F\u6D3B\u8D26\u6237ID \u7684\u5176\u4E2D\u4E00\u4E2A"); } }); }); }; // ------------------门店的 end ---------------------- // -----------------商品信息 start-------------------- /** * 获取商品的数据 (注意这里还有一个是商品列表的) *文档位置:https://partner.open-douyin.com/docs/resource/zh-CN/local-life/develop/OpenAPI/goods/draft.get * @param product_ids 商品ID列表,多个值使用,拼接 * @param out_ids 外部商品ID列表,多个值使用,拼接 * @param account_id 商家ID,传入时服务商须与该商家满足授权关系 */ tik_tok.prototype.get_merchandise_online_data = function (data) { return __awaiter(this, void 0, void 0, function () { var getAccessTokenres, url, _params, querystring, o_url, get_merchandise_onlineres; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getAccessToken()]; case 1: getAccessTokenres = _a.sent(); url = "".concat(tik_tok_live_url, "/goodlife/v1/goods/product/online/get"); _params = __assign({}, data); querystring = (0, util_1.querystringfun)(_params); o_url = url + "?".concat(querystring); return [4 /*yield*/, this.getRequest(o_url, { "access-token": getAccessTokenres })]; case 2: get_merchandise_onlineres = _a.sent(); return [2 /*return*/, get_merchandise_onlineres]; } }); }); }; /** * 获取商品的数据列表 (注意这里还有一个是商品的) * 文档位置:https://partner.open-douyin.com/docs/resource/zh-CN/local-life/develop/OpenAPI/goods/query * @param product_ids 商品ID列表,多个值使用,拼接 * @param out_ids 外部商品ID列表,多个值使用,拼接 * @param account_id 商家ID,传入时服务商须与该商家满足授权关系 */ tik_tok.prototype.get_merchandise_online_data_list = function (data) { return __awaiter(this, void 0, void 0, function () { var getAccessTokenres, url, _params, querystring, o_url, get_merchandise_online_data_list; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getAccessToken()]; case 1: getAccessTokenres = _a.sent(); url = "".concat(tik_tok_live_url, "/goodlife/v1/goods/product/online/query"); _params = __assign({}, data); querystring = (0, util_1.querystringfun)(_params); o_url = url + "?".concat(querystring); return [4 /*yield*/, this.getRequest(o_url, { "access-token": getAccessTokenres })]; case 2: get_merchandise_online_data_list = _a.sent(); return [2 /*return*/, get_merchandise_online_data_list]; } }); }); }; // -----------------商品信息 end---------------------- // -----------------订单的 start---------------------- /** * 查询订单 * 文档位置:https://partner.open-douyin.com/docs/resource/zh-CN/local-life/develop/OpenAPI/order.query/query * @param poi_id poi_id 抖音门店POI_ID * @param third_id 三方ID * @param account_id 本地生活账户ID */ tik_tok.prototype.get_trade_order_query = function (data) { return __awaiter(this, void 0, void 0, function () { var getAccessTokenres, url, _params, querystring, o_url, get_poi_queryres; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getAccessToken()]; case 1: getAccessTokenres = _a.sent(); url = "".concat(tik_tok_live_url, "/goodlife/v1/trade/order/query"); _params = __assign({}, data); querystring = (0, util_1.querystringfun)(_params); o_url = url + "?".concat(querystring); return [4 /*yield*/, this.getRequest(o_url, { "access-token": getAccessTokenres })]; case 2: get_poi_queryres = _a.sent(); return [2 /*return*/, get_poi_queryres]; } }); }); }; return tik_tok; }(base_1.Base)); exports.tik_tok = tik_tok;