SDKTools.ts 9.06 KB
import { GAMEDATA } from "../wxsdk/base/SDKConst";
import DateUtils from "../wxsdk/utils/DateUtils";
import WXSDK from "../wxsdk/WXSDK";

/*
* SDK工具类库;
*/
export class SDKTools {
  static get isWx() {
    return typeof wx !== 'undefined';
  }
  static get isSupported() {
    return typeof WXSDK === 'object';
  }

  static get envNum() {
    return WXSDK.data.envEnum;
  }

  static get data() {
    return WXSDK.data;
  }

  static get scene() {
    return WXSDK.data.scene;
  }
  static get uid() {
    return WXSDK.data.userId;
  }
  static get channel() {
    return WXSDK.data.channelId;
  }
  static get openId() {
    return WXSDK.data.openId;
  }

  static get pon() {
    return WXSDK.data.pon;
  }


  // 是否当天注册用户
  static get isnew() {
    try {
      let t = DateUtils.today;
      return WXSDK.data.regTime === t;
    } catch (error) {
      return false;
    }
  }
  // 是否首次登录
  static get isFirstLogin() {
    try {
      return WXSDK.data.isnew === 1;
    } catch (error) {
      return false;
    }
  }
  // 是否是某个时间点后注册的
  static isAfterTime(t) {
    try {
      let t1 = new Date(t).getTime();
      let t2 = new Date(`${WXSDK.data.regTime} 00:00:00`).getTime();
      // console.log("t1t2", t1, t2)
      return t2 >= t1;
    } catch (error) {
      return false;
    }
  }


  /**
   * banner
   * @param adUnitId bannerID
   * @param opts { //非必填  默认满屏居低
   *  type:默认1 banner 
   *  bannerWidth:
   *  offsetY: 距离底部多远  
   *  isOff:是否关闭默认显示banner
   * }
   */
  static createBanner(adUnitId: string = GAMEDATA.bannerId, opts?: { type?: number; bannerWidth?: number, offsetY?: number; adIntervals?: number, isOff?: boolean }) {
    if (!this.isWx) return
    return WXSDK.ad.createBanner(adUnitId, opts);
  }
  /**
   * banner 显示 ps:创建默认显示
   */
  static showBanner() {
    if (!this.isWx) return
    WXSDK.ad.showBanner()
  }
  /**
   * banner 隐藏
   */
  static hideBanner() {
    if (!this.isWx) return
    WXSDK.ad.hideBanner();
  }
  /**
   * banner 销毁
   */
  static destoryBanner() {
    if (!this.isWx) return
    WXSDK.ad.destoryBanner();
  }


  /**
  *  插屏
  */
  static createInterstitialAd(adUnitId: string = GAMEDATA.interstitialAdId) {
    if (!this.isWx) return Promise.resolve({ code: 1, msg: "暂无广告" })
    return WXSDK.ad.createInterstitialAd(adUnitId);
  }

  /**
   * 原生模板广告  全局只能存在一个  并且创建的位置最好是同一个位置   通过show hide控制   如果要改变位置,调用destory再调用创建(不建议频繁销毁创建,会导致广告拉取不到)
   * @param adUnitId 格子广告ID
   * @param opts { //非必填  默认满屏居低
   *  bannerWidth:
   * offsetY: 距离底部多远  
   * }
   */
  static createCustom(adUnitId: string = GAMEDATA.customId, opts: { top: number; left: number; adIntervals?: number }) {
    if (!this.isWx) return
    return WXSDK.ad.createCustom(adUnitId, opts);
  }
  /**
  * 原生广告 显示 ps:创建默认显示
  */
  static showCustom() {
    if (!this.isWx) return
    WXSDK.ad.showCustom()
  }
  /**
   * 原生广告 隐藏
   */
  static hideCustom() {
    if (!this.isWx) return
    WXSDK.ad.hideCustom()
  }
  /**
   * 原生广告 销毁
   */
  static destoryCustom() {
    if (!this.isWx) return
    WXSDK.ad.destoryCustom()
  }

  /**
   * 
   * @param data 游戏数据
   * @param type 游戏类型
   * @param opts 扩展参数
   */
  static navigateToMiniProgram(data: IGameList, type, opts?) {
    if (!this.isWx) return
    return WXSDK.ad.navigateToMiniProgram(data, type, opts)
  }


  /**
   * 订阅
   * @param template_ids  模板id eg['aaaaaaa','bbbbbbb']
   * @param ids 对应后台的id eg:['1','2']
   */
  static subScribe(template_ids: Array<string>, ids: Array<string>) {
    if (!this.isWx) return
    return WXSDK.game.subScribe(template_ids, ids)
  }

  /**
   * 登录,sdk已实现弱登录和强登录的判断
   */
  static login(isAuthorize?: boolean): Promise<any> {
    return WXSDK.game.login(isAuthorize);
  }
  /**
  * 保存数据
  * @param key
  * @param value
  */
  static saveData(key: string, value: string) {
    return WXSDK.game.saveData(key, value)
  }
  /**
   * 获取数据
   * @param key
   */
  static getData(key: string) {
    return WXSDK.game.getData(key)
  }

  /**
   * 添加排行榜 2021-5-17 废弃新游戏勿用
   * 
   * @param key 获取排行时掉对应key即可获取对应的排行
   * @param value 值
   */
  static rankAdd(key: string, value: number) {
    return WXSDK.game.rankAdd(key, value)
  }
  /**
  * 添加排行榜扩展数据
   * @param value 对象 扩展字段 段位 等级等等...
  */
  static updateRankData(value: Object = {}) {
    let val = JSON.stringify(value)
    return WXSDK.game.saveData('rankData', val)
  }
  /**
   * 排行榜数据 废弃新游戏勿用
   * type:1 日榜 2周榜 3月榜
   * isRankData: 是否有扩展参数
   */
  static rankList(key: string, type: number = 1, isRankData: boolean = false): Promise<IResult<any>> {
    if (!this.isWx) return Promise.resolve({ code: -1 })
    return WXSDK.game.rankList(key, type, isRankData);
  }
  /**
   * 添加排行榜 2021-5-17 新增
   * 
   * @param key 获取排行时掉对应key即可获取对应的排行
   * @param value 值
   * @param type 0永久排行1日排行2周排行3月排行,默认为0
   * @param sort 1 大到小 2小到大
   */
  static totalrankAdd(key: string, value: number, type: number = 0, sort: number = 1) {
    return WXSDK.game.totalrankAdd(key, value, type, sort);
  }
  /**
   * 排行榜数据 
   * type:0永久榜单 1 日榜 2周榜 3月榜
   * isRankData: 是否有扩展参数
   * @param sort 1 大到小 2小到大
   */
  static totalrankList(key: string, type: number = 0, isRankData: boolean = false, sort: number = 1): Promise<IResult<any>> {
    if (!this.isWx) return Promise.resolve({ code: -1 });
    return WXSDK.game.totalrankList(key, type, isRankData, sort);
  }



  /**
   * 互动数据解密
   * @param iv 
   * @param encryptedData 
   * @returns 
   */
  static wxencrypted(iv, encryptedData): Promise<IResult<any>> {
    if (!this.isWx) return Promise.resolve({ code: -1 })
    return WXSDK.game.wxencrypted(iv, encryptedData);
  }
  /**
   * 互导数据
   * 1抽屉广告2猜你喜欢3格子广告4试玩
   */
  static adList(key: number): Promise<IResult<any>> {
    if (!this.isWx) return Promise.resolve({ code: -1 })
    return WXSDK.game.adList(key);
  }
  /**
   * 添加曝光
   * @param type 1,抽屉 2 banner(猜你喜欢)3格子4猜你喜欢
   * @param arr 
   */
  static addExposure(type, arr: Array<IGameList>) {
    if (!this.isWx) return Promise.resolve({ code: -1 })
    return WXSDK.stat.addExposure(type, arr)
  }

  /**
   * 初始化在线参数,初始化游戏调用
   */
  static updateOnlineParams() {
    return WXSDK.online.updateOnlineConfig();
  }

  static getParamsInt(key: OnlineKeys | string, defaultVal: number = 0) {
    return WXSDK.online.getParamsInt(key.toString(), defaultVal)
  }

  static getParamsObj(key: OnlineKeys | string, defaultVal: any = {}) {
    return WXSDK.online.getParamsObj(key.toString(), defaultVal)
  }

  static getParamsString(key: OnlineKeys | string, defaultVal: string = '') {
    return WXSDK.online.getParamsString(key.toString(), defaultVal)
  }

  /**
   * 支付
   * @param params 
   * @param opts 
   * @returns 
   */
  static pay(params: { payid: string, goodid: string, money: number; orderid: string }, opts: any = {}) {
    return WXSDK.game.pay(params, opts);
  }
  /**
   * 检测支付是否成功
   */
  static orderQuery(source: string) {
    return WXSDK.game.orderQuery(source)
  }

  /**
   * 获取支付方式
   * @param goodid 商品id
   * @param orderid 订单号
   * @returns 
   */
  static preorder(goodid: string, orderid: string) {
    return WXSDK.game.preorder(goodid, orderid)
  }
  /**
   * 获取商品列表 
   */
  static goodslist() {
    return WXSDK.game.goodslist()
  }


  /**
   * 用户创角
   * @param role_name 角色名称
   * @param region 区服
   */
  static role(role_name: string, region: string) {
    return WXSDK.stat.role(role_name, region)
  }


  //抖音投放关键行为
  static behavior() {
    return WXSDK.game.behavior()
  }

  /**
   * 
   * scene    场景枚举值(1 资料;2 评论;3 论坛;4 社交日志)(必填)
    content        需检测的文本内容,文本字数的上限为2500字(必填)
    nickname    用户昵称(非必填)
    title        文本标题(非必填)
    signature    个性签名,该参数仅在资料类场景有效(scene=1)(非必填)
   */
  static checkmsg(scene: number, content: string, nickname?: string, title?: string, signature?: string) {
    return WXSDK.game.checkmsg(scene, content, nickname, title, signature)
  }

}

/**
 * 中台自定义配置表
 */
export enum OnlineKeys {
  isOpenInterstitialAdId = 'isOpenInterstitialAdId',
  shareRandom = 'shareRandom',
  switch_draw = 'switch_draw',
  switch_banner = 'switch_banner',
  switch_grid = 'switch_grid',
  calendar_level_max = 'calendar_level_max',
  over_show_ad = 'over_show_ad'
}