php获取微信小程序用户头像,微信小程序获取用户头像+昵称+openid,小程序登录!附前端后端源码!...

做一款小程序,如果需要判断用户,当然要获取一些基本信息,例如头像,昵称,openid。所以本次案例就直接上代码了。

小程序前端

index.wxml

获取头像昵称

{{userInfo.nickName}}

{{motto}}

index.js

//index.js

//获取应用实例

const app = getApp()

Page({

data: {

motto: 'Hello World',

userInfo: {},

hasUserInfo: false,

canIUse: wx.canIUse('button.open-type.getUserInfo')

},

onLoad: function () {

if (app.globalData.userInfo) {

this.setData({

userInfo: app.globalData.userInfo,

hasUserInfo: true

})

} else if (this.data.canIUse){

// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回

// 所以此处加入 callback 以防止这种情况

app.userInfoReadyCallback = res => {

this.setData({

userInfo: res.userInfo,

hasUserInfo: true

})

}

} else {

// 在没有 open-type=getUserInfo 版本的兼容处理

wx.getUserInfo({

success: res => {

app.globalData.userInfo = res.userInfo

this.setData({

userInfo: res.userInfo,

hasUserInfo: true

})

}

})

}

},

getUserInfo: function(e) {

console.log(e)

app.globalData.userInfo = e.detail.userInfo

//获取openid

wx.login({

success: function (res) {

console.log(res.code)

//发送请求获取openid

wx.request({

url: '你的域名/openid.php?code=code', //接口地址

data: { code: res.code },

header: {

'content-type': 'application/json' //默认值

},

success: function (res) {

//返回openid

console.log(res.data.openid)

//向数据库注册用户,验证用户

var that = this;

wx.request({

url: '你的域名/server.php?nickname=' + e.detail.userInfo.nickName + '&avatarUrl=' + e.detail.userInfo.avatarUrl + '&openid=' + res.data.openid,

data: {

},

header: {

'content-type': 'application/json'

},

success: function (res) {

//打印用户信息

console.log(res.data)

}

})

}

})

}

})

this.setData({

userInfo: e.detail.userInfo,

hasUserInfo: true,

})

}

})

index.wxss

/**index.wxss**/

.userinfo {

display: flex;

flex-direction: column;

align-items: center;

}

.userinfo-avatar {

width: 128rpx;

height: 128rpx;

margin: 20rpx;

border-radius: 50%;

}

.userinfo-nickname {

color: #aaa;

}

.usermotto {

margin-top: 200px;

}

后端

openid.php

这是通过code获取openid的后端

//声明CODE,获取小程序传过来的CODE

$code = $_GET["code"];

//配置appid

$appid = "你的小程序APPID";

//配置appscret

$secret = "你的小程序APPSRCRET";

//api接口

$api = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";

//获取GET请求

function httpGet($url){

$curl = curl_init();

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_TIMEOUT, 500);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);

curl_setopt($curl, CURLOPT_URL, $url);

$res = curl_exec($curl);

curl_close($curl);

return $res;

}

//发送

$str = httpGet($api);

echo $str;

?>

server.php

这是把用户名、头像、openid存到数据库的后端

header("Content-type:text/html;charset=utf8");

$nickname = $_GET["nickname"];

$avatarUrl = $_GET["avatarUrl"];

$openid = $_GET["openid"];

$con = mysql_connect("数据库地址","数据库账号","数据库密码");

mysql_select_db("数据库名", $con);

mysql_query("INSERT INTO 表名 (nickname, avatarUrl, openid) VALUES ('$nickname', '$avatarUrl', '$openid')");

echo "注册成功!";

mysql_close($con);

?>

数据库

bVbko3n?w=791&h=149

学习交流微信:face6009

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
很高兴为您解答这个问题。微信小程序可以通过调用微信公众号的接口实现公众号留言功能。以下是具体实现步骤: 1.在微信公众平台注册一个账号,创建一个公众号。 2.在公众号设置中打开开发者模式,获取开发者ID和开发者密钥。 3.在微信开发者工具中创建一个新的小程序项目,填小程序的基本信息。 4.在小程序中引入微信JSSDK,以便调用微信公众号的接口。 5.在小程序中创建一个留言页面,包含留言表单和提交按钮。 6.在小程序的JS文件中,编请求微信公众号接口的代码。具体代码如下: ```javascript wx.request({ url: 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' + access_token, method: 'POST', data: { "touser": openid, "msgtype": "text", "text": { "content": message } }, success: function(res) { console.log(res) }, fail: function(res) { console.log(res) } }) ``` 其中,access_token是微信公众号的接口调用凭证,openid用户openid,message是用户留言的内容。 7.在微信公众号中配置API地址,将小程序的请求地址添加到公众号的白名单中。 8.在微信公众号的后台中,设置自动回复规则,以便及时回复用户留言。 以上就是实现微信公众号留言功能的具体步骤。另外上前后端源码供参考: 前端源码:https://github.com/csdnceshi/WXMiniProgram-LeavingMessage 后端源码:https://github.com/csdnceshi/WXPublicPlatform-LeavingMessage

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值