common.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. // 门户模块公共函数库
  12. use think\Db;
  13. if (!function_exists('get_column_name')) {
  14. /**
  15. * 获取栏目名称
  16. * @param int $cid 栏目id
  17. * @author 蔡伟明 <314013107@qq.com>
  18. * @return string
  19. */
  20. function get_column_name($cid = 0)
  21. {
  22. $column_list = model('cms/column')->getList();
  23. return isset($column_list[$cid]) ? $column_list[$cid]['name'] : '';
  24. }
  25. }
  26. if (!function_exists('get_model_name')) {
  27. /**
  28. * 获取内容模型名称
  29. * @param string $id 内容模型id
  30. * @author 蔡伟明 <314013107@qq.com>
  31. * @return string
  32. */
  33. function get_model_name($id = '')
  34. {
  35. $model_list = model('cms/model')->getList();
  36. return isset($model_list[$id]) ? $model_list[$id]['name'] : '';
  37. }
  38. }
  39. if (!function_exists('get_model_title')) {
  40. /**
  41. * 获取内容模型标题
  42. * @param string $id 内容模型标题
  43. * @author 蔡伟明 <314013107@qq.com>
  44. * @return string
  45. */
  46. function get_model_title($id = '')
  47. {
  48. $model_list = model('cms/model')->getList();
  49. return isset($model_list[$id]) ? $model_list[$id]['title'] : '';
  50. }
  51. }
  52. if (!function_exists('get_model_type')) {
  53. /**
  54. * 获取内容模型类别:0-系统,1-普通,2-独立
  55. * @param int $id 模型id
  56. * @author 蔡伟明 <314013107@qq.com>
  57. * @return string
  58. */
  59. function get_model_type($id = 0)
  60. {
  61. $model_list = model('cms/model')->getList();
  62. return isset($model_list[$id]) ? $model_list[$id]['type'] : '';
  63. }
  64. }
  65. if (!function_exists('get_model_table')) {
  66. /**
  67. * 获取内容模型附加表名
  68. * @param int $id 模型id
  69. * @author 蔡伟明 <314013107@qq.com>
  70. * @return string
  71. */
  72. function get_model_table($id = 0)
  73. {
  74. $model_list = model('cms/model')->getList();
  75. return isset($model_list[$id]) ? $model_list[$id]['table'] : '';
  76. }
  77. }
  78. if (!function_exists('is_default_field')) {
  79. /**
  80. * 检查是否为系统默认字段
  81. * @param string $field 字段名称
  82. * @author 蔡伟明 <314013107@qq.com>
  83. * @return bool
  84. */
  85. function is_default_field($field = '')
  86. {
  87. $system_fields = cache('cms_system_fields');
  88. if (!$system_fields) {
  89. $system_fields = Db::name('cms_field')->where('model', 0)->column('name');
  90. cache('cms_system_fields', $system_fields);
  91. }
  92. return in_array($field, $system_fields, true);
  93. }
  94. }
  95. if (!function_exists('table_exist')) {
  96. /**
  97. * 检查附加表是否存在
  98. * @param string $table_name 附加表名
  99. * @author 蔡伟明 <314013107@qq.com>
  100. * @return string
  101. */
  102. function table_exist($table_name = '')
  103. {
  104. return true == Db::query("SHOW TABLES LIKE '{$table_name}'");
  105. }
  106. }
  107. if (!function_exists('time_tran')) {
  108. /**
  109. * 转换时间
  110. * @param int $timer 时间戳
  111. * @author 蔡伟明 <314013107@qq.com>
  112. * @return string
  113. */
  114. function time_tran($timer)
  115. {
  116. $diff = $_SERVER['REQUEST_TIME'] - $timer;
  117. $day = floor($diff / 86400);
  118. $free = $diff % 86400;
  119. if ($day > 0) {
  120. return $day . " 天前";
  121. } else {
  122. if ($free > 0) {
  123. $hour = floor($free / 3600);
  124. $free = $free % 3600;
  125. if ($hour > 0) {
  126. return $hour . " 小时前";
  127. } else {
  128. if ($free > 0) {
  129. $min = floor($free / 60);
  130. $free = $free % 60;
  131. if ($min > 0) {
  132. return $min . " 分钟前";
  133. } else {
  134. if ($free > 0) {
  135. return $free . " 秒前";
  136. } else {
  137. return '刚刚';
  138. }
  139. }
  140. } else {
  141. return '刚刚';
  142. }
  143. }
  144. } else {
  145. return '刚刚';
  146. }
  147. }
  148. }
  149. }