1
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.ruoyi.system.mapper;
|
||||
|
||||
import java.util.List;
|
||||
import com.ruoyi.system.domain.BanmaAccount;
|
||||
|
||||
/**
|
||||
* 斑马账号 Mapper
|
||||
*/
|
||||
public interface BanmaAccountMapper {
|
||||
BanmaAccount selectById(Long id);
|
||||
List<BanmaAccount> selectList(BanmaAccount query);
|
||||
int insert(BanmaAccount entity);
|
||||
int update(BanmaAccount entity);
|
||||
int deleteById(Long id);
|
||||
int clearDefault();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.ruoyi.system.service;
|
||||
|
||||
import java.util.List;
|
||||
import com.ruoyi.system.domain.BanmaAccount;
|
||||
|
||||
/**
|
||||
* 斑马账号 Service 接口
|
||||
*/
|
||||
public interface IBanmaAccountService {
|
||||
List<BanmaAccount> listSimple();
|
||||
Long saveOrUpdate(BanmaAccount entity);
|
||||
void remove(Long id);
|
||||
boolean refreshToken(Long id);
|
||||
void refreshAllTokens();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.ruoyi.system.service.impl;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import com.ruoyi.system.domain.BanmaAccount;
|
||||
import com.ruoyi.system.mapper.BanmaAccountMapper;
|
||||
import com.ruoyi.system.service.IBanmaAccountService;
|
||||
|
||||
/**
|
||||
* 斑马账号 Service 实现
|
||||
*/
|
||||
@Service
|
||||
public class BanmaAccountServiceImpl implements IBanmaAccountService {
|
||||
|
||||
@Autowired
|
||||
private BanmaAccountMapper mapper;
|
||||
private final RestTemplate restTemplate = new RestTemplate();
|
||||
private static final String LOGIN_URL = "https://banma365.cn/api/login";
|
||||
|
||||
@Override
|
||||
public List<BanmaAccount> listSimple() {
|
||||
List<BanmaAccount> list = mapper.selectList(new BanmaAccount());
|
||||
// 隐藏密码
|
||||
for (BanmaAccount a : list) { a.setPassword(null); }
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long saveOrUpdate(BanmaAccount entity) {
|
||||
if (entity.getId() == null) {
|
||||
mapper.insert(entity);
|
||||
} else {
|
||||
mapper.update(entity);
|
||||
}
|
||||
if (Objects.equals(entity.getIsDefault(), 1)) {
|
||||
mapper.clearDefault();
|
||||
BanmaAccount only = new BanmaAccount();
|
||||
only.setId(entity.getId());
|
||||
only.setIsDefault(1);
|
||||
mapper.update(only);
|
||||
}
|
||||
return entity.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Long id) { mapper.deleteById(id); }
|
||||
|
||||
@Override
|
||||
public boolean refreshToken(Long id) {
|
||||
BanmaAccount one = mapper.selectById(id);
|
||||
if (one == null || one.getStatus() == null || one.getStatus() == 0) return false;
|
||||
if (one.getUsername() == null || one.getPassword() == null) return false;
|
||||
String token = validateAndGetToken(one.getUsername(), one.getPassword());
|
||||
if (token != null) {
|
||||
BanmaAccount upd = new BanmaAccount();
|
||||
upd.setId(one.getId());
|
||||
upd.setToken("Bearer " + token);
|
||||
upd.setTokenExpireAt(new Date(System.currentTimeMillis() + 2L * 24 * 60 * 60 * 1000));
|
||||
mapper.update(upd);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证账号密码并获取Token(不保存到数据库)
|
||||
*/
|
||||
public String validateAndGetToken(String username, String password) {
|
||||
if (username == null || password == null) return null;
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
java.util.Map<String, String> body = new java.util.HashMap<>();
|
||||
body.put("username", username);
|
||||
body.put("password", password);
|
||||
@SuppressWarnings("unchecked")
|
||||
java.util.Map<String, Object> resp = restTemplate.postForObject(LOGIN_URL, new HttpEntity<>(body, headers), java.util.Map.class);
|
||||
if (resp == null) return null;
|
||||
Object code = resp.get("code");
|
||||
if (code instanceof Number && ((Number) code).intValue() == 0) {
|
||||
Object data = resp.get("data");
|
||||
if (data instanceof java.util.Map) {
|
||||
Object token = ((java.util.Map<?, ?>) data).get("token");
|
||||
if (token instanceof String && !((String) token).isEmpty()) {
|
||||
return (String) token;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refreshAllTokens() {
|
||||
BanmaAccount q = new BanmaAccount();
|
||||
q.setStatus(1);
|
||||
List<BanmaAccount> list = mapper.selectList(q);
|
||||
for (BanmaAccount a : list) {
|
||||
try { refreshToken(a.getId()); } catch (Exception ignore) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.ruoyi.system.mapper.BanmaAccountMapper">
|
||||
|
||||
<resultMap id="BanmaAccountResult" type="com.ruoyi.system.domain.BanmaAccount">
|
||||
<result property="id" column="id"/>
|
||||
<result property="name" column="name"/>
|
||||
<result property="username" column="username"/>
|
||||
<result property="password" column="password"/>
|
||||
<result property="token" column="token"/>
|
||||
<result property="tokenExpireAt" column="token_expire_at"/>
|
||||
<result property="isDefault" column="is_default"/>
|
||||
<result property="status" column="status"/>
|
||||
<result property="remark" column="remark"/>
|
||||
<result property="createBy" column="create_by"/>
|
||||
<result property="createTime" column="create_time"/>
|
||||
<result property="updateBy" column="update_by"/>
|
||||
<result property="updateTime" column="update_time"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id, name, username, password, token, token_expire_at, is_default, status, remark, create_by, create_time, update_by, update_time
|
||||
</sql>
|
||||
|
||||
<select id="selectById" parameterType="long" resultMap="BanmaAccountResult">
|
||||
select <include refid="Base_Column_List"/> from banma_account where id = #{id}
|
||||
</select>
|
||||
|
||||
<select id="selectList" parameterType="com.ruoyi.system.domain.BanmaAccount" resultMap="BanmaAccountResult">
|
||||
select <include refid="Base_Column_List"/> from banma_account
|
||||
<where>
|
||||
<if test="name != null and name != ''"> and name like concat('%', #{name}, '%')</if>
|
||||
<if test="username != null and username != ''"> and username like concat('%', #{username}, '%')</if>
|
||||
<if test="status != null"> and status = #{status}</if>
|
||||
</where>
|
||||
order by create_time desc
|
||||
</select>
|
||||
|
||||
<insert id="insert" parameterType="com.ruoyi.system.domain.BanmaAccount" useGeneratedKeys="true" keyProperty="id">
|
||||
insert into banma_account
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="name != null">name,</if>
|
||||
<if test="username != null">username,</if>
|
||||
<if test="password != null">password,</if>
|
||||
<if test="token != null">token,</if>
|
||||
<if test="tokenExpireAt != null">token_expire_at,</if>
|
||||
<if test="isDefault != null">is_default,</if>
|
||||
<if test="status != null">status,</if>
|
||||
<if test="remark != null">remark,</if>
|
||||
<if test="createBy != null">create_by,</if>
|
||||
create_time
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="name != null">#{name},</if>
|
||||
<if test="username != null">#{username},</if>
|
||||
<if test="password != null">#{password},</if>
|
||||
<if test="token != null">#{token},</if>
|
||||
<if test="tokenExpireAt != null">#{tokenExpireAt},</if>
|
||||
<if test="isDefault != null">#{isDefault},</if>
|
||||
<if test="status != null">#{status},</if>
|
||||
<if test="remark != null">#{remark},</if>
|
||||
<if test="createBy != null">#{createBy},</if>
|
||||
sysdate()
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<update id="update" parameterType="com.ruoyi.system.domain.BanmaAccount">
|
||||
update banma_account
|
||||
<trim prefix="set" suffixOverrides=",">
|
||||
<if test="name != null">name=#{name},</if>
|
||||
<if test="username != null">username=#{username},</if>
|
||||
<if test="password != null">password=#{password},</if>
|
||||
<if test="token != null">token=#{token},</if>
|
||||
<if test="tokenExpireAt != null">token_expire_at=#{tokenExpireAt},</if>
|
||||
<if test="isDefault != null">is_default=#{isDefault},</if>
|
||||
<if test="status != null">status=#{status},</if>
|
||||
<if test="remark != null">remark=#{remark},</if>
|
||||
<if test="updateBy != null">update_by=#{updateBy},</if>
|
||||
update_time = sysdate()
|
||||
</trim>
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
<update id="clearDefault">
|
||||
update banma_account set is_default = 0 where is_default = 1
|
||||
</update>
|
||||
|
||||
<delete id="deleteById" parameterType="long">
|
||||
delete from banma_account where id = #{id}
|
||||
</delete>
|
||||
|
||||
</mapper>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user