puzhibing
2023-07-26 709a4a10be56952ead6340e4822fce41a66e47cd
management/guns-admin/src/main/java/com/stylefeng/guns/modular/api/ApiController.java
@@ -1,22 +1,26 @@
package com.stylefeng.guns.modular.api;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.util.JwtTokenUtil;
import com.stylefeng.guns.modular.system.dao.UserMapper;
import com.stylefeng.guns.modular.system.model.TAgent;
import com.stylefeng.guns.modular.system.model.User;
import com.stylefeng.guns.modular.system.service.ITAgentService;
import com.stylefeng.guns.modular.system.util.MallBook.model.BindAccount;
import com.stylefeng.guns.modular.system.util.MallBook.model.InterfaceResponse;
import com.stylefeng.guns.modular.system.util.MallBook.util.TrhRequest;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
@@ -27,53 +31,83 @@
 * @Date 2018/7/20 23:39
 */
@RestController
@RequestMapping("/gunsApi")
@RequestMapping("")
public class ApiController extends BaseController {
    @Autowired
    private UserMapper userMapper;
    private ITAgentService tAgentService;
    /**
     * api登录接口,通过账号密码获取token
     */
    @RequestMapping("/auth")
    public Object auth(@RequestParam("username") String username,
                       @RequestParam("password") String password) {
        //封装请求账号密码为shiro可验证的token
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password.toCharArray());
        //获取数据库中的账号密码,准备比对
        User user = userMapper.getByAccount(username);
        String credentials = user.getPassword();
        String salt = user.getSalt();
        ByteSource credentialsSalt = new Md5Hash(salt);
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                new ShiroUser(), credentials, credentialsSalt, "");
    @ResponseBody
    @PostMapping("/base/driver/microenterpriseCallback")
    public void microenterpriseCallback(@RequestBody InterfaceResponse response){
        if("0000".equals(response.getCode())){
            JSONObject jsonObject = JSON.parseObject(response.getResult());
            String status = jsonObject.getString("status");
            String parameter1 = jsonObject.getString("parameter1");
            TAgent tAgent = tAgentService.selectById(parameter1);
            if("2".equals(status)){
                tAgent.setMerchantStatus(2);
                tAgentService.updateById(tAgent);
                System.err.println("注册代理商子商户失败");
            }
            if("0".equals(status)){
                tAgent.setMerchantStatus(0);
                tAgentService.updateById(tAgent);
                System.err.println("注册代理商子商户处理中");
            }
            if("1".equals(status)){
                String userId = jsonObject.getString("userId");
                tAgent.setMerchantNumber(userId);
                tAgent.setMerchantStatus(1);
                tAgentService.updateById(tAgent);
        //校验用户账号密码
        HashedCredentialsMatcher md5CredentialsMatcher = new HashedCredentialsMatcher();
        md5CredentialsMatcher.setHashAlgorithmName(ShiroKit.hashAlgorithmName);
        md5CredentialsMatcher.setHashIterations(ShiroKit.hashIterations);
        boolean passwordTrueFlag = md5CredentialsMatcher.doCredentialsMatch(
                usernamePasswordToken, simpleAuthenticationInfo);
        if (passwordTrueFlag) {
            HashMap<String, Object> result = new HashMap<>();
            result.put("token", JwtTokenUtil.generateToken(String.valueOf(user.getId())));
            return result;
        } else {
            return new ErrorTip(500, "账号密码错误!");
                //开始绑定结算账户
                BindAccount bindAccount = new BindAccount();
                bindAccount.setUserId(tAgent.getMerchantNumber());
                bindAccount.setCertId(tAgent.getMerchantIDCode());
                bindAccount.setCardName(tAgent.getCardName());
                bindAccount.setCardNo(tAgent.getCardNo());
                bindAccount.setBankAcctType(tAgent.getBankAcctType().toString());
                bindAccount.setPhone(tAgent.getPhone());
                bindAccount.setBankCode(tAgent.getBankCode());
                /**
                 * 省份编码
                 */
                bindAccount.setProvCode("0035");
                /**
                 * 地区编码
                 */
                bindAccount.setAreaCode("3501");
                TrhRequest<BindAccount> request = new TrhRequest();
                InterfaceResponse execute = request.execute(bindAccount, BindAccount.SERVICE_CODE);
                if("0000".equals(execute.getCode())){
                    JSONObject jsonObject1 = JSON.parseObject(execute.getResult());
                    String status1 = jsonObject1.getString("status");
                    if("2".equals(status1)){
                        tAgent.setBankStatus(2);
                        tAgentService.updateById(tAgent);
                        System.err.println("绑定结算账户失败" );
                    }
                    if("1".equals(status1)){
                        tAgent.setBankStatus(1);
                        tAgentService.updateById(tAgent);
                        System.err.println("绑定结算账户成功");
                    }
                    if("0".equals(status1)){
                        tAgent.setBankStatus(0);
                        tAgentService.updateById(tAgent);
                        System.err.println("绑定结算账户处理中");
                    }
                }else{
                    System.err.println("绑定结算账户失败:" + execute.getMsg());
                }
            }
        }else{
            System.err.println("注册代理商子商户异常:" + response.getMsg());
        }
    }
    /**
     * 测试接口是否走鉴权
     */
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public Object test() {
        return SUCCESS_TIP;
    }
}