SDKToolsPlus.ts 13.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
import { sys } from "cc";
import { EventCenter } from "../../framework/core/utils/EventCenter";
import { EnvCode } from "../../framework/wxsdk/base/SDKEnum";
import DataService from "../../framework/wxsdk/service/DataService";
import { GameEvent } from "../../game/const/GameEvent";
import GameServicePlus from "./GameServicePlus";
import { SDKTools } from "./SDKTools";

export class ServerError {
  private static _errors = {
    "0": { msg: "成功", desc: "接口调用成功" },
    "1000": { msg: "操作失败", desc: "检查Hermes后台配置是否正确" },
    "1001": { msg: "未知错误", desc: "联系开发人员排查错误" },
    "1002": { msg: "参数错误", desc: "请参照接口文档对比参数和参数类型是否正确" },
    "1003": { msg: "签名错误", desc: "检查appkey跟后台是否对应" },
    "1004": { msg: "数据不存在", desc: "查询的数据不存在等" },
    "1005": { msg: "解析参数失败,请检查参数合法性", desc: "参数中是否有危险字符,比如SQL注入等" },
    "4500": { msg: "商品不存在", desc: "检查Hermes后台配置是否正确" },
    "4501": { msg: "订单未支付", desc: "" },
    "4502": { msg: "不能重复发放", desc: "" },
    "4503": { msg: "兑换码不存在", desc: "	" },
    "4504": { msg: "兑换码不在有效期内", desc: "	" },
    "4505": { msg: "兑换码已失效", desc: "	" },
    "4506": { msg: "iv error", desc: "防止用户本地修改数据" },
    "4507": { msg: "数据被修改", desc: "用户数据被GM工具修改,需重新登录重置" },
    "4508": { msg: "此商品已达到最大购买限制", desc: "限购" },
    "4509": { msg: "没有上传文件", desc: "检查文件name属性是否正确" },
    "4510": { msg: "文件超出最大限制", desc: "最大1M" },
    "4511": { msg: "请先授权获取微信头像信息", desc: "登录接口获取" },
    "4513": { msg: "没有达到刷新条件", desc: "生成过一次且没有领取 才可刷新" },
    "4514": { msg: "刷新次数超限", desc: "刷新次数达到 配置最大项" },
    "4515": { msg: "分享失效", desc: "跨天失效等" },
    "4516": { msg: "分享不存在", desc: "分享ID不存在等" },
    "4517": { msg: "已被领取", desc: "被其他人领取" },
    "4518": { msg: "今日领取次数超限", desc: "今天领取的次数达到配置最大项" },
    "4519": { msg: "已领过其他选项", desc: "只能领取一个" },
    "4520": { msg: "没有达到领取条件", desc: "领取进度是否完成" },
    "4522": { msg: "只能领取自己的奖励", desc: "" },
    "1009": { msg: "您的账号已因数据异常而被封禁,如需申诉可以向微信客服反馈。", desc: "" },
    "3004": { msg: "活动已经结束", desc: "" },
  }

  private static _unknownError = { msg: "未知错误", desc: "联系开发人员排查错误" };
  static getError(code: string): { msg: string, desc: string } {
    return this._errors[code] || this._unknownError;
  }

  static this(key: string): string {
    return (this._errors[key] || this._unknownError).msg;
  }
}

/*
* SDK工具类库;
*/
export class SDKToolsPlus extends SDKTools {
  static get isTT() {
    return sys.platform == sys.Platform.BYTEDANCE_MINI_GAME;
  }

  //提交订单
  static doPreorder(data, goodid: string, price: number, orderid: string) {
    if (!data.code && data.data) {
      let { config, paytype } = data.data;
      // goodid = goodid + "";
      // console.log("支付方式"+paytype);
      switch (paytype) {
        case "1"://H5 客服消息支付,游戏内调用发片消息接口 sendMessagePath:path
          var { img, title, path } = config;
          wx.openCustomerServiceConversation({
            showMessageCard: true, //是否显示会话内消息卡片,设置此参数为 true,用户进入客服会话会在右下角显示"可能要发送的小程序"提示,用户点击后可以快速发送小程序消息
            sendMessageTitle: title ? title : '我要充值!',//会话内消息卡片标题
            sendMessagePath: path,//会话内消息卡片路径//channel=${SDKTools.ChannelId}&item=10&uid=${SDKTools.UserId}
            sendMessageImg: img ? img : 'https://wxsdk-cdn.miso-lab.com/moon-island/share/fx_chongzhi.jpg',//会话内消息卡片图片路径
            success: (ret) => {
              //记录状态
              //onshow检测状态,判断是否充值成功
              console.log("支付成功:", ret);
              EventCenter.I.emit(GameEvent.PAY_POLLING, true);
            },
            fail: (ret) => {
              console.log("fail", ret);
              EventCenter.I.emit(GameEvent.PAY_FAIL, orderid, goodid);
            }
          })
          break;
        case "2":


          break;
        case "3"://米大师支付,游戏支付成功需要调用米大师订单上报接口
          var { payid } = config;
          SDKTools.pay({ payid, goodid, money: price, orderid: orderid }).then(res => {
            if (!res.code) {
              console.log("支付成功", res);
              EventCenter.I.emit(GameEvent.PAY_CONFIRM, orderid, goodid);
            } else {
              console.log("支付失败", res);
              EventCenter.I.emit(GameEvent.PAY_FAIL, orderid, goodid);
            }
          }).catch(err => {
            console.log("支付失败2:", err);
            EventCenter.I.emit(GameEvent.PAY_FAIL, orderid, goodid);
          })
          break;
        case "4"://跳转小程序支付,游戏直接跳转返回的小程序路径
          var { appid, page } = config;
          wx.navigateToMiniProgram({
            appId: appid,
            path: page,
            envVersion: DataService.I.EnvEnum === EnvCode.Prod ? 'release' : "trial",//opts.envVersion || 'release',
            success(res: any) {
              console.log("成功:", res)
              // resolve(res);
              EventCenter.I.emit(GameEvent.PAY_POLLING, true);
            },
            fail(err: any) {
              console.log("失败", err)
              EventCenter.I.emit(GameEvent.PAY_FAIL, orderid, goodid);
              // reject(err);
            }
          });
          break;
        case "5"://扫码小程序支付,imgurl为二维码地址,提示用户扫码即可
          EventCenter.I.emit(GameEvent.PAY_POLLING, true);
          var { imgurl } = config;
          wx.previewImage({
            current: imgurl, // 当前显示图片的http链接
            urls: [imgurl] // 需要预览的图片http链接列表
          })

          break;
        case "6"://二维码支付,windows下需要调用扫码支付接口
          EventCenter.I.emit(GameEvent.PAY_POLLING, true);
          var { qrcode } = config;
          wx.previewImage({
            current: qrcode, // 当前显示图片的http链接
            urls: [qrcode] // 需要预览的图片http链接列表
          })

          break;
        case "7"://抖音支付
          var { payid } = config;
          SDKTools.pay({ payid, goodid, money: price, orderid: orderid }).then(res => {
            if (!res.code) {
              console.log("支付成功", res);
              EventCenter.I.emit(GameEvent.PAY_CONFIRM, orderid, goodid);
            } else {
              console.log("支付失败", res);
              EventCenter.I.emit(GameEvent.PAY_FAIL, orderid, goodid);
            }
          }).catch(err => {
            console.log("支付失败", err);
            EventCenter.I.emit(GameEvent.PAY_FAIL, orderid, goodid);
          })
          break;
        case "10"://支付宝支付
          var { payid } = config;
          SDKTools.pay({ payid, goodid, money: price, orderid: orderid }).then(res => {
            if (!res.code) {
              console.log("111tttttt"+Date.now());
              EventCenter.I.emit(GameEvent.PAY_POLLING_NOW, true);
            } else {
              console.log("支付失败", JSON.stringify(res));
              EventCenter.I.emit(GameEvent.PAY_FAIL, orderid, goodid);
            }
          }).catch(err => {
            console.log("支付失败", JSON.stringify(err));
            EventCenter.I.emit(GameEvent.PAY_FAIL, orderid, goodid);
          })
          break;
        default:
          // utils.tips("出错!")
          break;
      }
    } else {
      // utils.tips("出错!")
    }
  }

  /**
   * 获取群二维码
   */
  static QRcode() {
    return GameServicePlus.I.getQRcode();
  }

  /**
   * 兑换CDK
   */
  static convertCDkey(cdkey: string) {
    return GameServicePlus.I.convertCDkey(cdkey);
  }

  /**
   * 商品列表
   */
  static goodsList() {
    return GameServicePlus.I.goodsList();
  }

  /**
   * 生成订单
   */
  static createOrder(goodid: string) {
    return GameServicePlus.I.createOrder(goodid);
  }

  /**
   * 查询订单
   */
  static queryOrder(no: string) {
    return GameServicePlus.I.queryOrder(no);
  }

  /**
   * 确认订单
   */
  static confirmOrder(no: string) {
    return GameServicePlus.I.confirmOrder(no);
  }

  /**
   * 获取玩家历史存档
   */
  static getUserHistory(uid: string, token: string) {
    return GameServicePlus.I.getUserHistory(uid, token);
  }

  /**
   * 获取玩家历史存档
    * @param data //需要包含uid,token
   */
  static saveUserHistory(data: any) {
    return GameServicePlus.I.saveUserHistory(data);
  }

  /**
    *  重置GM标识
    */
  static setAup(typ: number = 0) {
    return GameServicePlus.I.setAup(typ);
  }

  /**
   * 获取分享图片
   * @param items 2,1,0|3,1,1|3,1,1|3,1,1|3,1,1格式:2,1,0(类型,id,领取状态)领取状态:1已领取0未领取
   * @param zoom 图片缩放比例
   * @returns 
   */
  static getShareImage(items: string, zoom = 1) {
    return GameServicePlus.I.getShareImage(items, zoom);
  }

  /**
   * 获取今日分享
   * @param refresh 是否刷新
   */
  static getShare(refresh?: number) {
    return GameServicePlus.I.getShare(refresh);
  }

  /**
   * 获取分享详情
   * @param id 分享id
   * @param date 分享日期 格式:20220524
   */
  static getShareInfo(id: number, date: string) {
    if (typeof id === "string") {
      id = parseInt(id);
    }
    return GameServicePlus.I.getShareInfo(id, date);
  }

  /**
   * 领取好友分享
   * @param id 分享id
   * @param date 分享日期 格式:20220524
   * @param itemid 分享选项ID
   */
  static getShareReward(id: number, date: string, itemid: number) {
    if (typeof id === "string") {
      id = parseInt(id);
    }
    return GameServicePlus.I.getShareReward(id, date, itemid);
  }

  /**
   * 领取额外进度奖励
   * @param id 分享id
   * @param date 分享日期 格式:20220524
   */
  static getProgressReward(id: number, date: string) {
    if (typeof id === "string") {
      id = parseInt(id);
    }
    return GameServicePlus.I.getProgressReward(id, date);
  }

  static getInviteInfo() {
    return GameServicePlus.I.getInviteInfo();
  }

  static receiveInvite(id: number) {
    return GameServicePlus.I.receiveInvite(id);
  }

  static heartbeat() {
    return GameServicePlus.I.beatheart();
  }

  /**
   * 领取列表
   * @param status 类型1周卡2月卡
   */
  static recCardList() {
    return GameServicePlus.I.recCardList();
  }

  /**
   * 领取列表
   * @param typ 第几天 week周 month月
   */
  static recCardReward(typ: string) {
    return GameServicePlus.I.recCardReward(typ);
  }

  /**
   * 领取基金列表
   */
  static recGrowthList() {
    return GameServicePlus.I.recGrowthList();
  }

  /**
   * 领取基金列表
   * @param goodid 购买的基金商品ID
   * @param level 等级
   */
  static recGrowthReward(goodid: string, level: number) {
    return GameServicePlus.I.recGrowthReward(goodid, level);
  }

  /**
   * 获取无尽礼包数据
   */
  static recEndlessList(groupId: string) {
    return GameServicePlus.I.recEndlessList(groupId);
  }

  /**
   * 领取无尽礼包
   * @param date 当前日期
   */
  static recEndlessReward(groupId: string, id: string, date: string) {
    return GameServicePlus.I.recEndlessReward(groupId, id, date);
  }

  /**
   * 获取新手礼包数据
   */
  static getNewbieGiftData() {
    return GameServicePlus.I.getNewbieGiftData();
  }

  /**
   * 领取新手礼包
   * @param days 当前天
   */
  static recNewbieGift(days: string) {
    return GameServicePlus.I.recNewbieGift(days);
  }


  /**
   * 订阅消息
   * @param tmplId 订阅ID
   * @param message 订阅内容
   * @param sendtime 订阅发送时间s二选一参数
   * @param times 订阅延迟时间s二选一参数
   * @param status 1取消以前相同模板信息,默认为0不取消
   */
  static async sendSubScribe(tmplId: string, message: string, sendtime: number, times: number = 0, status = 1) {
    return GameServicePlus.I.sendSubScribe(tmplId, message, sendtime, times, status);
  }

  /**
   * 取消订阅消息
   * @param tmplId 订阅ID
   */
  static async cancelSubscribe(tmplId: string) {
    return GameServicePlus.I.cancelSubscribe(tmplId);
  }
  /**
   * 自定义活动
   * @param id 活动ID(后台的ID)
   */
  static activityCustom(id?: string) {
    return GameServicePlus.I.activityCustom(id);
  }
  /**
     * 添加排行榜
     * @param typ 游戏端自定义排行标识
     * @param fraction 分数 如果上报的分数小于以前上报的分数,则不会更新
     * @param rankData 扩展字段
     * @returns 
     */
  static rankAddV2(typ: string, fraction: number, rankData?: string) {
    return GameServicePlus.I.rankAddV2(typ, fraction, rankData);
  }
  /**
   * 排行榜列表
   * @param typ 游戏端自定义排行标识
   * @param percent percent等于1 的时候会返回排行榜总人数和自己的当前排名
   */
  static rankListV2(typ: string, percent: number = 0) {
    return GameServicePlus.I.rankListV2(typ, percent);
  }

}