| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace app\common\model;
- use think\Model;
- class CooperationOption extends Model
- {
- protected $name = 'cooperation_option';
- protected $autoWriteTimestamp = false;
- public function getTypeList(): array
- {
- return [
- 'country_region' => '大洲/区域',
- 'country' => '国家/地区',
- 'industry' => '行业',
- ];
- }
- public static function getOptions(string $type, string $lang = 'cn'): array
- {
- $type = trim($type);
- $lang = strtolower(trim($lang));
- $lang = $lang !== '' ? $lang : 'cn';
- $rows = static::where(['type' => $type, 'lang' => $lang, 'status' => 1])
- ->order('listorder', 'asc')
- ->order('id', 'asc')
- ->select()
- ->toArray();
- if (!$rows && $lang !== 'cn') {
- $rows = static::where(['type' => $type, 'lang' => 'cn', 'status' => 1])
- ->order('listorder', 'asc')
- ->order('id', 'asc')
- ->select()
- ->toArray();
- }
- return $rows ?: [];
- }
- public static function getCountryTree(string $lang = 'cn'): array
- {
- $lang = strtolower(trim($lang));
- $lang = $lang !== '' ? $lang : 'cn';
- $regions = static::getOptions('country_region', $lang);
- $countries = static::getOptions('country', $lang);
- $tree = [];
- foreach ($regions as $region) {
- $value = (string)$region['value'];
- $tree[$value] = [
- 'title' => $region['title'],
- 'value' => $value,
- 'children' => [],
- ];
- }
- foreach ($countries as $country) {
- $parent = (string)($country['parent_value'] ?? '');
- if ($parent === '' || !isset($tree[$parent])) {
- $parent = 'OTHER';
- if (!isset($tree[$parent])) {
- $tree[$parent] = [
- 'title' => $lang === 'en' ? 'Other' : '其他',
- 'value' => $parent,
- 'children' => [],
- ];
- }
- }
- $tree[$parent]['children'][] = [
- 'title' => $country['title'],
- 'value' => $country['value'],
- ];
- }
- return array_values($tree);
- }
- }
|