微信小程序登录页php后台,微信小程序实现微信登陆(TP5后端)

本文详细介绍了如何使用ThinkPHP5框架实现微信小程序的账号绑定和登录功能。前端小程序通过调用微信登录接口获取code,然后将code发送到后台。后台通过code获取微信openid,将openid存入用户表,实现账号绑定。当用户再次登录时,同样流程检查openid,若存在则直接登录。此外,还展示了后端如何解密获取openid以及处理用户登录的逻辑。
摘要由CSDN通过智能技术生成

思路:

小程序登录获取code,将code传到后台;

后台用code得到微信用户id,即openid,将openid存储在用户表中,完成绑定

登录时,再次获取code并传给后台,得到openid,若用户表中存在,便可直接登录

以下仅是代码片段,更多代码在Github

back_end/application/api/controller

mini_program/pages/student_mine

mini_program/pages/login

微信与小程序账号绑定

小程序前端获取code,将code与id传回后台

wx_binding: function () {

var that = this

wx.login({

success: function (res) {

console.log("code: ", res.code)

wx.request({

url: ‘******/user/wxbinding‘,

data: {

code: res.code,

id: getApp().globalData.user.id,

},

method: "POST",

header: {

"Content-Type": "application/x-www-form-urlencoded"

},

})

}

})

}

ThinkPHP5后端接受到code,用code得到openid,并将openid与账号id绑定

public function wxBinding() {

$url = "https://api.weixin.qq.com/sns/jscode2session";

// 参数

$params = array();

$params[‘appid‘] = ‘******‘;

$params[‘secret‘] = ‘******‘;

$params[‘js_code‘] = $_POST[‘code‘];

$params[‘grant_type‘] = ‘authorization_code‘;

// 微信API返回的session_key 和 openid

$arr = $this -> httpCurl($url, $params, ‘POST‘);

$arr = json_decode($arr, true);

// 判断是否成功

if (isset($arr[‘errcode‘]) && !empty($arr[‘errcode‘])) {

return json([‘error_code‘ => ‘2‘, ‘msg‘ => $arr[‘errmsg‘], "result" => null]);

}

$openid = $arr[‘openid‘];

// 插入openid

$bind = db(‘user‘) -> where(‘id‘, $_POST[‘id‘]) -> update([‘openid‘ => $openid]);

if ($bind) {

return json([‘error_code‘ => ‘0‘, ‘msg‘ => ‘绑定成功‘]);

} else {

// 找不到账号

return json([‘error_code‘ => ‘1‘, ‘msg‘ => ‘绑定失败‘]);

}

}

解密获取openid

use \think\Exception;

function httpCurl($url, $params, $method = ‘GET‘, $header = array(), $multi = false) {

date_default_timezone_set(‘PRC‘);

$opts = array(

CURLOPT_TIMEOUT => 30,

CURLOPT_RETURNTRANSFER => 1,

CURLOPT_SSL_VERIFYPEER => false,

CURLOPT_SSL_VERIFYHOST => false,

CURLOPT_HTTPHEADER => $header,

CURLOPT_COOKIESESSION => true,

CURLOPT_FOLLOWLOCATION => 1,

CURLOPT_COOKIE

=> session_name(). ‘=‘.session_id(),

);

/* 根据请求类型设置特定参数 */

switch (strtoupper($method)) {

case ‘GET‘:

// $opts[CURLOPT_URL] = $url . ‘?‘ . http_build_query($params);

// 链接后拼接参数 & 非?

$opts[CURLOPT_URL] = $url. ‘?‘.http_build_query($params);

break;

case ‘POST‘: //判断是否传输文件

$params = $multi ? $params : http_build_query($params);

$opts[CURLOPT_URL] = $url;

$opts[CURLOPT_POST] = 1;

$opts[CURLOPT_POSTFIELDS] = $params;

break;

default:

throw new Exception(‘不支持的请求方式!‘);

}

/* 初始化并执行curl请求 */

$ch = curl_init();

curl_setopt_array($ch, $opts);

$data = curl_exec($ch);

$error = curl_error($ch);

curl_close($ch);

if ($error) throw new Exception(‘请求发生错误:‘.$error);

return $data;

}

微信登录小程序

小程序前端返回code

wx.login({

success: function (res) {

console.log("code: ", res.code)

wx.request({

url: ‘******/user/wxlogin‘,

data: {

code: res.code,

},

method: "POST",

header: {

"Content-Type": "application/x-www-form-urlencoded"

},

success: function (res) {

},

})

}

})

后端获取openid,若有用户,返回用户信息给小程序

public function wxLogin() {

if (empty($_POST[‘code‘])) {

return json([‘error_code‘ => ‘1‘, ‘msg‘ => ‘请输入code!‘]);

}

$url = "https://api.weixin.qq.com/sns/jscode2session";

// 参数

$params = array();

$params[‘appid‘] = ‘******‘;

$params[‘secret‘] = ‘******‘;

$params[‘js_code‘] = $_POST[‘code‘];

$params[‘grant_type‘] = ‘authorization_code‘;

// 微信API返回的session_key 和 openid

$arr = $this -> httpCurl($url, $params, ‘POST‘);

$arr = json_decode($arr, true);

// 判断是否成功

if (isset($arr[‘errcode‘]) && !empty($arr[‘errcode‘])) {

return json([‘error_code‘ => ‘2‘, ‘msg‘ => $arr[‘errmsg‘], "result" => null]);

}

$openid = $arr[‘openid‘];

// $session_key = $arr[‘session_key‘];

// 从数据库中查找是否有该openid

$user = db(‘user‘) -> where(‘openid‘, $openid) -> find();

if ($user) {

unset($user[‘openid‘]); // 删除openid

unset($user[‘password‘]); // 删除密码

return json([‘error_code‘ => ‘0‘, ‘msg‘ => ‘登录成功‘, ‘data‘ => $user]);

} else {

// 该微信用户没有绑定账号

return json([‘error_code‘ => ‘1‘, ‘msg‘ => ‘您没有绑定账号,请登录后在“我的”页面绑定~‘]);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值