zoukankan      html  css  js  c++  java
  • 使用微信JS-SDK调用微信浏览器的接口

    先附上微信公众平台的相关链接:
    微信公众平台:https://mp.weixin.qq.com/
    微信公众平台开发文档:https://mp.weixin.qq.com/wiki
     
    阅读文档可知,基本流程如下:

    1.绑定域名

    先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。

    2.引入JS文件

    在需要调用JS接口的页面引入如下JS文件 http(s)://res.wx.qq.com/open/js/jweixin-1.0.0.js

    如需使用摇一摇周边功能,请引入 http://res.wx.qq.com/open/js/jweixin-1.1.0.js

    3.通过config接口注入权限验证配置

    wx.config({
        debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
        appId: '', // 必填,公众号的唯一标识
        timestamp: , // 必填,生成签名的时间戳
        nonceStr: '', // 必填,生成签名的随机串
        signature: '',// 必填,签名,见附录1
        jsApiList: [] // 必填,需要使用的JS接口列表
    });

    4.通过ready接口处理成功验证

    wx.ready(function(){
        // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作。
    });

    5.在ready方法里进行相关接口的调用,比如:分享到微信朋友圈

    wx.onMenuShareTimeline({
        title: '', // 分享标题
        link: '', // 分享链接
        imgUrl: '', // 分享图标
        success: function () { 
            // 用户确认分享后执行的回调函数
        },
        cancel: function () { 
            // 用户取消分享后执行的回调函数
        }
    });

    调用流程其实很简单,但是第3步config需要的东西需要准备一下,还是阅读文档:

    签名算法

    签名生成规则如下:参与签名的字段包括

    1.noncestr(随机字符串)

    2.有效的jsapi_ticket

    1.获取access_token
    
    http请求方式: GET
    https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
    
    2.用第一步拿到的access_token 获得jsapi_ticket
    
    http请求方式: GET
    
    https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi

    3.timestamp(时间戳)

    4.url(当前网页的URL,不包含#及其后面部分)

    5.对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式(key1=value1&key2=value2…)均为小写字符,拼接成字符串string1。

    对string1作sha1加密,字段名和字段值都采用原始值,不进行URL 转义

    注意事项

    1.签名用的noncestr和timestamp必须与wx.config中的nonceStr和timestamp相同。

    2.签名用的url必须是调用JS接口页面的完整URL。

    3.出于安全考虑,开发者必须在服务器端实现签名的逻辑。

    附上C#代码:

     public static string GetSha1Str(string str)
            {
                byte[] strRes = Encoding.UTF8.GetBytes(str);
                HashAlgorithm iSha = new SHA1CryptoServiceProvider();
                strRes = iSha.ComputeHash(strRes);
                var enText = new StringBuilder();
                foreach (byte iByte in strRes)
                {
                    enText.AppendFormat("{0:x2}", iByte);
                }
                return enText.ToString();
            }
     public static string GetAccessToken()
            {
                string apiUrl = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, secret);
                string token = null;
                if (System.Web.HttpContext.Current.Session["JsAccessToken"] == null)
                {
                    string result = GetMethod(apiUrl);
                    if (!string.IsNullOrEmpty(result))
                    {
                        JObject jo = (JObject)JsonConvert.DeserializeObject(result);
                        token = jo["access_token"].ToString();
                        System.Web.HttpContext.Current.Session["JsAccessToken"] = token;
                        System.Web.HttpContext.Current.Session.Timeout = 7200;
                    }
                }
                else
                {
                    token = System.Web.HttpContext.Current.Session["JsAccessToken"].ToString();
                }
                return token;
            }
     public static string GetJsApiTicket()
            {
                string apiUrl = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", GetAccessToken());
                string ticket = null;
                if (System.Web.HttpContext.Current.Session["JsApiTicket"] == null)
                {
                    string result = GetMethod(apiUrl);
                    if (!string.IsNullOrEmpty(result))
                    {
                        JObject jo = (JObject)JsonConvert.DeserializeObject(result);
                        ticket = jo["ticket"].ToString();
                        System.Web.HttpContext.Current.Session["JsApiTicket"] = ticket;
                        System.Web.HttpContext.Current.Session.Timeout = 7200;
                    }
                }
                else
                {
                    ticket = System.Web.HttpContext.Current.Session["JsApiTicket"].ToString();
                }
                return ticket;
            }
      public static string Sign(string jsapiTicket, string nonceStr, string timestamp, string url)
            {
                string str = string.Format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}", jsapiTicket, nonceStr, timestamp, url);
                return GetSha1Str(str);
            }
      public static string DateToUnix()
            {
                DateTime timeStamp = new DateTime(1970, 1, 1);  //得到1970年的时间戳
                long a = (DateTime.UtcNow.Ticks - timeStamp.Ticks) / 10000000;
                return a.ToString();
            }

    以上,即可实现微信网页中使用JS-SDK调用微信浏览器的各种接口。

  • 相关阅读:
    创建者模式 -- 单例模式(反射&序列化)
    设计模式(总)
    并不是static final 修饰的变量都是编译期常量
    内部类和静态内部类的加载顺序
    所有的Java虚拟机必须实现在每个类或接口被Java程序 “ 首次主动使用 ” 时才初始化他们
    24 shell 管道命令与过滤器
    26 bash shell中的信号
    25 Linux中的信号
    23 shell 进程替换
    22 shell组命令与子进程
  • 原文地址:https://www.cnblogs.com/talentzemin/p/5462011.html
Copyright © 2011-2022 走看看