微信公眾號的總結(jié)
用戶授權(quán)
- 判斷無code參數(shù)(重定向后會獲取code)
var query = {
domain: 'https://open.weixin.qq.com/connect/oauth2/authorize',//請求code的域名
appid: '公眾號的appid(wx1234567898765432)',//appid
scope: 'snsapi_userinfo',//snsapi_userinfo(非靜默)
state: saveSharingPerson(),//自定義分享的參數(shù)
redirect_uri: encodeURIComponent(window.location.href),//wx重定向地址 redirect_uri
response_type: 'code'
}
var url = query.domain + '?appid=' + query.appid + '&redirect_uri='+_self.$basePath+'/auth' +'&response_type=code&scope=snsapi_base&state='+query.state+'#wechat_redirect'
window.location.href = url//路徑跳轉(zhuǎn)
- 判斷參數(shù)有code(根據(jù)code獲取用戶信息)
獲取用戶信息后跳轉(zhuǎn)頁面(最好replace)
微信支付的調(diào)用
import wx from 'weixin-js-sdk'
function payApi(data){
return new Promise((resolve, reject) => {
wx.config({
debug: false, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數(shù)瞒津,可以在pc端打開,參數(shù)信息會通過log打出糕伐,僅在pc端時才會打印。
appId: data.appId, // 必填揖曾,公眾號的唯一標識
timestamp: data.timeStamp, // 必填刺彩,生成簽名的時間戳
nonceStr: data.nonceStr, // 必填,生成簽名的隨機串
signature: data.signature, // 必填躯砰,簽名每币,見附錄1
jsApiList: ["chooseWXPay", "getLocation"] // 必填,需要使用的JS接口列表琢歇,所有JS接口列表見附錄2
});
wx.ready(function() {
if (typeof WeixinJSBridge == "undefined") {
if(('typeof WeixinJSBridge == undefined')){
resolve(res)
return;
}
if(document.addEventListener) {
document.addEventListener(
"WeixinJSBridgeReady",
onBridgeReady,
false
);
}else if (document.attachEvent) {
document.attachEvent("WeixinJSBridgeReady", onBridgeReady);
document.attachEvent("onWeixinJSBridgeReady", onBridgeReady);
}
}else{
WeixinJSBridge.invoke(
"getBrandWCPayRequest", {
appId: data.appId, //公眾號名稱兰怠,由商戶傳入
timeStamp: data.timeStamp, //時間戳,自1970年以來的秒數(shù)
nonceStr: data.nonceStr, //隨機串
package: data.package,//訂單詳情擴展字符串 統(tǒng)一下單接口返回的prepay_id參數(shù)值李茫,提交格式如:prepay_id=***
signType: "MD5", //微信簽名方式:
paySign: data.paySign //微信簽名
},
function(res) {
if (res.err_msg != "get_brand_wcpay_request:ok") {// 使用以上方式判斷前端返回,微信團隊鄭重提示:res.err_msg將在用戶支付成功后返回ok揭保,但并不保證它絕對可靠。
reject(res)
} else {
resolve(res)
}
}
)}
})
})
}
//返回data
{
"appId":"wxa69d932fd28deb71",
"nonceStr":"0a1317ac6d5c4cf18cd223b5088d3fdd",
"orderId":"102268847030590",
"package":"prepay_id=wx20180226111446966f8718dc0989102255",
"paySign":"9182F2CB8AAAFB3C694693FF5FF36BFC",
"signType":"MD5",
"signature":"b15aa9625efb36cda79d6d9e631f1bba8ad53e20",
"timeStamp":"1519614886"
}
payApi(options)
.then((res)=>{})//支付成功
.catch((err)=>{})//支付失敗
微信的sdk的信息分享配置
wx.config({
debug: true, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會在客戶端alert出來魄宏,若要查看傳入的參數(shù)秸侣,可以在pc端打開,參數(shù)信息會通過log打出宠互,僅在pc端時才會打印味榛。
appId: data.appId, // 必填,公眾號的唯一標識
timestamp: data.timeStamp, // 必填予跌,生成簽名的時間戳
nonceStr: data.nonceStr, // 必填搏色,生成簽名的隨機串
signature: data.signature, // 必填,簽名券册,見附錄1
jsApiList: ['checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'showMenuItems'
] // 必填频轿,需要使用的JS接口列表,所有JS接口列表見附錄2
});
wx.ready(function () {
var link = window.location.href;
var protocol = window.location.protocol;
var host = window.location.host;
//分享朋友圈
wx.onMenuShareTimeline({
title: '這是一個自定義的標題烁焙!',
link: link,
imgUrl: protocol+'//'+host+'/resources/images/icon.jpg',// 自定義圖標
trigger: function (res) {
// 不要嘗試在trigger中使用ajax異步請求修改本次分享的內(nèi)容航邢,因為客戶端分享操作是一個同步操作,這時候使用ajax的回包會還沒有返回.
//alert('click shared');
},
success: function (res) {
//alert('shared success');
//some thing you should do
},
cancel: function (res) {
//alert('shared cancle');
},
fail: function (res) {
//alert(JSON.stringify(res));
}
});
//分享給好友
wx.onMenuShareAppMessage({ //onMenuShareQQ onMenuShareTimeline
title: '這是一個自定義的標題骄蝇!', // 分享標題
desc: '這是一個自定義的描述翠忠!', // 分享描述
link: link, // 分享鏈接,該鏈接域名或路徑必須與當前頁面對應的公眾號JS安全域名一致
imgUrl: protocol+'//'+host+'/resources/images/icon.jpg', // 自定義圖標
type: 'link', // 分享類型,music乞榨、video或link,不填默認為link
dataUrl: '', // 如果type是music或video当娱,則要提供數(shù)據(jù)鏈接吃既,默認為空
success: function () {
// 用戶確認分享后執(zhí)行的回調(diào)函數(shù)
},
cancel: function () {
// 用戶取消分享后執(zhí)行的回調(diào)函數(shù)
}
});
wx.error(function (res) {
alert(res.errMsg);
});
});
//配置信息data
data:{
"URLLLLL":"https://www.baidu.com/testOAserver/deposit",
"appId":"wxa69d932fd28deb71",
"nonceStr":"cde254ba68a44b8c86ef17727fbf1f1e",
"signature":"66da685aeed95f309c7feee7c00863ac80f29805",
"timeStamp":"1519617353"
}