minif_node
Version:
各类平台的对接的一个sdk
282 lines (281 loc) • 14.7 kB
JavaScript
"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.wxmini = void 0;
/***
* 关于小程序微信模块下的api的接入
*/
var base_1 = require("./base");
var AccountApiPool_1 = require("./common/AccountApiPool");
var util_1 = require("./common/util");
var wxminiurl = 'https://api.weixin.qq.com';
var wxmini = /** @class */ (function (_super) {
__extends(wxmini, _super);
/**
* 构造器
* @param obj object类型 包括下面参数
* @param appid 微信小程序的appid
* @param appSecret 微信小程序的appSecret
* @param redis_options redis的配置信
*/
function wxmini(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
*/
wxmini.prototype.getAccessToken = function () {
return __awaiter(this, void 0, void 0, function () {
var token_key, url, AccessToken, redisred, redisredgetna, redisredget, _params, querystring, o_url, gettokenres, redisredsetres, redisredtime, tokenCache, tokenCacheres, _params, querystring, o_url, gettokenres, tokenCacheset;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
token_key = "".concat(this.appid, "-wxmini_AccessToken");
url = "".concat(wxminiurl, "/cgi-bin/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:
_params = {
appid: this.appid,
secret: this.appSecret,
grant_type: 'client_credential'
};
querystring = (0, util_1.querystringfun)(_params);
o_url = url + "?".concat(querystring);
return [4 /*yield*/, this.getRequest(o_url)];
case 4:
gettokenres = _a.sent();
// 说明有问题
if (!gettokenres.access_token) {
throw new Error("".concat(JSON.stringify(gettokenres)));
}
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:
_params = {
appid: this.appid,
secret: this.appSecret,
grant_type: 'client_credential'
};
querystring = (0, util_1.querystringfun)(_params);
o_url = url + "?".concat(querystring);
return [4 /*yield*/, this.getRequest(o_url)];
case 10:
gettokenres = _a.sent();
// 说明有问题
if (!gettokenres.access_token) {
throw new Error("".concat(JSON.stringify(gettokenres)));
}
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];
}
});
});
};
// -----------------小程序登陆 start-------------------
/**
* 微信小程序登陆
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html
* @param code 小程序端login生成的code
* @returns
*/
wxmini.prototype.wxcodelogin = 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(wxminiurl, "/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);
console.log(o_url);
return [4 /*yield*/, this.getRequest(o_url, {
access_token: token
})];
case 2:
wxloginres = _a.sent();
return [2 /*return*/, wxloginres];
}
});
});
};
// -----------------小程序登陆 end----------------------
// -----------------用户信息 start-------------------
/**
* 手机号验证
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-info/phone-number/getPhoneNumber.html
* @param code 小程序端login生成的code
* @returns
*/
wxmini.prototype.getPhoneNumber = function (code) {
return __awaiter(this, void 0, void 0, function () {
var token, url, _params, querystring, o_url, getPhoneNumberres;
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\u9A8C\u8BC1\u624B\u673A\u53F7\u7684code");
return [4 /*yield*/, this.getAccessToken()];
case 1:
token = _a.sent();
url = "".concat(wxminiurl, "/wxa/business/getuserphonenumber");
_params = {
code: code,
access_token: token
};
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: token
})];
case 2:
getPhoneNumberres = _a.sent();
return [2 /*return*/, getPhoneNumberres];
}
});
});
};
// -----------------用户信息 end----------------------
// -----------------小程序码和小程序链接 start-------------------
/**
* 获取不限制的小程序码(未测试) | 生成小程序码
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/qr-code/getUnlimitedQRCode.html
* @param code 小程序端login生成的code
* @returns
*/
wxmini.prototype.getUnlimitedQRCode = function (data) {
return __awaiter(this, void 0, void 0, function () {
var token, url, wxloginres;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!data.scene)
throw new Error("\u5F53\u524D\u6709\u5B57\u6BB5\u662F\u5FC5\u586B\u7684~");
return [4 /*yield*/, this.getAccessToken()];
case 1:
token = _a.sent();
url = "".concat(wxminiurl, "/wxa/getwxacodeunlimit?access_token=").concat(token);
return [4 /*yield*/, this.postRequest(url, __assign({}, data), {
access_token: token
})];
case 2:
wxloginres = _a.sent();
return [2 /*return*/, wxloginres];
}
});
});
};
return wxmini;
}(base_1.Base));
exports.wxmini = wxmini;