| | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.baomidou.mybatisplus.service.impl.ServiceImpl; |
| | | import com.supersavedriving.user.core.common.constant.JwtConstants; |
| | | import com.supersavedriving.user.core.shiro.ShiroKit; |
| | | import com.supersavedriving.user.core.shiro.ShiroUser; |
| | | import com.supersavedriving.user.core.util.JwtTokenUtil; |
| | | import com.supersavedriving.user.core.util.ToolUtil; |
| | | import com.supersavedriving.user.modular.system.dao.AppUserMapper; |
| | | import com.supersavedriving.user.modular.system.model.AppUser; |
| | | import com.supersavedriving.user.modular.system.model.Coupon; |
| | | import com.supersavedriving.user.modular.system.model.UserToCoupon; |
| | | import com.supersavedriving.user.modular.system.service.IAppUserService; |
| | | import com.supersavedriving.user.modular.system.service.ICouponService; |
| | | import com.supersavedriving.user.modular.system.service.IUserToCouponService; |
| | | import com.supersavedriving.user.modular.system.util.RedisUtil; |
| | | import com.supersavedriving.user.modular.system.util.ResultUtil; |
| | | import com.supersavedriving.user.modular.system.util.weChat.WXCore; |
| | | import com.supersavedriving.user.modular.system.util.weChat.WeChatUtil; |
| | | import com.supersavedriving.user.modular.system.util.weChat.model.Code2Session; |
| | | import com.supersavedriving.user.modular.system.warpper.CouponWarpper; |
| | | import com.supersavedriving.user.modular.system.warpper.SignInToRegister; |
| | | import com.supersavedriving.user.modular.system.warpper.SignInToRegisterWarpper; |
| | | import org.apache.shiro.authc.SimpleAuthenticationInfo; |
| | | import org.apache.shiro.authc.UsernamePasswordToken; |
| | | import org.apache.shiro.authc.credential.HashedCredentialsMatcher; |
| | |
| | | import org.apache.shiro.util.ByteSource; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | |
| | | /** |
| | |
| | | |
| | | private final String salt = "s5d1"; |
| | | |
| | | @Autowired |
| | | private ICouponService couponService; |
| | | |
| | | @Autowired |
| | | private IUserToCouponService userToCouponService; |
| | | |
| | | |
| | | |
| | | @Override |
| | | public ResultUtil<String> appUserLogin(String jscode) throws Exception { |
| | | Code2Session code2Session = weChatUtil.code2Session(jscode); |
| | | if(code2Session.getErrcode() != 0){ |
| | | if(null != code2Session.getErrcode() && code2Session.getErrcode() != 0){ |
| | | return ResultUtil.error(code2Session.getErrmsg()); |
| | | } |
| | | String openid = code2Session.getOpenid(); |
| | |
| | | * @throws Exception |
| | | */ |
| | | @Override |
| | | public ResultUtil signInToRegister(SignInToRegister signInToRegister) throws Exception { |
| | | public ResultUtil<SignInToRegisterWarpper> signInToRegister(SignInToRegister signInToRegister) throws Exception { |
| | | SignInToRegisterWarpper warpper = new SignInToRegisterWarpper(); |
| | | if(ToolUtil.isEmpty(signInToRegister.getJscode())){ |
| | | return ResultUtil.paranErr("jscode"); |
| | | } |
| | |
| | | return ResultUtil.paranErr("ivPhone"); |
| | | } |
| | | Code2Session code2Session = weChatUtil.code2Session(signInToRegister.getJscode()); |
| | | if(code2Session.getErrcode() != 0){ |
| | | if(null != code2Session.getErrcode() && code2Session.getErrcode() != 0){ |
| | | return ResultUtil.error(code2Session.getErrmsg()); |
| | | } |
| | | String openid = code2Session.getOpenid(); |
| | |
| | | appUser.setInviterId(signInToRegister.getInviterId()); |
| | | appUser.setInviterType(signInToRegister.getInviterType()); |
| | | this.insert(appUser); |
| | | //发送优惠券 |
| | | boolean lock = redisUtil.lock(); |
| | | if(lock){ |
| | | List<CouponWarpper> list = pushCoupon(appUser.getId()); |
| | | redisUtil.unlock(); |
| | | warpper.setCoupons(list); |
| | | } |
| | | } |
| | | if(appUser.getStatus() == 2){ |
| | | return ResultUtil.error("账号被冻结"); |
| | |
| | | if(ToolUtil.isEmpty(token)){ |
| | | return ResultUtil.error("获取身份凭证失败"); |
| | | } |
| | | return ResultUtil.success(token); |
| | | warpper.setToken(token); |
| | | return ResultUtil.success(warpper); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 发送优惠券 |
| | | * @param userId |
| | | */ |
| | | public List<CouponWarpper> pushCoupon(Integer userId){ |
| | | List<Coupon> coupons = couponService.selectList(new EntityWrapper<Coupon>().eq("coupon_type", 2).eq("coupon_state", 1).gt("remaining_quantity", 0)); |
| | | List<CouponWarpper> list = new ArrayList<>(); |
| | | for (Coupon coupon : coupons) { |
| | | UserToCoupon userToCoupon = new UserToCoupon(); |
| | | userToCoupon.setCouponId(coupon.getId()); |
| | | userToCoupon.setCreateTime(new Date()); |
| | | userToCoupon.setUserId(userId); |
| | | userToCoupon.setStatus(1); |
| | | userToCoupon.setCouponTotal(coupon.getCouponSendQuantity() > coupon.getRemainingQuantity() ? |
| | | coupon.getRemainingQuantity() : coupon.getCouponSendQuantity()); |
| | | userToCoupon.setValidCount(userToCoupon.getCouponTotal()); |
| | | userToCoupon.setExpireTime(new Date(System.currentTimeMillis() + (coupon.getCouponValidity() * 24 * 60 * 60 * 1000))); |
| | | userToCouponService.insert(userToCoupon); |
| | | |
| | | coupon.setRemainingQuantity(coupon.getCouponSendQuantity() > coupon.getRemainingQuantity() ? 0 : |
| | | coupon.getRemainingQuantity() - coupon.getCouponSendQuantity()); |
| | | couponService.updateById(coupon); |
| | | |
| | | CouponWarpper couponWarpper = new CouponWarpper(); |
| | | couponWarpper.setCouponConditionalAmount(coupon.getCouponConditionalAmount()); |
| | | couponWarpper.setCouponPreferentialAmount(coupon.getCouponPreferentialAmount()); |
| | | couponWarpper.setCouponName(coupon.getCouponName()); |
| | | couponWarpper.setNumber(userToCoupon.getValidCount()); |
| | | couponWarpper.setExpirationDate(userToCoupon.getExpireTime().getTime()); |
| | | list.add(couponWarpper); |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public Integer getUserByRequest() throws Exception { |
| | | ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| | | HttpServletRequest request = servletRequestAttributes.getRequest(); |
| | | String requestHeader = request.getHeader(JwtConstants.AUTH_HEADER); |
| | | if (ToolUtil.isNotEmpty(requestHeader) && requestHeader.startsWith("Bearer ")) { |
| | | requestHeader = requestHeader.substring(requestHeader.indexOf(" ") + 1); |
| | | String key = null; |
| | | int length = requestHeader.length(); |
| | | if(length > 16){ |
| | | key = requestHeader.substring(length - 16); |
| | | }else{ |
| | | key = requestHeader; |
| | | } |
| | | String value = redisUtil.getValue(key); |
| | | return null != value ? Integer.valueOf(value) : null; |
| | | }else{ |
| | | return null; |
| | | } |
| | | } |
| | | } |