asp.net调用微信扫一扫功能
时间:2017-09-04 10:56:46
收藏:0
阅读:384
1、页面引用http://res.wx.qq.com/open/js/jweixin-1.0.0.js
2、前台代码
function shaomiao() {
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: ‘‘, // 必填,公众号的唯一标识
timestamp: ‘<%=time %>‘, // 必填,生成签名的时间戳(随便填写)
nonceStr: ‘<%=randstr %>‘, // 必填,生成签名的随机串(随便填写)
signature: ‘<%=signstr %>‘, // 必填,签名,见附录1
jsApiList: [‘checkJsApi‘,
‘onMenuShareTimeline‘,
‘onMenuShareAppMessage‘,
‘onMenuShareQQ‘,
‘onMenuShareWeibo‘,
‘hideMenuItems‘,
‘showMenuItems‘,
‘hideAllNonBaseMenuItem‘,
‘showAllNonBaseMenuItem‘,
‘translateVoice‘,
‘startRecord‘,
‘stopRecord‘,
‘onRecordEnd‘,
‘playVoice‘,
‘pauseVoice‘,
‘stopVoice‘,
‘uploadVoice‘,
‘downloadVoice‘,
‘chooseImage‘,
‘previewImage‘,
‘uploadImage‘,
‘downloadImage‘,
‘getNetworkType‘,
‘openLocation‘,
‘getLocation‘,
‘hideOptionMenu‘,
‘showOptionMenu‘,
‘closeWindow‘,
‘scanQRCode‘,
‘chooseWXPay‘,
‘openProductSpecificView‘,
‘addCard‘,
‘chooseCard‘,
‘openCard‘
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function () {
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
wx.scanQRCode({
needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
success: function (res) {
result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
if (typeof (result) != "undefined") {
//result 扫描后的值
}
}
});
});
wx.error(function (res) {
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
});
}
3、后台代码
public string time = "";
public string randstr = "";
public string signstr = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//调用微信二维码扫描
Response.Cache.SetNoStore();
string[] str = wxHelper.GetWXInfo(this.Request.Url.ToString()).Split(‘,‘);
time = str[0];
randstr = str[1];
signstr = str[2];
}
}
4、wxHelper类
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections.Generic;
using System.Net;
/// <summary>
/// wxHelper 的摘要说明
/// </summary>
public partial class wxHelper
{
/// <summary>
/// 签名生成时间
/// </summary>
public static string dtime = "";
/// <summary>
/// 签名提交url地址
/// </summary>
public static string url = "";
/// <summary>
/// 生成签名的时间戳
/// </summary>
public static string time = "";
/// <summary>
/// 生成签名的随机串
/// </summary>
public static string randstr = "";
/// <summary>
/// 签名
/// </summary>
public static string signstr = "";
/// <summary>
/// appid
/// </summary>
private readonly static string appid = "******";
/// <summary>
/// secret
/// </summary>
private readonly static string secret = "******";
public wxHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 获得accesstoken
/// </summary>
/// <returns></returns>
public static string AccessToken()
{
return SendRequest("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret, Encoding.UTF8);
}
/// <summary>
/// 根据accesstoken获得ticket
/// </summary>
/// <returns></returns>
public static string GetTicket()
{
string access_token = AccessToken();
string url1 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token.Substring(access_token.IndexOf(‘:‘) + 2, access_token.IndexOf(‘,‘) - 3 - access_token.IndexOf(‘:‘)) + "&type=jsapi";
string requstStr = SendRequest(url1, Encoding.UTF8);
string ticket = requstStr.Substring(requstStr.