小程序如何获取手机号( thinkphp3.2.3框架)
本篇文章给大家带来的内容是关于小程序如何获取手机号( thinkphp3.2.3框架),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
1、直接上代码php
namespace HomeController;use ThinkController;class ApiController extends Controller { /** * error code 说明. *
//2、小程序
2.1在app.js 启动页面里先login
// 登录 // if (!wx.getStorageSync('session_key') || wx.getStorageSync('time') { console.log(res) // 发送 res.code 到后台换取 openId, sessionKey, unionId wx.request({ url: 'https://www.zhixiaobing.com/index.php?m=&c=api&a=getopenId', header: { "Content-Type": "application/x-www-form-urlencoded" }, method: 'post', data: { code: res.code }, success: function (res) { console.log(res.data); wx.setStorageSync('openid', res.data.openid) wx.setStorageSync('session_key', res.data.session_key) wx.setStorageSync('time', parseInt(Date.parse(new Date())) + 7200) } }) } })
//2.2 在小程序模板里写组件
<button>//这是官方的组件点击会弹出授权页面</button>
在js里写下面的函数
立即学习“PHP免费学习笔记(深入)”;
getPhoneNumber: function (e) { var that =this; var session_key = wx.getStorageSync('session_key') if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { wx.showModal({ title: '提示', showCancel: false, content: '未授权', success: function (res) { } }) } else {//确认授权 wx.request({ url: 'https://www.showoow.com/index.php?m=mini&c=api&a=getPhoneNumber&openid=' + wx.getStorageSync('openid'), //openid是app.js 已经存的 header: {"Content-Type": "application/x-www-form-urlencoded" }, method: "get", data: { encryptedData: e.detail.encryptedData, iv: e.detail.iv, session_key:session_key }, success:function(res){ if (res.data.msg.phoneNumber){ console.log(res); wx.showModal({ title: '提示', showCancel: false, content: '授权成功', success: function () { wx.setStorageSync('phoneNumber', res.data.msg.phoneNumber); var time = Date.parse(new Date()) + 60 * 60 * 24 * 2 wx.setStorageSync('exp', time ); } }) setTimeout(function(){ wx.navigateTo({ url: '/pages/form/form', }) },1500); that.setData({ show:'show', hiden:'' }) }else{ wx.showToast({ title: '授权失败', icon:'loading' }) } }, fail:function(){ wx.showToast({ title: '授权失败', icon: 'loading' }) } }) } },
到此小程序获取手机号完结,在公司写了个小程序,可以正常获取手机号