woodenfish-bot 2.8.1 → 2.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/es/index.js +27 -23
  2. package/lib/index.js +27 -23
  3. package/package.json +1 -1
package/es/index.js CHANGED
@@ -716,7 +716,7 @@ try {
716
716
 
717
717
  var _regeneratorRuntime = /*@__PURE__*/getDefaultExportFromCjs(regenerator);
718
718
 
719
- var version = "2.8.1";
719
+ var version = "2.8.3";
720
720
 
721
721
  var loglevel = {exports: {}};
722
722
 
@@ -1106,7 +1106,7 @@ var addUserAgent = function addUserAgent(header, appID, apiVersion) {
1106
1106
  header['X-Union-Appid'] = "".concat(appID);
1107
1107
  }
1108
1108
  };
1109
- function getAccessToken(file_token, url, data, appID, token) {
1109
+ function getAccessToken(access_token_file, url, data, appID) {
1110
1110
  var now_time = getTimeStampNumber();
1111
1111
  var requestData = JSON.stringify(data);
1112
1112
  var options = {
@@ -1129,13 +1129,21 @@ function getAccessToken(file_token, url, data, appID, token) {
1129
1129
  console.log('response_access_token', JSON.stringify(parsedData));
1130
1130
  var access_token = parsedData.access_token;
1131
1131
  var expires_in = now_time + parseInt(parsedData.expires_in);
1132
+ var access_token_json = JSON.parse(fs.readFileSync(access_token_file).toString());
1133
+ // 确保appID在token['bot']中存在
1134
+ if (!access_token_json['bot']) {
1135
+ access_token_json['bot'] = {};
1136
+ }
1137
+ if (!access_token_json['bot'][appID]) {
1138
+ access_token_json['bot'][appID] = {};
1139
+ }
1132
1140
  // 修改access_token字段的值
1133
- token['bot'][appID]['access_token'] = access_token;
1134
- token['bot'][appID]['expires_in'] = expires_in.toString();
1141
+ access_token_json['bot'][appID]['access_token'] = access_token;
1142
+ access_token_json['bot'][appID]['expires_in'] = expires_in.toString();
1135
1143
  // 将修改后的配置写入json文件,保留原有格式
1136
- var token_new = JSON.stringify(token, null, 2);
1144
+ var token_new = JSON.stringify(access_token_json, null, 2);
1137
1145
  // 写入配置文件,utf-8格式,如果文件不存在会自动创建
1138
- fs.writeFileSync(file_token, token_new, 'utf-8');
1146
+ fs.writeFileSync(access_token_file, token_new, 'utf-8');
1139
1147
  console.log('配置已成功写入到access_token.json文件中。');
1140
1148
  resolve(access_token);
1141
1149
  } catch (error) {
@@ -1157,7 +1165,7 @@ function getAccessToken(file_token, url, data, appID, token) {
1157
1165
  // 添加 User-Agent
1158
1166
  var addAuthorization = /*#__PURE__*/function () {
1159
1167
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(header, appID, token, clientSecret, apiVersion) {
1160
- var now_time, url, data, file_token, _token, access_token, expires_in;
1168
+ var now_time, url, data, access_token_file, default_token_json, access_token, expires_in, access_token_json;
1161
1169
  return _regeneratorRuntime.wrap(function _callee$(_context) {
1162
1170
  while (1) switch (_context.prev = _context.next) {
1163
1171
  case 0:
@@ -1171,31 +1179,27 @@ var addAuthorization = /*#__PURE__*/function () {
1171
1179
  appId: appID,
1172
1180
  clientSecret: clientSecret
1173
1181
  };
1174
- file_token = './access_token.json';
1175
- _token = {
1176
- "bot": _defineProperty({}, appID, {
1177
- "access_token": "",
1178
- "expires_in": ""
1182
+ access_token_file = './access_token.json';
1183
+ default_token_json = {
1184
+ bot: _defineProperty({}, appID, {
1185
+ access_token: "",
1186
+ expires_in: ""
1179
1187
  })
1180
1188
  };
1181
1189
  access_token = '';
1182
1190
  expires_in = 0;
1183
1191
  try {
1184
1192
  // 判断文件是否存在
1185
- if (fs.existsSync(file_token)) {
1193
+ if (fs.existsSync(access_token_file)) {
1186
1194
  // 执行文件存在时的处理逻辑
1187
- _token = JSON.parse(fs.readFileSync(file_token).toString());
1188
- // 判断是否存在appID
1189
- if (_token.hasOwnProperty('bot') && _token['bot'].hasOwnProperty(appID)) {
1190
- access_token = _token['bot'][appID]['access_token'];
1191
- expires_in = parseInt(_token['bot'][appID]['expires_in']);
1192
- } else {
1193
- // 不存在appID时的处理逻辑
1194
- fs.writeFileSync(file_token, JSON.stringify(_token, null, 2), 'utf-8');
1195
+ access_token_json = JSON.parse(fs.readFileSync(access_token_file).toString()); // 判断是否存在appID
1196
+ if (access_token_json.hasOwnProperty('bot') && access_token_json['bot'].hasOwnProperty(appID)) {
1197
+ access_token = access_token_json['bot'][appID]['access_token'];
1198
+ expires_in = parseInt(access_token_json['bot'][appID]['expires_in']);
1195
1199
  }
1196
1200
  } else {
1197
1201
  // 执行文件不存在时的处理逻辑
1198
- fs.writeFileSync(file_token, JSON.stringify(_token, null, 2), 'utf-8');
1202
+ fs.writeFileSync(access_token_file, JSON.stringify(default_token_json, null, 2), 'utf-8');
1199
1203
  }
1200
1204
  } catch (err) {
1201
1205
  console.error('读取文件时发生错误:', err);
@@ -1207,7 +1211,7 @@ var addAuthorization = /*#__PURE__*/function () {
1207
1211
  }
1208
1212
  console.log('access_token', access_token, 'expire_time', expires_in);
1209
1213
  _context.next = 13;
1210
- return getAccessToken(file_token, url, data, appID, _token);
1214
+ return getAccessToken(access_token_file, url, data, appID);
1211
1215
  case 13:
1212
1216
  access_token = _context.sent;
1213
1217
  case 14:
package/lib/index.js CHANGED
@@ -718,7 +718,7 @@ try {
718
718
 
719
719
  var _regeneratorRuntime = /*@__PURE__*/getDefaultExportFromCjs(regenerator);
720
720
 
721
- var version = "2.8.1";
721
+ var version = "2.8.3";
722
722
 
723
723
  var loglevel = {exports: {}};
724
724
 
@@ -1108,7 +1108,7 @@ var addUserAgent = function addUserAgent(header, appID, apiVersion) {
1108
1108
  header['X-Union-Appid'] = "".concat(appID);
1109
1109
  }
1110
1110
  };
1111
- function getAccessToken(file_token, url, data, appID, token) {
1111
+ function getAccessToken(access_token_file, url, data, appID) {
1112
1112
  var now_time = getTimeStampNumber();
1113
1113
  var requestData = JSON.stringify(data);
1114
1114
  var options = {
@@ -1131,13 +1131,21 @@ function getAccessToken(file_token, url, data, appID, token) {
1131
1131
  console.log('response_access_token', JSON.stringify(parsedData));
1132
1132
  var access_token = parsedData.access_token;
1133
1133
  var expires_in = now_time + parseInt(parsedData.expires_in);
1134
+ var access_token_json = JSON.parse(fs.readFileSync(access_token_file).toString());
1135
+ // 确保appID在token['bot']中存在
1136
+ if (!access_token_json['bot']) {
1137
+ access_token_json['bot'] = {};
1138
+ }
1139
+ if (!access_token_json['bot'][appID]) {
1140
+ access_token_json['bot'][appID] = {};
1141
+ }
1134
1142
  // 修改access_token字段的值
1135
- token['bot'][appID]['access_token'] = access_token;
1136
- token['bot'][appID]['expires_in'] = expires_in.toString();
1143
+ access_token_json['bot'][appID]['access_token'] = access_token;
1144
+ access_token_json['bot'][appID]['expires_in'] = expires_in.toString();
1137
1145
  // 将修改后的配置写入json文件,保留原有格式
1138
- var token_new = JSON.stringify(token, null, 2);
1146
+ var token_new = JSON.stringify(access_token_json, null, 2);
1139
1147
  // 写入配置文件,utf-8格式,如果文件不存在会自动创建
1140
- fs.writeFileSync(file_token, token_new, 'utf-8');
1148
+ fs.writeFileSync(access_token_file, token_new, 'utf-8');
1141
1149
  console.log('配置已成功写入到access_token.json文件中。');
1142
1150
  resolve(access_token);
1143
1151
  } catch (error) {
@@ -1159,7 +1167,7 @@ function getAccessToken(file_token, url, data, appID, token) {
1159
1167
  // 添加 User-Agent
1160
1168
  var addAuthorization = /*#__PURE__*/function () {
1161
1169
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(header, appID, token, clientSecret, apiVersion) {
1162
- var now_time, url, data, file_token, _token, access_token, expires_in;
1170
+ var now_time, url, data, access_token_file, default_token_json, access_token, expires_in, access_token_json;
1163
1171
  return _regeneratorRuntime.wrap(function _callee$(_context) {
1164
1172
  while (1) switch (_context.prev = _context.next) {
1165
1173
  case 0:
@@ -1173,31 +1181,27 @@ var addAuthorization = /*#__PURE__*/function () {
1173
1181
  appId: appID,
1174
1182
  clientSecret: clientSecret
1175
1183
  };
1176
- file_token = './access_token.json';
1177
- _token = {
1178
- "bot": _defineProperty({}, appID, {
1179
- "access_token": "",
1180
- "expires_in": ""
1184
+ access_token_file = './access_token.json';
1185
+ default_token_json = {
1186
+ bot: _defineProperty({}, appID, {
1187
+ access_token: "",
1188
+ expires_in: ""
1181
1189
  })
1182
1190
  };
1183
1191
  access_token = '';
1184
1192
  expires_in = 0;
1185
1193
  try {
1186
1194
  // 判断文件是否存在
1187
- if (fs.existsSync(file_token)) {
1195
+ if (fs.existsSync(access_token_file)) {
1188
1196
  // 执行文件存在时的处理逻辑
1189
- _token = JSON.parse(fs.readFileSync(file_token).toString());
1190
- // 判断是否存在appID
1191
- if (_token.hasOwnProperty('bot') && _token['bot'].hasOwnProperty(appID)) {
1192
- access_token = _token['bot'][appID]['access_token'];
1193
- expires_in = parseInt(_token['bot'][appID]['expires_in']);
1194
- } else {
1195
- // 不存在appID时的处理逻辑
1196
- fs.writeFileSync(file_token, JSON.stringify(_token, null, 2), 'utf-8');
1197
+ access_token_json = JSON.parse(fs.readFileSync(access_token_file).toString()); // 判断是否存在appID
1198
+ if (access_token_json.hasOwnProperty('bot') && access_token_json['bot'].hasOwnProperty(appID)) {
1199
+ access_token = access_token_json['bot'][appID]['access_token'];
1200
+ expires_in = parseInt(access_token_json['bot'][appID]['expires_in']);
1197
1201
  }
1198
1202
  } else {
1199
1203
  // 执行文件不存在时的处理逻辑
1200
- fs.writeFileSync(file_token, JSON.stringify(_token, null, 2), 'utf-8');
1204
+ fs.writeFileSync(access_token_file, JSON.stringify(default_token_json, null, 2), 'utf-8');
1201
1205
  }
1202
1206
  } catch (err) {
1203
1207
  console.error('读取文件时发生错误:', err);
@@ -1209,7 +1213,7 @@ var addAuthorization = /*#__PURE__*/function () {
1209
1213
  }
1210
1214
  console.log('access_token', access_token, 'expire_time', expires_in);
1211
1215
  _context.next = 13;
1212
- return getAccessToken(file_token, url, data, appID, _token);
1216
+ return getAccessToken(access_token_file, url, data, appID);
1213
1217
  case 13:
1214
1218
  access_token = _context.sent;
1215
1219
  case 14:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "woodenfish-bot",
3
- "version": "2.8.1",
3
+ "version": "2.8.3",
4
4
  "description": "woodenfish-bot",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"