DictData.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. *------------------------------------------------------
  4. * 全局数据字典的调用,提供配置缓存的类型
  5. *------------------------------------------------------
  6. *
  7. * @author qqiu@qq.com
  8. * @date 2016/5/26 11:17
  9. * @version V1.0
  10. *
  11. */
  12. namespace App\Services\Base;
  13. use App\Models\BaseDictDataModel;
  14. use Illuminate\Support\Facades\Cache;
  15. class DictData
  16. {
  17. private $_dict;
  18. private $_model;
  19. private $_mmCache = 'table_dict_data_all';
  20. public function __construct() {
  21. if( !$this->_model ) $this->_model = new BaseDictDataModel();
  22. }
  23. /**
  24. * 取得数据字典对应数据
  25. *
  26. * @param string $table_code 数据字典的类型
  27. * @param string $code 数据字典代码
  28. * @param string $val 数据字典数据对应下标
  29. * @return array 返回数据字典数组
  30. * @throws CException $table_code, $code 不能为空
  31. *
  32. */
  33. public function get($table_code, $code, $val = null)
  34. {
  35. $mKey = Cache::get($this->_mmCache);
  36. if(isset($mKey[$table_code][$code])) {
  37. $arr = $mKey[$table_code][$code];
  38. }else{
  39. $this->updateCache();
  40. if(isset($this->_dict[$table_code][$code])) {
  41. $arr = $this->_dict[$table_code][$code];
  42. }else{
  43. $arr = [];
  44. }
  45. }
  46. if(!$arr) {
  47. return null;
  48. }
  49. if(isset($val)) {
  50. if(array_key_exists($val, $arr)) {
  51. return $arr[$val];
  52. }else{
  53. return '';
  54. }
  55. }else{
  56. return $arr;
  57. }
  58. }
  59. /**
  60. * 创建Select代码
  61. *
  62. * @param string $table_code 数据字典的类型
  63. * @param string $code 数据字典代码
  64. * @param string $selection 被选中的值。
  65. * @param array $htmlOptions
  66. * 额外的HTML属性:如 [name=>'cate', id=>'cate'] 这些属性作用在select上。
  67. * 若使用特定关键字empty时,自动创建一个option,
  68. * empty: 字符串,指定空选项的文本,它的值为空。
  69. * empty: 数组,option的value值对应key,文本对应value。
  70. */
  71. public function select($table_code, $code, $selection = null, $htmlOptions = [])
  72. {
  73. $selectOption = $htmlOptions;
  74. unset($selectOption['empty']);
  75. $html = "<select";
  76. foreach($selectOption as $key => $val) {
  77. $html .= " {$key}=\"{$val}\"";
  78. }
  79. $html .= ">\n";
  80. if (isset ( $htmlOptions ['empty'] )) {
  81. $value = "";
  82. $label = $htmlOptions ['empty'];
  83. if (is_array ( $label )) {
  84. $value = array_keys ( $label );
  85. $value = $value [0];
  86. $label = array_values ( $label );
  87. $label = $label [0];
  88. }
  89. $html .= "<option value=\"{$value}\">{$label}</option>\n";
  90. }
  91. $optionHtml = $this->option($table_code, $code, $selection);
  92. $html .= $optionHtml;
  93. $html .= "</select>";
  94. return $html;
  95. }
  96. /**
  97. * 创建select的option选项
  98. *
  99. * @param $table_code
  100. * @param $code
  101. * @param null $selection
  102. * @return string
  103. */
  104. public function option($table_code, $code, $selection = null)
  105. {
  106. $data = $this->get($table_code, $code);
  107. $html = "";
  108. foreach ($data as $k => $v) {
  109. $html .= "<option value=\"{$k}\"";
  110. if($k == $selection) {
  111. $html .= " selected ";
  112. }
  113. $html .= ">{$v}</option>\n";
  114. }
  115. return $html;
  116. }
  117. /**
  118. * 更新对应数据字典,如参数都为空全部更新
  119. *
  120. * @param string $table_code 需要更新的类型
  121. * @param string $code 需要更新的代码
  122. * @return bool 返回成功失败
  123. * @throws CException
  124. */
  125. public function updateCache()
  126. {
  127. $obData = $this->_model->all();
  128. $dict = array();
  129. foreach($obData as $key => $value) {
  130. $dict[$value->dict_table_code][$value->dict_code][$value->value] = $value->name;
  131. }
  132. $this->_dict = $dict;
  133. Cache::forever($this->_mmCache, $this->_dict);
  134. return true;
  135. }
  136. }