俄罗斯贵宾会-俄罗斯贵宾会官网
做最好的网站

Editor富文本编辑器配置【不含图片上传】

前端时间,开发了一个资讯类的项目,但销售部门进行微信推广时,分享的链接直接是网页链接加分享符号,即难看又不正规,于是研究了一下微信自定义的分享功能

一,下载地址:http://ueditor.baidu.com/website/download.html

  1)根据需要下载相应版本

    例  Java:选择jsp+utf-8;

未配置自义定分享时,分享的页面:俄罗斯贵宾会 1

二,放置在项目中

  1)解压下载文件,并重命名为editor;

  2)复制文件放置在项目webapp目录下;

  3)把editor目录下的js文件(3个)剪切放在你的js文件目录下;

  4)复制editor内全部文件到js目录一份(不复制可以使用,但页面会报404错误,估计是在初始化时需要访问其他文件)

成功配置后:俄罗斯贵宾会 2  

三,配置静态资源映射

  1)在Springmvc.xml配置文件中配置相应属性(前提是已经在web.xml中配置了相应配置);

    web.xml配置:  

          <servlet>
          <servlet-name>portal</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation,

          springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
          <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring/springmvc.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
          </servlet>

          <servlet-mapping>
          <servlet-name>portal</servlet-name>
          <url-pattern>/</url-pattern>
          </servlet-mapping>

    Springmvc配置:    

          <mvc:resources mapping="/umeditor/**" location="/umeditor/" />      
          <mvc:resources mapping="/js/**" location="/js/" />

前期准备工作:

四,页面配置

  1)在head引入文件:    

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link href="umeditor/themes/default/css/umeditor.css" type="text/css" rel="stylesheet">
        <script type="text/javascript" src="umeditor/third-party/jquery.min.js"></script>
        <script type="text/javascript" src="umeditor/third-party/template.min.js"></script>
        <script type="text/javascript" charset="utf-8" src="js/umeditor.config.js"></script>
        <script type="text/javascript" charset="utf-8" src="js/umeditor.min.js"></script>
        <script type="text/javascript" src="umeditor/lang/zh-cn/zh-cn.js"></script>

  2)画出div放置编辑器:        

        <!-- 文本编辑器位置 -->
        <div id="myEditor" style="width:1000px;height:240px;">
          <p>提示内容</p>
        </div>

  3)实例化编辑器:        

        <script type="text/javascript">
          //实例化编辑器
          var um = UM.getEditor('myEditor');

        </script>

  4)常用方法(其他见官网demo):        

        <input type="button" value="获取内容" onclick="getAllHtml()">
        <input type="button" value="填充内容" onclick="setAllHtml()">

 

        <script type="text/javascript">

        //获取内容
        function getAllHtml() {
        alert(UM.getEditor('myEditor').getContent())
        }

        //填充内容
        function setAllHtml() {
        alert(UM.getEditor('myEditor').setContent())
        }

        </script>

1.认证公众号的appId,appSecret

五,效果图

    俄罗斯贵宾会 3俄罗斯贵宾会,  

2.各种获取微信信息链接,封装在配置文件里(此部分查找微信自定义分享API,地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115)

 # 获取access_token请求地址
  getAccessTokenUrl: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s
  #获取accessToken
  getAccessTokenOAuthUrl: https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code
  # 获取用户基本信息请求地址
  getUserInfoUrl: https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN
  #获取code
  getCodeUrl: https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=%s&scope=%s&state=%s#wechat_redirect
  #获取ticket
  getTicketUrl: https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi

3.controller层

       /**
     * 微信配置信息实体
     */
    @Autowired
    private WeiXinProperties weiXinProperties;
    //微信参数
    private String accessToken;
    private String jsApiTicket;
    //获取参数的时刻
    private Long getTiketTime = 0L;
    private Long getTokenTime = 0L;
    //参数的有效时间,单位是秒(s)
    private Long tokenExpireTime = 0L;
    private Long ticketExpireTime = 0L;

 /**
     * 微信自定义分享
     */
    @RequestMapping(value = "/getShareInfo", method = RequestMethod.POST)
    public Map<String, String> getShareInfo(HttpServletRequest request,
                                            HttpServletResponse response, String url) {
        //当前时间
        long now = System.currentTimeMillis();

        //判断accessToken是否已经存在或者token是否过期
        if (StringUtils.isBlank(accessToken) || (now - getTokenTime > tokenExpireTime * 1000)) {
            JSONObject tokenInfo = getAccessToken();
            if (tokenInfo != null) {
                accessToken = tokenInfo.getString("access_token");
                tokenExpireTime = tokenInfo.getLongValue("expires_in");
                //获取token的时间
                getTokenTime = System.currentTimeMillis();
                log.info("accessToken====>" + accessToken);
                log.info("tokenExpireTime====>" + tokenExpireTime + "s");
                log.info("getTokenTime====>" + getTokenTime + "ms");
            } else {
                log.info("====>tokenInfo is null~");
                log.info("====>failure of getting tokenInfo,please do some check~");
            }
        }
        //判断jsApiTicket是否已经存在或者是否过期
        if (StringUtils.isBlank(jsApiTicket) || (now - getTiketTime > ticketExpireTime * 1000)) {
            JSONObject ticketInfo = getJsApiTicket(accessToken);
            if (ticketInfo != null) {
                log.info("ticketInfo====>" + ticketInfo.toJSONString());
                jsApiTicket = ticketInfo.getString("ticket");
                ticketExpireTime = ticketInfo.getLongValue("expires_in");
                getTiketTime = System.currentTimeMillis();
                log.info("jsApiTicket====>" + jsApiTicket);
                log.info("ticketExpireTime====>" + ticketExpireTime + "s");
                log.info("getTiketTime====>" + getTiketTime + "ms");
            } else {
                log.info("====>ticketInfo is null~");
                log.info("====>failure of getting tokenInfo,please do some check~");
            }
        }
        //生成微信权限验证的参数
        Map<String, String> wechatParam = makeWXTicket(jsApiTicket, url);
        return wechatParam;

    }

    //获取accessToken
    private JSONObject getAccessToken() {
        //String accessTokenUrl = https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        //获取微信端的accessToken
        String requestUrl = String.format(weiXinProperties.getGetAccessTokenUrl(), weiXinProperties.getAppId(), weiXinProperties.getAppSecret());
        String result = send(requestUrl);
        JSONObject jsonObject = JSON.parseObject(result);
        return jsonObject;
    }

    //获取ticket
    private JSONObject getJsApiTicket(String access_token) {
        //String apiTicketUrl = https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
        // 通过acessToken 获取ticket
        String requestUrl = String.format(weiXinProperties.getGetTicketUrl(), access_token);
        String result = send(requestUrl);  //此部分用到了我们项目的工具类,整段复制本代码时,可以用以下代替
     //JSONObject jsonObject = httpUtils.doGet(requestUrl)
        JSONObject jsonObject = JSON.parseObject(result);
        return jsonObject;
    }

    //生成微信权限验证的参数
    public Map<String, String> makeWXTicket(String jsApiTicket, String url) {
        Map<String, String> ret = new HashMap<String, String>();
        String nonceStr = createNonceStr();
        String timestamp = createTimestamp();
        String string1;
        String signature = "";

        //注意这里参数名必须全部小写,且必须有序
        string1 = "jsapi_ticket=" + jsApiTicket +
                "&noncestr=" + nonceStr +
                "&timestamp=" + timestamp +
                "&url=" + url;
        log.info("String1=====>" + string1);
        try {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(string1.getBytes("UTF-8"));
            signature = byteToHex(crypt.digest());
            log.info("signature=====>" + signature);
        } catch (NoSuchAlgorithmException e) {
            log.error("WeChatController.makeWXTicket=====Start");
            log.error(e.getMessage(), e);
            log.error("WeChatController.makeWXTicket=====End");
        } catch (UnsupportedEncodingException e) {
            log.error("WeChatController.makeWXTicket=====Start");
            log.error(e.getMessage(), e);
            log.error("WeChatController.makeWXTicket=====End");
        }

        ret.put("url", url);
        ret.put("jsapi_ticket", jsApiTicket);
        ret.put("nonceStr", nonceStr);
        ret.put("timestamp", timestamp);
        ret.put("signature", signature);
        ret.put("appid", weiXinProperties.getAppId());

        return ret;
    }

    /**
     * 发送请求
     *
     * @param url
     * @return
     * @throws Exception
     */
    String send(String url) {
        return HttpClientTools.post(url);
    }

    //字节数组转换为十六进制字符串
    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 createNonceStr() {
        return UUID.randomUUID().toString();
    }

    //生成时间戳
    private static String createTimestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }

4.引入share.js.要分享的页面

本文由俄罗斯贵宾会发布于Web前端,转载请注明出处:Editor富文本编辑器配置【不含图片上传】

您可能还会对下面的文章感兴趣: