RegionLogic.php 6.3 KB

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