package com.java110.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.java110.app.smo.wxLogin.IWxLoginSMO;
import com.java110.core.base.controller.BaseController;
import com.java110.core.context.IPageData;
import com.java110.core.context.PageData;
import com.java110.dto.wxLogin.UserInfo;
import com.java110.dto.wxLogin.WxLoginInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 微信小程序登录处理类
 */
@RestController
@RequestMapping(path = "/app")
public class WxLoginController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(WxLoginController.class);

    @Autowired
    private IWxLoginSMO wxLoginSMOImpl;


    /**
     * 微信登录接口
     *
     * @param postInfo
     * @param request
     */
    @RequestMapping(path = "/loginWx", method = RequestMethod.POST)
    public ResponseEntity<String> loginWx(@RequestBody String postInfo, HttpServletRequest request) {
        ResponseEntity<String> responseEntity = null;
        JSONObject postObj = JSONObject.parseObject(postInfo);
        String code = JSONObject.parseObject(postInfo).getString("code");
        JSONObject userInfo = postObj.getJSONObject("userInfo");
        if (code == null || userInfo == null) {
            logger.error("code is null");
            responseEntity = new ResponseEntity<>("code is null", HttpStatus.BAD_REQUEST);
            return responseEntity;
        }
        /*String sessionKey = null;
        String openId = null;*/
//        try {
//            /*WxMaJscode2SessionResult result = this.wxMaService.getUserService().getSessionInfo(code);
//            sessionKey = result.getSessionKey();
//            openId = result.getOpenid();*/
//        } catch (Exception e) {
//            logger.error("login fail by wx", e);
//            e.printStackTrace();
//        }

        IPageData pd = PageData.newInstance().builder("", "","", postInfo,
                "", "", "", "",
                request.getHeader("APP_ID"));

        return wxLoginSMOImpl.doLogin(pd);

        //login first
        /*User user = userService.queryByOpenid(openId);
        if (user == null) {
            user = new User();
            user.setUsername(openId);
            //Md5Hash md5Hash = new Md5Hash(openId, openId,1024);
            user.setPassword(openId); //密码保存加密的openid
            user.setWeixinOpenid(openId);
            user.setAvatar(userInfo.getAvatarUrl());
            user.setNickname(userInfo.getNickName());
            user.setGender(userInfo.getGender());
            user.setUserLevel((byte) 0); //普通用户
            user.setStatus((byte) 0); //0可用 1禁用 2注销
            user.setLastLoginTime(LocalDateTime.now());
            user.setLastLoginIp(IpUtil.getIpAddr(request));
            user.setSessionKey(sessionKey);
            //user.setRoleid("2"); //默认普通用户
            userService.add(user);
        } else {
            user.setLastLoginTime(LocalDateTime.now());
            user.setLastLoginIp(IpUtil.getIpAddr(request));
            user.setSessionKey(sessionKey);
            //LogUtil.info("登录 " + user.toString());
            if (userService.updateById(user) == 0) {
                //LogUtil.error("update login user error");
                return ResponseUtil.updatedDataFailed();
            }
        }
        String token = UserTokenManager.generateToken(user.getId());
        JSONObject jsonObject = new JSONObject();
        //生成token
        jsonObject.put("errno", "0");
        jsonObject.put("token", token);
        jsonObject.put("userInfo", userInfo);
        LOGGER.info(jsonObject);
        //LogUtil.info("login success");
        //LogUtil.info(jsonObject);
        return ResponseUtil.ok(jsonObject);*/

        //return new ResponseEntity<>("",HttpStatus.OK);


    }

    /**
     * 微信登出
     *
     * @return
     */
   /* @RequestMapping("/logout")
    public JSONObject logoutWx() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return ResponseUtil.ok();
    }*/

}

@@ -340,24 +340,74 @@
 xLoginInfo;%0A
+import com.java110.utils.constant.CommonConstant;%0A
 import org.s
@@ -1910,24 +1910,25 @@
       %7D%0A
+%0A
         
 /*String
@@ -1923,416 +1923,99 @@
     
-/*String sessionKey = null;%0A        String openId = null;*/%0A//        try %7B%0A//            /*WxMaJscode2SessionResult result = this.wxMaService.getUserService().getSessionInfo(code);%0A//            sessionKey = result.getSessionKey();%0A//            openId = result.getOpenid();*/%0A//        %7D catch (Exception e) %7B%0A//            logger.error(%22login fail by wx%22, e);%0A//            e.printStackTrace(
+IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA
 );%0A
-//
         
 %7D%0A%0A 
@@ -2006,35 +2006,26 @@
 A);%0A        
-%7D%0A%0A        
+/*
 IPageData pd
@@ -2156,16 +2156,18 @@
 P_ID%22));
+*/
 %0A%0A      
@@ -2207,1990 +2207,13 @@
 d);%0A
-%0A        //login first%0A        /*User user = userService.queryByOpenid(openId);%0A        if (user == null) %7B%0A            user = new User();%0A            user.setUsername(openId);%0A            //Md5Hash md5Hash = new Md5Hash(openId, openId,1024);%0A            user.setPassword(openId); //%E5%AF%86%E7%A0%81%E4%BF%9D%E5%AD%98%E5%8A%A0%E5%AF%86%E7%9A%84openid%0A            user.setWeixinOpenid(openId);%0A            user.setAvatar(userInfo.getAvatarUrl());%0A            user.setNickname(userInfo.getNickName());%0A            user.setGender(userInfo.getGender());%0A            user.setUserLevel((byte) 0); //%E6%99%AE%E9%80%9A%E7%94%A8%E6%88%B7%0A            user.setStatus((byte) 0); //0%E5%8F%AF%E7%94%A8 1%E7%A6%81%E7%94%A8 2%E6%B3%A8%E9%94%80%0A            user.setLastLoginTime(LocalDateTime.now());%0A            user.setLastLoginIp(IpUtil.getIpAddr(request));%0A            user.setSessionKey(sessionKey);%0A            //user.setRoleid(%222%22); //%E9%BB%98%E8%AE%A4%E6%99%AE%E9%80%9A%E7%94%A8%E6%88%B7%0A            userService.add(user);%0A        %7D else %7B%0A            user.setLastLoginTime(LocalDateTime.now());%0A            user.setLastLoginIp(IpUtil.getIpAddr(request));%0A            user.setSessionKey(sessionKey);%0A            //LogUtil.info(%22%E7%99%BB%E5%BD%95 %22 + user.toString());%0A            if (userService.updateById(user) == 0) %7B%0A                //LogUtil.error(%22update login user error%22);%0A                return ResponseUtil.updatedDataFailed();%0A            %7D%0A        %7D%0A        String token = UserTokenManager.generateToken(user.getId());%0A        JSONObject jsonObject = new JSONObject();%0A        //%E7%94%9F%E6%88%90token%0A        jsonObject.put(%22errno%22, %220%22);%0A        jsonObject.put(%22token%22, token);%0A        jsonObject.put(%22userInfo%22, userInfo);%0A        LOGGER.info(jsonObject);%0A        //LogUtil.info(%22login success%22);%0A        //LogUtil.info(jsonObject);%0A        return ResponseUtil.ok(jsonObject);*/%0A%0A        //return new ResponseEntity%3C%3E(%22%22,HttpStatus.OK);%0A%0A%0A    %7D%0A%0A    /**%0A     * %E5%BE%AE%E4%BF%A1%E7%99%BB%E5%87%BA%0A     *%0A     * @return%0A     */%0A   /* @RequestMapping(%22/logout%22)%0A    public JSONObject logoutWx() %7B%0A        Subject subject = SecurityUtils.getSubject();%0A        subject.logout();%0A        return ResponseUtil.ok();%0A    %7D*/
+    %7D
 %0A%0A%7D%0A

修改 登录 控制类