1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/ko-orz-tekiiSDK

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
В этом репозитории не указан файл с открытой лицензией (LICENSE). При использовании обратитесь к конкретному описанию проекта и его зависимостям в коде.
Клонировать/Скачать
tekiiUtil.js 6 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
maxmon Отправлено 19.07.2020 11:15 ce80c96
console.log('TekiiUtil is loaded.')
const host = 'https://tekii.cn/market';
const alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789';
function getTopenid() {
// 微信openid为28位,tOpenid为27位(3+24)
let topenid = '';
let _topenid = wx.getStorageSync('topenid')
if (_topenid) {
topenid = _topenid
} else {
let alphabetLen = alphabet.length;
for (let i = 0; i < 24; i++) {
topenid += alphabet[(Math.random() * alphabetLen) | 0];
}
topenid = 'tq_' + topenid;
wx.setStorageSync('topenid', topenid);
}
return topenid;
}
/**
* 获取openid
* @param {object} config 本小程序的数据
* @param {string} config.appid 本小程序的appid
* @param {string} config.tSecret 本小程序的tSecret
*/
function getOpenid(config, cb) {
wx.login({
success: (usrInfo) => {
wx.request({
url: `${host}/sns/jscode2session`,
method: 'POST',
data: {
"code": usrInfo.code,
"appid": config.appid
},
header: {
'content-type': 'application/json',
't-secret': config.tSecret
},
success(res) {
if (res.data.errCode) {
console.log(res.data.errMsg);
cb && cb(getTopenid());
} else {
cb && cb(res.data.info.openid);
}
},
fail: console.error
})
}
})
}
/**
* 记录用户进入小程序,来自广告的数据
* @param {object} query 来自广告的数据
* @param {string} query.appid 广告主的appid
* @param {string} query.tid 用户在广告主那儿的tid
* @param {object} config 本小程序的数据
* @param {string} config.appid 本小程序的appid
* @param {string} config.tSecret 本小程序的tSecret
*/
function login(query, config, cb) {
wx.request({
url: `${host}/sdk/login`,
method: 'POST',
data: {
'from_tid': query.tid,
'from_appid': query.appid,
res: query.res,
openid: query.openid,
appid: config.appid,
},
header: {
'content-type': 'application/json',
't-secret': config.tSecret
},
success(res) {
cb && cb(res.data.info.tid);
},
fail: console.error
})
}
/**
* 处理init之后从launch里拿到的参数
* @param {object} res 小程序加载后回调的对象
*/
function afterLaunch(res, config) {
let { query } = res;
query = JSON.parse(JSON.stringify(query));
// this.globalData = this.globalData || {};
getOpenid(config, (openid) => {
console.log('openid is:', openid)
query.openid = openid;
query.res = JSON.stringify(res);
wx.setStorageSync('openid', openid)
wx && wx.aldstat && wx.aldstat.sendOpenid(openid)
// 记录广告进入的数据
login(query, config, (tid) => {
console.log('ok', tid)
wx.setStorageSync('tid', tid);
});
})
}
/**
* 初始化用户登录
* @param {object} config
* @param {string} config.tSecret 本小程序的tSecret
*/
function init(config) {
config.appid = gameAppId;
if (!config.tSecret) {
throw new Error('缺少tSecret,请联系天奇工作人员获取');
}
wx.setStorageSync('tSecret', config.tSecret);
if (wx.getLaunchOptionsSync) {
// 小游戏
let res = wx.getLaunchOptionsSync();
afterLaunch(res, config);
} else {
// 小程序
App({
onLaunch(res) {
afterLaunch(res, config);
}
})
}
console.log('TekiiUtil is inited.')
}
/**
* 记录用户行为
* @param {string} to_appid 目标小程序的appid
* @param {string} type 类型: 1、跳转其他小程序; 2、微信banner广告; 3、微信插屏广告; 4、微信激励视频广告; 5、用户分享; 6、其他
* @param {string} other 备注
*/
function logad(to_appid, type, other, cb) {
let appid = gameAppId;
let tid = wx.getStorageSync('tid');
let tSecret = wx.getStorageSync('tSecret');
wx.request({
url: `${host}/sdk/logad`,
method: 'POST',
data: {
appid,
tid,
to_appid,
type,
other,
},
header: {
'content-type': 'application/json',
't-secret': tSecret
},
success(res) {
if (res.data.errMsg) {
throw new Error(res.data.errMsg);
}
cb && cb(!res.data.errCode);
},
fail: console.error
})
}
/**
* 打开小程序
* @param {string} appid 目标appid
* @param {string} path 目标路径
* @param {object} config 配置
*/
function toad(appid, path, config = {}) {
let tid = wx.getStorageSync('tid');
console.log('tid', tid, 'ready to:', appid, path);
let _path = `tid=${tid}&appid=${gameAppId}`;
// 删除原有路径上的tid和appid,防止重复
path = path.replace(/([?&])tid=.*?(&|$)/g, '$1')
path = path.replace(/([?&])appid=.*?(&|$)/g, '$1')
if (path.indexOf('?') > -1) {
if (path[path.length - 1] !== '&') {
path += '&';
}
path += _path;
} else {
path += '?' + _path;
}
if (config.before) config.before()
wx.navigateToMiniProgram({
appId: appid, // 要跳转的小程序的appid
path, // 跳转的目标页面
success(res) {
logad(appid, 1, 'toad:' + path)
if (config.after) config.after()
}
})
}
/**
* 记录自定义小程序广告
* @param {string} self_ad_id 自定义广告id
* @param {number} type 类型 1展示 2点击 3确认跳转
*/
function logsad(self_ad_id, type, cb) {
let appid = gameAppId;
let tid = wx.getStorageSync('tid');
let tSecret = wx.getStorageSync('tSecret');
wx.request({
url: `${host}/selfAd/log`,
method: 'GET',
data: {
tid,
type,
self_ad_id
},
header: {
't-secret': tSecret
},
success(res) {
if (res.data.errMsg) {
throw new Error(res.data.errMsg);
}
cb && cb(!res.data.errCode);
},
fail: console.error
})
}
module.exports = {
init,
logad,
toad,
logsad
}

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/ko-orz-tekiiSDK.git
git@api.gitlife.ru:oschina-mirror/ko-orz-tekiiSDK.git
oschina-mirror
ko-orz-tekiiSDK
ko-orz-tekiiSDK
master