CacheRepository.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\common\repositories\system;
  3. use app\common\dao\system\CacheDao;
  4. use app\common\repositories\BaseRepository;
  5. use think\db\exception\DbException;
  6. use think\exception\ValidateException;
  7. use think\facade\Cache;
  8. /**
  9. * Class CacheRepository
  10. * @package app\common\repositories\system
  11. * @author zfy
  12. * @day 2020-04-24
  13. * @mixin CacheDao
  14. */
  15. class CacheRepository extends BaseRepository
  16. {
  17. /**
  18. * CacheRepository constructor.
  19. * @param CacheDao $dao
  20. */
  21. public function __construct(CacheDao $dao)
  22. {
  23. $this->dao = $dao;
  24. }
  25. /**
  26. * @param string $key
  27. * @param $result
  28. * @param int $expire_time
  29. * @throws DbException
  30. * @author zfy
  31. * @day 2020-04-24
  32. */
  33. public function save(string $key, $result, int $expire_time = 0)
  34. {
  35. if (!$this->dao->fieldExists('key', $key)) {
  36. $this->dao->create(compact('key', 'result', 'expire_time'));
  37. } else {
  38. $this->dao->keyUpdate($key, compact('result', 'expire_time'));
  39. }
  40. }
  41. public function addres()
  42. {
  43. $re = (Cache::get('AAAAAA'));
  44. unset($re['省市编码']);
  45. if(!$re) throw new ValidateException('无数据');
  46. $shen = [];
  47. $shi =[];
  48. $qu = [];
  49. foreach ($re as $key => $value){
  50. $item = explode(',',$value);
  51. $cout = count($item);
  52. if($cout == 2){
  53. $shen[$item[1]] = [
  54. 'value' => $key ,
  55. 'label' => $item[1],
  56. ];
  57. }
  58. if($cout == 3){
  59. if($item[1] == ''){
  60. $shen[$item[2]] = [
  61. 'value' => $key ,
  62. 'label' => $item[2],
  63. ];
  64. $item[1] = $item[2];
  65. }
  66. $_v = [
  67. 'value' => $key,
  68. 'label' => $item[2]
  69. ];
  70. $shi[$item[1]][] = $_v;
  71. }
  72. if($cout == 4){
  73. $_v = [
  74. 'value' => $key,
  75. 'label' => $item[3]
  76. ];
  77. $qu[$item[2]][] = $_v;
  78. }
  79. }
  80. $data = [];
  81. foreach ($shen as $s => $c){
  82. foreach ($shi as $i => $c_){
  83. if($c['label'] == $i){
  84. if($c['label'] == $i) {
  85. $san = [];
  86. foreach ($c_ as $key => $value) {
  87. if (isset($qu[$value['label']])) {
  88. $value['child'] = $qu[$value['label']];
  89. $san[] = $value;
  90. }
  91. }
  92. }
  93. $c['child'] = $san;
  94. }
  95. }
  96. $zls[$s] = $c;
  97. }
  98. $data = array_values($zls);
  99. file_put_contents('address.js',json_encode($data,JSON_UNESCAPED_UNICODE));
  100. //$this->save('applyments_addres',$data);
  101. }
  102. }