Files
xk-api-yii/common/helpers/FakeId.php

41 lines
1015 B
PHP
Raw Normal View History

2024-09-19 11:32:39 +08:00
<?php
/**
* Created by PhpStorm.
* User: chatfeed
* Date: 2022/5/25
* Time: 7:28 PM
*/
namespace common\helpers;
use admin\exceptions\ApiException;
/**
* ID加前缀偏移真实ID
*/
class FakeId
{
//店员DY供应商GY药店YI业务员XS,用户YH,市场专员ZY 药店账号 限2位字符
private static $offset = ['DY'=>612155,'GY'=>215851,'YD'=>721143,'XS'=>116555,'YH'=>135435,'ZY'=> 123456];
public static function encodeId($type,$id){
if(!isset(self::$offset[$type])){
throw new ApiException("{$type}类型不存在");
}
return $type.($id+self::$offset[$type]);
}
public static function decodeId($id){
if(!$id){
return 0;
}
preg_match('/([A-Z]{2})(\d+)/s',$id,$macths);
if(!isset($macths[1]) || !isset(self::$offset[$macths[1]])){
throw new ApiException("{$macths[1]}类型不存在");
}
;
return (int)$macths[2]-self::$offset[$macths[1]];
}
}