ASP.NET MVC做的微信WEBAPP中调用微信JSSDK扫一扫
今天做一个项目,是在微信上用的,微信WEB APP,里面用到了调用手机摄像头扫一扫二维码的功能,记得以前某个项目里写有的,但是找不到之前那个项目源码了,想复制粘贴也复制不了了,只好对着微信的那个开发文档重新再写过 ,顺便写个博客,以后碰到相同的问题直接复制博客里的代码就行了
以下是显示在微信上的页面:

以下是页面的代码,(用到了MUI):
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<header class="mui-bar mui-bar-nav">
<h1 class="mui-title">扫码认证</h1>
</header>
<div class="mui-content">
<div style="text-align: center; padding:0.5rem; background: white;">
<img id="img_shaoma" src="/content/images/shaoma.png" style="width:100px;" />
</div>
<div style="margin-top:0.5rem;padding:2rem; text-align: center; background:white;">
<input id="txtyzm" type="text" placeholder="输入认证码验证产品" />
<button type="button" class="mui-btn mui-btn-success">点击认证</button>
</div>
</div>
@section script{
<script src="/content/js/mui.min.js"></script>
<script type="text/javascript">
mui.init();
</script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
wx.config({
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: "@ViewBag.appid", // 必填,企业号的唯一标识,此处填写企业号corpid
timestamp: @ViewBag.timestamp, // 必填,生成签名的时间戳
nonceStr: "@ViewBag.noncestr", // 必填,生成签名的随机串
signature: "@ViewBag.signature",// 必填,签名,见附录1
jsApiList: ['scanQRCode'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function () {
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
document.getElementById('img_shaoma').addEventListener('tap', function () {
wx.scanQRCode({
needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
success: function (res) {
var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
// mui.alert("扫码结果:" + result);
document.getElementById("txtyzm").value = result;
}
});
})
});
</script>
}
以下是对应的控制器后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Niunan.NiaoRen.Web.Areas.Seller.Controllers
{
public class ShaoMaController : Controller
{
//卖家中心-扫码页面
public ActionResult Index()
{
#region 用到微信扫一扫接口需要用的东西
WxPayAPI.NiunanWXHelper wxhelper = new WxPayAPI.NiunanWXHelper();
WxPayAPI.WxPayData config_data = wxhelper.GetJSSDKConfig();
ViewBag.appid = config_data.GetValue("appId");
ViewBag.timestamp = config_data.GetValue("timestamp");
ViewBag.noncestr = config_data.GetValue("nonceStr");
ViewBag.signature = config_data.GetValue("signature");
#endregion
return View();
}
}
}
以下是NiunanWXHelper 的代码,用到了一些原来微信官方DEMO里的一些方法,所以创建在了微信官方DEMO的那个项目中:
using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Security;
/// <summary>
/// 牛腩自己写的微信helper
/// https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
/// </summary>
namespace WxPayAPI
{
public class NiunanWXHelper
{
/// <summary>
/// 返回使用微信JS-SDK接口的配置
// appId: @ViewBag.wx_appid, // 必填,企业号的唯一标识,此处填写企业号corpid
// timestamp: @ViewBag.wx_timestamp, // 必填,生成签名的时间戳
// nonceStr: @ViewBag.wx_noncestr, // 必填,生成签名的随机串
// signature: @ViewBag.wx_signature,// 必填,签名,见附录1
/// </summary>
/// <returns></returns>
public WxPayData GetJSSDKConfig()
{
string appid = WxPayConfig.APPID;
string secret = WxPayConfig.APPSECRET;
string timestamp = WxPayApi.GenerateTimeStamp();
string noncestr = WxPayApi.GenerateNonceStr();
string signature = "";
//签名算法 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
//1. 获取AccessToken(有效期7200秒,开发者必须在自己的服务全局缓存access_token)
string url1 = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}";
string result = HttpService.Get(url1);
JsonData jd = JsonMapper.ToObject(result);
string access_token = (string)jd["access_token"];
//2. 用第一步拿到的access_token 采用http GET方式请求获得jsapi_ticket(有效期7200秒,开发者必须在自己的服务全局缓存jsapi_ticket)
string url2 = $"https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={access_token}&type=jsapi";
string result2 = HttpService.Get(url2);
JsonData jd2 = JsonMapper.ToObject(result2);
string ticket = (string)jd2["ticket"];
//3. 开始签名
string now_url = HttpContext.Current.Request.Url.AbsoluteUri;
string no_jiami = $"jsapi_ticket={ticket}&noncestr={noncestr}×tamp={timestamp}&url={now_url}";
//SHA1加密
signature = FormsAuthentication.HashPasswordForStoringInConfigFile(no_jiami, "SHA1");
WxPayData data = new WxPayData();
data.SetValue("appId", appid);
data.SetValue("timestamp", timestamp);
data.SetValue("nonceStr", noncestr);
data.SetValue("signature", signature);
Log.Debug(this.GetType().ToString(), "使用微信JS-SDK接口的配置 : " + data.ToPrintStr());
return data;
}
}
}
从官网下载的DEMO是ASPX的,我把里面的lib文件夹和business文件夹抽出来放到WXPAYAPI项目中了

ASP.NET MVC做的微信WEBAPP中调用微信JSSDK扫一扫的更多相关文章
- Asp.net mvc web api 在项目中的实际应用
Asp.net mvc web api 在项目中的实际应用 前言:以下只是记录本人在项目中的应用,而web api在数据传输方面有多种实现方式,具体可根据实际情况而定! 1:数据传输前的加密,以下用到 ...
- ASP.NET MVC 做的网站项目
感谢博客园团队日夜为广大需要获取知识人们所做的奉献 博客园团队您们辛苦了 ASP.NET MVC 实现有论坛功能的网站(有iis发布网站 这是之前写的... www.lazyfitness.cn 经过 ...
- 微信网页开发调用微信jssdk接口遇到的坑以及最终解决方法 (持续更新)
1.微信网页开发调用jssdk时报permission denied 大致是两个原因 (1)首先注册时未将你所调用的接口名字添加至jsApiList (2)第二个就是你的这个公众号没有权限使用这个ap ...
- ASP.NET MVC 学习4、Controller中添加SearchIndex页面,实现简单的查询功能
参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-method ...
- [小技巧][ASP.Net MVC Hack] 使用 HTTP 报文中的 Header 字段进行身份验证
在一些 Web 系统中,身份验证是依靠硬件证书进行的:在电脑上插入 USB 证书,浏览器插件读取证书的相关信息,然后在发送 HTTP 登录请求时顺便在 Header 字段附加上身份信息.服务器端处理这 ...
- asp.net mvc,做 301 永久重定向
以下代码为 asp.net mvc 4.0 代码做的 301 永久重定向 string url = “http://www.csdn.net/test.html” Response.StatusCod ...
- asp.net mvc Partial OutputCache 在SpaceBuilder中的应用实践
最近给SpaceBuilder增加OutputCache 时发现了一些问题,贴在这做个备忘,也方便遇到类似问题的朋友查阅. 目前SpaceBuilder表现层使用是asp.net mvc v1.0,使 ...
- 在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下:
Asp.net MVC中的ViewData与ViewBag ViewData ViewBag 它是Key/Value字典集合 它是dynamic类型对像 从Asp.net MVC 1 就有了 ASP. ...
- ASP.NET MVC 学习8、Controller中的Detail和Delete方法
参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and ...
随机推荐
- java 解析excel工具类
java 解析excel工具类 CreateTime--2018年3月5日16:48:08 Author:Marydon ReadExcelUtils.java import java.io.Fi ...
- 【BI】OLTP与OLAP的区别
概念 OLTP:联机事务处理(On-Line transaction Processing) OLAP:联机分析处理(On-Line Analytical Processing) (1)OLTP是传统 ...
- 利用jquery修改href的部分字符
试了好久 就一句代码即可. $(document).ready(function(){ $('a').each(function(){ this.href = this.href.replace('y ...
- WinForm中 事件 委托 多线程的应用【以一个下载进度条为例】
第一步:首先我们创建一个winfor的项目 第二步:我们建一个窗体 在一个窗体里面 打开一个另外的窗体 另外的窗体有一个按钮 点击后就开始下载 下载完成后 在注册窗体上面 显示下载完成(达到在一个窗体 ...
- ios中core Plot (2)
#import "ViewController.h" @interface ViewController () //指定要画得view @property(nonatomic,as ...
- (原)torch7中指定可见的GPU
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/7418694.html 参考网址: https://gitter.im/torch/torch7/arc ...
- java struts2入门学习--防止表单重复提交.OGNL语言学习
一.知识点回顾 防止表单重复提交核心思想: 客户端和服务器端和写一个token,比较两个token的值相同,则非重复提交;不同,则是重复提交. 1.getSession三种方式比较: request. ...
- Xcode 常用调试技巧总结
NSLog,po命令和普通断点调试相信每个iOS开发者都会,这里就不作介绍了. 一.Memory Graph Xcode8新增:Memory Graph解决闭包引用循环问题 有很多叹号说明就有问题了. ...
- 【LeetCode】213. House Robber II
House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...
- 如何禁用Visual Studio 2013的浏览器链接功能
VS2013新增的Browser Link功能虽然“强大”,但我并不需要. 但默认是开启的,会在页面中自动添加如下的代码,真是烦人! <!-- Visual Studio Browser Lin ...