服务 / 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 |
获取Ark币余额 |
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
• | ||
---|---|---|
获取 Ark 币余额更新的委托 Precautions 只在支持 IAP 的 233/Playza/口袋方舟 内使用时生效 Returns
|
onOrderDelivered
• | ||
---|---|---|
获取订单发货的委托 Precautions
Returns
|
onPremiumMemberOrderDelivered
• | ||
---|---|---|
获取订单发货的委托 Precautions
Returns
|
onPremiumMemberStatusUpdate
• |
---|
获取大会员状态更新时触发的委托 Precautions 暂未考虑单机模式,只在支持大会员的 233/Playza/口袋方舟 内使用时生效 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 大会员扣除钥匙下单状态 |
Precautions
暂未考虑单机模式,只在支持大会员的 233/Playza/口袋方舟 内使用时生效
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
获取Ark币余额
Precautions
只在支持IAP的233/Playza/口袋方舟内使用时生效
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 = await mw.Player.localPlayer;
await TimeUtil.delaySecond(5);
let arkCount = PurchaseService.getArkBalance();
player.character.name = "ArkCount: " + arkCount;
}
}
@Component
export default class PurchaseExample extends mw.Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): `Promise`<`void`\> {
let player = await mw.Player.localPlayer;
await TimeUtil.delaySecond(5);
let arkCount = PurchaseService.getArkBalance();
player.character.name = "ArkCount: " + arkCount;
}
}
getUserKeyNumber
• Static
getUserKeyNumber(getUserKeyNumberResult
, keyType?
): void
client
获取用户剩余钥匙数量
Parameters
getUserKeyNumberResult (keyNumber : number ) => void | 结果回调,查询到结果后执行回调函数。keyNumber : 剩余钥匙数量 |
---|---|
keyType? number | 搜索的钥匙类型,暂时只有类型 1 default: 1 range: 1 type: 整数 |
Precautions
暂未考虑单机模式,只在支持大会员的 233/Playza/口袋方舟 内使用时生效
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
暂未考虑单机模式,第一次结果会通过异步回调的方式返回(构造时自动请求),后续会通过缓存直接执行回调函数(同步), 只在支持大会员的233/Playza/口袋方舟内使用时生效
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
暂未考虑单机模式,第一次结果会通过异步回调的方式返回(构造时自动请求),后续会通过缓存直接执行回调函数(同步)。老版本的233乐园可能出现无回调的情况,需要自行判断超时
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 | 兑换结果的回调函数 |
01 : 兑换成功
02 : 兑换失败(兑换码不存在)
03 : 兑换失败(兑换码不在使用期限内)
04 : 兑换失败(兑换码已使用)
05 : 兑换失败(兑换超时)
06 : 兑换失败(礼包库存不足)
07 : 兑换失败(超出兑换次数)
RedeemResponse中message : 兑换信息
当兑换状态为成功时:兑换信息为礼包内容道具详情。
当兑换状态为失败时:兑换信息为失败详情,例如:兑换码不存在。 \
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----------")
}
}