Links.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | Links模型
  13. // +----------------------------------------------------------------------
  14. namespace addons\links\model;
  15. use think\facade\Db;
  16. use think\Model;
  17. /**
  18. * 模型
  19. */
  20. class Links extends Model
  21. {
  22. /**
  23. * 获取友情链接
  24. * @param $tag
  25. */
  26. public function getList($tag)
  27. {
  28. $tag['order'] = !empty($tag['order']) ? $tag['order'] : 'listorder,id desc';
  29. $cacheTime = !empty($tag['cache']) && is_numeric($tag['cache']) ? intval($tag['cache']) : 3600;
  30. //每页显示总数
  31. $num = isset($tag['num']) && intval($tag['num']) > 0 ? intval($tag['num']) : 10;
  32. $cacheID = to_guid_string($tag);
  33. if ($cacheData = cache($cacheID)) {
  34. return $cacheData;
  35. }
  36. $map = [['status', '=', 1]];
  37. if (isset($tag['link_type'])) {
  38. $map[] = ['link_type', '=', (int) $tag['link_type']];
  39. }
  40. if (isset($tag['terms_id'])) {
  41. $map[] = ['terms_id', '=', (int) $tag['terms_id']];
  42. }
  43. $obj = $this->where($map)->where($tag['where'])->order($tag['order'])->limit($num)->select();
  44. if (is_array($obj)) {
  45. if (empty($obj)) {
  46. return [];
  47. }
  48. $array = $obj;
  49. } else {
  50. if ($obj->isEmpty()) {
  51. return [];
  52. }
  53. $array = $obj->toArray();
  54. }
  55. $lang = function_exists('cms_current_lang') ? cms_current_lang() : 'cn';
  56. $defaultLang = function_exists('cms_default_lang') ? cms_default_lang() : 'cn';
  57. foreach ($array as &$item) {
  58. $translate = null;
  59. if ($lang && $lang !== $defaultLang) {
  60. $translate = Db::name('links_data')->where(['did' => $item['id'], 'lang' => $lang])->find();
  61. if (!$translate) {
  62. $translate = Db::name('links_data')->where(['did' => $item['id'], 'lang' => $defaultLang])->find();
  63. }
  64. } else {
  65. $translate = Db::name('links_data')->where(['did' => $item['id'], 'lang' => $defaultLang])->find();
  66. }
  67. if ($translate) {
  68. if (!empty($translate['name'])) {
  69. $item['name'] = $translate['name'];
  70. }
  71. if (!empty($translate['description'])) {
  72. $item['description'] = $translate['description'];
  73. }
  74. }
  75. }
  76. unset($item);
  77. cache($cacheID, $array, $cacheTime);
  78. return $array;
  79. }
  80. }