小程序获得手机号,ASP源代码。手机同微信:13522116288(全套源代码出售)
小程序前台代码:
js文件代码:
getPhoneNumber: function (e) { ///点击 获取手机号码按钮
var my_url = doMain + "myphone.ashx"
var that = this;
wx.checkSession({
success: function () {
var ency = e.detail.encryptedData;
var iv = e.detail.iv;
var sessionk = wx.getStorageSync("sessionKey");
var my_code = wx.getStorageSync("get_code")
var url_all = my_url + "?encryptedData=" + ency + "&iv=" + iv
url_all = url_all + "&sessionkey=" + sessionk + "&code=" + my_code
//console.log(url_all)
if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
that.setData({
modalstatus: true
});
}
else {
//同意授权
wx.request({
method: "GET",
url: my_url,
data: {
encryptedData: ency,
iv: iv,
sessionkey: sessionk,
code: my_code
},
header: {
'content-type': 'application/json' // 默认值
},
success: (res) => {
console.log("得到手机号=========res",res);
var phone = res.data.phoneNumber; ///得到手机号
console.log(phone);
wx.setStorageSync("sisoft_phone2", phone)
that.setData({
get_phone2: phone
});
wf_catch_kahao(that); // 加载卡号------------------20200411
}
, fail: function (res) {
console.log("解密失败~~~~~~~~~~~~~");
console.log(res);
}
});
}
},
fail: function () {
console.log("session_key 已经失效,需要重新执行登录流程");
that.wxlogin(); //重新登录
}
});
},
wxml文件:
<view class='tr_img'>
<image src="{{logon_img}}" style=" width:20px;height:20px; "> </image>
<view class='tr_phone' >登录手机号:{{get_phone2}}
<button style="margin-left: 40rpx; margin-right: 10rpx; border-radius: 80rpx"
style="{{get_phone2!=''?'display:none':''}}"
open-type='getPhoneNumber' bindgetphonenumber="getPhoneNumber"> 登录手机号 </button>
</view>
</view>
ASP后台代码 myphone.ashx :
<%@ webhandler language="C#" class="AverageHandler" %>
using System;
using System.Net;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using LitJson;
public class AverageHandler : IHttpHandler
{
public bool IsReusable
{ get { return true; } }
public void ProcessRequest(HttpContext ctx)
{
ctx.Response.ContentType = "application/json";
HttpRequest Request = ctx.Request;
string text = Request["encryptedData"];
string IV = Request["iv"];
//小程序appid和appsecret配置
string appid = "写上你的小程序的appid";
string secret = "写上你的小程序的密钥";
string code = Request["code"];//微信获取登录的口令
Stream s_re = WebRequest.Create("https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code").GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s_re, Encoding.UTF8);
string strLine = sr.ReadToEnd();
//ctx.Response.Write(strLine);
sr.Close();
try
{
JsonData jo = JsonMapper.ToObject(strLine);
string Key = Request["sessionkey"];// jo["session_key"].ToString();
//string weixinID = jo["openid"].ToString();
string str= AES_decrypt(text,Key,IV);
ctx.Response.Write(str);
}
catch (Exception ex)
{
//return "";
}
}
public string AES_decrypt(string encryptedDataStr, string key, string iv)
{
RijndaelManaged rijalg = new RijndaelManaged();
//设置 cipher 格式 AES-128-CBC
rijalg.KeySize = 128;
rijalg.Padding = PaddingMode.PKCS7;
rijalg.Mode = CipherMode.CBC;
rijalg.Key = Convert.FromBase64String(key);
rijalg.IV = Convert.FromBase64String(iv);
byte[] encryptedData= Convert.FromBase64String(encryptedDataStr);
//解密
ICryptoTransform decryptor = rijalg.CreateDecryptor(rijalg.Key, rijalg.IV);
string result;
using (MemoryStream msDecrypt = new MemoryStream(encryptedData))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
result = srDecrypt.ReadToEnd();
}
}
}
return result;
}
}