Documentation
¶
Overview ¶
Package miniprogram reaches the host APIs WeChat's and Douyin's mini programs and mini games are built around for growth and revenue: the options the app launched or came back with, share cards and the host's share menu, a group's share ticket, the user's hosted data and the friend ranking the open data context draws, rewarded, interstitial and banner ads, virtual payments, subscribe messages and the game club button.
Current tells whether the app runs as a mini program or a mini game, and in which platform's app, on every target.
Each call answers ErrUnsupported, without asking the host, where cx.Env().HasMiniProgramAPI is false for its API: on every other target, on QQ and Alipay, and where the platform does not document the API for a mini program or a mini game (docs/public/en/miniprogram-host.md lists which). Answers run on cx's engine goroutine through core.Scope.Post, and none runs once cx is disposed.
Index ¶
- Variables
- func OnShareRequest(cx *core.Scope, fn func() ShareCard)
- func OnShow(cx *core.Scope, fn func(Launch))
- func RequestPayment(cx *core.Scope, p Payment, done func(error))
- func RequestSubscribeMessage(cx *core.Scope, templateIDs []string, ...)
- func SetCloudStorage(cx *core.Scope, kv map[string]string, done func(error))
- func Share(cx *core.Scope, card ShareCard, done func(error))
- func ShareTicketInfo(cx *core.Scope, ticket string, done func(ShareInfo, error))
- func ShowRanking(cx *core.Scope, r Ranking) error
- func ShowShareMenu(cx *core.Scope, withShareTicket bool, done func(error))
- type BannerAd
- type BannerFrame
- type Container
- type GameClubButton
- type GameClubOptions
- type HostError
- type InterstitialAd
- type Kind
- type Launch
- type Payment
- type Platform
- type Ranking
- type Referrer
- type RewardedAd
- type ShareCard
- type ShareInfo
- type SubscribeStatus
Constants ¶
This section is empty.
Variables ¶
var ErrCancelled = errors.New("miniprogram: cancelled")
ErrCancelled is what a share or a payment answers when the user cancelled it.
var ErrUnsupported = fmt.Errorf("miniprogram: the host has no such API: %w", errors.ErrUnsupported)
ErrUnsupported is what every call answers where the host has no such API; errors.Is(err, errors.ErrUnsupported) holds too.
var RankingImage = core.ImageSource{URI: driver.MiniProgramRankingURI}
RankingImage is the source of an image view that shows the friend ranking ShowRanking asks for: a mini game's screen painter draws the open data context's shared canvas there, and again every frame while the view shows, as the context draws when the friends' data arrives. The platforms allow the shared canvas onto the screen canvas only, so the ranking ignores TintColor and leaves nsgo dev -automation's screenshots blank where it shows.
Functions ¶
func OnShareRequest ¶
OnShareRequest makes fn's card what the host's share menu shares while cx lives: fn runs at once and again whenever the signals it reads change, as the host asks for the card synchronously, so the menu shares the latest. In a mini program the card opens the app's page with its Query. It shows the menu's share item, which stays hidden until the app calls OnShareRequest or ShowShareMenu where the platform leaves that to the app (WeChat, and Douyin's mini programs below base library 2.91.0), and hides it again when cx ends unless ShowShareMenu asked for it. Without a card, the menu shares the app with the platform's title and picture.
func OnShow ¶
OnShow runs fn with what the app came back to the foreground with, each time it does while cx lives: a warm start from a share card carries its query here.
func RequestPayment ¶
RequestPayment asks the host for the virtual payment p describes and answers done: nil once the platform reports it paid, which the app's server confirms with the platform before delivering, else a core.PurchaseError: core.PurchaseCancelled (wrapped with ErrCancelled) when the user cancels, core.PurchaseFailed wrapping the HostError otherwise, and core.PurchasesUnavailable with ErrUnsupported where there is no such API.
func RequestSubscribeMessage ¶
func RequestSubscribeMessage(cx *core.Scope, templateIDs []string, done func(map[string]SubscribeStatus, error))
RequestSubscribeMessage asks the user to subscribe to the message templates (WeChat takes up to 5, Douyin 3, all one-off or all long-term), which it may only do after a tap or a payment, and answers each template's status.
func SetCloudStorage ¶
SetCloudStorage writes the user's hosted key-value data in a mini game, which the friend ranking reads: at most 128 keys a user, a key at most 128 bytes and a key and value together at most 1024. A ranked value is JSON whose wxgame (WeChat) or ttgame (Douyin) object holds score and update_time.
func Share ¶
Share opens the host's share sheet for card from the app's own button, in a mini game. WeChat reports nothing back, so done gets nil once the sheet is asked for; Douyin answers its result, ErrCancelled when the user closes the sheet. A mini program shares only through its menu (OnShareRequest).
func ShareTicketInfo ¶
ShareTicketInfo answers a WeChat group share ticket's encrypted data, for the app's server to decrypt.
func ShowRanking ¶
ShowRanking has the open data context draw the friend ranking r asks for, which an image view whose Source is RankingImage shows at r's size. It needs nsgo.yaml's minigame.friend_ranking, which declares the context.
func ShowShareMenu ¶
ShowShareMenu shows the share item in the host's menu for the rest of the run, on WeChat with a group's share ticket in what a card shared to a group launches with when withShareTicket. Until the app calls it or OnShareRequest, NSGo keeps the item hidden where the platform does (WeChat, and Douyin's mini programs below base library 2.91.0); elsewhere the platform decides, and Douyin's mini programs show it on every page from 2.91.0.
Types ¶
type BannerAd ¶
type BannerAd struct {
// contains filtered or unexported fields
}
BannerAd is a banner ad the host draws over the game at its frame, in a mini game.
func NewBannerAd ¶
func NewBannerAd(cx *core.Scope, unitID string, frame BannerFrame) *BannerAd
NewBannerAd makes a banner ad of the platform's ad unit at frame, destroyed with cx. It loads as Load or Show first makes it.
func (*BannerAd) Destroy ¶
func (b *BannerAd) Destroy()
Destroy removes the banner; its calls then fail.
type BannerFrame ¶
BannerFrame places a banner ad, in points: its left edge, top and width, which the platform scales its height to.
type Container ¶
type Container struct {
Kind Kind
// Platform is the host app, "" when Kind is None.
Platform Platform
}
Container is what the app runs as and in.
func Current ¶
func Current() Container
Current answers what the app runs as and in: Kind MiniGame or MiniProgram with its Platform in a build for a minigame_* or miniprogram_* target, the zero Container on every other target. It holds for the whole run, so an app can branch on it anywhere, such as to leave out links to outside the app that WeChat's review refuses in mini programs and mini games.
type GameClubButton ¶
type GameClubButton struct {
// contains filtered or unexported fields
}
GameClubButton is a WeChat mini game's game club button, which the host draws over the game and which opens the game's club.
func NewGameClubButton ¶
func NewGameClubButton(cx *core.Scope, o GameClubOptions) (*GameClubButton, error)
NewGameClubButton makes the game club button o places, shown until Hide or Destroy, and destroyed with cx; ErrUnsupported where the host has none.
func (*GameClubButton) OnTap ¶
func (b *GameClubButton) OnTap(fn func())
OnTap runs fn each time the user taps the button, as the host opens the club.
type GameClubOptions ¶
type GameClubOptions struct {
// Icon is WeChat's: green (the default), white, dark or light.
Icon string
Left, Top, Width, Height core.Points
// OpenLink opens a post, topic or event page of the club, the ID WeChat's MP console gives for it.
OpenLink string
}
GameClubOptions place the game club button, which the host draws over the game, in points.
type HostError ¶
HostError is a host API's failure: its name, the platform's code (errCode, errNo or errno, 0 when it gave none) and its message (errMsg).
type InterstitialAd ¶
type InterstitialAd struct {
// contains filtered or unexported fields
}
InterstitialAd is an interstitial ad, shown between the game's scenes.
func NewInterstitialAd ¶
func NewInterstitialAd(cx *core.Scope, unitID string) *InterstitialAd
NewInterstitialAd makes an interstitial ad of the platform's ad unit, destroyed with cx.
func (*InterstitialAd) Destroy ¶
func (i *InterstitialAd) Destroy()
Destroy releases the ad; its calls then fail.
func (*InterstitialAd) Load ¶
func (i *InterstitialAd) Load(done func(error))
Load fetches an ad, answering nil once one is ready to show.
func (*InterstitialAd) OnClose ¶
func (i *InterstitialAd) OnClose(fn func())
OnClose runs fn when the user closes the ad.
func (*InterstitialAd) OnError ¶
func (i *InterstitialAd) OnError(fn func(error))
OnError runs fn with the host's failures to load or show an ad.
func (*InterstitialAd) Show ¶
func (i *InterstitialAd) Show(done func(error))
Show shows the ad, loading one and trying once more if none is ready, and answers nil once it shows.
type Kind ¶
type Kind int
Kind is what an app runs as: a mini program, a mini game or neither.
type Launch ¶
type Launch struct {
// Scene is the platform's scene value: WeChat's number in decimal (1007 for a chat's share card), Douyin's string as it is.
Scene string
// Query is the query a share card or a link opened the app with, such as a ShareCard's.
Query map[string]string
ShareTicket string
// Referrer is the mini program, official account or app that opened this one.
Referrer Referrer
// Path is the code package path a mini program opened at; empty in a mini game.
Path string
}
Launch is what the app launched or came back to the foreground with.
type Payment ¶
type Payment struct {
Params map[string]any
// Item buys an item directly in a WeChat mini game (requestMidasPaymentGameItem) rather than game currency.
Item bool
}
Payment is a virtual payment's request in the platform's own fields, which the app's server signs: WeChat's mini games' requestMidasPayment (mode, env, offerId, currencyType, buyQuantity, outTradeNo, ...) or, with Item, requestMidasPaymentGameItem (signData, paySig, signature); WeChat's mini programs' requestVirtualPayment (signData, mode, paySig, signature); Douyin's requestGamePayment (mode, env, currencyType, platform, buyQuantity, customId, goodType, ...).
type Platform ¶
type Platform string
Platform is the app a mini program or mini game runs in, as its build target names it.
type Ranking ¶
type Ranking struct {
// Key is the hosted data key the scores are under, as SetCloudStorage wrote them: the value's wxgame.score or ttgame.score, else the number it is.
Key string
ShareTicket string
// Width and Height are the ranking's size in points: the image view's that shows it.
Width, Height core.Points
// Dark draws light text for a dark background.
Dark bool
}
Ranking asks the open data context for a friend ranking.
type RewardedAd ¶
type RewardedAd struct {
// contains filtered or unexported fields
}
RewardedAd is a rewarded video ad: the user earns the reward by watching it to its end, which OnClose reports. WeChat's mini games and Douyin's keep one per game, WeChat's mini programs one per page, so RewardedAds alive at once share it: OnClose runs only for the one that showed it last, OnError for the one that loaded or showed it last, and the host's ad is destroyed with the last of them.
func NewRewardedAd ¶
func NewRewardedAd(cx *core.Scope, unitID string) *RewardedAd
NewRewardedAd makes a rewarded video ad of the platform's ad unit, destroyed with cx.
func (*RewardedAd) Destroy ¶
func (r *RewardedAd) Destroy()
Destroy releases the ad; its calls then fail.
func (*RewardedAd) Load ¶
func (r *RewardedAd) Load(done func(error))
Load fetches an ad, answering nil once one is ready to show; ErrUnsupported where the host has none, a HostError otherwise (WeChat's 1004 when it has no ad for the user, which is no fault).
func (*RewardedAd) OnClose ¶
func (r *RewardedAd) OnClose(fn func(completed bool))
OnClose runs fn when the user closes the ad, completed when they watched it to its end and so earned the reward.
func (*RewardedAd) OnError ¶
func (r *RewardedAd) OnError(fn func(error))
OnError runs fn with the host's failures to load or show an ad.
func (*RewardedAd) Show ¶
func (r *RewardedAd) Show(done func(error))
Show shows the ad, loading one and trying once more if none is ready, and answers nil once it shows.
type ShareInfo ¶
type ShareInfo struct {
ShareInfo is a group share ticket's data, encrypted for the app's server, which holds the session key that decrypts it (WeChat's openGId).
type SubscribeStatus ¶
type SubscribeStatus string
SubscribeStatus is the user's answer for one subscribe message template.
const ( SubscribeAccept SubscribeStatus = "accept" SubscribeAcceptWithAudio SubscribeStatus = "acceptWithAudio" SubscribeAcceptWithAlert SubscribeStatus = "acceptWithAlert" SubscribeReject SubscribeStatus = "reject" SubscribeBan SubscribeStatus = "ban" SubscribeFilter SubscribeStatus = "filter" // SubscribeFail is Douyin's for a template it could not ask about. SubscribeFail SubscribeStatus = "fail" )
The statuses the platforms answer.
func (SubscribeStatus) Accepted ¶
func (s SubscribeStatus) Accepted() bool
Accepted reports whether the user subscribed, so the app's server may send the template's message.