【微信授权】cocos creator 3.6.1 微信头像 用户登录授权,微信昵称,用户授权按钮UserInfoButton 创建。

cocos creator 微信头像,微信昵称获取,用户授权按钮显示。头像不显示问题解决,头像显示授权文字
注意,目前微信提供的api只能拿到用户头像和昵称,其余信息都没法拿到
如果你能够拿到的话,可以留下评论告诉我方法,我会再次优化该博客,感谢

0.官方api链接

微信API相关接口文档

1.微信登录授权脚本【typescript】

import { _decorator, Component, Node, url, Sprite, assetManager, ImageAsset, SpriteFrame, Texture2D, Label, EditBox, NodeEventType } from 'cc';
const { ccclass, property } = _decorator;
import 'miniprogram-api-typings';

@ccclass('WXLogin')
export class WXLogin extends Component {

  @property(Node)
  avatar: Node

  @property(Label)
  name_Label: Label

  systemInfo;
  screenWidth;
  screenHeight;
  //用户信息
  userInfo;

  onLoad(){
    this.login();
  }

  login() {
    var self = this;
    const wx = window['wx'];//避开ts语法检测
    const info = this.systemInfo = wx.getSystemInfoSync();//立即获取系统信息
    const w : number = this.screenWidth = info.screenWidth;//屏幕宽
    const h : number = this.screenHeight = info.screenHeight;//屏幕高
    console.log("屏幕宽:", w)
    console.log("屏幕高:", h)
    //获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。
    wx.getSetting(
      {
        success(res) {
          //如果用户已经授权
          if (res.authSetting["scope.userInfo"]) {
            wx.getUserInfo({
              success(res) {
                console.log("授权成功")
                this.userInfo = res.userInfo;
                console.log("用户已经授权,用户信息" + res.userInfo.nickName);
                console.log("nickName:" + this.userInfo.nickName);
                console.log("avatarUrl:" + this.userInfo.avatarUrl);
                self.setAvatar(this.userInfo.avatarUrl);
                self.name_Label.string = this.userInfo.nickName as string;
              }
            });
            //如果用户没有授权
          } else {
            console.log("创建全屏透明==[createUserInfoButton]");
            let button = wx.createUserInfoButton({
              type: 'text',
              text: '登录',
              style: {
                left: w/2-45,
                top: h - 30,
                width: 90,
                height: 40,
                lineHeight: 40,
                backgroundColor: "#66CC00",
                color: "#FFFFFF",
                textAlign: "center",
                fontSize: 18,
                borderRadius: 10
              }
            });
            //用户授权确认
            button.onTap((res) => {
              if (res.userInfo) {
                console.log("用户同意授权:", res.userInfo.nickName);
                this.userInfo = res.userInfo;
                self.setAvatar(this.userInfo.avatarUrl);
                self.name_Label.string = this.userInfo.nickName as string;
                button.destroy();
              } else {
                console.log("用户拒绝授权:");
                button.destroy();
              }
            });
          }
        }
      }
    );
  }

  //设置头像
  setAvatar(url): void {
    let spire = this.avatar.getComponent(Sprite)
    assetManager.loadRemote<ImageAsset>(url + "?aaa=aa.jpg", { ext: '.jpg' }, (_err, imageAsset) => {
      let sp = new SpriteFrame();
      let texture = new Texture2D();
      texture.image = imageAsset;
      sp.texture = texture
      spire.spriteFrame = sp;
    })
  }


}


2.获取头像显示到组件上

assetManager.loadRemote(avatarUrl, {ext:.png’}, (err, imageAsset)=>{
	let spriteFrame = new SpriteFrame();
	const texture = new Texture2D();
	texture.image = imageAsset;
	spriteFrame.texture=texture;
	//这里去设置头像的spriteFrame就OK了
	that.headerIcon.spriteFrame = spriteFrame
})

2.2头像显示问题解决

1.配置域名

小程序后台管理 >开发管理> 开发设置==> 服务器域名
在这里插入图片描述

2.添加aaa=aa.jpg

在这里插入图片描述

3.demo演示

在这里插入图片描述

4.我的github和gitee 到时候会把项目demo发布到上面,敬请期待,留言加快博主进度

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
Cocos Creator是一款流行的跨平台游戏开发引擎,可以支持在多个平台上进行游戏开发,其中包括微信小程序平台。下面是关于Cocos Creator微信登录的详细说明。 1. 引入微信登录功能:在Cocos Creator中,可以通过引入微信官方提供的API实现微信登录功能。首先,需要在项目中添加微信登录的依赖库,然后在对应的脚本文件中引入相关的微信登录API。 2. 获取微信登录凭证:在用户点击微信登录按钮后,可以调用微信登录API来获取用户微信登录凭证,凭证的获取需要通过微信授权用户需要同意授权。凭证成功获取后,可以获取到用户的唯一标识openid和会话密钥session_key。 3. 与游戏服务器进行验证:获取到微信登录凭证后,可以将凭证传递给游戏服务器进行验证。游戏服务器可以通过调用微信提供的接口验证凭证的有效性,并获取用户微信账号信息。验证成功后,可以将用户的游戏信息与微信账号进行绑定,并生成游戏内的唯一标识token。 4. 使用微信账号登录游戏:验证成功后,可以将生成的token返回给客户端,并保存在本地。下次用户再次打开游戏时,可以直接读取本地保存的token,并传递给游戏服务器进行验证。验证通过后,用户可以直接使用微信账号登录游戏。 通过以上步骤,我们可以在Cocos Creator中实现微信登录功能。当然,在实际开发中,还需要考虑到错误处理、登录状态的管理以及用户信息的更新等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

军大君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值