随笔 - 22  文章 - 0 评论 - 1 阅读 - 7843
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

闲来无事,花了几个小时研究了下 微信的 JSSDK。

将学习中所用到的代码都复制到这儿,以后查看的时候方便点.


PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$AppID = "";    //APPID
$AppSecret = "";    //APPSECRET
$token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$AppID&secret=$AppSecret";
$token_content = file_get_contents( $token_url );
// token 次数有限 需要缓存
$token = "" ;        //取得上面 获得的 token
 
$jsapi_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$tokenK&type=jsapi";
$jsapi_content = file_get_contents( $jsapi_url );
//  需要缓存
$url = "";    //调用 JSSDK 的 URL
$noncestr="";    //随机生成的 16位数,JS里也会用到
$jsapi_ticket = "";    //上面获得的ticket
$timestamp = time();
$string1 = "jsapi_ticket=$jsapi_ticket&noncestr=$noncestr&timestamp=$timestamp&url=$url";
//拼接字符串
$signature = sha1($string1);    //    JS的签名

  


JS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<html>
<head>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript" src="/themes/default/wap/js/zepto.js"></script>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<script>
    var wxConfig = {
            debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
            appId: '<{$AppID}>', // 必填,公众号的唯一标识
            timestamp: <{$timestamp}>, // 必填,生成签名的时间戳
            nonceStr: '<{$noncestr}>', // 必填,生成签名的随机串
            signature: '<{$signature}>',// 必填,签名,见附录1
            jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage','chooseImage','startRecord','scanQRCode','getLocation'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2<br>                //http://mp.weixin.qq.com/wiki/11/74ad127cc054f6b80759c40f77ec03db.html#.E9.99.84.E5.BD.952-.E6.89.80.E6.9C.89JS.E6.8E.A5.E5.8F.A3.E5.88.97.E8.A1.A8
        };
    wx.config(wxConfig);
</script>
</head>
<body>
<button id="share">分享到朋友圈</button>
<button id="share2">分享给朋友</button>
<button id="choose">选择图片</button>
<button id="record">录音录音</button>
<button id="QRcode">扫一扫</button>
<button id="where">我在哪</button>
</body>
</html>
<script>
    wx.ready(function(){
        $("#share").tap(function(){
            wx.onMenuShareTimeline({
                title: '酱油酱油', // 分享标题
                link: 'http://xxxx.com', // 分享链接
                imgUrl: '', // 分享图标
                success: function () {
                    alert("分享成功!");
                },
                cancel: function () {
                    alert("分享失败?");
                }
            });
        });
        $("#share2").tap(function(){
            wx.onMenuShareAppMessage({
                title: '酱油酱油', // 分享标题
                link: 'http://xxxx.com', // 分享链接
                imgUrl: '', // 分享图标
                success: function () {
                    alert("分享成功!");
                },
                cancel: function () {
                    alert("分享失败?");
                }
            });
        });
        $("#reload").tap(function(){
            location.reload();
        });
        $("#choose").tap(function(){
            wx.chooseImage({
                count: 1, // 默认9
                sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
                sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
                success: function (res) {
                    var localIds = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
                }
            });
        });
        $("#record").tap(function(){
            wx.startRecord({
              cancel: function () {
                alert('用户拒绝授权录音');
              }
            });
        });
        $("#QRcode").tap(function(){
            alert("kaishi")
            wx.scanQRCode({
                needResult: 0, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
                scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码,默认二者都有
                success: function (res) {
                    var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
                     
                }
            });
        });
        $("#where").tap(function(){
            alert("fff")
            wx.getLocation({
                type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
                success: function (res) {
                    var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
                    var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
                    var speed = res.speed; // 速度,以米/每秒计
                    var accuracy = res.accuracy; // 位置精度
                    var str = "纬度为"+latitude;
                        str += ";经度为"+longitude;
                    alert(str);
                }
            });
        });
    });
</script>

  

更多的详情 在 详情

 

posted on   小玥光  阅读(345)  评论(0编辑  收藏  举报
编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
· 程序员转型AI:行业分析
点击右上角即可分享
微信分享提示