package com.simple.controller; import com.simple.annotation.AuthIgnore; import com.simple.common.ErrorCode; import com.simple.common.Result; import com.simple.common.ResultData; import com.simple.domain.po.WxMall; import com.simple.domain.po.WxMerchant; import com.simple.domain.po.WxMerchantBUser; import com.simple.enums.EnumMerchantStatus; import com.simple.service.WxMallService; import com.simple.service.WxMerchantBUserService; import com.simple.service.WxMerchantService; import com.simple.utils.IPUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.util.Date; import java.util.HashMap; import java.util.Map; @RestController @Api(description="B端用户相关接口") @RequestMapping("/api/user") public class WxMerchantBUserController extends BaseController { private Logger logger = Logger.getLogger(WxMerchantBUserController.class); @Autowired private WxMerchantBUserService wxMerchantBUserService; @Autowired private WxMerchantService wxMerchantService; @Autowired private WxMallService wxMallService; @ApiOperation("查寻当bUser详情接口") @GetMapping("/detail") public ResultData detail() { Map resultMap = new HashMap(); WxMerchantBUser user = wxMerchantBUserService.getById(getUser().getId()); if (user==null) return new ResultData(ErrorCode.USER_IS_EMPTY); WxMerchant merchant = wxMerchantService.getById(user.getMerchantId()); if (merchant==null) return new ResultData(ErrorCode.MCH_INFO_NOT_FOUND); WxMall mall = wxMallService.getByTenantId(merchant.getTenantId()); if (mall==null) return new ResultData(ErrorCode.MALL_INFO_NOT_FOUND); resultMap.put("phone",user.getPhone()); resultMap.put("name",user.getName()); resultMap.put("merchant_name",merchant.getName()); resultMap.put("merchant_img_url",merchant.getImgUrl()); resultMap.put("mall_name",mall.getName()); resultMap.put("service_phone",mall.getServicePhone()); return new ResultData(resultMap); } /** * 用户登录 * @param map * @return */ @AuthIgnore @PostMapping("/login") @ApiOperation(value="用户登录", notes="{" + "\"appId\":\"string\"," + "\"phone\":\"string\",\"password\":\"string\"," + "\"latitude\":\"string\",\"longitude\":\"string\"}") public ResultData userLogin(@RequestBody Map map) { logger.debug(map.toString()); Map resultMap = new HashMap(); String appId = map.get("appId"); String phone = map.get("phone"); String password = map.get("password"); String latitude = map.get("latitude"); String longitude = map.get("longitude"); //登录凭证不能为空 if (StringUtils.isBlank(phone)) { return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "手机号不能为空"); } if (StringUtils.isBlank(password)) { return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "密码不能为空"); } String token = null; HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); String ipaddress = IPUtil.getIpAddr(request); if (!StringUtils.isBlank(latitude)&&!StringUtils.isBlank(longitude)) { //return new ResultData(ErrorCode.PARAMETER_NOT_NULL.getCode(), "经纬度未获取"); logger.info("B端用户: " + phone + " 登录 IP" + ipaddress + ", 经纬度(" + longitude + "," + latitude + ")"); } WxMerchantBUser user = new WxMerchantBUser(); user.setAppId(appId); user.setPhone(phone); Date currentDate = new Date(); WxMerchantBUser user1 = null; try { user1 = wxMerchantBUserService.getBUserByAppId(user); } catch (Exception e) { logger.error(e.getMessage()); logger.error("B端用户不存在, phone: " + phone); return new ResultData(ErrorCode.USER_IS_EMPTY); } if (user1 != null) { // check merchant 状态 WxMerchant merchant = null; try { merchant = wxMerchantService.getById(user1.getMerchantId()); } catch (Exception e) { logger.error(ErrorCode.DB_FAIL.getMessage( ) + ": " + user1.getMerchantId()); return new ResultData(ErrorCode.DB_FAIL); } if (merchant == null) { logger.error(ErrorCode.MERCHANT_INFO_NOT_FOUND.getMessage( ) + ": " + user1.getMerchantId()); return new ResultData(ErrorCode.MERCHANT_INFO_NOT_FOUND); } if (merchant.getStatus() == EnumMerchantStatus.NOT_VALID.getCode()) { logger.error(ErrorCode.MERCHANT_INFO_NOT_VALID.getMessage( ) + ": " + user1.getMerchantId()); return new ResultData(ErrorCode.MERCHANT_INFO_NOT_VALID); } // check password if (user1.getBUserPwd().equalsIgnoreCase(password)) { user1.createToken(currentDate); token = user1.getToken(); try { wxMerchantBUserService.saveOrUpdate(user1); } catch (Exception e) { logger.error("B端用户更新用户信息失败, e:" + e.getMessage()); return new ResultData(ErrorCode.DB_FAIL.getCode(), "数据库保存失败,e:" + e.getMessage()); } resultMap.put("token", token); return new ResultData(resultMap); } else { return new ResultData(ErrorCode.PASSWORD_ERROR); } } else { logger.error("B端用户不存在, phone: " + phone); return new ResultData(ErrorCode.USER_IS_EMPTY); } } @AuthIgnore @ApiOperation("修改密码") @PostMapping("/updatepwd") public ResultData updatepwd(@RequestBody Map params) { // String phone,String code,String pwd String phone=params.get("phone"); String code=params.get("code"); String pwd=params.get("pwd"); boolean blank = StringUtils.isBlank(phone); if(blank){ return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"手机号不能为空"); } blank = StringUtils.isBlank(code); if(blank){ return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"验证码不能为空"); } blank = StringUtils.isBlank(pwd); if(blank){ return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"密码不能为空"); } return wxMerchantBUserService.updatepwd(phone,code,pwd); } }