UNPKG

minif_node

Version:

各类平台的对接的一个sdk

228 lines (227 loc) 12 kB
"use strict"; /*** * qq小程序的 */ 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 __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.qqmini = void 0; var base_1 = require("./base"); var AccountApiPool_1 = require("./common/AccountApiPool"); var util_1 = require("./common/util"); var qqmini_url = 'https://api.q.qq.com'; var qqmini = /** @class */ (function (_super) { __extends(qqmini, _super); /** * 构造器 * @param obj object类型 包括下面参数 * @param appid qq的appid * @param appSecret qq的appSecret * @param redis_options redis的配置信息 * */ function qqmini(obj) { var _this = _super.call(this) || this; _this.appid = obj.appid; _this.appSecret = obj.appSecret; _this.redisconfig = obj.redis_options; return _this; } /*** * 获取这次的请求的token信息 * 文档位置:https://q.qq.com/wiki/develop/miniprogram/server/open_port/port_use.html#getaccesstoken */ qqmini.prototype.getAccessToken = function () { return __awaiter(this, void 0, void 0, function () { var token_key, url, AccessToken, reqoption, redisred, redisredgetna, redisredget, querystring, o_url, gettokenres, redisredsetres, redisredtime, tokenCache, tokenCacheres, querystring, o_url, gettokenres, tokenCacheset; return __generator(this, function (_a) { switch (_a.label) { case 0: token_key = "".concat(this.appid, "-qqmini_AccessToken"); url = "".concat(qqmini_url, "/api/getToken"); reqoption = { appid: this.appid, secret: this.appSecret, grant_type: 'client_credential' }; 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: querystring = (0, util_1.querystringfun)(reqoption); o_url = url + "?".concat(querystring); return [4 /*yield*/, this.getRequest(o_url)]; case 4: gettokenres = _a.sent(); // 说明有问题 if (gettokenres.errcode != 0) { throw new Error("code:".concat(gettokenres.errcode, ",\u8FD4\u56DE\u6D88\u606F:").concat(gettokenres.errmsg)); } return [4 /*yield*/, redisred.set(token_key, JSON.stringify(gettokenres.access_token))]; case 5: redisredsetres = _a.sent(); return [4 /*yield*/, redisred.expire(token_key, gettokenres.expires_in - 600)]; case 6: redisredtime = _a.sent(); if (!redisredsetres) throw new Error("\u8BBE\u7F6Eredis\u503C\u9519\u8BEF~"); AccessToken = gettokenres.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: querystring = (0, util_1.querystringfun)(reqoption); o_url = url + "?".concat(querystring); return [4 /*yield*/, this.getRequest(o_url)]; case 10: gettokenres = _a.sent(); // 说明有问题 if (gettokenres.errcode != 0) { throw new Error("code:".concat(gettokenres.errcode, ",\u8FD4\u56DE\u6D88\u606F:").concat(gettokenres.errmsg)); } tokenCacheset = tokenCache.set(token_key, gettokenres.access_token, gettokenres.expires_in - 600); AccessToken = gettokenres.access_token; _a.label = 11; case 11: return [2 /*return*/, AccessToken]; } }); }); }; /** * qq小程序登陆 * 文档地址:https://q.qq.com/wiki/develop/miniprogram/server/open_port/port_login.html#code2session * @param code 小程序端login生成的code * @returns */ qqmini.prototype.qqcodelogin = function (code) { return __awaiter(this, void 0, void 0, function () { var token, url, _params, querystring, o_url, wxloginres; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!code) throw new Error("\u5F53\u524D\u9700\u8981\u4F20\u5165\u5C0F\u7A0B\u5E8F\u7AEF\u4F20\u8FC7\u6765\u7684\u767B\u9646\u7684code"); return [4 /*yield*/, this.getAccessToken()]; case 1: token = _a.sent(); url = "".concat(qqmini_url, "/sns/jscode2session"); _params = { js_code: code, secret: this.appSecret, appid: this.appid, grant_type: 'authorization_code' }; querystring = (0, util_1.querystringfun)(_params); o_url = url + "?".concat(querystring); return [4 /*yield*/, this.getRequest(o_url, { access_token: token })]; case 2: wxloginres = _a.sent(); return [2 /*return*/, wxloginres]; } }); }); }; // -----------------小程序码和小程序链接 start------------------- /** * 获取小程序码 * 文档地址:https://q.qq.com/wiki/develop/game/server/open-port/qr-code.html#%E8%AF%B7%E6%B1%82%E5%9C%B0%E5%9D%80 * @param code 小程序端login生成的code * @returns */ qqmini.prototype.getUnlimitedQRCode = function (path) { return __awaiter(this, void 0, void 0, function () { var token, url, qq_qrcode; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getAccessToken()]; case 1: token = _a.sent(); url = "".concat(qqmini_url, "/api/json/qqa/CreateMiniCode?access_token=").concat(token); return [4 /*yield*/, this.postRequest(url, { appid: this.appid, access_token: token, path: path, }, { access_token: token })]; case 2: qq_qrcode = _a.sent(); return [2 /*return*/, qq_qrcode]; } }); }); }; return qqmini; }(base_1.Base)); exports.qqmini = qqmini;