小游戏广告,支付和一些常用接口处理

This commit is contained in:
宫欣海 2025-04-12 19:07:21 +08:00
parent e6827ae014
commit de804f7fc7
22 changed files with 2134 additions and 37 deletions

View File

@ -3516,6 +3516,15 @@ interface My {
*
*/
getLaunchOptionsSync(): AliyMiniprogram.AppLaunchOptions;
getEnterOptionsSync(): AliyMiniprogram.AppLaunchOptions;
requestGamePayment(data: {
customId: string,
buyQuantity: number,
extraInfo?: Record<string, any>,
success?: (res: { resultCode: number }) => void,
fail?: (res: CallBack.Fail) => void,
complete?: () => void
}): void;
/** 监听小游戏回到前台的事件 */
onShow(listener: (res: AliyMiniprogram.AppLaunchOptions) => void);

375
libs/lib.bytedance.api.d.ts vendored Normal file
View File

@ -0,0 +1,375 @@
/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description: API
*/
declare namespace BytedanceMiniprogram {
type IAnyObject = Record<string, any>
type GeneralSuccessCallback = (res: GeneralSuccessResult) => void;
type GeneralFailCallback = (res: GeneralFailResult) => void;
type GeneralCompleteCallback = (res: any) => void;
interface GeneralSuccessResult {
errMsg: string;
}
interface GeneralFailCodeResult {
errCode: number;
errMsg: string;
}
interface GeneralFailResult {
errMsg: string;
errNo?: number;
}
/** 获取版本信息和环境变量 */
interface EnvInfo {
/** 小程序信息 */
microapp: {
/** 小程序版本号 */
mpVersion: string;
/** 小程序环境 */
envType: string;
/** 小程序appId */
appId: string;
};
/** 插件信息 */
plugin: Record<string, unknown>;
/** 通用参数 */
common: {
/** 用户数据存储的路径 */
USER_DATA_PATH: string;
/** 校验白名单属性中的appInfoLaunchFrom后返回额外信息 */
location: string | undefined;
launchFrom: string | undefined;
schema: string | undefined;
};
}
interface SystemInfo {
/** 操作系统版本 */
system: string;
/** 操作系统类型 */
platform: string;
/** 手机品牌 */
brand: string;
/** 手机型号 */
model: string;
/** 宿主 App 版本号 */
version: string;
/**
* 宿 APP
*
* - Toutiao
* - Douyin )
* - news_article_lite )
* - live_stream
* - XiGua 西
* - PPX
*/
appName: string;
/** 客户端基础库版本 */
SDKVersion: string;
/** 屏幕宽度 */
screenWidth: number;
/** 屏幕高度 */
screenHeight: number;
/** 可使用窗口宽度 */
windowWidth: number;
/** 可使用窗口高度 */
windowHeight: number;
/** 设备像素比 */
pixelRatio: number;
/** 状态栏的高度,单位 px */
statusBarHeight: number;
/** 在竖屏正方向下的安全区域 */
safeArea: {
/** 安全区域左上角横坐标 */
left: number;
/** 安全区域右下角横坐标 */
right: number;
/** 安全区域左上角纵坐标 */
top: number;
/** 安全区域右下角纵坐标 */
bottom: number;
/** 安全区域的宽度,单位逻辑像素 */
width: number;
/** 安全区域的高度,单位逻辑像素 */
height: number;
};
}
interface LaunchParams {
/**
*
* @version 1.12.0
*/
path: string;
/**
*
* @version 1.12.0
*/
scene: string;
/**
*
* @version 1.12.0
*/
query: object;
/**
* {}
* @version 1.15.0
*/
referrerInfo: {
/** 来源小程序的 appId */
appId: string;
/** 来源小程序传过来的数据,场景值为 011009 或 011010 时支持。 */
extraData: object;
};
/**
* 10 schema0
* @version 1.90.0
*/
showFrom: number;
}
interface SocketTask {
/**
* Socket code
* , undefined
*/
readonly readyState?: 0 | 1 | 2 | 3;
/** 表示 Socket 正在连接的常量 */
readonly CONNECTING: 0;
/** 表示 Socket 连接已经打开的常量 */
readonly OPEN: 1;
/** 表示 Socket 连接关闭中的常量 */
readonly CLOSING: 2;
/** 表示 Socket 连接已关闭的常量 */
readonly CLOSED: 3;
/**
* ### WebSocket
*/
send: (res: {
data: string | ArrayBuffer,
success?: GeneralSuccessCallback,
fail?: GeneralFailCallback,
complete?: GeneralCompleteCallback
}) => void;
/** ### 关闭 WebSocket 连接的方法。 */
close: (res: {
code?: number,
reason?: string,
success?: GeneralSuccessCallback,
fail?: GeneralFailCallback,
complete?: GeneralCompleteCallback
}) => void;
/**
* ### WebSocket
* WebSocket open
*/
onOpen: (
callback: (res: {
/** 连接服务器返回的 Response Header */
header: Record<string, unknown>;
/** 使用的网络传输层协议 */
protocolType: string;
/** websocket 类型 */
socketType: "ttnet" | "tradition";
}) => void
) => void;
/** 监听 WebSocket 与服务器的连接断开的事件 */
onClose: (
callback: (res: {
/** 使用的网络传输层协议 */
protocolType: string;
/** websocket 类型 */
socketType: string;
/** 错误信息 */
errMsg: string;
/** 关闭原因 */
reason: string;
/** 关闭 code */
code: string;
}) => void
) => void;
/** ### 监听 WebSocket 接收到服务器发送信息的事件。 */
onMessage: (
callback: (res: {
/** 接收到的服务器消息 */
data: string | ArrayBuffer;
/** websocket 使用的协议 */
protocolType: string;
/** websocket 类型 */
socketType: "ttnet" | "tradition";
}) => void
) => void;
/** ### 监听 WebSocket 发生错误的事件 */
onError: (
callback: (res: {
/** 错误信息 */
errMsg: string;
}) => void
) => void;
}
interface RewardedVideoAd {
/** 广告创建后默认是隐藏的,可以通过该方法显示广告 */
show: () => Promise<any>;
/** 当广告素材加载出现错误时,可以通过 load 方法手动加载 */
load: () => Promise<any>;
/** 绑定广告 load 事件的监听器 */
onLoad: (fn: Callback) => void;
/** 解除绑定 load 事件的监听器 */
offLoad: (fn: Callback) => void;
/** 绑定 error 事件的监听器 */
onError: (fn: (data: GeneralFailCodeResult) => void) => void;
/** 解除绑定 error 事件的监听器 */
onClose: (fn: (data: { isEnded: boolean, count?: number }) => void) => void;
/** 绑定 close 事件的监听器 */
offClose: (fn: Callback) => void;
}
interface CreateRewardedVideoAdOption {
/** 广告位 id */
adUnitId: string;
/** 是否开启再得广告模式(只支持安卓系统的抖音和抖音极速版) */
multiton?: boolean;
/**
* 广广1xx
* xx multitonRewardMsg 7
* multiton true
*/
multitonRewardMsg?: string[];
/**
* 广 1-4multiton true
*/
multitonRewardTimes?: number;
/**
* 广N个获得xx 1xx
* N 广
*/
progressTip?: boolean;
}
interface IPaymentOptions {
/** 支付的类型, 目前仅为"game" */
mode: "game";
/** 环境配置,目前合法值仅为"0" */
env: 0;
/** 货币类型,目前合法值仅为"CNY" */
currencyType: "CNY";
/** 申请接入时的平台,目前仅为"android" */
platform: "android";
/**
* * =
* goodType为游戏币场景时必传
*/
buyQuantity?: number;
/**
* id"1" ID ID "_"
*/
zoneId?: string;
/**
*
*
*/
customId: string;
/** 游戏开发者自定义的其他信息,订单支付成功后通过服务端支付结果回调回传。字符串长度最大不能超过 256。 */
extraInfo?: string;
/** 支付场景 默认:0 */
goodType?: number;
/** goodType为道具直购场景时必传代表道具现金价格单位为【分】如道具价格为0.1元则回传10 */
orderAmount?: string;
/** goodType为道具直购场景时代表道具名称长度限制小于等于10个字符用于区分道具类型 */
goodName?: string;
success?: (res: GeneralSuccessResult) => void;
fail?: (res: GeneralFailCodeResult) => void;
complete?: (res: any) => void;
}
interface IAwemeCustomerOptions {
/** 游戏开发者自定义的其他信息,订单支付成功后通过服务端支付结果回调回传。字符串长度最大不能超过 256。强烈建议传入 */
extraInfo?: string;
/**
* id"1" ID ID "_"
*/
zoneId?: string;
/** 币种目前仅为“DIAMOND” */
currencyType: "DIAMOND" | "CNY";
/**
* * =
* goodType为游戏币场景时必传
*/
buyQuantity?: number;
/** 支付场景 默认:0 */
goodType?: number;
/** goodType为道具直购场景时必传代表道具现金价格单位为【分】如道具价格为0.1元则回传10 */
orderAmount?: string;
/** goodType为道具直购场景时代表道具名称长度限制小于等于10个字符用于区分道具类型 */
goodName?: string;
/**
*
*
*/
customId: string;
success?: (res: GeneralSuccessResult) => void;
fail?: (res: GeneralFailResult) => void;
complete?: (res: any) => void;
}
interface TT {
getEnvInfoSync(): EnvInfo;
getSystemInfoSync(): SystemInfo;
getLaunchOptionsSync(): LaunchParams;
exitMiniProgram(res: {
success?: GeneralSuccessCallback,
fail?: GeneralFailCallback,
complete?: GeneralCompleteCallback
}): void;
setClipboardData(res: {
data: string,
success?: GeneralSuccessCallback,
fail?: GeneralFailCallback,
complete?: GeneralCompleteCallback
}): void;
connectSocket(res: {
/** Socket 连接地址 */
url: string;
/** 请求头 */
header?: Record<string, string>;
/** 子协议 */
protocols?: string[];
success?: (res: { socketTaskId: number }) => void,
fail?: GeneralFailCallback,
complete?: GeneralCompleteCallback
}): SocketTask;
// requestMidasPayment(res: MidasPaymentOption): void;
createRewardedVideoAd(option: CreateRewardedVideoAdOption): RewardedVideoAd;
/** 支付 */
requestGamePayment(res: IPaymentOptions): void;
/** 发起抖音钻石支付 */
openAwemeCustomerService(res: IAwemeCustomerOptions): void;
}
}
declare const tt: BytedanceMiniprogram.TT

511
libs/lib.wx.api.d.ts vendored
View File

@ -24,6 +24,11 @@ declare namespace WechatMiniprogram {
complete?: () => void;
}
interface GeneralCallbackResult {
/** 错误信息 */
errMsg: string
}
interface ConnectSocketOption extends ICommonCallBack {
/** 开发者服务器 wss 接口地址 */
url: string
@ -148,6 +153,510 @@ declare namespace WechatMiniprogram {
interface Wx {
connectSocket(option: ConnectSocketOption): SocketTask
}
}
/** 启动参数 */
interface LaunchOptionsApp {
/** `2.20.0`
*
* API
*
*
* - 'default': ;
* - 'nativeFunctionalized': ;
* - 'browseOnly': ;
* - 'embedded': ; */
apiCategory:
| 'default'
| 'nativeFunctionalized'
| 'browseOnly'
| 'embedded'
/** 打开的文件信息数组只有从聊天素材场景打开scene为1173才会携带该参数 */
forwardMaterials: ForwardMaterials[]
/** 启动小程序的路径 (代码包路径) */
path: string
/** 启动小程序的 query 参数 */
query: Record<string, string>
/** 来源信息。从另一个小程序、公众号或 App 进入小程序时返回。否则返回 `{}`。(参见后文注意) */
referrerInfo: ReferrerInfo
/** 启动小程序的[场景值](https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/scene.html) */
scene: number
/** /chatType /
*
*
* - 1: 微信联系人单聊;
* - 2: 企业微信联系人单聊;
* - 3: 普通微信群聊;
* - 4: 企业微信互通群聊; */
chatType?: 1 | 2 | 3 | 4
/** shareTicket详见[获取更多转发信息](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share.html) */
shareTicket?: string
}
/** 当前小程序运行的宿主环境 */
interface AppBaseInfoHost {
/** 宿主 app第三方App 对应的 appId 当小程序运行在第三方App环境时才返回 */
appId: string
}
interface AppBaseInfo {
/** 客户端基础库版本 */
SDKVersion: string
/** 是否已打开调试。可通过右上角菜单或 [wx.setEnableDebug](https://developers.weixin.qq.com/miniprogram/dev/api/base/debug/wx.setEnableDebug.html) 打开调试。 */
enableDebug: boolean
/** 当前小程序运行的宿主环境 */
host: AppBaseInfoHost
/** 微信设置的语言 */
language: string
/** 微信版本号 */
version: string
/** `light``dark``"darkmode":true` undefined
*
*
* - 'dark': ;
* - 'light': ; */
theme?: 'dark' | 'light'
}
interface SafeArea {
/** 安全区域右下角纵坐标 */
bottom: number
/** 安全区域的高度,单位逻辑像素 */
height: number
/** 安全区域左上角横坐标 */
left: number
/** 安全区域右下角横坐标 */
right: number
/** 安全区域左上角纵坐标 */
top: number
/** 安全区域的宽度,单位逻辑像素 */
width: number
}
interface WindowInfo {
/** 设备像素比 */
pixelRatio: number
/** 在竖屏正方向下的安全区域。部分机型没有安全区域概念,也不会返回 safeArea 字段,开发者需自行兼容。 */
safeArea: SafeArea
/** 屏幕高度单位px */
screenHeight: number
/** 窗口上边缘的y值 */
screenTop: number
/** 屏幕宽度单位px */
screenWidth: number
/** 状态栏的高度单位px */
statusBarHeight: number
/** 可使用窗口高度单位px */
windowHeight: number
/** 可使用窗口宽度单位px */
windowWidth: number
}
interface SystemInfo {
/** `1.1.0`
*
* */
SDKVersion: string
/** `2.6.0`
*
* 使 iOS */
albumAuthorized: boolean
/** `1.8.0`
*
* Android-2 0-1>=1<br> IDE */
benchmarkLevel: number
/** `2.6.0`
*
* */
bluetoothEnabled: boolean
/** `1.5.0`
*
* */
brand: string
/** `2.6.0`
*
* 使 */
cameraAuthorized: boolean
/**
*
*
* - 'portrait': ;
* - 'landscape': ; */
deviceOrientation: 'portrait' | 'landscape'
/** `2.15.0`
*
* [wx.setEnableDebug](https://developers.weixin.qq.com/miniprogram/dev/api/base/debug/wx.setEnableDebug.html) 打开调试。 */
enableDebug: boolean
/** `1.5.0`
*
* px--- */
fontSizeSetting: number
/** `2.12.3`
*
* 宿 */
host: SystemInfoHost
/** 微信设置的语言 */
language: string
/** `2.6.0`
*
* 使 */
locationAuthorized: boolean
/** `2.6.0`
*
* */
locationEnabled: boolean
/** `true` 表示模糊定位,`false` 表示精确定位,仅 iOS 支持 */
locationReducedAccuracy: boolean
/** `2.6.0`
*
* 使 */
microphoneAuthorized: boolean
/** 设备型号。新机型刚推出一段时间会显示unknown微信会尽快进行适配。 */
model: string
/** `2.6.0`
*
* iOS */
notificationAlertAuthorized: boolean
/** `2.6.0`
*
* */
notificationAuthorized: boolean
/** `2.6.0`
*
* iOS */
notificationBadgeAuthorized: boolean
/** `2.6.0`
*
* iOS */
notificationSoundAuthorized: boolean
/** `2.19.3`
*
* 使 */
phoneCalendarAuthorized: boolean
/** 设备像素比 */
pixelRatio: number
/**
*
*
* - 'ios': iOS微信 iPhoneiPad;
* - 'android': Android微信;
* - 'windows': Windows微信;
* - 'mac': macOS微信;
* - 'devtools': ; */
platform: 'ios' | 'android' | 'windows' | 'mac' | 'devtools'
/** `2.7.0`
*
* safeArea */
safeArea: SafeArea
/** `1.1.0`
*
* px */
screenHeight: number
/** `1.1.0`
*
* px */
screenWidth: number
/** `1.9.0`
*
* px */
statusBarHeight: number
/** 操作系统及版本 */
system: string
/** 微信版本号 */
version: string
/** `2.6.0`
*
* Wi-Fi */
wifiEnabled: boolean
/** 可使用窗口高度单位px */
windowHeight: number
/** 可使用窗口宽度单位px */
windowWidth: number
/** `2.11.0`
*
* `light``dark``"darkmode":true` undefined
*
*
* - 'dark': ;
* - 'light': ; */
theme?: 'dark' | 'light'
}
interface DeviceInfo {
/** 应用微信APP二进制接口类型仅 Android 支持) */
abi: string
/** 设备性能等级(仅 Android 支持)。取值为:-2 或 0该设备无法运行小游戏-1性能未知>=1设备性能值该值越高设备性能越好目前最高不到50 */
benchmarkLevel: number
/** 设备品牌 */
brand: string
/** `2.29.0`
*
* CPU Android Tips: GPU WebGLRenderingContext.getExtension('WEBGL_debug_renderer_info') */
cpuType: string
/** `2.25.1`
*
* Android */
deviceAbi: string
/** `2.30.0`
*
* MB */
memorySize: string
/** 设备型号。新机型刚推出一段时间会显示unknown微信会尽快进行适配。 */
model: string
/** 客户端平台 */
platform: string
/** 操作系统及版本 */
system: string
}
/** 小程序账号信息 */
interface MiniProgram {
/** 小程序 appId */
appId: string
/** `2.10.0`
*
*
*
*
* - 'develop': ;
* - 'trial': ;
* - 'release': ; */
envVersion: 'develop' | 'trial' | 'release'
/** `2.10.2`
*
* 线 */
version: string
}
/** 插件账号信息(仅在插件中调用时包含这一项) */
interface Plugin {
/** 插件 appId */
appId: string
/** 插件版本号 */
version: string
}
/** 账号信息 */
interface AccountInfo {
/** 小程序账号信息 */
miniProgram: MiniProgram
/** 插件账号信息(仅在插件中调用时包含这一项) */
plugin: Plugin
}
interface MidasPaymentOption {
/** 支付的类型,不同的支付类型有各自额外要传的附加参数 */
mode: "game",
/** 是否为沙盒环境 0: 正式环境 1: 沙盒环境 */
env?: 0 | 1,
/** 商户号 在米大师侧申请的应用id */
offerId: string,
/** 货币类型 */
currencyType: "CNY",
/** 申请接入时的平台platform 与应用id有关 */
platform?: "android" | "windows",
/** 购买数量。mode=game 时必填。购买数量 */
buyQuantity: number,
/** 分区ID 默认1 */
zoneId?: string,
/**
* 使使32_-|*线)wx.requestMidasPayment都换新的outTradeNo线
*/
outTradeNo: string,
/** 接口调用成功的回调函数 */
success?: (res: { errMsg: string }) => void,
/** 接口调用失败的回调函数 */
fail?: (res: { errCode: number, errMsg: string, errno: number }) => void,
/** 接口调用结束的回调函数(调用成功、失败都会执行) */
complete?: () => void;
}
interface RewardedVideoAdOnCloseListenerResult {
/** `2.1.0`
*
* */
isEnded: boolean
}
interface RewardedVideoAdOnErrorListenerResult {
/** `2.2.2`
*
*
*
*
* - 1000: 后端接口调用失败;
* - 1001: 参数错误;
* - 1002: 广告单元无效;
* - 1003: 内部错误;
* - 1004: 无合适的广告;
* - 1005: 广告组件审核中;
* - 1006: 广告组件被驳回;
* - 1007: 广告组件被封禁;
* - 1008: 广告单元已关闭; */
errCode: 1000 | 1001 | 1002 | 1003 | 1004 | 1005 | 1006 | 1007 | 1008
/** 错误信息 */
errMsg: string
}
/** onClose 传入的监听函数。不传此参数则移除所有监听函数。 */
type RewardedVideoAdOffCloseCallback = (
result: RewardedVideoAdOnCloseListenerResult
) => void
/** onError 传入的监听函数。不传此参数则移除所有监听函数。 */
type RewardedVideoAdOffErrorCallback = (
result: RewardedVideoAdOnErrorListenerResult
) => void
/** 用户点击 `关闭广告` 按钮的事件的监听函数 */
type RewardedVideoAdOnCloseCallback = (
result: RewardedVideoAdOnCloseListenerResult
) => void
/** 激励视频错误事件的监听函数 */
type RewardedVideoAdOnErrorCallback = (
result: RewardedVideoAdOnErrorListenerResult
) => void
/** onLoad 传入的监听函数。不传此参数则移除所有监听函数。 */
type OffLoadCallback = (res: GeneralCallbackResult) => void
type OnLoadCallback = (res: GeneralCallbackResult) => void
interface RewardedVideoAd {
/**
* [Promise RewardedVideoAd.load()](https://developers.weixin.qq.com/miniprogram/dev/api/ad/RewardedVideoAd.load.html)
* 使
* 广 */
load(): Promise<any>
/**
* [Promise RewardedVideoAd.show()](https://developers.weixin.qq.com/miniprogram/dev/api/ad/RewardedVideoAd.show.html)
* 使
* 广广 */
show(): Promise<any>
/**
* [RewardedVideoAd.destroy()](https://developers.weixin.qq.com/miniprogram/dev/api/ad/RewardedVideoAd.destroy.html)
* `2.8.0`
* 使
* 广 */
destroy(): void
/**
* [RewardedVideoAd.offClose(function listener)](https://developers.weixin.qq.com/miniprogram/dev/api/ad/RewardedVideoAd.offClose.html)
* 使
* `关闭广告`
*
* ****
*
* ```js
* const listener = function (res) { console.log(res) }
*
* RewardedVideoAd.onClose(listener)
* RewardedVideoAd.offClose(listener) // 需传入与监听时同一个的函数对象
* ```
*/
offClose(
/** onClose 传入的监听函数。不传此参数则移除所有监听函数。 */
listener?: RewardedVideoAdOffCloseCallback
): void
/**
* [RewardedVideoAd.offError(function listener)](https://developers.weixin.qq.com/miniprogram/dev/api/ad/RewardedVideoAd.offError.html)
* 使
*
*
* ****
*
* ```js
* const listener = function (res) { console.log(res) }
*
* RewardedVideoAd.onError(listener)
* RewardedVideoAd.offError(listener) // 需传入与监听时同一个的函数对象
* ```
*/
offError(
/** onError 传入的监听函数。不传此参数则移除所有监听函数。 */
listener?: RewardedVideoAdOffErrorCallback
): void
/**
* [RewardedVideoAd.offLoad(function listener)](https://developers.weixin.qq.com/miniprogram/dev/api/ad/RewardedVideoAd.offLoad.html)
* 使
* 广
* ****
* ```js
* const listener = function (res) { console.log(res) }
*
* RewardedVideoAd.onLoad(listener)
* RewardedVideoAd.offLoad(listener) // 需传入与监听时同一个的函数对象
* ```
*/
offLoad(
/** onLoad 传入的监听函数。不传此参数则移除所有监听函数。 */
listener?: OffLoadCallback
): void
/**
* [RewardedVideoAd.onClose(function listener)](https://developers.weixin.qq.com/miniprogram/dev/api/ad/RewardedVideoAd.onClose.html)
* 使
* `关闭广告` */
onClose(
/** 用户点击 `关闭广告` 按钮的事件的监听函数 */
listener: RewardedVideoAdOnCloseCallback
): void
/**
* [RewardedVideoAd.onError(function listener)](https://developers.weixin.qq.com/miniprogram/dev/api/ad/RewardedVideoAd.onError.html)
* 使
*
*
* ****
*
* onError获取到的错误信息
* 线[](https://mp.weixin.qq.com/)里面搜寻错误日志,还可以针对异常返回加上适当的监控信息。
*
* | | | | |
* | ------ | -------------- | --------------- | -------------------------- |
* | 1000 | | | |
* | 1001 | | 使 | developers.weixin.qq.com确认具体教程|
* | 1002 | 广 | APP的广告ID | mp.weixin.qq.com确认广告位ID |
* | 1003 | | | |
* | 1004 | 广 | 广广 | |
* | 1005 | 广 | 广广 | mp.weixin.qq.com确认审核状态|
* | 1006 | 广 | 广广 | mp.weixin.qq.com确认审核状态|
* | 1007 | 广 | 广广 | mp.weixin.qq.com确认小程序广告封禁状态 |
* | 1008 | 广 | 广广 | mp.weixin.qq.com重新打开对应广告位的展现| */
onError(
/** 激励视频错误事件的监听函数 */
listener: RewardedVideoAdOnErrorCallback
): void
/**
* [RewardedVideoAd.onLoad(function listener)](https://developers.weixin.qq.com/miniprogram/dev/api/ad/RewardedVideoAd.onLoad.html)
* 使
* 广 */
onLoad(
/** 激励视频广告加载事件的监听函数 */
listener: OnLoadCallback
): void
}
interface CreateRewardedVideoAdOption {
/** 广告单元 id */
adUnitId: string
/**
* `2.8.0`
* false
*/
multiton?: boolean
}
interface Wx {
getLaunchOptionsSync(): LaunchOptionsApp;
getEnterOptionsSync(): LaunchOptionsApp;
getWindowInfo(): WindowInfo;
getAppBaseInfo(): AppBaseInfo;
getSystemInfoSync(): SystemInfo;
getDeviceInfo(): DeviceInfo;
getAccountInfoSync(): AccountInfo;
exitMiniProgram(): void;
setClipboardData(res: { data: string, fail: (res: GeneralCallbackResult) => void }): void;
requestMidasPayment(res: MidasPaymentOption): void;
/**
* [[RewardedVideoAd](https://developers.weixin.qq.com/miniprogram/dev/api/ad/RewardedVideoAd.html) wx.createRewardedVideoAd(Object object)](https://developers.weixin.qq.com/miniprogram/dev/api/ad/wx.createRewardedVideoAd.html)
* `2.0.4`
* 使 `2.8.1`
*
* 广 [wx.getSystemInfoSync()](https://developers.weixin.qq.com/miniprogram/dev/api/base/system/wx.getSystemInfoSync.html) 返回对象的 SDKVersion 判断基础库版本号后再使用该 API小游戏端要求 >= 2.0.4 小程序端要求 >= 2.6.0)。调用该方法创建的激励视频广告是一个单例(小游戏端是全局单例,小程序端是页面内单例,在小程序端的单例对象不允许跨页面使用)。
*/
createRewardedVideoAd(option: CreateRewardedVideoAdOption): RewardedVideoAd
}
}
declare const wx: WechatMiniprogram.Wx

View File

@ -7,6 +7,7 @@
import { Asset, game, native, sys } from "cc";
import { Platform } from "../global/Platform";
import { log, warn } from "../tool/log";
import { Utils } from "../tool/Utils";
const TAG = "hotupdate:";
@ -60,7 +61,7 @@ export class HotUpdateManager {
// 创建 am 对象
this._am = native.AssetsManager.create("", this._writablePath);
this._am.setVersionCompareHandle(this._versionCompareHandle);
this._am.setVersionCompareHandle(Utils.compareVersion);
this._am.setVerifyCallback(this._verifyCallback);
// 加载本地的 manifest
log(`${TAG} 加载本地的 manifest:${manifest.nativeUrl}`);
@ -223,33 +224,6 @@ export class HotUpdateManager {
}
}
/**
*
* @param version1
* @param version2
* 0version1大于version2
* 0version1等于version2
* 0version1小于version2
*/
private _versionCompareHandle(version1: string, version2: string): number {
log(`${TAG}本地资源版本号:${version1} 远程资源版本号:${version2}`);
let v1 = version1.split('.');
let v2 = version2.split('.');
for (let i = 0; i < v1.length; ++i) {
let a = parseInt(v1[i]);
let b = parseInt(v2[i] || '0');
if (a === b) {
continue;
} else {
return a - b;
}
}
if (v2.length > v1.length) {
return -1;
}
return 0;
}
private _verifyCallback(path: string, asset: native.ManifestAsset): boolean {
// 资源是否被压缩, 如果压缩我们不需要检查它的md5值
let compressed = asset.compressed;

View File

@ -79,3 +79,10 @@ export { ConditionBase } from "./condition/node/ConditionBase";
/** 热更新 */
export { HotUpdateManager } from "./hotupdate/HotUpdateManager";
/** 小游戏 */
export { AlipayCommon } from "./minigame/alipay/AlipayCommon";
export { BytedanceCommon } from "./minigame/bytedance/BytedanceCommon";
export { MiniHelper } from "./minigame/MiniHelper";
export { WechatCommon } from "./minigame/wechat/WechatCommon";

View File

@ -0,0 +1,68 @@
/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description:
*/
import { Platform } from "../global/Platform";
import { BytedanceCommon } from "../kunpocc";
import { AlipayAds } from "./alipay/AlipayAds";
import { AlipayCommon } from "./alipay/AlipayCommon";
import { AlipayPay } from "./alipay/AlipayPay";
import { BytedanceAds } from "./bytedance/BytedanceAds";
import { BytedancePay } from "./bytedance/BytedancePay";
import { IMiniRewardAds } from "./interface/IMiniAds";
import { IMiniCommon } from "./interface/IMiniCommon";
import { IMiniPay } from "./interface/IMiniPay";
import { WechatAds } from "./wechat/WechatAds";
import { WechatCommon } from "./wechat/WechatCommon";
import { WechatPay } from "./wechat/WechatPay";
export class MiniHelper {
/** 基础数据 */
private static _common: IMiniCommon = null;
/** 广告 */
private static _ad: IMiniRewardAds = null;
/** 支付 */
private static _pay: IMiniPay = null;
public static common<T extends IMiniCommon>(): T {
if (!this._common) {
if (Platform.isWX) {
this._common = new WechatCommon();
} else if (Platform.isAlipay) {
this._common = new AlipayCommon();
this._ad = new AlipayAds();
} else if (Platform.isBytedance) {
this._common = new BytedanceCommon();
}
}
return this._common as T;
}
public static ad<T extends IMiniRewardAds>(): T {
if (!this._ad) {
if (Platform.isWX) {
this._ad = new WechatAds();
} else if (Platform.isAlipay) {
this._ad = new AlipayAds();
} else if (Platform.isBytedance) {
this._ad = new BytedanceAds();
}
}
return this._ad as T;
}
public static pay<T extends IMiniPay>(): T {
if (!this._pay) {
if (Platform.isWX) {
this._pay = new WechatPay();
} else if (Platform.isAlipay) {
this._pay = new AlipayPay();
} else if (Platform.isBytedance) {
this._pay = new BytedancePay();
}
}
return this._pay as T;
}
}

View File

@ -0,0 +1,75 @@
/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description: 广
*/
import { warn } from "../../tool/log";
import { MiniErrorCode } from "../header";
import { IMiniRewardAds } from "../interface/IMiniAds";
export class AlipayAds implements IMiniRewardAds {
private _adUnitId: string = "";
private _video_ad: AliyMiniprogram.RewardedAd = null;
/**
* 广
*/
private _success: () => void;
/**
* 广
*/
private _fail: (errCode: number, errMsg: string) => void;
public init(adUnitId: string): void {
this._adUnitId = adUnitId;
}
public showAds(res: { success: () => void, fail: (errCode: number, errMsg: string) => void }): void {
if (this._adUnitId === "") {
warn(MiniErrorCode.AD_NOT_INIT.msg);
res.fail(MiniErrorCode.AD_NOT_INIT.code, MiniErrorCode.AD_NOT_INIT.msg);
return;
}
this._success = res.success;
this._fail = res.fail;
if (!this._video_ad) {
this._video_ad = this.createVideoAd();
}
this._video_ad.load().then(() => {
this._video_ad.show();
}).catch((res: { errMsg: string; errNo: number }) => {
this._fail(res.errNo, res.errMsg);
this.reset();
});
}
private createVideoAd(): AliyMiniprogram.RewardedAd {
let videoAd = my.createRewardedAd({ adUnitId: this._adUnitId, multiton: false });
/** 广告加载失败 */
videoAd.onError((res: AliyMiniprogram.CallBack.Fail) => {
this._fail?.(res.error, res.errorMessage);
this.reset();
});
videoAd.onClose((res: { isEnded: boolean }) => {
if ((res && res.isEnded) || res === undefined) {
/** 广告播放完成 */
this?._success();
this.reset();
} else {
/** 中途退出,不发放奖励 */
this?._fail(MiniErrorCode.AD_EXIT.code, MiniErrorCode.AD_EXIT.msg);
this.reset();
}
});
return videoAd;
}
/** 防止多次回调 */
private reset(): void {
this._success = null;
this._fail = null;
}
}

View File

@ -0,0 +1,119 @@
/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description:
*/
import { warn } from "../../tool/log";
import { IMiniCommon } from "../interface/IMiniCommon";
export class AlipayCommon implements IMiniCommon {
private _launchOptions: AliyMiniprogram.AppLaunchOptions = null;
private _systemInfo: getSystemInfoSyncReturn = null;
private _accountInfo: AliyMiniprogram.AccountInfo = null;
/**
* @internal
*/
constructor() {
this._launchOptions = my.getLaunchOptionsSync();
}
public getLaunchOptions(): AliyMiniprogram.AppLaunchOptions {
return this._launchOptions;
}
public getHotLaunchOptions(): Record<string, any> {
return my.getEnterOptionsSync();
}
/**
*
*/
public getLibVersion(): string {
return my.SDKVersion;
}
/**
* ios | android | ohos | windows | mac | devtools
*/
public getPlatform(): 'ios' | 'android' | 'ohos' | 'windows' | 'mac' | 'devtools' | 'iPad' {
let platform = this.getSystemInfo().platform;
if (platform === 'iOS' || platform == 'iPhone OS') {
return 'ios';
} else if (platform.indexOf('iPad') > 0) {
return 'iPad';
}
return platform as ('ios' | 'android' | 'ohos' | 'windows' | 'mac' | 'devtools' | 'iPad');
}
/**
*
*/
public getEnvType(): 'release' | 'debug' {
return this.getAccountInfo().miniProgram.envVersion == "release" ? "release" : "debug";
}
/**
* 宿 ( 宿 )
*/
public getHostVersion(): string {
return this.getSystemInfo().version;
}
/**
*
*/
public getScreenSize(): { width: number, height: number } {
const systemInfo = this.getSystemInfo();
return {
width: systemInfo.windowWidth,
height: systemInfo.windowHeight
}
}
/**
* 退 ()
*/
public exitMiniProgram(): void {
my.exitProgram();
}
/**
*
*/
public setClipboardData(text: string): void {
my.setClipboard({
text: text,
fail: (res: AliyMiniprogram.CallBack.Fail) => {
warn(`复制到剪切板失败 code:${res.error} msg:${res.errorMessage}`);
}
});
}
private getSystemInfo(): getSystemInfoSyncReturn {
if (this._systemInfo) {
return this._systemInfo;
}
if (my.getSystemInfoSync) {
this._systemInfo = my.getSystemInfoSync();
return this._systemInfo;
}
warn("getSystemInfo 失败");
return null;
}
private getAccountInfo(): AliyMiniprogram.AccountInfo {
if (this._accountInfo) {
return this._accountInfo;
}
if (my.getAccountInfoSync) {
this._accountInfo = my.getAccountInfoSync();
return this._accountInfo;
}
warn("getAccountInfo 失败");
return null;
}
}

View File

@ -0,0 +1,72 @@
/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description:
*/
import { log, warn } from "../../tool/log";
import { Utils } from "../../tool/Utils";
import { MiniErrorCode, PriceLimitList } from "../header";
import { IMiniPay, IMiniPayParams } from "../interface/IMiniPay";
import { MiniHelper } from "../MiniHelper";
export class AlipayPay implements IMiniPay {
private _unitPriceQuantity: number = 0;
public init(offerId: string, unitPriceQuantity: number): void {
this._unitPriceQuantity = unitPriceQuantity;
}
public isPayable(rmb: number): boolean {
return PriceLimitList.includes(rmb);
}
public pay(res: IMiniPayParams): void {
if (this._unitPriceQuantity === 0) {
warn("请先调用 init 方法初始化");
res.fail({ errCode: MiniErrorCode.PAY_NOT_INIT.code, errMsg: MiniErrorCode.PAY_NOT_INIT.msg });
return;
}
if (!this.isPayable(res.rmb)) {
res.fail({ errCode: -15016, errMsg: "传入价格不满足限定条件" });
return;
}
let platform = MiniHelper.common().getPlatform();
if (platform === "ios") {
res.fail({ errCode: MiniErrorCode.IOS_FORBIDDEN.code, errMsg: MiniErrorCode.IOS_FORBIDDEN.msg });
return;
}
if (platform === "iPad") {
res.fail({ errCode: MiniErrorCode.IOS_FORBIDDEN.code, errMsg: "iPad禁止支付" });
return;
}
if (Utils.compareVersion(MiniHelper.common().getHostVersion(), "10.3.90") < 0) {
res.fail({ errCode: MiniErrorCode.IOS_FORBIDDEN.code, errMsg: "支付宝版本过低, 请升级支付宝" });
return;
}
log(`AlipayPay rmb:${res.rmb}元 orderId:${res.orderId} sandbox:${res.sandbox || 0} shopId:${res.shopId} shopName:${res.shopName}`);
let extraInfo = {
shopId: res.shopId,
shopName: res.shopName,
sandbox: res.sandbox || 0,
}
if (res.extraInfo) {
// 合并extraInfo和res.extraInfo
extraInfo = { ...extraInfo, ...res.extraInfo };
}
my.requestGamePayment({
customId: res.orderId,
buyQuantity: res.rmb * this._unitPriceQuantity,
extraInfo: extraInfo,
success: (param: { resultCode: number }) => {
res.success({ code: param.resultCode, message: "success" });
},
fail: (param: AliyMiniprogram.CallBack.Fail) => {
warn(`WechatPay fail code:${param.error} msg:${param.errorMessage}`);
res.fail({ errCode: param.error, errMsg: param.errorMessage });
}
});
}
}

View File

@ -0,0 +1,81 @@
/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description: 广
*/
import { log, warn } from "../../tool/log";
import { MiniErrorCode } from "../header";
import { IMiniRewardAds } from "../interface/IMiniAds";
export class BytedanceAds implements IMiniRewardAds {
private _adUnitId: string = "";
private _video_ad: BytedanceMiniprogram.RewardedVideoAd = null;
/**
* 广
*/
private _success: () => void;
/**
* 广
*/
private _fail: (errCode: number, errMsg: string) => void;
public init(adUnitId: string): void {
this._adUnitId = adUnitId;
}
/**
* 广
*/
public showAds(res: { success: () => void, fail: (errCode: number, errMsg: string) => void }): void {
if (this._adUnitId === "") {
warn(MiniErrorCode.AD_NOT_INIT.msg);
res.fail(MiniErrorCode.AD_NOT_INIT.code, MiniErrorCode.AD_NOT_INIT.msg);
return;
}
this._success = res.success;
this._fail = res.fail;
if (!this._video_ad) {
this._video_ad = this.createVideoAd();
}
log("加载广告");
this._video_ad.load().then(() => {
log("广告加载成功");
this._video_ad.show();
}).catch((res: { errMsg: string; errNo: number }) => {
warn(`广告加载失败 errCode:${res.errNo} errMsg:${res.errMsg}`);
this._fail(res.errNo, res.errMsg);
this.reset();
});
}
private createVideoAd(): BytedanceMiniprogram.RewardedVideoAd {
let videoAd = tt.createRewardedVideoAd({ adUnitId: this._adUnitId, multiton: false });
/** 激励视频错误事件的监听函数 */
videoAd.onError((res: { errMsg: string; errCode: number }) => {
warn(`激励视频广告 onError:${res.errCode}:${res.errMsg}`);
this._fail(res.errCode, res.errMsg);
this.reset();
});
videoAd.onClose((res: { isEnded: boolean, count?: number }) => {
if ((res && res.isEnded) || res === undefined) {
/** 广告播放完成 */
this?._success();
this.reset();
} else {
/** 中途退出,不发放奖励 */
this?._fail(MiniErrorCode.AD_EXIT.code, MiniErrorCode.AD_EXIT.msg);
this.reset();
}
});
return videoAd;
}
/** 防止多次回调 */
private reset(): void {
this._success = null;
this._fail = null;
}
}

View File

@ -0,0 +1,125 @@
/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description:
*/
import { LaunchParams } from "@douyin-microapp/typings/types/app";
import { warn } from "../../tool/log";
import { IMiniCommon } from "../interface/IMiniCommon";
export class BytedanceCommon implements IMiniCommon {
private _launchOptions: BytedanceMiniprogram.LaunchParams = null;
private _systemInfo: BytedanceMiniprogram.SystemInfo = null;
private _envInfo: BytedanceMiniprogram.EnvInfo = null;
/**
* @internal
*/
constructor() {
this._launchOptions = tt.getLaunchOptionsSync();
}
/**
*
*/
public getLaunchOptions(): LaunchParams {
return this._launchOptions;
}
/**
*
*/
public getHotLaunchOptions(): LaunchParams {
warn("字节跳动小游戏未提供热启动参数获取方式,请在 onShow 中获取");
return null;
}
/**
*
*/
public getLibVersion(): string {
return this.getSystemInfo()?.SDKVersion || "0.0.1";
}
/**
* 宿 ()
*/
public getHostVersion(): string {
return this.getSystemInfo()?.version || "0.0.1";
}
/**
* 宿 APP "Toutiao"
* [https://developer.open-douyin.com/docs/resource/zh-CN/mini-game/develop/api/system/system-information/tt-get-system-info-sync]
*/
public getHostName(): string {
return this.getSystemInfo()?.appName || "unknown";
}
/**
*
*/
public getPlatform(): 'ios' | 'android' | 'ohos' | 'windows' | 'mac' | 'devtools' {
return this.getSystemInfo().platform as ('ios' | 'android' | 'ohos' | 'windows' | 'mac' | 'devtools');
}
/**
*
*/
public getEnvType(): 'release' | 'debug' {
return this.getEnvInfo().microapp.envType == "production" ? "release" : "debug";
}
/**
* 退
*/
public exitMiniProgram(): void {
tt.exitMiniProgram?.({});
}
public getScreenSize(): { width: number, height: number } {
const systemInfo = this.getSystemInfo();
return {
width: systemInfo.screenWidth,
height: systemInfo.screenHeight,
};
}
/**
*
*/
public setClipboardData(text: string): void {
tt.setClipboardData({
data: text,
fail: (res: { errMsg: string, errNo?: number }) => {
warn(`复制到剪切板失败 errCode:${res.errNo} errMsg:${res.errMsg}`);
}
});
}
private getEnvInfo(): BytedanceMiniprogram.EnvInfo {
if (this._envInfo) {
return this._envInfo;
}
if (tt.getEnvInfoSync) {
this._envInfo = tt.getEnvInfoSync();
return this._envInfo;
}
warn("getEnvInfo 失败");
return null;
}
private getSystemInfo(): BytedanceMiniprogram.SystemInfo {
if (this._systemInfo) {
return this._systemInfo;
}
if (tt.getSystemInfoSync) {
this._systemInfo = tt.getSystemInfoSync();
return this._systemInfo;
}
warn("getSystemInfo 失败");
return null;
}
}

View File

@ -0,0 +1,125 @@
/**
* @Author: Gongxh
* @Date: 2025-04-12
* @Description:
* https://developer.open-douyin.com/docs/resource/zh-CN/mini-game/develop/api/payment/tt-request-game-payment
*/
import { log, warn } from "../../tool/log";
import { Utils } from "../../tool/Utils";
import { MiniErrorCode, PriceLimitList } from "../header";
import { IMiniPay, IMiniPayParams } from "../interface/IMiniPay";
import { MiniHelper } from "../MiniHelper";
import { BytedanceCommon } from "./BytedanceCommon";
enum DouyinMiniAppName {
Toutiao = "Toutiao", // 今日头条
Douyin = "Douyin", // 抖音
ToutiaoLite = "news_article_lite", // 今日头条极速版
LiveStream = "live_stream", // 火山小视频
XiGua = "XiGua", // 西瓜
PPX = "PPX", // 皮皮虾
DouyinLite = "DouyinLite", // 抖音极速版
LiveStreamLite = "live_stream_lite",// 火山小视频极速版
NovelFm = "novel_fm", // 番茄畅听
NovelApp = "novelapp", // 番茄小说
}
export class BytedancePay implements IMiniPay {
private _unitPriceQuantity: number = 0;
public init(offerId: string, unitPriceQuantity: number): void {
this._unitPriceQuantity = unitPriceQuantity;
}
public isPayable(rmb: number): boolean {
return PriceLimitList.includes(rmb);
}
public pay(res: IMiniPayParams): void {
if (this._unitPriceQuantity === 0) {
warn("请先调用 init 方法初始化");
res.fail({ errCode: MiniErrorCode.PAY_NOT_INIT.code, errMsg: MiniErrorCode.PAY_NOT_INIT.msg });
return;
}
if (!this.isPayable(res.rmb)) {
res.fail({ errCode: -15016, errMsg: "传入价格不满足限定条件" });
return;
}
let platform = MiniHelper.common().getPlatform();
log(`BytedancePay rmb:${res.rmb}元 orderId:${res.orderId} sandbox:${res.sandbox || 0} shopId:${res.shopId} shopName:${res.shopName}`);
if (platform === "android") {
this.payAndroid(res);
} else if (platform === "ios") {
this.payIos(res);
} else {
res.fail({ errCode: MiniErrorCode.PAY_NOT_IMPLEMENTED.code, errMsg: `${MiniErrorCode.IOS_FORBIDDEN.msg} platform:${platform}` });
}
}
private payAndroid(res: IMiniPayParams): void {
let extraInfo: Record<string, any> = {
shopId: res.shopId,
shopName: res.shopName,
sandbox: res.sandbox || 0,
}
for (let key in res.extraInfo) {
extraInfo[key] = res.extraInfo[key];
}
log("扩展参数:", JSON.stringify(extraInfo));;
tt.requestGamePayment({
mode: 'game',
env: 0,
currencyType: 'CNY',
platform: 'android',
buyQuantity: Math.floor(res.rmb * this._unitPriceQuantity),
zoneId: '1',
customId: res.orderId,
extraInfo: JSON.stringify(extraInfo),
success: (param: BytedanceMiniprogram.GeneralSuccessResult) => {
res.success({ code: 0, message: param.errMsg });
},
fail: (param: BytedanceMiniprogram.GeneralFailCodeResult) => {
warn(`BytedancePay fail code:${param.errCode} msg:${param.errMsg}`);
res.fail({ errCode: param.errCode, errMsg: param.errMsg });
}
});
}
private payIos(res: IMiniPayParams): void {
let appname = MiniHelper.common<BytedanceCommon>().getHostName();
if (appname != DouyinMiniAppName.Douyin && appname != DouyinMiniAppName.DouyinLite) {
res.fail({ errCode: MiniErrorCode.PAY_NOT_IMPLEMENTED.code, errMsg: `${MiniErrorCode.PAY_NOT_IMPLEMENTED.msg} 宿主:${appname}` });
return;
}
if (!tt.openAwemeCustomerService || Utils.compareVersion(MiniHelper.common().getLibVersion(), "2.64.0") < 0) {
res.fail({ errCode: MiniErrorCode.VERSION_LOW.code, errMsg: "抖音版本号过低,请升级后再试" });
return;
}
let extraInfo = {
shopId: res.shopId,
shopName: res.shopName,
sandbox: res.sandbox || 0,
}
if (res.extraInfo) {
// 合并extraInfo和res.extraInfo
extraInfo = { ...extraInfo, ...res.extraInfo };
}
tt.openAwemeCustomerService({
currencyType: "CNY",
buyQuantity: Math.floor(res.rmb * this._unitPriceQuantity),
zoneId: '1',
/** 游戏唯一订单号 */
customId: res.orderId,
/** 游戏开发者自定义的其他信息 字符串长度最大不能超过 256。*/
extraInfo: JSON.stringify(extraInfo),
success: (params: BytedanceMiniprogram.GeneralSuccessResult) => {
res.success({ code: 0, message: params.errMsg });
},
fail: (params: BytedanceMiniprogram.GeneralFailResult) => {
warn(`BytedancePay fail code:${params.errNo} msg:${params.errMsg}`);
res.fail({ errCode: params.errNo, errMsg: params.errMsg });
}
});
}
}

24
src/minigame/header.ts Normal file
View File

@ -0,0 +1,24 @@
/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description:
*/
/** 记录一些错误码 */
export const MiniErrorCode = {
/** 支付未初始化 */
PAY_NOT_INIT: { code: -96001, msg: "支付未初始化" },
/** ios禁止支付 */
IOS_FORBIDDEN: { code: -96002, msg: "ios禁止支付" },
/** 当前平台未实现支付 */
PAY_NOT_IMPLEMENTED: { code: -96003, msg: "当前平台未实现支付" },
/** 版本号低 */
VERSION_LOW: { code: -96004, msg: "版本号过低" },
/** 广告未初始化 */
AD_NOT_INIT: { code: -97001, msg: "广告未初始化, 需要先调用init方法初始化" },
/** 广告中途退出*/
AD_EXIT: { code: -97002, msg: "广告中途退出" },
}
/** 统一价格限制列表 (微信、支付宝和字节 取交集) */
export const PriceLimitList = [1, 3, 6, 8, 12, 18, 25, 30, 40, 45, 50, 60, 68, 73, 78, 88, 98, 108, 118, 128, 148, 168, 188, 198, 328, 648, 998, 1998, 2998];

View File

@ -0,0 +1,20 @@
/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description: 广
*/
/** 激励视频广告 */
export interface IMiniRewardAds {
/**
* 广
* @param adUnitId 广ID
* 广
*/
init(adUnitId: string): void;
/**
* 广
*/
showAds(res: { success: () => void, fail: (errCode: number, errMsg: string) => void }): void;
}

View File

@ -0,0 +1,53 @@
/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description:
*/
export interface IMiniCommon {
/**
*
*/
getLaunchOptions(): Record<string, any>;
/**
*
*/
getHotLaunchOptions(): Record<string, any>;
/**
*
*/
getLibVersion(): string;
/**
* ios | android | ohos | windows | mac | devtools | iPad
* iPad ios
*/
getPlatform(): 'ios' | 'android' | 'ohos' | 'windows' | 'mac' | 'devtools' | 'iPad';
/**
*
* release | debug
*/
getEnvType(): 'release' | 'debug';
/**
* 宿 ()
*/
getHostVersion(): string;
/**
*
*/
getScreenSize(): { width: number, height: number };
/**
* 退
*/
exitMiniProgram(): void;
/**
*
*/
setClipboardData(text: string): void;
}

View File

@ -0,0 +1,69 @@
/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description:
*/
export interface IMiniPayParams {
/**
* ()
*/
rmb: number;
/**
*
*/
orderId: string;
/**
* 0: 正式环境 1: 沙盒环境
*/
sandbox?: 0 | 1;
/**
* ID
*/
shopId: string;
/**
*
*/
shopName: string;
/**
*
*/
extraInfo?: Record<string, any>;
/**
*
* @param res.code
* @param res.message
*/
success: (res: { code: number, message: string }) => void;
/**
*
* @param res.errCode
* @param res.errMsg
*/
fail: (res: { errCode: number, errMsg: string }) => void;
}
export interface IMiniPay {
/**
* (null)
* @param offerId
* @param unitPriceQuantity 1 /
*/
init(offerId: string, unitPriceQuantity: number): void;
/**
*
* @param rmb ()
* @returns
*/
isPayable(rmb: number): boolean;
/**
*
*/
pay(res: IMiniPayParams): void;
}

View File

@ -0,0 +1,78 @@
/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description: 广
*/
import { warn } from "../../kunpocc";
import { MiniErrorCode } from "../header";
import { IMiniRewardAds } from "../interface/IMiniAds";
export class WechatAds implements IMiniRewardAds {
private _adUnitId: string = "";
private _video_ad: WechatMiniprogram.RewardedVideoAd = null;
/**
* 广
*/
private _success: () => void;
/**
* 广
*/
private _fail: (errCode: number, errMsg: string) => void;
public init(adUnitId: string): void {
this._adUnitId = adUnitId;
}
/**
* 广
*/
public showAds(res: { success: () => void, fail: (errCode: number, errMsg: string) => void }): void {
if (this._adUnitId === "") {
warn(MiniErrorCode.AD_NOT_INIT.msg);
res.fail(MiniErrorCode.AD_NOT_INIT.code, MiniErrorCode.AD_NOT_INIT.msg);
return;
}
this._success = res.success;
this._fail = res.fail;
if (!this._video_ad) {
this._video_ad = this.createVideoAd();
}
this._video_ad.load().then(() => {
this._video_ad.show();
}).catch((res: { errMsg: string; errNo: number }) => {
warn(`广告加载失败 errCode:${res.errNo} errMsg:${res.errMsg}`);
this._fail(res.errNo, res.errMsg);
this.reset();
});
}
private createVideoAd(): WechatMiniprogram.RewardedVideoAd {
let videoAd = wx.createRewardedVideoAd({ adUnitId: this._adUnitId });
/** 激励视频错误事件的监听函数 */
videoAd.onError((res: WechatMiniprogram.RewardedVideoAdOnErrorListenerResult) => {
warn(`激励视频广告 onError:${res.errCode}:${res.errMsg}`);
this._fail(res.errCode, res.errMsg);
this.reset();
});
videoAd.onClose((res: WechatMiniprogram.RewardedVideoAdOnCloseListenerResult) => {
if ((res && res.isEnded) || res === undefined) {
/** 广告播放完成 */
this?._success();
} else {
/** 中途退出,不发放奖励 */
this?._fail(MiniErrorCode.AD_EXIT.code, MiniErrorCode.AD_EXIT.msg);
}
this.reset();
});
return videoAd;
}
/** 防止多次回调 */
private reset(): void {
this._success = null;
this._fail = null;
}
}

View File

@ -0,0 +1,208 @@
/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description:
*/
import { warn } from "../../tool/log";
import { IMiniCommon } from "../interface/IMiniCommon";
export class WechatCommon implements IMiniCommon {
private _launchOptions: WechatMiniprogram.LaunchOptionsApp = null;
private _accountInfo: WechatMiniprogram.AccountInfo = null;
/** 基础库 2.25.3 开始支持的信息 */
private _appBaseInfo: WechatMiniprogram.AppBaseInfo = null;
private _deviceInfo: WechatMiniprogram.DeviceInfo = null;
private _windowInfo: WechatMiniprogram.WindowInfo = null;
/** 从基础库 2.20.1 开始,本接口停止维护 */
private _systemInfo: WechatMiniprogram.SystemInfo = null;
/**
* @internal
*/
constructor() {
// 获取冷启动参数
this._launchOptions = wx.getLaunchOptionsSync();
}
/**
*
*/
public getLaunchOptions(): WechatMiniprogram.LaunchOptionsApp {
return this._launchOptions;
}
/**
*
*/
public getHotLaunchOptions(): WechatMiniprogram.LaunchOptionsApp {
return wx.getEnterOptionsSync();
}
/**
*
*/
public getLibVersion(): string {
return this.getAppBaseInfo()?.SDKVersion || "0.0.1";
}
/**
* 宿 ()
*/
public getHostVersion(): string {
return this.getAppBaseInfo()?.version || "0.0.1";
}
/**
*
*/
public getPlatform(): 'ios' | 'android' | 'ohos' | 'windows' | 'mac' | 'devtools' {
return this.getDeviceInfo().platform as ('ios' | 'android' | 'ohos' | 'windows' | 'mac' | 'devtools');
}
/**
*
*/
public getEnvType(): 'release' | 'debug' {
return this.getVersionInfo().miniProgram.envVersion == "release" ? "release" : "debug";
}
/**
* 退
*/
public exitMiniProgram(): void {
wx.exitMiniProgram?.();
}
public getScreenSize(): { width: number, height: number } {
const windowInfo = this.getWindowInfo();
return {
width: windowInfo.screenWidth,
height: windowInfo.screenHeight,
};
}
/**
*
*/
public setClipboardData(text: string): void {
wx.setClipboardData({
data: text,
fail: (res: WechatMiniprogram.GeneralCallbackResult) => {
warn("复制到剪切板失败", res.errMsg);
}
});
}
private getAppBaseInfo(): WechatMiniprogram.AppBaseInfo {
if (this._appBaseInfo) {
return this._appBaseInfo;
}
if (wx.getAppBaseInfo) {
this._appBaseInfo = wx.getAppBaseInfo();
return this._appBaseInfo;
}
const systemInfo = this.getSystemInfo();
if (systemInfo) {
this._appBaseInfo = {
SDKVersion: systemInfo.SDKVersion,
enableDebug: systemInfo.enableDebug,
host: systemInfo.host,
language: systemInfo.language,
version: systemInfo.version,
theme: systemInfo.theme,
}
return this._appBaseInfo;
}
warn("getAppBaseInfo 失败");
return null;
}
private getVersionInfo(): WechatMiniprogram.AccountInfo {
if (this._accountInfo) {
return this._accountInfo;
}
if (wx.getAccountInfoSync) {
this._accountInfo = wx.getAccountInfoSync();
return this._accountInfo;
}
warn("getVersionInfo 失败");
return {
miniProgram: {
envVersion: "release",
appId: "unknown",
version: "0.0.1",
},
plugin: {
appId: "unknown",
version: "0.0.1",
},
};
}
public getDeviceInfo(): WechatMiniprogram.DeviceInfo {
if (this._deviceInfo) {
return this._deviceInfo;
}
if (wx.getDeviceInfo) {
this._deviceInfo = wx.getDeviceInfo();
return this._deviceInfo;
}
const systemInfo = this.getSystemInfo();
if (systemInfo) {
this._deviceInfo = {
abi: "unknown",
benchmarkLevel: systemInfo.benchmarkLevel,
brand: systemInfo.brand,
cpuType: "unknown",
deviceAbi: "unknown",
memorySize: "unknown",
model: systemInfo.model,
platform: systemInfo.platform,
system: systemInfo.system,
}
return this._deviceInfo;
}
warn("getDeviceInfo 失败");
return null;
}
public getWindowInfo(): WechatMiniprogram.WindowInfo {
if (this._windowInfo) {
return this._windowInfo;
}
if (wx.getWindowInfo) {
this._windowInfo = wx.getWindowInfo();
return this._windowInfo;
}
const systemInfo = this.getSystemInfo();
if (systemInfo) {
this._windowInfo = {
pixelRatio: systemInfo.pixelRatio,
safeArea: systemInfo.safeArea,
screenHeight: systemInfo.screenHeight,
screenTop: 0,
screenWidth: systemInfo.screenWidth,
statusBarHeight: systemInfo.statusBarHeight,
windowHeight: systemInfo.windowHeight,
windowWidth: systemInfo.windowWidth,
}
}
warn("getWindowInfo 失败");
return null;
}
private getSystemInfo(): WechatMiniprogram.SystemInfo {
if (this._systemInfo) {
return this._systemInfo;
}
if (wx.getSystemInfoSync) {
this._systemInfo = wx.getSystemInfoSync();
return this._systemInfo;
}
warn("getSystemInfo 失败");
return null;
}
}

View File

@ -0,0 +1,67 @@
/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description:
*/
import { log, warn } from "../../tool/log";
import { MiniErrorCode, PriceLimitList } from "../header";
import { IMiniPay, IMiniPayParams } from "../interface/IMiniPay";
import { MiniHelper } from "../MiniHelper";
export class WechatPay implements IMiniPay {
private _offerId: string = "";
private _unitPriceQuantity: number = 1;
public init(offerId: string, unitPriceQuantity: number): void {
this._offerId = offerId;
this._unitPriceQuantity = unitPriceQuantity;
}
public isPayable(rmb: number): boolean {
return PriceLimitList.includes(rmb);
}
public pay(res: IMiniPayParams): void {
if (this._offerId === "") {
warn("请先调用 init 方法初始化");
res.fail({ errCode: MiniErrorCode.PAY_NOT_INIT.code, errMsg: MiniErrorCode.PAY_NOT_INIT.msg });
return;
}
if (!this.isPayable(res.rmb)) {
res.fail({ errCode: -15016, errMsg: "传入价格不满足限定条件" });
return;
}
let platform = MiniHelper.common().getPlatform();
if (platform === "ios") {
res.fail({ errCode: MiniErrorCode.IOS_FORBIDDEN.code, errMsg: MiniErrorCode.IOS_FORBIDDEN.msg });
return;
}
if (platform === "windows" || platform === "mac") {
platform = "windows";
} else {
platform = "android";
}
log(`WechatPay rmb:${res.rmb}元 orderId:${res.orderId} sandbox:${res.sandbox || 0} shopId:${res.shopId} shopName:${res.shopName}`);
wx.requestMidasPayment({
mode: "game",
/** 沙箱环境 */
env: res.sandbox || 0,
offerId: this._offerId,
currencyType: "CNY",
platform: platform,
buyQuantity: res.rmb * this._unitPriceQuantity,
zoneId: "1",
/** 游戏唯一订单号 */
outTradeNo: res.orderId,
success: (param: { errMsg: string }) => {
res.success({ code: 0, message: param.errMsg });
},
fail: (param: { errCode: number, errMsg: string, errno: number }) => {
warn(`WechatPay fail code:${param.errCode} msg:${param.errMsg} errno:${param.errno}`);
res.fail({ errCode: param.errCode, errMsg: param.errMsg });
}
});
}
}

View File

@ -4,7 +4,6 @@
* @Description: socket
*/
import { SocketTask } from "@douyin-microapp/typings/types/api";
import { Platform } from "../../global/Platform";
import { debug, warn } from "../../tool/log";
@ -36,7 +35,7 @@ export class Socket {
* socket对象
* @internal
*/
private _socket: WebSocket | WechatMiniprogram.SocketTask | AliyMiniprogram.SocketTask | SocketTask;
private _socket: WebSocket | WechatMiniprogram.SocketTask | AliyMiniprogram.SocketTask | BytedanceMiniprogram.SocketTask;
/**
* @param {string} url URL WebSocket URL
@ -129,8 +128,8 @@ export class Socket {
* socket
* @internal
*/
private createBytedanceSocket(url: string, timeout?: number, protocols?: string[]): SocketTask {
let socket: SocketTask = tt.connectSocket({
private createBytedanceSocket(url: string, timeout?: number, protocols?: string[]): BytedanceMiniprogram.SocketTask {
let socket: BytedanceMiniprogram.SocketTask = tt.connectSocket({
url,
protocols: protocols,
success: () => { debug("socket success") },
@ -208,7 +207,7 @@ export class Socket {
} else if (Platform.isAlipay) {
(this._socket as AliyMiniprogram.SocketTask).send({ data: data });
} else if (Platform.isBytedance) {
(this._socket as SocketTask).send({ data: data });
(this._socket as BytedanceMiniprogram.SocketTask).send({ data: data });
} else {
(this._socket as WebSocket).send(data);
}
@ -230,7 +229,7 @@ export class Socket {
(this._socket as AliyMiniprogram.SocketTask).send({ data: data });
}
} else if (Platform.isBytedance) {
(this._socket as SocketTask).send({ data: data });
(this._socket as BytedanceMiniprogram.SocketTask).send({ data: data });
} else {
(this._socket as WebSocket).send(data);
}
@ -247,7 +246,7 @@ export class Socket {
} else if (Platform.isAlipay) {
(this._socket as AliyMiniprogram.SocketTask).close({ code: code, reason: reason });
} else if (Platform.isBytedance) {
(this._socket as SocketTask).close({ code: code, reason: reason });
(this._socket as BytedanceMiniprogram.SocketTask).close({ code: code, reason: reason });
} else {
(this._socket as WebSocket).close(code, reason);
}

39
src/tool/Utils.ts Normal file
View File

@ -0,0 +1,39 @@
/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description:
*/
export class Utils {
/**
*
* @param version1
* @param version2
* 0version1大于version2
* 0version1等于version2
* 0version1小于version2
*/
public static compareVersion(version1: string, version2: string): number {
let v1 = version1.split('.');
let v2 = version2.split('.');
const len = Math.max(v1.length, v2.length);
while (v1.length < len) {
v1.push('0');
}
while (v2.length < len) {
v2.push('0');
}
for (let i = 0; i < len; ++i) {
let num1 = parseInt(v1[i]);
let num2 = parseInt(v2[i]);
if (num1 > num2) {
return 1;
} else if (num1 < num2) {
return -1;
}
}
return 0;
}
}

View File

@ -14,7 +14,8 @@
"@cocos/creator-types/editor",
"./libs/lib.wx.api.d.ts",
"./libs/lib.ali.api.d.ts",
"@douyin-microapp/typings"
"./libs/lib.bytedance.api.d.ts",
// "@douyin-microapp/typings"
]
},
"include": [