Wechat game API calls Egret

Time:2020-8-14

Revision of the official website of Yezi E-sports https://www.xxe.io/ Brand new
In egret, you can directly call the API of wechat games. With platform code added in egret, you can call various platform APIs.

The new project will find that the SRC folder contains a Platform.ts If not, please create.

/**

Platform data interface.
Since each game usually needs to be published on multiple platforms, a unified interface is extracted for developers to obtain platform data information
It is recommended that developers encapsulate the platform logic in this way to ensure the stability of the overall structure
Because the interface forms of different platforms are different, egret recommends that developers encapsulate all interfaces as asynchronous forms based on promise
*/
declare interface Platform {
getUserInfo(): Promise;
login(): Promise
}
class DebugPlatform implements Platform {
async getUserInfo() {
return { nickName: “username” }
}
async login() {
}
}
if (!window.platform) {
window.platform = new DebugPlatform();
}
declare let platform: Platform;
declare interface Window {
platform: Platform
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
And copy the code to Platform.ts Medium.

2、 Package and publish the wechat project and open it in the wechat developer tool. You can see platform.js File, if not, please create platform.js Documents. Other interfaces of wechat should also be written to the file, such as sharing, wechat Click to enter, etc.

/**

Please at egret engine Main.ts Call in platform.login The () method is called here.
*/
class WxgamePlatform {
name = ‘wxgame’
login() {
return new Promise((resolve, reject) => {
wx.login({
success: (res) => {
resolve(res)
}
})
})
}

getUserInfo() {

return new Promise((resolve, reject) => {
    wx.getUserInfo({
        withCredentials: true,
        success: function (res) {
            var userInfo = res.userInfo
            var nickName = userInfo.nickName
            var avatarUrl = userInfo.avatarUrl
            var gender =  userInfo.gender  //Gender 0: unknown, 1: male, 2: female
            var province = userInfo.province
            var city = userInfo.city
            var country = userInfo.country
            resolve(userInfo);
        }
    })
})

}

openDataContext = new WxgameOpenDataContext();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
}

class WxgameOpenDataContext {
createDisplayObject(type,width,height){
const bitmapdata = new egret.BitmapData(sharedCanvas);
bitmapdata.$deleteSource = false;
const texture = new egret.Texture();
texture._setBitmapData(bitmapdata);
const bitmap = new egret.Bitmap(texture);
bitmap.width = width;
bitmap.height = height;

egret.startTick((timeStarmp) => {
    egret.WebGLUtils.deleteWebGLTexture(bitmapdata.webGLTexture);
    bitmapdata.webGLTexture = null;
    return false;
}, this);
return bitmap;

}

postMessage(data){

const openDataContext = wx.getOpenDataContext();
openDataContext.postMessage(data);

}
1
2
3
4
5
6
7
8
9
10
11
12
}

window.platform = new WxgamePlatform();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Platform declares the platform function, which can be called directly in egret, and the code can be implemented in each platform.

Three, calling login in egret and getting WeChat user information. stay main.ts Write the following code in.

await platform.login () call the login API,
const userInfo = await platform.getUserInfo (); get wechat user information and return user information,
Await is called asynchronously, and the result of the call is returned. The next step is executed.

class Main extends egret.DisplayObjectContainer {

public constructor() {

super();
this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);

}

private onAddToStage(event: egret.Event) {

this.runGame().catch(e => {
    console.log(e);
})

}

private async runGame() {

await platform.login();
const userInfo = await platform.getUserInfo();
this.createGameScene(userInfo);

}

private textfield: egret.TextField;

/**

  • Create game scene
  • Create a game scene

*/
private createGameScene(userInfo: any) {

let bg: eui.Rect = new eui.Rect();
this.addChild(bg);
bg.width = this.stage.width;
bg.height = this.stage.height;
bg.fillColor = 0xF8F8F8;

let avatar: eui.Image = new eui.Image();
avatar.x = 100;
avatar.y = 100;
avatar.width = 120;
avatar.height = 120;
avatar.source = userInfo.avatarUrl;
this.addChild(avatar);

let nickName: eui.Label = new eui.Label();
nickName.x = 100;
nickName.y = 250;
nickName.textColor = 0xff0000;
nickName.text = userInfo.nickName;
this.addChild(nickName);

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Package the wechat project and open it in the developer tool. Run to get wechat user information.
————————————————