|
- package com.iformall.controller;
-
- import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
- import com.alibaba.fastjson.JSONObject;
- import com.google.code.kaptcha.Producer;
- import com.iformall.annotation.AuthIgnore;
- import com.iformall.annotation.TenantIgnore;
- import com.iformall.common.ErrorCode;
- import com.iformall.common.Result;
- import com.iformall.common.ResultData;
- import com.iformall.domain.po.*;
- import com.iformall.domain.po.sm.InviteCodeInfo;
- import com.iformall.enums.*;
- import com.iformall.exception.MallinkException;
- import com.iformall.service.*;
- import com.iformall.service.cuser.CUserServiceFactory;
- import com.iformall.service.sm.InviteCodeInfoService;
- import com.iformall.service.sm.InviteCodeService;
- import com.iformall.service.sm.UserConsumptionPackageService;
- import com.iformall.service.sm.UserCreateVideoNumService;
- import com.iformall.utils.Constant;
- import com.iformall.utils.IPUtil;
- import com.iformall.utils.PasswordHelper;
- import com.iformall.utils.RedisCacheUtils;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiImplicitParam;
- import io.swagger.annotations.ApiImplicitParams;
- import io.swagger.annotations.ApiOperation;
- import org.apache.commons.io.IOUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
-
- import javax.imageio.ImageIO;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- @RestController
- @RequestMapping("/api/miniApp")
- @Api(description = "用户登陆相关接口")
- public class MiniAppUserController extends BaseController {
-
- private final Logger logger = LoggerFactory.getLogger(this.getClass());
-
- @Autowired
- private CUserServiceFactory cuserFactory;
-
- @Autowired
- @Qualifier("objectCommonRedisTemplate")
- RedisTemplate<String, Object> redisTemplate;
-
- @Autowired
- private WxCUserBasicInfoService wxCUserBasicInfoService;
-
- @Autowired
- WxAppinfoService wxAppinfoService;
-
-
- /**
- * 用户登录
- * 小程序登陆
- *
- * @param map
- * @return
- */
- @AuthIgnore
- @TenantIgnore
- @PostMapping("/login")
- @ApiOperation(value = "用户登录", notes = "{\"appId\":\"string\",\"code\":\"string\",\"scene\":\"string\",\"sceneAddress\":\"string\",\"latitude\":\"string\",\"longitude\":\"string\",\"systemInfo\":\"string\"}")
- public ResultData userLogin(@RequestBody Map<String, String> map) {
- logger.info("dologin >>>>>>>>>>>>>>>>>"+map.toString());
-
- Map resultMap = new HashMap();
-
- String appId = map.get("appId");
- String code = map.get("code");
- //登录凭证不能为空
- if (StringUtils.isBlank(appId)) {
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "appId不能为空");
- }
- if (StringUtils.isBlank(code)) {
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "code不能为空");
- }
-
- WxAppinfo wxAppinfo = wxAppinfoService.getOnlyByAppIdFromRedis(appId);
- if(wxAppinfo == null){
- return new ResultData(ErrorCode.APP_ID_NOT_FOUND);
- }
- if(!wxAppinfo.getEnable().equals(EnumEnableType.Enable.getCode())){
- return new ResultData(ErrorCode.APP_ID_NOT_ENABLE);
- }
- EnumAppPlat appPlat = EnumAppPlat.getByCode(wxAppinfo.getPlat());
- if(appPlat == null){
- return new ResultData(ErrorCode.APP_PLAT_ERROR);
- }
-
- String session_key = null;
- String openId = null;
- String unionId = null;
-
- try {
- WxMaJscode2SessionResult session = cuserFactory.getCUserService(appPlat).jsCode2SessionInfo(code, wxAppinfo, false);
- logger.info("sessionResult: " + session);
- if (null != session) {
- logger.info("sessionResultDetail: [openid]" + session.getOpenid()+",[sessionkey]"+session.getSessionKey()+",[unionid]"+session.getUnionid());
- }
-
- //获取会话密钥(session_key)
- session_key = session.getSessionKey();
- openId = session.getOpenid();
- unionId = session.getUnionid();
-
- logger.info("session_key: " + session_key + ", openId: " + openId + ", unionId: " + unionId);
- resultMap.put("openId", openId);
- } catch (Exception e) {
- logger.error(e.getMessage(), e);
- return new ResultData(ErrorCode.SESSION_KEY_DECODE_ERR);
- }
-
- HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
- // request.setAttribute(Constant.TENANT_ID, wxMall.getTenantId());
- // request.setAttribute(Constant.PARENT_TENANT_ID, wxMall.getParentTenantId());
- String ipaddress = IPUtil.getIpAddr(request);
-
- CUser cUser = new CUser();
- cUser.setAppId(appId);
- cUser.setOpenId(openId);
-
- cUser.setUnionId(unionId);
- cUser.setRegisterIp(ipaddress);
- cUser.setSessionKey(session_key);
-
- //处理登陆用户
- cUser = cuserFactory.getCUserService(appPlat).handleLoginUser(cUser);
-
- if(cUser.getUserId() != null){
- WxCUserBasicInfo basicInfo = wxCUserBasicInfoService.getById(cUser.getUserId(), getTenantInfo().getTenantId());
- if(basicInfo != null){
- wxCUserBasicInfoService.handleLoginUser(basicInfo);
- resultMap.put("token", basicInfo.getToken());
- }
- }
- return new ResultData(resultMap);
- }
-
- /**
- * 授权登陆
- *
- * @param map
- * @return
- */
- @AuthIgnore
- @PostMapping("/loginPhone")
- @ApiOperation(value = "授权后获取用户的手机号", notes = "{\"encryptedData\":\"string\",\"iv\":\"string\"}")
- public ResultData getUserPhone(@RequestBody Map<String, String> map) {
- logger.info(map.toString());
- String appId = map.get("appId");
- String openId = map.get("openId");
- String encryptedData = map.get("encryptedData");
- String iv = map.get("iv");
-
- if(StringUtils.isBlank(appId)){
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "appId 不能为空");
- }
- if(StringUtils.isBlank(openId)){
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "openId 不能为空");
- }
-
- WxAppinfo wxAppinfo = wxAppinfoService.getOnlyByAppIdFromRedis(appId);
- if(wxAppinfo == null){
- return new ResultData(ErrorCode.APP_ID_NOT_FOUND);
- }
- if(!wxAppinfo.getEnable().equals(EnumEnableType.Enable.getCode())){
- return new ResultData(ErrorCode.APP_ID_NOT_ENABLE);
- }
- EnumAppPlat appPlat = EnumAppPlat.getByCode(wxAppinfo.getPlat());
- if(appPlat == null){
- return new ResultData(ErrorCode.APP_PLAT_ERROR);
- }
-
- //登录凭证不能为空
- if (StringUtils.isBlank(encryptedData)) {
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "encryptedData 不能为空");
- }
- if (StringUtils.isBlank(iv)) {
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "iv 不能为空");
- }
-
- String uid = map.get("uid");
-
- UserBasicFrom from = new UserBasicFrom();
- from.setPlat(appPlat.getCode());
- from.setFromProject(EnumProject.PROJECT_5.getCode());
- if(StringUtils.isNotBlank(uid)){
- try {
- from.setFromUserId(Long.parseLong(uid));
- from.setFromType(EnumUserBasicFrom.FROM_3.getCode());
- }catch(Exception e){}
- }
-
-
- Map resultMap = new HashMap();
-
- try {
- CUser user = cuserFactory.getCUserService(appPlat).getByOpenId(openId, getTenantInfo().getTenantId());
- if (user == null) {
- return new ResultData(ErrorCode.USER_IS_EMPTY);
- }
- // logger.info("ceshi "+user.getAppPlat()+"======"+user.getClass());
- // 解密 并注册
- WxCUserBasicInfo basicInfo = cuserFactory.getCUserService(appPlat).decryptPhoneNoInfo(encryptedData, iv, user, wxAppinfo, false,from);
- wxCUserBasicInfoService.handleLoginUser(basicInfo);
-
- resultMap.put("token", basicInfo.getToken());
-
- } catch (MallinkException e) {
- return new ResultData(e.getErrorCode(), e.getMessage());
- }catch (Exception e) {
- this.logger.error(e.getMessage(), e);
- return new ResultData(ErrorCode.DB_FAIL.getCode(), "解密并保存出错", resultMap);
- }
-
- return new ResultData(resultMap);
- }
-
- }
|