微信分享二

来源:互联网 发布:ps4数码宝贝网络侦探 编辑:程序博客网 时间:2024/06/07 20:03

define(['app','constants','angular'], function (app,constants,angular) {    'use strict';    app.controller('mainCtrl', ['$scope','$rootScope','$state','$q',        function ($scope,$rootScope,$state,$q) {            var url  = location.href.split('#')[0].replace("&","%26");            var link ;//链接            var title = "";//标题            var desc = "城乡保险";//描述            var imgUrl ;//图片            $.ajax({                type: "POST",                async:true,                cache: false,                url: "/shiewarp-web/proposal/getWechatSignature?url="+url,                success: function(message){                    link = message.wxLink;                    imgUrl = message.wxImage;                    wx.config({                        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。                        appId: message.appid, // 必填,公众号的唯一标识                        timestamp: message.timestamp+'', // 必填,生成签名的时间戳                        nonceStr: message.nonceStr+'', // 必填,生成签名的随机串                        signature: message.signature+'',// 必填,签名,见附录1                        jsApiList: ['onMenuShareTimeline',                            'onMenuShareAppMessage',                            'onMenuShareQQ',                            'onMenuShareQZone'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2                    });                    //通过error接口处理失败验证                    wx.error(function (res) {                    });                    wx.ready(function(){                        wx.onMenuShareTimeline({                            title: title, // 分享标题                            link: link, // 分享链接                            imgUrl: imgUrl, // 分享图标                            success: function () {                            },                            cancel: function () {                            }                        });                        wx.onMenuShareAppMessage({                            title: title, // 分享标题                            desc: desc, // 分享描述                            link: link, // 分享链接                            imgUrl:imgUrl, // 分享图标                            type: '', // 分享类型,music、video或link,不填默认为link                            dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空                            success: function () {                            },                            cancel: function () {                            }                        });                        wx.onMenuShareQQ({                            title: title, // 分享标题                            desc: desc, // 分享描述                            link: link, // 分享链接                            imgUrl: imgUrl, // 分享图标                            success: function () {                            },                            cancel: function () {                            }                        });                        wx.onMenuShareQZone({                            title: title, // 分享标题                            desc: desc, // 分享描述                            link: link, // 分享链接                            imgUrl: imgUrl, // 分享图标                            success: function () {                            },                            cancel: function () {                            }                        });                    });                }            });            /* *            * 获取用户已登录信息            * */            //var user = $$user.getUser();            //$scope.loginUser = {};            //$scope.loginUser.userCode = user.userCode;            //$scope.loginUser.userInfo_Name = user.userName;            //console.info("welcode:",user.userName);        }]);});
    @RequestMapping(value = "getWechatSignature",produces = "text/json;charset=UTF-8")    @ResponseBody    public String getWechatSignature(String url){        TokenInfo tokenInfo =  tokenInfoDao.getTokenInfo("weixinTocket");        Map<String, String> responseMap =  WechatSDK.sign(tokenInfo.getToken(),url);        ObjectMapper objectMapper = new ObjectMapper();        String jsonString = null;        try {            jsonString = objectMapper.writeValueAsString(responseMap);        } catch (IOException e) {            e.printStackTrace();        }        return  jsonString;    }
    public static Map<String, String> sign(String jsapi_ticket, String url) {        Map<String, String> ret = new HashMap<String, String>();        String nonce_str = create_nonce_str();        String timestamp = create_timestamp();        String string1;        String signature = "";
        //注意这里参数名必须全部小写,且必须有序        string1 = "jsapi_ticket=" + jsapi_ticket +                "&noncestr=" + nonce_str +                "&timestamp=" + timestamp +                "&url=" + url;        System.out.println(string1);
        try        {            MessageDigest crypt = MessageDigest.getInstance("SHA-1");            crypt.reset();            crypt.update(string1.getBytes("UTF-8"));            signature = byteToHex(crypt.digest());        }        catch (NoSuchAlgorithmException e)        {            e.printStackTrace();        }        catch (UnsupportedEncodingException e)        {            e.printStackTrace();        }
        ret.put("url", url);        ret.put("jsapi_ticket", jsapi_ticket);        ret.put("nonceStr", nonce_str);        ret.put("timestamp", timestamp);        ret.put("signature", signature);
        return ret;    }
    private static String byteToHex(final byte[] hash) {        Formatter formatter = new Formatter();        for (byte b : hash)        {            formatter.format("%02x", b);        }        String result = formatter.toString();        formatter.close();        return result;    }
    private static String create_nonce_str() {        return UUID.randomUUID().toString();    }
    private static String create_timestamp() {        return Long.toString(System.currentTimeMillis() / 1000);    }
@Service("accessTokenService")public class AccessTokenServiceImpl implements AccessTokenService {
    private final String wechatApi =  "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
    private final String wechatTicket="https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&";
      public Map getAccessToken(String appID, String appSecret) throws IOException {        String result = "";        HttpGet httpGet =  new HttpGet(wechatApi+"&appid="+appID+"&secret="+appSecret);//这里发送get请求        HttpClient httpClient = new DefaultHttpClient();        HttpResponse response = httpClient.execute(httpGet);        // 判断网络连接状态码是否正常(0--200都数正常)        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {            result= EntityUtils.toString(response.getEntity(),"utf-8");        }        Map<String,Object>  returnMap =  JSON.parseObject(result, Map.class);        return returnMap;    }
        public Map<String, Object> getTicket(String token) throws IOException {        String result = "";        HttpGet httpGet =  new HttpGet(wechatTicket+"&access_token="+token);//这里发送get请求        HttpClient httpClient = new DefaultHttpClient();        HttpResponse response = httpClient.execute(httpGet);        // 判断网络连接状态码是否正常(0--200都数正常)        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {            result= EntityUtils.toString(response.getEntity(),"utf-8");        }        Map<String,Object>  returnMap =  JSON.parseObject(result, Map.class);        return returnMap;    }
0 0
原创粉丝点击