package com.matrix.system.shopXcx.api.action; 
 | 
  
 | 
import com.matrix.biz.bean.BizUser; 
 | 
import com.matrix.biz.service.BizUserService; 
 | 
import com.matrix.component.redis.RedisUserLoginUtils; 
 | 
import com.matrix.component.tools.HttpCurlUtil; 
 | 
import com.matrix.component.wechat.externalInterface.weixinUtil.WeixinServiceUtil; 
 | 
import com.matrix.core.pojo.AjaxResult; 
 | 
import com.matrix.core.tools.LogUtil; 
 | 
import com.matrix.core.tools.StringUtils; 
 | 
import com.matrix.core.web.BaseAction; 
 | 
import com.matrix.system.common.constance.AppConstance; 
 | 
import com.matrix.system.common.interceptor.HostInterceptor; 
 | 
import com.matrix.system.shopXcx.api.WeChatApiTools; 
 | 
import net.sf.json.JSONObject; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.beans.factory.annotation.Value; 
 | 
import org.springframework.stereotype.Controller; 
 | 
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.ResponseBody; 
 | 
  
 | 
import java.util.Collections; 
 | 
import java.util.Date; 
 | 
  
 | 
/** 
 | 
 * @description (用户表) 
 | 
 * @author jyy 
 | 
 * @date 2019-05-31 10:03 
 | 
 */ 
 | 
@Controller 
 | 
@RequestMapping(value = "wxapi/user") 
 | 
public class WxUserAction extends BaseAction { 
 | 
  
 | 
    @Autowired 
 | 
    private BizUserService bizUserService; 
 | 
  
 | 
  
 | 
    @Autowired 
 | 
    private RedisUserLoginUtils redisUserLoginUtils; 
 | 
  
 | 
  
 | 
    @Autowired 
 | 
    WeChatApiTools weChatApiTools; 
 | 
  
 | 
  
 | 
  
 | 
    @Autowired 
 | 
    private WeixinServiceUtil weixinServiceUtil; 
 | 
  
 | 
    @Value("${debug}") 
 | 
    private String isDebug; 
 | 
  
 | 
    /** 
 | 
     * 小程序登录 
 | 
     *  
 | 
     * @author JIANGYOUYAO 
 | 
     * @email 935090232@qq.com 
 | 
     * @date 2018年5月23日 
 | 
     * @param code 
 | 
     * @return 
 | 
     * @throws Exception 
 | 
     */ 
 | 
    @RequestMapping(value = "/login/{keys}") 
 | 
    public @ResponseBody 
 | 
    AjaxResult wxLogin(@PathVariable("keys") String code) throws Exception { 
 | 
        AjaxResult res = new AjaxResult(); 
 | 
        LogUtil.info("code:{}" + code); 
 | 
        if (StringUtils.isNotBlank(code)) { 
 | 
            String requrl = weChatApiTools.getXcxLoginUrl(code, HostInterceptor.getCompanyId()); 
 | 
            String reslutData = HttpCurlUtil.sendGetHttp(requrl, null); 
 | 
            JSONObject json = JSONObject.fromObject(reslutData); 
 | 
            LogUtil.debug("微信登录获取到登录信息={}", json); 
 | 
  
 | 
            if(json.containsKey("errcode")){ 
 | 
                res.setStatus(AjaxResult.STATUS_FAIL); 
 | 
                res.setInfo("自动登录失败"); 
 | 
                LogUtil.debug("微信登录获取到异常信息errcode"); 
 | 
                return res; 
 | 
            } 
 | 
  
 | 
            String openId = json.getString("openid"); 
 | 
            String sessionKey = json.getString("session_key"); 
 | 
            LogUtil.debug("openId={},sessionKey={}", openId, sessionKey); 
 | 
            // 查询用户是否存在 
 | 
            BizUser bizUser = null; 
 | 
            synchronized(this){ 
 | 
                bizUser = bizUserService.findByOpenId(openId); 
 | 
                if (bizUser == null) { 
 | 
                    // 新增用户 
 | 
                    bizUser = new BizUser(); 
 | 
                    bizUser.setSessionKey(sessionKey); 
 | 
                    bizUser.setIsSales(BizUser.NOT_SALES); 
 | 
                    bizUser.setOpenId(openId); 
 | 
                    bizUser.setLastLoginTime(new Date()); 
 | 
                    bizUser.setUserType(AppConstance.USER_TYPE_CUSTOMER); 
 | 
                    bizUser.setCompanyId(HostInterceptor.getCompanyId()); 
 | 
                    bizUserService.add(bizUser); 
 | 
  
 | 
                } else { 
 | 
                    bizUser.setSessionKey(sessionKey); 
 | 
                    bizUser.setLastLoginTime(new Date()); 
 | 
                    // 更新用户sessionKey 
 | 
                    bizUserService.modifyByModel(bizUser); 
 | 
                } 
 | 
            } 
 | 
            // 存放redis 
 | 
            String token = redisUserLoginUtils.saveUserInfo(bizUser); 
 | 
            LogUtil.info("用户token={}", token); 
 | 
            res.putInMap("token", token); 
 | 
            res.putInMap("userInfo", bizUser); 
 | 
            res.setStatus(AjaxResult.STATUS_SUCCESS); 
 | 
        } else { 
 | 
            res.setStatus(AjaxResult.STATUS_FAIL); 
 | 
            res.setInfo("自动登录失败"); 
 | 
        } 
 | 
        return res; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 查询用户信息 
 | 
     */ 
 | 
    @RequestMapping(value = "/findUserInfo") 
 | 
    public @ResponseBody 
 | 
    AjaxResult findUserInfo() { 
 | 
        BizUser loginUser = redisUserLoginUtils.getLoginUser(BizUser.class); 
 | 
        String openId = loginUser.getOpenId(); 
 | 
        BizUser bizUser = bizUserService.findByOpenId(openId); 
 | 
        AjaxResult res = new AjaxResult(); 
 | 
        res.putInMap("userInfo", bizUser); 
 | 
        res.setStatus(AjaxResult.STATUS_SUCCESS); 
 | 
        return res; 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 接收用户信息 
 | 
     */ 
 | 
    @RequestMapping(value = "/saveUserInfo") 
 | 
    public @ResponseBody 
 | 
    AjaxResult saveUserInfo(@RequestBody BizUser bizUser) { 
 | 
  
 | 
        BizUser loginUser = redisUserLoginUtils.getLoginUser(BizUser.class); 
 | 
        bizUser.setUserId(loginUser.getUserId()); 
 | 
        // TODO 赋值操作 
 | 
        int i = bizUserService.saveUserInfo(bizUser); 
 | 
  
 | 
        if (i == 0) { 
 | 
            return new AjaxResult(AjaxResult.STATUS_FAIL, "更新失败"); 
 | 
        } 
 | 
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, Collections.singletonList(bizUser)); 
 | 
    } 
 | 
  
 | 
  
 | 
  
 | 
  
 | 
} 
 |