CooperationOption.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class CooperationOption extends Model
  5. {
  6. protected $name = 'cooperation_option';
  7. protected $autoWriteTimestamp = false;
  8. public function getTypeList(): array
  9. {
  10. return [
  11. 'country_region' => '大洲/区域',
  12. 'country' => '国家/地区',
  13. 'industry' => '行业',
  14. ];
  15. }
  16. public static function getOptions(string $type, string $lang = 'cn'): array
  17. {
  18. $type = trim($type);
  19. $lang = strtolower(trim($lang));
  20. $lang = $lang !== '' ? $lang : 'cn';
  21. $rows = static::where(['type' => $type, 'lang' => $lang, 'status' => 1])
  22. ->order('listorder', 'asc')
  23. ->order('id', 'asc')
  24. ->select()
  25. ->toArray();
  26. if (!$rows && $lang !== 'cn') {
  27. $rows = static::where(['type' => $type, 'lang' => 'cn', 'status' => 1])
  28. ->order('listorder', 'asc')
  29. ->order('id', 'asc')
  30. ->select()
  31. ->toArray();
  32. }
  33. return $rows ?: [];
  34. }
  35. public static function getCountryTree(string $lang = 'cn'): array
  36. {
  37. $lang = strtolower(trim($lang));
  38. $lang = $lang !== '' ? $lang : 'cn';
  39. $regions = static::getOptions('country_region', $lang);
  40. $countries = static::getOptions('country', $lang);
  41. $tree = [];
  42. foreach ($regions as $region) {
  43. $value = (string)$region['value'];
  44. $tree[$value] = [
  45. 'title' => $region['title'],
  46. 'value' => $value,
  47. 'children' => [],
  48. ];
  49. }
  50. foreach ($countries as $country) {
  51. $parent = (string)($country['parent_value'] ?? '');
  52. if ($parent === '' || !isset($tree[$parent])) {
  53. $parent = 'OTHER';
  54. if (!isset($tree[$parent])) {
  55. $tree[$parent] = [
  56. 'title' => $lang === 'en' ? 'Other' : '其他',
  57. 'value' => $parent,
  58. 'children' => [],
  59. ];
  60. }
  61. }
  62. $tree[$parent]['children'][] = [
  63. 'title' => $country['title'],
  64. 'value' => $country['value'],
  65. ];
  66. }
  67. return array_values($tree);
  68. }
  69. }