Skip to content
PurchaseService

服务 / PurchaseService

PurchaseService Class

应用内购服务

Table of contents

Accessors

onArkBalanceUpdated(): MulticastDelegate<OnArkBalanceUpdated> client
获取 Ark 币余额更新的委托
onOrderDelivered(): MulticastDelegate<OnOrderDelivered> server
获取订单发货的委托
onPremiumMemberOrderDelivered(): MulticastDelegate<OnKeyConsume> server
获取订单发货的委托
onPremiumMemberStatusUpdate(): MulticastDelegate<(result: boolean) => void> client
获取大会员状态更新时触发的委托

Methods

consumeKey(boxId: string, number: number, keyType: number, placeOrderResult: (status: consumeKeyStatus) => void): void client
大会员开宝箱消耗金钥匙
findKeyUsageHistory(currentPage: number, pageSize: number, historyResult: (total: number, currentPage: number, bill: keyUsageInfo[]) => void): void
分页查询大会员使用钥匙的历史记录
getArkBalance(): void client
获取代币余额
getUserKeyNumber([getUserKeyNumber](mw.PurchaseService.md#getuserkeynumber)Result, keyType?: number): void client
获取用户剩余钥匙数量
isPremiumMember([isPremiumMember](mw.PurchaseService.md#ispremiummember)Result): void client
获取用户是否是大会员
isPremiumMemberSupported(isSupportedResult: (result: boolean) => void): void client
获取用户使用软件版本是否有大会员功能
openPremiumMemberPurchasePage(): void client
跳转会员充值页面
placeOrder(commodityId: string, amount: number, [placeOrder](mw.PurchaseService.md#placeorder)Result): void client
下单指定数量的指定商品
redeemGiftCode(player: Player, GiftCode: string, redeemCallback: (result: RedeemResponse) => void): void server
礼包码兑换

Accessors

onArkBalanceUpdated

Static get onArkBalanceUpdated(): MulticastDelegate<OnArkBalanceUpdated> client

获取 Ark 币余额更新的委托

Returns

MulticastDelegate<OnArkBalanceUpdated>代币余额更新的委托

onOrderDelivered

Static get onOrderDelivered(): MulticastDelegate<OnOrderDelivered> server

获取订单发货的委托

Precautions

仅在服务端有效。暂未考虑单机模式,使用需谨慎

Returns

MulticastDelegate<OnOrderDelivered>订单发货的委托

onPremiumMemberOrderDelivered

Static get onPremiumMemberOrderDelivered(): MulticastDelegate<OnKeyConsume> server

获取订单发货的委托

Precautions

仅在服务端有效。暂未考虑单机模式,使用需谨慎

Returns

MulticastDelegate<OnKeyConsume>订单发货的委托

onPremiumMemberStatusUpdate

Static get onPremiumMemberStatusUpdate(): MulticastDelegate<(result: boolean) => void> client

获取大会员状态更新时触发的委托

Returns

MulticastDelegate<(result: boolean) => void>大会员状态更新时触发的委托

Methods

consumeKey

Static consumeKey(boxId, number, keyType, placeOrderResult): void client

大会员开宝箱消耗金钥匙

Parameters

boxId string宝箱 ID,代表一种福利,暂时自定义id。后续会在开发者平台配制 range:依据 boxId 长度
number number领取的宝箱数量
default: 1 range: 不做限制 type: 整数
keyType number钥匙类型,当前只有一种钥匙类型值为 1
default: 1 range: 1 type:整数
placeOrderResult (status: consumeKeyStatus) => void订单结果。status 大会员扣除钥匙下单状态
使用示例: 需要先在服务端监听发货信息,扣除钥匙时会判断服务端是否监听
ts
if (SystemUtil.isServer()) {
     console.log("监听发货");
     //发货监听
     mw.PurchaseService.onPremiumMemberOrderDelivered.add((
          playerController: mw.Player,
          orderId: string,
          boxId: string,
          amount: number,
          confirmOrder: (bReceived: boolean) => void
     ) => {
          mw.Event.dispatchToClient(playerController, orderId, boxId, amount);
          //确认收货
          confirmOrder(true);
     });
}

// 扣除钥匙
mw.PurchaseService.consumeKey("1000", 1, 1, (status : mw.consumeKeyStatus) => {
     console.log("consumeKey status : " + status);
     if (status == mw.consumeKeyStatus.Success) {
          console.log("下单成功");
     } else if (status == mw.consumeKeyStatus.PremiumMemberNotSupported) {
          console.log("不支持大会员");
     } else if (status == mw.consumeKeyStatus.NotPremiumMember) {
          console.log("不是大会员");
     } else if (status == mw.consumeKeyStatus.InsufficientKeys) {
          console.log("钥匙不足");
     } else if (status == mw.consumeKeyStatus.Error) {
          console.log("扣除失败");
     }
});
if (SystemUtil.isServer()) {
     console.log("监听发货");
     //发货监听
     mw.PurchaseService.onPremiumMemberOrderDelivered.add((
          playerController: mw.Player,
          orderId: string,
          boxId: string,
          amount: number,
          confirmOrder: (bReceived: boolean) => void
     ) => {
          mw.Event.dispatchToClient(playerController, orderId, boxId, amount);
          //确认收货
          confirmOrder(true);
     });
}

// 扣除钥匙
mw.PurchaseService.consumeKey("1000", 1, 1, (status : mw.consumeKeyStatus) => {
     console.log("consumeKey status : " + status);
     if (status == mw.consumeKeyStatus.Success) {
          console.log("下单成功");
     } else if (status == mw.consumeKeyStatus.PremiumMemberNotSupported) {
          console.log("不支持大会员");
     } else if (status == mw.consumeKeyStatus.NotPremiumMember) {
          console.log("不是大会员");
     } else if (status == mw.consumeKeyStatus.InsufficientKeys) {
          console.log("钥匙不足");
     } else if (status == mw.consumeKeyStatus.Error) {
          console.log("扣除失败");
     }
});

findKeyUsageHistory

Static findKeyUsageHistory(currentPage, pageSize, historyResult): void

分页查询大会员使用钥匙的历史记录

Parameters

currentPage number查找第几页
type:整数型
range:(1, 65535)
pageSize number每页大小
type:整数型
range:(1, 100)
historyResult (total: number, currentPage: number, bill: keyUsageInfo[]) => void查询结果回调函数
使用示例:将代码片段放入脚本中即可
ts
mw.PurchaseService.findKeyUsageHistory(1, 100, (total : number, currentPage : number, bills : mw.keyUsageInfo[]) => {
    console.log("bill len : " + bills.length);
    for(let bill of bills)
    {
        console.log("bill orderId : " + bill.orderId);
        console.log("bill boxId : " + bill.boxId);
        console.log("bill number : " + bill.number);
        console.log("bill consumeTime : " + bill.consumeTime);
    }
});
mw.PurchaseService.findKeyUsageHistory(1, 100, (total : number, currentPage : number, bills : mw.keyUsageInfo[]) => {
    console.log("bill len : " + bills.length);
    for(let bill of bills)
    {
        console.log("bill orderId : " + bill.orderId);
        console.log("bill boxId : " + bill.boxId);
        console.log("bill number : " + bill.number);
        console.log("bill consumeTime : " + bill.consumeTime);
    }
});

getArkBalance

Static getArkBalance(): void client

获取代币余额

使用示例:创建一个名为PurchaseExample的脚本,放置在对象栏中,打开脚本,将原本内容修改为如下内容,发布游戏在移动端测试,5秒后会显示代币余额
ts
@Component
export default class PurchaseExample extends mw.Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        //在客户端注册刷新监听
        const onArkUpdate = (amount: number) => {
            //刷新逻辑,amount为当前代币数量
        }
        PurchaseService.onArkBalanceUpdated.add(onArkUpdate);

        //触发代币余额刷新。接收更新的值要用PurchaseService.onArkBalanceUpdated监听
        PurchaseService.getArkBalance();
    }
}
@Component
export default class PurchaseExample extends mw.Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        //在客户端注册刷新监听
        const onArkUpdate = (amount: number) => {
            //刷新逻辑,amount为当前代币数量
        }
        PurchaseService.onArkBalanceUpdated.add(onArkUpdate);

        //触发代币余额刷新。接收更新的值要用PurchaseService.onArkBalanceUpdated监听
        PurchaseService.getArkBalance();
    }
}

getUserKeyNumber

Static getUserKeyNumber(getUserKeyNumberResult, keyType?): void client

获取用户剩余钥匙数量

Parameters

getUserKeyNumberResult (keyNumber: number) => void结果回调,查询到结果后执行回调函数。keyNumber : 剩余钥匙数量
keyType? number搜索的钥匙类型,暂时只有类型 1
default: 1 range: 1 type: 整数
使用示例:创建一个名为PurchaseExample的脚本,放置在对象栏中,打开脚本,将原本内容修改为如下内容,发布游戏在移动端测试,可以看到当前用户剩余钥匙数量
ts
@Component
export default class PurchaseExample extends mw.Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let player = Player.localPlayer;
        PurchaseService.getUserKeyNumber(keyNumber => {
            player.character.displayName = "当前用户剩余钥匙数量:", keyNumber.toString();
        })
    }
}
@Component
export default class PurchaseExample extends mw.Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let player = Player.localPlayer;
        PurchaseService.getUserKeyNumber(keyNumber => {
            player.character.displayName = "当前用户剩余钥匙数量:", keyNumber.toString();
        })
    }
}

isPremiumMember

Static isPremiumMember(isPremiumMemberResult): void client

获取用户是否是大会员

Parameters

isPremiumMemberResult (result: boolean) => void结果回调,查询到结果后执行回调函数。true:是大会员,false:不是大会员

Precautions

暂未考虑单机模式,第一次结果会通过异步回调的方式返回(构造时自动请求),后续会通过缓存直接执行回调函数(同步),

使用示例:创建一个名为PurchaseExample的脚本,放置在对象栏中,打开脚本,将原本内容修改为如下内容,发布游戏在移动端测试,可以看到当前用户是否是大会员
ts
@Component
export default class PurchaseExample extends mw.Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let player = Player.localPlayer;
        PurchaseService.isPremiumMember(isSupport => {
            player.character.displayName = isSupport ? "当前玩家是大会员" : "当前玩家不是大会员";
        })
    }
}
@Component
export default class PurchaseExample extends mw.Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let player = Player.localPlayer;
        PurchaseService.isPremiumMember(isSupport => {
            player.character.displayName = isSupport ? "当前玩家是大会员" : "当前玩家不是大会员";
        })
    }
}

isPremiumMemberSupported

Static isPremiumMemberSupported(isSupportedResult): void client

获取用户使用软件版本是否有大会员功能

Parameters

isSupportedResult (result: boolean) => void结果回调,查询到结果后执行回调函数。true:支持大会员功能,false:不支持大会员功能

Precautions

暂未考虑单机模式,第一次结果会通过异步回调的方式返回(构造时自动请求),后续会通过缓存直接执行回调函数(同步)。老版本的App可能出现无回调的情况,需要自行判断超时

使用示例:创建一个名为PurchaseExample的脚本,放置在对象栏中,打开脚本,将原本内容修改为如下内容,发布游戏在移动端测试,可以看到当前app是否支持大会员功能
ts
@Component
export default class PurchaseExample extends mw.Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let player = Player.localPlayer;
        PurchaseService.isPremiumMemberSupported(isSupport => {
            player.character.displayName = isSupport ? "当前app支持大会员功能" : "当前app不支持大会员功能";
        })
    }
}
@Component
export default class PurchaseExample extends mw.Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let player = Player.localPlayer;
        PurchaseService.isPremiumMemberSupported(isSupport => {
            player.character.displayName = isSupport ? "当前app支持大会员功能" : "当前app不支持大会员功能";
        })
    }
}

openPremiumMemberPurchasePage

Static openPremiumMemberPurchasePage(): void client

跳转会员充值页面

使用示例:创建一个名为PurchaseExample的脚本,放置在对象栏中,打开脚本,将原本内容修改为如下内容,发布游戏在移动端测试,10秒后会自动打开会员充值页面。
ts
@Component
export default class PurchaseExample extends mw.Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        await TimeUtil.delaySecond(10);
        PurchaseService.openPremiumMemberPurchasePage();
    }
}
@Component
export default class PurchaseExample extends mw.Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        await TimeUtil.delaySecond(10);
        PurchaseService.openPremiumMemberPurchasePage();
    }
}

placeOrder

Static placeOrder(commodityId, amount, placeOrderResult): void client

下单指定数量的指定商品

Parameters

commodityId string商品Id range: 依据商品 ID 长度而定
amount number数量 range:不做限制 type:整数
placeOrderResult (status: number, msg: string) => void订单状态回调

status 含义:

status = 200: 订单支付成功

status = 408: 请求超时

status = 409: 处理下单回调报错

status = 410: 处理支付回调报错

status = 501: 余额不足

status = 502: 暂未开放购买

status = 503: amount参数类型错误


redeemGiftCode

Static redeemGiftCode(player, GiftCode, redeemCallback): void server

礼包码兑换

Parameters

player Player需要兑换的玩家对象
GiftCode string礼包码
range: 依据兑换码长度而定
redeemCallback (result: RedeemResponse) => void兑换结果的回调函数

RedeemResponse中status : 兑换状态。

200 : 兑换成功

400 : 兑换失败(兑换码不存在)

423 : 兑换失败(请求频繁)

1002 :兑换失败(兑换码不在使用期限内)

1010 :兑换失败(兑换码已使用)

1011 :兑换失败(兑换超时)

1012 :兑换失败(礼包库存不足)

1013 :兑换失败(超出兑换次数)

1014 :兑换失败(数据库失败)

1015 :兑换失败(非本游戏道具)

RedeemResponse中message : 兑换信息

当兑换状态为成功时:兑换信息为礼包内容道具详情。

当兑换状态为失败时:兑换信息为失败详情,例如:兑换码不存在。

使用示例:创建一个名为PurchaseExample的脚本,放置在对象栏中,打开脚本,将原本内容修改为如下内容,发布游戏在移动端测试,5秒后会显示代币余额
ts
@Component
export default class NewScript extends Script {
    @Property({replicated:true,multicast:true})
    public player:Player;
    @Property({replicated:true,multicast:true})
    public playerid:string

    protected onStart(): void {
        if(SystemUtil.isClient()) {
            this.playerid = Player.localPlayer.userId;
            console.log(this.playerid);
        }
        if(SystemUtil.isServer()){
            this.textFunc();
        }
    }

    @RemoteFunction(Server)
    public textFunc(){
        Player.getAllPlayers().forEach(element => {
            this.player = element;
        });
        // ""中填写兑换码
        PurchaseService.redeemGiftCode(this.player, "" ,(result:RedeemResponse)=>{
            console.log("result.message------------" + result.message);
            console.log("result.message------------" + result.status);
        });
        console.log("--------over----------")
    }
}
@Component
export default class NewScript extends Script {
    @Property({replicated:true,multicast:true})
    public player:Player;
    @Property({replicated:true,multicast:true})
    public playerid:string

    protected onStart(): void {
        if(SystemUtil.isClient()) {
            this.playerid = Player.localPlayer.userId;
            console.log(this.playerid);
        }
        if(SystemUtil.isServer()){
            this.textFunc();
        }
    }

    @RemoteFunction(Server)
    public textFunc(){
        Player.getAllPlayers().forEach(element => {
            this.player = element;
        });
        // ""中填写兑换码
        PurchaseService.redeemGiftCode(this.player, "" ,(result:RedeemResponse)=>{
            console.log("result.message------------" + result.message);
            console.log("result.message------------" + result.status);
        });
        console.log("--------over----------")
    }
}