package com.stylefeng.guns.modular.api; import cn.hutool.crypto.SecureUtil; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.stripe.exception.SignatureVerificationException; import com.stripe.model.PaymentIntent; import com.stripe.net.Webhook; import com.stripe.param.PaymentIntentUpdateParams; import com.stylefeng.guns.core.base.controller.BaseController; import com.stylefeng.guns.core.base.tips.ErrorTip; import com.stylefeng.guns.core.common.constant.factory.ConstantFactory; import com.stylefeng.guns.core.shiro.ShiroUser; import com.stylefeng.guns.core.util.Convert; import com.stylefeng.guns.core.util.JwtTokenUtil; import com.stylefeng.guns.core.util.MD5Util; import com.stylefeng.guns.modular.system.dao.TCompanyMapper; import com.stylefeng.guns.modular.system.dao.UserMapper; import com.stylefeng.guns.modular.system.model.TCompany; import com.stylefeng.guns.modular.system.model.TUser; import com.stylefeng.guns.modular.system.model.User; import com.stylefeng.guns.modular.system.model.UserInfo; import com.stylefeng.guns.modular.system.service.ITUserService; import com.stylefeng.guns.modular.system.utils.EmailUtil; import com.stylefeng.guns.modular.system.utils.RedisUtil; import com.stylefeng.guns.modular.system.utils.tips.SuccessTip; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Random; /** * 接口控制器提供 * * @author stylefeng * @Date 2018/7/20 23:39 */ @RestController @Api(tags = "登录") @RequestMapping("/gunsApi") public class ApiController extends BaseController { @Resource private TCompanyMapper companyMapper; @Resource private ITUserService userService; @Autowired private RedisUtil redisUtil; /** * api登录接口,通过账号密码获取token */ @PostMapping("/companyLogin") @ApiOperation(value = "用户登录", notes = "用户登录") @ApiImplicitParams({ @ApiImplicitParam(name = "username", value = "用户账号", required = true, dataType = "String"), @ApiImplicitParam(name = "password", value = "用户密码", required = true, dataType = "String") }) public Object companyLogin(@RequestParam("username") String username, @RequestParam("password") String password) { //获取数据库中的账号密码,准备比对 List user = userService.selectList(new EntityWrapper().eq("account",username)); if (user.size()==0) { return new ErrorTip(500, "Account password error!"); } UserInfo userInfo = new UserInfo(); BeanUtils.copyProperties(user, userInfo); // String credentials = user.getPassword(); // String salt = user.getSalt(); // ByteSource credentialsSalt = new Md5Hash(salt); // SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo( // new ShiroUser(), credentials, credentialsSalt, ""); //校验用户账号密码 // HashedCredentialsMatcher md5CredentialsMatcher = new HashedCredentialsMatcher(); // md5CredentialsMatcher.setHashAlgorithmName(ShiroKit.hashAlgorithmName); // md5CredentialsMatcher.setHashIterations(ShiroKit.hashIterations); // boolean passwordTrueFlag = md5CredentialsMatcher.doCredentialsMatch( // usernamePasswordToken, simpleAuthenticationInfo); String encrypt = MD5Util.encrypt(password); if (!encrypt.equals(user.get(0).getPassword())) { return new ErrorTip(500, "Account password error!"); } else { TUser tUser = user.get(0); HashMap result = new HashMap<>(); result.put("token", JwtTokenUtil.generateToken(String.valueOf(tUser.getId()))); result.put("user", tUser); // 1需要完善信息 if(tUser.getPhone()==null){ result.put("userInfo", 1); }else { result.put("userInfo", 2); } return new SuccessTip(result); } } private ShiroUser shiroUser(User user) { ShiroUser shiroUser = new ShiroUser(); shiroUser.setId(user.getId()); shiroUser.setAccount(user.getAccount()); shiroUser.setDeptId(user.getDeptid()); shiroUser.setDeptName(ConstantFactory.me().getDeptName(user.getDeptid())); shiroUser.setName(user.getName()); Integer[] roleArray = Convert.toIntArray(user.getRoleid()); List roleList = new ArrayList(); List roleNameList = new ArrayList(); for (int roleId : roleArray) { roleList.add(roleId); roleNameList.add(ConstantFactory.me().getSingleRoleName(roleId)); } shiroUser.setRoleList(roleList); shiroUser.setRoleNames(roleNameList); return shiroUser; } /** * 测试接口是否走鉴权 */ @RequestMapping(value = "/test", method = RequestMethod.POST) public Object test() { return SUCCESS_TIP; } @PostMapping("/forget") @ApiOperation(value = "忘记密码", notes = "忘记密码") @ApiImplicitParams({ @ApiImplicitParam(name = "username", value = "用户账号", required = true, dataType = "String"), @ApiImplicitParam(name = "password", value = "用户密码", required = true, dataType = "String"), @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String"), }) public Object forget(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("code") String code) { List user = userService.selectList(new EntityWrapper().eq("account",username)); String value = redisUtil.getValue(username); if(!code.equals(value)){ return new ErrorTip(5001, "Verification code error!"); } if (user.size()==0){ return new ErrorTip(500, "账号不存在!"); } user.get(0).setPassword(MD5Util.encrypt(password)); userService.updateById(user.get(0)); return new SuccessTip(); } @PostMapping("/register") @ApiOperation(value = "用户注册", notes = "用户注册") @ApiImplicitParams({ @ApiImplicitParam(name = "companyName", value = "companyName", required = true, dataType = "String"), @ApiImplicitParam(name = "email", value = "email", required = true, dataType = "String"), @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String"), @ApiImplicitParam(name = "password", value = "password", required = true, dataType = "String"), }) public Object register(@RequestParam("companyName") String companyName, @RequestParam("email") String email, @RequestParam("code") String code, @RequestParam("password") String password) { String value = redisUtil.getValue(email); if(!code.equals(value)){ return new ErrorTip(5001, "Verification code error!"); } TUser tUser = new TUser(); tUser.setCompanyName(companyName); tUser.setAccount(email); tUser.setEmail(email); tUser.setPassword(SecureUtil.md5(password)); userService.insert(tUser); return new SuccessTip(); } @PostMapping("/sendCode") @ApiOperation(value = "发送验证码", notes = "发送验证码") @ApiImplicitParams({ @ApiImplicitParam(name = "email", value = "用户邮箱", required = true, dataType = "String"), }) public Object sendCode(@RequestParam("email") String email) { String randomNumber = getRandomString(6); redisUtil.setStrValue(email,randomNumber,300); try { EmailUtil.sendMailGMail(email, randomNumber); return new com.stylefeng.guns.core.base.tips.SuccessTip(); }catch (Exception e){ e.printStackTrace(); return new ErrorTip(500,"ERROR"); } } private String getRandomString(int length) { String base = "0123456789"; Random random = new Random(); StringBuffer sb = new StringBuffer(); for(int i = 0; i < length; ++i) { int number = random.nextInt(base.length()); sb.append(base.charAt(number)); } return sb.toString(); } /** * 支付回调处理方法。 * * @param payload 事件负载数据(JSON 格式字符串) * @param sigHeader Stripe 签名头信息 */ @RequestMapping(value = "returnUrl") public void handlePaymentWebhook(String payload, String sigHeader) { // 从 Stripe 获取 Webhook 私钥 String webhookSecret = "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"; try { // 验证签名并解析负载数据 PaymentIntent paymentIntent = (PaymentIntent) Webhook.constructEvent(payload, sigHeader, webhookSecret).getData().getObject(); // 处理付款意向状态 if (paymentIntent.getStatus().equals("succeeded")) { // 如果付款已成功,请在此处添加成功后的逻辑 // 将付款意向状态更新为已处理 PaymentIntentUpdateParams updateParams = PaymentIntentUpdateParams.builder() .setMetadata( new HashMap() {{ put("handled", "true"); }}) .build(); paymentIntent.update(updateParams); } } catch (SignatureVerificationException e) { // 如果签名无效,则记录并丢弃此事件 e.printStackTrace(); } catch (Exception e) { // 在处理任何其他异常情况时,应该记录错误并尽快通知管理员 e.printStackTrace(); } } }