合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
>[info] 数据脱敏 参考 `rrzuji` 代码: ### 目的: 为了统一平台的脱敏规则,避免对同一类型数据采用不同的脱敏规则,也为了能够灵活调整脱敏规则,因此在Yii框架中提供了统一的脱敏方法 ### 代码路径: \\common\\Tool\\DataMasking\\DataMaskingTools \\common\\Service\\Security\\DataMasking\\DataMaskingService ### 调用方法示例: ![](https://img.kancloud.cn/87/37/873790c1309283e42d9cb4c5acad890a_592x516.png) 目前提供了姓名,手机号,地址,邮箱,身份证号等的脱敏方法,如有其他需要可自行实现 #### 单个调用: 使用场景:适用于单个数据的脱敏 ~~~ DataMaskingTools::setData($model->alipay_user_id)->alipayUserId(); DataMaskingTools::setData($model->mobile)->phone(); ~~~ 实例演示: ![](https://img.kancloud.cn/d5/4e/d54e83ebfb7accca2a734250b6cd2bb9_1316x448.png) #### 数组嵌套: 适用场景:需要脱敏的值在数组内有嵌套的情况 ~~~ $maskingDataAlias = [ 'address' => 'address', 'receive.address' => 'address', 'receive.bind_phone' => 'phone', 'receive.name' => 'name', 'receive.phone' => 'phone', 'user_cert.id_name' => 'name', ]; if (!empty($data['data']['listData'])) { DataMaskingService::setData(['alias' => $maskingDataAlias])->maskDataListByAliasAndNesting($data['data']['listData']); } ~~~ 实例演示: ![](https://img.kancloud.cn/93/a5/93a5450abdc2b16b48c4b8446fe675f9_1207x493.png) #### 简单数组: 适用场景:需要脱敏的值没有嵌套的情况 ~~~ $alias = [ 'name' => 'name', ]; if (!empty($data['data']['list'])) { DataMaskingService::setData(['alias' => $alias])->maskDataListByAlias($data['data']['list']); } ~~~ ~~~ /** * 将一个字符串部分字符用*替代隐藏 如110881099**383179X * * @param string $string 待转换的字符串 * @param int $bengin 起始位置,从0开始计数,当$type=4时,表示左侧保留长度 * @param int $len 需要转换成*的字符个数,当$type=4时,表示右侧保留长度 * @param int $type 转换类型:0,从左向右隐藏;1,从右向左隐藏;2,从指定字符位置分割前由右向左隐藏;3,从指定字符位置分割后由左向右隐藏;4,保留首末指定字符串 * @param string $glue 分割符 * @return string * @author: huangzudeng <237124111@qq.com> * @Date: 2021/6/8 15:14 */ public static function hideStr(string $string, int $bengin = 0, int $len = 4, int $type = 0, $glue = "@") { if (empty($string)) { return $string; } $array = []; if ($type == 0 || $type == 1 || $type == 4) { $strlen = $length = mb_strlen($string); while ($strlen) { $array[] = mb_substr($string, 0, 1, "utf8"); $string = mb_substr($string, 1, $strlen, "utf8"); $strlen = mb_strlen($string); } } switch ($type) { case 0 : for ($i = $bengin; $i < ($bengin + $len); $i++) { isset($array[$i]) && $array[$i] = "*"; } $string = implode("", $array); break; case 1 : $array = array_reverse($array); for ($i = $bengin; $i < ($bengin + $len); $i++) { isset($array[$i]) && $array[$i] = "*"; } $string = implode("", array_reverse($array)); break; case 2 : $array = explode($glue, $string); $array[0] = self::hideStr($array[0], $bengin, $len, 1); $string = implode($glue, $array); break; case 3 : $array = explode($glue, $string); $array[1] = self::hideStr($array[1], $bengin, $len, 0); $string = implode($glue, $array); break; case 4 : $left = $bengin; $right = $len; $tem = array(); for ($i = 0; $i < ($length - $right); $i++) { if (isset($array[$i])) { $tem[] = $i >= $left ? "*" : $array[$i]; } } $array = array_chunk(array_reverse($array), $right); $array = array_reverse($array[0]); for ($i = 0; $i < $right; $i++) { $tem[] = $array[$i]; } $string = implode("", $tem); break; } return $string; } ~~~ * **调用实例:** ~~~ StringHelper::hideStr($data['phone'], 3, 4) ~~~ ~~~ /** * 将一个字符串部分字符用$re替代隐藏 改为最多显示4个re * * @param string $string 待处理的字符串 * @param int $start 规定在字符串的何处开始, * @param int $length 规定要隐藏的字符串长度。默认是直到字符串的结尾。 * @param string $re 替代符 * @return bool|string * @author rrzj_2609 * @date 2019/6/1 */ public static function hideString($string, $start = 0, $length = 0, $re = '*') { if (empty($string)) { return false; } $strArr = array(); $mbStringLength = mb_strlen($string); while ($mbStringLength) { // 循环把字符串变为数组 $strArr[] = mb_substr($string, 0, 1, 'utf8'); $string = mb_substr($string, 1, $mbStringLength, 'utf8'); $mbStringLength = mb_strlen($string); } $stringLength = count($strArr); $begin = $start >= 0 ? $start : ($stringLength - abs($start)); $end = $last = $stringLength - 1; if ($length > 0) { $end = $begin + $length - 1; } elseif ($length < 0) { $end -= abs($length); } $reLen = 0; for ($i = $begin; $i <= $end; $i++) { if ($reLen < 4) { $strArr[$i] = $re; } else { $strArr[$i] = false; } $reLen++; } if ($begin > $end || $begin > $last || $end > $last) { return false; } array_filter($strArr); return implode('', $strArr); } ~~~ * **调用实例:** ~~~ hideString('420029810@qq.com', 2, -7) ~~~