继续上一篇,这章写网页jssdk验证,完成后,公众号开发,就基本结束了。 然而发过一篇文章后才发现并不能被百度搜到,不知道以后会不会被百度搜到,有点失落,不然发出来有什么意思
flask wechatpy jssdk验证
server.py要几个文件了,所以标下
#今天不行了,要自己动手了
#不能享受一步到位了
#只是个测试程序,后面把token和ticket取出来单独储存
from flask import Flask, request, redirect, jsonify, session, render_template
import functools
from wechatpy.oauth import WeChatOAuth
from wechatpy.enterprise import WeChatClient
import wechatpy
import time
import random
app = Flask(__name__)
CORP_ID = 'wx*****************'
SECRET = 'cdc**********************'
client2 = wechatpy.WeChatClient(
CORP_ID,
SECRET
)
app.secret_key = 'key'
client = WeChatClient(
CORP_ID,
SECRET
)
url=''
@app.route('/wx/')
def index():
global url
print('')
#print()
code = request.args.get('code', None)
if code:
oauthClient = WeChatOAuth(app_id=CORP_ID ,
secret=SECRET,
redirect_uri=url)
try:
user_info = oauthClient.fetch_access_token(code)
except Exception as e:
print ('发生错误')
return render_template("html/网页应用.html")
else:
state = request.args.get('state', None)#stat自定义内容
url=client.oauth.authorize_url(request.url,state=state)
return redirect(url )
#因为百度搜不到,我也就不想怎注释了
#此处为微信jssdk的ajax请求
#需要在公众上设置该网址jssdk权限,别忘了
@app.route('/wx/jssdk',methods = ['POST'])
def wxjssdk():
print('………………………………………',request.form)
#print('-----------------',client2.jsapi.get_jsapi_card_params)
url=request.form.get('url')
ticket=client2.jsapi.get_jsapi_ticket()
print(ticket)
noncestr=''.join(random.sample(string.ascii_letters + string.digits, 8))
timestamp=str(int(time.time()))
signature=client2.jsapi.get_jsapi_signature(noncestr, ticket, timestamp, url)
print(signature)
return jsonify({'timestamp':timestamp,'nonceStr':noncestr,'signature':signature})
if __name__ == '__main__':
app.run(
host='0.0.0.0',port=443,debug = True,
ssl_context=(
'./SSL/1_jinxinhp.cn_bundle.crt',
'./SSL/2_jinxinhp.cn.key'
)
)#4个0表示监听所有ip。默认只监听本地
前端文件 html文件路径为 \templates\html\网页应用.html 网页应用.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>回调页面</title>
</head>
<body>
<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script>
// 请求签名
$.ajax({
url: "jssdk",
type: 'post',
data: { url: location.href.split('#')[0]},
success:function(res){
//alert('res: '+res.signature);
wx.config({
debug: false,
appId: 'wx-------------------',
timestamp: res.timestamp,
nonceStr: res.nonceStr,
signature: res.signature,
jsApiList: [
'onMenuShareTimeline',
'closeWindow',
'hideAllNonBaseMenuItem',
'showMenuItems',
'onMenuShareTimeline',
'getLocation','openLocation'
]
});
wx.ready(function () {//jssdk 初始化完成
alert('成功');
});
//wx.closeWindow();//关闭窗口"menuItem:exposeArticle"
wx.error(function (res) {
alert('错误:'+res.errMsg); // 正式环境记得关闭啊!!!!
});
}
});
///返回关闭窗口
$(function(){
pushHistory();//加入空的页面记录
window.addEventListener("popstate", function(e) {
wx.closeWindow();//关闭窗口
}, false);
function pushHistory() { //加入空的页面记录
var state = {
title: "title",
url: "#"
};
window.history.pushState(state, "title", "#");
}
}) ///返回关闭窗口
</script>
</body>
</html>
好了,网页应用到处结束了。你可以任意构建自己的程序了。 看看我过几天写不写支付,支付也一样。 下次写了支付再传一次