miniprogram

package
v0.5.1-nightly.20261204 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

View Source
var ErrCancelled = errors.New("miniprogram: cancelled")

ErrCancelled is what a share or a payment answers when the user cancelled it.

View Source
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.

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

func OnShareRequest(cx *core.Scope, fn func() ShareCard)

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

func OnShow(cx *core.Scope, fn func(Launch))

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

func RequestPayment(cx *core.Scope, p Payment, done func(error))

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

func SetCloudStorage(cx *core.Scope, kv map[string]string, done func(error))

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

func Share(cx *core.Scope, card ShareCard, done func(error))

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

func ShareTicketInfo(cx *core.Scope, ticket string, done func(ShareInfo, error))

ShareTicketInfo answers a WeChat group share ticket's encrypted data, for the app's server to decrypt.

func ShowRanking

func ShowRanking(cx *core.Scope, r Ranking) error

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

func ShowShareMenu(cx *core.Scope, withShareTicket bool, done func(error))

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.

func (*BannerAd) Hide

func (b *BannerAd) Hide()

Hide hides the banner.

func (*BannerAd) Load

func (b *BannerAd) Load(done func(error))

Load makes the banner and answers nil once its ad has loaded.

func (*BannerAd) OnError

func (b *BannerAd) OnError(fn func(error))

OnError runs fn with the host's failures to load or show the banner.

func (*BannerAd) OnResize

func (b *BannerAd) OnResize(fn func(width, height core.Points))

OnResize runs fn with the banner's size once the host scales it to its width.

func (*BannerAd) Show

func (b *BannerAd) Show(done func(error))

Show shows the banner and answers nil once it shows.

type BannerFrame

type BannerFrame struct {
	Left, Top, Width core.Points
}

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) Destroy

func (b *GameClubButton) Destroy()

Destroy removes the button.

func (*GameClubButton) Hide

func (b *GameClubButton) Hide()

Hide hides the button.

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.

func (*GameClubButton) Show

func (b *GameClubButton) Show()

Show shows the button.

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

type HostError struct {
	API     string
	Code    int
	Message string
}

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).

func (*HostError) Error

func (e *HostError) Error() string

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.

const (
	// None is every target but the mini program and mini game ones.
	None Kind = iota
	// MiniProgram is a mini program: the miniprogram_* targets.
	MiniProgram
	// MiniGame is a mini game: the minigame_* targets.
	MiniGame
)

The kinds an app runs as.

func (Kind) String

func (k Kind) String() string

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 names the group a WeChat share card was opened from, for ShareTicketInfo and a group ranking.
	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.

func LaunchOptions

func LaunchOptions(cx *core.Scope) (Launch, error)

LaunchOptions answers what the app launched with: the scene, the query of the share card or link that opened it, a WeChat group's share ticket and the referrer. Each return to the foreground goes to OnShow.

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.

const (
	WeChat Platform = "wechat"
	QQ     Platform = "qq"
	Alipay Platform = "alipay"
	Douyin Platform = "douyin"
)

The platforms NSGo builds mini programs and mini games for.

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 ranks a WeChat group's members instead of the user's friends.
	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 Referrer

type Referrer struct {
	AppID string
	// ExtraData is the data it passed, as JSON.
	ExtraData string
}

Referrer is the mini program, official account or app that opened this one.

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 ShareCard

type ShareCard struct {
	// Title is the card's title; empty for the platform's (the app's name).
	Title string
	// ImageURL is the card's picture, a URL or a package path (5:4 on WeChat); empty for the platform's. Douyin's mini games take the picture from the developer console and ignore it.
	ImageURL string
	// TemplateID is a share material the platform approved: WeChat's imageUrlId, Douyin's templateId.
	TemplateID string
	// Query is what the app opens with from the card, key1=val1&key2=val2, which Launch.Query and OnShow hand back.
	Query string
}

ShareCard is what a share shows and opens the app with.

type ShareInfo

type ShareInfo struct {
	EncryptedData, IV string
	// CloudID is set for mini programs with WeChat's cloud development.
	CloudID string
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL