kd.js 2.34 KB
let appId = 'e13baf8d4a0d468eae005935332c5429';
let appKey = '1c60d1543db640e5970ffa4a794eb5c6';
KD.init(appId, appKey);
let weixin = new KD.weixin();
weixin.initConfig();
// window.weixin = weixin;
let openId = weixin.getOpenId();
window.openId = openId;
window.openid = openId;
window.wxUserInfo = null;

let urlShortCode = 's2n7/';
let href = location.href;
if (href.indexOf("192.168.") != -1 || href.indexOf("172.20.") != -1) {
    urlShortCode = "";
}
let domain = `${location.protocol}//${document.domain}/${urlShortCode}`;
let shareTitle = "";
let shareDesc = "";
let shareLink = `${domain}index.html`;
let shareImgUrl = `${domain}share.png`;

window.shareCodeLink = shareLink;

var KdIns = window.KdIns || ({});
// 设置分享
KdIns.setShare = function (title, desc, link, imgUrl) {
    if (!title) title = shareTitle;
    if (!desc) desc = shareDesc;
    if (!link) link = shareLink;
    if (!imgUrl) imgUrl = shareImgUrl;
    weixin.setShare(title, desc, link, imgUrl, (pos) => {})
}

console.log("domain:", domain);
KdIns.setShare(``, ``, ``, ``);

// KdIns.setShareShareCode = function (shareCode) {
//     let shareCodeLink = `${shareLink}#/coop?shareCode=${shareCode}`;
//     window.shareCodeLink = shareCodeLink;
//     KdIns.setShare(``, ``, shareCodeLink, ``);
// }

// KdIns.getShareLink = function (shareCode) {
//     let shareCodeLink = `${shareLink}#/coop?shareCode=${shareCode}`;
//     return shareCodeLink;
// }

// 
KdIns.setShareShareCode = function (shareCode) {
    let shareCodeLink = `${shareLink}?f=coop&shareCode=${shareCode}`;
    window.shareCodeLink = shareCodeLink;
    KdIns.setShare(``, ``, shareCodeLink, ``);
}
KdIns.getShareLink = function (shareCode) {
    let shareCodeLink = `${shareLink}?f=coop&shareCode=${shareCode}`;
    return shareCodeLink;
}


// 读取微信身份
KdIns.getUserInfo = function () {
    return new Promise((resolve, reject) => {
        weixin.getUserInfo((res) => {
            window.wxUserInfo = res;
            window.wxUserInfo.avatar = window.wxUserInfo.headimgurl;
            resolve(res);
        });
    });
}

// 读取微信身份CGI模式,需要用户显示授权后才能获得数据。获取信息中包含了用户的关注信息与unionid
KdIns.getCgiUserInfo = function () {
    return new Promise((resolve, reject) => {
        weixin.getCgiUserInfo((res) => {
            resolve(res);
        });
    });
}