微信小程序获取手机号(未测试)

建文件
** 个人账户无法获取手机号!!!**
在这里插入图片描述
global 里的代码

let global={
	//获取值
	userInfo:{
		openid:null,
		session_key:null
	},
	// 小程序的
	appId:'',
	appSecret:'',
		/**
		 * 获取opinid和sessionkey
		 */
		// 或者方法 14,23-27
	// getOpenId:function(getform='js'){
		getOpenId:function()
		var that=this;
		//获取code
		uni.login({
				provider:'weixin',
				success:function(res){
					console.log('获取code:'+JSON.stringify(res))
		let code=res.code;
		// let callback-function(data){
		// 	that.userInfo.openid=data.openid;
		// 	that.userInfo.session_key=data.session_key;
		// }
		// if(getform=='js') that.getOpenIdByJs(code,callback);
		
		that.getOpenIdByJs(code,data=>{
			that.userInfo.openid=data.openid;
		that.userInfo.session_key=data.session_key;
		
		});
		}
		});
		},
		// 
		/**通过code换取对应的key(js版本  ,数据不安全)
		 * @param {Object} code
		 */
		getOpenIdByJs:function(code,callback){
			// let url='https://api.weixin.qq.com/sns/jscode2session?appid='+this.appId+
			// '&secret='+this.appSecret+'&js_code='+code+'&grant_type=authorization_code';
			// 	uni.request({
			// url:url,
			// data:{},
			// 第二种写法
				uni.request({
			url:'https://api.weixin.qq.com/sns/jscode2session?appid',
			data:{
				appid:this.appId,
				secret:this.appSecret,
				js_code:code,
				grant_type:'authorization_code'
			},
			success: (res) => {
				console.log('获取opinid:'+JSON.stringify(res))
			callback(res.data);
			}
		});
		},
		/**
		 * 获取解密并返回
		 * @param {Object} encryptedData
		 * @param {Object} 
		 */
		getMobile(encryptedData,iv){
		let data=this.decoeWxData(encryptedData,iv)	;
		},
			/**微信解密
			 * @param {Object} encryptedData 密文
			 * @param {Object} iv 向量
			 */
		decoeWxData:function(encryptedData,iv){
			var WXBizDataCrypt = require('./WXBizDataCrypt.js')
			var pc = new WXBizDataCrypt(this.appId, this.userInfo.session_key)
			var data = pc.decryptData(encryptedData , iv)
		
			console.log('解密后 data: ',JSON.stringify(data) )
return data;
		}
		
};
// 导出
module.exports=global;

main.js代码

import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false
// 引入 获取小程序
import global from '@/common/global.js';
// 全局用$
Vue.prototype.$global=global;
// 登录时自动获取  直接调用
Vue.prototype.$global.getOpenId();
App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()

index代码

<template>
	<view>
<input type="text" v-model="mobile" />
		<!-- <image class="logo" src="/static/guoguo.png"></image> -->
	        <button class="phoneLogin" type="primary"  open-type="getPhoneNumber" @getphonenumber="onGetPhone">手机号一键登录</button>
	        <!-- 如果用微信登录,获取微信相关用户信息 -->
	        <button  class="wxLogin" >获取用户信息</button>
	</view>
</template>
<script>
    export default {
        data() {
            return {
				mobile:'我是空的'
			}
        },
		methods:{
			getPhoneNumber(e){
			let encryptedData=e.detail.encryptedData;
				 iv=e.detail.iv;
				this.mobile= this.$global.getMobile(encryptedData,iv);
			
			}
		}
		}
</script>
<style>
	.phoneLogin{
		width: 80%;
		margin-top: 50%;
	}
	.wxLogin{
		width: 80%;
		margin-top: 10%;
	}
</style>

Wx里的代码(是微信解密的数据库的)

var crypto = require('crypto')

function WXBizDataCrypt(appId, sessionKey) {
  this.appId = appId
  this.sessionKey = sessionKey
}

WXBizDataCrypt.prototype.decryptData = function (encryptedData, iv) {
  // base64 decode
  var sessionKey = new Buffer(this.sessionKey, 'base64')
  encryptedData = new Buffer(encryptedData, 'base64')
  iv = new Buffer(iv, 'base64')

  try {
     // 解密
    var decipher = crypto.createDecipheriv('aes-128-cbc', sessionKey, iv)
    // 设置自动 padding 为 true,删除填充补位
    decipher.setAutoPadding(true)
    var decoded = decipher.update(encryptedData, 'binary', 'utf8')
    decoded += decipher.final('utf8')
    
    decoded = JSON.parse(decoded)

  } catch (err) {
    throw new Error('Illegal Buffer')
  }

  if (decoded.watermark.appid !== this.appId) {
    throw new Error('Illegal Buffer')
  }

  return decoded
}

module.exports = WXBizDataCrypt

加油!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值