微信绑定用户服务端代码-根据code获取openId然后绑定用户

目录结构:

isa.qa.core.weixin.message.resp包和isa.qa.core.weixin.util包中为微信绑定的工具类,就不一一贴出代码,详见附件,下载地址:

http://files.cnblogs.com/files/007sx/weixin_util.zip

jar包(包括了微信支付所需jar此处一起列出)  pom.xml

    <!-- weixin -->
    <dependency>
        <groupId>com.ning</groupId>
        <artifactId>async-http-client</artifactId>
        <version>1.8.13</version>
    </dependency>
    <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.4.7</version>
    </dependency>
    <dependency>
      <groupId>com.gson</groupId>
      <artifactId>wechat</artifactId>
      <version>1.0.8</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.14.4</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp</groupId>
        <artifactId>okhttp</artifactId>
        <version>2.7.5</version>
    </dependency>
    <dependency>
    <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>org.jdom</groupId>
        <artifactId>jdom</artifactId>
        <version>1.1</version>
    </dependency>

WeixinResource.java-连接微信服务器代码接口:

package isa.qa.blep.admin.controller;

import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import isa.qa.core.weixin.message.resp.TextMessage;
import isa.qa.core.weixin.util.MessageUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.gson.util.SHA1;

/**
 * REST controller for managing WorkOrder.
 */
@Controller
@RequestMapping("/api/wechat")
public class WeixinResource {

    private final Logger log = LoggerFactory.getLogger(WeixinResource.class);

//    @Resource(name = "memberServiceImpl")
//    public MemberService memberService;

    private static Map<String,String> TIME_OPENID_MAP = new HashMap<String,String>();

    /**
     * POST  /menus -> Create weixin menus.
     */

    //http://localhost:8080/api/wechat/gateway?signature=123432&timestamp=12343&nonce=1234&echostr=1223434
    @RequestMapping(value = "/gateway",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String weChatValidate(HttpServletRequest request,HttpServletResponse response) throws Exception {
        System.out.println("enter weChatValidate!");
        String _token = "mylng";
        String outPut = "error";
        String signature = request.getParameter("signature");// 微信加密签名
        String timestamp = request.getParameter("timestamp");// 时间戳
        TIME_OPENID_MAP.put(timestamp, "");
        String nonce = request.getParameter("nonce");// 随机数 1413789908
        String echostr = request.getParameter("echostr");//
        System.out.println("in get nonce: "+nonce);
        String[] str = { _token, timestamp, nonce };
        Arrays.sort(str); // 字典序排序
        String bigStr = str[0] + str[1] + str[2]; // SHA1加密
        String digest = SHA1.encode(bigStr);
        if (digest.equals(signature)) {
            outPut = echostr;
            System.out.println("check success!");
        }
        System.out.println("write response:"+outPut);
        PrintWriter writer = response.getWriter();
        writer.write(outPut);
        writer.flush();
        writer.close();
        return null;
    }

    @RequestMapping(value = "/gateway",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public void handleWechatMessage(HttpServletRequest request,HttpServletResponse response) throws Exception {
        System.out.println("received wechat event!");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String nonce = request.getParameter("nonce");
        System.out.println("in post nonce: "+nonce);
        request.getSession().setAttribute("nonce", nonce);
        // 调用核心业务类接收消息、处理消息
        String respMessage = processRequest(request,response);
        // 响应消息
        PrintWriter out = response.getWriter();
        out.print(respMessage);
        out.close();
  }

    /**
     * 处理微信发来的请求
     *
     * @param request
     * @return
     */
    public String processRequest(HttpServletRequest request,HttpServletResponse response) {
     String respMessage = null;
     try {
      // 默认返回的文本消息内容
      String respContent = "请求处理异常,请稍候尝试!";
      // xml请求解析
      Map<String, String> requestMap = MessageUtil.parseXml(request);
      // 发送方帐号(open_id)
      String fromUserName = requestMap.get("FromUserName");
        request.setAttribute("openId", fromUserName);
        HttpSession session=request.getSession();
        session.setAttribute("openId", fromUserName);

      System.out.println("from:"+fromUserName);
      // 公众帐号
      String toUserName = requestMap.get("ToUserName");
      // 消息类型
      String msgType = requestMap.get("MsgType");
      System.out.println("MsgType:"+msgType);
      // 回复文本消息
      TextMessage textMessage = new TextMessage();
      textMessage.setToUserName(fromUserName);
      textMessage.setFromUserName(toUserName);
      textMessage.setCreateTime(new Date().getTime());
      textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
      textMessage.setFuncFlag(0);
      // 文本消息
      if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
       respContent = "您发送的是文本消息!";
      }
      // 图片消息
      else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {
       respContent = "您发送的是图片消息!";
      }
      // 地理位置消息
      else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {
       respContent = "您发送的是地理位置消息!";
      }
      // 链接消息
      else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) {
       respContent = "您发送的是链接消息!"+requestMap.get("EventKey");
      }
      // 音频消息
      else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {
       respContent = "您发送的是音频消息!";
      }
      // 事件推送
      else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
       // 事件类型
       String eventType = requestMap.get("Event");
       // 订阅
       if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {
        respContent = "谢谢您的关注!";
       }
       // 取消订阅
       else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {
        // TODO 取消订阅后用户再收不到公众号发送的消息,因此不需要回复消息
       }
       // 自定义菜单点击事件
       else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {
           // TODO 自定义菜单权没有开放,暂不处理该类消息
           String key = requestMap.get("EventKey");
           if(key.equals("MAINTAIN")){
               respContent = "服务热线:400-008-7070";
           }
           if(key.equals("INFO")){
               respContent = "我是爱车,我为主人服务!";
           }
           System.out.println("event key:"+key);
       }
       // 自定义view界面
       else if (eventType.equals(MessageUtil.EVENT_TYPE_VIEW)){
           String url = requestMap.get("EventKey");
           System.out.println("url:"+url);
//           String[] urls = url.split("&redirect_uri=");
//           String lastUrl = "";
//           String lastUrl2 = "";
//           Member user = memberService.findByWeixinOpenId(fromUserName);
           /*if (user == null) {
               lastUrl = urls[1];
               String[] urls1 = lastUrl.split("&response_type=");
               String str1 = urls1[0] + "?openId=" + fromUserName;
               String url0 = URLEncoder.encode(str1,"UTF-8");
               lastUrl2 = urls[0] + "&redirect_uri=" + url0 + "&response_type=" + urls1[1];
           } else {

//                JsonConfig config = new JsonConfig();
//                config.setIgnoreDefaultExcludes(false);
//                config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
                    //这里是核心,过滤掉不想使用的属性
//                config .setExcludes(filterNames) ;

//               JsonConfig config = new JsonConfig();
//               config.setJsonPropertyFilter(new PropertyFilter(){
//                   public boolean apply(Object source, String name, Object value) {
//                       if(name.equals("lockedDate") || name.equals("loginDate") || name.equals("loginPhoneDate") || name.equals("birth") || name.equals("loginDate")) {  // loginPhoneDate checkDate birth
//                           return true;
//                       } else {
//                           return false;
//                       }
//                   }
//               });

//               JSONObject json = JSONObject.fromObject(user, config);//将java对象转换为json对象
//               String userJson = json.toString();//将json对象转换为字符串

//               System.out.println("userJson:"+userJson);
               lastUrl = urls[0] + "&member=" + user.toString() + "#w" + urls[1];
           }*/
       }
      }
      textMessage.setContent(respContent);
      respMessage = MessageUtil.textMessageToXml(textMessage);
     } catch (Exception e) {
      e.printStackTrace();
     }
     return respMessage;

   }

}

wechat.properties-配置微信AppId和AppSecret   (注意该文件所在位置为ConfKit.java所在包所在的src/main/java下)

#ht

AppId       = wxd5609f7b5b4dd051
AppSecret   = 2d1c183c1e99c378c7a60b595c825e03

MessageProcessingHandlerImpl=com.gson.MessageProcessingHandlerImpl

WeixinLoginController.java

openId绑定用户的代码,根据实际项目绑定代码按需修改,注意项目中如果使用了权限管理,注意把微信相关接口不拦截。

package isa.qa.blep.admin.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import isa.qa.core.common.BoqumaConstant;
import isa.qa.core.dao.EcshopUserDao;
import isa.qa.core.dto.EcshopUserDto;
import isa.qa.core.model.EcshopUsers;
import isa.qa.core.weixin.util.WeixinToken;

@CrossOrigin
@Controller
@RequestMapping(value = "/api")
public class WeixinLoginController {
    @Autowired
    private EcshopUserDao userDao;

    /**
     * 获取openId
     * @param wxCode
     * @param req
     * @return
     */
    @RequestMapping(value = "/weixin/code/{wxCode}/wxcode",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Object getWeiXinOpenId(@PathVariable String wxCode){
        System.out.println("wxCode--》》》:"+wxCode);
        if ("undefined".equals(wxCode)) {
            return null;
        }
        String wxOpenId = "";
        EcshopUsers user = null;
        try {
            wxOpenId = WeixinToken.getWeChatId(wxCode);
            System.out.println("根据code获取得的wxOpenId:"+wxOpenId);
            user = userDao.findOneByOpenId(wxOpenId);
            if (user == null) {
                System.out.println("返回的openid:"+wxOpenId);
                Map<String, String> map = new HashMap<String, String>();
                map.put("openId", wxOpenId);
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;

    }

    /**
     * 微信登陆(绑定)
     * @param ecshopUserDto
     * @param openId
     * @return
     */
    @RequestMapping(value = "/weixin/{openId}/member",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Object checkMember(@RequestBody EcshopUserDto ecshopUserDto, @PathVariable String openId){
        String userName = ecshopUserDto.getUserName();
        Md5PasswordEncoder md5 = new Md5PasswordEncoder();
        md5.setEncodeHashAsBase64(true);
        String encryptedPassword = md5.encodePassword(ecshopUserDto.getPassword(),BoqumaConstant.SALT);
        System.out.println("微信登录用户--UserName:"+userName);
        EcshopUsers user = userDao.findOneByUserNameAndPassword(userName,encryptedPassword);
        if (user == null) {
            System.out.println("用户名或密码错误!");
            Map<String, String> map = new HashMap<String, String>();
            map.put("hint", "用户名或密码错误!");
            return map;
        } else {
            user.setOpenId(openId);
            userDao.save(user);
            System.out.println("绑定成功!");
            return user;
        }

    }
}

补充:

微信菜单的配置示例:

{
    "button":[
        {
            "name" : "优惠活动",
            "sub_button" : [
                {
                    "type": "view",
                    "name": "最新促销",
                    "url":  "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx195166055e5eddb2&redirect_uri=http://carowl.cn/app/index.html#/sales-list&response_type=code&scope=snsapi_base&state=wx&connect_redirect=1#wechat_redirect"
                },{
                    "type": "view",
                    "name": "近期活动",
                    "url":  "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx195166055e5eddb2&redirect_uri=http://carowl.cn/app/index.html#/near-activities&response_type=code&scope=snsapi_base&state=wx&connect_redirect=1#wechat_redirect"
                },{
                    "type": "view",
                    "name": "产品展厅",
                    "url":  "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx195166055e5eddb2&redirect_uri=http://carowl.cn/app/index.html#/products&response_type=code&scope=snsapi_base&state=wx&connect_redirect=1#wechat_redirect"
                }
            ]
        },
        {
            "name" : "预约服务",
            "sub_button" : [
                {
                    "type": "view",
                    "name": "试驾预约",
                    "url":  "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx195166055e5eddb2&redirect_uri=http://carowl.cn/app/index.html#/repairAppointment/try///carId//&response_type=code&scope=snsapi_base&state=wx&connect_redirect=1#wechat_redirect"
                },{
                    "type": "view",
                    "name": "维修预约",
                    "url":  "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx195166055e5eddb2&redirect_uri=http://carowl.cn/app/index.html#/repairAppointment////carId//&response_type=code&scope=snsapi_base&state=wx&connect_redirect=1#wechat_redirect"
                },{
                    "type": "view",
                    "name": "保养预约",
                    "url":  "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx195166055e5eddb2&redirect_uri=http://carowl.cn/app/index.html#/repairAppointment/protect///carId//&response_type=code&scope=snsapi_base&state=wx&connect_redirect=1#wechat_redirect"
                }

            ]
        },
        {
            "name" : "我的地盘",
            "sub_button" : [
                {
                    "type": "view",
                    "name": "一键救援",
                    "url":  "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx195166055e5eddb2&redirect_uri=http://carowl.cn/app/index.html#/oneKey&response_type=code&scope=snsapi_base&state=wx&connect_redirect=1#wechat_redirect"
                },{
                    "type": "view",
                    "name": "在线客服",
                    "url":  "http://kefu.easemob.com/webim/im.html?tenantId=6137"
                },{
                    "type": "view",
                    "name": "优惠券",
                    "url":  "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx195166055e5eddb2&redirect_uri=http://carowl.cn/app/index.html#/packetlist&response_type=code&scope=snsapi_base&state=wx&connect_redirect=1#wechat_redirect"
                },{
                    "type": "view",
                    "name": "会员中心",
                    "url":  "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx195166055e5eddb2&redirect_uri=http://carowl.cn/app/index.html#/member-center&response_type=code&scope=snsapi_base&state=wx&connect_redirect=1#wechat_redirect"
                },{
                    "type": "view",
                    "name": "APP下载",
                    "url":  "http://carowl.cn/download_page/index.html"
                }

            ]
        },
    ]
}

公众号配置的一些流程:

1.登录公众号 https://mp.weixin.qq.com/

2.启用开发者模式

3.进入开发-基本配置

4.填入相关信息,之一URL和Token都要对应上上面所配置的接口和设置的Token

5.网页授权获取用户信息  修改   修改为上一部URL中的 wechat.htib.com.cn

6.微信菜单的配置:

(1).进入在线接口调试工具

(2).配置菜单(先基础配置得到access_token,再使用access_token配置菜单)

时间: 2024-10-24 22:45:06

微信绑定用户服务端代码-根据code获取openId然后绑定用户的相关文章

七牛云存储android客户端及java服务端代码编写

前一篇博客提到让我很伤心的c应用,由于是一款供用户上传图片的应用,因此必须解决图片存储问题,如果直接将图片存储至服务器,当用户上传图片较多,服务器空间将很快吃紧,同时也没有那么大的带宽,现实中我买的阿里云服务器是最低配置,数据盘才20G,带宽才1M,如果用这样配置的服务器做图片存储,那实在太扯了.于是很自然的想到用图片云存储服务器,通过不断查找资料,最后将目标定位在七牛云和又拍云.在做选择时,主要对比了两者之间的价格及技术优势,也看了很多相关话题讨论,个人认为这两者无论从技术方案还是产品价格,都

纯正商业级应用-Node.js Koa2开发微信小程序服务端

第1章 前言.导学与node.js如何理解Node.js?前端到底要不要学习Node.js?本课程能让你学到什么? 第2章 Koa2的那点事儿与异步编程模型Koa非常的精简,基本上,没有经过二次开发的Koa根本“不能”用.本章我们讲解Koa的重要特性,理解什么是洋葱模型?以及在KOA中如何进行异步编程?很多同学都了解以上知识点,但听完本章,你会有一些不一样的理解,比如:为什么要有洋葱模型?没有会怎样?Koa中间件一定是异步的吗? ... 第3章 路由系统的改造Koa-router需要进行一些改造

阅读高手编写的类似QQ聊天的服务端代码业务层设计总结

业务层的代码也应该是面向接口编程,先抽象一个接口或是抽象类,规范一些算法或者功能框架,再在其子类或是实现类中完成具体的方法,易于后期代码的维护. 1.业务层缓存技术 如果数据对实时性要求不高,可以把数据缓存在内存中,提高效率.一般都是利用集合来缓存数据.如下代码: /** * 存放写线程的缓存器 * * @author way */ public class OutputThreadMap { private HashMap<Integer, OutputThread> map; <sp

Socket通信客户端和服务端代码

这两天研究了下Socket通信,简单实现的客户端和服务端代码 先上winfrom图片,客户端和服务端一样 服务端代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; usin

C#根据WSDL文件生成WebService服务端代码

转自:http://www.cnblogs.com/liyi93/archive/2012/01/30/2332320.html 虽然现在已经进入了.NET FrameWork 4.0的时代,WebService也已经逐渐被淘汰,取而代之的是WCF. 但在工作中难免遇到需要兼容旧版本程序和按照以前的文档进行开发. 一般一个已经实现功能的WebService会发布自己的WSDL文件,供客户端调用生成代理类. 但有时是先有server与client交互的接口定义(WSDL)文件,然后由server和

IOS IAP APP内支付 Java服务端代码

IOS IAP APP内支付 Java服务端代码 场景:作为后台需要为app提供服务,在ios中,app内进行支付购买时需要进行二次验证. 基础:可以参考上一篇转载的博文In-App Purchase(iap)快速指南了解原理. 直接先上服务端测试通过的代码: import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.U

Photon Server 实现注册与登录(二) --- 服务端代码整理

一.有的代码前端和后端都会用到.比如一些请求的Code.使用需要新建项目存放公共代码. 新建项目Common存放公共代码: EventCode :存放服务端自动发送信息给客户端的code OperationCode : 区分请求和响应 ReturnCode : 服务端响应码 ParameterCode : 解析参数Toos/DicTool.cs : 数据基本上都是用Dictionary读取,这里工具话一下. 二.代码 Toos/DicTool.cs using System.Collection

android简单登录注册服务端代码实现

长时间不写,知识都淡忘了!现在实现简单登录注册功能,供以后参考!!!! 项目下载地址: https://github.com/majunm/TestServiceDemo.git 服务器: Tomcat/7.0.40  数据库: mysql5.0 数据库创建: mysql 正确安装 配置完path 后 如下界面: mysql -u root -p    // 连接mysql数据库 create database july;//创建数据库 数据库名 july show databases; //

H5+上传注意要点及服务端代码

// 上传文件 function upload(num) { console.log("num:" + num); console.log("headImg.src.:" + headImg.src); if(num == 0 && headImg.src.indexOf('img/header.png') > -1) { plus.nativeUI.alert("请添加头像图片文件!"); return; } consol