Commit ff2edf1169e2156e4374c337c4e93b07308d6a72
1 parent
b9204825
Exists in
master
and in
3 other branches
X
Showing
51 changed files
with
7553 additions
and
0 deletions
Show diff stats
| @@ -0,0 +1,36 @@ | @@ -0,0 +1,36 @@ | ||
| 1 | +bin-debug | ||
| 2 | +bin-release | ||
| 3 | +exml.e.d.ts | ||
| 4 | +.idea/**/ | ||
| 5 | +.idea | ||
| 6 | +libs/modules | ||
| 7 | +node_modules/ | ||
| 8 | +bin/res | ||
| 9 | +bin/res/* | ||
| 10 | +bin/ui | ||
| 11 | +bin/ui/* | ||
| 12 | +bin/js | ||
| 13 | +bin/js/* | ||
| 14 | +bin/code.js | ||
| 15 | +bin/texture.png | ||
| 16 | +bin/unpack.json | ||
| 17 | +.idea/**/* | ||
| 18 | +.idea | ||
| 19 | +.laya/pubset.json | ||
| 20 | +.laya/settings.json | ||
| 21 | +.laya/chrome | ||
| 22 | +.laya/chrome/* | ||
| 23 | +release/wxgame/code.js | ||
| 24 | +release/wxgame/local | ||
| 25 | + | ||
| 26 | +wxsdk/*.meta | ||
| 27 | +sdk/*.meta | ||
| 28 | +wxsdk/*/*.meta | ||
| 29 | + | ||
| 30 | + | ||
| 31 | +.DS_Store | ||
| 32 | +wxgame/.DS_Store | ||
| 33 | + | ||
| 34 | +Thumbs.db | ||
| 35 | +code/Thumbs.db | ||
| 36 | +/tool/clear.sh |
| @@ -0,0 +1,87 @@ | @@ -0,0 +1,87 @@ | ||
| 1 | +import WXSDK from "../wxsdk/WXSDK"; | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +export class Analytics { | ||
| 5 | + private static _instance: Analytics; | ||
| 6 | + private systemType: number | ||
| 7 | + static get I(): Analytics { | ||
| 8 | + return this._instance || (this._instance = new Analytics); | ||
| 9 | + } | ||
| 10 | + | ||
| 11 | + private static IGNORE_LOGGER = false; | ||
| 12 | + | ||
| 13 | + constructor() { | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + private get isSupported() { | ||
| 17 | + return typeof WXSDK === 'object'; | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + // 游戏打点 | ||
| 21 | + dot(dot_type: string, data: any = {}) { | ||
| 22 | + if (typeof wx == 'undefined') return | ||
| 23 | + // wx.aldSendEvent(dot_type, data); //阿拉丁打点,没需求注释即可 | ||
| 24 | + return WXSDK.stat.dot(dot_type, data); | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + // loading完成 | ||
| 28 | + loadingFinish() { | ||
| 29 | + return WXSDK.stat.loadingFinish(); | ||
| 30 | + } | ||
| 31 | + // 用户停留30s打点 | ||
| 32 | + stay(time) { | ||
| 33 | + return WXSDK.stat.stay(time) | ||
| 34 | + } | ||
| 35 | + // 关键行为 | ||
| 36 | + behaviors() { | ||
| 37 | + return WXSDK.stat.behaviors() | ||
| 38 | + } | ||
| 39 | + // 导出打点 | ||
| 40 | + jumps(id, type) { | ||
| 41 | + return WXSDK.stat.jumps(id, type) | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + /** | ||
| 45 | + * 关卡开始 | ||
| 46 | + * @param stageid 关卡ID, 必须1.1、12.2 “.”前面代表模式 后面代表关卡 | ||
| 47 | + * @param stagename 关卡名称,格式:"xx模式-第x关" | ||
| 48 | + * @param pattern 模式名称,格式:"xx模式" | ||
| 49 | + */ | ||
| 50 | + levelStart(stageid, stagename, pattern) { | ||
| 51 | + return WXSDK.stat.levelStart(stageid, stagename, pattern) | ||
| 52 | + } | ||
| 53 | + /** | ||
| 54 | + * 关卡进行中 | ||
| 55 | + * @param stageid 关卡ID, 必须1.1、12.2 “.”前面代表模式 后面代表关卡 | ||
| 56 | + * @param stagename 关卡名称,格式:"xx模式-第x关" | ||
| 57 | + * @param pattern 模式名称,格式:"xx模式" | ||
| 58 | + * @param event 事件 tools:使用道具 award:奖励 | ||
| 59 | + * @param params_id 道具ID | ||
| 60 | + * @param params_name 道具名称 | ||
| 61 | + * @param params_count 道具数量 | ||
| 62 | + * @param params_desc 道具描述 | ||
| 63 | + */ | ||
| 64 | + levelRunning(stageid, stagename, pattern, event, params_id, params_name, params_count, params_desc?) { | ||
| 65 | + return WXSDK.stat.levelRunning(stageid, stagename, pattern, event, params_id, params_name, params_count, params_desc) | ||
| 66 | + } | ||
| 67 | + /** | ||
| 68 | + * 关卡结束 | ||
| 69 | + * @param stageid 关卡ID, 必须1.1、12.2 “.”前面代表模式 后面代表关卡 | ||
| 70 | + * @param stagename 关卡名称,格式:"xx模式-第x关" | ||
| 71 | + * @param pattern 模式名称,格式:"xx模式" | ||
| 72 | + * @param event complete:成功 fail:失败 | ||
| 73 | + * @param times 时间 | ||
| 74 | + * @param perc 失败时的完成进度 (浮点数) | ||
| 75 | + */ | ||
| 76 | + async levelEnd(stageid, stagename, pattern, event, times, perc?) { | ||
| 77 | + return WXSDK.stat.levelEnd(stageid, stagename, pattern, event, times, perc) | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | +} | ||
| 81 | +export enum EventKey { | ||
| 82 | + toggleScene = 'toggleScene', | ||
| 83 | + recoverGame = 'recoverGame', | ||
| 84 | + replayGame = 'replayGame', | ||
| 85 | + settingClose = 'settingClose', | ||
| 86 | + updateLvUI = 'updateLvUI', | ||
| 87 | +} | ||
| 0 | \ No newline at end of file | 88 | \ No newline at end of file |
| @@ -0,0 +1,329 @@ | @@ -0,0 +1,329 @@ | ||
| 1 | +import { GAMEDATA } from "../wxsdk/base/SDKConst"; | ||
| 2 | +import DateUtils from "../wxsdk/utils/DateUtils"; | ||
| 3 | +import WXSDK from "../wxsdk/WXSDK"; | ||
| 4 | + | ||
| 5 | +/* | ||
| 6 | +* SDK工具类库; | ||
| 7 | +*/ | ||
| 8 | +export class SDKTools { | ||
| 9 | + static get isWx() { | ||
| 10 | + return typeof wx !== 'undefined'; | ||
| 11 | + } | ||
| 12 | + static get isSupported() { | ||
| 13 | + return typeof WXSDK === 'object'; | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + static get envNum() { | ||
| 17 | + return WXSDK.data.envEnum; | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + static get scene() { | ||
| 21 | + return WXSDK.data.scene; | ||
| 22 | + } | ||
| 23 | + static get uid() { | ||
| 24 | + return WXSDK.data.userId; | ||
| 25 | + } | ||
| 26 | + static get channel() { | ||
| 27 | + return WXSDK.data.channelId; | ||
| 28 | + } | ||
| 29 | + static get openId() { | ||
| 30 | + return WXSDK.data.openId; | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + // 是否当天注册用户 | ||
| 34 | + static get isnew() { | ||
| 35 | + try { | ||
| 36 | + let t = DateUtils.today; | ||
| 37 | + return WXSDK.data.regTime === t; | ||
| 38 | + } catch (error) { | ||
| 39 | + return false; | ||
| 40 | + } | ||
| 41 | + } | ||
| 42 | + // 是否首次登录 | ||
| 43 | + static get isFirstLogin() { | ||
| 44 | + try { | ||
| 45 | + return WXSDK.data.isnew === 1; | ||
| 46 | + } catch (error) { | ||
| 47 | + return false; | ||
| 48 | + } | ||
| 49 | + } | ||
| 50 | + // 是否是某个时间点后注册的 | ||
| 51 | + static isAfterTime(t) { | ||
| 52 | + try { | ||
| 53 | + let t1 = new Date(t).getTime(); | ||
| 54 | + let t2 = new Date(`${WXSDK.data.regTime} 00:00:00`).getTime(); | ||
| 55 | + // console.log("t1t2", t1, t2) | ||
| 56 | + return t2 >= t1; | ||
| 57 | + } catch (error) { | ||
| 58 | + return false; | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + | ||
| 63 | + /** | ||
| 64 | + * banner | ||
| 65 | + * @param adUnitId bannerID | ||
| 66 | + * @param opts { //非必填 默认满屏居低 | ||
| 67 | + * type:默认1 banner | ||
| 68 | + * bannerWidth: | ||
| 69 | + * offsetY: 距离底部多远 | ||
| 70 | + * isOff:是否关闭默认显示banner | ||
| 71 | + * } | ||
| 72 | + */ | ||
| 73 | + static createBanner(adUnitId: string = GAMEDATA.bannerId, opts?: { type?: number; bannerWidth?: number, offsetY?: number; adIntervals?: number, isOff?: boolean }) { | ||
| 74 | + if (!this.isWx) return | ||
| 75 | + return WXSDK.ad.createBanner(adUnitId, opts); | ||
| 76 | + } | ||
| 77 | + /** | ||
| 78 | + * banner 显示 ps:创建默认显示 | ||
| 79 | + */ | ||
| 80 | + static showBanner() { | ||
| 81 | + if (!this.isWx) return | ||
| 82 | + WXSDK.ad.showBanner() | ||
| 83 | + } | ||
| 84 | + /** | ||
| 85 | + * banner 隐藏 | ||
| 86 | + */ | ||
| 87 | + static hideBanner() { | ||
| 88 | + if (!this.isWx) return | ||
| 89 | + WXSDK.ad.hideBanner(); | ||
| 90 | + } | ||
| 91 | + /** | ||
| 92 | + * banner 销毁 | ||
| 93 | + */ | ||
| 94 | + static destoryBanner() { | ||
| 95 | + if (!this.isWx) return | ||
| 96 | + WXSDK.ad.destoryBanner(); | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + | ||
| 100 | + /** | ||
| 101 | + * 插屏 | ||
| 102 | + */ | ||
| 103 | + static createInterstitialAd(adUnitId: string = GAMEDATA.interstitialAdId) { | ||
| 104 | + if (!this.isWx) return | ||
| 105 | + WXSDK.ad.createInterstitialAd(adUnitId); | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + | ||
| 109 | + /** | ||
| 110 | + * 格子广告 | ||
| 111 | + * @param adUnitId 格子广告ID | ||
| 112 | + * @param opts { //非必填 默认满屏居低 | ||
| 113 | + * bannerWidth: | ||
| 114 | + * offsetY: 距离底部多远 | ||
| 115 | + * isOff:是否默认不显示 | ||
| 116 | + * } | ||
| 117 | + */ | ||
| 118 | + static createGrid(key: string, adUnitId: string = GAMEDATA.gridId, opts?: { gridCount?: number; bannerWidth?: number, offsetY: number; adIntervals?: number, isOff?: boolean }) { | ||
| 119 | + if (!this.isWx) return | ||
| 120 | + return WXSDK.ad.createGrid(key, adUnitId, opts); | ||
| 121 | + } | ||
| 122 | + /** | ||
| 123 | + * 格子广告 显示 ps:创建默认显示 | ||
| 124 | + */ | ||
| 125 | + static showGrid(key: string) { | ||
| 126 | + if (!this.isWx) return | ||
| 127 | + WXSDK.ad.showGrid(key) | ||
| 128 | + } | ||
| 129 | + /** | ||
| 130 | + * 格子广告 隐藏 | ||
| 131 | + */ | ||
| 132 | + static hideGrid(key: string) { | ||
| 133 | + if (!this.isWx) return | ||
| 134 | + WXSDK.ad.hideGrid(key) | ||
| 135 | + } | ||
| 136 | + /** | ||
| 137 | + * 格子广告 销毁 | ||
| 138 | + */ | ||
| 139 | + static destoryGrid(key: string) { | ||
| 140 | + if (!this.isWx) return | ||
| 141 | + WXSDK.ad.destoryGrid(key) | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + | ||
| 145 | + /** | ||
| 146 | + * 原生模板广告 全局只能存在一个 并且创建的位置最好是同一个位置 通过show hide控制 如果要改变位置,调用destory再调用创建(不建议频繁销毁创建,会导致广告拉取不到) | ||
| 147 | + * @param adUnitId 格子广告ID | ||
| 148 | + * @param opts { //非必填 默认满屏居低 | ||
| 149 | + * bannerWidth: | ||
| 150 | + * offsetY: 距离底部多远 | ||
| 151 | + * } | ||
| 152 | + */ | ||
| 153 | + static createCustom(adUnitId: string = GAMEDATA.customId, opts: { top: number; left: number; adIntervals?: number }) { | ||
| 154 | + if (!this.isWx) return | ||
| 155 | + return WXSDK.ad.createCustom(adUnitId, opts); | ||
| 156 | + } | ||
| 157 | + /** | ||
| 158 | + * 原生广告 显示 ps:创建默认显示 | ||
| 159 | + */ | ||
| 160 | + static showCustom() { | ||
| 161 | + if (!this.isWx) return | ||
| 162 | + WXSDK.ad.showCustom() | ||
| 163 | + } | ||
| 164 | + /** | ||
| 165 | + * 原生广告 隐藏 | ||
| 166 | + */ | ||
| 167 | + static hideCustom() { | ||
| 168 | + if (!this.isWx) return | ||
| 169 | + WXSDK.ad.hideCustom() | ||
| 170 | + } | ||
| 171 | + /** | ||
| 172 | + * 原生广告 销毁 | ||
| 173 | + */ | ||
| 174 | + static destoryCustom() { | ||
| 175 | + if (!this.isWx) return | ||
| 176 | + WXSDK.ad.destoryCustom() | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + /** | ||
| 180 | + * | ||
| 181 | + * @param data 游戏数据 | ||
| 182 | + * @param type 游戏类型 | ||
| 183 | + * @param opts 扩展参数 | ||
| 184 | + */ | ||
| 185 | + static navigateToMiniProgram(data: IGameList, type, opts?) { | ||
| 186 | + if (!this.isWx) return | ||
| 187 | + return WXSDK.ad.navigateToMiniProgram(data, type, opts) | ||
| 188 | + } | ||
| 189 | + | ||
| 190 | + | ||
| 191 | + /** | ||
| 192 | + * 订阅 | ||
| 193 | + * @param template_ids 模板id eg['aaaaaaa','bbbbbbb'] | ||
| 194 | + * @param ids 对应后台的id eg:['1','2'] | ||
| 195 | + */ | ||
| 196 | + static subScribe(template_ids: Array<string>, ids: Array<string>) { | ||
| 197 | + if (!this.isWx) return | ||
| 198 | + return WXSDK.game.subScribe(template_ids, ids) | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + /** | ||
| 202 | + * 登录,sdk已实现弱登录和强登录的判断 | ||
| 203 | + */ | ||
| 204 | + static login(isAuthorize?: boolean): Promise<any> { | ||
| 205 | + return WXSDK.game.login(isAuthorize); | ||
| 206 | + } | ||
| 207 | + /** | ||
| 208 | + * 保存数据 | ||
| 209 | + * @param key | ||
| 210 | + * @param value | ||
| 211 | + */ | ||
| 212 | + static saveData(key: string, value: string) { | ||
| 213 | + return WXSDK.game.saveData(key, value) | ||
| 214 | + } | ||
| 215 | + /** | ||
| 216 | + * 获取数据 | ||
| 217 | + * @param key | ||
| 218 | + */ | ||
| 219 | + static getData(key: string) { | ||
| 220 | + return WXSDK.game.getData(key) | ||
| 221 | + } | ||
| 222 | + | ||
| 223 | + /** | ||
| 224 | + * 添加排行榜 2021-5-17 废弃新游戏勿用 | ||
| 225 | + * | ||
| 226 | + * @param key 获取排行时掉对应key即可获取对应的排行 | ||
| 227 | + * @param value 值 | ||
| 228 | + */ | ||
| 229 | + static rankAdd(key: string, value: number) { | ||
| 230 | + return WXSDK.game.rankAdd(key, value) | ||
| 231 | + } | ||
| 232 | + /** | ||
| 233 | + * 添加排行榜扩展数据 | ||
| 234 | + * @param value 对象 扩展字段 段位 等级等等... | ||
| 235 | + */ | ||
| 236 | + static updateRankData(value: Object = {}) { | ||
| 237 | + let val = JSON.stringify(value) | ||
| 238 | + return WXSDK.game.saveData('rankData', val) | ||
| 239 | + } | ||
| 240 | + /** | ||
| 241 | + * 排行榜数据 废弃新游戏勿用 | ||
| 242 | + * type:1 日榜 2周榜 3月榜 | ||
| 243 | + * isRankData: 是否有扩展参数 | ||
| 244 | + */ | ||
| 245 | + static rankList(key: string, type: number = 1, isRankData: boolean = false): Promise<IResult<any>> { | ||
| 246 | + if (!this.isWx) return Promise.resolve({ code: -1 }) | ||
| 247 | + return WXSDK.game.rankList(key, type, isRankData); | ||
| 248 | + } | ||
| 249 | + /** | ||
| 250 | + * 添加排行榜 2021-5-17 新增 | ||
| 251 | + * | ||
| 252 | + * @param key 获取排行时掉对应key即可获取对应的排行 | ||
| 253 | + * @param value 值 | ||
| 254 | + * @param type 0永久排行1日排行2周排行3月排行,默认为0 | ||
| 255 | + * @param sort 1 大到小 2小到大 | ||
| 256 | + */ | ||
| 257 | + static totalrankAdd(key: string, value: number, type: number = 0, sort: number = 1) { | ||
| 258 | + return WXSDK.game.totalrankAdd(key, value, type, sort); | ||
| 259 | + } | ||
| 260 | + /** | ||
| 261 | + * 排行榜数据 | ||
| 262 | + * type:0永久榜单 1 日榜 2周榜 3月榜 | ||
| 263 | + * isRankData: 是否有扩展参数 | ||
| 264 | + * @param sort 1 大到小 2小到大 | ||
| 265 | + */ | ||
| 266 | + static totalrankList(key: string, type: number = 0, isRankData: boolean = false, sort: number = 1): Promise<IResult<any>> { | ||
| 267 | + if (!this.isWx) return Promise.resolve({ code: -1 }); | ||
| 268 | + return WXSDK.game.totalrankList(key, type, isRankData, sort); | ||
| 269 | + } | ||
| 270 | + | ||
| 271 | + | ||
| 272 | + | ||
| 273 | + | ||
| 274 | + /** | ||
| 275 | + * 互导数据 | ||
| 276 | + * 1抽屉广告2猜你喜欢3格子广告4试玩 | ||
| 277 | + */ | ||
| 278 | + static adList(key: number): Promise<IResult<any>> { | ||
| 279 | + if (!this.isWx) return Promise.resolve({ code: -1 }) | ||
| 280 | + return WXSDK.game.adList(key); | ||
| 281 | + } | ||
| 282 | + /** | ||
| 283 | + * 添加曝光 | ||
| 284 | + * @param type 1,抽屉 2 banner(猜你喜欢)3格子4猜你喜欢 | ||
| 285 | + * @param arr | ||
| 286 | + */ | ||
| 287 | + static addExposure(type, arr: Array<IGameList>) { | ||
| 288 | + if (!this.isWx) return Promise.resolve({ code: -1 }) | ||
| 289 | + return WXSDK.stat.addExposure(type, arr) | ||
| 290 | + } | ||
| 291 | + | ||
| 292 | + /** | ||
| 293 | + * 初始化在线参数,初始化游戏调用 | ||
| 294 | + */ | ||
| 295 | + static updateOnlineParams() { | ||
| 296 | + return WXSDK.online.updateOnlineConfig(); | ||
| 297 | + } | ||
| 298 | + | ||
| 299 | + static getParamsInt(key: OnlineKeys | string, defaultVal: number = 0) { | ||
| 300 | + return WXSDK.online.getParamsInt(key.toString(), defaultVal) | ||
| 301 | + } | ||
| 302 | + | ||
| 303 | + static getParamsObj(key: OnlineKeys | string, defaultVal: any = {}) { | ||
| 304 | + return WXSDK.online.getParamsObj(key.toString(), defaultVal) | ||
| 305 | + } | ||
| 306 | + | ||
| 307 | + static getParamsString(key: OnlineKeys | string, defaultVal: string = '') { | ||
| 308 | + return WXSDK.online.getParamsString(key.toString(), defaultVal) | ||
| 309 | + } | ||
| 310 | + | ||
| 311 | + /** | ||
| 312 | + * 支付 | ||
| 313 | + * @param params | ||
| 314 | + * @param opts | ||
| 315 | + * @returns | ||
| 316 | + */ | ||
| 317 | + static pay(params: { money: number; source:string }, opts: any = {}) { | ||
| 318 | + return WXSDK.game.pay(params, opts) | ||
| 319 | + } | ||
| 320 | + | ||
| 321 | +} | ||
| 322 | + | ||
| 323 | +/** | ||
| 324 | + * 中台自定义配置表 | ||
| 325 | + */ | ||
| 326 | +export enum OnlineKeys { | ||
| 327 | + isOpenInterstitialAdId = 'isOpenInterstitialAdId', | ||
| 328 | + shareRandom = 'shareRandom', | ||
| 329 | +} | ||
| 0 | \ No newline at end of file | 330 | \ No newline at end of file |
| @@ -0,0 +1,94 @@ | @@ -0,0 +1,94 @@ | ||
| 1 | +import { utils } from "../Base/utils"; | ||
| 2 | +import { WxHelper } from "../Base/WxHelper"; | ||
| 3 | +import WXSDK from "../wxsdk/WXSDK"; | ||
| 4 | +import { OnlineKeys, SDKTools } from "./SDKTools"; | ||
| 5 | + | ||
| 6 | +/* | ||
| 7 | +* 分享与视频工具类; | ||
| 8 | +*/ | ||
| 9 | +export class ShareTools { | ||
| 10 | + private static isFail: boolean = false; | ||
| 11 | + /** | ||
| 12 | + * 是否进入分享 onShow有插屏的话用这个判断 | ||
| 13 | + */ | ||
| 14 | + public static onShowAd: boolean = false; | ||
| 15 | + | ||
| 16 | + public static isTest: boolean = false; | ||
| 17 | + /** | ||
| 18 | + * 验证分享:可处理成功、失败 | ||
| 19 | + * @param shareKey | ||
| 20 | + * @param params params.fail 有就不处理,没有自动处理 | ||
| 21 | + * @param opts 目前支持4个key 1,title自定义分享标题 2,img_url自定义分享图片 3,share_type(不走后台配置写死走视频or分享。1分享2视频3无视频则分享)4,closeSimulate是否关闭模拟分享 | ||
| 22 | + */ | ||
| 23 | + static share(shareKey: string, params?: { success?: Function, fail?: Function, context?: any }, opts?: any) { | ||
| 24 | + if (typeof wx === 'undefined' || this.isTest) { | ||
| 25 | + params && params.success && params.success(); | ||
| 26 | + return | ||
| 27 | + } | ||
| 28 | + this.onShowAd = true; | ||
| 29 | + WXSDK.share.share(shareKey.toString(), params, opts).then(async res => { | ||
| 30 | + this.onShowAd = false; | ||
| 31 | + params && params.success && params.success(res); | ||
| 32 | + }).catch(async err => { | ||
| 33 | + console.log("err", err) | ||
| 34 | + if (err && err.code && err.code === 1006) { | ||
| 35 | + if (Math.random() > SDKTools.getParamsInt(OnlineKeys.shareRandom, 1) && !this.isFail) { | ||
| 36 | + this.isFail = true; | ||
| 37 | + let modalRes = await WxHelper.showModal({ | ||
| 38 | + title: '分享失败', | ||
| 39 | + content: `${Math.random() > 0.5 ? '分享失败,请重新分享!' : '请分享其它群试试!'}`, | ||
| 40 | + showCancel: true, | ||
| 41 | + confirmText: '去分享' | ||
| 42 | + }) | ||
| 43 | + if (modalRes) { | ||
| 44 | + ShareTools.share(shareKey, params, opts); | ||
| 45 | + return | ||
| 46 | + } | ||
| 47 | + } else { | ||
| 48 | + this.isFail = false; | ||
| 49 | + } | ||
| 50 | + } | ||
| 51 | + this.onShowAd = false; | ||
| 52 | + if (!params || !params.fail) { | ||
| 53 | + utils.tips(err.msg) | ||
| 54 | + } else { | ||
| 55 | + params && params.fail && params.fail(err); | ||
| 56 | + } | ||
| 57 | + })//this.buildParams(params) | ||
| 58 | + } | ||
| 59 | + /** | ||
| 60 | + * 纯净分享 不处理回调 | ||
| 61 | + * @param shareKey | ||
| 62 | + */ | ||
| 63 | + static pureShare(shareKey: string, opts = {}) { | ||
| 64 | + this.share(shareKey, {}, { closeSimulate: true, ...opts }); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + /** | ||
| 68 | + * 设置右上角分享key | ||
| 69 | + * @param key | ||
| 70 | + */ | ||
| 71 | + static setForwardKey(key) { | ||
| 72 | + WXSDK.share.setForwardKey(key) | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + /** | ||
| 76 | + * 获取分享还是视频 | ||
| 77 | + * @param key | ||
| 78 | + */ | ||
| 79 | + static getShareType(key) { | ||
| 80 | + try { | ||
| 81 | + return WXSDK.share.getType(key) | ||
| 82 | + } catch (err) { | ||
| 83 | + console.log("1") | ||
| 84 | + return 2 | ||
| 85 | + } | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | + | ||
| 91 | + | ||
| 92 | +export enum ShareKey { | ||
| 93 | + | ||
| 94 | +} |
| @@ -0,0 +1,55 @@ | @@ -0,0 +1,55 @@ | ||
| 1 | +import { __LOG__ } from "./base/SDKConst"; | ||
| 2 | +import DataService from "./service/DataService"; | ||
| 3 | +import SdkData from "./service/entity/SdkData"; | ||
| 4 | +import ShareVideoService from "./service/ShareVideoService"; | ||
| 5 | +import WxInit from "./wx/WxInit"; | ||
| 6 | +import GameService from "./service/GameService"; | ||
| 7 | +import LogService from "./service/LogService"; | ||
| 8 | +import AdService from "./service/AdService"; | ||
| 9 | +import OnlineService from "./service/OnlineService"; | ||
| 10 | +import WxInterstitial from "./wx/WxInterstitial"; | ||
| 11 | +export default class WXSDK { | ||
| 12 | + public static get isWx(): boolean { | ||
| 13 | + return typeof (wx) != "undefined" | ||
| 14 | + } | ||
| 15 | + public static get data(): SdkData { | ||
| 16 | + return DataService.I.Data; | ||
| 17 | + } | ||
| 18 | + public static get share(): ShareVideoService { | ||
| 19 | + return ShareVideoService.I; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + public static get game(): GameService { | ||
| 23 | + return GameService.I; | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + public static get ad(): AdService { | ||
| 27 | + return AdService.I; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + public static get stat(): LogService { | ||
| 31 | + return LogService.I; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + public static get online(): OnlineService { | ||
| 35 | + return OnlineService.I; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + | ||
| 39 | + public static async init() { | ||
| 40 | + if (this.isWx) { | ||
| 41 | + WxInit.I.init(); | ||
| 42 | + //视频预加载 启动预加载视频会闪屏 | ||
| 43 | + setTimeout(() => { | ||
| 44 | + WxInterstitial.initInterstitialAd();//插屏预加载 | ||
| 45 | + ShareVideoService.I.preloadVideo(); | ||
| 46 | + }, 2000); | ||
| 47 | + } | ||
| 48 | + await this.game.env(); | ||
| 49 | + if (this.isWx) { | ||
| 50 | + ShareVideoService.I.init(); | ||
| 51 | + } | ||
| 52 | + return Promise.resolve(); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | +} | ||
| 0 | \ No newline at end of file | 56 | \ No newline at end of file |
| @@ -0,0 +1,11 @@ | @@ -0,0 +1,11 @@ | ||
| 1 | +export default interface BaseIStorage { | ||
| 2 | + getItem(key: string): string; | ||
| 3 | + | ||
| 4 | + setItem(key: string, value: string): void; | ||
| 5 | + | ||
| 6 | + removeItem(key: string): void; | ||
| 7 | + | ||
| 8 | + hasKey(key: string): boolean; | ||
| 9 | + | ||
| 10 | + clear(): void; | ||
| 11 | +} | ||
| 0 | \ No newline at end of file | 12 | \ No newline at end of file |
| @@ -0,0 +1,172 @@ | @@ -0,0 +1,172 @@ | ||
| 1 | +// 游戏配置数据 只需要改动这里的配置信息 | ||
| 2 | +// 游戏配置数据 只需要改动这里的配置信息 | ||
| 3 | +export const GAMEDATA = { | ||
| 4 | + game_id: '10060', | ||
| 5 | + channel_id: '10060', | ||
| 6 | + version: '1.0.0', | ||
| 7 | + appkey: 'dd2bd347b23befef947c6d95de0d6cc9', | ||
| 8 | + interstitialAdId: 'adunit-e8f80e01fb6e1b8e',//插屏ID | ||
| 9 | + bannerId: 'adunit-cc8fff6dee9188d4',//banner | ||
| 10 | + gridId: '',//格子 | ||
| 11 | + customId: 'adunit-cf69738dc3970fa7',//原生模板单个 //adunit-fb925feb16d2c13b | ||
| 12 | + videoAd: 'adunit-4f1adc59ecc219bb',//初始化视频广告id | ||
| 13 | + shareMessageToFriend: { // 暂时只支持一个场景值 | ||
| 14 | + scene: 10, //定向分享场景值1-50 配>0的会初始化 | ||
| 15 | + sharekey: 'shareMessageToFriendScene',//定向分享对应后台的分享key | ||
| 16 | + share_id: 26,//定向分享对应后台的分享key | ||
| 17 | + }, | ||
| 18 | + default_share: { | ||
| 19 | + content: '2021最新的数独题库来啦!', | ||
| 20 | + icon: 'https://cdn-wxsdk.miso-lab.com/0a/1c4c9d24237b33a4c6fd81d35e7ade.png?attname=share.png', | ||
| 21 | + id: '9999', | ||
| 22 | + key: 'default', | ||
| 23 | + title: '默认', | ||
| 24 | + typ: 1, | ||
| 25 | + videoid: '' | ||
| 26 | + }, | ||
| 27 | + MidasPay: { // 米大师虚拟支付配置 | ||
| 28 | + OfferId: "1450031480", // 在米大师申请的应用id | ||
| 29 | + ZoneId: "1", // 分区ID,默认:1 | ||
| 30 | + Mode: "game", // 默认:game | ||
| 31 | + CurrencyType: "CNY", // 默认:CNY | ||
| 32 | + Platform: '' // 申请接入时的平台 | ||
| 33 | + } | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | + | ||
| 37 | +// sdk版本 | ||
| 38 | +export const SDKVersion = 'v1.0'; | ||
| 39 | +// 是否打印 | ||
| 40 | +export const __LOG__ = true; | ||
| 41 | + | ||
| 42 | +//游戏基础信息 | ||
| 43 | +export const VersionHost = 'https://wxsdk-ver.d3games.com/version'; | ||
| 44 | + | ||
| 45 | +// 登录服务器url | ||
| 46 | +export const LoginHost = { | ||
| 47 | + Prod: 'https://login-wxsdk.d3games.com/', | ||
| 48 | + Pre: 'https://login-wxsdk-pre.d3games.com/' | ||
| 49 | +}; | ||
| 50 | +// 打点服务器URL | ||
| 51 | +export const DotHost = { | ||
| 52 | + Prod: 'https://wxsdk-api.cn-beijing.log.aliyuncs.com/', | ||
| 53 | + Pre: 'https://wxsdk-api.cn-beijing.log.aliyuncs.com/' | ||
| 54 | +}; | ||
| 55 | +// 业务接口 | ||
| 56 | +export const GameHost = { | ||
| 57 | + Prod: 'https://wxsdk-data.d3games.com/', | ||
| 58 | + Pre: 'https://wxsdk-data-pre.d3games.com/', | ||
| 59 | +}; | ||
| 60 | +// | ||
| 61 | +export const OrderHost = { | ||
| 62 | + Prod: 'https://wxsdk-order.d3games.com/', | ||
| 63 | + Pre: 'https://wxsdk-order-pre.d3games.com/', | ||
| 64 | +}; | ||
| 65 | + | ||
| 66 | +export const HostKeys = { | ||
| 67 | + //打点服务器 | ||
| 68 | + Active: 'logstores/login/track', //活跃用户 | ||
| 69 | + AdStat: 'logstores/adlog/track', // 上报广告行为 | ||
| 70 | + logOut: 'logstores/times/track', // 上报时长 | ||
| 71 | + Share: 'logstores/share/track', // 上报分享 | ||
| 72 | + loadingFinish: 'logstores/firstsecren/track', //结束加载 | ||
| 73 | + stay: 'logstores/stay/track', //用户停留 | ||
| 74 | + behaviors: 'logstores/behaviors/track', //关键行为 | ||
| 75 | + dot: 'logstores/events/track', //自定义打点 | ||
| 76 | + jumps: 'logstores/jumps/track', //游戏跳转 | ||
| 77 | + level: 'logstores/level/track', //关卡打点 | ||
| 78 | + | ||
| 79 | + //登录 | ||
| 80 | + Login: 'api/login', //登录 | ||
| 81 | + weakLogin: 'api/login', //登录 | ||
| 82 | + Reftoken: 'api/reftoken', //刷新登录令牌 | ||
| 83 | + | ||
| 84 | + //业务接口 | ||
| 85 | + ShareList: 'api/share/lst', //分享配置列表 | ||
| 86 | + getConfig: 'api/game/config', //在线参数 | ||
| 87 | + subscribe: 'api/subscribe/add', //订阅 | ||
| 88 | + saveData: 'api/member/savedata', //存数据 | ||
| 89 | + getData: 'api/member/getdata', //取数据 | ||
| 90 | + rankAdd: 'api/rank/add', //排行榜添加分数 517废弃 | ||
| 91 | + rankList: 'api/rank/list', //世界排行榜 517废弃 | ||
| 92 | + totalrankAdd: 'game/totalrank/add', //排行榜添加分数 | ||
| 93 | + totalrankList: 'game/totalrank/list', //世界排行榜 | ||
| 94 | + adList: 'api/adplan/list', //广告计划列表 | ||
| 95 | + | ||
| 96 | + //订单 | ||
| 97 | + orderReport: 'api/order/report' //订单信息上报 | ||
| 98 | +}; | ||
| 99 | + | ||
| 100 | +// 本地存储keys | ||
| 101 | +export const StorageKeys = { | ||
| 102 | + SDKAdVtKey: '__pcsdk_advt_key__', | ||
| 103 | + SDKAdMvKey: '__pcsdk_admv_key__', | ||
| 104 | + SDKAdMvNewKey: '__pcsdk_admv_new_key__', | ||
| 105 | + SDKTokenKey: '__pcsdk_token_key__', | ||
| 106 | + SDKLaunchKey: '__pcsdk_launch_key__', | ||
| 107 | + SDKEventLaunchKey: '__pcsdk_event_launch', | ||
| 108 | + SDKOnlienKey: '__pcsdk_online_env_key__', | ||
| 109 | + SDKStartUpKey: '__pcsdk_startup_key__', | ||
| 110 | + SDKDayAllShareNumKey: '__pcsdk_day_allsharenum_key__', | ||
| 111 | + SDKDayAllVideoNumKey: '__pcsdk_day_allvideonum_key__', | ||
| 112 | + SDKIntegralShareNumKey: '__pcsdk_integral_sharenum_key__', | ||
| 113 | + SDKVideoOverShareNumKey: '__pcsdk_video_over_sharenum_$0_key__', | ||
| 114 | + SDKShareRatioKey: '__pcsdk_shareratio_key__', | ||
| 115 | + SDKUserShieldKey: '__pcsdk_usershield_key__' | ||
| 116 | +}; | ||
| 117 | + | ||
| 118 | +// 互导广告类型 | ||
| 119 | +export const BannerType = { | ||
| 120 | + | ||
| 121 | +}; | ||
| 122 | + | ||
| 123 | +export const ErrorCode = { | ||
| 124 | + UnKnow: { code: 1001, msg: '未知错误' }, | ||
| 125 | + InvalidLogin: { code: 10000, msg: '登陆失效' } | ||
| 126 | +}; | ||
| 127 | + | ||
| 128 | +export const InterstitalAdError = { | ||
| 129 | + AdQuit: { code: 1000, msg: '要看完广告哦!' }, | ||
| 130 | + AdFail: { code: 1001, msg: '加载广告失败!' }, | ||
| 131 | + AdInvalid: { code: 999, msg: '广告UID不存在!' }, | ||
| 132 | + AdNotOpen: { code: 1002, msg: '微信版本过低,暂不支持!' }, | ||
| 133 | + AdPlaying: { code: 1003, msg: '正在加载中...' } | ||
| 134 | +}; | ||
| 135 | + | ||
| 136 | +export const ShareVideoError = { | ||
| 137 | + VideoQuit: { code: 1000, msg: '要看完视频哦!' }, | ||
| 138 | + VideoFail: { code: 1001, msg: '视频广告加载失败!' }, | ||
| 139 | + VideoInvalid: { code: 999, msg: '视频UID不存在!' }, | ||
| 140 | + VideoNotOpen: { code: 1002, msg: '视频组件未开放!' }, | ||
| 141 | + VideoPlaying: { code: 1003, msg: '正在观看视频中...' }, | ||
| 142 | + ShareFail: { code: 1004, msg: '分享失败,请尝试发送至不同群!' }, | ||
| 143 | + ShareSame: { code: 1005, msg: '别总骚扰这个群,换个群分享吧!' }, | ||
| 144 | + ShareNotGroup: { code: 1006, msg: '请分享到群哦!' }, | ||
| 145 | + NotNet: { code: 1007, msg: '网络错误~' }, | ||
| 146 | + ShareRuleFail: { code: 1008, msg: '分享失败,请尝试发送至不同群!' }, | ||
| 147 | + ShareOverLimit: { code: 1009, msg: '今日已达分享上限次数,请明日再来' }, | ||
| 148 | +}; | ||
| 149 | + | ||
| 150 | +export const BannerError = { | ||
| 151 | + BannerInvalid: { code: 1000, msg: '广告 uid不能为空!' }, | ||
| 152 | + BannerFail: { code: 1001, msg: '加载广告失败!' }, | ||
| 153 | + BannerNotOpen: { code: 1002, msg: '加载广告失败!' } | ||
| 154 | +}; | ||
| 155 | + | ||
| 156 | +export const Method = { | ||
| 157 | + Get: 'GET', | ||
| 158 | + Post: 'POST' | ||
| 159 | +}; | ||
| 160 | + | ||
| 161 | +export const SceneCode = { | ||
| 162 | + WX_STORE: 1104, // 我的小程序, | ||
| 163 | + WX_SHARE_FRIEND: 1007, // 好友分享 | ||
| 164 | + WX_SHARE_GROUP: 1008, // 群分享 | ||
| 165 | + WX_SHARE_TICKET: 1044, | ||
| 166 | + WX_INTEGRAL: 1139 // 积分投放 | ||
| 167 | +}; | ||
| 168 | + | ||
| 169 | +// sdk系统默认分享id | ||
| 170 | +export const SDKDotType = { | ||
| 171 | + Share: 1001 // 会话进入,无渠道ID | ||
| 172 | +}; | ||
| 0 | \ No newline at end of file | 173 | \ No newline at end of file |
| @@ -0,0 +1,228 @@ | @@ -0,0 +1,228 @@ | ||
| 1 | + | ||
| 2 | +/** 返回参数类型 */ | ||
| 3 | +interface IResult<T> { | ||
| 4 | + /** 错误码 */ | ||
| 5 | + code?: number; | ||
| 6 | + /** 错误信息 */ | ||
| 7 | + msg?: string; | ||
| 8 | + /** 返回数据 */ | ||
| 9 | + data?: T; | ||
| 10 | +} | ||
| 11 | + | ||
| 12 | +/** 微信api 返回用户信息 */ | ||
| 13 | +interface WxUserInfo { | ||
| 14 | + /** 用户昵称 */ | ||
| 15 | + nickName: string; | ||
| 16 | + /** 用户头像图片的 URL。URL 最后一个数值代表正方形头像大小 | ||
| 17 | + * (有 0、46、64、96、132 数值可选,0 代表 640x640 的正方形头像,46 表示 46x46 的正方形头像, | ||
| 18 | + * 剩余数值以此类推。默认132),用户没有头像时该项为空。若用户更换头像,原有头像 URL 将失效。 */ | ||
| 19 | + avatarUrl: string; | ||
| 20 | + /** 用户性别 gender的合法值(0未知,1男性,2女性) */ | ||
| 21 | + gender: number; | ||
| 22 | + /** 用户所在国家 */ | ||
| 23 | + country: string; | ||
| 24 | + /** 用户所在省份 */ | ||
| 25 | + province: string; | ||
| 26 | + /** 用户所在城市 */ | ||
| 27 | + city: string; | ||
| 28 | + /** 显示 country,province,city 所用的语言(en英文,zh_CN简体中文,zh_TW繁体中文) */ | ||
| 29 | + language: string; | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +/** 显示模态对话框参数类型 */ | ||
| 33 | +interface ShowModalType { | ||
| 34 | + /** 提示的标题 */ | ||
| 35 | + title: string; | ||
| 36 | + /** 提示的内容 */ | ||
| 37 | + content: string; | ||
| 38 | + /** 是否显示取消按钮 */ | ||
| 39 | + showCancel?: boolean; | ||
| 40 | + /** 取消按钮的文字,最多 4 个字符 */ | ||
| 41 | + cancelText?: string; | ||
| 42 | + /** 取消按钮的文字颜色,必须是 16 进制格式的颜色字符串 */ | ||
| 43 | + cancelColor?: string; | ||
| 44 | + /** 确认按钮的文字,最多 4 个字符 */ | ||
| 45 | + confirmText?: string; | ||
| 46 | + /** 确认按钮的文字颜色,必须是 16 进制格式的颜色字符串 */ | ||
| 47 | + confirmColor?: string; | ||
| 48 | + /** 接口调用成功的回调函数 */ | ||
| 49 | + success?: (res: { confirm: boolean, cancel: boolean }) => void; | ||
| 50 | + /** 接口调用失败的回调函数 */ | ||
| 51 | + fail?: () => void; | ||
| 52 | + /** 接口调用结束的回调函数(调用成功、失败都会执行) */ | ||
| 53 | + complete?: () => void; | ||
| 54 | +} | ||
| 55 | +/** | ||
| 56 | + * 排行榜数据 | ||
| 57 | + */ | ||
| 58 | +interface IRankData { | ||
| 59 | + fraction: number, // 对应type传入的分数、数量或者其他 | ||
| 60 | + headurl: string, //头像 | ||
| 61 | + nickname: string,//昵称 | ||
| 62 | + uid: number//用户id | ||
| 63 | + rankData: any //扩展参数 | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +interface _NetworkTypeSuccessObject { | ||
| 67 | + networkType: string; // wifi/2g/3g/4g/unknown(Android 下不常见的网络类型)/none(无网络) | ||
| 68 | +} | ||
| 69 | +interface _StyleObject { | ||
| 70 | + left?: number; | ||
| 71 | + | ||
| 72 | + top?: number; | ||
| 73 | + | ||
| 74 | + width?: number; | ||
| 75 | + | ||
| 76 | + height?: number; | ||
| 77 | +} | ||
| 78 | +interface _StyleGameObject { | ||
| 79 | + left?: number; | ||
| 80 | + top?: number; | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | +interface _BannerAdObject { | ||
| 84 | + adUnitId: string; | ||
| 85 | + adIntervals?: number; | ||
| 86 | + style: _StyleObject; | ||
| 87 | +} | ||
| 88 | +interface _FeedbackButtonObject { | ||
| 89 | + /** | ||
| 90 | + * 按钮的类型 | ||
| 91 | + */ | ||
| 92 | + type: string; | ||
| 93 | + | ||
| 94 | + /** | ||
| 95 | + * 按钮上的文本,仅当 type 为 text 时有效 | ||
| 96 | + */ | ||
| 97 | + text?: string; | ||
| 98 | + | ||
| 99 | + /** | ||
| 100 | + * 按钮的背景图片,仅当 type 为 image 时有效 | ||
| 101 | + */ | ||
| 102 | + image?: string; | ||
| 103 | + | ||
| 104 | + /** | ||
| 105 | + * 按钮的样式 | ||
| 106 | + */ | ||
| 107 | + style: _ButtonStyle; | ||
| 108 | +} | ||
| 109 | + | ||
| 110 | +interface _UserInfoButton { | ||
| 111 | + type: string; | ||
| 112 | + | ||
| 113 | + text: string; | ||
| 114 | + | ||
| 115 | + image: string; | ||
| 116 | + | ||
| 117 | + style: _ButtonStyle; | ||
| 118 | + | ||
| 119 | + show: Function; | ||
| 120 | + | ||
| 121 | + hide: Function; | ||
| 122 | + | ||
| 123 | + destroy: Function; | ||
| 124 | + | ||
| 125 | + onTap: (callback: Function) => void; | ||
| 126 | + | ||
| 127 | + offTap: (callback: Function) => void; | ||
| 128 | +} | ||
| 129 | + | ||
| 130 | +interface _ShareAppMessageObject { | ||
| 131 | + /** | ||
| 132 | + * 是否使用带 shareTicket 的转发详情 | ||
| 133 | + */ | ||
| 134 | + withShareTicket?: boolean; | ||
| 135 | + /** | ||
| 136 | + * 发标题,不传则默认使用当前小游戏的昵称。 | ||
| 137 | + */ | ||
| 138 | + title?: string; | ||
| 139 | + | ||
| 140 | + /** | ||
| 141 | + * 转发显示图片的链接,可以是网络图片路径或本地图片文件路径或相对代码包根目录的图片文件路径。显示图片长宽比是 5: 4 | ||
| 142 | + */ | ||
| 143 | + imageUrl?: string; | ||
| 144 | + | ||
| 145 | + /** | ||
| 146 | + * 查询字符串,必须是 key1 = val1 & key2=val2 的格式。从这条转发消息进入后,可通过 wx.getLaunchOptionSync() 或 wx.onShow() 获取启动参数中的 query。 | ||
| 147 | + */ | ||
| 148 | + query?: string; | ||
| 149 | + | ||
| 150 | + /** | ||
| 151 | + * 审核通过的图片 ID,详见 使用审核通过的转发图片 | ||
| 152 | + */ | ||
| 153 | + imageUrlId?: string; | ||
| 154 | + | ||
| 155 | + /** | ||
| 156 | + * 10.10后废弃 | ||
| 157 | + */ | ||
| 158 | + success?: (ret?: any) => void; | ||
| 159 | + | ||
| 160 | + /** | ||
| 161 | + * 接口调用失败的回调函数 | ||
| 162 | + */ | ||
| 163 | + fail?: (err?: any) => void; | ||
| 164 | + | ||
| 165 | + /** | ||
| 166 | + * 接口调用结束的回调函数(调用成功、失败都会执行) | ||
| 167 | + */ | ||
| 168 | + complete?: () => void; | ||
| 169 | + | ||
| 170 | + /** | ||
| 171 | + * 接口取消的回调 | ||
| 172 | + */ | ||
| 173 | + cancel?: () => void; | ||
| 174 | +} | ||
| 175 | +interface _UpdateShareMenuObject { | ||
| 176 | + /** | ||
| 177 | + * 是否使用带 shareTicket 的转发 | ||
| 178 | + */ | ||
| 179 | + withShareTicket?: boolean; | ||
| 180 | + | ||
| 181 | + /** | ||
| 182 | + * 接口调用成功的回调函数 | ||
| 183 | + */ | ||
| 184 | + success?: () => void; | ||
| 185 | + | ||
| 186 | + /** | ||
| 187 | + * 接口调用失败的回调函数 | ||
| 188 | + */ | ||
| 189 | + fail?: () => void; | ||
| 190 | + | ||
| 191 | + /** | ||
| 192 | + * 接口调用结束的回调函数(调用成功、失败都会执行) | ||
| 193 | + */ | ||
| 194 | + complete?: () => void; | ||
| 195 | + | ||
| 196 | + /** | ||
| 197 | + * 接口取消的回调 | ||
| 198 | + */ | ||
| 199 | + cancel?: () => void; | ||
| 200 | +} | ||
| 201 | +interface _getShareInfoSuccessObject { | ||
| 202 | + /** | ||
| 203 | + * 错误信息 | ||
| 204 | + */ | ||
| 205 | + errMsg: string; | ||
| 206 | + | ||
| 207 | + /** | ||
| 208 | + * 包括敏感数据在内的完整转发信息的加密数据,详细见[加密数据解密算法](./signature.md#加密数据解密算法) | ||
| 209 | + */ | ||
| 210 | + encryptedData: string; | ||
| 211 | + | ||
| 212 | + /** | ||
| 213 | + * 加密算法的初始向量,详细见[加密数据解密算法](./signature.md#加密数据解密算法) | ||
| 214 | + */ | ||
| 215 | + iv: string; | ||
| 216 | +} | ||
| 217 | + | ||
| 218 | + | ||
| 219 | +interface IGameList { | ||
| 220 | + id: number,//唯一id | ||
| 221 | + appid: string,//微信appid | ||
| 222 | + path: string, //携带参数 | ||
| 223 | + icon: string, //游戏icon | ||
| 224 | + game: string //游戏名称 | ||
| 225 | + adtyp: string,//类型 | ||
| 226 | + animation: string,//骨骼 | ||
| 227 | + highlight: string,//是否高亮 | ||
| 228 | +} |
| @@ -0,0 +1,97 @@ | @@ -0,0 +1,97 @@ | ||
| 1 | +export enum EnvCode { | ||
| 2 | + Prod = 2, // 正式环境 | ||
| 3 | + Pre = 1 // 测试环境 | ||
| 4 | +} | ||
| 5 | + | ||
| 6 | +// 性别 | ||
| 7 | +export enum Gender { | ||
| 8 | + Invalid = -1, // 无效 | ||
| 9 | + Unknown = 0, // 未知 | ||
| 10 | + Male = 1, // 男 | ||
| 11 | + Female = 2 // 女 | ||
| 12 | +} | ||
| 13 | + | ||
| 14 | +export enum BannerStat { | ||
| 15 | + Click = 0, // 0 点击广告icon | ||
| 16 | + Cancel = -1 // 1 取消(监听弹框【取消】事件) | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +// 分享视频类型 | ||
| 20 | +export enum ShareVideoType { | ||
| 21 | + None = -1, // -1无分享无视频 | ||
| 22 | + Share = 1, // 0分享 | ||
| 23 | + Video = 2, // 2看视频 | ||
| 24 | + VideoToShare = 3, // 3无视频则分享 | ||
| 25 | +} | ||
| 26 | + | ||
| 27 | +// 分享视频来源 | ||
| 28 | +export enum ShareVideoFrom { | ||
| 29 | + None, // 无分享 | ||
| 30 | + Share, // 同步分享 | ||
| 31 | + ShareAysnc, // 异步分享 | ||
| 32 | + Video // 看视频 | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +// 用户策略log统计,枚举类型 | ||
| 36 | +export enum TacticType { | ||
| 37 | + ShareLaunch = 10, // 分享调起 | ||
| 38 | + ShareInterrupt = 11, // 分享中断 | ||
| 39 | + ShareSuccess = 12, // 分享完成 | ||
| 40 | + ShareEnterLaunch = 13, // 分享被点入-用户启动游戏, | ||
| 41 | + ShareEnterGame = 14, // 分享被点入-用户进入游戏成功 | ||
| 42 | + | ||
| 43 | + VideoFetchSuccess = 20, // 视频拉取播放 | ||
| 44 | + VideoPlayInterrupt = 21,// 视频播放中断 | ||
| 45 | + VideoPlayComplete = 22, // 视频播放完成 | ||
| 46 | + VideoFetchFail = 23, // 视频拉取失败 | ||
| 47 | + | ||
| 48 | + InterstitialAdSuccess = 30, // 插屏广告拉取播放 | ||
| 49 | + InterstitialAdFail = 31 // 插屏广告拉取失败 | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +//分享打点类型 | ||
| 53 | +export enum DOT_SHARE_TYPE { | ||
| 54 | + default,//占位 | ||
| 55 | + share, //点击分享 | ||
| 56 | + click, //从别人的分享点入 | ||
| 57 | +} | ||
| 58 | +//广告类型 | ||
| 59 | +export enum DOT_AD_TYPE { | ||
| 60 | + default,//占位 | ||
| 61 | + banner,//banner | ||
| 62 | + video,//视频 | ||
| 63 | + interstitial,//插屏 | ||
| 64 | + grid,//格子广告 | ||
| 65 | + custom,//原生 | ||
| 66 | + | ||
| 67 | +} | ||
| 68 | +//广告状态 | ||
| 69 | +export enum DOT_AD_STATUS { | ||
| 70 | + default,//占位 | ||
| 71 | + request,//请求 | ||
| 72 | + rt,//返回 | ||
| 73 | + show,//展示 | ||
| 74 | + click,//点击 | ||
| 75 | + complete,//完成 | ||
| 76 | + interrupt,//中断 | ||
| 77 | + fail,//失败 | ||
| 78 | +} | ||
| 79 | + | ||
| 80 | +// 网络类型 | ||
| 81 | +export enum NetworkType { | ||
| 82 | + 'Wifi' = 'wifi', | ||
| 83 | + '2g' = '2g', | ||
| 84 | + '3g' = '3g', | ||
| 85 | + '4g' = '4g', | ||
| 86 | + 'Unknown' = 'unknown', // Android 下不常见的网络类型 | ||
| 87 | + 'None' = 'none' // 无网络 | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | + | ||
| 91 | +export enum AdType{ | ||
| 92 | + default, | ||
| 93 | + draw, | ||
| 94 | + guessLike, | ||
| 95 | + grid, | ||
| 96 | + play | ||
| 97 | +} | ||
| 0 | \ No newline at end of file | 98 | \ No newline at end of file |
| @@ -0,0 +1,186 @@ | @@ -0,0 +1,186 @@ | ||
| 1 | +/** | ||
| 2 | + * 事件管理器 | ||
| 3 | + * eg: | ||
| 4 | + * 注册一个事件:EventCenter.I.add( EventEnum.SCROLL_MAIL_VIEW , this.handleEvent , this ) | ||
| 5 | + * 移除一个事件:EventCenter.I.remove( EventData.TYPES.SCROLL_MAIL_VIEW , this.handleEvent ) | ||
| 6 | + * 触发一个事件:EventCenter.I.emit( EventData.TYPES.SCROLL_MAIL_VIEW, ...args ) | ||
| 7 | + */ | ||
| 8 | +// tslint:disable-next-line: jsdoc-format | ||
| 9 | +export default class SDKEventCenter { | ||
| 10 | + private _events: any; | ||
| 11 | + private _onceReturnValue: any; | ||
| 12 | + | ||
| 13 | + private static instance: SDKEventCenter; | ||
| 14 | + static get I(): SDKEventCenter { | ||
| 15 | + return this.instance || (this.instance = new SDKEventCenter); | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + // 防止外部初始化 | ||
| 19 | + private constructor() { | ||
| 20 | + this._events = {}; | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + private getEvents(): any { | ||
| 24 | + return this._events || (this._events = {}); | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + private getListeners(evt: string): any { | ||
| 28 | + let events = this.getEvents(); | ||
| 29 | + return events[evt] || (events[evt] = []); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + private getListenersAsObject(evt: string) { | ||
| 33 | + let listeners = this.getListeners(evt); | ||
| 34 | + let response: any; | ||
| 35 | + if (listeners instanceof Array) { | ||
| 36 | + response = {}; | ||
| 37 | + response[evt] = listeners; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + return response || listeners; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + private isValidListener(listener: any): boolean { | ||
| 44 | + if (typeof listener === 'function' || listener instanceof RegExp) | ||
| 45 | + return true; | ||
| 46 | + | ||
| 47 | + else if (listener && typeof listener === 'object') | ||
| 48 | + return this.isValidListener(listener.listener); | ||
| 49 | + | ||
| 50 | + else | ||
| 51 | + return false; | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + private getOnceReturnValue() { | ||
| 55 | + if (this.hasOwnProperty('_onceReturnValue')) { | ||
| 56 | + return this._onceReturnValue; | ||
| 57 | + } else { | ||
| 58 | + return true; | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + public set onceReturnValue(value: any) { | ||
| 63 | + this._onceReturnValue = value; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + indexOf(listeners: any[], listener: Function, context: any) { | ||
| 67 | + let i = listeners.length; | ||
| 68 | + while (i--) { | ||
| 69 | + if (listeners[i].listener === listener && (context ? listeners[i].context === context : true)) { | ||
| 70 | + return i; | ||
| 71 | + } | ||
| 72 | + } | ||
| 73 | + return -1; | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + /** | ||
| 77 | + * @param evt {String} evt | ||
| 78 | + * @param listener {Function|Object} listener | ||
| 79 | + * @param context {Object} context listener执行的上下文:当listener为function时候执行的上下文,当listener为Object时,可以不传,context可作为listener的一个context属性 | ||
| 80 | + */ | ||
| 81 | + add(evt: string, listener: any, context?: any): SDKEventCenter { | ||
| 82 | + if (!evt || !evt.constructor) { | ||
| 83 | + throw new TypeError('evt must be a string'); | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + if (!this.isValidListener(listener)) { | ||
| 87 | + throw new TypeError('listener must be a function'); | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + let listeners = this.getListenersAsObject(evt); | ||
| 91 | + let listenerIsWrapped = typeof listener === 'object'; | ||
| 92 | + let key: string; | ||
| 93 | + for (key in listeners) { | ||
| 94 | + if (listeners.hasOwnProperty(key) && this.indexOf(listeners[key], listener, context) === -1) { | ||
| 95 | + listeners[key].push(listenerIsWrapped ? listener : { | ||
| 96 | + listener, | ||
| 97 | + context, | ||
| 98 | + once: false | ||
| 99 | + }); | ||
| 100 | + } | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + return this; | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + once(evt: string, listener: Function, context?: any): SDKEventCenter { | ||
| 107 | + return this.add(evt, { | ||
| 108 | + listener, | ||
| 109 | + context, | ||
| 110 | + once: true | ||
| 111 | + }); | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + remove(evt: string, listener: Function, context?: any): SDKEventCenter { | ||
| 115 | + let listeners = this.getListenersAsObject(evt); | ||
| 116 | + let index: number; | ||
| 117 | + let key: string; | ||
| 118 | + for (key in listeners) { | ||
| 119 | + if (listeners.hasOwnProperty(key)) { | ||
| 120 | + index = this.indexOf(listeners[key], listener, context); | ||
| 121 | + if (index !== -1) { | ||
| 122 | + listeners[key].splice(index, 1); | ||
| 123 | + } | ||
| 124 | + } | ||
| 125 | + } | ||
| 126 | + return this; | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + removeAll(evt: string): SDKEventCenter { | ||
| 130 | + let type = typeof evt; | ||
| 131 | + let events = this.getEvents(); | ||
| 132 | + if (type === 'string') { | ||
| 133 | + delete events[evt]; | ||
| 134 | + } else { | ||
| 135 | + delete this._events; | ||
| 136 | + } | ||
| 137 | + return this; | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + trigger(evt: string, ...args: any[]): SDKEventCenter { | ||
| 141 | + let listenersMap = this.getListenersAsObject(evt); | ||
| 142 | + let listeners: any; | ||
| 143 | + let listener: any; | ||
| 144 | + let i: number; | ||
| 145 | + let key: string; | ||
| 146 | + let response: any; | ||
| 147 | + | ||
| 148 | + for (key in listenersMap) { | ||
| 149 | + if (listenersMap.hasOwnProperty(key)) { | ||
| 150 | + listeners = listenersMap[key].slice(0); | ||
| 151 | + | ||
| 152 | + for (i = 0; i < listeners.length; i++) { | ||
| 153 | + listener = listeners[i]; | ||
| 154 | + | ||
| 155 | + if (listener.once === true) { | ||
| 156 | + this.remove(evt, listener.listener, listener.context); | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + response = listener.listener.apply(listener.context || this, args || []); | ||
| 160 | + | ||
| 161 | + if (response === this.getOnceReturnValue()) { | ||
| 162 | + this.remove(evt, listener.listener, listener.context); | ||
| 163 | + } | ||
| 164 | + } | ||
| 165 | + } | ||
| 166 | + } | ||
| 167 | + return this; | ||
| 168 | + } | ||
| 169 | + | ||
| 170 | + emit(evt: string, ...args: any[]) { | ||
| 171 | + // let args = Array.prototype.slice.call(arguments, 1); | ||
| 172 | + return this.trigger(evt, ...args); | ||
| 173 | + } | ||
| 174 | + | ||
| 175 | + defineEvent(evt: string): SDKEventCenter { | ||
| 176 | + this.getListeners(evt); | ||
| 177 | + return this; | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + defineEvents(evts: string[]): SDKEventCenter { | ||
| 181 | + for (let i = 0, len = evts.length; i < len; i++) { | ||
| 182 | + this.defineEvent(evts[i]); | ||
| 183 | + } | ||
| 184 | + return this; | ||
| 185 | + } | ||
| 186 | +} | ||
| 0 | \ No newline at end of file | 187 | \ No newline at end of file |
| @@ -0,0 +1,14 @@ | @@ -0,0 +1,14 @@ | ||
| 1 | +/** | ||
| 2 | + * 事件枚举名称 | ||
| 3 | + */ | ||
| 4 | +export enum SDKEventEnum { | ||
| 5 | + APP_SHOW = 'app.show', | ||
| 6 | + APP_HIDE = 'app.hide', | ||
| 7 | + TACTIC_UPDATE = 'tactic.update', | ||
| 8 | + BANNER_HIDE = 'banner.hide', | ||
| 9 | + BANNER_SHOW = 'banner.show', | ||
| 10 | + BANNER_ERROR = 'banner.error', | ||
| 11 | + BANNER_DESTORY = 'banner.destory', | ||
| 12 | + BANNER_SUCCESS = 'banner.success', | ||
| 13 | + ONLINE_SUCCESS = 'online.success' | ||
| 14 | +} | ||
| 0 | \ No newline at end of file | 15 | \ No newline at end of file |
| @@ -0,0 +1,63 @@ | @@ -0,0 +1,63 @@ | ||
| 1 | +import SDKHttp from "./SDKHttp"; | ||
| 2 | +import { VersionHost, HostKeys } from "../base/SDKConst"; | ||
| 3 | +import DataService from "../service/DataService"; | ||
| 4 | + | ||
| 5 | + | ||
| 6 | +export class SDKApi { | ||
| 7 | + | ||
| 8 | + public static Version = (...args) => SDKHttp.httpPost(`${VersionHost}`, ...args); | ||
| 9 | + | ||
| 10 | + //GameApi | ||
| 11 | + public static ShareList = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.ShareList}`, ...args); | ||
| 12 | + | ||
| 13 | + public static getConfig = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.getConfig}`, ...args); | ||
| 14 | + //订阅 | ||
| 15 | + public static subscribe = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.subscribe}`, ...args); | ||
| 16 | + // | ||
| 17 | + public static saveData = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.saveData}`, ...args); | ||
| 18 | + public static getData = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.getData}`, ...args); | ||
| 19 | + //排行榜添加分数 废弃 | ||
| 20 | + public static rankAdd = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.rankAdd}`, ...args); | ||
| 21 | + //排行榜添加分数 | ||
| 22 | + public static totalrankAdd = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.totalrankAdd}`, ...args); | ||
| 23 | + //排行榜 废弃 | ||
| 24 | + public static rankList = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.rankList}`, ...args); | ||
| 25 | + //排行榜 | ||
| 26 | + public static totalrankList = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.totalrankList}`, ...args); | ||
| 27 | + //广告计划列表 | ||
| 28 | + public static adList = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.adList}`, ...args); | ||
| 29 | + | ||
| 30 | + | ||
| 31 | + //LoginApi | ||
| 32 | + public static Login = (...args) => SDKHttp.httpPost(`${DataService.I.LoginApi}${HostKeys.Login}`, ...args); | ||
| 33 | + | ||
| 34 | + public static reftoken = (...args) => SDKHttp.httpPost(`${DataService.I.LoginApi}${HostKeys.Reftoken}`, ...args); | ||
| 35 | + | ||
| 36 | + public static weakLogin = (...args) => SDKHttp.httpPost(`${DataService.I.LoginApi}${HostKeys.weakLogin}`, ...args); | ||
| 37 | + | ||
| 38 | + | ||
| 39 | + //DotApi | ||
| 40 | + // public static dot = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.Dot}`, ...args); | ||
| 41 | + | ||
| 42 | + public static logOut = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.logOut}`, ...args); | ||
| 43 | + | ||
| 44 | + public static loadingFinish = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.loadingFinish}`, ...args); | ||
| 45 | + | ||
| 46 | + public static active = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.Active}`, ...args); | ||
| 47 | + | ||
| 48 | + public static share = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.Share}`, ...args); | ||
| 49 | + | ||
| 50 | + public static adStat = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.AdStat}`, ...args); | ||
| 51 | + | ||
| 52 | + public static stay = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.stay}`, ...args); | ||
| 53 | + | ||
| 54 | + public static behaviors = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.behaviors}`, ...args); | ||
| 55 | + | ||
| 56 | + public static dot = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.dot}`, ...args); | ||
| 57 | + | ||
| 58 | + public static jumps = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.jumps}`, ...args); | ||
| 59 | + | ||
| 60 | + public static level = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.level}`, ...args); | ||
| 61 | + | ||
| 62 | + public static pay = (...args) => SDKHttp.httpPost(`${DataService.I.OrderApi}${HostKeys.orderReport}`, ...args); | ||
| 63 | +} |
| @@ -0,0 +1,107 @@ | @@ -0,0 +1,107 @@ | ||
| 1 | +import { GAMEDATA } from "../base/SDKConst"; | ||
| 2 | +import SignUtils from "../utils/SignUtils"; | ||
| 3 | + | ||
| 4 | +export default class SDKHttp { | ||
| 5 | + public static async httpRequest(url: string, method: string, data?: any, dataType: "json" | "string" = "json") { | ||
| 6 | + return new Promise<IResult<any>>((resolve, reject) => { | ||
| 7 | + data = { | ||
| 8 | + ...data, | ||
| 9 | + ver: GAMEDATA.version, | ||
| 10 | + gameid: GAMEDATA.game_id, | ||
| 11 | + sign_type: 'md5', | ||
| 12 | + time_stamp: (Math.floor(Date.now() / 1000)) + '', | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + // if (url.indexOf('totalrank')>-1) { | ||
| 16 | + // // console.log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") | ||
| 17 | + // url = 'https://wxsdk-game-pre.d3games.com/game/totalrank/list'; | ||
| 18 | + // } | ||
| 19 | + | ||
| 20 | + //生成sign | ||
| 21 | + data = { | ||
| 22 | + ...data, | ||
| 23 | + sign: SignUtils.I.createSign(data) | ||
| 24 | + } | ||
| 25 | + if (data && typeof data === "object") { | ||
| 26 | + data = JSON.stringify(data); | ||
| 27 | + } | ||
| 28 | + //console.error("sign", url, JSON.stringify(data)) | ||
| 29 | + data = data || ""; | ||
| 30 | + if (method == "GET" && data != "") { | ||
| 31 | + data = JSON.parse(data); | ||
| 32 | + let str = '' | ||
| 33 | + for (let key in data) { | ||
| 34 | + str = str + `${key}` + '=' + `${data[key]}&` | ||
| 35 | + } | ||
| 36 | + url += "?" + str; | ||
| 37 | + data = ""; | ||
| 38 | + } | ||
| 39 | + let info = "[url:" + url + ", data:" + data + "]"; | ||
| 40 | + let xhr = new XMLHttpRequest(); | ||
| 41 | + xhr.onreadystatechange = function () { | ||
| 42 | + if (xhr.readyState == 4) { | ||
| 43 | + if (xhr.status >= 200 && xhr.status < 400) { | ||
| 44 | + let responseText: any = xhr.responseText; | ||
| 45 | + // cc.log("responseText", responseText) | ||
| 46 | + try { | ||
| 47 | + responseText = JSON.parse(responseText); | ||
| 48 | + // cc.log("responseText22", responseText) | ||
| 49 | + if (url.indexOf('.json') > -1) { | ||
| 50 | + resolve({ code: 0, data: responseText, msg: responseText.msg }); | ||
| 51 | + } else { | ||
| 52 | + resolve({ code: +responseText.code, data: responseText.data, msg: responseText.msg }); | ||
| 53 | + } | ||
| 54 | + return | ||
| 55 | + } catch (ex) { | ||
| 56 | + // console.error("httpRequest[parseError]:responseText=" + xhr.responseText); | ||
| 57 | + resolve({ msg: "JSON parse error:" + ex.message, code: -1 }); | ||
| 58 | + return; | ||
| 59 | + } | ||
| 60 | + } else { | ||
| 61 | + console.error(xhr.status, '网络请求失败!'); | ||
| 62 | + resolve({ code: -1 }); | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | + }; | ||
| 66 | + | ||
| 67 | + xhr.ontimeout = function (info): void { | ||
| 68 | + // cc.error("info1", info) | ||
| 69 | + resolve({ msg: `请求超时!`, code: -1 }); | ||
| 70 | + } | ||
| 71 | + xhr.onerror = function (info): void { | ||
| 72 | + // cc.error("info2", info) | ||
| 73 | + resolve({ msg: `请求失败!`, code: -1 }); | ||
| 74 | + } | ||
| 75 | + xhr.onabort = function (info): void { | ||
| 76 | + // cc.error("info3", info) | ||
| 77 | + resolve({ msg: `请求失败!`, code: -1 }); | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + xhr.open(method, url, true); | ||
| 81 | + | ||
| 82 | + if (method == "POST") { | ||
| 83 | + xhr.setRequestHeader("Content-Type", "application/json;charset=utf-8")//application/x-www-form-urlencoded | ||
| 84 | + // if (cc.sys.os === 'Android') { | ||
| 85 | + // cc.error("http__uid", AppSdkData.I.uid); | ||
| 86 | + // xhr.setRequestHeader('Uuid', `${AppSdkData.I.uid}`); | ||
| 87 | + // } else { | ||
| 88 | + // xhr.setRequestHeader('Uuid', `909`); | ||
| 89 | + // } | ||
| 90 | + | ||
| 91 | + } | ||
| 92 | + xhr.timeout = 3000; | ||
| 93 | + xhr.send(data); | ||
| 94 | + | ||
| 95 | + | ||
| 96 | + | ||
| 97 | + }); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + public static async httpGet(url: string, data?: any, dataType: "json" | "string" = "json") { | ||
| 101 | + return this.httpRequest(url, "GET", data, dataType); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + public static httpPost(url: string, data?: any, dataType: "json" | "string" = "json") { | ||
| 105 | + return this.httpRequest(url, "POST", data, dataType); | ||
| 106 | + } | ||
| 107 | +} | ||
| 0 | \ No newline at end of file | 108 | \ No newline at end of file |
| @@ -0,0 +1,378 @@ | @@ -0,0 +1,378 @@ | ||
| 1 | + | ||
| 2 | +export class Md5 { | ||
| 3 | + // One time hashing functions | ||
| 4 | + public static hashStr(str: string, raw?: false): string | ||
| 5 | + public static hashStr(str: string, raw?: true): Int32Array | ||
| 6 | + public static hashStr(str: string, raw: boolean = false) { | ||
| 7 | + return this.onePassHasher | ||
| 8 | + .start() | ||
| 9 | + .appendStr(str) | ||
| 10 | + .end(raw); | ||
| 11 | + } | ||
| 12 | + | ||
| 13 | + public static hashAsciiStr(str: string, raw?: false): string | ||
| 14 | + public static hashAsciiStr(str: string, raw?: true): Int32Array | ||
| 15 | + public static hashAsciiStr(str: string, raw: boolean = false) { | ||
| 16 | + return this.onePassHasher | ||
| 17 | + .start() | ||
| 18 | + .appendAsciiStr(str) | ||
| 19 | + .end(raw); | ||
| 20 | + } | ||
| 21 | + // Private Static Variables | ||
| 22 | + private static stateIdentity = new Int32Array([1732584193, -271733879, -1732584194, 271733878]); | ||
| 23 | + private static buffer32Identity = new Int32Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); | ||
| 24 | + private static hexChars = '0123456789abcdef'; | ||
| 25 | + private static hexOut: string[] = []; | ||
| 26 | + | ||
| 27 | + // Permanent instance is to use for one-call hashing | ||
| 28 | + private static onePassHasher = new Md5(); | ||
| 29 | + | ||
| 30 | + private static _hex(x: any): string { | ||
| 31 | + const hc = Md5.hexChars; | ||
| 32 | + const ho = Md5.hexOut; | ||
| 33 | + let n; | ||
| 34 | + let offset; | ||
| 35 | + let j; | ||
| 36 | + let i; | ||
| 37 | + | ||
| 38 | + for (i = 0; i < 4; i += 1) { | ||
| 39 | + offset = i * 8; | ||
| 40 | + n = x[i]; | ||
| 41 | + for (j = 0; j < 8; j += 2) { | ||
| 42 | + ho[offset + 1 + j] = hc.charAt(n & 0x0F); | ||
| 43 | + n >>>= 4; | ||
| 44 | + ho[offset + 0 + j] = hc.charAt(n & 0x0F); | ||
| 45 | + n >>>= 4; | ||
| 46 | + } | ||
| 47 | + } | ||
| 48 | + return ho.join(''); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + private static _md5cycle(x: Int32Array|Uint32Array, k: Int32Array|Uint32Array) { | ||
| 52 | + let a = x[0]; | ||
| 53 | + let b = x[1]; | ||
| 54 | + let c = x[2]; | ||
| 55 | + let d = x[3]; | ||
| 56 | + // ff() | ||
| 57 | + a += (b & c | ~b & d) + k[0] - 680876936 | 0; | ||
| 58 | + a = (a << 7 | a >>> 25) + b | 0; | ||
| 59 | + d += (a & b | ~a & c) + k[1] - 389564586 | 0; | ||
| 60 | + d = (d << 12 | d >>> 20) + a | 0; | ||
| 61 | + c += (d & a | ~d & b) + k[2] + 606105819 | 0; | ||
| 62 | + c = (c << 17 | c >>> 15) + d | 0; | ||
| 63 | + b += (c & d | ~c & a) + k[3] - 1044525330 | 0; | ||
| 64 | + b = (b << 22 | b >>> 10) + c | 0; | ||
| 65 | + a += (b & c | ~b & d) + k[4] - 176418897 | 0; | ||
| 66 | + a = (a << 7 | a >>> 25) + b | 0; | ||
| 67 | + d += (a & b | ~a & c) + k[5] + 1200080426 | 0; | ||
| 68 | + d = (d << 12 | d >>> 20) + a | 0; | ||
| 69 | + c += (d & a | ~d & b) + k[6] - 1473231341 | 0; | ||
| 70 | + c = (c << 17 | c >>> 15) + d | 0; | ||
| 71 | + b += (c & d | ~c & a) + k[7] - 45705983 | 0; | ||
| 72 | + b = (b << 22 | b >>> 10) + c | 0; | ||
| 73 | + a += (b & c | ~b & d) + k[8] + 1770035416 | 0; | ||
| 74 | + a = (a << 7 | a >>> 25) + b | 0; | ||
| 75 | + d += (a & b | ~a & c) + k[9] - 1958414417 | 0; | ||
| 76 | + d = (d << 12 | d >>> 20) + a | 0; | ||
| 77 | + c += (d & a | ~d & b) + k[10] - 42063 | 0; | ||
| 78 | + c = (c << 17 | c >>> 15) + d | 0; | ||
| 79 | + b += (c & d | ~c & a) + k[11] - 1990404162 | 0; | ||
| 80 | + b = (b << 22 | b >>> 10) + c | 0; | ||
| 81 | + a += (b & c | ~b & d) + k[12] + 1804603682 | 0; | ||
| 82 | + a = (a << 7 | a >>> 25) + b | 0; | ||
| 83 | + d += (a & b | ~a & c) + k[13] - 40341101 | 0; | ||
| 84 | + d = (d << 12 | d >>> 20) + a | 0; | ||
| 85 | + c += (d & a | ~d & b) + k[14] - 1502002290 | 0; | ||
| 86 | + c = (c << 17 | c >>> 15) + d | 0; | ||
| 87 | + b += (c & d | ~c & a) + k[15] + 1236535329 | 0; | ||
| 88 | + b = (b << 22 | b >>> 10) + c | 0; | ||
| 89 | + // gg() | ||
| 90 | + a += (b & d | c & ~d) + k[1] - 165796510 | 0; | ||
| 91 | + a = (a << 5 | a >>> 27) + b | 0; | ||
| 92 | + d += (a & c | b & ~c) + k[6] - 1069501632 | 0; | ||
| 93 | + d = (d << 9 | d >>> 23) + a | 0; | ||
| 94 | + c += (d & b | a & ~b) + k[11] + 643717713 | 0; | ||
| 95 | + c = (c << 14 | c >>> 18) + d | 0; | ||
| 96 | + b += (c & a | d & ~a) + k[0] - 373897302 | 0; | ||
| 97 | + b = (b << 20 | b >>> 12) + c | 0; | ||
| 98 | + a += (b & d | c & ~d) + k[5] - 701558691 | 0; | ||
| 99 | + a = (a << 5 | a >>> 27) + b | 0; | ||
| 100 | + d += (a & c | b & ~c) + k[10] + 38016083 | 0; | ||
| 101 | + d = (d << 9 | d >>> 23) + a | 0; | ||
| 102 | + c += (d & b | a & ~b) + k[15] - 660478335 | 0; | ||
| 103 | + c = (c << 14 | c >>> 18) + d | 0; | ||
| 104 | + b += (c & a | d & ~a) + k[4] - 405537848 | 0; | ||
| 105 | + b = (b << 20 | b >>> 12) + c | 0; | ||
| 106 | + a += (b & d | c & ~d) + k[9] + 568446438 | 0; | ||
| 107 | + a = (a << 5 | a >>> 27) + b | 0; | ||
| 108 | + d += (a & c | b & ~c) + k[14] - 1019803690 | 0; | ||
| 109 | + d = (d << 9 | d >>> 23) + a | 0; | ||
| 110 | + c += (d & b | a & ~b) + k[3] - 187363961 | 0; | ||
| 111 | + c = (c << 14 | c >>> 18) + d | 0; | ||
| 112 | + b += (c & a | d & ~a) + k[8] + 1163531501 | 0; | ||
| 113 | + b = (b << 20 | b >>> 12) + c | 0; | ||
| 114 | + a += (b & d | c & ~d) + k[13] - 1444681467 | 0; | ||
| 115 | + a = (a << 5 | a >>> 27) + b | 0; | ||
| 116 | + d += (a & c | b & ~c) + k[2] - 51403784 | 0; | ||
| 117 | + d = (d << 9 | d >>> 23) + a | 0; | ||
| 118 | + c += (d & b | a & ~b) + k[7] + 1735328473 | 0; | ||
| 119 | + c = (c << 14 | c >>> 18) + d | 0; | ||
| 120 | + b += (c & a | d & ~a) + k[12] - 1926607734 | 0; | ||
| 121 | + b = (b << 20 | b >>> 12) + c | 0; | ||
| 122 | + // hh() | ||
| 123 | + a += (b ^ c ^ d) + k[5] - 378558 | 0; | ||
| 124 | + a = (a << 4 | a >>> 28) + b | 0; | ||
| 125 | + d += (a ^ b ^ c) + k[8] - 2022574463 | 0; | ||
| 126 | + d = (d << 11 | d >>> 21) + a | 0; | ||
| 127 | + c += (d ^ a ^ b) + k[11] + 1839030562 | 0; | ||
| 128 | + c = (c << 16 | c >>> 16) + d | 0; | ||
| 129 | + b += (c ^ d ^ a) + k[14] - 35309556 | 0; | ||
| 130 | + b = (b << 23 | b >>> 9) + c | 0; | ||
| 131 | + a += (b ^ c ^ d) + k[1] - 1530992060 | 0; | ||
| 132 | + a = (a << 4 | a >>> 28) + b | 0; | ||
| 133 | + d += (a ^ b ^ c) + k[4] + 1272893353 | 0; | ||
| 134 | + d = (d << 11 | d >>> 21) + a | 0; | ||
| 135 | + c += (d ^ a ^ b) + k[7] - 155497632 | 0; | ||
| 136 | + c = (c << 16 | c >>> 16) + d | 0; | ||
| 137 | + b += (c ^ d ^ a) + k[10] - 1094730640 | 0; | ||
| 138 | + b = (b << 23 | b >>> 9) + c | 0; | ||
| 139 | + a += (b ^ c ^ d) + k[13] + 681279174 | 0; | ||
| 140 | + a = (a << 4 | a >>> 28) + b | 0; | ||
| 141 | + d += (a ^ b ^ c) + k[0] - 358537222 | 0; | ||
| 142 | + d = (d << 11 | d >>> 21) + a | 0; | ||
| 143 | + c += (d ^ a ^ b) + k[3] - 722521979 | 0; | ||
| 144 | + c = (c << 16 | c >>> 16) + d | 0; | ||
| 145 | + b += (c ^ d ^ a) + k[6] + 76029189 | 0; | ||
| 146 | + b = (b << 23 | b >>> 9) + c | 0; | ||
| 147 | + a += (b ^ c ^ d) + k[9] - 640364487 | 0; | ||
| 148 | + a = (a << 4 | a >>> 28) + b | 0; | ||
| 149 | + d += (a ^ b ^ c) + k[12] - 421815835 | 0; | ||
| 150 | + d = (d << 11 | d >>> 21) + a | 0; | ||
| 151 | + c += (d ^ a ^ b) + k[15] + 530742520 | 0; | ||
| 152 | + c = (c << 16 | c >>> 16) + d | 0; | ||
| 153 | + b += (c ^ d ^ a) + k[2] - 995338651 | 0; | ||
| 154 | + b = (b << 23 | b >>> 9) + c | 0; | ||
| 155 | + // ii() | ||
| 156 | + a += (c ^ (b | ~d)) + k[0] - 198630844 | 0; | ||
| 157 | + a = (a << 6 | a >>> 26) + b | 0; | ||
| 158 | + d += (b ^ (a | ~c)) + k[7] + 1126891415 | 0; | ||
| 159 | + d = (d << 10 | d >>> 22) + a | 0; | ||
| 160 | + c += (a ^ (d | ~b)) + k[14] - 1416354905 | 0; | ||
| 161 | + c = (c << 15 | c >>> 17) + d | 0; | ||
| 162 | + b += (d ^ (c | ~a)) + k[5] - 57434055 | 0; | ||
| 163 | + b = (b << 21 | b >>> 11) + c | 0; | ||
| 164 | + a += (c ^ (b | ~d)) + k[12] + 1700485571 | 0; | ||
| 165 | + a = (a << 6 | a >>> 26) + b | 0; | ||
| 166 | + d += (b ^ (a | ~c)) + k[3] - 1894986606 | 0; | ||
| 167 | + d = (d << 10 | d >>> 22) + a | 0; | ||
| 168 | + c += (a ^ (d | ~b)) + k[10] - 1051523 | 0; | ||
| 169 | + c = (c << 15 | c >>> 17) + d | 0; | ||
| 170 | + b += (d ^ (c | ~a)) + k[1] - 2054922799 | 0; | ||
| 171 | + b = (b << 21 | b >>> 11) + c | 0; | ||
| 172 | + a += (c ^ (b | ~d)) + k[8] + 1873313359 | 0; | ||
| 173 | + a = (a << 6 | a >>> 26) + b | 0; | ||
| 174 | + d += (b ^ (a | ~c)) + k[15] - 30611744 | 0; | ||
| 175 | + d = (d << 10 | d >>> 22) + a | 0; | ||
| 176 | + c += (a ^ (d | ~b)) + k[6] - 1560198380 | 0; | ||
| 177 | + c = (c << 15 | c >>> 17) + d | 0; | ||
| 178 | + b += (d ^ (c | ~a)) + k[13] + 1309151649 | 0; | ||
| 179 | + b = (b << 21 | b >>> 11) + c | 0; | ||
| 180 | + a += (c ^ (b | ~d)) + k[4] - 145523070 | 0; | ||
| 181 | + a = (a << 6 | a >>> 26) + b | 0; | ||
| 182 | + d += (b ^ (a | ~c)) + k[11] - 1120210379 | 0; | ||
| 183 | + d = (d << 10 | d >>> 22) + a | 0; | ||
| 184 | + c += (a ^ (d | ~b)) + k[2] + 718787259 | 0; | ||
| 185 | + c = (c << 15 | c >>> 17) + d | 0; | ||
| 186 | + b += (d ^ (c | ~a)) + k[9] - 343485551 | 0; | ||
| 187 | + b = (b << 21 | b >>> 11) + c | 0; | ||
| 188 | + | ||
| 189 | + x[0] = a + x[0] | 0; | ||
| 190 | + x[1] = b + x[1] | 0; | ||
| 191 | + x[2] = c + x[2] | 0; | ||
| 192 | + x[3] = d + x[3] | 0; | ||
| 193 | + } | ||
| 194 | + | ||
| 195 | + private _dataLength: number; | ||
| 196 | + private _bufferLength: number; | ||
| 197 | + | ||
| 198 | + private _state: Int32Array = new Int32Array(4); | ||
| 199 | + private _buffer: ArrayBuffer = new ArrayBuffer(68); | ||
| 200 | + private _buffer8: Uint8Array; | ||
| 201 | + private _buffer32: Uint32Array; | ||
| 202 | + | ||
| 203 | + constructor() { | ||
| 204 | + this._buffer8 = new Uint8Array(this._buffer, 0, 68); | ||
| 205 | + this._buffer32 = new Uint32Array(this._buffer, 0, 17); | ||
| 206 | + this.start(); | ||
| 207 | + } | ||
| 208 | + | ||
| 209 | + public start() { | ||
| 210 | + this._dataLength = 0; | ||
| 211 | + this._bufferLength = 0; | ||
| 212 | + this._state.set(Md5.stateIdentity); | ||
| 213 | + return this; | ||
| 214 | + } | ||
| 215 | + | ||
| 216 | + // Char to code point to to array conversion: | ||
| 217 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt | ||
| 218 | + // #Example.3A_Fixing_charCodeAt_to_handle_non-Basic-Multilingual-Plane_characters_if_their_presence_earlier_in_the_string_is_unknown | ||
| 219 | + public appendStr(str: string) { | ||
| 220 | + const buf8 = this._buffer8; | ||
| 221 | + const buf32 = this._buffer32; | ||
| 222 | + let bufLen = this._bufferLength; | ||
| 223 | + let code; | ||
| 224 | + let i; | ||
| 225 | + | ||
| 226 | + for (i = 0; i < str.length; i += 1) { | ||
| 227 | + code = str.charCodeAt(i); | ||
| 228 | + if (code < 128) { | ||
| 229 | + buf8[bufLen++] = code; | ||
| 230 | + } else if (code < 0x800) { | ||
| 231 | + buf8[bufLen++] = (code >>> 6) + 0xC0; | ||
| 232 | + buf8[bufLen++] = code & 0x3F | 0x80; | ||
| 233 | + } else if (code < 0xD800 || code > 0xDBFF) { | ||
| 234 | + buf8[bufLen++] = (code >>> 12) + 0xE0; | ||
| 235 | + buf8[bufLen++] = (code >>> 6 & 0x3F) | 0x80; | ||
| 236 | + buf8[bufLen++] = (code & 0x3F) | 0x80; | ||
| 237 | + } else { | ||
| 238 | + code = ((code - 0xD800) * 0x400) + (str.charCodeAt(++i) - 0xDC00) + 0x10000; | ||
| 239 | + if (code > 0x10FFFF) { | ||
| 240 | + throw new Error('Unicode standard supports code points up to U+10FFFF'); | ||
| 241 | + } | ||
| 242 | + buf8[bufLen++] = (code >>> 18) + 0xF0; | ||
| 243 | + buf8[bufLen++] = (code >>> 12 & 0x3F) | 0x80; | ||
| 244 | + buf8[bufLen++] = (code >>> 6 & 0x3F) | 0x80; | ||
| 245 | + buf8[bufLen++] = (code & 0x3F) | 0x80; | ||
| 246 | + } | ||
| 247 | + if (bufLen >= 64) { | ||
| 248 | + this._dataLength += 64; | ||
| 249 | + Md5._md5cycle(this._state, buf32); | ||
| 250 | + bufLen -= 64; | ||
| 251 | + buf32[0] = buf32[16]; | ||
| 252 | + } | ||
| 253 | + } | ||
| 254 | + this._bufferLength = bufLen; | ||
| 255 | + return this; | ||
| 256 | + } | ||
| 257 | + | ||
| 258 | + public appendAsciiStr(str: string) { | ||
| 259 | + const buf8 = this._buffer8; | ||
| 260 | + const buf32 = this._buffer32; | ||
| 261 | + let bufLen = this._bufferLength; | ||
| 262 | + let i; | ||
| 263 | + let j = 0; | ||
| 264 | + | ||
| 265 | + for (; ;) { | ||
| 266 | + i = Math.min(str.length - j, 64 - bufLen); | ||
| 267 | + while (i--) { | ||
| 268 | + buf8[bufLen++] = str.charCodeAt(j++); | ||
| 269 | + } | ||
| 270 | + if (bufLen < 64) { | ||
| 271 | + break; | ||
| 272 | + } | ||
| 273 | + this._dataLength += 64; | ||
| 274 | + Md5._md5cycle(this._state, buf32); | ||
| 275 | + bufLen = 0; | ||
| 276 | + } | ||
| 277 | + this._bufferLength = bufLen; | ||
| 278 | + return this; | ||
| 279 | + } | ||
| 280 | + | ||
| 281 | + public appendByteArray(input: Uint8Array) { | ||
| 282 | + const buf8 = this._buffer8; | ||
| 283 | + const buf32 = this._buffer32; | ||
| 284 | + let bufLen = this._bufferLength; | ||
| 285 | + let i; | ||
| 286 | + let j = 0; | ||
| 287 | + | ||
| 288 | + for (; ;) { | ||
| 289 | + i = Math.min(input.length - j, 64 - bufLen); | ||
| 290 | + while (i--) { | ||
| 291 | + buf8[bufLen++] = input[j++]; | ||
| 292 | + } | ||
| 293 | + if (bufLen < 64) { | ||
| 294 | + break; | ||
| 295 | + } | ||
| 296 | + this._dataLength += 64; | ||
| 297 | + Md5._md5cycle(this._state, buf32); | ||
| 298 | + bufLen = 0; | ||
| 299 | + } | ||
| 300 | + this._bufferLength = bufLen; | ||
| 301 | + return this; | ||
| 302 | + } | ||
| 303 | + | ||
| 304 | + public getState() { | ||
| 305 | + const self = this; | ||
| 306 | + const s = self._state; | ||
| 307 | + | ||
| 308 | + return { | ||
| 309 | + buffer: String.fromCharCode.apply(null, self._buffer8), | ||
| 310 | + buflen: self._bufferLength, | ||
| 311 | + length: self._dataLength, | ||
| 312 | + state: [s[0], s[1], s[2], s[3]] | ||
| 313 | + }; | ||
| 314 | + } | ||
| 315 | + | ||
| 316 | + public setState(state: any) { | ||
| 317 | + const buf = state.buffer; | ||
| 318 | + const x = state.state; | ||
| 319 | + const s = this._state; | ||
| 320 | + let i; | ||
| 321 | + | ||
| 322 | + this._dataLength = state.length; | ||
| 323 | + this._bufferLength = state.buflen; | ||
| 324 | + s[0] = x[0]; | ||
| 325 | + s[1] = x[1]; | ||
| 326 | + s[2] = x[2]; | ||
| 327 | + s[3] = x[3]; | ||
| 328 | + | ||
| 329 | + for (i = 0; i < buf.length; i += 1) { | ||
| 330 | + this._buffer8[i] = buf.charCodeAt(i); | ||
| 331 | + } | ||
| 332 | + } | ||
| 333 | + | ||
| 334 | + public end(raw: boolean = false) { | ||
| 335 | + const bufLen = this._bufferLength; | ||
| 336 | + const buf8 = this._buffer8; | ||
| 337 | + const buf32 = this._buffer32; | ||
| 338 | + const i = (bufLen >> 2) + 1; | ||
| 339 | + let dataBitsLen; | ||
| 340 | + | ||
| 341 | + this._dataLength += bufLen; | ||
| 342 | + | ||
| 343 | + buf8[bufLen] = 0x80; | ||
| 344 | + buf8[bufLen + 1] = buf8[bufLen + 2] = buf8[bufLen + 3] = 0; | ||
| 345 | + buf32.set(Md5.buffer32Identity.subarray(i), i); | ||
| 346 | + | ||
| 347 | + if (bufLen > 55) { | ||
| 348 | + Md5._md5cycle(this._state, buf32); | ||
| 349 | + buf32.set(Md5.buffer32Identity); | ||
| 350 | + } | ||
| 351 | + | ||
| 352 | + // Do the final computation based on the tail and length | ||
| 353 | + // Beware that the final length may not fit in 32 bits so we take care of that | ||
| 354 | + dataBitsLen = this._dataLength * 8; | ||
| 355 | + if (dataBitsLen <= 0xFFFFFFFF) { | ||
| 356 | + buf32[14] = dataBitsLen; | ||
| 357 | + } else { | ||
| 358 | + const matches = dataBitsLen.toString(16).match(/(.*?)(.{0,8})$/); | ||
| 359 | + if (matches === null) { | ||
| 360 | + return; | ||
| 361 | + } | ||
| 362 | + | ||
| 363 | + const lo = parseInt(matches[2], 16); | ||
| 364 | + const hi = parseInt(matches[1], 16) || 0; | ||
| 365 | + | ||
| 366 | + buf32[14] = lo; | ||
| 367 | + buf32[15] = hi; | ||
| 368 | + } | ||
| 369 | + | ||
| 370 | + Md5._md5cycle(this._state, buf32); | ||
| 371 | + | ||
| 372 | + return raw ? this._state : Md5._hex(this._state); | ||
| 373 | + } | ||
| 374 | +} | ||
| 375 | + | ||
| 376 | +if (Md5.hashStr('hello') !== '5d41402abc4b2a76b9719d911017c592') { | ||
| 377 | + console.error('Md5 self test failed.'); | ||
| 378 | +} | ||
| 0 | \ No newline at end of file | 379 | \ No newline at end of file |
| @@ -0,0 +1,723 @@ | @@ -0,0 +1,723 @@ | ||
| 1 | + | ||
| 2 | +interface _OnlineInterface { | ||
| 3 | + /** | ||
| 4 | + * 请求在线参数 | ||
| 5 | + * 在游戏启动时候请求在线参数,最早的获取得的在线参数。 | ||
| 6 | + */ | ||
| 7 | + updateOnlineConfig(): Promise<any>; | ||
| 8 | + | ||
| 9 | + /** | ||
| 10 | + * 获取在线参数 | ||
| 11 | + * @param key 在线参数key | ||
| 12 | + */ | ||
| 13 | + getParams(key: string): any; | ||
| 14 | + | ||
| 15 | + /** | ||
| 16 | + * 获取对象数值在线配置参数 | ||
| 17 | + * @param key 在线参数key | ||
| 18 | + * @param defaultVal 获取不到使用默认值 | ||
| 19 | + */ | ||
| 20 | + getParamsObj(key: string, defaultVal?: any); | ||
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * 获取数字在线配置参数 | ||
| 24 | + * @param key 在线参数key | ||
| 25 | + * @param defaultVal 获取不到使用默认值 | ||
| 26 | + */ | ||
| 27 | + getParamsInt(key: string, defaultVal?: number): number; | ||
| 28 | + | ||
| 29 | + /** | ||
| 30 | + * 获取字符串在线配置参数 | ||
| 31 | + * @param key 在线参数key | ||
| 32 | + * @param defaultVal 获取不到使用默认值 | ||
| 33 | + */ | ||
| 34 | + getParamsString(key: string, defaultVal?: string): string; | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +interface _ConfigInterface { | ||
| 38 | + /** | ||
| 39 | + * 合并覆盖配置:合并config.js配置信息 | ||
| 40 | + * @param conf Object 配置信息 | ||
| 41 | + */ | ||
| 42 | + merge(conf: any): void; | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +interface _StorageInterface { | ||
| 46 | + /** | ||
| 47 | + * 设置本地存储 | ||
| 48 | + * @param expiration 过期时间(秒) | ||
| 49 | + */ | ||
| 50 | + set(key: string, value: Object, expiration?: number): void; | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * 获取缓存 | ||
| 54 | + * @param key | ||
| 55 | + */ | ||
| 56 | + get(key: string): any; | ||
| 57 | + | ||
| 58 | + /** | ||
| 59 | + * 移除缓存 | ||
| 60 | + * @param key | ||
| 61 | + */ | ||
| 62 | + remove(key: string): void; | ||
| 63 | + | ||
| 64 | + /** | ||
| 65 | + * 清除缓存 | ||
| 66 | + */ | ||
| 67 | + clear(): void; | ||
| 68 | + | ||
| 69 | + /** | ||
| 70 | + * 判断缓存是否存在 | ||
| 71 | + * @param key | ||
| 72 | + */ | ||
| 73 | + isExist(key: string): boolean; | ||
| 74 | +} | ||
| 75 | + | ||
| 76 | +interface _PlatformInterface { | ||
| 77 | + /** | ||
| 78 | + * 创建授权按钮 | ||
| 79 | + * @param type 页面类型 | ||
| 80 | + * @param params 按钮样式,参照微信授权按钮参数 | ||
| 81 | + * @param opts {Object} | ||
| 82 | + * { | ||
| 83 | + * isDebug:boolean 是否是调试模式 | ||
| 84 | + * callback:(status: number, ret: _GetUserInfoSuccessObject | null ) => void | ||
| 85 | + * 回调函数 | ||
| 86 | + * 参数status: 1(授权成功) 0(授权失败) -1(取消授权)取消授权会弹出是否调整到设置界面设置 | ||
| 87 | + * 参数ret:当status状态为1的时候返回wx.getUserInfo返回的数据信息_GetUserInfoSuccessObject,status为0或者-1返回null | ||
| 88 | + * context:any 函数执行上下文 | ||
| 89 | + * } | ||
| 90 | + */ | ||
| 91 | + createUserBtn( | ||
| 92 | + type: string, | ||
| 93 | + params: Array<_UserInfoButtonObject> | _UserInfoButtonObject, | ||
| 94 | + opts: { isDebug?: boolean; callback: (status: number, ret: _GetUserInfoSuccessObject | null) => void; context: any } | ||
| 95 | + ): void; | ||
| 96 | + | ||
| 97 | + /** | ||
| 98 | + * 根据类型显示授权按钮 | ||
| 99 | + * @param type | ||
| 100 | + */ | ||
| 101 | + showUserBtn(type: string): void; | ||
| 102 | + | ||
| 103 | + /** | ||
| 104 | + * 根据类型隐藏授权按钮 | ||
| 105 | + * @param type | ||
| 106 | + */ | ||
| 107 | + hideUserBtn(type: string): void; | ||
| 108 | + | ||
| 109 | + /** | ||
| 110 | + * 根据类型销毁授权按钮 | ||
| 111 | + * @param type | ||
| 112 | + */ | ||
| 113 | + destoryUserBtn(type: string): void; | ||
| 114 | + | ||
| 115 | + /** | ||
| 116 | + * 销毁所有授权按钮 | ||
| 117 | + */ | ||
| 118 | + destoryAllUserBtn(): void; | ||
| 119 | + | ||
| 120 | + /** | ||
| 121 | + * 打开客服消息 | ||
| 122 | + */ | ||
| 123 | + openCustomerServiceConversation(params?: _CustomerServiceConversationObject): number; | ||
| 124 | + | ||
| 125 | + /** | ||
| 126 | + * 检测更新 | ||
| 127 | + * @param data 参数与wx.showModal一模一样,可不传递,不传递更新状态下弹出默认的弹出框:title=>更新提示 content=>新版本已经准备好,是否重启应用? | ||
| 128 | + */ | ||
| 129 | + checkUpdate(data?: _ShowModalObject): void; | ||
| 130 | + | ||
| 131 | + /** | ||
| 132 | + * 长震动 | ||
| 133 | + */ | ||
| 134 | + vibrateLong(): Promise<any>; | ||
| 135 | + | ||
| 136 | + /** | ||
| 137 | + * 短震动 | ||
| 138 | + */ | ||
| 139 | + vibrateShort(): Promise<any>; | ||
| 140 | + | ||
| 141 | + /** | ||
| 142 | + * 开启关闭群排行 | ||
| 143 | + * @param val | ||
| 144 | + */ | ||
| 145 | + updateShareMenu(val: boolean): void; | ||
| 146 | + | ||
| 147 | + /** | ||
| 148 | + * 显示模态弹出框 | ||
| 149 | + * @param data | ||
| 150 | + */ | ||
| 151 | + showModal(data: _ShowModalObject): void; | ||
| 152 | + | ||
| 153 | + /** | ||
| 154 | + * 复制文本 | ||
| 155 | + * @param str | ||
| 156 | + */ | ||
| 157 | + copy(str: string): Promise<any>; | ||
| 158 | + | ||
| 159 | + /** | ||
| 160 | + * 多平台发起支付 | ||
| 161 | + * @param params {Object} | ||
| 162 | + * { | ||
| 163 | + * money: 支付花费钱,单位元 | ||
| 164 | + * desc: 支付道具的描述 | ||
| 165 | + * } | ||
| 166 | + */ | ||
| 167 | + pay(params: { money: number; desc: string }); | ||
| 168 | + | ||
| 169 | + | ||
| 170 | + logPay(params: { type: number; source: string; amount: number; buy_id: string | number; buy_name: string; item_info: string }); | ||
| 171 | + | ||
| 172 | + /** | ||
| 173 | + * 订阅消息api | ||
| 174 | + * @param tmplIds Array<string> 需要订阅的消息模板的id的集合,格式为:["a","b"] | ||
| 175 | + */ | ||
| 176 | + subScribe(tmplIds: Array<string>): Promise<any>; | ||
| 177 | + | ||
| 178 | + /** | ||
| 179 | + * 获取视频是否正在播放中 | ||
| 180 | + */ | ||
| 181 | + isVideoPlaying(): boolean; | ||
| 182 | + | ||
| 183 | + /** | ||
| 184 | + * 获取小游戏推荐弹窗组件GamePortal是否正在加载中 | ||
| 185 | + */ | ||
| 186 | + isGamePortalPlaying(): boolean; | ||
| 187 | + | ||
| 188 | + /** | ||
| 189 | + * 获取插屏广告 or 推荐弹窗组件 是否正在加载中 | ||
| 190 | + */ | ||
| 191 | + isInterstitialPlaying(): boolean; | ||
| 192 | + | ||
| 193 | + /** | ||
| 194 | + * 显示插屏广告 | ||
| 195 | + * @param adKey 广告adUnitId | ||
| 196 | + * @param opts 扩展参数 | ||
| 197 | + */ | ||
| 198 | + interstitialShow(adUnitId: string, opts?: any): Promise<any>; | ||
| 199 | + | ||
| 200 | + /** | ||
| 201 | + * 显示插屏广告 or 小游戏推荐弹窗组件 | ||
| 202 | + * @param adKey 广告adUnitId | ||
| 203 | + * @param opts 扩展参数 | ||
| 204 | + */ | ||
| 205 | + gamePortalShow(adUnitId: string, opts?: any): Promise<any>; | ||
| 206 | + | ||
| 207 | + /** | ||
| 208 | + * 创建并显示小游戏推荐icon组件 | ||
| 209 | + * @param adKey 广告adUnitId | ||
| 210 | + * @param opts 扩展参数,创建数量和样式 | ||
| 211 | + */ | ||
| 212 | + gameIconShow(adUnitId: string, opts: { count: number, style: Array<_GameIconStyleItem> }): Promise<any>; | ||
| 213 | + | ||
| 214 | + gameIconDestroy(): void; | ||
| 215 | + | ||
| 216 | + /** | ||
| 217 | + * banner广告组件api | ||
| 218 | + */ | ||
| 219 | + banner: _BannerInterface; | ||
| 220 | + | ||
| 221 | + /** | ||
| 222 | + * 获取系统信息 | ||
| 223 | + */ | ||
| 224 | + getSystemData(): SystemInfoSyncReturnValue; | ||
| 225 | + | ||
| 226 | + /** | ||
| 227 | + * 获取启动信息 | ||
| 228 | + */ | ||
| 229 | + getLaunchData(): LaunchInfoSyncReturnValue; | ||
| 230 | +} | ||
| 231 | + | ||
| 232 | +interface SystemInfoSyncReturnValue { | ||
| 233 | + /** | ||
| 234 | + * 手机品牌 | ||
| 235 | + */ | ||
| 236 | + brand: any; | ||
| 237 | + | ||
| 238 | + /** | ||
| 239 | + * 手机型号 | ||
| 240 | + */ | ||
| 241 | + model: any; | ||
| 242 | + | ||
| 243 | + /** | ||
| 244 | + * 设备像素比 | ||
| 245 | + */ | ||
| 246 | + pixelRatio: any; | ||
| 247 | + | ||
| 248 | + /** | ||
| 249 | + * 屏幕宽度 | ||
| 250 | + */ | ||
| 251 | + screenWidth: any; | ||
| 252 | + | ||
| 253 | + /** | ||
| 254 | + * 屏幕高度 | ||
| 255 | + */ | ||
| 256 | + screenHeight: any; | ||
| 257 | + | ||
| 258 | + /** | ||
| 259 | + * 可使用窗口宽度 | ||
| 260 | + */ | ||
| 261 | + windowWidth: any; | ||
| 262 | + | ||
| 263 | + /** | ||
| 264 | + * 可使用窗口高度 | ||
| 265 | + */ | ||
| 266 | + windowHeight: any; | ||
| 267 | + | ||
| 268 | + /** | ||
| 269 | + * 状态栏的高度 | ||
| 270 | + */ | ||
| 271 | + statusBarHeight: any; | ||
| 272 | + | ||
| 273 | + /** | ||
| 274 | + * 微信设置的语言 | ||
| 275 | + */ | ||
| 276 | + language: any; | ||
| 277 | + | ||
| 278 | + /** | ||
| 279 | + * 微信版本号 | ||
| 280 | + */ | ||
| 281 | + version: any; | ||
| 282 | + | ||
| 283 | + /** | ||
| 284 | + * 操作系统版本 | ||
| 285 | + */ | ||
| 286 | + system: any; | ||
| 287 | + | ||
| 288 | + /** | ||
| 289 | + * 客户端平台 | ||
| 290 | + */ | ||
| 291 | + platform: any; | ||
| 292 | + | ||
| 293 | + /** | ||
| 294 | + * 用户字体大小设置。以“我-设置-通用-字体大小”中的设置为准,单位:px | ||
| 295 | + */ | ||
| 296 | + fontSizeSetting: any; | ||
| 297 | + | ||
| 298 | + /** | ||
| 299 | + * 客户端基础库版本 | ||
| 300 | + */ | ||
| 301 | + SDKVersion: any; | ||
| 302 | +} | ||
| 303 | + | ||
| 304 | +interface LaunchInfoSyncReturnValue { | ||
| 305 | + scene: number; | ||
| 306 | + | ||
| 307 | + query: any; | ||
| 308 | + | ||
| 309 | + shareTicket: string; | ||
| 310 | + | ||
| 311 | + referrerInfo: _LaunchInfoReferrerInfo; | ||
| 312 | +} | ||
| 313 | + | ||
| 314 | +interface _LaunchInfoReferrerInfo { | ||
| 315 | + /** | ||
| 316 | + * 来源小程序、公众号或 App 的 appId | ||
| 317 | + */ | ||
| 318 | + appId: string; | ||
| 319 | + | ||
| 320 | + /** | ||
| 321 | + * 来源小程序传过来的数据,scene = 1037或1038时支持 | ||
| 322 | + */ | ||
| 323 | + extraData: any; | ||
| 324 | +} | ||
| 325 | + | ||
| 326 | +interface _BannerInterface { | ||
| 327 | + /** | ||
| 328 | + * 初始化设置宽度 建议初始化调用 | ||
| 329 | + * @param designWidth 游戏设计宽度,默认宽度:750 | ||
| 330 | + * @param bannerWidth banner显示宽度,默认宽度:750 | ||
| 331 | + */ | ||
| 332 | + setWidth(designWidth: number, bannerWidth: number); | ||
| 333 | + | ||
| 334 | + /** | ||
| 335 | + * 加载banner广告 | ||
| 336 | + * @param adUnitId 广告的unitId,微信后台申请提供 | ||
| 337 | + * @param opts {Object} | ||
| 338 | + * { | ||
| 339 | + * designWidth: 游戏设计宽度,默认宽度:750 同setWidth api的designWidth参数 | ||
| 340 | + * bannerWidth banner显示宽度,默认宽度:750 同setWidth api的bannerWidth参数 | ||
| 341 | + * } | ||
| 342 | + */ | ||
| 343 | + load(adUnitId: string, opts?: { type?: number; designWidth?: number; bannerWidth?: number }): void; | ||
| 344 | + | ||
| 345 | + /** | ||
| 346 | + * 显示banner广告 | ||
| 347 | + */ | ||
| 348 | + show(adUnitId: string): void; | ||
| 349 | + | ||
| 350 | + /** | ||
| 351 | + * 隐藏banner广告 | ||
| 352 | + */ | ||
| 353 | + hide(adUnitId: string): void; | ||
| 354 | + | ||
| 355 | + /** | ||
| 356 | + * 切换显示和隐藏banner广告 | ||
| 357 | + * @param isshow 是否显示/隐藏 true为显示 false为隐藏 | ||
| 358 | + */ | ||
| 359 | + toggle(adUnitId: string, isshow: boolean): void; | ||
| 360 | + | ||
| 361 | + /** | ||
| 362 | + * 销毁banner广告 | ||
| 363 | + */ | ||
| 364 | + destory(adUnitId: string): void; | ||
| 365 | + | ||
| 366 | + /** | ||
| 367 | + * 清除所有banner定时器 | ||
| 368 | + */ | ||
| 369 | + clear(): void; | ||
| 370 | +} | ||
| 371 | + | ||
| 372 | +/** | ||
| 373 | + * 内部游戏选传传信息 | ||
| 374 | + */ | ||
| 375 | +interface _LoginInnerData { | ||
| 376 | + channel?: number | string; | ||
| 377 | + userId?: number | string; | ||
| 378 | + regTime?: number | string; | ||
| 379 | + openId?: string; | ||
| 380 | + token?:string; | ||
| 381 | + refToken?:string; | ||
| 382 | + expire?:number; | ||
| 383 | + isnew?:number | ||
| 384 | +} | ||
| 385 | + | ||
| 386 | +/** | ||
| 387 | + * 外部游戏必传openId和regTime | ||
| 388 | + */ | ||
| 389 | +interface _LoginOuterData { | ||
| 390 | + openId: string; | ||
| 391 | + regTime: number | string; | ||
| 392 | +} | ||
| 393 | + | ||
| 394 | +interface _LogLevelData { | ||
| 395 | + /** | ||
| 396 | + * 关卡ID(或者合成项目的等级) | ||
| 397 | + */ | ||
| 398 | + id: string | number; | ||
| 399 | + | ||
| 400 | + /** | ||
| 401 | + * 玩法类型(-1老版本闯关,0新版闯关,1合成) | ||
| 402 | + */ | ||
| 403 | + ptype: number; | ||
| 404 | + | ||
| 405 | + /** | ||
| 406 | + * 战斗属性(例如枪的等级) | ||
| 407 | + */ | ||
| 408 | + attr?: number; | ||
| 409 | + | ||
| 410 | + /** | ||
| 411 | + * 操作次数 | ||
| 412 | + */ | ||
| 413 | + anum?: number; | ||
| 414 | + | ||
| 415 | + /** | ||
| 416 | + * 是否使用复活,rnum传递true,表示使用复活,复活次数+1;传递false或者不传递没有使用复活,复活次数不变 | ||
| 417 | + */ | ||
| 418 | + rnum?: boolean; | ||
| 419 | + | ||
| 420 | + /** | ||
| 421 | + * 耗时,单位秒 | ||
| 422 | + */ | ||
| 423 | + ctime?: number; | ||
| 424 | + | ||
| 425 | + /** | ||
| 426 | + * 是否第一次体验关卡,1是,0否 | ||
| 427 | + */ | ||
| 428 | + first?: number; | ||
| 429 | + | ||
| 430 | + /** | ||
| 431 | + * 结束类型,1、通关;2、失败;3、主动退出-onhide | ||
| 432 | + */ | ||
| 433 | + type?: number; | ||
| 434 | + | ||
| 435 | + /** | ||
| 436 | + * 扩展参数:{ key1:val1, key2:val2 }的格式 | ||
| 437 | + */ | ||
| 438 | + ext?: any; | ||
| 439 | +} | ||
| 440 | + | ||
| 441 | +interface _LogResData { | ||
| 442 | + /** | ||
| 443 | + * 资源ID,客户端自定义配置 | ||
| 444 | + */ | ||
| 445 | + id: string | number; | ||
| 446 | + | ||
| 447 | + /** | ||
| 448 | + * 资源名称,客户端自定义配置 | ||
| 449 | + */ | ||
| 450 | + name: string; | ||
| 451 | + | ||
| 452 | + /** | ||
| 453 | + * 类型,0减少,1增加 | ||
| 454 | + */ | ||
| 455 | + type: number; | ||
| 456 | + | ||
| 457 | + /** | ||
| 458 | + * 变化数量 | ||
| 459 | + */ | ||
| 460 | + cnum: number; | ||
| 461 | + | ||
| 462 | + /** | ||
| 463 | + * 变化后剩余数量 | ||
| 464 | + */ | ||
| 465 | + onum: number; | ||
| 466 | + | ||
| 467 | + /** | ||
| 468 | + * 关卡id | ||
| 469 | + */ | ||
| 470 | + levelid: number; | ||
| 471 | + | ||
| 472 | + /** | ||
| 473 | + * 变化原因,客户端自定义配置 | ||
| 474 | + */ | ||
| 475 | + reason: string; | ||
| 476 | +} | ||
| 477 | + | ||
| 478 | +interface _ExposureData { | ||
| 479 | + banner_id: string | number; | ||
| 480 | +} | ||
| 481 | + | ||
| 482 | +interface _RefererInfoData { | ||
| 483 | + /** | ||
| 484 | + * 来源小程序、公众号或 App 的 appId | ||
| 485 | + */ | ||
| 486 | + appId: string; | ||
| 487 | + | ||
| 488 | + /** | ||
| 489 | + * 来源小程序传过来的数据,scene=1037或1038时支持 | ||
| 490 | + */ | ||
| 491 | + extData: any; | ||
| 492 | +} | ||
| 493 | + | ||
| 494 | +interface _UserInfo { | ||
| 495 | + /** | ||
| 496 | + * 用户昵称 | ||
| 497 | + */ | ||
| 498 | + nickName: string; | ||
| 499 | + | ||
| 500 | + /** | ||
| 501 | + * 用户头像图片的 URL。URL 最后一个数值代表正方形头像大小(有 0、46、64、96、132 数值可选,0 代表 640x640 的正方形头像, | ||
| 502 | + * 46 表示 46x46 的正方形头像,剩余数值以此类推。默认132),用户没有头像时该项为空。若用户更换头像,原有头像 URL 将失效。 | ||
| 503 | + */ | ||
| 504 | + avatarUrl: string; | ||
| 505 | + | ||
| 506 | + /** | ||
| 507 | + * 用户性别,gender 的合法值 0:未知 1:男性 2:女性 | ||
| 508 | + */ | ||
| 509 | + gender: number; | ||
| 510 | + | ||
| 511 | + /** | ||
| 512 | + * 用户所在国家 | ||
| 513 | + */ | ||
| 514 | + country: string; | ||
| 515 | + | ||
| 516 | + /** | ||
| 517 | + * 用户所在身份 | ||
| 518 | + */ | ||
| 519 | + province: string; | ||
| 520 | + | ||
| 521 | + /** | ||
| 522 | + * 用户所在城市 | ||
| 523 | + */ | ||
| 524 | + city: string; | ||
| 525 | + | ||
| 526 | + /** | ||
| 527 | + * 显示 country,province,city 所用的语言 | ||
| 528 | + * language 的合法值 en:英文 zh_CN:简体中文 zh_TW:繁体中文 | ||
| 529 | + */ | ||
| 530 | + language: string; | ||
| 531 | +} | ||
| 532 | + | ||
| 533 | +interface _ShowModalObject { | ||
| 534 | + title?: string; | ||
| 535 | + | ||
| 536 | + content?: string; | ||
| 537 | + | ||
| 538 | + showCancel?: boolean; | ||
| 539 | + | ||
| 540 | + cancelText?: string; | ||
| 541 | + | ||
| 542 | + cancelColor?: string; | ||
| 543 | + | ||
| 544 | + confirmText?: string; | ||
| 545 | + | ||
| 546 | + confirmColor?: string; | ||
| 547 | + | ||
| 548 | + success?: (result: _ShowModalSuccessObject) => void; | ||
| 549 | + | ||
| 550 | + fail?: (err) => void; | ||
| 551 | + | ||
| 552 | + complete?: () => void; | ||
| 553 | +} | ||
| 554 | + | ||
| 555 | +interface _ShowModalSuccessObject { | ||
| 556 | + /** | ||
| 557 | + * 为 true 时,表示用户点击了确定按钮 | ||
| 558 | + */ | ||
| 559 | + confirm: boolean; | ||
| 560 | + | ||
| 561 | + /** | ||
| 562 | + * 为 true 时,表示用户点击了取消(用于 Android 系统区分点击蒙层关闭还是点击取消按钮关闭) | ||
| 563 | + */ | ||
| 564 | + cancel: boolean; | ||
| 565 | +} | ||
| 566 | + | ||
| 567 | + | ||
| 568 | +interface _GetUserInfoSuccessObject { | ||
| 569 | + /** | ||
| 570 | + * 用户信息对象,不包含 openid 等敏感信息 | ||
| 571 | + */ | ||
| 572 | + userInfo: _UserInfo; | ||
| 573 | + | ||
| 574 | + /** | ||
| 575 | + * 不包括敏感信息的原始数据字符串,用于计算签名。 | ||
| 576 | + */ | ||
| 577 | + rawData: string; | ||
| 578 | + | ||
| 579 | + /** | ||
| 580 | + * 使用 sha1( rawData + sessionkey ) 得到字符串,用于校验用户信息,参考文档 [signature](./signature.md)。 | ||
| 581 | + */ | ||
| 582 | + signature: string; | ||
| 583 | + | ||
| 584 | + /** | ||
| 585 | + * 包括敏感数据在内的完整用户信息的加密数据,详细见[加密数据解密算法](./signature.md#加密数据解密算法) | ||
| 586 | + */ | ||
| 587 | + encryptedData: string; | ||
| 588 | + | ||
| 589 | + /** | ||
| 590 | + * 加密算法的初始向量,详细见[加密数据解密算法](./signature.md#加密数据解密算法) | ||
| 591 | + */ | ||
| 592 | + iv: string; | ||
| 593 | +} | ||
| 594 | + | ||
| 595 | +interface _UserInfoButtonObject { | ||
| 596 | + /** | ||
| 597 | + * 按钮的类型 | ||
| 598 | + */ | ||
| 599 | + type: string; | ||
| 600 | + | ||
| 601 | + /** | ||
| 602 | + * 按钮上的文本,仅当 type 为 text 时有效 | ||
| 603 | + */ | ||
| 604 | + text?: string; | ||
| 605 | + | ||
| 606 | + /** | ||
| 607 | + * 按钮的背景图片,仅当 type 为 image 时有效 | ||
| 608 | + */ | ||
| 609 | + image?: string; | ||
| 610 | + | ||
| 611 | + /** | ||
| 612 | + * 按钮的样式 | ||
| 613 | + */ | ||
| 614 | + style: _ButtonStyle; | ||
| 615 | + | ||
| 616 | + /** | ||
| 617 | + * 是否带上登录态信息。当 withCredentials 为 true 时,要求此前有调用过 wx.login 且登录态尚未过期, | ||
| 618 | + * 此时返回的数据会包含 encryptedData, iv 等敏感信息;当 withCredentials 为 false 时,不要求有登录态, | ||
| 619 | + * 返回的数据不包含 encryptedData, iv 等敏感信息。 | ||
| 620 | + */ | ||
| 621 | + withCredentials?: boolean; | ||
| 622 | +} | ||
| 623 | + | ||
| 624 | +interface _ButtonStyle { | ||
| 625 | + left: number; | ||
| 626 | + | ||
| 627 | + top: number; | ||
| 628 | + | ||
| 629 | + width: number; | ||
| 630 | + | ||
| 631 | + height: number; | ||
| 632 | + | ||
| 633 | + backgroundColor?: string; | ||
| 634 | + | ||
| 635 | + borderColor?: string; | ||
| 636 | + | ||
| 637 | + textAlign?: string; | ||
| 638 | + | ||
| 639 | + borderWidth?: number; | ||
| 640 | + | ||
| 641 | + borderRadius?: number; | ||
| 642 | + | ||
| 643 | + fontSize?: number; | ||
| 644 | + | ||
| 645 | + lineHeight?: number; | ||
| 646 | +} | ||
| 647 | + | ||
| 648 | +interface _CustomerServiceConversationObject { | ||
| 649 | + /** | ||
| 650 | + * 充值档位id | ||
| 651 | + */ | ||
| 652 | + itemId?: string | number; | ||
| 653 | + | ||
| 654 | + /** | ||
| 655 | + * 会话来源 | ||
| 656 | + */ | ||
| 657 | + sessionFrom?: string; | ||
| 658 | + | ||
| 659 | + /** | ||
| 660 | + * false 否 是否显示会话内消息卡片,设置此参数为 true,用户进入客服会话会在右下角显示"可能要发送的小程序"提示,用户点击后可以快速发送小程序消息 | ||
| 661 | + */ | ||
| 662 | + showMessageCard?: boolean; | ||
| 663 | + | ||
| 664 | + /** | ||
| 665 | + * 会话内消息卡片标题 | ||
| 666 | + */ | ||
| 667 | + sendMessageTitle?: string; | ||
| 668 | + | ||
| 669 | + /** | ||
| 670 | + * 会话内消息卡片路径 | ||
| 671 | + */ | ||
| 672 | + sendMessagePath?: string; | ||
| 673 | + | ||
| 674 | + /** | ||
| 675 | + * 会话内消息卡片图片路径 | ||
| 676 | + */ | ||
| 677 | + sendMessageImg?: string; | ||
| 678 | + | ||
| 679 | + /** | ||
| 680 | + * 接口调用成功的回调函数 | ||
| 681 | + */ | ||
| 682 | + success?: (ret?: any) => void; | ||
| 683 | + | ||
| 684 | + /** | ||
| 685 | + * 接口调用失败的回调函数 | ||
| 686 | + */ | ||
| 687 | + fail?: (err: any) => void; | ||
| 688 | + | ||
| 689 | + /** | ||
| 690 | + * 接口调用结束的回调函数(调用成功、失败都会执行) | ||
| 691 | + */ | ||
| 692 | + complete?: () => void; | ||
| 693 | +} | ||
| 694 | + | ||
| 695 | +interface _GameIconStyleItem { | ||
| 696 | + // 游戏名称是否隐藏 | ||
| 697 | + appNameHidden: boolean; | ||
| 698 | + // 游戏名称的颜色色值 | ||
| 699 | + color: string; | ||
| 700 | + // 游戏icon的宽高值 | ||
| 701 | + size: number; | ||
| 702 | + // 游戏icon的border尺寸 | ||
| 703 | + borderWidth: number; | ||
| 704 | + // 游戏icon的border颜色色值 | ||
| 705 | + borderColor: string; | ||
| 706 | + // 游戏icon的X轴坐标 | ||
| 707 | + left: number; | ||
| 708 | + // 游戏icon的Y轴坐标 | ||
| 709 | + top: number; | ||
| 710 | +} | ||
| 711 | + | ||
| 712 | +interface _adStat { | ||
| 713 | + // 游戏id | ||
| 714 | + game_id: number, | ||
| 715 | + // openid | ||
| 716 | + openid: string, | ||
| 717 | + // 渠道id | ||
| 718 | + channel_id: number, | ||
| 719 | + // 注册时间 | ||
| 720 | + reg_time: number, | ||
| 721 | + // 类型 默认0 | ||
| 722 | + type: number, | ||
| 723 | +} | ||
| 0 | \ No newline at end of file | 724 | \ No newline at end of file |
| @@ -0,0 +1,30 @@ | @@ -0,0 +1,30 @@ | ||
| 1 | +import BaseIStorage from "../base/BaseIStorage"; | ||
| 2 | + | ||
| 3 | +export default class Storage implements BaseIStorage { | ||
| 4 | + getItem(key: string) { | ||
| 5 | + return localStorage.getItem(key) || ''; | ||
| 6 | + } | ||
| 7 | + | ||
| 8 | + setItem(key: string, value: string) { | ||
| 9 | + localStorage.setItem(key, value); | ||
| 10 | + } | ||
| 11 | + | ||
| 12 | + removeItem(key: string) { | ||
| 13 | + localStorage.removeItem(key); | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + clear() { | ||
| 17 | + localStorage.clear(); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + hasKey(key: string): boolean { | ||
| 21 | + let len = localStorage.length; | ||
| 22 | + if (len) { | ||
| 23 | + for (let i = 0; i < len; i++) { | ||
| 24 | + let find = localStorage.key(i); | ||
| 25 | + if (key === find) return true; | ||
| 26 | + } | ||
| 27 | + } | ||
| 28 | + return false; | ||
| 29 | + } | ||
| 30 | +} | ||
| 0 | \ No newline at end of file | 31 | \ No newline at end of file |
| @@ -0,0 +1,28 @@ | @@ -0,0 +1,28 @@ | ||
| 1 | +import BaseIStorage from "../../base/BaseIStorage"; | ||
| 2 | + | ||
| 3 | +export default class WxStorage implements BaseIStorage { | ||
| 4 | + getItem(key: string) { | ||
| 5 | + return wx.getStorageSync(key); | ||
| 6 | + } | ||
| 7 | + | ||
| 8 | + setItem(key: string, value: string) { | ||
| 9 | + wx.setStorageSync(key, value); | ||
| 10 | + } | ||
| 11 | + | ||
| 12 | + removeItem(key: string) { | ||
| 13 | + wx.removeStorageSync(key); | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + clear() { | ||
| 17 | + wx.clearStorageSync(); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + hasKey(key: string): boolean { | ||
| 21 | + let keys = wx.getStorageInfoSync().keys; | ||
| 22 | + let finds: string[] = []; | ||
| 23 | + if (keys && keys.length) | ||
| 24 | + finds = keys.filter((k: string) => k === key); | ||
| 25 | + | ||
| 26 | + return finds.length !== 0; | ||
| 27 | + } | ||
| 28 | +} | ||
| 0 | \ No newline at end of file | 29 | \ No newline at end of file |
| @@ -0,0 +1,9 @@ | @@ -0,0 +1,9 @@ | ||
| 1 | +{ | ||
| 2 | + "ver": "1.0.8", | ||
| 3 | + "uuid": "6013014c-7aa3-4eb4-b818-583d937a9f05", | ||
| 4 | + "isPlugin": false, | ||
| 5 | + "loadPluginInWeb": true, | ||
| 6 | + "loadPluginInNative": true, | ||
| 7 | + "loadPluginInEditor": false, | ||
| 8 | + "subMetas": {} | ||
| 9 | +} | ||
| 0 | \ No newline at end of file | 10 | \ No newline at end of file |
| @@ -0,0 +1,126 @@ | @@ -0,0 +1,126 @@ | ||
| 1 | +import WxApi from "../wx/WxApi"; | ||
| 2 | +import WxBanner from "../wx/WxBanner"; | ||
| 3 | +import WxGrid from "../wx/WxGrid"; | ||
| 4 | +import WxCustom from "../wx/WxCustom"; | ||
| 5 | +import WxInterstitial from "../wx/WxInterstitial"; | ||
| 6 | +import { GAMEDATA } from "../base/SDKConst"; | ||
| 7 | +import LogService from "./LogService"; | ||
| 8 | + | ||
| 9 | + | ||
| 10 | + | ||
| 11 | +export default class AdService { | ||
| 12 | + private constructor() { | ||
| 13 | + } | ||
| 14 | + /** | ||
| 15 | + * 创建banner | ||
| 16 | + * @param adUnitId | ||
| 17 | + * @param opts | ||
| 18 | + */ | ||
| 19 | + createBanner(adUnitId: string, opts?: { type?: number; bannerWidth?: number, offsetY?: number; adIntervals?: number, isOff?: boolean }) { | ||
| 20 | + return WxBanner.I.create(adUnitId, opts); | ||
| 21 | + } | ||
| 22 | + /** | ||
| 23 | + * banner 显示 ps:创建默认显示 | ||
| 24 | + */ | ||
| 25 | + showBanner() { | ||
| 26 | + WxBanner.I.show() | ||
| 27 | + } | ||
| 28 | + /** | ||
| 29 | + * banner 隐藏 | ||
| 30 | + */ | ||
| 31 | + hideBanner() { | ||
| 32 | + WxBanner.I.hide(); | ||
| 33 | + } | ||
| 34 | + /** | ||
| 35 | + * banner 销毁 | ||
| 36 | + */ | ||
| 37 | + destoryBanner() { | ||
| 38 | + WxBanner.I.destory(); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + | ||
| 42 | + /** | ||
| 43 | + * 插屏 | ||
| 44 | + * @param adUnitId | ||
| 45 | + */ | ||
| 46 | + createInterstitialAd(adUnitId: string) { | ||
| 47 | + WxInterstitial.showInterstitialAd(adUnitId) | ||
| 48 | + // let interstitialAd = WxApi.I.createInterstitialAd(adUnitId); | ||
| 49 | + // interstitialAd.show(); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * 创建格子广告 | ||
| 54 | + * @param adUnitId | ||
| 55 | + * @param opts | ||
| 56 | + */ | ||
| 57 | + createGrid(key: string, adUnitId: string, opts?: { gridCount?: number; bannerWidth?: number, offsetY: number; adIntervals?: number }) { | ||
| 58 | + return WxGrid.I.create(key, adUnitId, opts); | ||
| 59 | + } | ||
| 60 | + /** | ||
| 61 | + * 格子广告 显示 ps:创建默认显示 | ||
| 62 | + */ | ||
| 63 | + showGrid(key: string) { | ||
| 64 | + WxGrid.I.show(key) | ||
| 65 | + } | ||
| 66 | + /** | ||
| 67 | + * 格子广告 隐藏 | ||
| 68 | + */ | ||
| 69 | + hideGrid(key: string) { | ||
| 70 | + WxGrid.I.hide(key); | ||
| 71 | + } | ||
| 72 | + /** | ||
| 73 | + * 格子广告 销毁 | ||
| 74 | + */ | ||
| 75 | + destoryGrid(key: string) { | ||
| 76 | + WxGrid.I.destory(key); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + /** | ||
| 80 | + * 创建原生广告 | ||
| 81 | + * @param adUnitId | ||
| 82 | + * @param opts | ||
| 83 | + */ | ||
| 84 | + createCustom(adUnitId: string, opts: { top: number; left: number; adIntervals?: number }) { | ||
| 85 | + return WxCustom.I.create(adUnitId, opts); | ||
| 86 | + } | ||
| 87 | + /** | ||
| 88 | + * 原生广告 显示 ps:创建默认显示 | ||
| 89 | + */ | ||
| 90 | + showCustom() { | ||
| 91 | + WxCustom.I.show() | ||
| 92 | + } | ||
| 93 | + /** | ||
| 94 | + * 格子广告 隐藏 | ||
| 95 | + */ | ||
| 96 | + hideCustom() { | ||
| 97 | + WxCustom.I.hide(); | ||
| 98 | + } | ||
| 99 | + /** | ||
| 100 | + * 格子广告 销毁 | ||
| 101 | + */ | ||
| 102 | + destoryCustom() { | ||
| 103 | + WxCustom.I.destory(); | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + | ||
| 107 | + | ||
| 108 | + // 导出 | ||
| 109 | + navigateToMiniProgram(data, type, opts?) { | ||
| 110 | + let { id, appid, path, game } = data; | ||
| 111 | + LogService.I.jumps(id, type); | ||
| 112 | + if (!path) path = `?channel_id=${GAMEDATA.channel_id}`; | ||
| 113 | + return WxApi.I.navigateToMiniProgram(appid, path, opts) | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + | ||
| 117 | + | ||
| 118 | + | ||
| 119 | + | ||
| 120 | + | ||
| 121 | + | ||
| 122 | + private static instance: AdService; | ||
| 123 | + static get I(): AdService { | ||
| 124 | + return this.instance || (this.instance = new AdService()); | ||
| 125 | + } | ||
| 126 | +} | ||
| 0 | \ No newline at end of file | 127 | \ No newline at end of file |
| @@ -0,0 +1,458 @@ | @@ -0,0 +1,458 @@ | ||
| 1 | +import SdkData from "./entity/SdkData"; | ||
| 2 | +import SDKUtils from "../utils/SDKUtils"; | ||
| 3 | +import StorageUtils from "../utils/StorageUtils"; | ||
| 4 | +import { StorageKeys, GAMEDATA, LoginHost, GameHost, DotHost, OrderHost } from "../base/SDKConst"; | ||
| 5 | +import { Gender, NetworkType } from "../base/SDKEnum"; | ||
| 6 | +import WxLaunch from "../wx/WxLaunch"; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * SDK数据管理类 | ||
| 10 | + */ | ||
| 11 | +export default class DataService { | ||
| 12 | + private _data: SdkData; | ||
| 13 | + | ||
| 14 | + private constructor() { | ||
| 15 | + this._data = new SdkData; | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + private setValue(key: string, val: any) { | ||
| 19 | + // console.error(key,val) | ||
| 20 | + if (SDKUtils.isUndefined(val)) | ||
| 21 | + return; | ||
| 22 | + | ||
| 23 | + this._data = { | ||
| 24 | + ...this._data, | ||
| 25 | + [key]: val | ||
| 26 | + }; | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + get Data(): SdkData { | ||
| 30 | + return this._data; | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + setLoginData(data: _LoginInnerData) { | ||
| 34 | + if (!data || Object.keys(data).length === 0) | ||
| 35 | + return; | ||
| 36 | + | ||
| 37 | + if (data.hasOwnProperty('openId')) | ||
| 38 | + this.setOpenId(data.openId); | ||
| 39 | + | ||
| 40 | + if (data.hasOwnProperty('channel')) | ||
| 41 | + this.setChannelId(data.channel); | ||
| 42 | + | ||
| 43 | + if (data.hasOwnProperty('userId')) | ||
| 44 | + this.setUserId(data.userId); | ||
| 45 | + | ||
| 46 | + if (data.hasOwnProperty('regTime')) | ||
| 47 | + this.setRegTime(data.regTime); | ||
| 48 | + | ||
| 49 | + if (data.hasOwnProperty('token')) | ||
| 50 | + this.setToken(data.token); | ||
| 51 | + | ||
| 52 | + if (data.hasOwnProperty('refToken')) | ||
| 53 | + this.setRefToken(data.refToken); | ||
| 54 | + | ||
| 55 | + if (data.hasOwnProperty('expire')) | ||
| 56 | + this.setExpice(data.expire); | ||
| 57 | + | ||
| 58 | + if (data.hasOwnProperty('isnew')) | ||
| 59 | + this.setIsnew(data.isnew); | ||
| 60 | + | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + get expice() { | ||
| 64 | + return this._data.expice | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + setExpice(val: any) { | ||
| 68 | + this.setValue('expire', val); | ||
| 69 | + return this; | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + get isnew() { | ||
| 73 | + return this._data.isnew | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + setIsnew(val: any) { | ||
| 77 | + this.setValue('isnew', val); | ||
| 78 | + return this; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + | ||
| 82 | + get Scene() { | ||
| 83 | + return this._data.scene || 0; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + setScene(val: any) { | ||
| 87 | + this.setValue('scene', val); | ||
| 88 | + return this; | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + get ReferrerInfo() { | ||
| 92 | + return this._data.referrerInfo || {}; | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + setReferrerInfo(val: any) { | ||
| 96 | + this.setValue('referrerInfo', val); | ||
| 97 | + return this; | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + get Token() { | ||
| 101 | + return this._data.token || '';//|| StorageUtils.I.get(StorageKeys.SDKTokenKey) || | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + setToken(val: any) { | ||
| 105 | + this.setValue('token', val); | ||
| 106 | + // StorageUtils.I.set(StorageKeys.SDKTokenKey, val); | ||
| 107 | + return this; | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + get refToken() { | ||
| 111 | + return this._data.refToken | ||
| 112 | + } | ||
| 113 | + setRefToken(val: any) { | ||
| 114 | + this.setValue('refToken', val); | ||
| 115 | + return this; | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + get OpenId() { | ||
| 119 | + return this._data.openId; | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + private setOpenId(val: any) { | ||
| 123 | + this.setValue('openId', val); | ||
| 124 | + // this.fixLaunchKey(val); | ||
| 125 | + return this; | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + get UserId() { | ||
| 129 | + if (this._data.userId) | ||
| 130 | + return this._data.userId; | ||
| 131 | + else | ||
| 132 | + return this.OpenId; | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + setUserId(val: any) { | ||
| 136 | + this.setValue('userId', val); | ||
| 137 | + return this; | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + get RegTime() { | ||
| 141 | + return this._data.regTime; | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + setRegTime(val: any) { | ||
| 145 | + this.setValue('regTime', val); | ||
| 146 | + return this; | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + get LaunchTime() { | ||
| 150 | + return this._data.launchTime; | ||
| 151 | + } | ||
| 152 | + | ||
| 153 | + setLaunchTime(val: any) { | ||
| 154 | + this.setValue('launchTime', val); | ||
| 155 | + return this; | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + get ShareId() { | ||
| 159 | + return this._data.shareId; | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + setShareId(val: number) { | ||
| 163 | + this.setValue('shareId', val); | ||
| 164 | + return this; | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + get ShareKey() { | ||
| 168 | + return this._data.shareKey; | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + setShareKey(val: string) { | ||
| 172 | + this.setValue('shareKey', val); | ||
| 173 | + return this; | ||
| 174 | + } | ||
| 175 | + | ||
| 176 | + get Platform(): string { | ||
| 177 | + return this._data.platform; | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + setPlatform(val: string) { | ||
| 181 | + this.setValue('platform', val); | ||
| 182 | + return this; | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + // get NetworkType(): NetworkType { | ||
| 186 | + // return this._data.networkType; | ||
| 187 | + // } | ||
| 188 | + | ||
| 189 | + setNetworkType(networkType: NetworkType) { | ||
| 190 | + this.setValue('networkType', networkType); | ||
| 191 | + return this; | ||
| 192 | + } | ||
| 193 | + | ||
| 194 | + get Gender(): Gender { | ||
| 195 | + return this._data.gender || Gender.Unknown; | ||
| 196 | + } | ||
| 197 | + | ||
| 198 | + setGender(val: Gender) { | ||
| 199 | + this.setValue('gender', val); | ||
| 200 | + return this; | ||
| 201 | + } | ||
| 202 | + | ||
| 203 | + get ChannelId(): any { | ||
| 204 | + return this._data.channelId; | ||
| 205 | + } | ||
| 206 | + | ||
| 207 | + setChannelId(val: any) { | ||
| 208 | + this.setValue('channelId', val); | ||
| 209 | + return this; | ||
| 210 | + } | ||
| 211 | + | ||
| 212 | + get QueryChannelId(): any { | ||
| 213 | + return this._data.queryChannelId || 0; | ||
| 214 | + } | ||
| 215 | + | ||
| 216 | + setQueryChannelId(val: any) { | ||
| 217 | + this.setValue('queryChannelId', val); | ||
| 218 | + return this; | ||
| 219 | + } | ||
| 220 | + | ||
| 221 | + get InviteType(): any { | ||
| 222 | + return this._data.inviteType || 0; | ||
| 223 | + } | ||
| 224 | + | ||
| 225 | + setInviteType(val: any) { | ||
| 226 | + this.setValue('inviteType', val); | ||
| 227 | + return this; | ||
| 228 | + } | ||
| 229 | + | ||
| 230 | + get QueryUserInviteUid(): any { | ||
| 231 | + return this._data.queryUserInviteUid || 0; | ||
| 232 | + } | ||
| 233 | + | ||
| 234 | + setQueryUserInviteUid(val: any) { | ||
| 235 | + //console.warn('====> PCSDK.data.setQueryUserInviteUid', val); | ||
| 236 | + this.setValue('queryUserInviteUid', val); | ||
| 237 | + return this; | ||
| 238 | + } | ||
| 239 | + | ||
| 240 | + get QueryExtData(): any { | ||
| 241 | + return this._data.queryExtData || {}; | ||
| 242 | + } | ||
| 243 | + | ||
| 244 | + setQueryExtData(val: any) { | ||
| 245 | + this.setValue('queryExtData', val); | ||
| 246 | + return this; | ||
| 247 | + } | ||
| 248 | + | ||
| 249 | + get UserInviteUid(): any { | ||
| 250 | + return this._data.inviteUid || 0; | ||
| 251 | + } | ||
| 252 | + | ||
| 253 | + setUserInviteUid(val: any) { | ||
| 254 | + this.setValue('inviteUid', val); | ||
| 255 | + return this; | ||
| 256 | + } | ||
| 257 | + | ||
| 258 | + // get UserState(): UserState { | ||
| 259 | + // return this._data.userState || UserState.Default; | ||
| 260 | + // } | ||
| 261 | + | ||
| 262 | + setUserState(val: any) { | ||
| 263 | + this.setValue('userState', val); | ||
| 264 | + return this; | ||
| 265 | + } | ||
| 266 | + | ||
| 267 | + get Shield(): any { | ||
| 268 | + return this._data.shield || StorageUtils.I.get(StorageKeys.SDKUserShieldKey) || 0; | ||
| 269 | + } | ||
| 270 | + | ||
| 271 | + setShield(val: any) { | ||
| 272 | + this.setValue('shield', val); | ||
| 273 | + StorageUtils.I.set(StorageKeys.SDKUserShieldKey, val); | ||
| 274 | + return this; | ||
| 275 | + } | ||
| 276 | + | ||
| 277 | + get ShareTicket(): any { | ||
| 278 | + return this._data.shareTicket; | ||
| 279 | + } | ||
| 280 | + | ||
| 281 | + setShareTicket(val: any) { | ||
| 282 | + this.setValue('shareTicket', val); | ||
| 283 | + return this; | ||
| 284 | + } | ||
| 285 | + | ||
| 286 | + get SystemId(): number { | ||
| 287 | + return this._data.systemId; | ||
| 288 | + } | ||
| 289 | + | ||
| 290 | + setSystemId() { | ||
| 291 | + let systemId = this.Platform === 'android' ? 1 : 0; | ||
| 292 | + this.setValue('systemId', systemId); | ||
| 293 | + return this; | ||
| 294 | + } | ||
| 295 | + | ||
| 296 | + get Authorize(): boolean { | ||
| 297 | + return this._data.authorize; | ||
| 298 | + } | ||
| 299 | + | ||
| 300 | + setAuthorize(val: boolean) { | ||
| 301 | + // DebugUtils.I.dynamic('====> PCSDK data setAuthorize: ' + val); | ||
| 302 | + this.setValue('authorize', val); | ||
| 303 | + return this; | ||
| 304 | + } | ||
| 305 | + | ||
| 306 | + get Version(): string { | ||
| 307 | + return this._data.version; | ||
| 308 | + } | ||
| 309 | + | ||
| 310 | + setVersion(val: string) { | ||
| 311 | + this.setValue('version', val); | ||
| 312 | + return this; | ||
| 313 | + } | ||
| 314 | + | ||
| 315 | + get CdnUrl(): string { | ||
| 316 | + return this._data.cdnUrl; | ||
| 317 | + } | ||
| 318 | + | ||
| 319 | + setCdnUrl(val: string) { | ||
| 320 | + this.setValue('cdnUrl', val); | ||
| 321 | + return this; | ||
| 322 | + } | ||
| 323 | + | ||
| 324 | + get EnvEnum() { | ||
| 325 | + return this._data.envEnum; | ||
| 326 | + } | ||
| 327 | + | ||
| 328 | + setEnvEnum(val: number) { | ||
| 329 | + this.setValue('envEnum', val); | ||
| 330 | + return this; | ||
| 331 | + } | ||
| 332 | + | ||
| 333 | + get LoginApi() { | ||
| 334 | + let path = LoginHost.Prod; | ||
| 335 | + if (this.EnvEnum === 1) { | ||
| 336 | + path = LoginHost.Pre | ||
| 337 | + } | ||
| 338 | + return path; | ||
| 339 | + } | ||
| 340 | + get GameApi() { | ||
| 341 | + let path = GameHost.Prod; | ||
| 342 | + if (this.EnvEnum === 1) { | ||
| 343 | + path = GameHost.Pre | ||
| 344 | + } | ||
| 345 | + return path; | ||
| 346 | + } | ||
| 347 | + | ||
| 348 | + get DotApi() { | ||
| 349 | + let path = DotHost.Prod; | ||
| 350 | + if (this.EnvEnum === 1) { | ||
| 351 | + path = DotHost.Pre | ||
| 352 | + } | ||
| 353 | + return path; | ||
| 354 | + } | ||
| 355 | + | ||
| 356 | + get OrderApi() { | ||
| 357 | + let path = OrderHost.Prod; | ||
| 358 | + if (this.EnvEnum === 1) { | ||
| 359 | + path = OrderHost.Pre | ||
| 360 | + } | ||
| 361 | + return path; | ||
| 362 | + } | ||
| 363 | + | ||
| 364 | + | ||
| 365 | + get GameId() { | ||
| 366 | + return GAMEDATA.game_id; | ||
| 367 | + } | ||
| 368 | + | ||
| 369 | + get LaunchKey() { | ||
| 370 | + return this._data.launchKey || StorageUtils.I.get(StorageKeys.SDKLaunchKey) || ''; | ||
| 371 | + } | ||
| 372 | + | ||
| 373 | + fixLaunchKey(openId: string) { | ||
| 374 | + if (openId) { | ||
| 375 | + StorageUtils.I.set(StorageKeys.SDKLaunchKey, openId); | ||
| 376 | + this.setValue('launchKey', openId); | ||
| 377 | + } | ||
| 378 | + return this; | ||
| 379 | + } | ||
| 380 | + | ||
| 381 | + // getEventLaunchKey(): string { | ||
| 382 | + // let key = StorageKeys.SDKEventLaunchKey; | ||
| 383 | + // let launchKey = StorageUtils.I.get(key); | ||
| 384 | + // if (!launchKey) { | ||
| 385 | + // launchKey = StringUtils.createCode(); | ||
| 386 | + // StorageUtils.I.set(key, launchKey); | ||
| 387 | + // } | ||
| 388 | + // return launchKey; | ||
| 389 | + // } | ||
| 390 | + | ||
| 391 | + // get LaunchSource() { | ||
| 392 | + // return this._data.launchSource; | ||
| 393 | + // } | ||
| 394 | + | ||
| 395 | + setLaunchSource() { | ||
| 396 | + let source = 0; | ||
| 397 | + let { query } = WxLaunch.I.LaunchData; | ||
| 398 | + if (query) { | ||
| 399 | + if (query.scene) { | ||
| 400 | + let arr = (decodeURIComponent(query.scene) as string).split('&'); | ||
| 401 | + arr[1] && (source = 1); | ||
| 402 | + } else if (query.user_invite_id || query.user_invite_uid || query.invite_unionid) | ||
| 403 | + source = 1; | ||
| 404 | + } else | ||
| 405 | + source = 0; | ||
| 406 | + | ||
| 407 | + this.setValue('launchSource', source); | ||
| 408 | + return this; | ||
| 409 | + } | ||
| 410 | + | ||
| 411 | + get IsCross(): boolean { | ||
| 412 | + return this._data.isCross; | ||
| 413 | + } | ||
| 414 | + | ||
| 415 | + setIsCross(val: boolean) { | ||
| 416 | + this.setValue('isCross', val); | ||
| 417 | + return this; | ||
| 418 | + } | ||
| 419 | + | ||
| 420 | + get IsDrawer(): boolean { | ||
| 421 | + return this._data.isDrawer; | ||
| 422 | + } | ||
| 423 | + | ||
| 424 | + setIsDrawer(val: boolean) { | ||
| 425 | + this.setValue('isDrawer', val); | ||
| 426 | + return this; | ||
| 427 | + } | ||
| 428 | + | ||
| 429 | + get IsGuessLike(): boolean { | ||
| 430 | + return this._data.isGuessLike; | ||
| 431 | + } | ||
| 432 | + | ||
| 433 | + setIsGuessLike(val: boolean) { | ||
| 434 | + this.setValue('isGuessLike', val); | ||
| 435 | + return this; | ||
| 436 | + } | ||
| 437 | + | ||
| 438 | + // get ShareTotalNum(): number { | ||
| 439 | + // return IntegralService.I.getFinishShareNum(); | ||
| 440 | + // } | ||
| 441 | + | ||
| 442 | + // get VideoTotalNum(): number { | ||
| 443 | + // return IntegralService.I.getFinishVideoNum(); | ||
| 444 | + // } | ||
| 445 | + | ||
| 446 | + // get ShareDayNum(): number { | ||
| 447 | + // return LocalService.I.getDayAllShareNum(); | ||
| 448 | + // } | ||
| 449 | + | ||
| 450 | + // get VideoDayNum(): number { | ||
| 451 | + // return LocalService.I.getDayAllVideoNum(); | ||
| 452 | + // } | ||
| 453 | + | ||
| 454 | + private static instance: DataService; | ||
| 455 | + static get I(): DataService { | ||
| 456 | + return this.instance || (this.instance = new DataService()); | ||
| 457 | + } | ||
| 458 | +} | ||
| 0 | \ No newline at end of file | 459 | \ No newline at end of file |
| @@ -0,0 +1,114 @@ | @@ -0,0 +1,114 @@ | ||
| 1 | +import DataService from "./DataService"; | ||
| 2 | +import { SDKApi } from "../http/SDKApi"; | ||
| 3 | +import { __LOG__ } from "../base/SDKConst"; | ||
| 4 | +import WxLogin from "../wx/WxLogin"; | ||
| 5 | +import WxApi from "../wx/WxApi"; | ||
| 6 | +import WxPay from "../wx/WxPay"; | ||
| 7 | + | ||
| 8 | + | ||
| 9 | +export default class GameService { | ||
| 10 | + private constructor() { | ||
| 11 | + | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + env() { | ||
| 15 | + return new Promise((resolve, reject) => { | ||
| 16 | + SDKApi.Version().then((data: any) => { | ||
| 17 | + if (data) { | ||
| 18 | + __LOG__ && console.log("版本信息", data); | ||
| 19 | + let num = data.data ? data.data.env ? data.data.env : 2 : 2; | ||
| 20 | + DataService.I.setEnvEnum(+num) | ||
| 21 | + // DataService.I.setCdnUrl(data.res_url || ''); | ||
| 22 | + } | ||
| 23 | + // 设置在线参数请求 | ||
| 24 | + // let isAutoOnlineUse = CfgManager.I.config.IsOnlineAutoUse; | ||
| 25 | + // isAutoOnlineUse && OnlineService.I.updateOnlineConfig().then(() => EventCenter.I.emit(EventEnum.ONLINE_SUCCESS)); | ||
| 26 | + resolve(data); | ||
| 27 | + }).catch((err: any) => { | ||
| 28 | + reject(err); | ||
| 29 | + }); | ||
| 30 | + }); | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + login(isAuthorize = false) { | ||
| 34 | + return WxLogin.I.login(isAuthorize); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + | ||
| 38 | + saveData(key: string, content: string) { | ||
| 39 | + return SDKApi.saveData({ key, content, uid: DataService.I.UserId, token: DataService.I.Token }); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + getData(key: string) { | ||
| 43 | + return SDKApi.getData({ key, uid: DataService.I.UserId, token: DataService.I.Token }); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + rankAdd(key, value) { | ||
| 47 | + return SDKApi.rankAdd({ typ: key, fraction: value, uid: DataService.I.UserId, token: DataService.I.Token }) | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + rankList(typ, t, isRankData) { | ||
| 51 | + return SDKApi.rankList({ typ, t, rankdata: isRankData ? 1 : 0, uid: DataService.I.UserId, token: DataService.I.Token }) | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + totalrankAdd(key, value, type, sort) { | ||
| 55 | + return SDKApi.totalrankAdd({ typ: key, sort, t: type, fraction: value, uid: DataService.I.UserId, token: DataService.I.Token }) | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + totalrankList(typ, t, isRankData, sort) { | ||
| 59 | + return SDKApi.totalrankList({ typ, t, sort, rankdata: isRankData ? 1 : 0, uid: DataService.I.UserId, token: DataService.I.Token }) | ||
| 60 | + } | ||
| 61 | + adList(adtyp) { | ||
| 62 | + return SDKApi.adList({ adtyp, uid: DataService.I.UserId, token: DataService.I.Token }) | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + | ||
| 66 | + subScribe(tmplIds: Array<string>, ids: Array<string>): Promise<any> { | ||
| 67 | + return new Promise((resolve, reject) => { | ||
| 68 | + WxApi.I.subscribeMessage(tmplIds) | ||
| 69 | + .then((ret: any) => { | ||
| 70 | + ret = ret || {}; | ||
| 71 | + let acceptKeys = tmplIds.filter((idKey: string) => ret[idKey] && ret[idKey] === 'accept'); | ||
| 72 | + if (!acceptKeys.length) { | ||
| 73 | + reject({ errCode: 0, errMsg: '点击取消订阅' }); | ||
| 74 | + } else { | ||
| 75 | + let uid = +DataService.I.UserId; | ||
| 76 | + let token = DataService.I.Token + ''; | ||
| 77 | + let openid = DataService.I.OpenId + ''; | ||
| 78 | + let id = ids//JSON.stringify() | ||
| 79 | + SDKApi.subscribe({ | ||
| 80 | + uid, | ||
| 81 | + token, | ||
| 82 | + openid, | ||
| 83 | + status: 1, | ||
| 84 | + id, | ||
| 85 | + }) | ||
| 86 | + } | ||
| 87 | + // GameService.I.subScribe(acceptKeys) | ||
| 88 | + // .then(() => { | ||
| 89 | + // resolve(ret); | ||
| 90 | + // DebugUtils.I.dynamic('====> PCSDK subScribe订阅消息成功', ret); | ||
| 91 | + // }) | ||
| 92 | + // .catch((err) => reject(err || { errCode: 0, errMsg: 'GameApi subScribe请求错误' })); | ||
| 93 | + | ||
| 94 | + }) | ||
| 95 | + .catch(err => reject(err)); | ||
| 96 | + }); | ||
| 97 | + // return WxApi.I.subscribeMessage(template_ids); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + /** | ||
| 101 | + * 支付 | ||
| 102 | + * @param params | ||
| 103 | + * @param opts | ||
| 104 | + * @returns | ||
| 105 | + */ | ||
| 106 | + pay(params: { money: number; source:string}, opts: any = {}) { | ||
| 107 | + return WxPay.I.pay(params, opts); | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + private static instance: GameService; | ||
| 111 | + static get I(): GameService { | ||
| 112 | + return this.instance || (this.instance = new GameService()); | ||
| 113 | + } | ||
| 114 | +} | ||
| 0 | \ No newline at end of file | 115 | \ No newline at end of file |
| @@ -0,0 +1,461 @@ | @@ -0,0 +1,461 @@ | ||
| 1 | +import DataService from "./DataService"; | ||
| 2 | +import { __LOG__, GAMEDATA } from "../base/SDKConst"; | ||
| 3 | +import { SDKApi } from "../http/SDKApi"; | ||
| 4 | +import WxSystem from "../wx/WxSystem"; | ||
| 5 | +import StorageUtils from "../utils/StorageUtils"; | ||
| 6 | +import { DOT_SHARE_TYPE, DOT_AD_TYPE, DOT_AD_STATUS, AdType } from "../base/SDKEnum"; | ||
| 7 | +import DateUtils from "../utils/DateUtils"; | ||
| 8 | + | ||
| 9 | +export default class LogService { | ||
| 10 | + // 曝光数据 | ||
| 11 | + private exposureArr: any; | ||
| 12 | + // 资源变更统计 | ||
| 13 | + private logResArr: _LogResData[]; | ||
| 14 | + // 关卡统计 | ||
| 15 | + private logLevelArr: _LogLevelData[]; | ||
| 16 | + // 是否已经处理请求了shareList接口 | ||
| 17 | + private isHandledShare: boolean; | ||
| 18 | + private timeoutShareId: any; | ||
| 19 | + // reg接口是否已经返回数据状态 | ||
| 20 | + private regFinishState: number; | ||
| 21 | + | ||
| 22 | + private lastReqBannerTime: any; | ||
| 23 | + private lastReqBannerData: any; | ||
| 24 | + | ||
| 25 | + private queue: Array<any> = []; | ||
| 26 | + | ||
| 27 | + private constructor() { | ||
| 28 | + this.exposureArr = {}; | ||
| 29 | + this.logResArr = []; | ||
| 30 | + this.logLevelArr = []; | ||
| 31 | + this.regFinishState = 0; | ||
| 32 | + this.isHandledShare = false; | ||
| 33 | + this.timeoutShareId = 0; | ||
| 34 | + this.lastReqBannerTime = {}; | ||
| 35 | + this.lastReqBannerData = {}; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + setRegFinishState(state: number) { | ||
| 39 | + this.regFinishState = state; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + private get SystemId() { | ||
| 43 | + return DataService.I.SystemId; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + private get LaunchTime() { | ||
| 47 | + return DataService.I.LaunchTime; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + private get LaunchKey() { | ||
| 51 | + return DataService.I.LaunchKey; | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + // private get EventLaunchKey() { | ||
| 55 | + // return DataService.I.getEventLaunchKey(); | ||
| 56 | + // } | ||
| 57 | + | ||
| 58 | + // private get LaunchSource() { | ||
| 59 | + // return DataService.I.LaunchSource; | ||
| 60 | + // } | ||
| 61 | + | ||
| 62 | + private get Scene() { | ||
| 63 | + return DataService.I.Scene; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + private get RegTime() { | ||
| 67 | + return DataService.I.RegTime; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + private get UserId() { | ||
| 71 | + return DataService.I.UserId; | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + private get OpenId() { | ||
| 75 | + return DataService.I.OpenId; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + private get UserInviteUid() { | ||
| 79 | + return DataService.I.UserInviteUid; | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + private get QueryUserInviteUid() { | ||
| 83 | + return DataService.I.QueryUserInviteUid; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + private get QueryExtData() { | ||
| 87 | + return DataService.I.QueryExtData; | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + private get Shield() { | ||
| 91 | + return DataService.I.Shield; | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + | ||
| 95 | + private get shareKey() { | ||
| 96 | + return DataService.I.ShareKey; | ||
| 97 | + } | ||
| 98 | + private get shareId() { | ||
| 99 | + return DataService.I.ShareId; | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + fixLaunchKey(launch_key: string) { | ||
| 103 | + DataService.I.fixLaunchKey(launch_key); | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + //是否登录 | ||
| 107 | + isLogin: boolean = false; | ||
| 108 | + /** | ||
| 109 | + * 内部游戏调用设置:是否新老用户 | ||
| 110 | + * @param data | ||
| 111 | + */ | ||
| 112 | + setLogind(data: _LoginInnerData) { | ||
| 113 | + DataService.I.setLoginData(data); | ||
| 114 | + this.isLogin = true; | ||
| 115 | + if (this.queue.length > 0) { | ||
| 116 | + for (let i = 0; i < this.queue.length; i++) { | ||
| 117 | + let fun = this.queue[i]; | ||
| 118 | + fun(); | ||
| 119 | + } | ||
| 120 | + } | ||
| 121 | + this.queue = []; | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + async dot(eventkey: string, options = {}) { | ||
| 125 | + //2020-12-31 改二级key | ||
| 126 | + let itemkey = ''; | ||
| 127 | + let itemvalue = ''; | ||
| 128 | + if (JSON.stringify(options) != '{}' && typeof options === 'object') { | ||
| 129 | + for (let k in options) { | ||
| 130 | + if (itemkey === '') { | ||
| 131 | + itemkey = k; | ||
| 132 | + itemvalue = options[k]; | ||
| 133 | + } | ||
| 134 | + } | ||
| 135 | + } | ||
| 136 | + // !this.isLogin && await PromiseSame.get(SDKTools.login, 'login') | ||
| 137 | + let fun = () => SDKApi.dot({ | ||
| 138 | + ...this.buildParams(), | ||
| 139 | + eventkey, | ||
| 140 | + value: JSON.stringify(options), | ||
| 141 | + itemkey, | ||
| 142 | + itemvalue | ||
| 143 | + }); | ||
| 144 | + this.checkLogin(fun); | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + /** | ||
| 148 | + * 关卡开始 | ||
| 149 | + * @param stageid 关卡ID, 必须1.1、12.2 “.”前面代表模式 后面代表关卡 | ||
| 150 | + * @param stagename 关卡名称,格式:"xx模式-第x关" | ||
| 151 | + * @param pattern 模式名称,格式:"xx模式" | ||
| 152 | + */ | ||
| 153 | + async levelStart(stageid, stagename, pattern) { | ||
| 154 | + let fun = () => SDKApi.level({ | ||
| 155 | + ...this.buildParams(), | ||
| 156 | + stageid, | ||
| 157 | + stagename, | ||
| 158 | + pattern, | ||
| 159 | + typ: 1 | ||
| 160 | + }) | ||
| 161 | + this.checkLogin(fun); | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + /** | ||
| 165 | + * 关卡进行中 | ||
| 166 | + * @param stageid 关卡ID, 必须1.1、12.2 “.”前面代表模式 后面代表关卡 | ||
| 167 | + * @param stagename 关卡名称,格式:"xx模式-第x关" | ||
| 168 | + * @param pattern 模式名称,格式:"xx模式" | ||
| 169 | + * @param event 事件 tools:使用道具 award:奖励 | ||
| 170 | + * @param params_id 道具ID | ||
| 171 | + * @param params_name 道具名称 | ||
| 172 | + * @param params_count 道具数量 | ||
| 173 | + * @param params_desc 道具描述 | ||
| 174 | + */ | ||
| 175 | + async levelRunning(stageid, stagename, pattern, event, params_id, params_name, params_count, params_desc?) { | ||
| 176 | + let fun = () => SDKApi.level({ | ||
| 177 | + ...this.buildParams(), | ||
| 178 | + stageid, | ||
| 179 | + stagename, | ||
| 180 | + pattern, | ||
| 181 | + event, | ||
| 182 | + params_id, | ||
| 183 | + params_name, | ||
| 184 | + params_count, | ||
| 185 | + params_desc, | ||
| 186 | + typ: 2 | ||
| 187 | + }) | ||
| 188 | + this.checkLogin(fun); | ||
| 189 | + } | ||
| 190 | + | ||
| 191 | + /** | ||
| 192 | + * 关卡结束 | ||
| 193 | + * @param stageid 关卡ID, 必须1.1、12.2 “.”前面代表模式 后面代表关卡 | ||
| 194 | + * @param stagename 关卡名称,格式:"xx模式-第x关" | ||
| 195 | + * @param pattern 模式名称,格式:"xx模式" | ||
| 196 | + * @param event complete:成功 fail:失败 | ||
| 197 | + * @param times 时间 | ||
| 198 | + * @param perc 失败时的完成进度 (浮点数) | ||
| 199 | + */ | ||
| 200 | + async levelEnd(stageid, stagename, pattern, event, times,perc?) { | ||
| 201 | + let fun = () => SDKApi.level({ | ||
| 202 | + ...this.buildParams(), | ||
| 203 | + stageid, | ||
| 204 | + stagename, | ||
| 205 | + pattern, | ||
| 206 | + event, | ||
| 207 | + times, | ||
| 208 | + perc, | ||
| 209 | + typ: 3 | ||
| 210 | + }) | ||
| 211 | + this.checkLogin(fun); | ||
| 212 | + } | ||
| 213 | + | ||
| 214 | + /** | ||
| 215 | + * loading资源完成,新版加载游戏资源完成时调用 | ||
| 216 | + */ | ||
| 217 | + async loadingFinish() { | ||
| 218 | + let fun = () => SDKApi.loadingFinish({ | ||
| 219 | + ...this.buildParams() | ||
| 220 | + }) | ||
| 221 | + this.checkLogin(fun); | ||
| 222 | + // 30秒及时 | ||
| 223 | + this.startStay() | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + async active() { | ||
| 227 | + let fun = () => SDKApi.active({ | ||
| 228 | + ...this.buildParams(), | ||
| 229 | + isnew: 0, | ||
| 230 | + sharekey: this.shareKey, | ||
| 231 | + shareid: this.shareId | ||
| 232 | + }) | ||
| 233 | + this.checkLogin(fun); | ||
| 234 | + } | ||
| 235 | + | ||
| 236 | + | ||
| 237 | + /** | ||
| 238 | + * 用户log - 退出登录 | ||
| 239 | + * @param time_len 用户在线时长,单位毫秒 | ||
| 240 | + */ | ||
| 241 | + logOut(time_len: number) { | ||
| 242 | + let fun = () => SDKApi.logOut({ | ||
| 243 | + ...this.buildParams(), | ||
| 244 | + times: time_len | ||
| 245 | + }) | ||
| 246 | + this.checkLogin(fun); | ||
| 247 | + } | ||
| 248 | + | ||
| 249 | + /** | ||
| 250 | + * | ||
| 251 | + * @param sharekey | ||
| 252 | + * @param type 1分享 2点击 | ||
| 253 | + */ | ||
| 254 | + share(sharekey: string, shareid: number, type: DOT_SHARE_TYPE) { | ||
| 255 | + let fun = () => SDKApi.share({ | ||
| 256 | + ...this.buildParams(), | ||
| 257 | + typ: type, | ||
| 258 | + sharekey, | ||
| 259 | + shareid, | ||
| 260 | + shareuid: type === DOT_SHARE_TYPE.click ? this.QueryUserInviteUid : '', //点击才要分享id | ||
| 261 | + }) | ||
| 262 | + this.checkLogin(fun); | ||
| 263 | + } | ||
| 264 | + /** | ||
| 265 | + * adtyp:类型 1banner2激励视频3插屏4格子5原生模板 | ||
| 266 | + adstatus: 1请求 2返回 3展示 4点击 5完成 6中断 7失败 | ||
| 267 | + banner:1,3,6,7 | ||
| 268 | + video:1,5,6,7 | ||
| 269 | + interstitial:1 | ||
| 270 | + grid:1,3,6,7 | ||
| 271 | + custom:1,3,6,7 | ||
| 272 | + * @param sharekey | ||
| 273 | + * @param adid | ||
| 274 | + * @param adtyp | ||
| 275 | + * @param adstatus | ||
| 276 | + */ | ||
| 277 | + adStat(videokey: string, adid: string, adtyp: DOT_AD_TYPE, adstatus: DOT_AD_STATUS) { | ||
| 278 | + let fun = () => SDKApi.adStat({ | ||
| 279 | + ...this.buildParams(), | ||
| 280 | + videokey, | ||
| 281 | + adplat: "1", | ||
| 282 | + adid, | ||
| 283 | + adtyp, | ||
| 284 | + adstatus | ||
| 285 | + }) | ||
| 286 | + this.checkLogin(fun); | ||
| 287 | + } | ||
| 288 | + | ||
| 289 | + private stayInterval | ||
| 290 | + private startStay() { | ||
| 291 | + this.stayInterval = setInterval(this.stayFun.bind(this), 1000); | ||
| 292 | + } | ||
| 293 | + private stayFun() { | ||
| 294 | + let newUserTime = StorageUtils.I.get("newUserTime") || 0; | ||
| 295 | + newUserTime++; | ||
| 296 | + StorageUtils.I.set("newUserTime", newUserTime) | ||
| 297 | + let newUserDot = StorageUtils.I.get("newUserDot") || 0; | ||
| 298 | + if (newUserDot === 1) clearInterval(this.stayInterval); | ||
| 299 | + // console.log("newUserTime", newUserTime, newUserDot) | ||
| 300 | + if (newUserTime >= 30) { | ||
| 301 | + StorageUtils.I.set("newUserDot", 1); | ||
| 302 | + this.loadingFinishStay(30); | ||
| 303 | + clearInterval(this.stayInterval); | ||
| 304 | + } | ||
| 305 | + } | ||
| 306 | + async loadingFinishStay(times) { | ||
| 307 | + let fun = () => { | ||
| 308 | + if (DataService.I.RegTime === DateUtils.today) { | ||
| 309 | + SDKApi.stay({ | ||
| 310 | + ...this.buildParams(), | ||
| 311 | + times, | ||
| 312 | + }) | ||
| 313 | + } | ||
| 314 | + } | ||
| 315 | + this.checkLogin(fun); | ||
| 316 | + } | ||
| 317 | + | ||
| 318 | + /** | ||
| 319 | + * 用户停留30s打点 2021-1-6 废弃 | ||
| 320 | + */ | ||
| 321 | + stay(times) { | ||
| 322 | + // let fun = () => SDKApi.stay({ | ||
| 323 | + // ...this.buildParams(), | ||
| 324 | + // times, | ||
| 325 | + // }) | ||
| 326 | + // this.checkLogin(fun); | ||
| 327 | + } | ||
| 328 | + | ||
| 329 | + /** | ||
| 330 | + * 关键行为打点 | ||
| 331 | + */ | ||
| 332 | + behaviors() { | ||
| 333 | + let fun = () => SDKApi.behaviors({ | ||
| 334 | + ...this.buildParams(), | ||
| 335 | + }) | ||
| 336 | + this.checkLogin(fun); | ||
| 337 | + } | ||
| 338 | + | ||
| 339 | + checkLogin(fun) { | ||
| 340 | + if (this.isLogin) { | ||
| 341 | + fun(); | ||
| 342 | + } else { | ||
| 343 | + this.queue.push(fun) | ||
| 344 | + } | ||
| 345 | + } | ||
| 346 | + | ||
| 347 | + /** | ||
| 348 | + * 跳转打点 | ||
| 349 | + */ | ||
| 350 | + jumps(id, adtyp) { | ||
| 351 | + let fun = () => SDKApi.jumps({ | ||
| 352 | + ...this.buildParams(), | ||
| 353 | + adid: 0, | ||
| 354 | + adplat: 1, | ||
| 355 | + adtyp, | ||
| 356 | + typ: 1, | ||
| 357 | + times: 1, | ||
| 358 | + id | ||
| 359 | + }) | ||
| 360 | + this.checkLogin(fun); | ||
| 361 | + } | ||
| 362 | + /** | ||
| 363 | + * 曝光打点 | ||
| 364 | + */ | ||
| 365 | + bannerExposure2(id, adtyp, times) { | ||
| 366 | + let fun = () => SDKApi.jumps({ | ||
| 367 | + ...this.buildParams(), | ||
| 368 | + adid: 0, | ||
| 369 | + adplat: 1, | ||
| 370 | + adtyp, | ||
| 371 | + typ: 2, | ||
| 372 | + times, | ||
| 373 | + id | ||
| 374 | + }) | ||
| 375 | + this.checkLogin(fun); | ||
| 376 | + } | ||
| 377 | + | ||
| 378 | + /** | ||
| 379 | + * 曝光累计 | ||
| 380 | + */ | ||
| 381 | + addExposure(type: AdType | number, data: Array<IGameList>) { | ||
| 382 | + if (!data) return | ||
| 383 | + data.forEach(item => { | ||
| 384 | + let { id } = item; | ||
| 385 | + if (this.exposureArr.hasOwnProperty(type)) { | ||
| 386 | + let d = this.exposureArr[type]; | ||
| 387 | + if (d.hasOwnProperty(id)) { | ||
| 388 | + this.exposureArr[type][id]++; | ||
| 389 | + } else { | ||
| 390 | + this.exposureArr[type] = { | ||
| 391 | + ...this.exposureArr[type], | ||
| 392 | + [id]: 1 | ||
| 393 | + } | ||
| 394 | + } | ||
| 395 | + } else { | ||
| 396 | + let obj = { | ||
| 397 | + [type]: { [id]: 1 } | ||
| 398 | + } | ||
| 399 | + this.exposureArr = { | ||
| 400 | + ...this.exposureArr, | ||
| 401 | + ...obj | ||
| 402 | + } | ||
| 403 | + } | ||
| 404 | + }) | ||
| 405 | + // console.log("this.exposureArr", this.exposureArr) | ||
| 406 | + } | ||
| 407 | + | ||
| 408 | + /** | ||
| 409 | + * 曝光打点 | ||
| 410 | + */ | ||
| 411 | + bannerExposure() { | ||
| 412 | + if (JSON.stringify(this.exposureArr) === '{}') return; | ||
| 413 | + let obj = JSON.parse(JSON.stringify(this.exposureArr)); | ||
| 414 | + this.exposureArr = {}; | ||
| 415 | + for (let type in obj) { | ||
| 416 | + let data = obj[type]; | ||
| 417 | + for (let key2 in data) { | ||
| 418 | + this.bannerExposure2(key2, type, data[key2]) | ||
| 419 | + } | ||
| 420 | + } | ||
| 421 | + | ||
| 422 | + } | ||
| 423 | + | ||
| 424 | + | ||
| 425 | + /** | ||
| 426 | + * 构建登录/弱登录公用参数 | ||
| 427 | + */ | ||
| 428 | + private buildParams() { | ||
| 429 | + let APIVersion = '0.6.0';//固定值 | ||
| 430 | + let gameid = GAMEDATA.game_id; | ||
| 431 | + let channel = DataService.I.ChannelId; | ||
| 432 | + let brand = WxSystem.I.brand; | ||
| 433 | + let model = WxSystem.I.model; | ||
| 434 | + let version = WxSystem.I.version; | ||
| 435 | + let system = WxSystem.I.system; | ||
| 436 | + let platform = WxSystem.I.platform; | ||
| 437 | + let sdkversion = WxSystem.I.SDKVersion; | ||
| 438 | + let scene = DataService.I.Scene + ''; | ||
| 439 | + let uid = this.UserId; | ||
| 440 | + let env = DataService.I.EnvEnum === 1 ? 'pre' : 'prod'; | ||
| 441 | + return { | ||
| 442 | + gameid, | ||
| 443 | + channel, | ||
| 444 | + APIVersion, | ||
| 445 | + brand, | ||
| 446 | + model, | ||
| 447 | + version, | ||
| 448 | + system, | ||
| 449 | + platform, | ||
| 450 | + sdkversion, | ||
| 451 | + scene, | ||
| 452 | + uid, | ||
| 453 | + env | ||
| 454 | + }; | ||
| 455 | + } | ||
| 456 | + | ||
| 457 | + private static instance: LogService; | ||
| 458 | + static get I(): LogService { | ||
| 459 | + return this.instance || (this.instance = new LogService()); | ||
| 460 | + } | ||
| 461 | +} | ||
| 0 | \ No newline at end of file | 462 | \ No newline at end of file |
| @@ -0,0 +1,125 @@ | @@ -0,0 +1,125 @@ | ||
| 1 | +import LogService from './LogService'; | ||
| 2 | +import DataService from './DataService'; | ||
| 3 | +import { __LOG__ } from '../base/SDKConst'; | ||
| 4 | +import { SDKApi } from '../http/SDKApi'; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * 在线参数服务类 | ||
| 8 | + */ | ||
| 9 | +export default class OnlineService { | ||
| 10 | + private isDebug: boolean; | ||
| 11 | + private data: any; | ||
| 12 | + | ||
| 13 | + private constructor() { | ||
| 14 | + this.isDebug = false; | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + setData(data){ | ||
| 18 | + if(!data) return | ||
| 19 | + this.data = data; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + // get OnlineKey() { | ||
| 23 | + // return Keys.SDKOnlienKey.replace('env', DataService.I.EnvEnum.toString()); | ||
| 24 | + // } | ||
| 25 | + | ||
| 26 | + /** | ||
| 27 | + * 设置debug模式:true表示进入游戏后,实时获取后台在线参数 false表示缓存10分钟,10分钟后进入刷新最新参数信息 | ||
| 28 | + * 默认在非Debug模式下,是受请求时间间隔的限制的,即两次请求的时间间隔应该大于10分钟;而在Debug模式下,是不受时间间隔限制的。 | ||
| 29 | + * 开发模式下建议设置为true,发布提审设置为false | ||
| 30 | + * @param isDebug 是否debug模式 | ||
| 31 | + */ | ||
| 32 | + setDebugMode(isDebug: boolean) { | ||
| 33 | + this.isDebug = isDebug; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + /** | ||
| 37 | + * 请求在线参数 | ||
| 38 | + * 在游戏启动时候请求在线参数,最早的获取得的在线参数。 | ||
| 39 | + */ | ||
| 40 | + updateOnlineConfig() { | ||
| 41 | + return new Promise((resolve, reject) => { | ||
| 42 | + this.reqOnlineConfig() | ||
| 43 | + .then(ret => resolve(ret)) | ||
| 44 | + .catch(err => reject(err)); | ||
| 45 | + }); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + getParams(key: string) { | ||
| 49 | + if (!key) { | ||
| 50 | + __LOG__ && console.warn('SDK OnlineService 请传递key!'); | ||
| 51 | + return null; | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + let data = this.data; | ||
| 55 | + if (!data) | ||
| 56 | + return null; | ||
| 57 | + | ||
| 58 | + if (!data.hasOwnProperty(key)) { | ||
| 59 | + __LOG__ && console.warn('SDK OnlineService 请在后台配置在线参数key:【' + key + '】'); | ||
| 60 | + return null; | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + return data[key]; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + getParamsObj(...args: any[]): any { | ||
| 67 | + let ret = this.getParams(args[0]); | ||
| 68 | + if (args.length === 2 && !ret) | ||
| 69 | + return args[1]; | ||
| 70 | + | ||
| 71 | + if (!ret) | ||
| 72 | + return ret; | ||
| 73 | + | ||
| 74 | + return JSON.parse(ret); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + getParamsInt(...args: any[]): number { | ||
| 78 | + let ret = this.getParams(args[0]); | ||
| 79 | + if (args.length === 2 && !ret) | ||
| 80 | + return args[1]; | ||
| 81 | + | ||
| 82 | + return +ret; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + getParamsString(...args: any[]): string { | ||
| 86 | + let ret = this.getParams(args[0]); | ||
| 87 | + if (args.length === 2 && !ret) | ||
| 88 | + return args[1]; | ||
| 89 | + | ||
| 90 | + return ret; | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + private reqOnlineConfig() { | ||
| 94 | + return new Promise((resolve, reject) => { | ||
| 95 | + SDKApi.getConfig() | ||
| 96 | + .then((ret: any) => { | ||
| 97 | + if (this.data = ret && ret.data && ret.data.config) { | ||
| 98 | + this.data = ret.data.config | ||
| 99 | + } | ||
| 100 | + // console.log("OnlineConfig",ret) | ||
| 101 | + resolve(this.data); | ||
| 102 | + }) | ||
| 103 | + .catch(err => { | ||
| 104 | + reject(err); | ||
| 105 | + }); | ||
| 106 | + }); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + // private initParams() { | ||
| 110 | + // let adObj = this.getParamsObj(OnlineKeys.ADCrossPromotion, { | ||
| 111 | + // 'Cross': 0, // 交叉推广 | ||
| 112 | + // 'Drawer': 0, // 抽屉广告位 | ||
| 113 | + // 'GuessLike': 0 // 猜你喜欢 | ||
| 114 | + // }); | ||
| 115 | + // DataService.I | ||
| 116 | + // .setIsCross(adObj.Cross === 1) | ||
| 117 | + // .setIsDrawer(adObj.Drawer === 1) | ||
| 118 | + // .setIsGuessLike(adObj.GuessLike === 1); | ||
| 119 | + // } | ||
| 120 | + | ||
| 121 | + private static instance: OnlineService; | ||
| 122 | + static get I(): OnlineService { | ||
| 123 | + return this.instance || (this.instance = new OnlineService()); | ||
| 124 | + } | ||
| 125 | +} |
| @@ -0,0 +1,419 @@ | @@ -0,0 +1,419 @@ | ||
| 1 | +import { ShareVideoType, ShareVideoFrom } from "../base/SDKEnum"; | ||
| 2 | +import RandomUtils from "../utils/RandomUtils"; | ||
| 3 | +import ShareData from "./entity/ShareData"; | ||
| 4 | +import SDKShare from "../share/SDKShare"; | ||
| 5 | +import { __LOG__, GAMEDATA } from "../base/SDKConst"; | ||
| 6 | +import { SDKApi } from "../http/SDKApi"; | ||
| 7 | +import DataService from "./DataService"; | ||
| 8 | +import SDKVideo from "../share/SDKVideo"; | ||
| 9 | + | ||
| 10 | +export default class ShareVideoService { | ||
| 11 | + private forwardKey?: string; | ||
| 12 | + private shareObjs: { [key: string]: Array<ShareData> }; | ||
| 13 | + | ||
| 14 | + private constructor() { | ||
| 15 | + this.forwardKey = 'forward'; | ||
| 16 | + this.shareObjs = { | ||
| 17 | + 'default': [GAMEDATA.default_share] | ||
| 18 | + } | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + /** | ||
| 22 | + * 设置右上角转发key | ||
| 23 | + * @param shareKey 右上角转发key | ||
| 24 | + */ | ||
| 25 | + setForwardKey(shareKey: string) { | ||
| 26 | + this.forwardKey = shareKey; | ||
| 27 | + this.forward(this.forwardKey); | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + async init() { | ||
| 31 | + let data = await SDKApi.ShareList(); | ||
| 32 | + this.setShareVideoData(data); | ||
| 33 | + // console.log(JSON.stringify(data)) | ||
| 34 | + SDKShare.I.updateShareMenu(true);//打开群分享 | ||
| 35 | + | ||
| 36 | + if (GAMEDATA.shareMessageToFriend.scene > 0) { | ||
| 37 | + wx.setMessageToFriendQuery({ shareMessageToFriendScene: GAMEDATA.shareMessageToFriend.scene }) | ||
| 38 | + } | ||
| 39 | + if (this.forwardKey) this.forward(this.forwardKey); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + preload = true; | ||
| 43 | + preloadVideo() { | ||
| 44 | + SDKVideo.I.preloadVideo(GAMEDATA.videoAd); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + /** | ||
| 48 | + * 分享 | ||
| 49 | + * @param shareKey | ||
| 50 | + * @param params | ||
| 51 | + * @param opts 目前支持4个key 1,title自定义分享标题 2,img_url自定义分享图片 3,share_type(不走后台配置写死走视频or分享。1分享2视频3无视频则分享)4,closeSimulate是否关闭模拟分享 | ||
| 52 | + */ | ||
| 53 | + share(shareKey: string, params: any = {}, opts: any = {}): Promise<any> { | ||
| 54 | + return new Promise((resolve, reject) => { | ||
| 55 | + let { id, typ, content, key, icon, videoid } = this.getShareVideoData(shareKey); | ||
| 56 | + let _params = { | ||
| 57 | + title: content, | ||
| 58 | + imageUrl: icon, | ||
| 59 | + share_id: id, | ||
| 60 | + query: this.createQuery({ share_key: key, share_id: id }), | ||
| 61 | + }; | ||
| 62 | + if (opts) { | ||
| 63 | + if (opts.title) _params.title = opts.title; | ||
| 64 | + if (opts.img_url) _params.imageUrl = opts.img_url; | ||
| 65 | + if (opts.share_type) typ = opts.share_type; | ||
| 66 | + } | ||
| 67 | + // console.log("share_query",JSON.stringify(_params), _params.query); | ||
| 68 | + switch (+typ) { | ||
| 69 | + case ShareVideoType.Video: | ||
| 70 | + SDKVideo.I.show(shareKey, videoid).then(success => { | ||
| 71 | + resolve(success) | ||
| 72 | + }).catch(err => { | ||
| 73 | + reject(err); | ||
| 74 | + }) | ||
| 75 | + break; | ||
| 76 | + case ShareVideoType.VideoToShare: | ||
| 77 | + SDKVideo.I.show(shareKey, videoid).then(success => { | ||
| 78 | + resolve(success) | ||
| 79 | + }).catch(err => { | ||
| 80 | + if (err.code !== 1000 && err.code !== 1003) {//1000关闭1003正在播放 | ||
| 81 | + SDKShare.I.share(shareKey, _params, opts).then(success => { | ||
| 82 | + resolve(success) | ||
| 83 | + }).catch(err2 => { | ||
| 84 | + reject(err2) | ||
| 85 | + }) | ||
| 86 | + } else { | ||
| 87 | + reject(err) | ||
| 88 | + } | ||
| 89 | + }) | ||
| 90 | + break; | ||
| 91 | + case ShareVideoType.Share: | ||
| 92 | + SDKShare.I.share(shareKey, _params, opts).then(success => { | ||
| 93 | + resolve(success) | ||
| 94 | + }).catch(err => { | ||
| 95 | + reject(err) | ||
| 96 | + }) | ||
| 97 | + break; | ||
| 98 | + case ShareVideoType.None: | ||
| 99 | + default: | ||
| 100 | + resolve({}) | ||
| 101 | + break; | ||
| 102 | + } | ||
| 103 | + // share_desc = opts.shareTitle || share_desc; | ||
| 104 | + // share_desc = StringUtils.stringFormat(share_desc, opts.formater); | ||
| 105 | + // let shareMsg = { | ||
| 106 | + // title: share_desc, | ||
| 107 | + // imageUrl: opts.shareImg || share_icon, | ||
| 108 | + // query | ||
| 109 | + // }; | ||
| 110 | + // // __LOG__ && console.error(`SDK ShareVideoService ${opts.shareForward ? 'forward 右上角分享:' : 'share 主动拉起分享:'}`); | ||
| 111 | + // // __LOG__ && console.error(`SDK ShareVideoService 分享参数对象shareMsg: ${JSON.stringify(shareMsg)}`); | ||
| 112 | + // // __LOG__ && console.error('SDK ShareVideoService 分享扩展参数', opts); | ||
| 113 | + // DebugUtils.I.dynamic(`====> PCSDK ShareVideoService share 分享query: ${query}`); | ||
| 114 | + // let success = (ret: any) => { | ||
| 115 | + // // __LOG__ && console.error(`SDK ShareVideoService ${opts.shareForward ? 'forward 右上角分享:' : 'share 主动拉起分享:'} 成功回调: ${share_id}`); | ||
| 116 | + // if (!opts.__GROUP) { | ||
| 117 | + // // 不是群分享的情况下,进行统计,群分享需要验证 | ||
| 118 | + // TempService.I.add(TempDataKeys.ShareSuccess, 1); | ||
| 119 | + // share_id && LogService.I.tacticShare(share_id, share_key, TacticType.ShareSuccess); | ||
| 120 | + // } | ||
| 121 | + // resolve({ ...ret, share_id, share_key: shareKey }); | ||
| 122 | + // }; | ||
| 123 | + // let fail = (err: any) => { | ||
| 124 | + // // __LOG__ && console.error(`SDK ShareVideoService ${opts.shareForward ? 'forward 右上角分享:' : 'share 主动拉起分享:'} 失败回调: ${share_id}`); | ||
| 125 | + // if (!opts.__GROUP) { | ||
| 126 | + // share_id && LogService.I.tacticShare(share_id, share_key, TacticType.ShareInterrupt); | ||
| 127 | + // } | ||
| 128 | + // reject({ ...err, share_id, share_key: shareKey }); | ||
| 129 | + // }; | ||
| 130 | + // if (opts.shareForward) | ||
| 131 | + // // 右上角转发 | ||
| 132 | + // Platform.I.forward(shareMsg, { | ||
| 133 | + // ...opts, | ||
| 134 | + // success, | ||
| 135 | + // fail, | ||
| 136 | + // context: this | ||
| 137 | + // }); | ||
| 138 | + // else | ||
| 139 | + // // 普通分享 | ||
| 140 | + // Platform.I.share(shareMsg, opts) | ||
| 141 | + // .then((ret: any) => success(ret)) | ||
| 142 | + // .catch((err: any) => fail(err)); | ||
| 143 | + }); | ||
| 144 | + } | ||
| 145 | + | ||
| 146 | + /** | ||
| 147 | + * 右上角转发 | ||
| 148 | + */ | ||
| 149 | + forward(shareKey?: string) { | ||
| 150 | + let data = this.getShareVideoData(shareKey ? shareKey : this.forwardKey); | ||
| 151 | + let params = { | ||
| 152 | + title: data.content, | ||
| 153 | + imageUrl: data.icon, | ||
| 154 | + query: this.createQuery({ share_key: data.key, share_id: data.id }), | ||
| 155 | + }; | ||
| 156 | + // console.log("forward", JSON.stringify(params)); | ||
| 157 | + SDKShare.I.forward(params); | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + /** | ||
| 161 | + * 分享自动入口 | ||
| 162 | + * @param shareKey | ||
| 163 | + * @param opts { | ||
| 164 | + * type: 当shareType为VideoAndShare时候,该参数有用,0:分享 1:视频 | ||
| 165 | + * context: 函数执行上下文 | ||
| 166 | + * fail: Function 失败函数 | ||
| 167 | + * success: Function 成功函数 | ||
| 168 | + * } | ||
| 169 | + */ | ||
| 170 | + // dispatch(shareKey: string, opts: any = {}, queryObj: { [key: string]: number | string } = {}) { | ||
| 171 | + // let shareData = this.getShareVideoData(shareKey); | ||
| 172 | + // let { share_open, share_wxad_id } = shareData; | ||
| 173 | + // opts = { | ||
| 174 | + // ...opts, | ||
| 175 | + // share_wxad_id, | ||
| 176 | + // __ShareData__: shareData | ||
| 177 | + // }; | ||
| 178 | + // this.dispatchType(+share_open, shareKey, opts, queryObj); | ||
| 179 | + // } | ||
| 180 | + | ||
| 181 | + shareDispatch(shareKey: string, opts: any = {}, queryObj: { [key: string]: number | string } = {}) { | ||
| 182 | + // this.dispatch(shareKey, opts, queryObj); | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + /** | ||
| 186 | + * 根据分享类型进行处理 | ||
| 187 | + * @param shareType | ||
| 188 | + * @param shareKey | ||
| 189 | + * @param opts { | ||
| 190 | + * type: 当shareType为VideoAndShare时候,该参数有用,0:分享 1:视频 | ||
| 191 | + * context: 函数执行上下文 | ||
| 192 | + * fail: Function 失败函数 | ||
| 193 | + * success: Function 成功函数 | ||
| 194 | + * } | ||
| 195 | + */ | ||
| 196 | + dispatchType(shareType: ShareVideoType, shareKey: string, opts: any = {}, queryObj: { [key: string]: number | string } = {}) { | ||
| 197 | + // if (Platform.IsWx && OnlineService.I.getParamsInt(OnlineKeys.ShareUnlock, 1) !== 1) | ||
| 198 | + // shareType = ShareVideoType.None; | ||
| 199 | + | ||
| 200 | + // let share_wxad_id = opts.share_wxad_id || (opts.__ShareData__ || this.getShareVideoData(shareKey) || {}).share_wxad_id; | ||
| 201 | + // switch (shareType) { | ||
| 202 | + // case ShareVideoType.None: // 无分享 | ||
| 203 | + // this.handleSuccess(opts, shareType, ShareVideoFrom.None, null); | ||
| 204 | + // break; | ||
| 205 | + | ||
| 206 | + // case ShareVideoType.Share: // 同步分享 | ||
| 207 | + // // 同步分享点:是否开启后台配置中如果存在视频ID,则自动切换为视频点 | ||
| 208 | + // if (opts.shareAutoVideo && share_wxad_id) { | ||
| 209 | + // this.dispatchType(ShareVideoType.VideoToShare, shareKey, opts, queryObj); | ||
| 210 | + // break; | ||
| 211 | + // } | ||
| 212 | + // this.group(shareKey, queryObj, opts) | ||
| 213 | + // .then(ret => this.handleSuccess(opts, shareType, ShareVideoFrom.Share, ret)) | ||
| 214 | + // .catch(err => this.handleFail(opts, shareType, ShareVideoFrom.Share, err)); | ||
| 215 | + // break; | ||
| 216 | + | ||
| 217 | + // case ShareVideoType.ShareAysnc: // 异步分享 | ||
| 218 | + // break; | ||
| 219 | + | ||
| 220 | + // case ShareVideoType.ShareIntegral: // 分享积分 | ||
| 221 | + // // 是否开启分享积分,且配置为分享积分 | ||
| 222 | + // if (IntegralService.I.IsOpen) { | ||
| 223 | + // let data = IntegralService.I.convert(); | ||
| 224 | + // if (data) { | ||
| 225 | + // opts.shareIntegralData = data; | ||
| 226 | + // this.dispatchType(data.shareType, shareKey, opts, queryObj); | ||
| 227 | + // } else { | ||
| 228 | + // // 超过了现在,直接执行失败:今日已达分享上限次数,请明日再来 | ||
| 229 | + // this.handleFail(opts, shareType, ShareVideoFrom.Share, { ...ShareVideoError.ShareOverLimit }); | ||
| 230 | + // } | ||
| 231 | + // } else { | ||
| 232 | + // // 如果配置了分享积分,但是未开启开关,则推:无视频则分享 | ||
| 233 | + // this.dispatchType(ShareVideoType.VideoToShare, shareKey, opts, queryObj); | ||
| 234 | + // } | ||
| 235 | + // break; | ||
| 236 | + | ||
| 237 | + // case ShareVideoType.Video: // 看视频 | ||
| 238 | + // Platform.I.video(shareKey, share_wxad_id) | ||
| 239 | + // .then(ret => this.handleSuccess(opts, shareType, ShareVideoFrom.Video, ret)) | ||
| 240 | + // .catch((err: any) => this.handleFail(opts, shareType, ShareVideoFrom.Video, err)); | ||
| 241 | + // break; | ||
| 242 | + | ||
| 243 | + // case ShareVideoType.VideoToShare: // 无视频则分享 | ||
| 244 | + // if (!share_wxad_id) { | ||
| 245 | + // this.group(shareKey, queryObj, opts) | ||
| 246 | + // .then(ret => this.handleSuccess(opts, shareType, ShareVideoFrom.Share, ret)) | ||
| 247 | + // .catch(err => this.handleFail(opts, shareType, ShareVideoFrom.Share, err)); | ||
| 248 | + // return; | ||
| 249 | + // } | ||
| 250 | + // Platform.I.video(shareKey, share_wxad_id) | ||
| 251 | + // .then((ret) => this.handleSuccess(opts, shareType, ShareVideoFrom.Video, ret)) | ||
| 252 | + // .catch((err: any) => { | ||
| 253 | + // // 拉取视频失败/视频UID不存在/微信版本过低,暂不支持看视频,自动切换到分享 | ||
| 254 | + // let { VideoFail, VideoInvalid, VideoNotOpen } = ShareVideoError; | ||
| 255 | + // if (err && (err.code === VideoFail.code || err.code === VideoInvalid.code || err.code === VideoNotOpen.code)) | ||
| 256 | + // this.group(shareKey, queryObj, opts) | ||
| 257 | + // .then(ret => this.handleSuccess(opts, shareType, ShareVideoFrom.Share, ret)) | ||
| 258 | + // .catch(err => this.handleFail(opts, shareType, ShareVideoFrom.Share, err)); | ||
| 259 | + // else | ||
| 260 | + // this.handleFail(opts, shareType, ShareVideoFrom.Video, err); | ||
| 261 | + // }); | ||
| 262 | + // break; | ||
| 263 | + | ||
| 264 | + // case ShareVideoType.VideoAndShare: // 看视频 | ||
| 265 | + // if (share_wxad_id && opts.type === 1) { | ||
| 266 | + // Platform.I.video(shareKey, share_wxad_id) | ||
| 267 | + // .then(ret => this.handleSuccess(opts, shareType, ShareVideoFrom.Video, ret)) | ||
| 268 | + // .catch((err: any) => this.handleFail(opts, shareType, ShareVideoFrom.Video, err)); | ||
| 269 | + // } else { | ||
| 270 | + // this.group(shareKey, queryObj, opts) | ||
| 271 | + // .then(ret => this.handleSuccess(opts, shareType, ShareVideoFrom.Share, ret)) | ||
| 272 | + // .catch(err => this.handleFail(opts, shareType, ShareVideoFrom.Share, err)); | ||
| 273 | + // } | ||
| 274 | + // break; | ||
| 275 | + // } | ||
| 276 | + } | ||
| 277 | + | ||
| 278 | + shareWithType(shareType: ShareVideoType, shareKey: string, opts: any = {}, queryObj: { [key: string]: number | string } = {}) { | ||
| 279 | + this.dispatchType(shareType, shareKey, opts, queryObj); | ||
| 280 | + } | ||
| 281 | + | ||
| 282 | + /** | ||
| 283 | + * 分享id | ||
| 284 | + * @param shareKey | ||
| 285 | + */ | ||
| 286 | + getShareVideoID(shareKey: string): string { | ||
| 287 | + let shareData = this.getShareVideoData(shareKey); | ||
| 288 | + return shareData.id; | ||
| 289 | + } | ||
| 290 | + /** | ||
| 291 | + * videoid | ||
| 292 | + * @param shareKey | ||
| 293 | + */ | ||
| 294 | + getShareVideoID2(shareKey: string): string { | ||
| 295 | + let shareData = this.getShareVideoData(shareKey); | ||
| 296 | + return shareData.videoid; | ||
| 297 | + } | ||
| 298 | + | ||
| 299 | + getShareVideoType(shareKey: string): ShareVideoType { | ||
| 300 | + let shareData = this.getShareVideoData(shareKey); | ||
| 301 | + let shareType: ShareVideoType = +shareData.typ; | ||
| 302 | + // // 木有开启分享,直接返回None | ||
| 303 | + // if (Platform.IsWx && OnlineService.I.getParamsInt(OnlineKeys.ShareUnlock, 1) !== 1) | ||
| 304 | + // return ShareVideoType.None; | ||
| 305 | + | ||
| 306 | + // // 后台配置视频,判断是否加载出错过,出错返回分享,否则直接返回 | ||
| 307 | + // if (shareType === ShareVideoType.Video || shareType === ShareVideoType.VideoAndShare || shareType === ShareVideoType.VideoToShare) { | ||
| 308 | + // // 检测视频加载失败 | ||
| 309 | + // if (Platform.I.isVideoErrored()) | ||
| 310 | + // return ShareVideoType.Share; | ||
| 311 | + // else | ||
| 312 | + // return shareType; | ||
| 313 | + // } | ||
| 314 | + | ||
| 315 | + // // 是否开启分享积分,且配置为分享积分 | ||
| 316 | + // if (shareType === ShareVideoType.ShareIntegral && IntegralService.I.IsOpen) { | ||
| 317 | + // let data = IntegralService.I.convert(); | ||
| 318 | + // if (data) | ||
| 319 | + // return data.shareType; | ||
| 320 | + // } | ||
| 321 | + return shareType; | ||
| 322 | + } | ||
| 323 | + | ||
| 324 | + getShareType(shareKey: string): ShareVideoType { | ||
| 325 | + return this.getShareVideoType(shareKey); | ||
| 326 | + } | ||
| 327 | + | ||
| 328 | + getType(shareKey: string): ShareVideoType { | ||
| 329 | + return this.getShareVideoType(shareKey); | ||
| 330 | + } | ||
| 331 | + | ||
| 332 | + private handleSuccess(opts: any, shareType: ShareVideoType, from: ShareVideoFrom, ret: any) { | ||
| 333 | + // // 判断分享规则 | ||
| 334 | + // if (IntegralService.I.IsOpen && opts.shareIntegralData && from === ShareVideoFrom.Share) { | ||
| 335 | + // let shareNum = IntegralService.I.IntegralShareNum; | ||
| 336 | + // if (shareNum === 0) { | ||
| 337 | + // // 第1次分享 | ||
| 338 | + // IntegralService.I.setIntegralShareNum(); | ||
| 339 | + // IntegralService.I.resetShareRatio(); | ||
| 340 | + // __LOG__ && console.error(`SDK ShareVideoService 分享积分 第1次分享: 初始来源:${shareType}, 类型:${from}, 分享次数:${shareNum}`); | ||
| 341 | + // // 设定:第一次强制失败 | ||
| 342 | + // // return this.handleFail(opts, shareType, from, { ...ShareVideoError.ShareRuleFail }, true); | ||
| 343 | + // } else { | ||
| 344 | + // // 第n次分享 | ||
| 345 | + // IntegralService.I.setIntegralShareNum(); | ||
| 346 | + // IntegralService.I.addShareRatio(); | ||
| 347 | + | ||
| 348 | + // let shareRatio = IntegralService.I.ShareRatio; | ||
| 349 | + // let radomRatio = +Math.random().toFixed(2); | ||
| 350 | + // __LOG__ && console.error(`SDK ShareVideoService 分享积分 第${shareNum}次分享: 初始来源:${shareType}, 类型:${from}, 分享次数:${shareNum}, 随机概率:${radomRatio}, 失败概率:${shareRatio}`); | ||
| 351 | + // if (radomRatio <= shareRatio) { | ||
| 352 | + // __LOG__ && console.error(`SDK ShareVideoService 分享积分 触发失败规则:分享强制失败, radomRatio <= shareRatio: ${radomRatio} <= ${shareRatio}`); | ||
| 353 | + // return this.handleFail(opts, shareType, from, { ...ShareVideoError.ShareRuleFail }, true); | ||
| 354 | + // } | ||
| 355 | + // } | ||
| 356 | + | ||
| 357 | + // // 是否同步分享积分数据 | ||
| 358 | + // let { intergral, isAsyncNum } = opts.shareIntegralData; | ||
| 359 | + // isAsyncNum && LocalService.I.saveVideoOverShareNum(intergral); | ||
| 360 | + // __LOG__ && isAsyncNum && console.error(`SDK ShareVideoService 分享积分 触发视频看完后,再次分享次数限制更新成功,成功后数量:${LocalService.I.getVideoOverShareNum(intergral)}`); | ||
| 361 | + // } | ||
| 362 | + | ||
| 363 | + // let success = opts.success || function () { }; | ||
| 364 | + // let context = opts.context || this; | ||
| 365 | + // typeof success === 'function' && success.call(context, from, ret); | ||
| 366 | + // (from === ShareVideoFrom.Share) && TempService.I.add(TempDataKeys.ShareSuccess, 1); | ||
| 367 | + } | ||
| 368 | + | ||
| 369 | + private handleFail(opts: any, shareType: ShareVideoType, from: ShareVideoFrom, err: any, isFromIntegral?: boolean) { | ||
| 370 | + // if (err instanceof Error) return; | ||
| 371 | + // // 判断来自分享积分规则,则重置分享概率 | ||
| 372 | + // if (IntegralService.I.IsOpen && opts.shareIntegralData && from === ShareVideoFrom.Share) { | ||
| 373 | + // __LOG__ && console.error(`SDK ShareVideoService 强制失败分享,回滚到: ${IntegralService.I.ShareRatioInit}`); | ||
| 374 | + // IntegralService.I.resetShareRatio(); | ||
| 375 | + // } | ||
| 376 | + // let fail = opts.fail || function () { }; | ||
| 377 | + // let context = opts.context || this; | ||
| 378 | + // fail && fail.call(context, from, err); | ||
| 379 | + } | ||
| 380 | + | ||
| 381 | + private setShareVideoData(data: any) { | ||
| 382 | + (data.data || []).forEach((item: any) => { | ||
| 383 | + this.shareObjs[item.key] = this.shareObjs[item.key] || []; | ||
| 384 | + this.shareObjs[item.key].push(item); | ||
| 385 | + }); | ||
| 386 | + } | ||
| 387 | + | ||
| 388 | + private getShareVideoData(shareKey: string): ShareData { | ||
| 389 | + let list: Array<ShareData> = this.shareObjs[shareKey]; | ||
| 390 | + if (!list) { | ||
| 391 | + list = this.shareObjs.default; | ||
| 392 | + // let shareData: ShareData = CfgManager.I.config.ShareData; | ||
| 393 | + // if (!shareData) throw new TypeError('SDK ShareVideoService - 请在config.js中配置ShareData'); | ||
| 394 | + // let share_open = shareData.share_wxad_id ? ShareVideoType.Video : ShareVideoType.Share; | ||
| 395 | + // return { ...shareData, share_id: 99999 + '', share_key: shareKey, share_open }; | ||
| 396 | + } | ||
| 397 | + let index = RandomUtils.rand(0, list.length); | ||
| 398 | + return list[index]; | ||
| 399 | + } | ||
| 400 | + | ||
| 401 | + private createQuery(params = {}): string { | ||
| 402 | + params = { | ||
| 403 | + ...params, | ||
| 404 | + channel_id: GAMEDATA.channel_id, | ||
| 405 | + user_invite_uid: DataService.I.UserId | ||
| 406 | + }; | ||
| 407 | + let query = ''; | ||
| 408 | + for (let key in params) { | ||
| 409 | + query.length && (query += '&'); | ||
| 410 | + query += `${key}=${params[key]}`; | ||
| 411 | + } | ||
| 412 | + return query; | ||
| 413 | + } | ||
| 414 | + | ||
| 415 | + private static instance: ShareVideoService; | ||
| 416 | + static get I(): ShareVideoService { | ||
| 417 | + return this.instance || (this.instance = new ShareVideoService()); | ||
| 418 | + } | ||
| 419 | +} |
| @@ -0,0 +1,82 @@ | @@ -0,0 +1,82 @@ | ||
| 1 | +import { Gender, NetworkType } from "../../base/SDKEnum"; | ||
| 2 | + | ||
| 3 | +export default class SdkData { | ||
| 4 | + public token: string; // 游戏token | ||
| 5 | + public refToken: string; // 刷新凭证,可以在token失效前刷新token有效期 | ||
| 6 | + public expice: number; // token失效时间 | ||
| 7 | + public isnew: number; // 是否新用户1新用户0老用户 | ||
| 8 | + public scene: string; // 微信场景 | ||
| 9 | + public openId: string; // 用户openid | ||
| 10 | + public userId: number; // 用户id | ||
| 11 | + public regTime: string; // 注册账号时间 | ||
| 12 | + public launchTime: number; // 启动时间 | ||
| 13 | + public shareId: number; // 分享id | ||
| 14 | + public shareKey: string; // 分享key | ||
| 15 | + public platform: string; // 手机平台 ios, android | ||
| 16 | + public networkType: NetworkType; // 网络类型 | ||
| 17 | + public gender: Gender; // 性别 | ||
| 18 | + public channelId: number; // 渠道ID, 原始最终渠道id,会被后台返回的渠道覆盖 | ||
| 19 | + public queryChannelId: number; // 渠道ID, query渠道id,不会被后台返回的渠道覆盖 | ||
| 20 | + public inviteType: any; // 会话来源类型 | ||
| 21 | + public shareTicket: string; // 会话群分享ticket | ||
| 22 | + public systemId: number; // 系统类型: 0 iOS, 1 Android | ||
| 23 | + public launchKey: string; // 启动key,自动生成并缓存 | ||
| 24 | + public launchSource: number; // 启动来源 | ||
| 25 | + public authorize: boolean; // 是否已经登录授权 | ||
| 26 | + public referrerInfo: any; // referrerInfo信息 | ||
| 27 | + public version: string; // 服务端接口版本号 | ||
| 28 | + public cdnUrl: string; // cdn动态地址 | ||
| 29 | + public envEnum: number; // api环境变量 1测试 2正式 | ||
| 30 | + public inviteUid: number; // 用户来源邀请者ID,login后获取得到 | ||
| 31 | + public queryUserInviteUid: number; // 分享获取得到的query的邀请者id | ||
| 32 | + public queryExtData: object; // query扩展数据 | ||
| 33 | + public shield: number; // 是否开启屏蔽 | ||
| 34 | + public configParams: any; // 启动配置功能参数 | ||
| 35 | + public userState: UserState; // 启动配置功能参数 | ||
| 36 | + public isCross: boolean; // 是否开启交叉悬浮框推广位 | ||
| 37 | + public isDrawer: boolean; // 是否开启交叉抽屉推广位 | ||
| 38 | + public isGuessLike: boolean; // 是否开启交叉猜你喜欢推广位 | ||
| 39 | + | ||
| 40 | + constructor() { | ||
| 41 | + this.token = ''; | ||
| 42 | + this.refToken = ''; | ||
| 43 | + this.expice = 0; | ||
| 44 | + this.isnew = 0; | ||
| 45 | + this.scene = ''; | ||
| 46 | + this.openId = ''; | ||
| 47 | + this.userId = 0; | ||
| 48 | + this.regTime = ''; | ||
| 49 | + this.launchTime = 0; | ||
| 50 | + this.shareId = 0; | ||
| 51 | + this.shareKey = ''; | ||
| 52 | + this.platform = ''; | ||
| 53 | + this.networkType = NetworkType.Unknown; | ||
| 54 | + this.gender = Gender.Unknown; // 未知 | ||
| 55 | + this.channelId = 0; | ||
| 56 | + this.queryChannelId = 0; | ||
| 57 | + this.queryExtData = {}; | ||
| 58 | + this.inviteType = 0; | ||
| 59 | + this.shareTicket = ''; | ||
| 60 | + this.systemId = 0; | ||
| 61 | + this.launchKey = ''; | ||
| 62 | + this.launchSource = -1; | ||
| 63 | + this.authorize = false; | ||
| 64 | + this.cdnUrl = ''; | ||
| 65 | + this.envEnum = 2; | ||
| 66 | + this.version = '1.0'; | ||
| 67 | + this.inviteUid = 0; | ||
| 68 | + this.queryUserInviteUid = 0; | ||
| 69 | + this.shield = 0; | ||
| 70 | + this.configParams = null; | ||
| 71 | + this.userState = UserState.Default; | ||
| 72 | + this.isCross = false; | ||
| 73 | + this.isDrawer = false; | ||
| 74 | + this.isGuessLike = false; | ||
| 75 | + } | ||
| 76 | +} | ||
| 77 | + | ||
| 78 | +export enum UserState { | ||
| 79 | + Default = 0, | ||
| 80 | + Old, | ||
| 81 | + New | ||
| 82 | +} | ||
| 0 | \ No newline at end of file | 83 | \ No newline at end of file |
| @@ -0,0 +1,9 @@ | @@ -0,0 +1,9 @@ | ||
| 1 | +{ | ||
| 2 | + "ver": "1.0.8", | ||
| 3 | + "uuid": "f09e38ff-4d68-417e-8904-949127e4d321", | ||
| 4 | + "isPlugin": false, | ||
| 5 | + "loadPluginInWeb": true, | ||
| 6 | + "loadPluginInNative": true, | ||
| 7 | + "loadPluginInEditor": false, | ||
| 8 | + "subMetas": {} | ||
| 9 | +} | ||
| 0 | \ No newline at end of file | 10 | \ No newline at end of file |
| @@ -0,0 +1,21 @@ | @@ -0,0 +1,21 @@ | ||
| 1 | +import { ShareVideoType } from "../../base/SDKEnum"; | ||
| 2 | + | ||
| 3 | +export default class ShareData { | ||
| 4 | + public content: string; | ||
| 5 | + public icon: string; | ||
| 6 | + public id: string; | ||
| 7 | + public key: string; | ||
| 8 | + public title: string; | ||
| 9 | + public typ: number; | ||
| 10 | + public videoid: string; | ||
| 11 | + | ||
| 12 | + constructor() { | ||
| 13 | + this.content = ''; | ||
| 14 | + this.icon = ''; | ||
| 15 | + this.id = ''; | ||
| 16 | + this.key = ''; | ||
| 17 | + this.title = ''; | ||
| 18 | + this.typ = ShareVideoType.None; | ||
| 19 | + this.videoid = ''; | ||
| 20 | + } | ||
| 21 | +} | ||
| 0 | \ No newline at end of file | 22 | \ No newline at end of file |
| @@ -0,0 +1,9 @@ | @@ -0,0 +1,9 @@ | ||
| 1 | +{ | ||
| 2 | + "ver": "1.0.8", | ||
| 3 | + "uuid": "d9db9b13-470b-456d-95ff-75354ec23ca0", | ||
| 4 | + "isPlugin": false, | ||
| 5 | + "loadPluginInWeb": true, | ||
| 6 | + "loadPluginInNative": true, | ||
| 7 | + "loadPluginInEditor": false, | ||
| 8 | + "subMetas": {} | ||
| 9 | +} | ||
| 0 | \ No newline at end of file | 10 | \ No newline at end of file |
| @@ -0,0 +1,123 @@ | @@ -0,0 +1,123 @@ | ||
| 1 | +import SimulateShare from "./SimulateShare"; | ||
| 2 | +import { __LOG__, ShareVideoError } from "../base/SDKConst"; | ||
| 3 | +import LogService from "../service/LogService"; | ||
| 4 | +import { DOT_SHARE_TYPE } from "../base/SDKEnum"; | ||
| 5 | + | ||
| 6 | +export default class SDKShare { | ||
| 7 | + private shareSimulate: boolean | ||
| 8 | + | ||
| 9 | + private static instance: SDKShare; | ||
| 10 | + static get I(): SDKShare { | ||
| 11 | + return this.instance || (this.instance = new SDKShare()); | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + private constructor() { | ||
| 15 | + this.shareSimulate = true; | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + /** | ||
| 19 | + * 分享 | ||
| 20 | + * @param shareKey | ||
| 21 | + * @param params { | ||
| 22 | + * imageUrlId ?: string; 审核通过的图片 ID,详见 使用审核通过的转发图片 | ||
| 23 | + * } | ||
| 24 | + * @param opts | ||
| 25 | + */ | ||
| 26 | + share(shareKey: string, params: any, opts: any = {}): Promise<any> { | ||
| 27 | + return new Promise((resolve, reject) => { | ||
| 28 | + let { title, imageUrl, query, imageUrlId, share_id, withShareTicket } = params; | ||
| 29 | + let commonObj = { | ||
| 30 | + title, | ||
| 31 | + imageUrl, | ||
| 32 | + query, | ||
| 33 | + imageUrlId: imageUrlId || '', | ||
| 34 | + }; | ||
| 35 | + let callbackObj = { | ||
| 36 | + success: (ret?: any) => { | ||
| 37 | + __LOG__ && console.error('WxShare share success'); | ||
| 38 | + LogService.I.share(shareKey, share_id, DOT_SHARE_TYPE.share) | ||
| 39 | + resolve(ret); | ||
| 40 | + }, | ||
| 41 | + fail: (err?: any) => { | ||
| 42 | + __LOG__ && console.error('WxShare share fail'); | ||
| 43 | + reject({ ...ShareVideoError.ShareNotGroup }); | ||
| 44 | + }, | ||
| 45 | + cancel: () => { | ||
| 46 | + __LOG__ && console.error('WxShare share cancel', null); | ||
| 47 | + reject(null); | ||
| 48 | + } | ||
| 49 | + }; | ||
| 50 | + // 是否模拟分享 | ||
| 51 | + this.shareSimulate && !opts.closeSimulate && this.simulate({ ...callbackObj }); | ||
| 52 | + | ||
| 53 | + if(this.shareSimulate && opts.closeSimulate){ | ||
| 54 | + LogService.I.share(shareKey, share_id, DOT_SHARE_TYPE.share) | ||
| 55 | + } | ||
| 56 | + // 主动拉起转发 | ||
| 57 | + wx.shareAppMessage(commonObj); | ||
| 58 | + }); | ||
| 59 | + } | ||
| 60 | + /** | ||
| 61 | + * 打开群分享 | ||
| 62 | + * @param value | ||
| 63 | + */ | ||
| 64 | + updateShareMenu(value) { | ||
| 65 | + if (typeof wx == 'undefined') return | ||
| 66 | + wx.updateShareMenu({ | ||
| 67 | + withShareTicket: value | ||
| 68 | + }); | ||
| 69 | + } | ||
| 70 | + /** | ||
| 71 | + * 右上角转发 | ||
| 72 | + * @param params | ||
| 73 | + * @param opts | ||
| 74 | + */ | ||
| 75 | + forward(params: any, opts: any = {}) { | ||
| 76 | + let me = this; | ||
| 77 | + let { title, imageUrl, query, imageUrlId } = params; | ||
| 78 | + wx.onShareAppMessage(function () { | ||
| 79 | + let obj = { | ||
| 80 | + title, | ||
| 81 | + imageUrl, | ||
| 82 | + query, | ||
| 83 | + imageUrlId: imageUrlId || '', | ||
| 84 | + success: (ret?: any) => { | ||
| 85 | + __LOG__ && console.error('WxShare forward success'); | ||
| 86 | + opts.success && opts.success.call(opts.context, ret); | ||
| 87 | + }, | ||
| 88 | + fail: (err?: any) => { | ||
| 89 | + __LOG__ && console.error('WxShare forward fail'); | ||
| 90 | + opts.fail && opts.fail.call(opts.context, err); | ||
| 91 | + }, | ||
| 92 | + cancel: () => { | ||
| 93 | + __LOG__ && console.error('WxShare forward cancel', null); | ||
| 94 | + opts.fail && opts.fail.call(opts.context, null); | ||
| 95 | + } | ||
| 96 | + }; | ||
| 97 | + // 模拟分享 | ||
| 98 | + me.shareSimulate && !opts.closeSimulate && me.simulate({ ...obj }); | ||
| 99 | + return obj; | ||
| 100 | + }); | ||
| 101 | + // 显示当前页面的转发按钮 | ||
| 102 | + wx.showShareMenu({}); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + private createQuery(params = {}): string { | ||
| 106 | + params = { | ||
| 107 | + ...params, | ||
| 108 | + // channel_id: DataService.I.ChannelId, | ||
| 109 | + // user_invite_uid: DataService.I.UserId | ||
| 110 | + }; | ||
| 111 | + let query = ''; | ||
| 112 | + for (let key in params) { | ||
| 113 | + query.length && (query += '&'); | ||
| 114 | + query += `${key}=${params[key]}`; | ||
| 115 | + } | ||
| 116 | + return query; | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + private simulate(data: any) { | ||
| 120 | + SimulateShare.I.bind(data); | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | +} | ||
| 0 | \ No newline at end of file | 124 | \ No newline at end of file |
| @@ -0,0 +1,189 @@ | @@ -0,0 +1,189 @@ | ||
| 1 | +import { ShareVideoError, __LOG__ } from "../base/SDKConst"; | ||
| 2 | +import { DOT_AD_TYPE, DOT_AD_STATUS } from "../base/SDKEnum"; | ||
| 3 | +import LogService from "../service/LogService"; | ||
| 4 | + | ||
| 5 | +/* | ||
| 6 | +* 激烈视频 | ||
| 7 | +*/ | ||
| 8 | +export default class SDKVideo { | ||
| 9 | + private _isPlaying: boolean; | ||
| 10 | + private _isErrored: boolean; | ||
| 11 | + private resolve: any; | ||
| 12 | + private reject: any; | ||
| 13 | + private videoAd: any; | ||
| 14 | + private videoKey: string; | ||
| 15 | + private adUnitId: string; | ||
| 16 | + | ||
| 17 | + constructor() { | ||
| 18 | + this.videoKey = ''; | ||
| 19 | + this._isPlaying = false; | ||
| 20 | + this._isErrored = false; | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + get isErrored() { | ||
| 24 | + return this._isErrored; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + get isPlaying(): boolean { | ||
| 28 | + return this._isPlaying; | ||
| 29 | + } | ||
| 30 | + private videoAd2: any | ||
| 31 | + private preloadVideoAd: any | ||
| 32 | + private isPreload: boolean = false | ||
| 33 | + preloadVideo(adUnitId: string) { | ||
| 34 | + if (this.preloadVideoAd || this.isPreload) return | ||
| 35 | + this.isPreload = true; | ||
| 36 | + let ad = wx.createRewardedVideoAd({ | ||
| 37 | + adUnitId | ||
| 38 | + }); | ||
| 39 | + ad.onError(this.preError); | ||
| 40 | + ad.load().then(this.handleLoaded2).catch(() => { | ||
| 41 | + this.isPreload = false | ||
| 42 | + }) | ||
| 43 | + this.videoAd2 = ad; | ||
| 44 | + | ||
| 45 | + } | ||
| 46 | + private preError() { | ||
| 47 | + let that = SDKVideo.I; | ||
| 48 | + that.isPreload = false | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + private handleLoaded2() { | ||
| 52 | + let that = SDKVideo.I; | ||
| 53 | + that.preloadVideoAd = that.videoAd2; | ||
| 54 | + that.isPreload = false | ||
| 55 | + __LOG__ && console.warn("视频预加载成功", that.preloadVideoAd) | ||
| 56 | + // that.preloadVideoAd.ttttttt = '111111' | ||
| 57 | + console.warn(that.preloadVideoAd.isReady()); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + offPreload() { | ||
| 61 | + let that = SDKVideo.I; | ||
| 62 | + that.preloadVideoAd.offError(that.preError); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + async show(videoKey: string, adUnitId: string): Promise<any> { | ||
| 66 | + return new Promise(async (resolve, reject) => { | ||
| 67 | + if (this.isPlaying) | ||
| 68 | + return reject({ ...ShareVideoError.VideoPlaying }); | ||
| 69 | + | ||
| 70 | + if (!adUnitId) | ||
| 71 | + return reject({ ...ShareVideoError.VideoInvalid }); | ||
| 72 | + | ||
| 73 | + LogService.I.adStat(videoKey, this.adUnitId, DOT_AD_TYPE.video, DOT_AD_STATUS.request); | ||
| 74 | + let videoAd | ||
| 75 | + let hasVideo | ||
| 76 | + if (this.preloadVideoAd) { | ||
| 77 | + __LOG__ && console.log("使用预加载视频", this.preloadVideoAd) | ||
| 78 | + hasVideo = true | ||
| 79 | + this.offPreload() | ||
| 80 | + videoAd = this.preloadVideoAd; | ||
| 81 | + this.preloadVideoAd = null; | ||
| 82 | + // this.preloadVideo(adUnitId); | ||
| 83 | + } else { | ||
| 84 | + __LOG__ && console.log("不使用预加载视频") | ||
| 85 | + hasVideo = false | ||
| 86 | + videoAd = wx.createRewardedVideoAd({ | ||
| 87 | + adUnitId | ||
| 88 | + }); | ||
| 89 | + // this.preloadVideo(adUnitId); | ||
| 90 | + } | ||
| 91 | + // let videoAd = wx.createRewardedVideoAd({ | ||
| 92 | + // adUnitId | ||
| 93 | + // }); | ||
| 94 | + if (!videoAd) | ||
| 95 | + return reject({ ...ShareVideoError.VideoNotOpen }); | ||
| 96 | + | ||
| 97 | + this._isPlaying = true; | ||
| 98 | + this._isErrored = false; | ||
| 99 | + this.videoKey = videoKey; | ||
| 100 | + this.adUnitId = adUnitId; | ||
| 101 | + this.resolve = resolve; | ||
| 102 | + this.reject = reject; | ||
| 103 | + | ||
| 104 | + this.videoAd = videoAd; | ||
| 105 | + videoAd.onClose(this.handleClose); | ||
| 106 | + videoAd.onError(this.onError); | ||
| 107 | + // console.log("hasVideo",hasVideo) | ||
| 108 | + if (hasVideo) { | ||
| 109 | + try { | ||
| 110 | + await this.handleLoaded() | ||
| 111 | + } catch (error) { | ||
| 112 | + __LOG__ && console.log("error", error) | ||
| 113 | + await videoAd.load() | ||
| 114 | + this.handleLoaded() | ||
| 115 | + } | ||
| 116 | + } else { | ||
| 117 | + try { | ||
| 118 | + await videoAd.load() | ||
| 119 | + await this.handleLoaded() | ||
| 120 | + } catch (error) { | ||
| 121 | + this.handleError({}) | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + } | ||
| 125 | + this.preloadVideo(adUnitId); | ||
| 126 | + __LOG__ && console.warn('====> PCSDK WxVideo 请求视频adUnitId', adUnitId); | ||
| 127 | + }); | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + private onError(err: any) { | ||
| 131 | + // console.error("onError"); | ||
| 132 | + let that = SDKVideo.I; | ||
| 133 | + !that.isErrored && that.handleError(err); | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + private async handleLoaded() { | ||
| 137 | + let that = SDKVideo.I; | ||
| 138 | + try { | ||
| 139 | + __LOG__ && console.warn("handleLoaded2", that.videoAd, that.videoAd.isReady()); | ||
| 140 | + LogService.I.adStat(this.videoKey, this.adUnitId, DOT_AD_TYPE.video, DOT_AD_STATUS.show) | ||
| 141 | + | ||
| 142 | + if (that.videoAd.isReady()) { | ||
| 143 | + await that.videoAd.show(); | ||
| 144 | + } else { | ||
| 145 | + that.videoAd.load().then(() => { | ||
| 146 | + that.videoAd.show(); | ||
| 147 | + }).catch(that.handleError) | ||
| 148 | + } | ||
| 149 | + __LOG__ && console.log("handleLoaded_show") | ||
| 150 | + //打点 | ||
| 151 | + } catch (e) { | ||
| 152 | + __LOG__ && console.log("handleLoaded_show__err"); | ||
| 153 | + that.handleError({}) | ||
| 154 | + } | ||
| 155 | + // //数据保存 | ||
| 156 | + // TempService.I.add(TempDataKeys.VideoShow, 1); | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + private handleClose(res: any) { | ||
| 160 | + __LOG__ && console.warn('====> PCSDK WxVideo 广告关闭', res && res.isEnded || res === undefined); | ||
| 161 | + let that = SDKVideo.I; | ||
| 162 | + if (res && res.isEnded || res === undefined) { | ||
| 163 | + // 统计看视频成功 | ||
| 164 | + LogService.I.adStat(that.videoKey, that.adUnitId, DOT_AD_TYPE.video, DOT_AD_STATUS.complete) | ||
| 165 | + that.resolve && that.resolve({ type: 2 }); | ||
| 166 | + } else { | ||
| 167 | + LogService.I.adStat(that.videoKey, that.adUnitId, DOT_AD_TYPE.video, DOT_AD_STATUS.interrupt) | ||
| 168 | + that.reject && that.reject({ ...ShareVideoError.VideoQuit }); | ||
| 169 | + } | ||
| 170 | + that.videoAd.offClose(that.handleClose); | ||
| 171 | + that._isPlaying = false; | ||
| 172 | + that._isErrored = false; | ||
| 173 | + } | ||
| 174 | + | ||
| 175 | + private handleError(err: any) { | ||
| 176 | + __LOG__ && console.warn('====> PCSDK WxVideo 加载视频广告失败', err); | ||
| 177 | + let that = SDKVideo.I; | ||
| 178 | + LogService.I.adStat(that.videoKey, that.adUnitId, DOT_AD_TYPE.video, DOT_AD_STATUS.fail) | ||
| 179 | + that.reject && that.reject({ ...ShareVideoError.VideoFail }); | ||
| 180 | + that.videoAd && that.videoAd.offClose && that.videoAd.offClose(that.handleClose); | ||
| 181 | + that._isPlaying = false; | ||
| 182 | + that._isErrored = true; | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + private static instance: SDKVideo; | ||
| 186 | + static get I(): SDKVideo { | ||
| 187 | + return this.instance || (this.instance = new SDKVideo); | ||
| 188 | + } | ||
| 189 | +} | ||
| 0 | \ No newline at end of file | 190 | \ No newline at end of file |
| @@ -0,0 +1,91 @@ | @@ -0,0 +1,91 @@ | ||
| 1 | +import SDKBaseData from "../base/SDKBaseData"; | ||
| 2 | +import SDKEventCenter from "../base/SDKEventCenter"; | ||
| 3 | +import { SDKEventEnum } from "../base/SDKEventEnum"; | ||
| 4 | +import { __LOG__ } from "../base/SDKConst"; | ||
| 5 | + | ||
| 6 | +/* | ||
| 7 | +* 模拟分享; | ||
| 8 | +*/ | ||
| 9 | +export default class SimulateShare { | ||
| 10 | + private callbacks: any; | ||
| 11 | + private hideTime: number; | ||
| 12 | + private delayTime: number; | ||
| 13 | + | ||
| 14 | + constructor() { | ||
| 15 | + this.hideTime = 0; | ||
| 16 | + this.delayTime = 300; | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + private get SuccessTime(): number { | ||
| 20 | + return SDKBaseData.SimulateShareTime; | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + bind(callbacks: any) { | ||
| 24 | + __LOG__ && console.warn('SDK SimulateShare bind'); | ||
| 25 | + let { success, fail, cancel } = callbacks; | ||
| 26 | + let flag = true; | ||
| 27 | + callbacks.success = (e: any) => { | ||
| 28 | + flag && success(e); | ||
| 29 | + flag = false; | ||
| 30 | + }; | ||
| 31 | + callbacks.fail = (e: any) => { | ||
| 32 | + flag && fail(e); | ||
| 33 | + flag = false; | ||
| 34 | + }; | ||
| 35 | + callbacks.cancel = (e: any) => { | ||
| 36 | + flag && cancel(e); | ||
| 37 | + flag = false; | ||
| 38 | + }; | ||
| 39 | + this.hideTime = 0; | ||
| 40 | + this.callbacks = callbacks; | ||
| 41 | + SDKEventCenter.I.add(SDKEventEnum.APP_SHOW, this.onShow, this); | ||
| 42 | + SDKEventCenter.I.add(SDKEventEnum.APP_HIDE, this.onHide, this); | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + clear() { | ||
| 46 | + this.hideTime = 0; | ||
| 47 | + SDKEventCenter.I.remove(SDKEventEnum.APP_SHOW, this.onShow, this); | ||
| 48 | + SDKEventCenter.I.remove(SDKEventEnum.APP_HIDE, this.onHide, this); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + private onHide() { | ||
| 52 | + __LOG__ && console.warn('SDK SimulateShare hide'); | ||
| 53 | + this.hideTime = new Date().getTime(); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + private onShow() { | ||
| 57 | + let successTime = this.SuccessTime; | ||
| 58 | + __LOG__ && console.warn('SDK SimulateShare show', successTime); | ||
| 59 | + let { success, fail } = this.callbacks; | ||
| 60 | + let curTime = Date.now(); | ||
| 61 | + let time = curTime - this.hideTime; | ||
| 62 | + let timeout: any; | ||
| 63 | + | ||
| 64 | + if (time < successTime) { | ||
| 65 | + timeout = setTimeout(() => { | ||
| 66 | + __LOG__ && console.warn('SDK SimulateShare 模拟分享失败!'); | ||
| 67 | + fail && fail({ | ||
| 68 | + errMsg: '模拟分享失败!' | ||
| 69 | + }); | ||
| 70 | + clearTimeout(timeout); | ||
| 71 | + timeout = null; | ||
| 72 | + }, this.delayTime); | ||
| 73 | + } else { | ||
| 74 | + timeout = setTimeout(() => { | ||
| 75 | + __LOG__ && console.warn('SDK SimulateShare 模拟分享成功!'); | ||
| 76 | + success && success({ | ||
| 77 | + errMsg: '模拟分享成功!', | ||
| 78 | + shareTickets: ['simulate_ticket'] | ||
| 79 | + }); | ||
| 80 | + clearTimeout(timeout); | ||
| 81 | + timeout = null; | ||
| 82 | + }, this.delayTime); | ||
| 83 | + } | ||
| 84 | + this.clear(); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + private static instance: SimulateShare; | ||
| 88 | + static get I(): SimulateShare { | ||
| 89 | + return this.instance || (this.instance = new SimulateShare); | ||
| 90 | + } | ||
| 91 | +} | ||
| 0 | \ No newline at end of file | 92 | \ No newline at end of file |
| @@ -0,0 +1,296 @@ | @@ -0,0 +1,296 @@ | ||
| 1 | +// /** | ||
| 2 | +// * base64.ts | ||
| 3 | +// * | ||
| 4 | +// * Licensed under the BSD 3-Clause License. | ||
| 5 | +// * http://opensource.org/licenses/BSD-3-Clause | ||
| 6 | +// * | ||
| 7 | +// * References: | ||
| 8 | +// * http://en.wikipedia.org/wiki/Base64 | ||
| 9 | +// * | ||
| 10 | +// * @author Dan Kogai (https://github.com/dankogai) | ||
| 11 | +// */ | ||
| 12 | +// const version = '3.5.2'; | ||
| 13 | +// /** | ||
| 14 | +// * @deprecated use lowercase `version`. | ||
| 15 | +// */ | ||
| 16 | +// const VERSION = version; | ||
| 17 | +// const _hasatob = typeof atob === 'function'; | ||
| 18 | +// const _hasbtoa = typeof btoa === 'function'; | ||
| 19 | +// const _hasBuffer = typeof Buffer === 'function'; | ||
| 20 | +// const _TD = typeof TextDecoder === 'function' ? new TextDecoder() : undefined; | ||
| 21 | +// const _TE = typeof TextEncoder === 'function' ? new TextEncoder() : undefined; | ||
| 22 | +// const b64ch ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; | ||
| 23 | +// const b64chs = [...b64ch]; | ||
| 24 | +// const b64tab = ((a) => { | ||
| 25 | +// let tab = {}; | ||
| 26 | +// a.forEach((c, i) => tab[c] = i); | ||
| 27 | +// return tab; | ||
| 28 | +// })(b64chs); | ||
| 29 | +// const b64re = | ||
| 30 | +// /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/; | ||
| 31 | +// const _fromCC = String.fromCharCode.bind(String); | ||
| 32 | +// const _U8Afrom = typeof Uint8Array.from === 'function' | ||
| 33 | +// ? Uint8Array.from.bind(Uint8Array) | ||
| 34 | +// : (it, fn: (any) => number = (x) => x) => | ||
| 35 | +// new Uint8Array(Array.prototype.slice.call(it, 0).map(fn)); | ||
| 36 | +// const _mkUriSafe = (src: string) => src | ||
| 37 | +// .replace(/[+\/]/g, (m0) => m0 == '+' ? '-' : '_') | ||
| 38 | +// .replace(/=+$/m, ''); | ||
| 39 | +// const _tidyB64 = (s: string) => s.replace(/[^A-Za-z0-9\+\/]/g, ''); | ||
| 40 | +// /** | ||
| 41 | +// * polyfill version of `btoa` | ||
| 42 | +// */ | ||
| 43 | +// const btoaPolyfill = (bin: string) => { | ||
| 44 | +// // console.log('polyfilled'); | ||
| 45 | +// let u32, c0, c1, c2, asc = ''; | ||
| 46 | +// const pad = bin.length % 3; | ||
| 47 | +// for (let i = 0; i < bin.length;) { | ||
| 48 | +// if ((c0 = bin.charCodeAt(i++)) > 255 || | ||
| 49 | +// (c1 = bin.charCodeAt(i++)) > 255 || | ||
| 50 | +// (c2 = bin.charCodeAt(i++)) > 255) | ||
| 51 | +// throw new TypeError('invalid character found'); | ||
| 52 | +// u32 = (c0 << 16) | (c1 << 8) | c2; | ||
| 53 | +// asc += b64chs[u32 >> 18 & 63] | ||
| 54 | +// + b64chs[u32 >> 12 & 63] | ||
| 55 | +// + b64chs[u32 >> 6 & 63] | ||
| 56 | +// + b64chs[u32 & 63]; | ||
| 57 | +// } | ||
| 58 | +// return pad ? asc.slice(0, pad - 3) + "===".substring(pad) : asc; | ||
| 59 | +// }; | ||
| 60 | +// /** | ||
| 61 | +// * does what `window.btoa` of web browsers do. | ||
| 62 | +// * @param {String} bin binary string | ||
| 63 | +// * @returns {string} Base64-encoded string | ||
| 64 | +// */ | ||
| 65 | +// const _btoa = _hasbtoa ? (bin: string) => btoa(bin) | ||
| 66 | +// : _hasBuffer ? (bin: string) => Buffer.from(bin, 'binary').toString('base64') | ||
| 67 | +// : btoaPolyfill; | ||
| 68 | +// const _fromUint8Array = _hasBuffer | ||
| 69 | +// ? (u8a: Uint8Array) => Buffer.from(u8a).toString('base64') | ||
| 70 | +// : (u8a: Uint8Array) => { | ||
| 71 | +// // cf. https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string/12713326#12713326 | ||
| 72 | +// const maxargs = 0x1000; | ||
| 73 | +// let strs = []; | ||
| 74 | +// for (let i = 0, l = u8a.length; i < l; i += maxargs) { | ||
| 75 | +// strs.push(_fromCC.apply(null, u8a.subarray(i, i + maxargs))); | ||
| 76 | +// } | ||
| 77 | +// return _btoa(strs.join('')); | ||
| 78 | +// }; | ||
| 79 | +// /** | ||
| 80 | +// * converts a Uint8Array to a Base64 string. | ||
| 81 | +// * @param {boolean} [urlsafe] URL-and-filename-safe a la RFC4648 §5 | ||
| 82 | +// * @returns {string} Base64 string | ||
| 83 | +// */ | ||
| 84 | +// const fromUint8Array = (u8a: Uint8Array, urlsafe = false) => | ||
| 85 | +// urlsafe ? _mkUriSafe(_fromUint8Array(u8a)) : _fromUint8Array(u8a); | ||
| 86 | +// // This trick is found broken https://github.com/dankogai/js-base64/issues/130 | ||
| 87 | +// // const utob = (src: string) => unescape(encodeURIComponent(src)); | ||
| 88 | +// // reverting good old fationed regexp | ||
| 89 | +// const cb_utob = (c: string) => { | ||
| 90 | +// if (c.length < 2) { | ||
| 91 | +// var cc = c.charCodeAt(0); | ||
| 92 | +// return cc < 0x80 ? c | ||
| 93 | +// : cc < 0x800 ? (_fromCC(0xc0 | (cc >>> 6)) | ||
| 94 | +// + _fromCC(0x80 | (cc & 0x3f))) | ||
| 95 | +// : (_fromCC(0xe0 | ((cc >>> 12) & 0x0f)) | ||
| 96 | +// + _fromCC(0x80 | ((cc >>> 6) & 0x3f)) | ||
| 97 | +// + _fromCC(0x80 | (cc & 0x3f))); | ||
| 98 | +// } else { | ||
| 99 | +// var cc = 0x10000 | ||
| 100 | +// + (c.charCodeAt(0) - 0xD800) * 0x400 | ||
| 101 | +// + (c.charCodeAt(1) - 0xDC00); | ||
| 102 | +// return (_fromCC(0xf0 | ((cc >>> 18) & 0x07)) | ||
| 103 | +// + _fromCC(0x80 | ((cc >>> 12) & 0x3f)) | ||
| 104 | +// + _fromCC(0x80 | ((cc >>> 6) & 0x3f)) | ||
| 105 | +// + _fromCC(0x80 | (cc & 0x3f))); | ||
| 106 | +// } | ||
| 107 | +// }; | ||
| 108 | +// const re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g; | ||
| 109 | +// /** | ||
| 110 | +// * @deprecated should have been internal use only. | ||
| 111 | +// * @param {string} src UTF-8 string | ||
| 112 | +// * @returns {string} UTF-16 string | ||
| 113 | +// */ | ||
| 114 | +// const utob = (u: string) => u.replace(re_utob, cb_utob); | ||
| 115 | +// // | ||
| 116 | +// const _encode = _hasBuffer | ||
| 117 | +// ? (s: string) => Buffer.from(s, 'utf8').toString('base64') | ||
| 118 | +// : _TE | ||
| 119 | +// ? (s: string) => _fromUint8Array(_TE.encode(s)) | ||
| 120 | +// : (s: string) => _btoa(utob(s)); | ||
| 121 | +// /** | ||
| 122 | +// * converts a UTF-8-encoded string to a Base64 string. | ||
| 123 | +// * @param {boolean} [urlsafe] if `true` make the result URL-safe | ||
| 124 | +// * @returns {string} Base64 string | ||
| 125 | +// */ | ||
| 126 | +// const encode = (src: string, urlsafe = false) => urlsafe | ||
| 127 | +// ? _mkUriSafe(_encode(src)) | ||
| 128 | +// : _encode(src); | ||
| 129 | +// /** | ||
| 130 | +// * converts a UTF-8-encoded string to URL-safe Base64 RFC4648 §5. | ||
| 131 | +// * @returns {string} Base64 string | ||
| 132 | +// */ | ||
| 133 | +// const encodeURI = (src: string) => encode(src, true); | ||
| 134 | +// // This trick is found broken https://github.com/dankogai/js-base64/issues/130 | ||
| 135 | +// // const btou = (src: string) => decodeURIComponent(escape(src)); | ||
| 136 | +// // reverting good old fationed regexp | ||
| 137 | +// const re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g; | ||
| 138 | +// const cb_btou = (cccc: string) => { | ||
| 139 | +// switch (cccc.length) { | ||
| 140 | +// case 4: | ||
| 141 | +// var cp = ((0x07 & cccc.charCodeAt(0)) << 18) | ||
| 142 | +// | ((0x3f & cccc.charCodeAt(1)) << 12) | ||
| 143 | +// | ((0x3f & cccc.charCodeAt(2)) << 6) | ||
| 144 | +// | (0x3f & cccc.charCodeAt(3)), | ||
| 145 | +// offset = cp - 0x10000; | ||
| 146 | +// return (_fromCC((offset >>> 10) + 0xD800) | ||
| 147 | +// + _fromCC((offset & 0x3FF) + 0xDC00)); | ||
| 148 | +// case 3: | ||
| 149 | +// return _fromCC( | ||
| 150 | +// ((0x0f & cccc.charCodeAt(0)) << 12) | ||
| 151 | +// | ((0x3f & cccc.charCodeAt(1)) << 6) | ||
| 152 | +// | (0x3f & cccc.charCodeAt(2)) | ||
| 153 | +// ); | ||
| 154 | +// default: | ||
| 155 | +// return _fromCC( | ||
| 156 | +// ((0x1f & cccc.charCodeAt(0)) << 6) | ||
| 157 | +// | (0x3f & cccc.charCodeAt(1)) | ||
| 158 | +// ); | ||
| 159 | +// } | ||
| 160 | +// }; | ||
| 161 | +// /** | ||
| 162 | +// * @deprecated should have been internal use only. | ||
| 163 | +// * @param {string} src UTF-16 string | ||
| 164 | +// * @returns {string} UTF-8 string | ||
| 165 | +// */ | ||
| 166 | +// const btou = (b: string) => b.replace(re_btou, cb_btou); | ||
| 167 | +// /** | ||
| 168 | +// * polyfill version of `atob` | ||
| 169 | +// */ | ||
| 170 | +// const atobPolyfill = (asc: string) => { | ||
| 171 | +// // console.log('polyfilled'); | ||
| 172 | +// asc = asc.replace(/\s+/g, ''); | ||
| 173 | +// if (!b64re.test(asc)) throw new TypeError('malformed base64.'); | ||
| 174 | +// asc += '=='.slice(2 - (asc.length & 3)); | ||
| 175 | +// let u24, bin = '', r1, r2; | ||
| 176 | +// for (let i = 0; i < asc.length;) { | ||
| 177 | +// u24 = b64tab[asc.charAt(i++)] << 18 | ||
| 178 | +// | b64tab[asc.charAt(i++)] << 12 | ||
| 179 | +// | (r1 = b64tab[asc.charAt(i++)]) << 6 | ||
| 180 | +// | (r2 = b64tab[asc.charAt(i++)]); | ||
| 181 | +// bin += r1 === 64 ? _fromCC(u24 >> 16 & 255) | ||
| 182 | +// : r2 === 64 ? _fromCC(u24 >> 16 & 255, u24 >> 8 & 255) | ||
| 183 | +// : _fromCC(u24 >> 16 & 255, u24 >> 8 & 255, u24 & 255); | ||
| 184 | +// } | ||
| 185 | +// return bin; | ||
| 186 | +// }; | ||
| 187 | +// /** | ||
| 188 | +// * does what `window.atob` of web browsers do. | ||
| 189 | +// * @param {String} asc Base64-encoded string | ||
| 190 | +// * @returns {string} binary string | ||
| 191 | +// */ | ||
| 192 | +// const _atob = _hasatob ? (asc: string) => atob(_tidyB64(asc)) | ||
| 193 | +// : _hasBuffer ? (asc: string) => Buffer.from(asc, 'base64').toString('binary') | ||
| 194 | +// : atobPolyfill; | ||
| 195 | +// // | ||
| 196 | +// const _toUint8Array = _hasBuffer | ||
| 197 | +// ? (a: string) => _U8Afrom(Buffer.from(a, 'base64')) | ||
| 198 | +// : (a: string) => _U8Afrom(_atob(a), c => c.charCodeAt(0)); | ||
| 199 | +// /** | ||
| 200 | +// * converts a Base64 string to a Uint8Array. | ||
| 201 | +// */ | ||
| 202 | +// const toUint8Array = (a: string): Uint8Array => _toUint8Array(_unURI(a)); | ||
| 203 | +// // | ||
| 204 | +// const _decode = _hasBuffer | ||
| 205 | +// ? (a: string) => Buffer.from(a, 'base64').toString('utf8') | ||
| 206 | +// : _TD | ||
| 207 | +// ? (a: string) => _TD.decode(_toUint8Array(a)) | ||
| 208 | +// : (a: string) => btou(_atob(a)); | ||
| 209 | +// const _unURI = (a: string) => | ||
| 210 | +// _tidyB64(a.replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/')); | ||
| 211 | +// /** | ||
| 212 | +// * converts a Base64 string to a UTF-8 string. | ||
| 213 | +// * @param {String} src Base64 string. Both normal and URL-safe are supported | ||
| 214 | +// * @returns {string} UTF-8 string | ||
| 215 | +// */ | ||
| 216 | +// const decode = (src: string) => _decode(_unURI(src)); | ||
| 217 | +// // | ||
| 218 | +// const _noEnum = (v) => { | ||
| 219 | +// return { | ||
| 220 | +// value: v, enumerable: false, writable: true, configurable: true | ||
| 221 | +// }; | ||
| 222 | +// }; | ||
| 223 | +// /** | ||
| 224 | +// * extend String.prototype with relevant methods | ||
| 225 | +// */ | ||
| 226 | +// const extendString = function () { | ||
| 227 | +// const _add = (name, body) => Object.defineProperty( | ||
| 228 | +// String.prototype, name, _noEnum(body) | ||
| 229 | +// ); | ||
| 230 | +// _add('fromBase64', function () { return decode(this) }); | ||
| 231 | +// _add('toBase64', function (urlsafe) { return encode(this, urlsafe) }); | ||
| 232 | +// _add('toBase64URI', function () { return encode(this, true) }); | ||
| 233 | +// _add('toBase64URL', function () { return encode(this, true) }); | ||
| 234 | +// _add('toUint8Array', function () { return toUint8Array(this) }); | ||
| 235 | +// }; | ||
| 236 | +// /** | ||
| 237 | +// * extend Uint8Array.prototype with relevant methods | ||
| 238 | +// */ | ||
| 239 | +// const extendUint8Array = function () { | ||
| 240 | +// const _add = (name, body) => Object.defineProperty( | ||
| 241 | +// Uint8Array.prototype, name, _noEnum(body) | ||
| 242 | +// ); | ||
| 243 | +// _add('toBase64', function (urlsafe) { return fromUint8Array(this, urlsafe) }); | ||
| 244 | +// _add('toBase64URI', function () { return fromUint8Array(this, true) }); | ||
| 245 | +// _add('toBase64URL', function () { return fromUint8Array(this, true) }); | ||
| 246 | +// }; | ||
| 247 | +// /** | ||
| 248 | +// * extend Builtin prototypes with relevant methods | ||
| 249 | +// */ | ||
| 250 | +// const extendBuiltins = () => { | ||
| 251 | +// extendString(); | ||
| 252 | +// extendUint8Array(); | ||
| 253 | +// } | ||
| 254 | +// const gBase64 = { | ||
| 255 | +// version: version, | ||
| 256 | +// VERSION: VERSION, | ||
| 257 | +// atob: _atob, | ||
| 258 | +// atobPolyfill: atobPolyfill, | ||
| 259 | +// btoa: _btoa, | ||
| 260 | +// btoaPolyfill: btoaPolyfill, | ||
| 261 | +// fromBase64: decode, | ||
| 262 | +// toBase64: encode, | ||
| 263 | +// encode: encode, | ||
| 264 | +// encodeURI: encodeURI, | ||
| 265 | +// encodeURL: encodeURI, | ||
| 266 | +// utob: utob, | ||
| 267 | +// btou: btou, | ||
| 268 | +// decode: decode, | ||
| 269 | +// fromUint8Array: fromUint8Array, | ||
| 270 | +// toUint8Array: toUint8Array, | ||
| 271 | +// extendString: extendString, | ||
| 272 | +// extendUint8Array: extendUint8Array, | ||
| 273 | +// extendBuiltins: extendBuiltins, | ||
| 274 | +// } | ||
| 275 | +// // makecjs:CUT // | ||
| 276 | +// export { version }; | ||
| 277 | +// export { VERSION }; | ||
| 278 | +// export { _atob as atob }; | ||
| 279 | +// export { atobPolyfill }; | ||
| 280 | +// export { _btoa as btoa }; | ||
| 281 | +// export { btoaPolyfill } | ||
| 282 | +// export { decode as fromBase64 }; | ||
| 283 | +// export { encode as toBase64 }; | ||
| 284 | +// export { utob }; | ||
| 285 | +// export { encode }; | ||
| 286 | +// export { encodeURI }; | ||
| 287 | +// export { encodeURI as encodeURL }; | ||
| 288 | +// export { btou }; | ||
| 289 | +// export { decode }; | ||
| 290 | +// export { fromUint8Array }; | ||
| 291 | +// export { toUint8Array }; | ||
| 292 | +// export { extendString }; | ||
| 293 | +// export { extendUint8Array }; | ||
| 294 | +// export { extendBuiltins }; | ||
| 295 | +// // and finally, | ||
| 296 | +// export { gBase64 as Base64 }; |
| @@ -0,0 +1,24 @@ | @@ -0,0 +1,24 @@ | ||
| 1 | +/** | ||
| 2 | + * 日期工具类 | ||
| 3 | + */ | ||
| 4 | +export default class DateUtils { | ||
| 5 | + static get now(): number { | ||
| 6 | + return Math.floor(this.nowTime / 1000); | ||
| 7 | + } | ||
| 8 | + | ||
| 9 | + static get nowTime(): number { | ||
| 10 | + return new Date().getTime(); | ||
| 11 | + } | ||
| 12 | + | ||
| 13 | + static get today(): string { | ||
| 14 | + let time = new Date(this.nowTime); | ||
| 15 | + let year = time.getFullYear(); | ||
| 16 | + let month = time.getMonth() + 1; | ||
| 17 | + let date = time.getDate(); | ||
| 18 | + return `${year}-${this.add(month)}-${this.add(date)}`; | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + static add(num: number): string { | ||
| 22 | + return num < 10 ? '0' + num : '' + num; | ||
| 23 | + } | ||
| 24 | +} |
| @@ -0,0 +1,33 @@ | @@ -0,0 +1,33 @@ | ||
| 1 | +/** | ||
| 2 | + * 随机数工具类 | ||
| 3 | + */ | ||
| 4 | +export default class RandomUtils { | ||
| 5 | + /** | ||
| 6 | + * 在一个数组中随机获取一个元素 | ||
| 7 | + * @param arr 数组 | ||
| 8 | + * @returns {any} 随机出来的结果 | ||
| 9 | + */ | ||
| 10 | + static randomArray(arr: Array<any>): any { | ||
| 11 | + let index: number = Math.floor(Math.random() * arr.length); | ||
| 12 | + return arr[index]; | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + /** | ||
| 16 | + * 随机范围值[0, max), 不包含max | ||
| 17 | + * @param min | ||
| 18 | + * @param max | ||
| 19 | + */ | ||
| 20 | + static rand(min: number, max: number) { | ||
| 21 | + min = min || 0; | ||
| 22 | + max = max || 10000; | ||
| 23 | + return Math.floor(Math.random() * 10000) % (max - min) + min; | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + static rang(min: number, max: number) { | ||
| 27 | + return Math.round(Math.random() * (max - min) + min); | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + static randFloat(min: number, max: number) { | ||
| 31 | + return parseFloat((Math.random() * (max - min) + min).toFixed(2)); | ||
| 32 | + } | ||
| 33 | +} |
| @@ -0,0 +1,167 @@ | @@ -0,0 +1,167 @@ | ||
| 1 | +export default class SDKUtils { | ||
| 2 | + private static toString = Object.prototype.toString; | ||
| 3 | + | ||
| 4 | + static isString(o: any): boolean { | ||
| 5 | + return this.toString.call(o) === '[object String]'; | ||
| 6 | + } | ||
| 7 | + | ||
| 8 | + static isFunction(o: any): boolean { | ||
| 9 | + return typeof o === 'function'; | ||
| 10 | + } | ||
| 11 | + | ||
| 12 | + static isPlainObject(o: any): boolean { | ||
| 13 | + return o !== null && this.toString.call(o) === '[object Object]' && 'isPrototypeOf' in o; | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + static isObject(o: any): boolean { | ||
| 17 | + return o !== null && this.toString.call(o) === '[object Object]'; | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + static isArray(o: any): boolean { | ||
| 21 | + return this.toString.call(o) === '[object Array]'; | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + static isNumber(o: any): boolean { | ||
| 25 | + return typeof o === 'number' && isFinite(o); | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + static isUndefined(o: any): boolean { | ||
| 29 | + return typeof o === 'undefined' || o === 'undefined'; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + static hasProperty(o: any, key: string): boolean { | ||
| 33 | + if (!o) | ||
| 34 | + return false; | ||
| 35 | + return o.hasOwnProperty(key); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + // 去除左右空空格 | ||
| 39 | + static trim(str: any) { | ||
| 40 | + return str.replace(/^\s+/g, '').replace(/\s+$/g, ''); | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + static isEmpty(obj: any) { | ||
| 44 | + return !obj || typeof (obj) === 'undefined' || (this.isString(obj) && this.trim(obj) === '') || obj === 'null'; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + static covertArray<T>(data: any): Array<T> { | ||
| 48 | + let datas: Array<T> = []; | ||
| 49 | + if (this.isArray(data)) | ||
| 50 | + datas = [...data]; | ||
| 51 | + else | ||
| 52 | + datas = [data]; | ||
| 53 | + return datas; | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + // 对象序列化参数 | ||
| 57 | + static serializeParams(params: any) { | ||
| 58 | + if (!params) { | ||
| 59 | + return ''; | ||
| 60 | + } | ||
| 61 | + const enc = encodeURIComponent; | ||
| 62 | + return Object.keys(params) | ||
| 63 | + .map((key: string) => (`${enc(key)}=${enc(params[key])}`)).join('&'); | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + // 兼容方式 - 版本比较 | ||
| 67 | + static compareVersion(v1: any, v2: any) { | ||
| 68 | + v1 = v1.split('.'); | ||
| 69 | + v2 = v2.split('.'); | ||
| 70 | + let len = Math.max(v1.length, v2.length); | ||
| 71 | + | ||
| 72 | + while (v1.length < len) { | ||
| 73 | + v1.push('0'); | ||
| 74 | + } | ||
| 75 | + while (v2.length < len) { | ||
| 76 | + v2.push('0'); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + for (let i = 0; i < len; i++) { | ||
| 80 | + let num1 = parseInt(v1[i]); | ||
| 81 | + let num2 = parseInt(v2[i]); | ||
| 82 | + | ||
| 83 | + if (num1 > num2) { | ||
| 84 | + return 1; | ||
| 85 | + } else if (num1 < num2) { | ||
| 86 | + return -1; | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + return 0; | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + /** | ||
| 94 | + * 递归地将给定对象复制到新对象中 | ||
| 95 | + */ | ||
| 96 | + static clone(o: any): any { | ||
| 97 | + if (o === null || typeof o !== 'object') | ||
| 98 | + return o; | ||
| 99 | + | ||
| 100 | + if (this.isArray(o)) { | ||
| 101 | + let arr = o.slice(); | ||
| 102 | + for (let i = 0, len = arr.length; i < len; i++) { | ||
| 103 | + arr[i] = this.clone(arr[i]); | ||
| 104 | + } | ||
| 105 | + return arr; | ||
| 106 | + } else { | ||
| 107 | + let obj = {}; | ||
| 108 | + for (let k in o) { | ||
| 109 | + obj[k] = this.clone(o[k]); | ||
| 110 | + } | ||
| 111 | + return obj; | ||
| 112 | + } | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + /** | ||
| 116 | + * 递归地合并两个对象,将obj1的属性设置为obj2的属性,并在必要时创建属性。 | ||
| 117 | + * @param {Object} obj1 | ||
| 118 | + * @param {Object} obj2 | ||
| 119 | + * @return {Object} | ||
| 120 | + */ | ||
| 121 | + static merge(obj1: any, obj2: any, appendOnly: boolean): any { | ||
| 122 | + if (obj1 === null || typeof obj1 !== 'object') | ||
| 123 | + throw new TypeError('SDK merge() - 第一个参数必须是object, 不能为 ' + typeof obj1 + '。'); | ||
| 124 | + | ||
| 125 | + if (obj2 === null || typeof obj2 !== 'object') | ||
| 126 | + throw new TypeError('SDK merge() - 第二个参数必须是object, 不能为 ' + typeof obj2 + '。'); | ||
| 127 | + | ||
| 128 | + if (this.isArray(obj1) || this.isArray(obj2)) | ||
| 129 | + throw new TypeError('SDK merge() - 不支持数组合并。'); | ||
| 130 | + | ||
| 131 | + for (let k in obj2) { | ||
| 132 | + let obj1Val; | ||
| 133 | + let obj2Val = obj2[k]; | ||
| 134 | + if (Object.prototype.hasOwnProperty.call(obj1, k)) { | ||
| 135 | + if (!appendOnly) { | ||
| 136 | + obj1Val = obj1[k]; | ||
| 137 | + if (obj1Val !== null && typeof obj1Val === 'object' && | ||
| 138 | + obj2Val !== null && typeof obj2Val === 'object') | ||
| 139 | + this.merge(obj1Val, obj2Val, false); | ||
| 140 | + | ||
| 141 | + else | ||
| 142 | + obj1[k] = this.clone(obj2Val); | ||
| 143 | + } | ||
| 144 | + } else | ||
| 145 | + obj1[k] = this.clone(obj2Val); | ||
| 146 | + } | ||
| 147 | + return obj1; | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + /** | ||
| 151 | + * 简单函数promise转换 | ||
| 152 | + * @param fn 函数 例如: promisifyAsyncWrap( wx.getUserInfo )( { withCredentials : true } ).then( ret => {} ); | ||
| 153 | + */ | ||
| 154 | + static promisifyAsyncWrap(fn: any) { | ||
| 155 | + return (options: any) => new Promise((resolve, reject) => { | ||
| 156 | + let conf = { | ||
| 157 | + success: (res: any) => { | ||
| 158 | + resolve(res); | ||
| 159 | + }, | ||
| 160 | + fail: (err: any) => { | ||
| 161 | + reject(err); | ||
| 162 | + } | ||
| 163 | + }; | ||
| 164 | + fn(Object.assign({}, conf, options)); | ||
| 165 | + }); | ||
| 166 | + } | ||
| 167 | +} | ||
| 0 | \ No newline at end of file | 168 | \ No newline at end of file |
| @@ -0,0 +1,182 @@ | @@ -0,0 +1,182 @@ | ||
| 1 | +/* | ||
| 2 | +* 签名工具Sha1Utils | ||
| 3 | +*/ | ||
| 4 | +export default class Sha1Utils { | ||
| 5 | + private static instance: Sha1Utils; | ||
| 6 | + | ||
| 7 | + static get I(): Sha1Utils { | ||
| 8 | + return this.instance || (this.instance = new Sha1Utils); | ||
| 9 | + } | ||
| 10 | + | ||
| 11 | + private blockstart; | ||
| 12 | + private W; | ||
| 13 | + private H0; | ||
| 14 | + private H1; | ||
| 15 | + private H2; | ||
| 16 | + private H3; | ||
| 17 | + private H4; | ||
| 18 | + private A; | ||
| 19 | + private B; | ||
| 20 | + private C; | ||
| 21 | + private D; | ||
| 22 | + private E; | ||
| 23 | + private temp; | ||
| 24 | + public msg; | ||
| 25 | + private msg_len; | ||
| 26 | + private word_array; | ||
| 27 | + | ||
| 28 | + private init(msg) { | ||
| 29 | + this.blockstart = null; | ||
| 30 | + this.W = new Array(80); | ||
| 31 | + this.H0 = 0x67452301; | ||
| 32 | + this.H1 = 0xEFCDAB89; | ||
| 33 | + this.H2 = 0x98BADCFE; | ||
| 34 | + this.H3 = 0x10325476; | ||
| 35 | + this.H4 = 0xC3D2E1F0; | ||
| 36 | + this.A = this.B = this.C = this.D = this.E = this.temp = this.msg = this.msg_len = null; | ||
| 37 | + | ||
| 38 | + this.word_array = []; | ||
| 39 | + this.msg = this.Utf8Encode(msg); | ||
| 40 | + this.msg_len = this.msg.length; | ||
| 41 | + for (let i = 0; i < this.msg_len - 3; i += 4) { | ||
| 42 | + let j = this.msg.charCodeAt(i) << 24 | this.msg.charCodeAt(i + 1) << 16 | | ||
| 43 | + this.msg.charCodeAt(i + 2) << 8 | this.msg.charCodeAt(i + 3); | ||
| 44 | + this.word_array.push(j); | ||
| 45 | + } | ||
| 46 | + this.initMsg(this.msg_len); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + private initMsg(msg_len) { | ||
| 50 | + let i; | ||
| 51 | + switch (msg_len % 4) { | ||
| 52 | + case 0: | ||
| 53 | + i = 0x080000000; | ||
| 54 | + break; | ||
| 55 | + case 1: | ||
| 56 | + i = this.msg.charCodeAt(msg_len - 1) << 24 | 0x0800000; | ||
| 57 | + break; | ||
| 58 | + case 2: | ||
| 59 | + i = this.msg.charCodeAt(msg_len - 2) << 24 | this.msg.charCodeAt(msg_len - 1) << 16 | 0x08000; | ||
| 60 | + break; | ||
| 61 | + case 3: | ||
| 62 | + i = this.msg.charCodeAt(msg_len - 3) << 24 | this.msg.charCodeAt(msg_len - 2) << 16 | this.msg.charCodeAt(msg_len - 1) << 8 | 0x80; | ||
| 63 | + break; | ||
| 64 | + } | ||
| 65 | + this.word_array.push(i); | ||
| 66 | + while ((this.word_array.length % 16) !== 14) this.word_array.push(0); | ||
| 67 | + this.word_array.push(msg_len >>> 29); | ||
| 68 | + this.word_array.push((msg_len << 3) & 0x0ffffffff); | ||
| 69 | + this.getTemp(); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + private getTemp() { | ||
| 73 | + for (this.blockstart = 0; this.blockstart < this.word_array.length; this.blockstart += 16) { | ||
| 74 | + for (let i = 0; i < 16; i++) this.W[i] = this.word_array[this.blockstart + i]; | ||
| 75 | + for (let i = 16; i <= 79; i++) this.W[i] = this.rotate_left(this.W[i - 3] ^ this.W[i - 8] ^ this.W[i - 14] ^ this.W[i - 16], 1); | ||
| 76 | + this.A = this.H0; | ||
| 77 | + this.B = this.H1; | ||
| 78 | + this.C = this.H2; | ||
| 79 | + this.D = this.H3; | ||
| 80 | + this.E = this.H4; | ||
| 81 | + for (let i = 0; i <= 19; i++) { | ||
| 82 | + this.temp = (this.rotate_left(this.A, 5) + ((this.B & this.C) | (~this.B & this.D)) + this.E + this.W[i] + 0x5A827999) & 0x0ffffffff; | ||
| 83 | + this.E = this.D; | ||
| 84 | + this.D = this.C; | ||
| 85 | + this.C = this.rotate_left(this.B, 30); | ||
| 86 | + this.B = this.A; | ||
| 87 | + this.A = this.temp; | ||
| 88 | + } | ||
| 89 | + for (let i = 20; i <= 39; i++) { | ||
| 90 | + this.temp = (this.rotate_left(this.A, 5) + (this.B ^ this.C ^ this.D) + this.E + this.W[i] + 0x6ED9EBA1) & 0x0ffffffff; | ||
| 91 | + this.E = this.D; | ||
| 92 | + this.D = this.C; | ||
| 93 | + this.C = this.rotate_left(this.B, 30); | ||
| 94 | + this.B = this.A; | ||
| 95 | + this.A = this.temp; | ||
| 96 | + } | ||
| 97 | + for (let i = 40; i <= 59; i++) { | ||
| 98 | + this.temp = (this.rotate_left(this.A, 5) + ((this.B & this.C) | (this.B & this.D) | (this.C & this.D)) + this.E + this.W[i] + 0x8F1BBCDC) & 0x0ffffffff; | ||
| 99 | + this.E = this.D; | ||
| 100 | + this.D = this.C; | ||
| 101 | + this.C = this.rotate_left(this.B, 30); | ||
| 102 | + this.B = this.A; | ||
| 103 | + this.A = this.temp; | ||
| 104 | + } | ||
| 105 | + for (let i = 60; i <= 79; i++) { | ||
| 106 | + this.temp = (this.rotate_left(this.A, 5) + (this.B ^ this.C ^ this.D) + this.E + this.W[i] + 0xCA62C1D6) & 0x0ffffffff; | ||
| 107 | + this.E = this.D; | ||
| 108 | + this.D = this.C; | ||
| 109 | + this.C = this.rotate_left(this.B, 30); | ||
| 110 | + this.B = this.A; | ||
| 111 | + this.A = this.temp; | ||
| 112 | + } | ||
| 113 | + this.H0 = (this.H0 + this.A) & 0x0ffffffff; | ||
| 114 | + this.H1 = (this.H1 + this.B) & 0x0ffffffff; | ||
| 115 | + this.H2 = (this.H2 + this.C) & 0x0ffffffff; | ||
| 116 | + this.H3 = (this.H3 + this.D) & 0x0ffffffff; | ||
| 117 | + this.H4 = (this.H4 + this.E) & 0x0ffffffff; | ||
| 118 | + } | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + /** | ||
| 122 | + * 获取sha1加密字符串 | ||
| 123 | + */ | ||
| 124 | + hex_sha1(msg) { | ||
| 125 | + this.init(msg); | ||
| 126 | + let temp = this.cvt_hex(this.H0) + this.cvt_hex(this.H1) + this.cvt_hex(this.H2) + this.cvt_hex(this.H3) + this.cvt_hex(this.H4); | ||
| 127 | + return temp.toLowerCase(); | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + private rotate_left(n, s) { | ||
| 131 | + let t4 = (n << s) | (n >>> (32 - s)); | ||
| 132 | + return t4; | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + private lsb_hex(val) { | ||
| 136 | + let str = ''; | ||
| 137 | + let i; | ||
| 138 | + let vh; | ||
| 139 | + let vl; | ||
| 140 | + | ||
| 141 | + for (i = 0; i <= 6; i += 2) { | ||
| 142 | + vh = (val >>> (i * 4 + 4)) & 0x0f; | ||
| 143 | + vl = (val >>> (i * 4)) & 0x0f; | ||
| 144 | + str += vh.toString(16) + vl.toString(16); | ||
| 145 | + } | ||
| 146 | + return str; | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + private cvt_hex(val) { | ||
| 150 | + let str = ''; | ||
| 151 | + let i; | ||
| 152 | + let v; | ||
| 153 | + | ||
| 154 | + for (i = 7; i >= 0; i--) { | ||
| 155 | + v = (val >>> (i * 4)) & 0x0f; | ||
| 156 | + str += v.toString(16); | ||
| 157 | + } | ||
| 158 | + return str; | ||
| 159 | + } | ||
| 160 | + | ||
| 161 | + | ||
| 162 | + private Utf8Encode(string = '') { | ||
| 163 | + string = string.replace(/\r\n/g, '\n'); | ||
| 164 | + let utfText = ''; | ||
| 165 | + for (let n = 0; n < string.length; n++) { | ||
| 166 | + let c = string.charCodeAt(n); | ||
| 167 | + if (c < 128) { | ||
| 168 | + utfText += String.fromCharCode(c); | ||
| 169 | + } | ||
| 170 | + else if ((c > 127) && (c < 2048)) { | ||
| 171 | + utfText += String.fromCharCode((c >> 6) | 192); | ||
| 172 | + utfText += String.fromCharCode((c & 63) | 128); | ||
| 173 | + } | ||
| 174 | + else { | ||
| 175 | + utfText += String.fromCharCode((c >> 12) | 224); | ||
| 176 | + utfText += String.fromCharCode(((c >> 6) & 63) | 128); | ||
| 177 | + utfText += String.fromCharCode((c & 63) | 128); | ||
| 178 | + } | ||
| 179 | + } | ||
| 180 | + return utfText; | ||
| 181 | + } | ||
| 182 | +} |
| @@ -0,0 +1,85 @@ | @@ -0,0 +1,85 @@ | ||
| 1 | +import { Md5 } from "../lib/md5"; | ||
| 2 | +import { GAMEDATA } from "../base/SDKConst"; | ||
| 3 | +import SDKUtils from "./SDKUtils"; | ||
| 4 | + | ||
| 5 | +export default class SignUtils { | ||
| 6 | + private static instance: any; | ||
| 7 | + | ||
| 8 | + static get I(): SignUtils { | ||
| 9 | + return this.instance || (this.instance = new SignUtils()); | ||
| 10 | + } | ||
| 11 | + | ||
| 12 | + createSign(params: any) { | ||
| 13 | + let signStr = this.createQuery(params) + '' + GAMEDATA.appkey; | ||
| 14 | + //console.error("signStr",signStr) | ||
| 15 | + return Md5.hashStr(signStr); | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + private ksort(inputArr: any) { | ||
| 19 | + let tmp_arr = {}, | ||
| 20 | + keys: any[] = [], | ||
| 21 | + sorter, i, k: any, | ||
| 22 | + strictForIn = false, | ||
| 23 | + populateArr = {}; | ||
| 24 | + | ||
| 25 | + sorter = function (a, b) { | ||
| 26 | + let aFloat = parseFloat(a), | ||
| 27 | + bFloat = parseFloat(b), | ||
| 28 | + aNumeric = aFloat + '' === a, | ||
| 29 | + bNumeric = bFloat + '' === b; | ||
| 30 | + if (aNumeric && bNumeric) { | ||
| 31 | + return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0; | ||
| 32 | + } else if (aNumeric && !bNumeric) { | ||
| 33 | + return 1; | ||
| 34 | + } else if (!aNumeric && bNumeric) { | ||
| 35 | + return -1; | ||
| 36 | + } | ||
| 37 | + return a > b ? 1 : a < b ? -1 : 0; | ||
| 38 | + }; | ||
| 39 | + | ||
| 40 | + // Make a list of key names | ||
| 41 | + for (k in inputArr) { | ||
| 42 | + if (inputArr.hasOwnProperty(k)) { | ||
| 43 | + keys.push(k); | ||
| 44 | + } | ||
| 45 | + } | ||
| 46 | + keys.sort(sorter); | ||
| 47 | + | ||
| 48 | + // Rebuild array with sorted key names | ||
| 49 | + for (i = 0; i < keys.length; i++) { | ||
| 50 | + k = keys[i]; | ||
| 51 | + tmp_arr[k] = inputArr[k]; | ||
| 52 | + if (strictForIn) { | ||
| 53 | + delete inputArr[k]; | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | + for (i in tmp_arr) { | ||
| 57 | + if (tmp_arr.hasOwnProperty(i)) { | ||
| 58 | + populateArr[i] = tmp_arr[i]; | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + return strictForIn || populateArr; | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + private createQuery(params) { | ||
| 66 | + params = params || {}; | ||
| 67 | + // 1、ksort排序参数对象 | ||
| 68 | + let keys = Object.keys(this.ksort(params)); | ||
| 69 | + // 2、拼贴字符串 | ||
| 70 | + let key = ''; | ||
| 71 | + let query = ''; | ||
| 72 | + for (let i = 0, len = keys.length; i < len; i++) { | ||
| 73 | + // 为空,为 0的参数不参与签名,参数名为ver的参数不参与签名, 字符集为 utf-8 | ||
| 74 | + if (params[keys[i]] === '' || params[keys[i]] === '0' || params[keys[i]] === 0 || keys[i] === 'ver') continue | ||
| 75 | + key = keys[i]; | ||
| 76 | + i && (query += ''); | ||
| 77 | + if(SDKUtils.isArray(params[key])){ | ||
| 78 | + query += `${key}=${JSON.stringify(params[key])}`; | ||
| 79 | + }else{ | ||
| 80 | + query += `${key}=${params[key]}`; | ||
| 81 | + } | ||
| 82 | + } | ||
| 83 | + return query; | ||
| 84 | + } | ||
| 85 | +} | ||
| 0 | \ No newline at end of file | 86 | \ No newline at end of file |
| @@ -0,0 +1,93 @@ | @@ -0,0 +1,93 @@ | ||
| 1 | +/** | ||
| 2 | + * 微信存储功能工具 | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +import WxStorage from "../platform/wx/WxStorage"; | ||
| 6 | +import Storage from "../platform/Storage"; | ||
| 7 | + | ||
| 8 | +export default class StorageUtils { | ||
| 9 | + private store: any = null | ||
| 10 | + | ||
| 11 | + constructor() { | ||
| 12 | + if (typeof localStorage === 'object') | ||
| 13 | + this.store = new Storage; | ||
| 14 | + | ||
| 15 | + if (typeof (wx) != "undefined") | ||
| 16 | + this.store = new WxStorage; | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + private __isExpired(entity: any) { | ||
| 20 | + if (!entity) return true; | ||
| 21 | + let isExpired = this.timestamp - (entity.timestamp + entity.expiration) >= 0; | ||
| 22 | + return isExpired; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + get timestamp() { | ||
| 26 | + return +(new Date().getTime() / 1000).toFixed(0); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + set(key: string, value: Object, expiration = 0) { | ||
| 30 | + const entity = { | ||
| 31 | + timestamp: this.timestamp, | ||
| 32 | + expiration, | ||
| 33 | + key, | ||
| 34 | + value | ||
| 35 | + }; | ||
| 36 | + this.store.setItem(key, JSON.stringify(entity)); | ||
| 37 | + return this; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + get(key: string) { | ||
| 41 | + let entity: any; | ||
| 42 | + try { | ||
| 43 | + entity = this.store.getItem(key); | ||
| 44 | + if (entity) { | ||
| 45 | + entity = JSON.parse(entity); | ||
| 46 | + } else { | ||
| 47 | + return null; | ||
| 48 | + } | ||
| 49 | + } catch (err) { | ||
| 50 | + console.warn(err); | ||
| 51 | + return null; | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + // 没有设置过期时间, 则直接返回值 | ||
| 55 | + if (!entity.expiration) | ||
| 56 | + return entity.value; | ||
| 57 | + | ||
| 58 | + // 已过期 | ||
| 59 | + if (this.__isExpired(entity)) { | ||
| 60 | + this.remove(key); | ||
| 61 | + return null; | ||
| 62 | + } else { | ||
| 63 | + return entity.value; | ||
| 64 | + } | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + remove(key: string) { | ||
| 68 | + try { | ||
| 69 | + this.store.removeItem(key); | ||
| 70 | + } catch (err) { | ||
| 71 | + console.warn(err); | ||
| 72 | + } | ||
| 73 | + return this; | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + clear() { | ||
| 77 | + try { | ||
| 78 | + this.store.clear(); | ||
| 79 | + } catch (err) { | ||
| 80 | + console.warn(err); | ||
| 81 | + } | ||
| 82 | + return this; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + isExist(key: string): boolean { | ||
| 86 | + return this.store.hasKey(key); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + private static instance: StorageUtils; | ||
| 90 | + static get I(): StorageUtils { | ||
| 91 | + return this.instance || (this.instance = new StorageUtils); | ||
| 92 | + } | ||
| 93 | +} | ||
| 0 | \ No newline at end of file | 94 | \ No newline at end of file |
| @@ -0,0 +1,137 @@ | @@ -0,0 +1,137 @@ | ||
| 1 | +import { __LOG__ } from "../base/SDKConst"; | ||
| 2 | + | ||
| 3 | +enum PlatformType { | ||
| 4 | + WX = 'wx', | ||
| 5 | + QQ = 'qq' | ||
| 6 | +} | ||
| 7 | +enum Platform{ | ||
| 8 | + PlatformType = 'wx' | ||
| 9 | +} | ||
| 10 | +export default class Version { | ||
| 11 | + private DEFUALT = '0.0.0'; | ||
| 12 | + private VERSION = { | ||
| 13 | + createRewardedVideoAd: { | ||
| 14 | + [PlatformType.WX]: '2.0.4', | ||
| 15 | + [PlatformType.QQ]: '0.1.26' | ||
| 16 | + }, | ||
| 17 | + createInterstitialAd: { | ||
| 18 | + [PlatformType.WX]: '2.6.0' | ||
| 19 | + }, | ||
| 20 | + createFeedbackButton: { | ||
| 21 | + [PlatformType.WX]: '2.1.2' | ||
| 22 | + }, | ||
| 23 | + createUserInfoButton: { | ||
| 24 | + [PlatformType.WX]: '2.0.1' | ||
| 25 | + }, | ||
| 26 | + createBannerAd: { | ||
| 27 | + [PlatformType.WX]: '2.0.4', | ||
| 28 | + [PlatformType.QQ]: '0.1.26' | ||
| 29 | + }, | ||
| 30 | + createGameBanner: { | ||
| 31 | + [PlatformType.WX]: '2.7.5' | ||
| 32 | + }, | ||
| 33 | + createGamePortal: { | ||
| 34 | + [PlatformType.WX]: '2.7.5' | ||
| 35 | + }, | ||
| 36 | + createGameIcon: { | ||
| 37 | + [PlatformType.WX]: '2.8.2' | ||
| 38 | + }, | ||
| 39 | + setClipboardData: { | ||
| 40 | + [PlatformType.WX]: '1.1.0' | ||
| 41 | + }, | ||
| 42 | + setSubscribeMessage: { | ||
| 43 | + [PlatformType.WX]: '2.8.0' | ||
| 44 | + }, | ||
| 45 | + getUpdateManager: { | ||
| 46 | + [PlatformType.WX]: '1.9.90' | ||
| 47 | + }, | ||
| 48 | + updateShareMenu: { | ||
| 49 | + [PlatformType.WX]: '1.2.0' | ||
| 50 | + }, | ||
| 51 | + showShareMenu: { | ||
| 52 | + [PlatformType.WX]: '1.1.0' | ||
| 53 | + }, | ||
| 54 | + vibrate: { | ||
| 55 | + [PlatformType.WX]: '1.2.0' | ||
| 56 | + }, | ||
| 57 | + getShareInfo: { | ||
| 58 | + [PlatformType.WX]: '1.1.0' | ||
| 59 | + }, | ||
| 60 | + openCustomerServiceConversation: { | ||
| 61 | + [PlatformType.WX]: '2.0.3' | ||
| 62 | + } | ||
| 63 | + }; | ||
| 64 | + | ||
| 65 | + private constructor() { | ||
| 66 | + __LOG__ && console.error('SDK Version ', this.VERSION, this.getVRewardedVideoAd(), this.getVInterstitialAd()); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + getVRewardedVideoAd() { | ||
| 70 | + return this.VERSION.createRewardedVideoAd[Platform.PlatformType] || this.DEFUALT; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + getVInterstitialAd() { | ||
| 74 | + return this.VERSION.createInterstitialAd[Platform.PlatformType] || this.DEFUALT; | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + getVFeedbackButton() { | ||
| 78 | + return this.VERSION.createFeedbackButton[Platform.PlatformType] || this.DEFUALT; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + getVUserInfoButton() { | ||
| 82 | + return this.VERSION.createUserInfoButton[Platform.PlatformType] || this.DEFUALT; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + getVBannerAd() { | ||
| 86 | + return this.VERSION.createBannerAd[Platform.PlatformType] || this.DEFUALT; | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + getVGameBanner() { | ||
| 90 | + return this.VERSION.createGameBanner[Platform.PlatformType] || this.DEFUALT; | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + getVGamePortal() { | ||
| 94 | + return this.VERSION.createGamePortal[Platform.PlatformType] || this.DEFUALT; | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + getVGameIcon() { | ||
| 98 | + return this.VERSION.createGameIcon[Platform.PlatformType] || this.DEFUALT; | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + getVClipboardData() { | ||
| 102 | + return this.VERSION.setClipboardData[Platform.PlatformType] || this.DEFUALT; | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + getVSubscribeMessage() { | ||
| 106 | + return this.VERSION.setSubscribeMessage[Platform.PlatformType] || this.DEFUALT; | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + getVUpdateManager() { | ||
| 110 | + return this.VERSION.getUpdateManager[Platform.PlatformType] || this.DEFUALT; | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + getVVibrate() { | ||
| 114 | + return this.VERSION.vibrate[Platform.PlatformType] || this.DEFUALT; | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + getVShareInfo() { | ||
| 118 | + return this.VERSION.getShareInfo[Platform.PlatformType] || this.DEFUALT; | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + getVUpdateShareMenu() { | ||
| 122 | + return this.VERSION.updateShareMenu[Platform.PlatformType] || this.DEFUALT; | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + getVShowShareMenu() { | ||
| 126 | + return this.VERSION.showShareMenu[Platform.PlatformType] || this.DEFUALT; | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + getVCustomerService() { | ||
| 130 | + return this.VERSION.openCustomerServiceConversation[Platform.PlatformType] || this.DEFUALT; | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + private static instance: Version; | ||
| 134 | + static get I(): Version { | ||
| 135 | + return this.instance || (this.instance = new Version()); | ||
| 136 | + } | ||
| 137 | +} | ||
| 0 | \ No newline at end of file | 138 | \ No newline at end of file |
| @@ -0,0 +1,415 @@ | @@ -0,0 +1,415 @@ | ||
| 1 | +import SDKUtils from "../utils/SDKUtils"; | ||
| 2 | +import WxSystem from "./WxSystem"; | ||
| 3 | +import Version from "./Version"; | ||
| 4 | +import { __LOG__ } from "../base/SDKConst"; | ||
| 5 | +import { NetworkType } from "../base/SDKEnum"; | ||
| 6 | +import DataService from "../service/DataService"; | ||
| 7 | + | ||
| 8 | +export default class WxApi { | ||
| 9 | + private getVersionError(version: string): { errMsg: string; errCode: number } { | ||
| 10 | + return { errMsg: `支持最低版本:${version}`, errCode: -1 }; | ||
| 11 | + } | ||
| 12 | + | ||
| 13 | + canIUse(version: string): boolean { | ||
| 14 | + return SDKUtils.compareVersion(WxSystem.I.SDKVersion, version) >= 0; | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + // 注意 | ||
| 18 | + // Android 6.7.2 以下版本,点击取消或蒙层时,回调 fail, errMsg 为 "fail cancel"; | ||
| 19 | + // Android 6.7.2 及以上版本 和 iOS 点击蒙层不会关闭模态弹窗,所以尽量避免使用「取消」分支中实现业务逻辑 | ||
| 20 | + showModal(data: any) { | ||
| 21 | + wx.showModal(data); | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + // copy(str: string) { | ||
| 25 | + // let version = Version.I.getVClipboardData(); | ||
| 26 | + // if (!this.canIUse(version)) return Promise.reject(this.getVersionError(version)); | ||
| 27 | + | ||
| 28 | + // return new Promise((resolve, reject) => { | ||
| 29 | + // wx.setClipboardData({ | ||
| 30 | + // data: str, | ||
| 31 | + // success: (res: any) => { | ||
| 32 | + // resolve(res); | ||
| 33 | + // }, | ||
| 34 | + // fail: (err: any) => { | ||
| 35 | + // reject(err); | ||
| 36 | + // } | ||
| 37 | + // }); | ||
| 38 | + // }); | ||
| 39 | + // } | ||
| 40 | + | ||
| 41 | + subscribeMessage(tmplIds: Array<string>) { | ||
| 42 | + let version = Version.I.getVSubscribeMessage(); | ||
| 43 | + if (!this.canIUse(version)) return Promise.reject(this.getVersionError(version)); | ||
| 44 | + console.log("tmplIds",tmplIds) | ||
| 45 | + return new Promise((resolve, reject) => { | ||
| 46 | + wx.requestSubscribeMessage({ | ||
| 47 | + tmplIds, | ||
| 48 | + success: (ret: { errMsg: string }) => { | ||
| 49 | + resolve(ret); | ||
| 50 | + }, | ||
| 51 | + fail: (err: { errMsg: string; errCode: number }) => { | ||
| 52 | + reject(err); | ||
| 53 | + } | ||
| 54 | + }); | ||
| 55 | + }); | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + /** | ||
| 59 | + * 创建banner广告 | ||
| 60 | + */ | ||
| 61 | + createBannerAd(adUnitId: string, style: _StyleObject, adIntervals?: number) { | ||
| 62 | + if (!this.canIUse(Version.I.getVBannerAd())) return null; | ||
| 63 | + | ||
| 64 | + adIntervals = adIntervals || 30; | ||
| 65 | + return wx.createBannerAd({ | ||
| 66 | + adUnitId, | ||
| 67 | + style | ||
| 68 | + }); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + /** | ||
| 72 | + * 创建小游戏推荐banner | ||
| 73 | + */ | ||
| 74 | + createGameBanner(adUnitId: string, style: _StyleGameObject) { | ||
| 75 | + if (!this.canIUse(Version.I.getVGameBanner())) return null; | ||
| 76 | + | ||
| 77 | + return wx.createGameBanner({ | ||
| 78 | + adUnitId, | ||
| 79 | + style | ||
| 80 | + }); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + /** | ||
| 84 | + * 创建插屏广告组件 | ||
| 85 | + */ | ||
| 86 | + createInterstitialAd(adUnitId: string) { | ||
| 87 | + if (!this.canIUse(Version.I.getVInterstitialAd())) return null; | ||
| 88 | + return wx.createInterstitialAd({ | ||
| 89 | + adUnitId | ||
| 90 | + }); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + /** | ||
| 94 | + * 创建小游戏推荐icon组件 | ||
| 95 | + */ | ||
| 96 | + createGameIcon(adUnitId: string, opts: { count: number; style: Array<_GameIconStyleItem> }) {// | ||
| 97 | + if (!this.canIUse(Version.I.getVGameIcon())) return null; | ||
| 98 | + | ||
| 99 | + return wx.createGameIcon({ adUnitId, ...opts }); | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + /** | ||
| 103 | + * 创建小游戏推荐弹窗组件 | ||
| 104 | + */ | ||
| 105 | + createGamePortal(adUnitId: string) { | ||
| 106 | + if (!this.canIUse(Version.I.getVGamePortal())) return null; | ||
| 107 | + | ||
| 108 | + return wx.createGamePortal({ | ||
| 109 | + adUnitId | ||
| 110 | + }); | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + /** | ||
| 114 | + * 创建激励视频广告组件 | ||
| 115 | + */ | ||
| 116 | + createRewardedVideoAd(adUnitId: string) { | ||
| 117 | + if (!this.canIUse(Version.I.getVRewardedVideoAd())) return null; | ||
| 118 | + | ||
| 119 | + return wx.createRewardedVideoAd({ | ||
| 120 | + adUnitId | ||
| 121 | + }); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + /** | ||
| 125 | + * 创建打开意见反馈页面的按钮 | ||
| 126 | + */ | ||
| 127 | + createFeedbackButton(data: _FeedbackButtonObject) { | ||
| 128 | + if (!this.canIUse(Version.I.getVFeedbackButton())) return null; | ||
| 129 | + | ||
| 130 | + return wx.createFeedbackButton(data); | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + /** | ||
| 134 | + * 创建用户信息按钮 | ||
| 135 | + */ | ||
| 136 | + createUserInfoButton(data: _UserInfoButtonObject): _UserInfoButton | null {// | ||
| 137 | + if (!this.canIUse(Version.I.getVUserInfoButton())) return null; | ||
| 138 | + return wx.createUserInfoButton(data); | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + /** | ||
| 142 | + * 进入客服会话。要求在用户发生过至少一次 touch 事件后才能调用 | ||
| 143 | + */ | ||
| 144 | + openCustomerServiceConversation(params: _CustomerServiceConversationObject) {// | ||
| 145 | + if (!this.canIUse(Version.I.getVCustomerService())) return -1; | ||
| 146 | + wx.openCustomerServiceConversation({ ...params }); | ||
| 147 | + return 1; | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + vibrateLong() { | ||
| 151 | + if (!this.canIUse(Version.I.getVVibrate())) return Promise.reject(null); | ||
| 152 | + | ||
| 153 | + return new Promise((resolve, reject) => { | ||
| 154 | + wx.vibrateLong({ | ||
| 155 | + success: () => { | ||
| 156 | + resolve(1); | ||
| 157 | + }, | ||
| 158 | + fail: () => { | ||
| 159 | + resolve(0); | ||
| 160 | + } | ||
| 161 | + }); | ||
| 162 | + }); | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | + vibrateShort() { | ||
| 166 | + if (!this.canIUse(Version.I.getVVibrate())) return Promise.reject(null); | ||
| 167 | + | ||
| 168 | + return new Promise((resolve, reject) => { | ||
| 169 | + wx.vibrateShort({ | ||
| 170 | + success: () => { | ||
| 171 | + resolve(1); | ||
| 172 | + }, | ||
| 173 | + fail: () => { | ||
| 174 | + resolve(0); | ||
| 175 | + } | ||
| 176 | + }); | ||
| 177 | + }); | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + /** | ||
| 181 | + * 更新转发属性 | ||
| 182 | + */ | ||
| 183 | + updateShareMenu(value: boolean) { | ||
| 184 | + if (!this.canIUse(Version.I.getVUpdateShareMenu())) return; | ||
| 185 | + wx.updateShareMenu({ | ||
| 186 | + withShareTicket: value | ||
| 187 | + }); | ||
| 188 | + } | ||
| 189 | + | ||
| 190 | + /** | ||
| 191 | + * 检测更新 | ||
| 192 | + */ | ||
| 193 | + checkUpdate(data: _ShowModalObject): void {// | ||
| 194 | + if (!this.canIUse(Version.I.getVUpdateManager())) return; | ||
| 195 | + | ||
| 196 | + data = data || {}; | ||
| 197 | + // tslint:disable-next-line: no-this-assignment | ||
| 198 | + let that = this; | ||
| 199 | + // 默认参数 | ||
| 200 | + let params: _ShowModalObject = {// | ||
| 201 | + title: data.title || '更新提示', | ||
| 202 | + content: data.content || '新版本已经准备好,是否重启应用?', | ||
| 203 | + showCancel: false, | ||
| 204 | + success: (res: _ShowModalSuccessObject) => {// | ||
| 205 | + data.success && data.success(res); | ||
| 206 | + if (res.confirm) { | ||
| 207 | + updateManager.applyUpdate(); | ||
| 208 | + } | ||
| 209 | + }, | ||
| 210 | + fail: (err: any) => data.fail && data.fail(err), | ||
| 211 | + complete: () => data.complete && data.complete() | ||
| 212 | + }; | ||
| 213 | + // 合并参数 | ||
| 214 | + let keys = ['cancelText', 'cancelColor', 'confirmText', 'confirmColor']; | ||
| 215 | + keys.forEach(key => SDKUtils.hasProperty(data, key) && (params = { ...params, [key]: data[key] })); | ||
| 216 | + | ||
| 217 | + let updateManager = wx.getUpdateManager(); | ||
| 218 | + updateManager.onCheckForUpdate((res: any) => { | ||
| 219 | + __LOG__ && console.warn('====> PCSDK WxApi updateManager onCheckForUpdate', res.hasUpdate); | ||
| 220 | + }); | ||
| 221 | + updateManager.onUpdateReady(function () { | ||
| 222 | + that.showModal(params); | ||
| 223 | + }); | ||
| 224 | + updateManager.onUpdateFailed(function () { | ||
| 225 | + // 新的版本下载失败 | ||
| 226 | + __LOG__ && console.warn('SDK WxApi updateManager onUpdateFailed'); | ||
| 227 | + }); | ||
| 228 | + } | ||
| 229 | + | ||
| 230 | + /** | ||
| 231 | + * 显示当前页面的转发按钮 | ||
| 232 | + */ | ||
| 233 | + showShareMenu(shareApp: _ShareAppMessageObject, shareMenu: _UpdateShareMenuObject) { | ||
| 234 | + if (!this.canIUse(Version.I.getVShowShareMenu())) return; | ||
| 235 | + __LOG__ && console.error('showShareMenu shareApp'); | ||
| 236 | + __LOG__ && console.error('showShareMenu shareMenu'); | ||
| 237 | + wx.onShareAppMessage(() => shareApp); | ||
| 238 | + wx.showShareMenu(shareMenu); | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + /** | ||
| 242 | + * 主动拉起转发,进入选择通讯录界面。 | ||
| 243 | + */ | ||
| 244 | + shareAppMessage(data: _ShareAppMessageObject) { | ||
| 245 | + wx.shareAppMessage(data); | ||
| 246 | + } | ||
| 247 | + | ||
| 248 | + previewImage(imgList: Array<string>, index: number) { | ||
| 249 | + return new Promise((suc, fail) => { | ||
| 250 | + if (!imgList || imgList.length === 0) { | ||
| 251 | + fail(); | ||
| 252 | + return; | ||
| 253 | + } | ||
| 254 | + wx.previewImage({ | ||
| 255 | + current: imgList[index], // 当前显示图片的http链接 | ||
| 256 | + urls: imgList, // 需要预览的图片http链接列表 | ||
| 257 | + success(res: any) { | ||
| 258 | + suc({ ...res, qrcode: 1 }); | ||
| 259 | + }, | ||
| 260 | + fail(err: any) { | ||
| 261 | + fail(err); | ||
| 262 | + } | ||
| 263 | + }); | ||
| 264 | + }); | ||
| 265 | + } | ||
| 266 | + | ||
| 267 | + getNetworkType(): Promise<_NetworkTypeSuccessObject> { | ||
| 268 | + return new Promise((resolve, reject) => { | ||
| 269 | + wx.getNetworkType({ | ||
| 270 | + success(res: _NetworkTypeSuccessObject) { | ||
| 271 | + resolve(res); | ||
| 272 | + }, | ||
| 273 | + fail(err: any) { | ||
| 274 | + reject({ networkType: NetworkType.Unknown }); | ||
| 275 | + } | ||
| 276 | + }); | ||
| 277 | + }); | ||
| 278 | + } | ||
| 279 | + | ||
| 280 | + navigateToMiniProgram(appId: string, path: string, opts: any = {}) { | ||
| 281 | + return new Promise((resolve, reject) => { | ||
| 282 | + wx.navigateToMiniProgram({ | ||
| 283 | + appId, | ||
| 284 | + path, | ||
| 285 | + extraData: opts.extraData || {}, | ||
| 286 | + envVersion: opts.envVersion || 'release', | ||
| 287 | + success(res: any) { | ||
| 288 | + resolve(res); | ||
| 289 | + }, | ||
| 290 | + fail(err: any) { | ||
| 291 | + reject(err); | ||
| 292 | + } | ||
| 293 | + }); | ||
| 294 | + }); | ||
| 295 | + } | ||
| 296 | + | ||
| 297 | + /** | ||
| 298 | + * login获取code接口 | ||
| 299 | + */ | ||
| 300 | + login(): Promise<any> { | ||
| 301 | + return new Promise((resolve, reject) => { | ||
| 302 | + wx.login({ | ||
| 303 | + success: (ret: any) => { | ||
| 304 | + resolve(ret.code); | ||
| 305 | + }, | ||
| 306 | + fail: (err: any) => { | ||
| 307 | + reject(err); | ||
| 308 | + WxApi.I.setAuthorize({ | ||
| 309 | + errorTip: 'wx.login fail', | ||
| 310 | + ...err | ||
| 311 | + }); | ||
| 312 | + } | ||
| 313 | + }); | ||
| 314 | + }); | ||
| 315 | + } | ||
| 316 | + | ||
| 317 | + /** | ||
| 318 | + * 用户信息授权接口 | ||
| 319 | + */ | ||
| 320 | + getUserinfo(): Promise<any> { | ||
| 321 | + return new Promise((resolve, reject) => { | ||
| 322 | + wx.getUserInfo({ | ||
| 323 | + lang: "zh_CN", | ||
| 324 | + withCredentials: true, | ||
| 325 | + success: (ret: any) => { | ||
| 326 | + if (!ret) { | ||
| 327 | + reject(ret); | ||
| 328 | + return WxApi.I.setAuthorize({ | ||
| 329 | + errorTip: 'getUserInfo Success result is null', | ||
| 330 | + ...ret | ||
| 331 | + }); | ||
| 332 | + } | ||
| 333 | + resolve(ret); | ||
| 334 | + }, | ||
| 335 | + fail: (err: any) => { | ||
| 336 | + reject(err); | ||
| 337 | + WxApi.I.setAuthorize({ | ||
| 338 | + errorTip: 'getUserInfo Fail', | ||
| 339 | + ...err | ||
| 340 | + }); | ||
| 341 | + } | ||
| 342 | + }); | ||
| 343 | + }); | ||
| 344 | + } | ||
| 345 | + | ||
| 346 | + /** | ||
| 347 | + * 分享接口 | ||
| 348 | + * @param shareTicket | ||
| 349 | + */ | ||
| 350 | + getShareInfo(shareTicket: string): Promise<any> { | ||
| 351 | + if (!this.canIUse(Version.I.getVShareInfo())) return Promise.reject(null); | ||
| 352 | + | ||
| 353 | + return new Promise((resolve, reject) => { | ||
| 354 | + wx.getShareInfo({ | ||
| 355 | + shareTicket, | ||
| 356 | + success: (ret: _getShareInfoSuccessObject) => { | ||
| 357 | + let { errMsg } = ret; | ||
| 358 | + if (errMsg === 'getShareInfo:ok') { | ||
| 359 | + resolve(ret); | ||
| 360 | + } else { | ||
| 361 | + reject(ret); | ||
| 362 | + } | ||
| 363 | + }, | ||
| 364 | + fail: (err: any) => { | ||
| 365 | + reject(err); | ||
| 366 | + } | ||
| 367 | + }); | ||
| 368 | + }); | ||
| 369 | + } | ||
| 370 | + | ||
| 371 | + /** | ||
| 372 | + * 米大师充值 | ||
| 373 | + */ | ||
| 374 | + requestMidasPayment(params: { mode: string; env: number; offerId: string; currencyType: string; platform: string; buyQuantity: number; zoneId: string }): Promise<any> { | ||
| 375 | + let { mode, env, offerId, currencyType, platform, buyQuantity, zoneId } = params; | ||
| 376 | + console.warn('====> PCSDK WxApi requestMidasPayment 支付参数', { | ||
| 377 | + mode, | ||
| 378 | + env, | ||
| 379 | + offerId, | ||
| 380 | + currencyType, | ||
| 381 | + platform, | ||
| 382 | + buyQuantity, | ||
| 383 | + zoneId | ||
| 384 | + }); | ||
| 385 | + return new Promise((resolve, reject) => { | ||
| 386 | + wx.requestMidasPayment({ | ||
| 387 | + mode, | ||
| 388 | + env, | ||
| 389 | + offerId, | ||
| 390 | + currencyType, | ||
| 391 | + platform, | ||
| 392 | + buyQuantity, | ||
| 393 | + zoneId, | ||
| 394 | + success: ret => { | ||
| 395 | + if (ret && ret.errMsg === 'requestMidasPayment:ok') | ||
| 396 | + resolve(ret); | ||
| 397 | + else | ||
| 398 | + reject(ret); | ||
| 399 | + }, | ||
| 400 | + fail: err => { | ||
| 401 | + reject(err); | ||
| 402 | + } | ||
| 403 | + }); | ||
| 404 | + }); | ||
| 405 | + } | ||
| 406 | + | ||
| 407 | + private setAuthorize(error: any) { | ||
| 408 | + DataService.I.setAuthorize(false); | ||
| 409 | + } | ||
| 410 | + | ||
| 411 | + private static _instance: WxApi; | ||
| 412 | + static get I(): WxApi { | ||
| 413 | + return this._instance || (this._instance = new WxApi); | ||
| 414 | + } | ||
| 415 | +} | ||
| 0 | \ No newline at end of file | 416 | \ No newline at end of file |
| @@ -0,0 +1,230 @@ | @@ -0,0 +1,230 @@ | ||
| 1 | +import WxSystem from "./WxSystem"; | ||
| 2 | +import SDKUtils from "../utils/SDKUtils"; | ||
| 3 | +import { BannerError, __LOG__ } from "../base/SDKConst"; | ||
| 4 | +import LogService from "../service/LogService"; | ||
| 5 | +import { DOT_AD_STATUS, DOT_AD_TYPE } from "../base/SDKEnum"; | ||
| 6 | + | ||
| 7 | +/* | ||
| 8 | +* banner | ||
| 9 | +*/ | ||
| 10 | +export default class WxBanner { | ||
| 11 | + private static instance: WxBanner; | ||
| 12 | + static get I(): WxBanner { | ||
| 13 | + return this.instance || (this.instance = new WxBanner(750, 750)); | ||
| 14 | + } | ||
| 15 | + private designWidth: number; | ||
| 16 | + private bannerWidth: number; | ||
| 17 | + private bannerHeight: number; | ||
| 18 | + private bannerScale: number; | ||
| 19 | + private bannerParams: any; | ||
| 20 | + private _isErrored: boolean; | ||
| 21 | + private adUnitId: string; | ||
| 22 | + private bannerAd: any; | ||
| 23 | + private resolve: any; | ||
| 24 | + private reject: any; | ||
| 25 | + | ||
| 26 | + constructor(designWidth: number, bannerWidth: number) { | ||
| 27 | + designWidth = designWidth || 750; | ||
| 28 | + bannerWidth = bannerWidth || 750; | ||
| 29 | + | ||
| 30 | + this.adUnitId = ''; | ||
| 31 | + this._isErrored = false; | ||
| 32 | + this.designWidth = designWidth; | ||
| 33 | + this.bannerScale = WxSystem.I.winWidth / this.designWidth; | ||
| 34 | + this.bannerWidth = Math.max(this.bannerScale * bannerWidth, 300); | ||
| 35 | + this.bannerHeight = WxSystem.I.winHeight / this.bannerScale; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + get isErrored() { | ||
| 39 | + return this._isErrored; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + private queue: Function[] = []; | ||
| 43 | + private isEnd: boolean = false; | ||
| 44 | + | ||
| 45 | + create(adUnitId: string, opts?: { type?: number; bannerWidth?: number, offsetY?: number; adIntervals?: number, isOff?: boolean }) { | ||
| 46 | + this.bannerParams = opts || {}; | ||
| 47 | + if (opts && opts.bannerWidth) { | ||
| 48 | + this.bannerWidth = opts.bannerWidth; | ||
| 49 | + } | ||
| 50 | + this.isEnd = true; | ||
| 51 | + this.bannerParams.type = this.bannerParams.type || 1; | ||
| 52 | + this.bannerParams.offsetY = -this.bannerParams.offsetY || 0; | ||
| 53 | + this.bannerParams.adIntervals = this.bannerParams.adIntervals || 120; | ||
| 54 | + return new Promise((resolve, reject) => { | ||
| 55 | + this.resolve = resolve; | ||
| 56 | + this.reject = reject; | ||
| 57 | + this._isErrored = false; | ||
| 58 | + this.adUnitId = adUnitId; | ||
| 59 | + | ||
| 60 | + if (SDKUtils.isEmpty(adUnitId)) { | ||
| 61 | + WxBanner.I.handleQueue(); | ||
| 62 | + return this.reject({ ...BannerError.BannerInvalid, adUnitId: this.adUnitId }); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + // 设置样式(hack:修复qq版本) | ||
| 66 | + this.bannerParams.type === 2 && (this.bannerWidth = WxSystem.I.winWidth); | ||
| 67 | + let style = { top: 0, left: (WxSystem.I.winWidth - this.bannerWidth) / 2, width: this.bannerWidth }; | ||
| 68 | + style = { | ||
| 69 | + ...style, | ||
| 70 | + top: 0 + this.bannerParams.offsetY, | ||
| 71 | + }; | ||
| 72 | + // 创建并判断是否存在 | ||
| 73 | + // if (this.bannerParams.type === 2) | ||
| 74 | + // this.bannerAd = wx.createGameBanner({ adUnitId, style: { left: style.left, top: this.bannerHeight } }); | ||
| 75 | + // else | ||
| 76 | + if (this.bannerAd) { | ||
| 77 | + if (!opts || (opts && !opts.isOff)) { | ||
| 78 | + this.show(false); | ||
| 79 | + } | ||
| 80 | + return | ||
| 81 | + } | ||
| 82 | + this.bannerAd = wx.createBannerAd({ adUnitId, style, adIntervals: this.bannerParams.adIntervals }); | ||
| 83 | + LogService.I.adStat('banner', this.adUnitId, DOT_AD_TYPE.banner, DOT_AD_STATUS.request) | ||
| 84 | + if (!this.bannerAd) { | ||
| 85 | + WxBanner.I.handleQueue(); | ||
| 86 | + return this.reject({ ...BannerError.BannerNotOpen, adUnitId: this.adUnitId }); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + this.bannerAd.onLoad(this.onLoad); | ||
| 90 | + this.bannerAd.onError(this.onError); | ||
| 91 | + this.bannerAd.onResize(this.onResize); | ||
| 92 | + if (!opts || (opts && !opts.isOff)) { | ||
| 93 | + this.show(false); | ||
| 94 | + } | ||
| 95 | + }); | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + /** | ||
| 99 | + * | ||
| 100 | + * @param bol | ||
| 101 | + */ | ||
| 102 | + show(bol: boolean = true): boolean { | ||
| 103 | + if (WxBanner.I.isEnd && bol) { | ||
| 104 | + WxBanner.I.queue.push(WxBanner.I.show.bind(WxBanner.I)) | ||
| 105 | + return | ||
| 106 | + } | ||
| 107 | + WxBanner.I.isEnd = true | ||
| 108 | + if (this.bannerAd) { | ||
| 109 | + if (this.bannerAd.style.realHeight) | ||
| 110 | + this.bannerAd.style.top = WxSystem.I.winHeight - this.bannerAd.style.realHeight + this.bannerParams.offsetY; | ||
| 111 | + if (this.bannerParams.type === 2) { | ||
| 112 | + if (this.bannerAd.style.width) | ||
| 113 | + this.bannerAd.style.left = (WxSystem.I.winWidth - this.bannerAd.style.width) / 2; | ||
| 114 | + else | ||
| 115 | + this.bannerAd.style.left = (WxSystem.I.winWidth - this.bannerWidth) / 2; | ||
| 116 | + } else | ||
| 117 | + this.bannerAd.style.left = (WxSystem.I.winWidth - this.bannerWidth) / 2; | ||
| 118 | + | ||
| 119 | + LogService.I.adStat('banner', this.adUnitId, DOT_AD_TYPE.banner, DOT_AD_STATUS.show) | ||
| 120 | + this.bannerAd.show().catch((err: any) => this.handleShowError(err)); | ||
| 121 | + __LOG__ && console.error('wxBanner - show: ' + this.adUnitId, this.bannerAd.style); | ||
| 122 | + WxBanner.I.handleQueue() | ||
| 123 | + return true; | ||
| 124 | + } | ||
| 125 | + WxBanner.I.handleQueue() | ||
| 126 | + return false; | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + hide() { | ||
| 130 | + let that = WxBanner.I; | ||
| 131 | + if (that.isEnd) { | ||
| 132 | + that.queue.push(that.hide.bind(that)) | ||
| 133 | + return | ||
| 134 | + } | ||
| 135 | + that.isEnd = true | ||
| 136 | + if (this.bannerAd) { | ||
| 137 | + this.bannerAd.style.left = -9999; | ||
| 138 | + this.bannerAd.hide(); | ||
| 139 | + LogService.I.adStat('banner', that.adUnitId, DOT_AD_TYPE.banner, DOT_AD_STATUS.interrupt) | ||
| 140 | + __LOG__ && console.error('wxBanner - hide: ' + this.adUnitId); | ||
| 141 | + } | ||
| 142 | + that.handleQueue() | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + toggle(isshow: boolean) { | ||
| 146 | + isshow ? this.show() : this.hide(); | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + destory() { | ||
| 150 | + if (this.bannerAd) { | ||
| 151 | + this.bannerAd.style.left = -9999; | ||
| 152 | + this.bannerAd.destroy(); | ||
| 153 | + this.bannerAd = null; | ||
| 154 | + LogService.I.adStat('banner', this.adUnitId, DOT_AD_TYPE.banner, DOT_AD_STATUS.interrupt) | ||
| 155 | + __LOG__ && console.error('wxBanner - destory: ' + this.adUnitId); | ||
| 156 | + } | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + private onLoad() { | ||
| 160 | + let that = WxBanner.I; | ||
| 161 | + let bannerAd = that.bannerAd; | ||
| 162 | + if (!bannerAd) return; | ||
| 163 | + // Platform.IsQQ && that.show(); | ||
| 164 | + __LOG__ && console.error('wxBanner - onLoad: ' + that.adUnitId); | ||
| 165 | + if (bannerAd.style.realHeight) | ||
| 166 | + bannerAd.style.top = WxSystem.I.winHeight - bannerAd.style.realHeight + that.bannerParams.offsetY; | ||
| 167 | + that.unbind(); | ||
| 168 | + that.handleQueue(); | ||
| 169 | + that.resolve && that.resolve({ | ||
| 170 | + adUnitId: that.adUnitId, | ||
| 171 | + scale: that.bannerScale, | ||
| 172 | + width: bannerAd.style.realWidth / that.bannerScale, | ||
| 173 | + height: bannerAd.style.realHeight / that.bannerScale | ||
| 174 | + }); | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | + private onError(err: any) { | ||
| 178 | + __LOG__ && console.error('wxBanner - onError', err); | ||
| 179 | + let that = WxBanner.I; | ||
| 180 | + !that._isErrored && that.handleError(err, { ...BannerError.BannerFail, adUnitId: that.adUnitId }); | ||
| 181 | + } | ||
| 182 | + | ||
| 183 | + private onResize() { | ||
| 184 | + let that = WxBanner.I; | ||
| 185 | + let bannerAd = that.bannerAd; | ||
| 186 | + if (!bannerAd) return; | ||
| 187 | + bannerAd.style.top = WxSystem.I.winHeight - bannerAd.style.realHeight + that.bannerParams.offsetY; | ||
| 188 | + bannerAd.style.left = (WxSystem.I.winWidth - bannerAd.style.realWidth) / 2; | ||
| 189 | + } | ||
| 190 | + | ||
| 191 | + private handleShowError(ret: { errCode: number; errMsg: string }) { | ||
| 192 | + __LOG__ && console.error('wxBanner - handleShowError', ret); | ||
| 193 | + let that = WxBanner.I; | ||
| 194 | + let { errCode, errMsg } = ret; | ||
| 195 | + !that.isErrored && that.handleError(ret, { code: errCode, msg: errMsg }); | ||
| 196 | + } | ||
| 197 | + | ||
| 198 | + private handleError(ret: any, err: any) { | ||
| 199 | + let that = WxBanner.I; | ||
| 200 | + that.unbind(); | ||
| 201 | + that._isErrored = true; | ||
| 202 | + that.bannerAd = null; | ||
| 203 | + that.handleQueue() | ||
| 204 | + LogService.I.adStat('banner', that.adUnitId, DOT_AD_TYPE.banner, DOT_AD_STATUS.fail) | ||
| 205 | + that.reject && that.reject({ ...err, adUnitId: that.adUnitId }); | ||
| 206 | + __LOG__ && console.error('wxBanner - onError: ' + that.adUnitId); | ||
| 207 | + } | ||
| 208 | + | ||
| 209 | + private unbind() { | ||
| 210 | + if (this.bannerAd) { | ||
| 211 | + this.bannerAd.offLoad(this.onLoad); | ||
| 212 | + this.bannerAd.offError(this.onError); | ||
| 213 | + this.bannerAd.offResize(this.onResize); | ||
| 214 | + } | ||
| 215 | + } | ||
| 216 | + | ||
| 217 | + /** | ||
| 218 | + * 处队列 | ||
| 219 | + */ | ||
| 220 | + handleQueue() { | ||
| 221 | + let that = WxBanner.I; | ||
| 222 | + if (that.queue.length > 0) { | ||
| 223 | + that.isEnd = false; | ||
| 224 | + let fn = that.queue.shift(); | ||
| 225 | + fn(); | ||
| 226 | + } else { | ||
| 227 | + that.isEnd = false; | ||
| 228 | + } | ||
| 229 | + } | ||
| 230 | +} | ||
| 0 | \ No newline at end of file | 231 | \ No newline at end of file |
| @@ -0,0 +1,200 @@ | @@ -0,0 +1,200 @@ | ||
| 1 | +import WxSystem from "./WxSystem"; | ||
| 2 | +import SDKUtils from "../utils/SDKUtils"; | ||
| 3 | +import { BannerError, __LOG__ } from "../base/SDKConst"; | ||
| 4 | +import LogService from "../service/LogService"; | ||
| 5 | +import { DOT_AD_STATUS, DOT_AD_TYPE } from "../base/SDKEnum"; | ||
| 6 | + | ||
| 7 | +/* | ||
| 8 | +* 原生模板广告 | ||
| 9 | +*/ | ||
| 10 | +export default class WxCustom { | ||
| 11 | + private static instance: WxCustom; | ||
| 12 | + static get I(): WxCustom { | ||
| 13 | + return this.instance || (this.instance = new WxCustom()); | ||
| 14 | + } | ||
| 15 | + private bannerParams: any; | ||
| 16 | + private _isErrored: boolean; | ||
| 17 | + private adUnitId: string; | ||
| 18 | + private customAd: any; | ||
| 19 | + private resolve: any; | ||
| 20 | + private reject: any; | ||
| 21 | + | ||
| 22 | + constructor() { | ||
| 23 | + this.adUnitId = ''; | ||
| 24 | + this._isErrored = false; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + get isErrored() { | ||
| 28 | + return this._isErrored; | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + private queue: Function[] = []; | ||
| 32 | + private isEnd: boolean = false; | ||
| 33 | + | ||
| 34 | + create(adUnitId: string, opts?: { top: number, left: number, adIntervals?: number }) { | ||
| 35 | + this.bannerParams = opts || {}; | ||
| 36 | + this.bannerParams.adIntervals = this.bannerParams.adIntervals || 60; | ||
| 37 | + | ||
| 38 | + if (this.customAd) { | ||
| 39 | + // this.show(false); | ||
| 40 | + if (WxCustom.I.isEnd) { | ||
| 41 | + WxCustom.I.queue.push(WxCustom.I.show.bind(WxCustom.I)) | ||
| 42 | + return | ||
| 43 | + } | ||
| 44 | + } | ||
| 45 | + this.isEnd = true; | ||
| 46 | + return new Promise((resolve, reject) => { | ||
| 47 | + this.resolve = resolve; | ||
| 48 | + this.reject = reject; | ||
| 49 | + this._isErrored = false; | ||
| 50 | + this.adUnitId = adUnitId; | ||
| 51 | + | ||
| 52 | + if (SDKUtils.isEmpty(adUnitId)) { | ||
| 53 | + WxCustom.I.handleQueue(); | ||
| 54 | + return this.reject({ ...BannerError.BannerInvalid, adUnitId: this.adUnitId }); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + if (this.customAd) { | ||
| 58 | + this.show(false); | ||
| 59 | + return | ||
| 60 | + } | ||
| 61 | + let style = { top: opts.top, left: opts.left }; | ||
| 62 | + // console.log("style", style) | ||
| 63 | + // 创建并判断是否存在 | ||
| 64 | + this.customAd = wx.createCustomAd({ adUnitId, style, adIntervals: this.bannerParams.adIntervals }); | ||
| 65 | + // console.log("this.customAd") | ||
| 66 | + // console.log(this.customAd) | ||
| 67 | + // console.log(JSON.stringify(this.customAd)) | ||
| 68 | + LogService.I.adStat('custom', this.adUnitId, DOT_AD_TYPE.custom, DOT_AD_STATUS.request) | ||
| 69 | + // console.log("create_custom", this.customAd) | ||
| 70 | + if (!this.customAd) { | ||
| 71 | + return this.reject({ ...BannerError.BannerNotOpen, adUnitId: this.adUnitId }); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + this.customAd.onLoad(this.onLoad); | ||
| 75 | + this.customAd.onClose(this.onClose); | ||
| 76 | + this.customAd.onError(this.onError); | ||
| 77 | + }); | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + show(bol: boolean = true): boolean { | ||
| 81 | + if (WxCustom.I.isEnd && bol) { | ||
| 82 | + WxCustom.I.queue.push(WxCustom.I.show.bind(WxCustom.I)) | ||
| 83 | + return | ||
| 84 | + } | ||
| 85 | + WxCustom.I.isEnd = true; | ||
| 86 | + // console.log("that.isEnd", this.isEnd, this.customAd) | ||
| 87 | + if (this.customAd) { | ||
| 88 | + LogService.I.adStat('custom', this.adUnitId, DOT_AD_TYPE.custom, DOT_AD_STATUS.show) | ||
| 89 | + this.customAd.show().then(res=>{ | ||
| 90 | + WxCustom.I.handleQueue(); | ||
| 91 | + }).catch((err: any) => this.handleShowError(err)); | ||
| 92 | + __LOG__ && console.error('WxCustom - show: ' + this.adUnitId, this.customAd); | ||
| 93 | + return true; | ||
| 94 | + } | ||
| 95 | + WxCustom.I.handleQueue(); | ||
| 96 | + return false; | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + hide() { | ||
| 100 | + let that = WxCustom.I; | ||
| 101 | + // console.log("that.isEnd", that.isEnd) | ||
| 102 | + if (that.isEnd) { | ||
| 103 | + that.queue.push(that.hide.bind(that)) | ||
| 104 | + return | ||
| 105 | + } | ||
| 106 | + that.isEnd = true | ||
| 107 | + if (this.customAd) { | ||
| 108 | + this.customAd.hide(); | ||
| 109 | + LogService.I.adStat('custom', that.adUnitId, DOT_AD_TYPE.custom, DOT_AD_STATUS.interrupt) | ||
| 110 | + __LOG__ && console.error('WxCustom - hide: ' + that.adUnitId); | ||
| 111 | + } | ||
| 112 | + that.handleQueue() | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + toggle(isshow: boolean) { | ||
| 116 | + isshow ? this.show() : this.hide(); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + destory() { | ||
| 120 | + if (this.customAd) { | ||
| 121 | + this.customAd.destroy(); | ||
| 122 | + this.customAd = null; | ||
| 123 | + LogService.I.adStat('custom', this.adUnitId, DOT_AD_TYPE.custom, DOT_AD_STATUS.interrupt) | ||
| 124 | + __LOG__ && console.error('WxCustom - destory: ' + this.adUnitId); | ||
| 125 | + } | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + private onLoad() { | ||
| 129 | + let that = WxCustom.I; | ||
| 130 | + let customAd = that.customAd; | ||
| 131 | + if (!customAd) return; | ||
| 132 | + that.show(false); | ||
| 133 | + __LOG__ && console.error('WxCustom - onLoad: ' + that.adUnitId); | ||
| 134 | + // if (customAd.style.top) { | ||
| 135 | + // customAd.style.top = that.bannerParams.top; | ||
| 136 | + // customAd.style.left = that.bannerParams.left; | ||
| 137 | + // } | ||
| 138 | + that.unbind(); | ||
| 139 | + that.resolve && that.resolve({ | ||
| 140 | + adUnitId: that.adUnitId, | ||
| 141 | + top: that.bannerParams.top, | ||
| 142 | + left: that.bannerParams.left, | ||
| 143 | + }); | ||
| 144 | + } | ||
| 145 | + | ||
| 146 | + private onError(err: any) { | ||
| 147 | + __LOG__ && console.error('WxCustom - onError', err); | ||
| 148 | + let that = WxCustom.I; | ||
| 149 | + !that._isErrored && that.handleError(err, { ...BannerError.BannerFail, adUnitId: that.adUnitId }); | ||
| 150 | + } | ||
| 151 | + //关闭广告 | ||
| 152 | + private onClose() { | ||
| 153 | + let that = WxCustom.I; | ||
| 154 | + that.customAd = null; | ||
| 155 | + LogService.I.dot("wxCustom_close") | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + private handleShowError(ret: { errCode: number; errMsg: string }) { | ||
| 159 | + __LOG__ && console.error('WxCustom - handleShowError', ret); | ||
| 160 | + let that = WxCustom.I; | ||
| 161 | + let { errCode, errMsg } = ret; | ||
| 162 | + !that.isErrored && that.handleError(ret, { code: errCode, msg: errMsg }); | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | + private handleError(ret: any, err: any) { | ||
| 166 | + let that = WxCustom.I; | ||
| 167 | + that.unbind(); | ||
| 168 | + that._isErrored = true; | ||
| 169 | + if(that.customAd){ | ||
| 170 | + that.customAd.destroy() | ||
| 171 | + } | ||
| 172 | + that.customAd = null | ||
| 173 | + that.handleQueue() | ||
| 174 | + that.reject && that.reject({ ...err, adUnitId: that.adUnitId }); | ||
| 175 | + LogService.I.adStat('custom', this.adUnitId, DOT_AD_TYPE.custom, DOT_AD_STATUS.fail) | ||
| 176 | + __LOG__ && console.error('WxCustom - onError: ' + that.adUnitId); | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + private unbind() { | ||
| 180 | + if (this.customAd) { | ||
| 181 | + this.customAd.offLoad(this.onLoad); | ||
| 182 | + this.customAd.offClose(this.onClose); | ||
| 183 | + this.customAd.offError(this.onError); | ||
| 184 | + } | ||
| 185 | + } | ||
| 186 | + | ||
| 187 | + /** | ||
| 188 | + * 处队列 | ||
| 189 | + */ | ||
| 190 | + handleQueue() { | ||
| 191 | + let that = WxCustom.I; | ||
| 192 | + if (that.queue.length > 0) { | ||
| 193 | + that.isEnd = false; | ||
| 194 | + let fn = that.queue.shift(); | ||
| 195 | + fn(); | ||
| 196 | + } else { | ||
| 197 | + that.isEnd = false; | ||
| 198 | + } | ||
| 199 | + } | ||
| 200 | +} | ||
| 0 | \ No newline at end of file | 201 | \ No newline at end of file |
| @@ -0,0 +1,233 @@ | @@ -0,0 +1,233 @@ | ||
| 1 | +import WxSystem from "./WxSystem"; | ||
| 2 | +import SDKUtils from "../utils/SDKUtils"; | ||
| 3 | +import { BannerError, __LOG__ } from "../base/SDKConst"; | ||
| 4 | +import LogService from "../service/LogService"; | ||
| 5 | +import { DOT_AD_STATUS, DOT_AD_TYPE } from "../base/SDKEnum"; | ||
| 6 | + | ||
| 7 | +/* | ||
| 8 | +* banner | ||
| 9 | +*/ | ||
| 10 | +export default class WxGrid { | ||
| 11 | + private static instance: WxGrid; | ||
| 12 | + static get I(): WxGrid { | ||
| 13 | + return this.instance || (this.instance = new WxGrid(750, 750)); | ||
| 14 | + } | ||
| 15 | + private designWidth: number; | ||
| 16 | + private bannerWidth: number; | ||
| 17 | + private bannerHeight: number; | ||
| 18 | + private bannerScale: number; | ||
| 19 | + private bannerParams: any; | ||
| 20 | + private _isErrored: boolean; | ||
| 21 | + private adUnitId: string; | ||
| 22 | + private gridAd: any; | ||
| 23 | + private resolve: any; | ||
| 24 | + private reject: any; | ||
| 25 | + private AdList: object | ||
| 26 | + | ||
| 27 | + constructor(designWidth: number, bannerWidth: number) { | ||
| 28 | + designWidth = designWidth || 750; | ||
| 29 | + bannerWidth = bannerWidth || 750; | ||
| 30 | + | ||
| 31 | + this.AdList = {} | ||
| 32 | + this.adUnitId = ''; | ||
| 33 | + this._isErrored = false; | ||
| 34 | + this.designWidth = designWidth; | ||
| 35 | + this.bannerScale = WxSystem.I.winWidth / this.designWidth; | ||
| 36 | + this.bannerWidth = Math.max(this.bannerScale * bannerWidth, 300); | ||
| 37 | + this.bannerHeight = WxSystem.I.winHeight / this.bannerScale; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + get isErrored() { | ||
| 41 | + return this._isErrored; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + create(key: string, adUnitId: string, opts?: { gridCount?: number; bannerWidth?: number, offsetY: number; adIntervals?: number }) { | ||
| 45 | + this.bannerParams = opts || {}; | ||
| 46 | + this.AdList[key] = { | ||
| 47 | + ...opts, | ||
| 48 | + adUnitId, | ||
| 49 | + key, | ||
| 50 | + isEnd: false, | ||
| 51 | + queue: [] | ||
| 52 | + } | ||
| 53 | + if (opts && opts.bannerWidth) { | ||
| 54 | + this.bannerWidth = opts.bannerWidth; | ||
| 55 | + } | ||
| 56 | + this.bannerParams.type = this.bannerParams.type || 1; | ||
| 57 | + this.bannerParams.gridCount = this.bannerParams.gridCount || 5; | ||
| 58 | + this.bannerParams.offsetY = -this.bannerParams.offsetY || 0; | ||
| 59 | + this.bannerParams.adIntervals = this.bannerParams.adIntervals || 60; | ||
| 60 | + return new Promise((resolve, reject) => { | ||
| 61 | + this.resolve = resolve; | ||
| 62 | + this.reject = reject; | ||
| 63 | + this._isErrored = false; | ||
| 64 | + this.adUnitId = adUnitId; | ||
| 65 | + | ||
| 66 | + if (SDKUtils.isEmpty(adUnitId)) | ||
| 67 | + return this.reject({ ...BannerError.BannerInvalid, adUnitId: this.adUnitId }); | ||
| 68 | + | ||
| 69 | + // 设置样式(hack:修复qq版本) | ||
| 70 | + this.bannerParams.type === 2 && (this.bannerWidth = WxSystem.I.winWidth); | ||
| 71 | + let style = { top: 0, left: (WxSystem.I.winWidth - this.bannerWidth) / 2, width: this.bannerWidth }; | ||
| 72 | + style = { | ||
| 73 | + ...style, | ||
| 74 | + top: 0 + this.bannerParams.offsetY | ||
| 75 | + }; | ||
| 76 | + if (this.AdList[key].gridAd) { | ||
| 77 | + if (this.AdList[key].isOff) { | ||
| 78 | + this.show(key) | ||
| 79 | + } | ||
| 80 | + resolve() | ||
| 81 | + return | ||
| 82 | + } | ||
| 83 | + // 创建并判断是否存在 | ||
| 84 | + this.gridAd = wx.createGridAd({ adUnitId, style: { left: style.left, top: this.bannerHeight }, gridCount: this.bannerParams.gridCount, }); | ||
| 85 | + LogService.I.adStat('grid', adUnitId, DOT_AD_TYPE.grid, DOT_AD_STATUS.request) | ||
| 86 | + if (!this.gridAd) | ||
| 87 | + return this.reject({ ...BannerError.BannerNotOpen, adUnitId: this.adUnitId }); | ||
| 88 | + this.AdList[key].gridAd = this.gridAd; | ||
| 89 | + this.gridAd.onLoad(this.onLoad); | ||
| 90 | + this.gridAd.onError(this.onError); | ||
| 91 | + this.gridAd.onResize(this.onResize); | ||
| 92 | + if (!this.AdList[key].isOff) { | ||
| 93 | + this.show(key); | ||
| 94 | + } | ||
| 95 | + }); | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + show(key: string): boolean { | ||
| 99 | + if (this.AdList[key].gridAd) { | ||
| 100 | + if (this.AdList[key].isEnd) { | ||
| 101 | + let fun = (key) => { | ||
| 102 | + WxGrid.I.show(key) | ||
| 103 | + } | ||
| 104 | + this.AdList[key].queue.push(fun); | ||
| 105 | + return | ||
| 106 | + } | ||
| 107 | + this.AdList[key].isEnd = true; | ||
| 108 | + | ||
| 109 | + if (this.AdList[key].gridAd.style.realHeight) | ||
| 110 | + this.AdList[key].gridAd.style.top = WxSystem.I.winHeight - this.AdList[key].gridAd.style.realHeight + this.AdList[key].offsetY; | ||
| 111 | + if (this.bannerParams.type === 2) { | ||
| 112 | + if (this.AdList[key].gridAd.style.width) | ||
| 113 | + this.AdList[key].gridAd.style.left = (WxSystem.I.winWidth - this.AdList[key].gridAd.style.width) / 2; | ||
| 114 | + else | ||
| 115 | + this.AdList[key].gridAd.style.left = (WxSystem.I.winWidth - this.bannerWidth) / 2; | ||
| 116 | + } else | ||
| 117 | + this.AdList[key].gridAd.style.left = (WxSystem.I.winWidth - this.bannerWidth) / 2; | ||
| 118 | + | ||
| 119 | + LogService.I.adStat('grid', this.AdList[key].adUnitId, DOT_AD_TYPE.grid, DOT_AD_STATUS.show) | ||
| 120 | + this.AdList[key].gridAd.show().catch((err: any) => this.handleShowError(err)); | ||
| 121 | + this.onResize(); | ||
| 122 | + __LOG__ && console.error('WxGrid - show: ' + this.adUnitId, this.AdList[key].gridAd.style); | ||
| 123 | + WxGrid.I.handleQueue(key) | ||
| 124 | + return true; | ||
| 125 | + } | ||
| 126 | + WxGrid.I.handleQueue(key) | ||
| 127 | + return false; | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + hide(key: string) { | ||
| 131 | + if (this.AdList[key].gridAd) { | ||
| 132 | + if (this.AdList[key].isEnd) { | ||
| 133 | + let fun = (key) => { | ||
| 134 | + WxGrid.I.hide(key) | ||
| 135 | + } | ||
| 136 | + this.AdList[key].queue.push(fun); | ||
| 137 | + return | ||
| 138 | + } | ||
| 139 | + this.AdList[key].isEnd = true; | ||
| 140 | + this.AdList[key].gridAd.style.left = -9999; | ||
| 141 | + this.AdList[key].gridAd.hide(); | ||
| 142 | + LogService.I.adStat('grid', this.AdList[key].adUnitId, DOT_AD_TYPE.grid, DOT_AD_STATUS.interrupt); | ||
| 143 | + __LOG__ && console.error('WxGrid - hide: ' + this.adUnitId); | ||
| 144 | + | ||
| 145 | + WxGrid.I.handleQueue(key) | ||
| 146 | + } | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + destory(key: string) { | ||
| 150 | + if (this.AdList[key] && this.AdList[key].gridAd) { | ||
| 151 | + this.AdList[key].gridAd.style.left = -9999; | ||
| 152 | + this.AdList[key].gridAd.destroy(); | ||
| 153 | + this.AdList[key].gridAd = null; | ||
| 154 | + LogService.I.adStat('grid', this.AdList[key].adUnitId, DOT_AD_TYPE.grid, DOT_AD_STATUS.interrupt) | ||
| 155 | + __LOG__ && console.error('WxGrid - destory: ' + this.AdList[key].adUnitId); | ||
| 156 | + } | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + private onLoad() { | ||
| 160 | + let that = WxGrid.I; | ||
| 161 | + let gridAd = that.gridAd; | ||
| 162 | + if (!gridAd) return; | ||
| 163 | + // Platform.IsQQ && that.show(); | ||
| 164 | + __LOG__ && console.error('WxGrid - onLoad: ' + that.adUnitId); | ||
| 165 | + if (gridAd.style.realHeight) { | ||
| 166 | + gridAd.style.top = WxSystem.I.winHeight - gridAd.style.realHeight + that.bannerParams.offsetY; | ||
| 167 | + gridAd.style.left = (WxSystem.I.winWidth - gridAd.style.realWidth) / 2; | ||
| 168 | + } | ||
| 169 | + | ||
| 170 | + that.unbind(); | ||
| 171 | + that.resolve && that.resolve({ | ||
| 172 | + adUnitId: that.adUnitId, | ||
| 173 | + scale: that.bannerScale, | ||
| 174 | + width: gridAd.style.realWidth / that.bannerScale, | ||
| 175 | + height: gridAd.style.realHeight / that.bannerScale | ||
| 176 | + }); | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + private onError(err: any) { | ||
| 180 | + __LOG__ && console.error('WxGrid - onError', err); | ||
| 181 | + let that = WxGrid.I; | ||
| 182 | + !that._isErrored && that.handleError(err, { ...BannerError.BannerFail, adUnitId: that.adUnitId }); | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + private onResize() { | ||
| 186 | + let that = WxGrid.I; | ||
| 187 | + let gridAd = that.gridAd; | ||
| 188 | + if (!gridAd) return; | ||
| 189 | + gridAd.style.top = WxSystem.I.winHeight - gridAd.style.realHeight + that.bannerParams.offsetY; | ||
| 190 | + gridAd.style.left = (WxSystem.I.winWidth - gridAd.style.realWidth) / 2; | ||
| 191 | + } | ||
| 192 | + | ||
| 193 | + private handleShowError(ret: { errCode: number; errMsg: string }) { | ||
| 194 | + __LOG__ && console.error('WxGrid - handleShowError', ret); | ||
| 195 | + let that = WxGrid.I; | ||
| 196 | + let { errCode, errMsg } = ret; | ||
| 197 | + !that.isErrored && that.handleError(ret, { code: errCode, msg: errMsg }); | ||
| 198 | + } | ||
| 199 | + | ||
| 200 | + private handleError(ret: any, err: any) { | ||
| 201 | + let that = WxGrid.I; | ||
| 202 | + that.unbind(); | ||
| 203 | + if (that.gridAd) { | ||
| 204 | + that.gridAd.destroy() | ||
| 205 | + } | ||
| 206 | + that.gridAd = null; | ||
| 207 | + that._isErrored = true; | ||
| 208 | + that.reject && that.reject({ ...err, adUnitId: that.adUnitId }); | ||
| 209 | + LogService.I.adStat('grid', that.adUnitId, DOT_AD_TYPE.grid, DOT_AD_STATUS.fail) | ||
| 210 | + __LOG__ && console.error('WxGrid - onError: ' + that.adUnitId); | ||
| 211 | + } | ||
| 212 | + | ||
| 213 | + private unbind() { | ||
| 214 | + if (this.gridAd) { | ||
| 215 | + this.gridAd.offLoad(this.onLoad); | ||
| 216 | + this.gridAd.offError(this.onError); | ||
| 217 | + this.gridAd.offResize(this.onResize); | ||
| 218 | + } | ||
| 219 | + } | ||
| 220 | + /** | ||
| 221 | + * 处队列 | ||
| 222 | + */ | ||
| 223 | + handleQueue(key) { | ||
| 224 | + let that = WxGrid.I; | ||
| 225 | + if (that.AdList[key].queue.length > 0) { | ||
| 226 | + that.AdList[key].isEnd = false; | ||
| 227 | + let fn = that.AdList[key].queue.shift(); | ||
| 228 | + fn(); | ||
| 229 | + } else { | ||
| 230 | + that.AdList[key].isEnd = false; | ||
| 231 | + } | ||
| 232 | + } | ||
| 233 | +} | ||
| 0 | \ No newline at end of file | 234 | \ No newline at end of file |
| @@ -0,0 +1,155 @@ | @@ -0,0 +1,155 @@ | ||
| 1 | +import WxApi from './WxApi'; | ||
| 2 | +import WxSystem from './WxSystem'; | ||
| 3 | +import WxLaunch from './WxLaunch'; | ||
| 4 | +import { __LOG__, GAMEDATA } from '../base/SDKConst'; | ||
| 5 | +import DataService from '../service/DataService'; | ||
| 6 | +import SDKUtils from '../utils/SDKUtils'; | ||
| 7 | +import SDKEventCenter from '../base/SDKEventCenter'; | ||
| 8 | +import { SDKEventEnum } from '../base/SDKEventEnum'; | ||
| 9 | +import { NetworkType, DOT_SHARE_TYPE } from '../base/SDKEnum'; | ||
| 10 | +import DateUtils from '../utils/DateUtils'; | ||
| 11 | +import LogService from '../service/LogService'; | ||
| 12 | + | ||
| 13 | +export default class WxInit { | ||
| 14 | + private onlineTime: number; | ||
| 15 | + | ||
| 16 | + private constructor() { | ||
| 17 | + this.onlineTime = DateUtils.nowTime; | ||
| 18 | + wx.onShow(this.onShow.bind(this)); | ||
| 19 | + wx.onHide(this.onHide.bind(this)); | ||
| 20 | + wx.onNetworkStatusChange(this.onNetworkStatusChange.bind(this)); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + init() { | ||
| 24 | + let { platform } = WxSystem.I.SystemData; | ||
| 25 | + let launchData = WxLaunch.I.LaunchData; | ||
| 26 | + console.log("启动信息", launchData) | ||
| 27 | + let { query, scene, referrerInfo } = launchData; | ||
| 28 | + let { | ||
| 29 | + invite_type, | ||
| 30 | + channelId, | ||
| 31 | + channel_id, | ||
| 32 | + td_channelid, | ||
| 33 | + channel, | ||
| 34 | + share_id, | ||
| 35 | + share_key, | ||
| 36 | + user_invite_uid, | ||
| 37 | + weixinadinfo, | ||
| 38 | + account_id, | ||
| 39 | + service_id, | ||
| 40 | + shareMessageToFriendScene | ||
| 41 | + } = query; | ||
| 42 | + | ||
| 43 | + let fromChannel = channelId || channel_id || channel || td_channelid; | ||
| 44 | + // 设置微信转化跟踪:第一项为来源广告的广告id,替换来源渠道id | ||
| 45 | + if (weixinadinfo) {//!fromChannel && | ||
| 46 | + let infoArr = weixinadinfo.split('.'); | ||
| 47 | + if (infoArr && infoArr.length) | ||
| 48 | + fromChannel = infoArr[0]; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + //定向分享统计 | ||
| 52 | + if (shareMessageToFriendScene && shareMessageToFriendScene >= 0 && shareMessageToFriendScene <= 50) { | ||
| 53 | + share_key = GAMEDATA.shareMessageToFriend.sharekey; | ||
| 54 | + share_id = GAMEDATA.shareMessageToFriend.share_id; | ||
| 55 | + } | ||
| 56 | + // 计算最终渠道ID | ||
| 57 | + // let lchannelId: number; | ||
| 58 | + // let { IntegralChannelId, ChannelId } = CfgManager.I.config; | ||
| 59 | + // if (fromChannel) | ||
| 60 | + // lchannelId = fromChannel; | ||
| 61 | + // else if (scene === SceneCode.WX_INTEGRAL && IntegralChannelId) | ||
| 62 | + // lchannelId = IntegralChannelId; | ||
| 63 | + // else | ||
| 64 | + // lchannelId = ChannelId; | ||
| 65 | + | ||
| 66 | + __LOG__ && console.warn(`====> PCSDK wxinit query user_invite_uid:${user_invite_uid}; fromChannel:${fromChannel}; channelId:${fromChannel}; scene:${scene}; share_id:${share_id}; share_key:${share_key};`); | ||
| 67 | + | ||
| 68 | + // 设置sdk 数据的信息 | ||
| 69 | + DataService.I | ||
| 70 | + .setScene(scene) | ||
| 71 | + .setReferrerInfo(referrerInfo || {}) | ||
| 72 | + .setShareId(share_id) | ||
| 73 | + .setPlatform(platform) | ||
| 74 | + .setShareKey(share_key) | ||
| 75 | + .setChannelId(fromChannel) | ||
| 76 | + .setQueryChannelId(fromChannel || GAMEDATA.channel_id) | ||
| 77 | + .setInviteType(invite_type) | ||
| 78 | + .setQueryUserInviteUid(user_invite_uid || 0) | ||
| 79 | + .setQueryExtData({ account_id, service_id }) | ||
| 80 | + .setSystemId() | ||
| 81 | + .setLaunchSource(); | ||
| 82 | + // 设置网络类型 | ||
| 83 | + WxApi.I.getNetworkType() | ||
| 84 | + .then((ret: _NetworkTypeSuccessObject) => this.setNetworkType(ret)) | ||
| 85 | + .catch((err: _NetworkTypeSuccessObject) => this.setNetworkType(err)); | ||
| 86 | + | ||
| 87 | + // 从会话过来,且没有渠道id,打点 | ||
| 88 | + // let shareScenes = [SceneCode.WX_SHARE_FRIEND, SceneCode.WX_SHARE_GROUP, SceneCode.WX_SHARE_TICKET]; | ||
| 89 | + // if (!fromChannel && shareScenes.indexOf(scene) >= 0) { | ||
| 90 | + // LogService.I.dot(SDKDotType.Share); | ||
| 91 | + // } | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + private onShow(opts: any) { | ||
| 95 | + console.log("onshow_opts", opts) | ||
| 96 | + let nowTime = DateUtils.nowTime; | ||
| 97 | + let { scene, shareTicket } = opts; | ||
| 98 | + this.onlineTime = nowTime; | ||
| 99 | + // 设置sdk 数据的信息 | ||
| 100 | + DataService.I | ||
| 101 | + .setScene(scene) | ||
| 102 | + .setLaunchTime(nowTime) | ||
| 103 | + .setLaunchSource(); | ||
| 104 | + | ||
| 105 | + if (opts && opts.query) { | ||
| 106 | + let { share_key, share_id, invite_type, user_invite_uid } = opts.query; | ||
| 107 | + // DebugUtils.I.dynamic('====> PCSDK onShow 参数:', JSON.stringify(opts)); | ||
| 108 | + // DebugUtils.I.dynamic(`====> PCSDK onShow scene:${scene}; share_id:${share_id}; share_key:${share_key};`); | ||
| 109 | + !SDKUtils.isUndefined(share_id) && DataService.I.setShareId(share_id); | ||
| 110 | + !SDKUtils.isUndefined(share_key) && DataService.I.setShareKey(share_key); | ||
| 111 | + !SDKUtils.isUndefined(invite_type) && DataService.I.setInviteType(invite_type); | ||
| 112 | + !SDKUtils.isUndefined(shareTicket) && DataService.I.setShareTicket(shareTicket); | ||
| 113 | + !SDKUtils.isUndefined(user_invite_uid) && DataService.I.setQueryUserInviteUid(user_invite_uid); | ||
| 114 | + if (share_id && share_key) { | ||
| 115 | + LogService.I.share(share_key, share_id, DOT_SHARE_TYPE.click); | ||
| 116 | + } | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + // 抛出onShow事件 | ||
| 120 | + SDKEventCenter.I.emit(SDKEventEnum.APP_SHOW, opts); | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + private onHide() { | ||
| 124 | + // 清除banner定时器 | ||
| 125 | + // Platform.I.banner && Platform.I.banner.clear(); | ||
| 126 | + | ||
| 127 | + SDKEventCenter.I.emit(SDKEventEnum.APP_HIDE); | ||
| 128 | + // 曝光同步 | ||
| 129 | + LogService.I.bannerExposure(); | ||
| 130 | + // // 资源变更同步 | ||
| 131 | + // LogService.I.syncLogRes(); | ||
| 132 | + // // 关卡同步 | ||
| 133 | + // LogService.I.syncLogLevel(); | ||
| 134 | + // 同步logout:在线时长同步 | ||
| 135 | + // console.error("this.onlineTime", this.onlineTime) | ||
| 136 | + if (this.onlineTime) { | ||
| 137 | + LogService.I.logOut(DateUtils.nowTime - this.onlineTime); | ||
| 138 | + this.onlineTime = 0; | ||
| 139 | + } | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + private onNetworkStatusChange(res: { isConnected: boolean; networkType: NetworkType }) { | ||
| 143 | + console.warn('网络变化了:', res); | ||
| 144 | + this.setNetworkType(res); | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + private setNetworkType(res: any) { | ||
| 148 | + res && res.networkType && DataService.I.setNetworkType(res.networkType as NetworkType); | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + private static instance: WxInit; | ||
| 152 | + static get I(): WxInit { | ||
| 153 | + return this.instance || (this.instance = new WxInit); | ||
| 154 | + } | ||
| 155 | +} | ||
| 0 | \ No newline at end of file | 156 | \ No newline at end of file |
| @@ -0,0 +1,49 @@ | @@ -0,0 +1,49 @@ | ||
| 1 | +import { GAMEDATA } from "../base/SDKConst"; | ||
| 2 | +import { DOT_AD_STATUS, DOT_AD_TYPE } from "../base/SDKEnum"; | ||
| 3 | +import LogService from "../service/LogService"; | ||
| 4 | +import WxApi from "./WxApi"; | ||
| 5 | + | ||
| 6 | +export default class WxInterstitial { | ||
| 7 | + /** 插屏广告ID */ | ||
| 8 | + private static interstitialAdId: string = GAMEDATA.interstitialAdId; | ||
| 9 | + /** 插屏广告实例 */ | ||
| 10 | + public static interstitialAd: any; | ||
| 11 | + /** 插屏广告是否加载完成 */ | ||
| 12 | + private static interstitialSuccell: boolean = false; | ||
| 13 | + /** 初始化插屏广告 */ | ||
| 14 | + public static initInterstitialAd(adUnitId: string = GAMEDATA.interstitialAdId) { | ||
| 15 | + this.interstitialAdId = adUnitId; | ||
| 16 | + this.createInterstitial(); | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + /** 显示插屏广告 */ | ||
| 20 | + public static showInterstitialAd(adUnitId: string = GAMEDATA.interstitialAdId) { | ||
| 21 | + this.interstitialAdId = adUnitId; | ||
| 22 | + if (!this.interstitialSuccell) { | ||
| 23 | + this.createInterstitial(true); | ||
| 24 | + return; | ||
| 25 | + } | ||
| 26 | + this.interstitialAd.show(); | ||
| 27 | + setTimeout(() => { | ||
| 28 | + this.createInterstitial(false); | ||
| 29 | + }, 5000); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + /** 创建插屏广告 */ | ||
| 33 | + private static createInterstitial(isShow: boolean = false) { | ||
| 34 | + LogService.I.adStat('InterstitialAd', this.interstitialAdId, DOT_AD_TYPE.interstitial, DOT_AD_STATUS.request) | ||
| 35 | + this.interstitialAd = WxApi.I.createInterstitialAd(this.interstitialAdId); | ||
| 36 | + this.interstitialAd.onLoad(() => { | ||
| 37 | + WxInterstitial.interstitialSuccell = true; | ||
| 38 | + LogService.I.adStat('InterstitialAd', this.interstitialAdId, DOT_AD_TYPE.interstitial, DOT_AD_STATUS.rt) | ||
| 39 | + if (isShow) { | ||
| 40 | + LogService.I.adStat('InterstitialAd', this.interstitialAdId, DOT_AD_TYPE.interstitial, DOT_AD_STATUS.show) | ||
| 41 | + this.interstitialAd.show() | ||
| 42 | + } | ||
| 43 | + }) | ||
| 44 | + this.interstitialAd.onError(err => { | ||
| 45 | + LogService.I.adStat('InterstitialAd', this.interstitialAdId, DOT_AD_TYPE.interstitial, DOT_AD_STATUS.fail) | ||
| 46 | + WxInterstitial.interstitialSuccell = false; | ||
| 47 | + }) | ||
| 48 | + } | ||
| 49 | +} | ||
| 0 | \ No newline at end of file | 50 | \ No newline at end of file |
| @@ -0,0 +1,32 @@ | @@ -0,0 +1,32 @@ | ||
| 1 | +export default class WxLaunch { | ||
| 2 | + private static instance: WxLaunch; | ||
| 3 | + private data: LaunchInfoSyncReturnValue; | ||
| 4 | + | ||
| 5 | + private constructor() { | ||
| 6 | + this.data = wx.getLaunchOptionsSync(); | ||
| 7 | + } | ||
| 8 | + | ||
| 9 | + get LaunchData() { | ||
| 10 | + return this.data; | ||
| 11 | + } | ||
| 12 | + | ||
| 13 | + get query() { | ||
| 14 | + return this.data.query; | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + get scene() { | ||
| 18 | + return this.data.scene; | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + get shareTicket() { | ||
| 22 | + return this.data.shareTicket; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + get referrerInfo() { | ||
| 26 | + return this.data.referrerInfo; | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + static get I(): WxLaunch { | ||
| 30 | + return this.instance || (this.instance = new WxLaunch()); | ||
| 31 | + } | ||
| 32 | +} | ||
| 0 | \ No newline at end of file | 33 | \ No newline at end of file |
| @@ -0,0 +1,183 @@ | @@ -0,0 +1,183 @@ | ||
| 1 | +import WxApi from "./WxApi"; | ||
| 2 | +import DataService from "../service/DataService"; | ||
| 3 | +import { ErrorCode } from "../base/SDKConst"; | ||
| 4 | +import { SDKApi } from "../http/SDKApi"; | ||
| 5 | +import WxSystem from "./WxSystem"; | ||
| 6 | +import DateUtils from "../utils/DateUtils"; | ||
| 7 | +import LogService from "../service/LogService"; | ||
| 8 | +import { DOT_SHARE_TYPE } from "../base/SDKEnum"; | ||
| 9 | +import ShareVideoService from "../service/ShareVideoService"; | ||
| 10 | +import OnlineService from "../service/OnlineService"; | ||
| 11 | + | ||
| 12 | +export default class WxLogin { | ||
| 13 | + /** | ||
| 14 | + * 发起登录 | ||
| 15 | + * @param isAuthorize Boolean 是否重新登录 | ||
| 16 | + * tip1:如果是重新登录,需要把之前的token清空 | ||
| 17 | + * tip2:必须先调用wxLogin才能使用getUserInfo | ||
| 18 | + */ | ||
| 19 | + async login(isAuthorize: boolean): Promise<any> { | ||
| 20 | + return new Promise(async (resolve, reject) => { | ||
| 21 | + let code = await WxApi.I.login(); | ||
| 22 | + if (isAuthorize) { | ||
| 23 | + WxApi.I.getUserinfo() | ||
| 24 | + .then((ret: any) => this.authedlogin(ret, code, resolve, reject)) | ||
| 25 | + .catch((err: any) => this.weakLogin(err, code, resolve, reject)); | ||
| 26 | + } else { | ||
| 27 | + this.weakLogin({ errCode: 1, msg: '默认未授权登录' }, code, resolve, reject) | ||
| 28 | + } | ||
| 29 | + }); | ||
| 30 | + } | ||
| 31 | + private isFirst: boolean | ||
| 32 | + | ||
| 33 | + /** | ||
| 34 | + * 授权后登录登陆 | ||
| 35 | + * @param ret | ||
| 36 | + * @param resolve | ||
| 37 | + * @param reject | ||
| 38 | + */ | ||
| 39 | + private async authedlogin(ret: _GetUserInfoSuccessObject, code: string, resolve: any, reject: any) {//_GetUserInfoSuccessObject | ||
| 40 | + let { rawData, iv, encryptedData, signature, userInfo } = ret; | ||
| 41 | + let params = { | ||
| 42 | + ...this.buildParams(), | ||
| 43 | + code, | ||
| 44 | + signature, | ||
| 45 | + iv: iv,//encodeURIComponent(), | ||
| 46 | + // raw_data: encodeURIComponent(Base64.encode(rawData)), | ||
| 47 | + encryptedData: encryptedData//encodeURIComponent(encryptedData) | ||
| 48 | + }; | ||
| 49 | + // 设置性别 | ||
| 50 | + userInfo && DataService.I.setGender(userInfo.gender); | ||
| 51 | + SDKApi.Login(params) | ||
| 52 | + .then((data: any) => { | ||
| 53 | + // console.log("登录", data) | ||
| 54 | + this.handleLogin(data, resolve, true); | ||
| 55 | + resolve(data); | ||
| 56 | + }) | ||
| 57 | + .catch((err: any) => reject(err)); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + /** | ||
| 61 | + * 弱登录 | ||
| 62 | + * @param err | ||
| 63 | + * @param resolve | ||
| 64 | + * @param reject | ||
| 65 | + */ | ||
| 66 | + private async weakLogin(err: any, code: string, resolve: any, reject: any) { | ||
| 67 | + // console.log("弱登录") | ||
| 68 | + // 重新登录不需要继续往下走 | ||
| 69 | + if (!err || err.errCode === ErrorCode.InvalidLogin.code) | ||
| 70 | + return; | ||
| 71 | + | ||
| 72 | + // 传递来源appid | ||
| 73 | + let refererInfo = DataService.I.ReferrerInfo; | ||
| 74 | + let params = { | ||
| 75 | + ...this.buildParams(), | ||
| 76 | + // refAppId: refererInfo.appId || 0, | ||
| 77 | + code | ||
| 78 | + }; | ||
| 79 | + | ||
| 80 | + SDKApi.weakLogin(params) | ||
| 81 | + .then((data: any) => { | ||
| 82 | + this.handleLogin(data, resolve, false); | ||
| 83 | + }) | ||
| 84 | + .catch((err: any) => reject(err)); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + /** | ||
| 88 | + * 处理登录/弱登录请求结果 | ||
| 89 | + * @param data | ||
| 90 | + * @param isAuthorize 是否授权:true=已授权 false=没有授权成功 | ||
| 91 | + */ | ||
| 92 | + private handleLogin(data: any, resolve: any, isAuthorize: boolean) { | ||
| 93 | + // console.log("登录请求结果data", data) | ||
| 94 | + if (data) { | ||
| 95 | + // 设置登录信息 | ||
| 96 | + if (data.data) { | ||
| 97 | + let { channel, uid, firstlogin, token, reftoken, openid, expire, isnew, gameconfig } = data.data; | ||
| 98 | + LogService.I.setLogind({ | ||
| 99 | + channel, | ||
| 100 | + userId: uid, | ||
| 101 | + regTime: firstlogin, | ||
| 102 | + openId: openid, | ||
| 103 | + isnew, | ||
| 104 | + token, | ||
| 105 | + refToken: reftoken, | ||
| 106 | + expire, | ||
| 107 | + }); | ||
| 108 | + OnlineService.I.setData(gameconfig) | ||
| 109 | + LogService.I.active(); | ||
| 110 | + if (!this.isFirst && DataService.I.ShareKey && DataService.I.ShareId) { | ||
| 111 | + this.isFirst = true; | ||
| 112 | + LogService.I.share(DataService.I.ShareKey, DataService.I.ShareId, DOT_SHARE_TYPE.click); | ||
| 113 | + } | ||
| 114 | + this.handleExpire(expire) | ||
| 115 | + ShareVideoService.I.forward() | ||
| 116 | + } | ||
| 117 | + } | ||
| 118 | + resolve(data) | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + /** | ||
| 122 | + * 构建登录/弱登录公用参数 | ||
| 123 | + */ | ||
| 124 | + private buildParams() { | ||
| 125 | + let channel = DataService.I.QueryChannelId + ''; | ||
| 126 | + let brand = WxSystem.I.brand; | ||
| 127 | + let model = WxSystem.I.model; | ||
| 128 | + let version = WxSystem.I.version; | ||
| 129 | + let system = WxSystem.I.system; | ||
| 130 | + let platform = WxSystem.I.platform; | ||
| 131 | + let sdkversion = WxSystem.I.SDKVersion; | ||
| 132 | + let shareuid = +DataService.I.QueryUserInviteUid; | ||
| 133 | + let scene = DataService.I.Scene + ''; | ||
| 134 | + let sharekey = DataService.I.ShareKey; | ||
| 135 | + let shareid = DataService.I.ShareId + ''; | ||
| 136 | + return { | ||
| 137 | + channel, | ||
| 138 | + brand, | ||
| 139 | + model, | ||
| 140 | + version, | ||
| 141 | + system, | ||
| 142 | + platform, | ||
| 143 | + sdkversion, | ||
| 144 | + shareuid, | ||
| 145 | + scene, | ||
| 146 | + sharekey, | ||
| 147 | + shareid | ||
| 148 | + }; | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + /** | ||
| 152 | + * 刷新token | ||
| 153 | + * @param expire | ||
| 154 | + */ | ||
| 155 | + private countDown | ||
| 156 | + private handleExpire(expire) { | ||
| 157 | + let time = expire - DateUtils.now; | ||
| 158 | + this.countDown && clearTimeout(this.countDown) | ||
| 159 | + this.countDown = setTimeout(this.refreshToken, time * 1000); | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + private refreshToken() { | ||
| 163 | + SDKApi.reftoken({ | ||
| 164 | + channel: DataService.I.ChannelId + '', | ||
| 165 | + uid: DataService.I.UserId, | ||
| 166 | + token: DataService.I.Token, | ||
| 167 | + reftoken: DataService.I.refToken | ||
| 168 | + }).then(res => { | ||
| 169 | + let { token, reftoken, expire } = res.data; | ||
| 170 | + DataService.I.setLoginData({ | ||
| 171 | + token, | ||
| 172 | + refToken: reftoken, | ||
| 173 | + expire, | ||
| 174 | + }); | ||
| 175 | + this.handleExpire(expire) | ||
| 176 | + }) | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + private static _instance: WxLogin; | ||
| 180 | + static get I(): WxLogin { | ||
| 181 | + return this._instance || (this._instance = new WxLogin); | ||
| 182 | + } | ||
| 183 | +} | ||
| 0 | \ No newline at end of file | 184 | \ No newline at end of file |
| @@ -0,0 +1,100 @@ | @@ -0,0 +1,100 @@ | ||
| 1 | +import { GAMEDATA } from '../base/SDKConst'; | ||
| 2 | +import { EnvCode } from '../base/SDKEnum'; | ||
| 3 | +import { SDKApi } from '../http/SDKApi'; | ||
| 4 | +import DataService from '../service/DataService'; | ||
| 5 | +import WxApi from './WxApi'; | ||
| 6 | +import WxSystem from './WxSystem'; | ||
| 7 | + | ||
| 8 | +export default class WxPay { | ||
| 9 | + /** | ||
| 10 | + * 发起支付 | ||
| 11 | + */ | ||
| 12 | + pay(params: { money: number; source:string}, opts: any = {}): Promise<any> { | ||
| 13 | + return new Promise((resolve, reject) => { | ||
| 14 | + let { Mode, OfferId, ZoneId, CurrencyType, Platform } = GAMEDATA.MidasPay; | ||
| 15 | + // 环境配置,0:米大师正式环境 1:米大师沙箱环境 | ||
| 16 | + let env =1;// DataService.I.EnvEnum === EnvCode.Prod ? 0 : 1; | ||
| 17 | + // 平台类型,android or ios,config.js有配置走配置,否则判断系统id | ||
| 18 | + let platform = Platform || (DataService.I.SystemId === 0 ? 'ios' : 'android'); | ||
| 19 | + let pms = { | ||
| 20 | + mode: Mode, | ||
| 21 | + env, | ||
| 22 | + platform, | ||
| 23 | + offerId: OfferId, | ||
| 24 | + currencyType: CurrencyType, | ||
| 25 | + buyQuantity: params.money * 10, | ||
| 26 | + zoneId: ZoneId | ||
| 27 | + }; | ||
| 28 | + console.log(pms); | ||
| 29 | + WxApi.I.requestMidasPayment(pms) | ||
| 30 | + .then(() => this.handlePaySuccess({ ...params, platform }, opts, resolve, reject)) | ||
| 31 | + .catch(err => this.handlePayError(err, reject)); | ||
| 32 | + }); | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + private async handlePaySuccess(data: { money: number;source:string, platform: string }, opts: any, resolve: any, reject: any) { | ||
| 36 | + // let code = await WxApi.I.login(); | ||
| 37 | + | ||
| 38 | + let extend =""; | ||
| 39 | + for(let key in opts){ | ||
| 40 | + extend +=(`${key}=${opts[key]}&`) | ||
| 41 | + } | ||
| 42 | + extend = extend.substring(0,extend.length-1); | ||
| 43 | + extend = encodeURIComponent(extend); | ||
| 44 | + let params = { | ||
| 45 | + ...this.buildParams(), | ||
| 46 | + ...opts, | ||
| 47 | + extend, | ||
| 48 | + source:data.source, | ||
| 49 | + type:1, | ||
| 50 | + amount: data.money * 100, | ||
| 51 | + platform: data.platform | ||
| 52 | + }; | ||
| 53 | + SDKApi.pay(params) | ||
| 54 | + .then(ret => resolve(ret)) | ||
| 55 | + .catch(err => this.handlePayError(err, reject)); | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + private handlePayError(err: any, reject: any) { | ||
| 59 | + console.log("支付失败",JSON.stringify(err)); | ||
| 60 | + reject(err); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + /** | ||
| 64 | + * 构建支付公用参数 | ||
| 65 | + */ | ||
| 66 | + private buildParams() { | ||
| 67 | + let APIVersion = '0.6.0';//固定值 | ||
| 68 | + let gameid = GAMEDATA.game_id; | ||
| 69 | + let channel = DataService.I.ChannelId; | ||
| 70 | + let openid = DataService.I.OpenId; | ||
| 71 | + let brand = WxSystem.I.brand; | ||
| 72 | + let model = WxSystem.I.model; | ||
| 73 | + let version = WxSystem.I.version; | ||
| 74 | + let system = WxSystem.I.system; | ||
| 75 | + let sdkversion = WxSystem.I.SDKVersion; | ||
| 76 | + let scene = DataService.I.Scene + ''; | ||
| 77 | + let uid = DataService.I.UserId; | ||
| 78 | + let token = DataService.I.Token; | ||
| 79 | + let env = DataService.I.EnvEnum === 1 ? 'pre' : 'prod'; | ||
| 80 | + return { | ||
| 81 | + gameid, | ||
| 82 | + openid, | ||
| 83 | + channel, | ||
| 84 | + brand, | ||
| 85 | + model, | ||
| 86 | + version, | ||
| 87 | + system, | ||
| 88 | + sdkversion, | ||
| 89 | + scene, | ||
| 90 | + uid, | ||
| 91 | + token, | ||
| 92 | + env | ||
| 93 | + }; | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + private static _instance: WxPay; | ||
| 97 | + static get I(): WxPay { | ||
| 98 | + return this._instance || (this._instance = new WxPay); | ||
| 99 | + } | ||
| 100 | +} | ||
| 0 | \ No newline at end of file | 101 | \ No newline at end of file |
| @@ -0,0 +1,59 @@ | @@ -0,0 +1,59 @@ | ||
| 1 | +export default class WxSystem { | ||
| 2 | + private static instance: WxSystem; | ||
| 3 | + private data: SystemInfoSyncReturnValue; | ||
| 4 | + | ||
| 5 | + private constructor() { | ||
| 6 | + this.data = wx.getSystemInfoSync(); | ||
| 7 | + } | ||
| 8 | + | ||
| 9 | + get SystemData() { | ||
| 10 | + return this.data; | ||
| 11 | + } | ||
| 12 | + | ||
| 13 | + get version() { | ||
| 14 | + return this.data.version; | ||
| 15 | + } | ||
| 16 | + get system() { | ||
| 17 | + return this.data.system; | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + get platform() { | ||
| 21 | + return this.data.platform; | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + get language() { | ||
| 25 | + return this.data.language; | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + get winWidth() { | ||
| 29 | + return this.data.windowWidth; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + get winHeight() { | ||
| 33 | + return this.data.windowHeight; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + get screenWidth() { | ||
| 37 | + return this.data.screenWidth; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + get screenHeight() { | ||
| 41 | + return this.data.screenHeight; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + get SDKVersion() { | ||
| 45 | + return this.data.SDKVersion; | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + get brand(){ | ||
| 49 | + return this.data.brand; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + get model(){ | ||
| 53 | + return this.data.model; | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + static get I(): WxSystem { | ||
| 57 | + return this.instance || (this.instance = new WxSystem()); | ||
| 58 | + } | ||
| 59 | +} |