美文网首页
给移动端强制更新的接口

给移动端强制更新的接口

作者: CoderZS | 来源:发表于2019-08-06 20:09 被阅读0次
    app版本控制表
    CREATE TABLE `app_version` (
      `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
      `client_phone_type` int(4) DEFAULT NULL COMMENT 'app类型 YGYAppVersionType : 1001IOS终端 1002Android终端',
      `upd_date` bigint(13) DEFAULT NULL COMMENT '版本更新时间',
      `download_file_name` varchar(100) DEFAULT NULL COMMENT '下载的更新文件fileId',
      `app_version` varchar(10) DEFAULT NULL COMMENT 'app版本号',
      `app_market` varchar(255) DEFAULT NULL COMMENT '已更新的备注',
      `upd_content` varchar(255) DEFAULT NULL COMMENT 'app更新内容',
      `forced_upd` int(4) DEFAULT NULL COMMENT '是否强制更新 : 0不强制 1强制',
      `remark` varchar(255) DEFAULT NULL COMMENT '备注',
      `create_ti` bigint(13) DEFAULT NULL COMMENT '创建时间',
      `create_user_id` int(11) DEFAULT NULL COMMENT '创建人id',
      `modify_ti` bigint(13) DEFAULT NULL COMMENT '修改时间',
      `modify_user_id` int(11) DEFAULT NULL COMMENT '修改人id',
      `version` int(4) DEFAULT '0' COMMENT '版本 默认为0 ,-1删除',
      PRIMARY KEY (`id`) USING BTREE
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='app版本控制表';
    
    版本实体类
    @Data
    @EqualsAndHashCode(callSuper = false)
    @Accessors(chain = true)
    @TableName("app_version")
    public class AppVersion implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        /**
         * id
         */
        private Integer id;
    
        /**
         * app类型  : 1001IOS终端 1002Android终端
         */
        private Integer clientPhoneType;
    
        /**
         * 版本更新时间
         */
        private Long updDate;
    
        /**
         * 下载的更新文件fileId
         */
        private String downloadFileName;
    
        /**
         * app版本号
         */
        private String appVersion;
    
        /**
         * 已更新的备注
         */
        private String appMarket;
    
        /**
         * app更新内容
         */
        private String updContent;
    
        /**
         * 是否强制更新  : 0不强制 1强制
         */
        private Integer forcedUpd;
    
        /**
         * 备注
         */
        private String remark;
    
        /**
         * 创建时间
         */
        private Long createTi;
    
        /**
         * 创建人id
         */
        private Integer createUserId;
    
        /**
         * 修改时间
         */
        private Long modifyTi;
    
        /**
         * 修改人id
         */
        private Integer modifyUserId;
    
        /**
         * 版本
         */
        private Integer version;
    
    }
    
    写给移动端的判断版本信息的接口
    @Slf4j
    @RestController
    @RequestMapping(value = "v1/app/version")
    public class VersionController {
    
        @Reference
        VersionService versionService;
    
        @PostMapping("/compareVersion")
        public BaseResponse compareVersion(HttpServletRequest request, HttpServletResponse response,
                                           @FastJson AppVersion appVersion) {
    
            logger.info("compareVersion 版本信息:" + appVersion);
            try {
                appVersion = versionService.compareVersion(appVersion);
    
                if (Objects.isNull(appVersion)) {
                    return BaseResponse.buildSuccess(appVersion);
                }
                HashMap<String, Object> appVersionMap = new HashMap<>();
                appVersionMap.put("title", "发现新版本");
                appVersionMap.put("content", appVersion.getUpdContent());
                appVersionMap.put("version", appVersion.getAppVersion());
                appVersionMap.put("updateType", appVersion.getForcedUpd());
                return BaseResponse.buildSuccess(appVersionMap);
            } catch (Exception e) {
                logger.error("compareVersion操作失败  appVersion{}", appVersion);
                return BaseResponse.buildFail("操作失败!");
            }
    
        }
    
    }
    
    service接口
    public interface VersionService {
        AppVersion compareVersion(AppVersion appVersion);
    }
    
    
    接口实现类
    @Service
    public class VersionServiceImpl implements VersionService {
        @Resource
        AppVersionMapper appVersionMapper;
    
        @Override
        public AppVersion compareVersion(AppVersion appVersion) {
    
            QueryWrapper<AppVersion> queryWrapper = new QueryWrapper<AppVersion>();
            queryWrapper.lambda().eq(AppVersion::getClientPhoneType, appVersion.getClientPhoneType()).orderByDesc(AppVersion::getCreateTi);
            AppVersion newAppVersion = appVersionMapper.selectOne(queryWrapper);
            if (Objects.isNull(newAppVersion)) {
                return null;
            }
            //   获取到了最新的版本号与现在的版本号对比
            int i = compareVersion(appVersion.getAppVersion(), newAppVersion.getAppVersion());
            if (i < 0) {
                return newAppVersion;
            } else {
                return null;
            }
        }
    
    
        private int compareVersion(String version1, String version2) {
            if (version1 == null || version2 == null) {
                throw new RuntimeException("版本号不能为空");
            }
            String[] versionArray1 = version1.split("\\.");//注意此处为正则匹配,不能用.;
            String[] versionArray2 = version2.split("\\.");
            int idx = 0;
            int minLength = Math.min(versionArray1.length, versionArray2.length);//取最小长度值
            int diff = 0;
            while (idx < minLength
                    && (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0//先比较长度
                    && (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) {//再比较字符
                ++idx;
            }
            //如果已经分出大小,则直接返回,如果未分出大小,则再比较位数,有子版本的为大;
            diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length;
            return diff;
        }
    }
    
    

    这里AppVersion newAppVersion = appVersionMapper.selectOne(queryWrapper);
    我们使用的是mybatisplus工具
    简单的sql我们可以自己写 ,根据移动端类型 iOS或者安卓端查询数据库最新的版本

    SELECT app_version,forced_upd FROM app_version WHERE client_phone_type = ? ORDER BY create_ti DESC 
    

    然后我们根据查询的最新版本号和自己传入的版本号对比
    调用这个方法

    compareVersion(String version1, String version2)
    

    如果小于0说明数据库查询的最新版本号大于传入版本号

    判断是否需要更新规则

    这个时候如果存在新版本则将最新版本信息返回出去,若没有新版本则返回null

    1,这里我们需要注意代码规范 controller不应该做业务处理 需要将业务处理放到Service
    2,@Reference@Slf4j注解可以根据自己的实际代码习惯 可以使用@Autowired和log4j的日志框架这里可以自由选择
    3,数据库app版本号类型是字符串类型 如1.2.1’ 4,更新标题在表里并没有给出一般版本更新不必要搞得太花哨 如果需要可以自己新增 5,更新内容字段我们可以使用特殊字符|`比如:1,修复类bug|2,新增查看周边功能|3,加入iOS13的新特性,我们通过特殊字符连接返回给移动端 让移动端自己分割
    6,本功能包含强制更新和更新提示两个功能,iOS如果强制更新被举报容易被强制下架,

    相关文章

      网友评论

          本文标题:给移动端强制更新的接口

          本文链接:https://www.haomeiwen.com/subject/fvtgdctx.html