vue鍋氬井淇″垎浜�

1銆佷笅杞絯eixin-js-sdk 鐨勪緷璧�

cnpm install weixin-js-sdk --save

2銆佸湪闇�瑕佸垎浜殑鏂囦欢涓紩鍏�

import wx from 'weixin-js-sdk'

3銆佸垎浜強鐩稿叧鐨勫墠绔唬鐮�

mounted:function () {

let urlStr = location.href.split('#')[0];

const ua = window.navigator.userAgent.toLowerCase();

if(ua.match(/MicroMessenger/i) == 'micromessenger'){

//鑾峰彇绛惧悕涓�

this.$http.post(BaseConfig.url+'/wechat/getSignature',

{"urlStr":urlStr}

).then((res) => {

// 璇锋眰鎴愬姛鐨勫洖璋�

var result = res.data;

if(result.code==1){

let data = JSON.parse(result.data);

wx.config({

debug : false, // 寮�鍚皟璇曟ā寮�,璋冪敤鐨勬墍鏈塧pi鐨勮繑鍥炲�间細鍦ㄥ鎴风alert鍑烘潵锛岃嫢瑕佹煡鐪嬩紶鍏ョ殑鍙傛暟锛屽彲浠ュ湪pc绔墦寮�锛屽弬鏁颁俊鎭細閫氳繃log鎵撳嚭锛屼粎鍦╬c绔椂鎵嶄細鎵撳嵃銆�

appId : data.appId, // 蹇呭~锛屽叕浼楀彿鐨勫敮涓�鏍囪瘑

timestamp : data.timestamp, // 蹇呭~锛岀敓鎴愮鍚嶇殑鏃堕棿鎴�

nonceStr : data.nonceStr, // 蹇呭~锛岀敓鎴愮鍚嶇殑闅忔満涓�

signature : data.signature,// 蹇呭~锛岀鍚嶏紝瑙侀檮褰�1

jsApiList: [聽 //蹇呭~锛岄渶瑕佷娇鐢ㄧ殑JS鎺ュ彛鍒楄〃

'onMenuShareTimeline',

'onMenuShareAppMessage',

'hideMenuItems',

'showMenuItems'

]

});

let title = "鍒嗕韩鐨勬爣棰�";

let desc = "鍒嗕韩鐨勫壇鏍囬";

let link = "鍒嗕韩鐨勯摼鎺ワ紝鍩熷悕瑕佽窡寰俊鍏紬鍙烽厤缃殑瀹夊叏鍩熷悕涓�鑷�";

let imgUrl = "鍒嗕韩鍥剧墖鐨剈rl";

wx.ready(function() {

//鎵归噺鏄剧ず鑿滃崟椤�

wx.showMenuItems({

menuList: [

"menuItem:share:appMessage",

"menuItem:share:timeline"

], // 瑕佹樉绀虹殑鑿滃崟椤�

success: function (res) {},

fail: function (res) {}

});

// 鎵归噺闅愯棌鑿滃崟椤�

wx.hideMenuItems({

menuList: [

'menuItem:share:qq', //鍒嗕韩鍒癚Q

'menuItem:share:weiboApp', // 鍒嗕韩鍒癢eibo

'menuItem:favorite', // 鏀惰棌

'menuItem:share:QZone', // 鍒嗕韩鍒� QQ 绌洪棿

'menuItem:copyUrl', // 澶嶅埗閾炬帴

'menuItem:openWithQQBrowser', // 鍦≦Q娴忚鍣ㄤ腑鎵撳紑

'menuItem:openWithSafari', // 鍦⊿afari涓墦寮�

'menuItem:share:email', // 閭欢

'menuItem:readMode', // 闃呰妯″紡

'menuItem:originPage' // 鍘熺綉椤�

] ,

success: function (res) {},

fail: function (res) {}

});

//鍒嗕韩缁欐湅鍙�

wx.onMenuShareAppMessage({

title : title,// 鍒嗕韩鏍囬

desc : desc,// 鍒嗕韩鎻忚堪

link : link,// 鍒嗕韩閾炬帴锛岃閾炬帴鍩熷悕鎴栬矾寰勫繀椤讳笌褰撳墠椤甸潰瀵瑰簲鐨勫叕浼楀彿JS瀹夊叏鍩熷悕涓�鑷�

imgUrl : imgUrl, // 鍒嗕韩鍥炬爣

type : 'link', // 鍒嗕韩绫诲瀷,music銆乿ideo鎴杔ink锛屼笉濉粯璁や负link

dataUrl : '', // 濡傛灉type鏄痬usic鎴杤ideo锛屽垯瑕佹彁渚涙暟鎹摼鎺ワ紝榛樿涓虹┖

success : function() {},

cancel : function() {}

});

//鍒嗕韩鍒版湅鍙嬪湀

wx.onMenuShareTimeline({

title : title,// 鍒嗕韩鏍囬

link : link,// 鍒嗕韩閾炬帴锛岃閾炬帴鍩熷悕鎴栬矾寰勫繀椤讳笌褰撳墠椤甸潰瀵瑰簲鐨勫叕浼楀彿JS瀹夊叏鍩熷悕涓�鑷�

imgUrl : imgUrl, // 鍒嗕韩鍥炬爣

success : function() {},

cancel : function() {}

});

});

wx.error(function(res) {

// config淇℃伅楠岃瘉澶辫触浼氭墽琛宔rror鍑芥暟锛屽绛惧悕杩囨湡瀵艰嚧楠岃瘉澶辫触锛屽叿浣撻敊璇俊鎭彲浠ユ墦寮�config鐨刣ebug妯″紡鏌ョ湅锛屼篃鍙互鍦ㄨ繑鍥炵殑res鍙傛暟涓煡鐪嬶紝瀵逛簬SPA鍙互鍦ㄨ繖閲屾洿鏂扮鍚嶃��

alert("error==" + res.errMsg);

});

}else{

wx.hideOptionMenu();

}

},聽 (err) => {

// 璇锋眰澶辫触鐨勫洖璋�

console.log("鑾峰彇绛惧悕涓插嚭閿欙紒"+err);

}

);

}

}

4銆佸悗绔幏鍙栫鍚嶄覆鍙傝�冧唬鐮�

public String getSignature(String urlStr) { logger.info("urlStr:"+urlStr); MapmodelMap = new HashMap();

String timestamp = System.currentTimeMillis()/1000+"";//鏃堕棿鎴陈� 鍗曚綅鏄

String nonceStr = "";//闅忔満瀛楃涓�

String jsapi_ticket = null;//璋冪敤鎺ュ彛鑾峰彇

try {

jsapi_ticket = "璇锋眰寰俊鏈嶅姟鍣ㄨ幏鍙杍sapi_ticket锛屽彲鍙傝�僡pi";

logger.info("jsapi_ticket:"+jsapi_ticket);

String signValue = "jsapi_ticket="+jsapi_ticket+"&noncestr="+nonceStr+"脳tamp="+timestamp+"&url="+urlStr;

logger.info("signValue:"+signValue);

String signature = getSha1(signValue);

logger.info("signature:"+signature);

modelMap.put("timestamp", timestamp);//鐢熸垚绛惧悕鐨勬椂闂存埑

modelMap.put("nonceStr", nonceStr);//鐢熸垚绛惧悕鐨勯殢鏈轰覆

modelMap.put("appId","APPID");//鍏紬鍙风殑鍞竴鏍囪瘑

modelMap.put("signature",signature );//绛惧悕涓�

logger.info("json:"+StringUtil.toJson(modelMap));

return StringUtil.toJson(modelMap);

} catch (Exception e) {

e.printStackTrace();

throw new CustomException(ResultEnum.GET_JSAPI_TICKET_FAIL.getCode(),ResultEnum.GET_JSAPI_TICKET_FAIL.getMsg());

}

}

// 瀵瑰瘑閽ヨ繘琛宻ha1鍔犲瘑

public static String getSha1(String str) {

if (str == null || str.length() == 0) {

return null;

}

char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

'a', 'b', 'c', 'd', 'e', 'f' };

try {

MessageDigest mdTemp = MessageDigest.getInstance("SHA1");

mdTemp.update(str.getBytes("GBK"));

byte[] md = mdTemp.digest();

int j = md.length;

char buf[] = new char[j * 2];

int k = 0;

for (int i = 0; i < j; i++) {

byte byte0 = md[i];

buf[k++] = hexDigits[byte0 >>> 4 & 0xf];

buf[k++] = hexDigits[byte0 & 0xf];

}

return new String(buf);

} catch (Exception e) {

return null;

}

}

你可能感兴趣的:(vue鍋氬井淇″垎浜�)