{"modulePath":"github.com/nsgo-dev/nsgo","version":"v0.5.0","isLatest":true,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package miniprogram\n\nPackage 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.\n\nCurrent tells whether the app runs as a mini program or a mini game, and in which platform's app, on every target.\n\nEach 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.\n\n## Variables\n\n```go\nvar ErrCancelled = errors.New(\"miniprogram: cancelled\")\n```\nErrCancelled is what a share or a payment answers when the user cancelled it.\n\n```go\nvar ErrUnsupported = fmt.Errorf(\"miniprogram: the host has no such API: %w\", errors.ErrUnsupported)\n```\nErrUnsupported is what every call answers where the host has no such API; errors.Is(err, errors.ErrUnsupported) holds too.\n\n```go\nvar RankingImage = core.ImageSource{URI: driver.MiniProgramRankingURI}\n```\nRankingImage 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.\n\n## Functions\n\n```go\nfunc OnShareRequest(cx *core.Scope, fn func() ShareCard)\n```\nOnShareRequest 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.\n\n```go\nfunc OnShow(cx *core.Scope, fn func(Launch))\n```\nOnShow 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.\n\n```go\nfunc RequestPayment(cx *core.Scope, p Payment, done func(error))\n```\nRequestPayment 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.\n\n```go\nfunc RequestSubscribeMessage(cx *core.Scope, templateIDs []string, done func(map[string]SubscribeStatus, error))\n```\nRequestSubscribeMessage 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.\n\n```go\nfunc SetCloudStorage(cx *core.Scope, kv map[string]string, done func(error))\n```\nSetCloudStorage 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.\n\n```go\nfunc Share(cx *core.Scope, card ShareCard, done func(error))\n```\nShare 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).\n\n```go\nfunc ShareTicketInfo(cx *core.Scope, ticket string, done func(ShareInfo, error))\n```\nShareTicketInfo answers a WeChat group share ticket's encrypted data, for the app's server to decrypt.\n\n```go\nfunc ShowRanking(cx *core.Scope, r Ranking) error\n```\nShowRanking 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.\n\n```go\nfunc ShowShareMenu(cx *core.Scope, withShareTicket bool, done func(error))\n```\nShowShareMenu 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.\n\n## Types\n\n```go\ntype BannerAd struct {\n\t// contains filtered or unexported fields\n}\n```\nBannerAd is a banner ad the host draws over the game at its frame, in a mini game.\n\n```go\nfunc NewBannerAd(cx *core.Scope, unitID string, frame BannerFrame) *BannerAd\n```\nNewBannerAd makes a banner ad of the platform's ad unit at frame, destroyed with cx. It loads as Load or Show first makes it.\n\n```go\nfunc (b *BannerAd) Destroy()\n```\nDestroy removes the banner; its calls then fail.\n\n```go\nfunc (b *BannerAd) Hide()\n```\nHide hides the banner.\n\n```go\nfunc (b *BannerAd) Load(done func(error))\n```\nLoad makes the banner and answers nil once its ad has loaded.\n\n```go\nfunc (b *BannerAd) OnError(fn func(error))\n```\nOnError runs fn with the host's failures to load or show the banner.\n\n```go\nfunc (b *BannerAd) OnResize(fn func(width, height core.Points))\n```\nOnResize runs fn with the banner's size once the host scales it to its width.\n\n```go\nfunc (b *BannerAd) Show(done func(error))\n```\nShow shows the banner and answers nil once it shows.\n\n```go\ntype BannerFrame struct {\n\tLeft, Top, Width core.Points\n}\n```\nBannerFrame places a banner ad, in points: its left edge, top and width, which the platform scales its height to.\n\n```go\ntype Container struct {\n\tKind Kind\n\t// Platform is the host app, \"\" when Kind is None.\n\tPlatform Platform\n}\n```\nContainer is what the app runs as and in.\n\n```go\nfunc Current() Container\n```\nCurrent 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.\n\n```go\ntype GameClubButton struct {\n\t// contains filtered or unexported fields\n}\n```\nGameClubButton is a WeChat mini game's game club button, which the host draws over the game and which opens the game's club.\n\n```go\nfunc NewGameClubButton(cx *core.Scope, o GameClubOptions) (*GameClubButton, error)\n```\nNewGameClubButton makes the game club button o places, shown until Hide or Destroy, and destroyed with cx; ErrUnsupported where the host has none.\n\n```go\nfunc (b *GameClubButton) Destroy()\n```\nDestroy removes the button.\n\n```go\nfunc (b *GameClubButton) Hide()\n```\nHide hides the button.\n\n```go\nfunc (b *GameClubButton) OnTap(fn func())\n```\nOnTap runs fn each time the user taps the button, as the host opens the club.\n\n```go\nfunc (b *GameClubButton) Show()\n```\nShow shows the button.\n\n```go\ntype GameClubOptions struct {\n\t// Icon is WeChat's: green (the default), white, dark or light.\n\tIcon                     string\n\tLeft, Top, Width, Height core.Points\n\t// OpenLink opens a post, topic or event page of the club, the ID WeChat's MP console gives for it.\n\tOpenLink string\n}\n```\nGameClubOptions place the game club button, which the host draws over the game, in points.\n\n```go\ntype HostError struct {\n\tAPI     string\n\tCode    int\n\tMessage string\n}\n```\nHostError 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).\n\n```go\nfunc (e *HostError) Error() string\n```\n\n```go\ntype InterstitialAd struct {\n\t// contains filtered or unexported fields\n}\n```\nInterstitialAd is an interstitial ad, shown between the game's scenes.\n\n```go\nfunc NewInterstitialAd(cx *core.Scope, unitID string) *InterstitialAd\n```\nNewInterstitialAd makes an interstitial ad of the platform's ad unit, destroyed with cx.\n\n```go\nfunc (i *InterstitialAd) Destroy()\n```\nDestroy releases the ad; its calls then fail.\n\n```go\nfunc (i *InterstitialAd) Load(done func(error))\n```\nLoad fetches an ad, answering nil once one is ready to show.\n\n```go\nfunc (i *InterstitialAd) OnClose(fn func())\n```\nOnClose runs fn when the user closes the ad.\n\n```go\nfunc (i *InterstitialAd) OnError(fn func(error))\n```\nOnError runs fn with the host's failures to load or show an ad.\n\n```go\nfunc (i *InterstitialAd) Show(done func(error))\n```\nShow shows the ad, loading one and trying once more if none is ready, and answers nil once it shows.\n\n```go\ntype Kind int\n```\nKind is what an app runs as: a mini program, a mini game or neither.\n\n```go\nconst (\n\t// None is every target but the mini program and mini game ones.\n\tNone Kind = iota\n\t// MiniProgram is a mini program: the miniprogram_* targets.\n\tMiniProgram\n\t// MiniGame is a mini game: the minigame_* targets.\n\tMiniGame\n)\n```\nThe kinds an app runs as.\n\n```go\nfunc (k Kind) String() string\n```\n\n```go\ntype Launch struct {\n\t// 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.\n\tScene string\n\t// Query is the query a share card or a link opened the app with, such as a ShareCard's.\n\tQuery map[string]string\n\t// ShareTicket names the group a WeChat share card was opened from, for ShareTicketInfo and a group ranking.\n\tShareTicket string\n\t// Referrer is the mini program, official account or app that opened this one.\n\tReferrer Referrer\n\t// Path is the code package path a mini program opened at; empty in a mini game.\n\tPath string\n}\n```\nLaunch is what the app launched or came back to the foreground with.\n\n```go\nfunc LaunchOptions(cx *core.Scope) (Launch, error)\n```\nLaunchOptions 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.\n\n```go\ntype Payment struct {\n\tParams map[string]any\n\t// Item buys an item directly in a WeChat mini game (requestMidasPaymentGameItem) rather than game currency.\n\tItem bool\n}\n```\nPayment 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, ...).\n\n```go\ntype Platform string\n```\nPlatform is the app a mini program or mini game runs in, as its build target names it.\n\n```go\nconst (\n\tWeChat Platform = \"wechat\"\n\tQQ     Platform = \"qq\"\n\tAlipay Platform = \"alipay\"\n\tDouyin Platform = \"douyin\"\n)\n```\nThe platforms NSGo builds mini programs and mini games for.\n\n```go\ntype Ranking struct {\n\t// 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.\n\tKey string\n\t// ShareTicket ranks a WeChat group's members instead of the user's friends.\n\tShareTicket string\n\t// Width and Height are the ranking's size in points: the image view's that shows it.\n\tWidth, Height core.Points\n\t// Dark draws light text for a dark background.\n\tDark bool\n}\n```\nRanking asks the open data context for a friend ranking.\n\n```go\ntype Referrer struct {\n\tAppID string\n\t// ExtraData is the data it passed, as JSON.\n\tExtraData string\n}\n```\nReferrer is the mini program, official account or app that opened this one.\n\n```go\ntype RewardedAd struct {\n\t// contains filtered or unexported fields\n}\n```\nRewardedAd 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.\n\n```go\nfunc NewRewardedAd(cx *core.Scope, unitID string) *RewardedAd\n```\nNewRewardedAd makes a rewarded video ad of the platform's ad unit, destroyed with cx.\n\n```go\nfunc (r *RewardedAd) Destroy()\n```\nDestroy releases the ad; its calls then fail.\n\n```go\nfunc (r *RewardedAd) Load(done func(error))\n```\nLoad 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).\n\n```go\nfunc (r *RewardedAd) OnClose(fn func(completed bool))\n```\nOnClose runs fn when the user closes the ad, completed when they watched it to its end and so earned the reward.\n\n```go\nfunc (r *RewardedAd) OnError(fn func(error))\n```\nOnError runs fn with the host's failures to load or show an ad.\n\n```go\nfunc (r *RewardedAd) Show(done func(error))\n```\nShow shows the ad, loading one and trying once more if none is ready, and answers nil once it shows.\n\n```go\ntype ShareCard struct {\n\t// Title is the card's title; empty for the platform's (the app's name).\n\tTitle string\n\t// 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.\n\tImageURL string\n\t// TemplateID is a share material the platform approved: WeChat's imageUrlId, Douyin's templateId.\n\tTemplateID string\n\t// Query is what the app opens with from the card, key1=val1\u0026key2=val2, which Launch.Query and OnShow hand back.\n\tQuery string\n}\n```\nShareCard is what a share shows and opens the app with.\n\n```go\ntype ShareInfo struct {\n\tEncryptedData, IV string\n\t// CloudID is set for mini programs with WeChat's cloud development.\n\tCloudID string\n}\n```\nShareInfo is a group share ticket's data, encrypted for the app's server, which holds the session key that decrypts it (WeChat's openGId).\n\n```go\ntype SubscribeStatus string\n```\nSubscribeStatus is the user's answer for one subscribe message template.\n\n```go\nconst (\n\tSubscribeAccept          SubscribeStatus = \"accept\"\n\tSubscribeAcceptWithAudio SubscribeStatus = \"acceptWithAudio\"\n\tSubscribeAcceptWithAlert SubscribeStatus = \"acceptWithAlert\"\n\tSubscribeReject          SubscribeStatus = \"reject\"\n\tSubscribeBan             SubscribeStatus = \"ban\"\n\tSubscribeFilter          SubscribeStatus = \"filter\"\n\t// SubscribeFail is Douyin's for a template it could not ask about.\n\tSubscribeFail SubscribeStatus = \"fail\"\n)\n```\nThe statuses the platforms answer.\n\n```go\nfunc (s SubscribeStatus) Accepted() bool\n```\nAccepted reports whether the user subscribed, so the app's server may send the template's message.\n\n","path":"github.com/nsgo-dev/nsgo/miniprogram","name":"miniprogram","synopsis":"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.","isRedistributable":true}
