function jssdk(wid, domain) {
    this.wid = wid;
    if (typeof domain == "undefined") {
        domain = window.location.origin;
    }
    this.domain = domain;
}

// title,desc,link,imgUrl
jssdk.prototype._shareQQ = function (shareData) {
    wx.onMenuShareQQ(shareData);
};

// title,desc,link,imgUrl
jssdk.prototype._shareWeibo = function (shareData) {
    wx.onMenuShareWeibo(shareData);
};

// title,link,imgUrl
jssdk.prototype._shareTimeline = function (shareData) {
    wx.onMenuShareTimeline(shareData);
};
// title,desc,link,imgUrl
// type 分享类型 music/video/link[default]
// dataUrl type=music/video，则要提供数据链接，默认为空
jssdk.prototype._shareAppMessage = function (shareData) {
    wx.onMenuShareAppMessage(shareData);
};
//title 分享给朋友标题 desc 分享给朋友描述 imgUrl  分享图标 timetitle 分享到朋友圈标题
jssdk.prototype.share = function (title, desc, imgUrl, timetitle, success) {
    if (typeof success != "function") {
        success = function () {
        };
    }
    var link=arguments[5] ? arguments[5] : _sdk_config.getUrl(true);
    _sdk_config.config(this.wid, this.domain);
    var that = this;
    wx.ready(function () {
        that._shareQQ({title: title, desc: desc, link: link, imgUrl: imgUrl, success: success});
        that._shareWeibo({title: title, desc: desc, link: link, imgUrl: imgUrl, success: success});
        that._shareTimeline({title: timetitle, link: link, imgUrl: imgUrl, success: success});
        that._shareAppMessage({title: title, desc: desc, link: link, imgUrl: imgUrl, success: success});
    });
}

jssdk.prototype.config = function (debug) {
    _sdk_config.config(this.wid, this.domain, debug);
}
jssdk.prototype.previewImage = function (data) {
    wx.previewImage(data);
}

jssdk.prototype.qrcode = function (callback) {
    wx.scanQRCode({
        needResult: 1,
        scanType: ["qrCode","barCode"],
        success: function (res) {
            callback(res.resultStr);
        }
    });
};

jssdk.prototype.getLocation = function (callback) {
    wx.ready(function(){
        wx.getLocation({
            type: 'gcj02', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
            success: function (res) {
                callback(res);
            }
        })
    });
}

jssdk.prototype.chooseWXPay = function (option) {
    wx.chooseWXPay(option);
};

// 控制分享接口
jssdk.prototype.controlShareMenu = function(option){
    _sdk_config.config(this.wid, this.domain);
    var that = this;
    wx.ready(function(){
        if (option != 'hideAllNonBaseMenuItem') {
            that._hideMenuItems(option);
        } else {
            wx.hideAllNonBaseMenuItem();    // 隐藏所有
        }
    });
};
// 选择隐藏
jssdk.prototype._hideMenuItems = function(option){
    var option = eval("("+option+")");
    wx.hideMenuItems({
        menuList: option
    });
};

jssdk.prototype.chooseImage = function (option, callback) {
    var def = {
        count: 9,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function(res) {
            callback(res);
        }
    };
    for(var i in option) {
        def[i] = option[i];
    }
    wx.chooseImage(def);
};

jssdk.prototype.uploadImage = function (option, callback) {
    var def = {
        localId: '',
        isShowProgressTips: 1,
        success: function(res) {
            callback(res);
        }
    };
    for(var i in option) {
        def[i] = option[i];
    }
    wx.uploadImage(def);
};

var _sdk_config = {
    _config_state: [],
    _url: "",
    _apiList: [
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'scanQRCode',
        'chooseWXPay',
        'previewImage',
        'openLocation',
        'getLocation',
        'openEnterpriseChat',
        'openEnterpriseContact',
        'hideAllNonBaseMenuItem', // 添加
        'hideMenuItems',
        'chooseImage',
        'uploadImage'
    ],
    addApiList: function (api) {
        this._apiList.push(api);
    },
    config: function (wid, domain, debug, force) {
        if (this._config_state[wid] && !force) {
            return;
        }
        this._config_state[wid] = true;
        var that = this;
        $.ajax({
            type: "get",
            url: domain + '/uc/api/wx-jssdk/config',
            dataType: "jsonp",
            jsonp: "callback",
            data: ({
                wid: wid,
                url: this.getUrl()
            }),
            success: function (data) {
                var configData = {
                    // beta: true,// 必须这么写，否则wx.invoke调用形式的jsapi会有问题
                    debug: debug ? true : false,
                    appId: data.appId,
                    timestamp: data.timestamp,
                    nonceStr: data.nonceStr,
                    signature: data.signature,
                    jsApiList: that._apiList
                };
                wx.config(configData);
            }
        });
    },
    getUrl: function (clear) {
        if (!this._url) {
            var l = window.location;
            this._url = l.protocol + '//' + l.host + l.pathname + l.search;
        }

        if (typeof clear != "undefined") {
            return this.delQueStr(this._url, ['code', 'CODE', 'state', 'STATE']);
        }
        return this._url;
    },
    delQueStr: function (url, strs) {
        var str = "";

        if (url.indexOf('?') != -1)
            str = url.substr(url.indexOf('?') + 1);
        else
            return url;
        for (var i = 0; i < strs.length; i++) {
            var returnurl = "";
            var ref = strs[i];
            if (str.indexOf('&') != -1) {
                var arr = str.split('&');
                for (var ii in arr) {
                    var nf = arr[ii].split('=');
                    if (nf[0] != ref) {
                        returnurl += arr[ii] + "&";
                    }
                }
                url = url.substr(0, url.indexOf('?')) + "?" + returnurl.substr(0, returnurl.length - 1);
            } else {
                var arr = str.split('=');
                if (arr[0] == ref)
                    url = url.substr(0, url.indexOf('?'));
                else
                    url = url;
            }
        }
        return url;
    }
};
