Browse Source

返回字段新增pinyin

master
wanghongjun 2 years ago
parent
commit
39c385d377
  1. 50
      app/common/lib/pinyin/PinyinNumber.php
  2. 11
      app/logic/Zone.php

50
app/common/lib/pinyin/PinyinNumber.php

@ -0,0 +1,50 @@
<?php
namespace app\common\lib\pinyin;
/**
* 中文转拼音类.
*/
class PinyinNumber
{
protected static $pinyin = [0 => 'ling', 1 => 'yi', 2 => 'er', 3 => 'san', 4 => 'si', 5 => 'wu', 6 => 'liu', 7 => 'qi', 8 => 'ba', 9 => 'jiu'];
protected static $unit = ['', 'shi', 'bai', 'qian', 'wan', 'shiwan', 'baiwan', 'qianwan', 'yi'];
/**
* 获取数字的拼音
* @param $number
* @param $strtoupper // 返回大写
* @return string
*/
public static function getPinyin($number,$strtoupper = false)
{
$number = strval($number); // 将数字转换为字符串
$length = strlen($number);
$pinyinStr = '';
for ($i = 0; $i < $length; $i++) {
$digit = intval($number[$i]);
if ($digit != 0) {
// 处理万以上的位数
if ($length - $i > 4) {
$pinyinStr .= self::$pinyin[$digit] . self::$unit[$length - $i - 1];
// 处理万位之间的零
if ($digit == 0 && $number[$i + 1] != 0) {
$pinyinStr .= 'ling';
}
} else {
$pinyinStr .= self::$pinyin[$digit] . self::$unit[$length - $i - 1];
}
} else {
// 处理连续的0
if ($i == 0 || $number[$i] != 0) {
$pinyinStr .= self::$pinyin[$digit];
}
}
}
if ($strtoupper) $pinyinStr = strtoupper($pinyinStr);
return $pinyinStr;
}
}

11
app/logic/Zone.php

@ -1,6 +1,7 @@
<?php <?php
namespace app\logic; namespace app\logic;
use app\common\lib\pinyin\PinyinNumber;
use app\model\AwardsRecords; use app\model\AwardsRecords;
use app\model\ConsumptionRecords; use app\model\ConsumptionRecords;
use app\model\User; use app\model\User;
@ -176,6 +177,7 @@ class Zone
foreach ($value as $k => $v) { foreach ($value as $k => $v) {
$data[$key][$k]['amount'] = rtrim($v['amount'],'.00');
unset($data[$key][$k]['id']); unset($data[$key][$k]['id']);
} }
} }
@ -232,14 +234,17 @@ class Zone
break; break;
} }
} }
$amount = rtrim($selectedPattern['amount'],'.00');
$pinyin = PinyinNumber::getPinyin(100000,true);
$data[$i][] = [ $data[$i][] = [
'amount' => $selectedPattern['amount'], 'amount' => $amount,
'pinyin' => $pinyin,
'image' => get_image_url($selectedPattern['image']) 'image' => get_image_url($selectedPattern['image'])
]; ];
$save_data[$i][] = [ $save_data[$i][] = [
'id' => $selectedPattern['id'], 'id' => $selectedPattern['id'],
'amount' => $selectedPattern['amount'], 'amount' => $amount,
'pinyin' => $pinyin,
'image' => get_image_url($selectedPattern['image']) 'image' => get_image_url($selectedPattern['image'])
]; ];
} }

Loading…
Cancel
Save