Skip to content

Commit 366c32c

Browse files
author
“yluchanan
committed
1.0
0 parents  commit 366c32c

16 files changed

+904
-0
lines changed

Jssdk.class.php

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
class JSSDK {
3+
private $appId;
4+
private $appSecret;
5+
6+
public function __construct($appId, $appSecret) {
7+
$this->appId = $appId;
8+
$this->appSecret = $appSecret;
9+
}
10+
11+
public function getSignPackage() {
12+
$jsapiTicket = $this->getJsApiTicket();
13+
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
14+
$timestamp = time();
15+
$nonceStr = $this->createNonceStr();
16+
17+
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
18+
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
19+
20+
$signature = sha1($string);
21+
22+
$signPackage = array(
23+
"appId" => $this->appId,
24+
"nonceStr" => $nonceStr,
25+
"timestamp" => $timestamp,
26+
"url" => $url,
27+
"signature" => $signature,
28+
"rawString" => $string
29+
);
30+
return $signPackage;
31+
}
32+
33+
private function createNonceStr($length = 16) {
34+
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
35+
$str = "";
36+
for ($i = 0; $i < $length; $i++) {
37+
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
38+
}
39+
return $str;
40+
}
41+
42+
private function getJsApiTicket() {
43+
// jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
44+
$data = json_decode(file_get_contents("jsapi_ticket.json"));
45+
if ($data->expire_time < time()) {
46+
$accessToken = $this->getAccessToken();
47+
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
48+
$res = json_decode($this->httpGet($url));
49+
$ticket = $res->ticket;
50+
if ($ticket) {
51+
$data->expire_time = time() + 7000;
52+
$data->jsapi_ticket = $ticket;
53+
$fp = fopen("jsapi_ticket.json", "w");
54+
fwrite($fp, json_encode($data));
55+
fclose($fp);
56+
}
57+
} else {
58+
$ticket = $data->jsapi_ticket;
59+
}
60+
61+
return $ticket;
62+
}
63+
64+
private function getAccessToken() {
65+
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
66+
$data = json_decode(file_get_contents("access_token.json"));
67+
if ($data->expire_time < time()) {
68+
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
69+
$res = json_decode($this->httpGet($url));
70+
$access_token = $res->access_token;
71+
if ($access_token) {
72+
$data->expire_time = time() + 7000;
73+
$data->access_token = $access_token;
74+
$fp = fopen("access_token.json", "w");
75+
fwrite($fp, json_encode($data));
76+
fclose($fp);
77+
}
78+
} else {
79+
$access_token = $data->access_token;
80+
}
81+
return $access_token;
82+
}
83+
84+
private function httpGet($url) {
85+
$curl = curl_init();
86+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
87+
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
88+
curl_setopt($curl, CURLOPT_URL, $url);
89+
90+
$res = curl_exec($curl);
91+
curl_close($curl);
92+
93+
return $res;
94+
}
95+
}

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
##该插件是基于jquery或者zepto来实现的h5移动端分享插件
2+
3+
##h5Share实现的功能有:
4+
5+
* 在UC浏览器或者QQ浏览器实现打开手机QQ(发送好友),微信APP朋友圈、发送好友的自定义分享。
6+
7+
![image](http://7qn7ih.com1.z0.glb.clouddn.com/20160216154326.png)
8+
![image](http://7qn7ih.com1.z0.glb.clouddn.com/20160216160333.jpg)
9+
![image](http://7qn7ih.com1.z0.glb.clouddn.com/20160216161011.jpg)
10+
* 微信浏览器浏览器右上角提示分享箭头。
11+
![image](http://7qn7ih.com1.z0.glb.clouddn.com/20160216154336.png)
12+
![image](http://7qn7ih.com1.z0.glb.clouddn.com/20160216154331.png)
13+
14+
* 在非上面的UC、QQ浏览器,微信其他浏览器只会显示QQ空间和新浪微博的h5分享ICON。
15+
16+
![image](http://7qn7ih.com1.z0.glb.clouddn.com/20160216154308.png)
17+
18+
##注意问题
19+
20+
* 在微信浏览器想实现分享,必须要在公众平台配置JS安全域名。
21+
* UC浏览器中的分享朋友圈、发送好友暂时没有发送图片的配置。(貌似没有UC没有提供接口)。

flexible.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
!
2+
function(a, b) {
3+
function c() {
4+
var b = f.getBoundingClientRect().width;
5+
b / i > 540 && (b = 540 * i);
6+
var c = b / 10;
7+
f.style.fontSize = c + "px",
8+
k.rem = a.rem = c
9+
}
10+
var d, e = a.document,
11+
f = e.documentElement,
12+
g = e.querySelector('meta[name="viewport"]'),
13+
h = e.querySelector('meta[name="flexible"]'),
14+
i = 0,
15+
j = 0,
16+
k = b.flexible || (b.flexible = {});
17+
if (g) {
18+
console.warn("将根据已有的meta标签来设置缩放比例");
19+
var l = g.getAttribute("content").match(/initial\-scale=([\d\.]+)/);
20+
l && (j = parseFloat(l[1]), i = parseInt(1 / j))
21+
} else if (h) {
22+
var m = h.getAttribute("content");
23+
if (m) {
24+
var n = m.match(/initial\-dpr=([\d\.]+)/),
25+
o = m.match(/maximum\-dpr=([\d\.]+)/);
26+
n && (i = parseFloat(n[1]), j = parseFloat((1 / i).toFixed(2))),
27+
o && (i = parseFloat(o[1]), j = parseFloat((1 / i).toFixed(2)))
28+
}
29+
}
30+
if (!i && !j) {
31+
var p = (a.navigator.appVersion.match(/android/gi), a.navigator.appVersion.match(/iphone/gi)),
32+
q = a.devicePixelRatio;
33+
i = p ? q >= 3 && (!i || i >= 3) ? 3 : q >= 2 && (!i || i >= 2) ? 2 : 1 : 1,
34+
j = 1 / i
35+
}
36+
if (f.setAttribute("data-dpr", i), !g) if (g = e.createElement("meta"), g.setAttribute("name", "viewport"), g.setAttribute("content", "initial-scale=" + j + ", maximum-scale=" + j + ", minimum-scale=" + j + ", user-scalable=no"), f.firstElementChild) f.firstElementChild.appendChild(g);
37+
else {
38+
var r = e.createElement("div");
39+
r.appendChild(g),
40+
e.write(r.innerHTML)
41+
}
42+
a.addEventListener("resize",
43+
function() {
44+
clearTimeout(d),
45+
d = setTimeout(c, 300)
46+
},
47+
!1),
48+
a.addEventListener("pageshow",
49+
function(a) {
50+
a.persisted && (clearTimeout(d), d = setTimeout(c, 300))
51+
},
52+
!1),
53+
"complete" === e.readyState ? e.body.style.fontSize = 12 * i + "px": e.addEventListener("DOMContentLoaded",
54+
function() {
55+
e.body.style.fontSize = 12 * i + "px"
56+
},
57+
!1),
58+
c(),
59+
k.dpr = a.dpr = i,
60+
k.refreshRem = c,
61+
k.rem2px = function(a) {
62+
var b = parseFloat(a) * this.rem;
63+
return "string" == typeof a && a.match(/rem$/) && (b += "px"),
64+
b
65+
},
66+
k.px2rem = function(a) {
67+
var b = parseFloat(a) / this.rem;
68+
return "string" == typeof a && a.match(/px$/) && (b += "rem"),
69+
b
70+
}
71+
} (window, window.lib || (window.lib = {}));

flexible_css.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

images/share/friend.png

9.28 KB
Loading

images/share/qq.png

7.33 KB
Loading

images/share/qzone.png

4.88 KB
Loading

images/share/share.png

23.4 KB
Loading

images/share/sina.png

6.75 KB
Loading

images/share/wechat.png

5.7 KB
Loading

index.php

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="keywords" content="" >
6+
<meta name="description" content="">
7+
<meta content="yes" name="apple-mobile-web-app-capable" />
8+
<meta content="yes" name="apple-touch-fullscreen" />
9+
<meta name="full-screen" content="yes">
10+
<meta name="x5-fullscreen" content="true" />
11+
<meta name="screen-orientation" content="portrait">
12+
<meta name="x5-orientation" content="portrait">
13+
<meta content="telephone=no,email=no" name="format-detection" />
14+
<meta name="viewport" content="width=device-width, initial-scale=1">
15+
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
16+
<link rel="stylesheet" href="share.css">
17+
<style>
18+
.btn{
19+
-webkit-appearance:none;
20+
display:block;
21+
font-size:16px;
22+
width:100%;
23+
line-height:40px;
24+
background:red;
25+
border:none;
26+
border-radius:0;
27+
color:#fff;
28+
}
29+
30+
</style>
31+
<title>分享插件,UC浏览器,手机QQ浏览器支持微信好友,朋友圈分享</title>
32+
</head>
33+
<body>
34+
<!--输入您微公司信公众号(或者测试号)的appId,appSecret;否则在微信会报错-->
35+
<?php
36+
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false){
37+
include 'Jssdk.class.php';
38+
$jssdk = new JSSDK('appId','appSecret');
39+
$signPackage = $jssdk->GetSignPackage();
40+
}
41+
?>
42+
<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p>
43+
<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p>
44+
<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p>
45+
<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p>
46+
<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p>
47+
<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p>
48+
<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p>
49+
<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p>
50+
<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p>
51+
<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p>
52+
<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p>
53+
<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p>
54+
<button type="button" class="btn" id="share">立即分享</button>
55+
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
56+
<script src="jquery.min.js"></script>
57+
<script src="flexible.js"></script>
58+
<script src="flexible_css.js"></script>
59+
<script src="share.js"></script>
60+
<script>
61+
<?php if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false){?>
62+
appShare({
63+
ele:'#share',//可选
64+
title:"标题",//可选
65+
desc:'描述',//可选
66+
appId: "<?php echo $signPackage['appId']?>",//可选
67+
timestamp:"<?php echo $signPackage['timestamp']?>",//可选
68+
nonceStr: "<?php echo $signPackage['nonceStr']?>",//可选
69+
signature: "<?php echo $signPackage['signature']?>"//可选
70+
});
71+
<?php }else{?>
72+
appShare({
73+
ele:'#share',//可选
74+
title:"标题",//可选
75+
desc:'描述',//可选
76+
});
77+
<?php };?>
78+
</script>
79+
</body>
80+
</html>

jquery.min.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

no_element.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="keywords" content="" >
6+
<meta name="description" content="">
7+
<meta content="yes" name="apple-mobile-web-app-capable" />
8+
<meta content="yes" name="apple-touch-fullscreen" />
9+
<meta name="full-screen" content="yes">
10+
<meta name="x5-fullscreen" content="true" />
11+
<meta name="screen-orientation" content="portrait">
12+
<meta name="x5-orientation" content="portrait">
13+
<meta content="telephone=no,email=no" name="format-detection" />
14+
<meta name="viewport" content="width=device-width, initial-scale=1">
15+
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
16+
<link rel="stylesheet" href="share.css">
17+
<title>分享插件,UC浏览器,手机QQ浏览器支持微信好友,朋友圈分享</title>
18+
</head>
19+
<body>
20+
<!--输入你微信公众号的appId,appSecret-->
21+
<?php
22+
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false){
23+
include 'Jssdk.class.php';
24+
$jssdk = new JSSDK('appId','appSecret');
25+
$signPackage = $jssdk->GetSignPackage();
26+
}
27+
?>
28+
分享该页面
29+
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
30+
<script src="jquery.min.js"></script>
31+
<script src="flexible.js"></script>
32+
<script src="flexible_css.js"></script>
33+
<script src="share.js"></script>
34+
<script>
35+
//适用于不需要绑定的元素,直接分享页面
36+
<?php if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false){?>
37+
appShare({
38+
title:"标题",//可选
39+
desc:'描述',//可选
40+
appId: "<?php echo $signPackage['appId']?>",//可选
41+
timestamp:"<?php echo $signPackage['timestamp']?>",//可选
42+
nonceStr: "<?php echo $signPackage['nonceStr']?>",//可选
43+
signature: "<?php echo $signPackage['signature']?>"//可选
44+
});
45+
<?php }else{?>
46+
appShare({
47+
title:"标题",//可选
48+
desc:'描述',//可选
49+
});
50+
<?php };?>
51+
</script>
52+
</body>
53+
</html>

0 commit comments

Comments
 (0)