之前做的版本用户这块是以获取用户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解密获取用户手机号码的更多相关文章

  1. 微信小程序前端调用后台方法并获取返回值

    wxml代码 <wxs src="../../wxs/string.wxs" module="tools" /> <!-- 调用tools.i ...

  2. 微信小程序需要https后台的创业机会思考

    最近比较关注微信小程序,而且微信小程序的后台必须强制要求https, https相对http成本要高很多了. 这里我感觉有2个商机 (1)提供https 中转服务器 ,按流量来收费 (2) 微信小程序 ...

  3. 微信小程序与Java后台通信

    一.写在前面 最近接触了小程序的开发,后端选择Java,因为小程序的代码运行在腾讯的服务器上,而我们自己编写的Java代码运行在我们自己部署的服务器上,所以一开始不是很明白小程序如何与后台进行通信的, ...

  4. 微信小程序与Java后台的通信

    一.写在前面 最近接触了小程序的开发,后端选择Java,因为小程序的代码运行在腾讯的服务器上,而我们自己编写的Java代码运行在我们自己部署的服务器上,所以一开始不是很明白小程序如何与后台进行通信的, ...

  5. 微信小程序登录JAVA后台

    代码地址如下:http://www.demodashi.com/demo/12736.html 登录流程时序登录流程时序 具体的登录说明查看 小程序官方API 项目的结构图: springboot项目 ...

  6. 微信小程序:java后台获取openId

    一.功能描述 openId是某个微信账户对应某个小程序或者公众号的唯一标识,但openId必须经过后台解密才能获取(之前实现过前台解密,可是由于微信小程序的种种限制,前台解密无法在小程序发布后使用) ...

  7. 微信小程序与java后台交互

    java后台使用的ssm框架,小程序连接的本地接口.跟正常的web访问没什么区别,也是后台获取url,返回json数据:只是小程序前台请求的url要带上http://localhost:80801. ...

  8. 原创:微信小程序调用PHP后台接口,解析纯html文本

    ---效果图片预览---    1.微信js动态传参:wx.request({        url: 'https://m.****.com/index.php/Home/Xiaoxxf/activ ...

  9. 微信小程序支付c#后台实现

    今天为大家带来比较简单的支付后台处理 首先下载官方的c#模板(WxPayAPI),将模板(WxPayAPI)添加到服务器上,然后在WxPayAPI项目目录中添加两个“一般处理程序” (改名为GetOp ...

随机推荐

  1. 相机位姿求解——P3P问题

    1.位姿求解是计算机视觉中经常遇到的,Perspective-n-Points, PnP(P3P)提供了一种解决方案,它是一种由3D-2D的位姿求解方式,即需要已知匹配的3D点和图像2D点.目前遇到的 ...

  2. 使用Condition

    /** * ReentrantLock使用Condition对象来实现wait和notify的功能* 使用Condition时,引用的Condition对象必须从Lock实例的newCondition ...

  3. 拓展 centos 7

    查看端口的占用情况 Centos6/contos7(ECS) 使用 netstat 查询系统上有多少TCP 多少UDP会话 netstat -tun -t: tcp -u: udp -a: all 表 ...

  4. tornado框架中redis使用

    一.安装依赖 pip3 install tornado-redis 二.导入模块 import tornadoredis 三.创建redis对象 import tornadoredis CONNECT ...

  5. PHP+jQuery中国地图热点数据统计展示实例

    一款PHP+jQuery实现的中国地图热点数据统计展示实例,当鼠标滑动到地图指定省份区域,在弹出的提示框中显示对应省份的数据信息. 首先在页面中加一个div#tip,用来展示地图信息的提示框和#map ...

  6. Dotnet Core中使用AutoMapper

    官网:http://automapper.org/ 文档:https://automapper.readthedocs.io/en/latest/index.html GitHub:https://g ...

  7. MySQL 8.0部分弃用的参数整理

    最近整理了一下MySQL 8.0的自动化安装,其中用到了一个MySQL 5.7版本的自定义配置文件,由于没有对(MySQL 8.0)做针对性修改,导致安装过程中出现了一些错误其中部分原因就是MySQL ...

  8. 《收获,不止SQL优化》这本书,有很多即用的脚本工具,或者根据自己的需求,改造重用,可以积累到自己的工具库中。

    以下两个脚本,官方来源: https://github.com/liangjingbin99/shouhuo/tree/master/%E7%AC%AC05%E7%AB%A0 1. 找出未使用绑定变量 ...

  9. 特殊权限SUID

    特殊权限SUID SUID : 运行某程序时,相应进程的属主是程序文件自身的属主,而不是启动者: chmod u+s File chmod u-s File 如果 FileB本身原来就有执行权限,则S ...

  10. Cocos2d-x开发教程——《萝莉快跑》

    更好的阅读体验请前往<萝莉快跑>开发教程. 配置:win7+Cocos2d-x.2.0.3+VS2012 目标读者:已经了解图形显示.动作.回调函数.定时器的用法. 一.基本知识点 1.动 ...