recommender.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. // version: 2.0.0_201807240950
  2. (function(e, a) { for(var i in a) e[i] = a[i]; }(window, /******/ (function(modules) { // webpackBootstrap
  3. /******/ // The module cache
  4. /******/ var installedModules = {};
  5. /******/
  6. /******/ // The require function
  7. /******/ function __webpack_require__(moduleId) {
  8. /******/
  9. /******/ // Check if module is in cache
  10. /******/ if(installedModules[moduleId]) {
  11. /******/ return installedModules[moduleId].exports;
  12. /******/ }
  13. /******/ // Create a new module (and put it into the cache)
  14. /******/ var module = installedModules[moduleId] = {
  15. /******/ i: moduleId,
  16. /******/ l: false,
  17. /******/ exports: {}
  18. /******/ };
  19. /******/
  20. /******/ // Execute the module function
  21. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  22. /******/
  23. /******/ // Flag the module as loaded
  24. /******/ module.l = true;
  25. /******/
  26. /******/ // Return the exports of the module
  27. /******/ return module.exports;
  28. /******/ }
  29. /******/
  30. /******/
  31. /******/ // expose the modules object (__webpack_modules__)
  32. /******/ __webpack_require__.m = modules;
  33. /******/
  34. /******/ // expose the module cache
  35. /******/ __webpack_require__.c = installedModules;
  36. /******/
  37. /******/ // define getter function for harmony exports
  38. /******/ __webpack_require__.d = function(exports, name, getter) {
  39. /******/ if(!__webpack_require__.o(exports, name)) {
  40. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  41. /******/ }
  42. /******/ };
  43. /******/
  44. /******/ // define __esModule on exports
  45. /******/ __webpack_require__.r = function(exports) {
  46. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  47. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  48. /******/ }
  49. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  50. /******/ };
  51. /******/
  52. /******/ // create a fake namespace object
  53. /******/ // mode & 1: value is a module id, require it
  54. /******/ // mode & 2: merge all properties of value into the ns
  55. /******/ // mode & 4: return value when already ns object
  56. /******/ // mode & 8|1: behave like require
  57. /******/ __webpack_require__.t = function(value, mode) {
  58. /******/ if(mode & 1) value = __webpack_require__(value);
  59. /******/ if(mode & 8) return value;
  60. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  61. /******/ var ns = Object.create(null);
  62. /******/ __webpack_require__.r(ns);
  63. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  64. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  65. /******/ return ns;
  66. /******/ };
  67. /******/
  68. /******/ // getDefaultExport function for compatibility with non-harmony modules
  69. /******/ __webpack_require__.n = function(module) {
  70. /******/ var getter = module && module.__esModule ?
  71. /******/ function getDefault() { return module['default']; } :
  72. /******/ function getModuleExports() { return module; };
  73. /******/ __webpack_require__.d(getter, 'a', getter);
  74. /******/ return getter;
  75. /******/ };
  76. /******/
  77. /******/ // Object.prototype.hasOwnProperty.call
  78. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  79. /******/
  80. /******/ // __webpack_public_path__
  81. /******/ __webpack_require__.p = "";
  82. /******/
  83. /******/
  84. /******/ // Load entry module and return exports
  85. /******/ return __webpack_require__(__webpack_require__.s = "./src/index.js");
  86. /******/ })
  87. /************************************************************************/
  88. /******/ ({
  89. /***/ "./src/config/development.js":
  90. /*!***********************************!*\
  91. !*** ./src/config/development.js ***!
  92. \***********************************/
  93. /*! exports provided: default */
  94. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  95. "use strict";
  96. __webpack_require__.r(__webpack_exports__);
  97. /* harmony default export */ __webpack_exports__["default"] = ({
  98. svr: 'https://test.ngo.minigame.qq.com'
  99. });
  100. /***/ }),
  101. /***/ "./src/config/index.js":
  102. /*!*****************************!*\
  103. !*** ./src/config/index.js ***!
  104. \*****************************/
  105. /*! exports provided: default */
  106. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  107. "use strict";
  108. __webpack_require__.r(__webpack_exports__);
  109. /* harmony import */ var _development__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./development */ "./src/config/development.js");
  110. /* harmony import */ var _production__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./production */ "./src/config/production.js");
  111. /* harmony default export */ __webpack_exports__["default"] = ({
  112. dev: _development__WEBPACK_IMPORTED_MODULE_0__["default"],
  113. prod: _production__WEBPACK_IMPORTED_MODULE_1__["default"]
  114. });
  115. /***/ }),
  116. /***/ "./src/config/production.js":
  117. /*!**********************************!*\
  118. !*** ./src/config/production.js ***!
  119. \**********************************/
  120. /*! exports provided: default */
  121. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  122. "use strict";
  123. __webpack_require__.r(__webpack_exports__);
  124. /* harmony default export */ __webpack_exports__["default"] = ({
  125. svr: 'https://goplatform.minigame.qq.com'
  126. });
  127. /***/ }),
  128. /***/ "./src/index.js":
  129. /*!**********************!*\
  130. !*** ./src/index.js ***!
  131. \**********************/
  132. /*! exports provided: Recommender */
  133. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  134. "use strict";
  135. __webpack_require__.r(__webpack_exports__);
  136. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "Recommender", function() { return Recommender; });
  137. /* harmony import */ var _platform_wx__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./platform/wx */ "./src/platform/wx.js");
  138. /* harmony import */ var _logger__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./logger */ "./src/logger/index.js");
  139. /* harmony import */ var _report__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./report */ "./src/report/index.js");
  140. /* harmony import */ var _config__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./config */ "./src/config/index.js");
  141. const ACTION_CLICK = 101,
  142. ACTION_EXPOSED = 100;
  143. var Recommender = function ({ appid, openid, debug = false, env = 'prod' }) {
  144. _logger__WEBPACK_IMPORTED_MODULE_1__["default"].setAble(debug);
  145. if (!appid) {
  146. console.error('请传入当前游戏的appid');
  147. return;
  148. }
  149. if (!openid) {
  150. console.warn('请传入当前玩家的openid');
  151. }
  152. this.appid = appid;
  153. this.openid = openid;
  154. this.config = _config__WEBPACK_IMPORTED_MODULE_3__["default"][env];
  155. this.index = 0;
  156. this.reporter = new _report__WEBPACK_IMPORTED_MODULE_2__["default"]({
  157. appid: this.appid,
  158. openid: this.openid,
  159. svr: this.config.svr
  160. });
  161. this.games = [];
  162. this.getConfig().then(res => {
  163. if (res.result == 0) {
  164. let config = res.data;
  165. for (let i = 0; i < config.Appids.length; ++i) {
  166. this.games.push({
  167. appid: config.Appids[i],
  168. icon: config.Icons[i]
  169. });
  170. }
  171. this.report(this.games[0], 0, ACTION_EXPOSED);
  172. _logger__WEBPACK_IMPORTED_MODULE_1__["default"].log('当前插件开关:', config.IsOpen ? '开启' : '关闭');
  173. if (this.readyHandler) {
  174. this.readyHandler(config.IsOpen, this.games[this.index]);
  175. }
  176. } else {
  177. _logger__WEBPACK_IMPORTED_MODULE_1__["default"].log('后台错误,插件开关关闭');
  178. if (this.readyHandler) {
  179. this.readyHandler(false, null);
  180. }
  181. }
  182. }).catch(e => {
  183. _logger__WEBPACK_IMPORTED_MODULE_1__["default"].log('网络错误,插件开关关闭:', e);
  184. if (this.readyHandler) {
  185. this.readyHandler(false, null);
  186. }
  187. });
  188. this.changeHandler = null;
  189. };
  190. Recommender.prototype = {
  191. getCurrentGame: function () {
  192. return this.games[this.index];
  193. },
  194. jumpToCurrentGame() {
  195. var game = this.games[this.index];
  196. this.report(game, this.index, ACTION_CLICK);
  197. if (wx.navigateToMiniProgram) {
  198. wx.navigateToMiniProgram({
  199. appId: game.appid,
  200. success: function () {
  201. _logger__WEBPACK_IMPORTED_MODULE_1__["default"].log('游戏跳转成功,appid: ' + game.appid);
  202. },
  203. fail: function () {
  204. _logger__WEBPACK_IMPORTED_MODULE_1__["default"].warn('游戏跳转失败,appid:' + game.appid);
  205. }
  206. });
  207. } else {
  208. _logger__WEBPACK_IMPORTED_MODULE_1__["default"].warn('当前小游戏库版本不支持跳转功能,请升级基础库到2.2.0以上');
  209. }
  210. this.index = (this.index + 1) % this.games.length;
  211. this.report(this.games[this.index], this.index, ACTION_EXPOSED);
  212. if (this.changeHandler && typeof this.changeHandler === 'function') {
  213. this.changeHandler(this.games[this.index]);
  214. }
  215. },
  216. onChange(cb) {
  217. if (typeof cb !== 'function') {
  218. _logger__WEBPACK_IMPORTED_MODULE_1__["default"].warn('请传入回调函数');
  219. return;
  220. }
  221. this.changeHandler = cb;
  222. },
  223. onReady(cb) {
  224. if (typeof cb !== 'function') {
  225. _logger__WEBPACK_IMPORTED_MODULE_1__["default"].warn('请传入回调函数');
  226. return;
  227. }
  228. this.readyHandler = cb;
  229. },
  230. getConfig() {
  231. return new Promise((resolve, reject) => {
  232. _platform_wx__WEBPACK_IMPORTED_MODULE_0__["platform"].request({
  233. url: `${this.config.svr}/mgplugin/getconfig?appid=${this.appid}`,
  234. method: 'GET',
  235. success: res => {
  236. resolve(res);
  237. },
  238. fail: () => {
  239. reject();
  240. }
  241. });
  242. });
  243. },
  244. report(game, index, action) {
  245. this.reporter.report({
  246. iSceneLevel1: 1000001,
  247. iSceneLevel2: 1,
  248. iSceneLevel3: index + 1,
  249. iActionId: action,
  250. iResourceId1: game.appid
  251. });
  252. }
  253. };
  254. /***/ }),
  255. /***/ "./src/logger/index.js":
  256. /*!*****************************!*\
  257. !*** ./src/logger/index.js ***!
  258. \*****************************/
  259. /*! exports provided: default */
  260. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  261. "use strict";
  262. __webpack_require__.r(__webpack_exports__);
  263. const TAG = '[recommender]';
  264. /* harmony default export */ __webpack_exports__["default"] = ({
  265. isAble: false,
  266. setAble(able) {
  267. this.isAble = able;
  268. },
  269. log: function (message, ...optionalParams) {
  270. if (this.isAble) {
  271. console.log('', TAG, message, ...optionalParams);
  272. }
  273. },
  274. error: function (message, ...optionalParams) {
  275. if (this.isAble) {
  276. console.error('', TAG, message, ...optionalParams);
  277. }
  278. },
  279. warn: function (message, ...optionalParams) {
  280. if (this.isAble) {
  281. console.warn('', TAG, message, ...optionalParams);
  282. }
  283. }
  284. });
  285. /***/ }),
  286. /***/ "./src/platform/wx.js":
  287. /*!****************************!*\
  288. !*** ./src/platform/wx.js ***!
  289. \****************************/
  290. /*! exports provided: platform */
  291. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  292. "use strict";
  293. __webpack_require__.r(__webpack_exports__);
  294. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "platform", function() { return platform; });
  295. /* harmony import */ var _logger__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../logger */ "./src/logger/index.js");
  296. class Platform {
  297. request({ url, data, method, success, fail }) {
  298. _logger__WEBPACK_IMPORTED_MODULE_0__["default"].log('开始请求:', url, '数据:', data || '');
  299. if (typeof wx === 'undefined') {
  300. let xhr = new XMLHttpRequest();
  301. xhr.onreadystatechange = () => {
  302. if (xhr.readyState == 4) {
  303. if (xhr.status >= 200 && xhr.status < 400) {
  304. let res = xhr.responseText;
  305. try {
  306. res = JSON.parse(res);
  307. _logger__WEBPACK_IMPORTED_MODULE_0__["default"].log('请求成功:', url, '回包:', res);
  308. } catch (e) {
  309. _logger__WEBPACK_IMPORTED_MODULE_0__["default"].warn('请求失败: ', url);
  310. fail(e);
  311. }
  312. if (success) {
  313. success(res);
  314. }
  315. } else {
  316. if (fail) {
  317. fail('server error: ' + xhr.status);
  318. }
  319. }
  320. }
  321. };
  322. xhr.open(method, url, true);
  323. xhr.setRequestHeader('Content-Type', 'application/json');
  324. xhr.send(data);
  325. } else {
  326. wx.request({
  327. url: url,
  328. data: data || {},
  329. header: {
  330. 'Content-Type': 'application/json'
  331. },
  332. method: method,
  333. success: function (res) {
  334. _logger__WEBPACK_IMPORTED_MODULE_0__["default"].log('请求成功:', url, '回包:', res);
  335. success(res.data);
  336. },
  337. fail: function (res) {
  338. _logger__WEBPACK_IMPORTED_MODULE_0__["default"].warn('请求失败: ', url);
  339. fail(res);
  340. }
  341. });
  342. }
  343. }
  344. }
  345. const platform = new Platform();
  346. /***/ }),
  347. /***/ "./src/report/index.js":
  348. /*!*****************************!*\
  349. !*** ./src/report/index.js ***!
  350. \*****************************/
  351. /*! exports provided: default */
  352. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  353. "use strict";
  354. __webpack_require__.r(__webpack_exports__);
  355. /* harmony import */ var _platform_wx__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../platform/wx */ "./src/platform/wx.js");
  356. /* harmony import */ var _logger__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../logger */ "./src/logger/index.js");
  357. const DATA_TEMPLATE = {
  358. dtLoginTime: -1,
  359. vQimei: -1,
  360. vAppVer: -1,
  361. PlatID: -1,
  362. vOsType: -1,
  363. osversion: -1,
  364. nettype: -1,
  365. model: -1,
  366. iActionId: -1,
  367. iSceneLevel1: -1,
  368. iSceneLevel2: -1,
  369. iSceneLevel3: -1,
  370. iResourceId1: -1,
  371. iResourceId2: -1,
  372. iResourceId3: -1,
  373. iResourceId4: -1,
  374. iResourceId5: -1,
  375. iRet: -1,
  376. vV1: -1,
  377. vV2: -1,
  378. vV3: -1,
  379. vV4: -1,
  380. vV5: -1,
  381. vV6: -1,
  382. vV7: -1,
  383. vV8: -1,
  384. vV9: -1,
  385. vV10: -1
  386. };
  387. /* harmony default export */ __webpack_exports__["default"] = (class {
  388. constructor({ appid, openid, svr }) {
  389. this.appid = appid;
  390. this.openid = openid;
  391. this.svr = svr;
  392. this.dataTemplate = Object.assign({}, DATA_TEMPLATE, {
  393. iResourceId2: this.appid
  394. });
  395. }
  396. report(data) {
  397. return new Promise((resolve, reject) => {
  398. data = Object.assign({}, this.dataTemplate, data);
  399. _logger__WEBPACK_IMPORTED_MODULE_1__["default"].log('开始上报:', data);
  400. let reportData = {
  401. data: [this.genDataString(data)],
  402. type: '0',
  403. openid: this.openid,
  404. appid: this.appid,
  405. openid: this.openid
  406. };
  407. _platform_wx__WEBPACK_IMPORTED_MODULE_0__["platform"].request({
  408. url: `${this.svr}/mpreport/reportplugintlog`,
  409. data: reportData,
  410. method: 'POST',
  411. success: () => {
  412. _logger__WEBPACK_IMPORTED_MODULE_1__["default"].log('上报成功:', data);
  413. resolve();
  414. },
  415. fail: () => {
  416. _logger__WEBPACK_IMPORTED_MODULE_1__["default"].warn('上报失败:', data);
  417. }
  418. });
  419. });
  420. }
  421. genDataString(data) {
  422. let arr = [];
  423. Object.keys(data).forEach(key => arr.push(data[key]));
  424. return arr.join('|');
  425. }
  426. });
  427. /***/ })
  428. /******/ })));
  429. //# sourceMappingURL=recommender.js.map