微信小程序通过getPhoneNumber后台PHP解密获取用户手机号码
之前做的版本用户这块是以获取用户openid为凭证,最近改版重新整理了一下,新增注册登录以手机号码为主,
两种(正常注册手机号码-密码+一键获取当前用户手机号码)
getPhoneNumber这个组件要通过button来实现。将button中的open-type=“getPhoneNumber”,并且绑定bindgetphonenumber事件获取回调。
在使用这个组件之前必须先调用 login 接口
然后传递code,iv,encryptedData参数到后台,后台解密
示例
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"
hover-class="none">一键自动注册</button>
getPhoneNumber: function (e) {
console.log(e.detail.errMsg)
if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
wx.showModal({
title: '提示',
showCancel: false,
content: '未授权',
success: function (res) { }
})
} else {
wx.login({
success: function (res) {
var code = res.code;
if (res.code) {
//发起网络请求
console.log(res.code)
} else {
console.log('获取用户登录态失败!' + res.errMsg)
}
wx.showModal({
title: '提示',
showCancel: false,
content: '同意授权',
success: function (res) {
var that = this;
console.log(123)
wx.request({
url: '/wxapplet/wx/wechat/phone',
data: {
code: code,
iv: e.detail.iv,
encryptedData: e.detail.encryptedData
},
method: 'GET',
header: {
'content-type': 'application/json'
},
success: function (res) {
wx.setStorageSync('user', res.data.data);
if(res.data.code == "200"){
console.log(res.data.data)
wx.showToast({
title: '一键绑定成功',
icon: 'success',
duration: 2000,
success: function(){
wx.switchTab({ url: '../user-center/index' });
}
})
}else{
wx.showModal({
title: '提示',
content: '一键绑定失败,请重新尝试',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定')
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
}
},
});
}
})
}
});
}
}
- 后台是php 框架是laravel
<?php
namespace App\Http\Controllers\WxApplet; use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repository\WxUserRepository; include_once app_path('/Http/Controllers/WxApplet/Php/wxBizDataCrypt.php');
class WechatController extends Controller
{
/**
* constructer.
*
* @param WxUserRepository $wxUser
*/
public function __construct
(
WxUserRepository $WxUserRepository
)
{
$this->WxUserRepository = $WxUserRepository;
}
/**
* 获取用户手机号码
*
* @return response
*/
public function getWechatUserPhone(Request $request)
{
$code = $request->get('code');
$encryptedData = $request->get('encryptedData');
$iv = $request->get('iv');
//小程序开发账户
$appid = "*******" ;
$secret = "*******";
$URL = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
$apiData=file_get_contents($URL);
if(!isset(json_decode($apiData)->errcode))
{
$sessionKey = json_decode($apiData)->session_key;
$info = new \WXBizDataCrypt($appid, $sessionKey);
$errCode = $info->decryptData($encryptedData, $iv, $data );
if ($errCode == 0)
{
$phone = json_decode($data)->phoneNumber;
$single_phone=$this->WxUserRepository->Single($phone);
if ($single_phone == null)
{
$wx_user = $this->WxUserRepository->Create($phone,$password);
}
return $this->Success(200003);
}
else {
return $this->fail(420004);
}
}
}
}
原文链接:https://blog.csdn.net/qq_34827048/article/details/78878121
微信小程序通过getPhoneNumber后台PHP解密获取用户手机号码的更多相关文章
- 微信小程序前端调用后台方法并获取返回值
wxml代码 <wxs src="../../wxs/string.wxs" module="tools" /> <!-- 调用tools.i ...
- 微信小程序需要https后台的创业机会思考
最近比较关注微信小程序,而且微信小程序的后台必须强制要求https, https相对http成本要高很多了. 这里我感觉有2个商机 (1)提供https 中转服务器 ,按流量来收费 (2) 微信小程序 ...
- 微信小程序与Java后台通信
一.写在前面 最近接触了小程序的开发,后端选择Java,因为小程序的代码运行在腾讯的服务器上,而我们自己编写的Java代码运行在我们自己部署的服务器上,所以一开始不是很明白小程序如何与后台进行通信的, ...
- 微信小程序与Java后台的通信
一.写在前面 最近接触了小程序的开发,后端选择Java,因为小程序的代码运行在腾讯的服务器上,而我们自己编写的Java代码运行在我们自己部署的服务器上,所以一开始不是很明白小程序如何与后台进行通信的, ...
- 微信小程序登录JAVA后台
代码地址如下:http://www.demodashi.com/demo/12736.html 登录流程时序登录流程时序 具体的登录说明查看 小程序官方API 项目的结构图: springboot项目 ...
- 微信小程序:java后台获取openId
一.功能描述 openId是某个微信账户对应某个小程序或者公众号的唯一标识,但openId必须经过后台解密才能获取(之前实现过前台解密,可是由于微信小程序的种种限制,前台解密无法在小程序发布后使用) ...
- 微信小程序与java后台交互
java后台使用的ssm框架,小程序连接的本地接口.跟正常的web访问没什么区别,也是后台获取url,返回json数据:只是小程序前台请求的url要带上http://localhost:80801. ...
- 原创:微信小程序调用PHP后台接口,解析纯html文本
---效果图片预览--- 1.微信js动态传参:wx.request({ url: 'https://m.****.com/index.php/Home/Xiaoxxf/activ ...
- 微信小程序支付c#后台实现
今天为大家带来比较简单的支付后台处理 首先下载官方的c#模板(WxPayAPI),将模板(WxPayAPI)添加到服务器上,然后在WxPayAPI项目目录中添加两个“一般处理程序” (改名为GetOp ...
随机推荐
- MSP430系列单片机特性及应用领域
概述 MSP430系列单片机是德州仪器1996年开始推向市场的一种16位超低功耗的混合信号处理器,给人们留下的最大的亮点是低功耗而且速度快,汇编语言用起来很灵活,寻址方式很多,指令很少,容易上手.主要 ...
- Spring Cloud进阶之路 | 一:服务注册与发现(nacos)
转载请注明作者及出处: 作者:银河架构师 原文链接:https://www.cnblogs.com/luas/p/12068846.html 1.版本 最新稳定版本为1.1.4,也可以从发版说明.博客 ...
- SpringCloudConfig对称加密 yml配置文件while parsing a block mapping
错误的配置!!! #-----------------db------------------ mybatis: type-aliases-package: com.book.product.pojo ...
- VUE程序调试的方法
目录 VUE程序调试的方法 1.写本文的背景 2.调试与测试 3.Console调试法 3.1 添加console.log指令 3.2 调出温度界面如下 3.3 Google浏览器的Console窗口 ...
- C++调用bat并实现传值
1.设置环境变量,这一类为路径 C++ void bat(const string& sDirC, const string& sDirD) { char Ddir[256]; Ddi ...
- Server基本语句的用法
1.创建数据库 create database databaseName use databaseName go /* 转到指定数据库 */ 2.创建表 create table tableNa ...
- [转]smtplib.SMTPDataError: (554, b'DT:SPM的异常
本文转自:https://blog.csdn.net/mapeifan/article/details/82428493 python 发送邮件,出现如下异常 异常如下: smtplib.SMTPDa ...
- Windows 10 神州网信版
一.版本介绍:官网链接:http://document.cmgos.com/release_notes/release_notes_V0_H 下载链接:请自行百度Windows 10 神州网信政府版( ...
- jenkins实现git自动拉取代码时替换配置文件
jenkins实现从git上自动拉取源代码——>自动编译——>发布到测试服务器——>验证测试,这个大家应该都知道,但是关于源代码里的配置文件,可能就会有点头疼了, 一般测试都会自己的 ...
- 深入浅出xpath轴定位
在web自动化里面经常要用到定位,常用的八种定位方式中我最喜欢xpath定位,功能很强大.结合它里面的文本定位.模糊定位.逻辑定位等,基本能搞定所有的元素定位问题. 今天要讨论的是xpath的另一种比 ...