You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

193 lines
7.1 KiB

  1. package com.simple.controller;
  2. import com.simple.annotation.AuthIgnore;
  3. import com.simple.common.ErrorCode;
  4. import com.simple.common.Result;
  5. import com.simple.common.ResultData;
  6. import com.simple.domain.po.WxMall;
  7. import com.simple.domain.po.WxMerchant;
  8. import com.simple.domain.po.WxMerchantBUser;
  9. import com.simple.enums.EnumMerchantStatus;
  10. import com.simple.service.WxMallService;
  11. import com.simple.service.WxMerchantBUserService;
  12. import com.simple.service.WxMerchantService;
  13. import com.simple.utils.IPUtil;
  14. import io.swagger.annotations.Api;
  15. import io.swagger.annotations.ApiOperation;
  16. import org.apache.commons.lang3.StringUtils;
  17. import org.apache.log4j.Logger;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.web.bind.annotation.*;
  20. import org.springframework.web.context.request.RequestContextHolder;
  21. import org.springframework.web.context.request.ServletRequestAttributes;
  22. import javax.servlet.http.HttpServletRequest;
  23. import java.util.Date;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. @RestController
  27. @Api(description="B端用户相关接口")
  28. @RequestMapping("/api/user")
  29. public class WxMerchantBUserController extends BaseController
  30. {
  31. private Logger logger = Logger.getLogger(WxMerchantBUserController.class);
  32. @Autowired
  33. private WxMerchantBUserService wxMerchantBUserService;
  34. @Autowired
  35. private WxMerchantService wxMerchantService;
  36. @Autowired
  37. private WxMallService wxMallService;
  38. @ApiOperation("查寻当bUser详情接口")
  39. @GetMapping("/detail")
  40. public ResultData detail() {
  41. Map resultMap = new HashMap();
  42. WxMerchantBUser user = wxMerchantBUserService.getById(getUser().getId());
  43. if (user==null)
  44. return new ResultData(ErrorCode.USER_IS_EMPTY);
  45. WxMerchant merchant = wxMerchantService.getById(user.getMerchantId());
  46. if (merchant==null)
  47. return new ResultData(ErrorCode.MCH_INFO_NOT_FOUND);
  48. WxMall mall = wxMallService.getByTenantId(merchant.getTenantId());
  49. if (mall==null)
  50. return new ResultData(ErrorCode.MALL_INFO_NOT_FOUND);
  51. resultMap.put("phone",user.getPhone());
  52. resultMap.put("name",user.getName());
  53. resultMap.put("merchant_name",merchant.getName());
  54. resultMap.put("merchant_img_url",merchant.getImgUrl());
  55. resultMap.put("mall_name",mall.getName());
  56. resultMap.put("service_phone",mall.getServicePhone());
  57. return new ResultData(resultMap);
  58. }
  59. /**
  60. * 用户登录
  61. * @param map
  62. * @return
  63. */
  64. @AuthIgnore
  65. @PostMapping("/login")
  66. @ApiOperation(value="用户登录", notes="{" +
  67. "\"appId\":\"string\"," +
  68. "\"phone\":\"string\",\"password\":\"string\"," +
  69. "\"latitude\":\"string\",\"longitude\":\"string\"}")
  70. public ResultData userLogin(@RequestBody Map<String, String> map) {
  71. logger.debug(map.toString());
  72. Map resultMap = new HashMap();
  73. String appId = map.get("appId");
  74. String phone = map.get("phone");
  75. String password = map.get("password");
  76. String latitude = map.get("latitude");
  77. String longitude = map.get("longitude");
  78. //登录凭证不能为空
  79. if (StringUtils.isBlank(phone)) {
  80. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "手机号不能为空");
  81. }
  82. if (StringUtils.isBlank(password)) {
  83. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "密码不能为空");
  84. }
  85. String token = null;
  86. HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
  87. String ipaddress = IPUtil.getIpAddr(request);
  88. if (!StringUtils.isBlank(latitude)&&!StringUtils.isBlank(longitude)) {
  89. //return new ResultData(ErrorCode.PARAMETER_NOT_NULL.getCode(), "经纬度未获取");
  90. logger.info("B端用户: " + phone + " 登录 IP" + ipaddress + ", 经纬度(" + longitude + "," + latitude + ")");
  91. }
  92. WxMerchantBUser user = new WxMerchantBUser();
  93. user.setAppId(appId);
  94. user.setPhone(phone);
  95. Date currentDate = new Date();
  96. WxMerchantBUser user1 = null;
  97. try {
  98. user1 = wxMerchantBUserService.getBUserByAppId(user);
  99. } catch (Exception e) {
  100. logger.error(e.getMessage());
  101. logger.error("B端用户不存在, phone: " + phone);
  102. return new ResultData(ErrorCode.USER_IS_EMPTY);
  103. }
  104. if (user1 != null) {
  105. // check merchant 状态
  106. WxMerchant merchant = null;
  107. try {
  108. merchant = wxMerchantService.getById(user1.getMerchantId());
  109. } catch (Exception e) {
  110. logger.error(ErrorCode.DB_FAIL.getMessage( ) + ": " + user1.getMerchantId());
  111. return new ResultData(ErrorCode.DB_FAIL);
  112. }
  113. if (merchant == null) {
  114. logger.error(ErrorCode.MERCHANT_INFO_NOT_FOUND.getMessage( ) + ": " + user1.getMerchantId());
  115. return new ResultData(ErrorCode.MERCHANT_INFO_NOT_FOUND);
  116. }
  117. if (merchant.getStatus() == EnumMerchantStatus.NOT_VALID.getCode()) {
  118. logger.error(ErrorCode.MERCHANT_INFO_NOT_VALID.getMessage( ) + ": " + user1.getMerchantId());
  119. return new ResultData(ErrorCode.MERCHANT_INFO_NOT_VALID);
  120. }
  121. // check password
  122. if (user1.getBUserPwd().equalsIgnoreCase(password)) {
  123. user1.createToken(currentDate);
  124. token = user1.getToken();
  125. try {
  126. wxMerchantBUserService.saveOrUpdate(user1);
  127. } catch (Exception e) {
  128. logger.error("B端用户更新用户信息失败, e:" + e.getMessage());
  129. return new ResultData(ErrorCode.DB_FAIL.getCode(), "数据库保存失败,e:" + e.getMessage());
  130. }
  131. resultMap.put("token", token);
  132. return new ResultData(resultMap);
  133. } else {
  134. return new ResultData(ErrorCode.PASSWORD_ERROR);
  135. }
  136. } else {
  137. logger.error("B端用户不存在, phone: " + phone);
  138. return new ResultData(ErrorCode.USER_IS_EMPTY);
  139. }
  140. }
  141. @AuthIgnore
  142. @ApiOperation("修改密码")
  143. @PostMapping("/updatepwd")
  144. public ResultData updatepwd(@RequestBody Map<String,String> params) {
  145. // String phone,String code,String pwd
  146. String phone=params.get("phone");
  147. String code=params.get("code");
  148. String pwd=params.get("pwd");
  149. boolean blank = StringUtils.isBlank(phone);
  150. if(blank){
  151. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"手机号不能为空");
  152. }
  153. blank = StringUtils.isBlank(code);
  154. if(blank){
  155. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"验证码不能为空");
  156. }
  157. blank = StringUtils.isBlank(pwd);
  158. if(blank){
  159. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"密码不能为空");
  160. }
  161. return wxMerchantBUserService.updatepwd(phone,code,pwd);
  162. }
  163. }