// version: 2.0.0_201807240950
(function(e, a) { for(var i in a) e[i] = a[i]; }(window, /******/ (function(modules) { // webpackBootstrap
/******/ 	// The module cache
/******/ 	var installedModules = {};
/******/
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/
/******/ 		// Check if module is in cache
/******/ 		if(installedModules[moduleId]) {
/******/ 			return installedModules[moduleId].exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = installedModules[moduleId] = {
/******/ 			i: moduleId,
/******/ 			l: false,
/******/ 			exports: {}
/******/ 		};
/******/
/******/ 		// Execute the module function
/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ 		// Flag the module as loaded
/******/ 		module.l = true;
/******/
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/
/******/
/******/ 	// expose the modules object (__webpack_modules__)
/******/ 	__webpack_require__.m = modules;
/******/
/******/ 	// expose the module cache
/******/ 	__webpack_require__.c = installedModules;
/******/
/******/ 	// define getter function for harmony exports
/******/ 	__webpack_require__.d = function(exports, name, getter) {
/******/ 		if(!__webpack_require__.o(exports, name)) {
/******/ 			Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ 		}
/******/ 	};
/******/
/******/ 	// define __esModule on exports
/******/ 	__webpack_require__.r = function(exports) {
/******/ 		if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 			Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 		}
/******/ 		Object.defineProperty(exports, '__esModule', { value: true });
/******/ 	};
/******/
/******/ 	// create a fake namespace object
/******/ 	// mode & 1: value is a module id, require it
/******/ 	// mode & 2: merge all properties of value into the ns
/******/ 	// mode & 4: return value when already ns object
/******/ 	// mode & 8|1: behave like require
/******/ 	__webpack_require__.t = function(value, mode) {
/******/ 		if(mode & 1) value = __webpack_require__(value);
/******/ 		if(mode & 8) return value;
/******/ 		if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ 		var ns = Object.create(null);
/******/ 		__webpack_require__.r(ns);
/******/ 		Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ 		if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ 		return ns;
/******/ 	};
/******/
/******/ 	// getDefaultExport function for compatibility with non-harmony modules
/******/ 	__webpack_require__.n = function(module) {
/******/ 		var getter = module && module.__esModule ?
/******/ 			function getDefault() { return module['default']; } :
/******/ 			function getModuleExports() { return module; };
/******/ 		__webpack_require__.d(getter, 'a', getter);
/******/ 		return getter;
/******/ 	};
/******/
/******/ 	// Object.prototype.hasOwnProperty.call
/******/ 	__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "";
/******/
/******/
/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(__webpack_require__.s = "./src/index.js");
/******/ })
/************************************************************************/
/******/ ({

/***/ "./src/config/development.js":
/*!***********************************!*\
  !*** ./src/config/development.js ***!
  \***********************************/
/*! exports provided: default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony default export */ __webpack_exports__["default"] = ({
    svr: 'https://test.ngo.minigame.qq.com'
});

/***/ }),

/***/ "./src/config/index.js":
/*!*****************************!*\
  !*** ./src/config/index.js ***!
  \*****************************/
/*! exports provided: default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _development__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./development */ "./src/config/development.js");
/* harmony import */ var _production__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./production */ "./src/config/production.js");



/* harmony default export */ __webpack_exports__["default"] = ({
    dev: _development__WEBPACK_IMPORTED_MODULE_0__["default"],
    prod: _production__WEBPACK_IMPORTED_MODULE_1__["default"]
});

/***/ }),

/***/ "./src/config/production.js":
/*!**********************************!*\
  !*** ./src/config/production.js ***!
  \**********************************/
/*! exports provided: default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony default export */ __webpack_exports__["default"] = ({
    svr: 'https://goplatform.minigame.qq.com'
});

/***/ }),

/***/ "./src/index.js":
/*!**********************!*\
  !*** ./src/index.js ***!
  \**********************/
/*! exports provided: Recommender */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "Recommender", function() { return Recommender; });
/* harmony import */ var _platform_wx__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./platform/wx */ "./src/platform/wx.js");
/* harmony import */ var _logger__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./logger */ "./src/logger/index.js");
/* harmony import */ var _report__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./report */ "./src/report/index.js");
/* harmony import */ var _config__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./config */ "./src/config/index.js");





const ACTION_CLICK = 101,
      ACTION_EXPOSED = 100;

var Recommender = function ({ appid, openid, debug = false, env = 'prod' }) {
    _logger__WEBPACK_IMPORTED_MODULE_1__["default"].setAble(debug);
    if (!appid) {
        console.error('请传入当前游戏的appid');
        return;
    }
    if (!openid) {
        console.warn('请传入当前玩家的openid');
    }
    this.appid = appid;
    this.openid = openid;
    this.config = _config__WEBPACK_IMPORTED_MODULE_3__["default"][env];
    this.index = 0;
    this.reporter = new _report__WEBPACK_IMPORTED_MODULE_2__["default"]({
        appid: this.appid,
        openid: this.openid,
        svr: this.config.svr
    });

    this.games = [];
    this.getConfig().then(res => {
        if (res.result == 0) {
            let config = res.data;
            for (let i = 0; i < config.Appids.length; ++i) {
                this.games.push({
                    appid: config.Appids[i],
                    icon: config.Icons[i]
                });
            }
            this.report(this.games[0], 0, ACTION_EXPOSED);
            _logger__WEBPACK_IMPORTED_MODULE_1__["default"].log('当前插件开关：', config.IsOpen ? '开启' : '关闭');
            if (this.readyHandler) {
                this.readyHandler(config.IsOpen, this.games[this.index]);
            }
        } else {
            _logger__WEBPACK_IMPORTED_MODULE_1__["default"].log('后台错误，插件开关关闭');
            if (this.readyHandler) {
                this.readyHandler(false, null);
            }
        }
    }).catch(e => {
        _logger__WEBPACK_IMPORTED_MODULE_1__["default"].log('网络错误，插件开关关闭：', e);
        if (this.readyHandler) {
            this.readyHandler(false, null);
        }
    });

    this.changeHandler = null;
};

Recommender.prototype = {
    getCurrentGame: function () {
        return this.games[this.index];
    },
    jumpToCurrentGame() {
        var game = this.games[this.index];
        this.report(game, this.index, ACTION_CLICK);
        if (wx.navigateToMiniProgram) {
            wx.navigateToMiniProgram({
                appId: game.appid,
                success: function () {
                    _logger__WEBPACK_IMPORTED_MODULE_1__["default"].log('游戏跳转成功，appid: ' + game.appid);
                },
                fail: function () {
                    _logger__WEBPACK_IMPORTED_MODULE_1__["default"].warn('游戏跳转失败，appid：' + game.appid);
                }
            });
        } else {
            _logger__WEBPACK_IMPORTED_MODULE_1__["default"].warn('当前小游戏库版本不支持跳转功能，请升级基础库到2.2.0以上');
        }
        this.index = (this.index + 1) % this.games.length;
        this.report(this.games[this.index], this.index, ACTION_EXPOSED);
        if (this.changeHandler && typeof this.changeHandler === 'function') {
            this.changeHandler(this.games[this.index]);
        }
    },
    onChange(cb) {
        if (typeof cb !== 'function') {
            _logger__WEBPACK_IMPORTED_MODULE_1__["default"].warn('请传入回调函数');
            return;
        }
        this.changeHandler = cb;
    },
    onReady(cb) {
        if (typeof cb !== 'function') {
            _logger__WEBPACK_IMPORTED_MODULE_1__["default"].warn('请传入回调函数');
            return;
        }
        this.readyHandler = cb;
    },
    getConfig() {
        return new Promise((resolve, reject) => {
            _platform_wx__WEBPACK_IMPORTED_MODULE_0__["platform"].request({
                url: `${this.config.svr}/mgplugin/getconfig?appid=${this.appid}`,
                method: 'GET',
                success: res => {
                    resolve(res);
                },
                fail: () => {
                    reject();
                }
            });
        });
    },
    report(game, index, action) {
        this.reporter.report({
            iSceneLevel1: 1000001,
            iSceneLevel2: 1,
            iSceneLevel3: index + 1,
            iActionId: action,
            iResourceId1: game.appid
        });
    }
};



/***/ }),

/***/ "./src/logger/index.js":
/*!*****************************!*\
  !*** ./src/logger/index.js ***!
  \*****************************/
/*! exports provided: default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
const TAG = '[recommender]';

/* harmony default export */ __webpack_exports__["default"] = ({
    isAble: false,
    setAble(able) {
        this.isAble = able;
    },
    log: function (message, ...optionalParams) {
        if (this.isAble) {
            console.log('', TAG, message, ...optionalParams);
        }
    },
    error: function (message, ...optionalParams) {
        if (this.isAble) {
            console.error('', TAG, message, ...optionalParams);
        }
    },
    warn: function (message, ...optionalParams) {
        if (this.isAble) {
            console.warn('', TAG, message, ...optionalParams);
        }
    }
});

/***/ }),

/***/ "./src/platform/wx.js":
/*!****************************!*\
  !*** ./src/platform/wx.js ***!
  \****************************/
/*! exports provided: platform */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "platform", function() { return platform; });
/* harmony import */ var _logger__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../logger */ "./src/logger/index.js");


class Platform {
    request({ url, data, method, success, fail }) {
        _logger__WEBPACK_IMPORTED_MODULE_0__["default"].log('开始请求：', url, '数据：', data || '');
        if (typeof wx === 'undefined') {
            let xhr = new XMLHttpRequest();
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4) {
                    if (xhr.status >= 200 && xhr.status < 400) {
                        let res = xhr.responseText;
                        try {
                            res = JSON.parse(res);
                            _logger__WEBPACK_IMPORTED_MODULE_0__["default"].log('请求成功：', url, '回包：', res);
                        } catch (e) {
                            _logger__WEBPACK_IMPORTED_MODULE_0__["default"].warn('请求失败: ', url);
                            fail(e);
                        }
                        if (success) {
                            success(res);
                        }
                    } else {
                        if (fail) {
                            fail('server error: ' + xhr.status);
                        }
                    }
                }
            };
            xhr.open(method, url, true);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.send(data);
        } else {
            wx.request({
                url: url,
                data: data || {},
                header: {
                    'Content-Type': 'application/json'
                },
                method: method,
                success: function (res) {
                    _logger__WEBPACK_IMPORTED_MODULE_0__["default"].log('请求成功：', url, '回包：', res);
                    success(res.data);
                },
                fail: function (res) {
                    _logger__WEBPACK_IMPORTED_MODULE_0__["default"].warn('请求失败: ', url);
                    fail(res);
                }
            });
        }
    }
}

const platform = new Platform();


/***/ }),

/***/ "./src/report/index.js":
/*!*****************************!*\
  !*** ./src/report/index.js ***!
  \*****************************/
/*! exports provided: default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _platform_wx__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../platform/wx */ "./src/platform/wx.js");
/* harmony import */ var _logger__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../logger */ "./src/logger/index.js");



const DATA_TEMPLATE = {
    dtLoginTime: -1,
    vQimei: -1,
    vAppVer: -1,
    PlatID: -1,
    vOsType: -1,
    osversion: -1,
    nettype: -1,
    model: -1,
    iActionId: -1,
    iSceneLevel1: -1,
    iSceneLevel2: -1,
    iSceneLevel3: -1,
    iResourceId1: -1,
    iResourceId2: -1,
    iResourceId3: -1,
    iResourceId4: -1,
    iResourceId5: -1,
    iRet: -1,
    vV1: -1,
    vV2: -1,
    vV3: -1,
    vV4: -1,
    vV5: -1,
    vV6: -1,
    vV7: -1,
    vV8: -1,
    vV9: -1,
    vV10: -1
};

/* harmony default export */ __webpack_exports__["default"] = (class {
    constructor({ appid, openid, svr }) {
        this.appid = appid;
        this.openid = openid;
        this.svr = svr;
        this.dataTemplate = Object.assign({}, DATA_TEMPLATE, {
            iResourceId2: this.appid
        });
    }

    report(data) {
        return new Promise((resolve, reject) => {
            data = Object.assign({}, this.dataTemplate, data);
            _logger__WEBPACK_IMPORTED_MODULE_1__["default"].log('开始上报：', data);
            let reportData = {
                data: [this.genDataString(data)],
                type: '0',
                openid: this.openid,
                appid: this.appid,
                openid: this.openid
            };
            _platform_wx__WEBPACK_IMPORTED_MODULE_0__["platform"].request({
                url: `${this.svr}/mpreport/reportplugintlog`,
                data: reportData,
                method: 'POST',
                success: () => {
                    _logger__WEBPACK_IMPORTED_MODULE_1__["default"].log('上报成功：', data);
                    resolve();
                },
                fail: () => {
                    _logger__WEBPACK_IMPORTED_MODULE_1__["default"].warn('上报失败：', data);
                }
            });
        });
    }

    genDataString(data) {
        let arr = [];
        Object.keys(data).forEach(key => arr.push(data[key]));
        return arr.join('|');
    }
});

/***/ })

/******/ })));
//# sourceMappingURL=recommender.js.map