RegionLogic.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\logic\BaseLogic;
  4. use app\common\model\article\Article;
  5. use app\common\model\decorate\DecoratePage;
  6. use app\common\model\decorate\DecorateTabbar;
  7. use app\common\model\setting\PostageRegion;
  8. use app\common\service\ConfigService;
  9. use app\common\service\FileService;
  10. use think\Exception;
  11. /**
  12. * index
  13. * Class RegionLogic
  14. * @package app\api\logic
  15. */
  16. class RegionLogic extends BaseLogic
  17. {
  18. public array $notNeedLogin = ['getRegionList','getLocation','get_first_char','get_first_char','array_group_by','my_trim'];
  19. /**
  20. * 获取城市列表
  21. * @return array
  22. */
  23. public static function getRegionList()
  24. {
  25. $area_list = PostageRegion::cache(true,1)->where('first','<>','')->field('id,name,merge_name,pid')->order('sorts asc,id asc')->select()->toArray();
  26. foreach ($area_list as &$vo) {
  27. $vo['field'] = self::get_first_char($vo['name']);
  28. }
  29. $arr = self::array_group_by($area_list, 'field');
  30. ksort($arr);
  31. $citys = array();
  32. foreach ($arr as $keys=>$vo){
  33. $citys[] = ['key'=>$keys,'list'=>$vo];
  34. }
  35. ksort($citys);
  36. //获取热门城市
  37. $map['id'] = ['in','1710'];
  38. $hot_list = PostageRegion::cache(true,1)->where($map)->field('id,pid,name,merge_name')->order('first asc')->select()->toArray();
  39. $list = ['hot_list'=>$hot_list,'city_list'=> $citys];
  40. return $list;
  41. }
  42. /**
  43. * 获取定位信息
  44. * @param $params
  45. * @return false|mixed|void
  46. */
  47. public static function getLocation($params)
  48. {
  49. try {
  50. $location = $params['latitude'].','.$params['longitude'];
  51. $key = 'SEGBZ-P3PK6-CU5SB-MQLQP-ZDPJS-PHFR6';
  52. $url = 'https://apis.map.qq.com/ws/geocoder/v1/?location=' . $location . '&key='.$key.'&get_poi=1';
  53. $result = http_request($url);
  54. if(empty($result)){
  55. throw new Exception('定位请求失败');
  56. }
  57. if ($result['status'] === 0) {
  58. //获取城市id
  59. if(empty($result['result']['address_component']['city'])){
  60. throw new Exception('定位获取shibai');
  61. }
  62. $city_id = PostageRegion::where('name','like',"%".$result['result']['address_component']['city']."%")->value('id');
  63. $result = $result['result']['address_component'];
  64. $result['city_id'] = $city_id;
  65. $result['merge_name'] = implode(',',[$result['province'],$result['city'],$result['district']]);
  66. return $result;
  67. }
  68. } catch (\Exception $e) {
  69. self::$error = $e->getMessage();
  70. return false;
  71. }
  72. }
  73. public static function get_first_char($s)
  74. {
  75. $s0 = mb_substr(self::my_trim($s), 0, 1); //获取名字的姓
  76. try {
  77. $s = iconv('UTF-8', 'GB2312', $s0); //将UTF-8转换成GB2312编码
  78. } catch (\Exception $e) {
  79. return 'Z#';
  80. }
  81. if (ord($s0) > 128) { //汉字开头,汉字没有以U、V开头的
  82. $asc = ord($s[0]) * 256 + ord($s[1]) - 65536;
  83. if ($asc >= -20319 and $asc <= -20284) return "A";
  84. if ($asc >= -20283 and $asc <= -19776) return "B";
  85. if ($asc >= -19775 and $asc <= -19219) return "C";
  86. if ($asc >= -19218 and $asc <= -18711) return "D";
  87. if ($asc >= -18710 and $asc <= -18527) return "E";
  88. if ($asc >= -18526 and $asc <= -18240) return "F";
  89. if ($asc >= -18239 and $asc <= -17760) return "G";
  90. if ($asc >= -17759 and $asc <= -17248) return "H";
  91. if ($asc >= -17247 and $asc <= -17418) return "I";
  92. if ($asc >= -17417 and $asc <= -16475) return "J";
  93. if ($asc >= -16474 and $asc <= -16213) return "K";
  94. if ($asc >= -16212 and $asc <= -15641) return "L";
  95. if ($asc >= -15640 and $asc <= -15166) return "M";
  96. if ($asc >= -15165 and $asc <= -14923) return "N";
  97. if ($asc >= -14922 and $asc <= -14915) return "O";
  98. if ($asc >= -14914 and $asc <= -14631) return "P";
  99. if ($asc >= -14630 and $asc <= -14150) return "Q";
  100. if ($asc >= -14149 and $asc <= -14091) return "R";
  101. if ($asc >= -14090 and $asc <= -13319) return "S";
  102. if ($asc >= -13318 and $asc <= -12839) return "T";
  103. if ($asc >= -12838 and $asc <= -12557) return "W";
  104. if ($asc >= -12556 and $asc <= -11848) return "X";
  105. if ($asc >= -11847 and $asc <= -11056) return "Y";
  106. if ($asc >= -11055 and $asc <= -10247) return "Z";
  107. } else if (ord($s) >= 48 and ord($s) <= 57) { //数字开头
  108. switch (iconv_substr($s, 0, 1, 'utf-8')) {
  109. case 1:
  110. return "Y";
  111. case 2:
  112. return "E";
  113. case 3:
  114. return "S";
  115. case 4:
  116. return "S";
  117. case 5:
  118. return "W";
  119. case 6:
  120. return "L";
  121. case 7:
  122. return "Q";
  123. case 8:
  124. return "B";
  125. case 9:
  126. return "J";
  127. case 0:
  128. return "L";
  129. }
  130. } else if (ord($s) >= 65 and ord($s) <= 90) { //大写英文开头
  131. return substr($s, 0, 1);
  132. } else if (ord($s) >= 97 and ord($s) <= 122) { //小写英文开头
  133. return strtoupper(substr($s, 0, 1));
  134. } else {
  135. return iconv_substr($s0, 0, 1, 'utf-8');//中英混合的词语,不适合上面的各种情况,因此直接提取首个字符即可
  136. }
  137. }
  138. public static function array_group_by($arr, $key)
  139. {
  140. $grouped = array();
  141. foreach ($arr as $value) {
  142. $grouped[$value[$key]][] = $value;
  143. }
  144. if (func_num_args() > 2) {
  145. $args = func_get_args();
  146. foreach ($grouped as $key => $value) {
  147. $parms = array_merge($value, array_slice($args, 2, func_num_args()));
  148. $grouped[$key] = call_user_func_array('array_group_by', $parms);
  149. }
  150. }
  151. return $grouped;
  152. }
  153. public static function my_trim($str)
  154. {
  155. $search = array(" ", " ", "\n", "\r", "\t");
  156. $replace = array("", "", "", "", "");
  157. return str_replace($search, $replace, $str);
  158. }
  159. }