common.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 应用公共文件
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author:
  10. // +----------------------------------------------------------------------
  11. use \think\facade\Db;
  12. error_reporting(0);
  13. /**
  14. * @return mixed
  15. * 获取站点信息
  16. */
  17. if (!function_exists('site_name')) {
  18. function site_name()
  19. {
  20. return Db::name('config')->where('code', 'site_name')
  21. ->value('value');
  22. }
  23. }
  24. if (!function_exists('site_logo')) {
  25. function site_logo()
  26. {
  27. return Db::name('config')->where('code', 'site_logo')
  28. ->value('value');
  29. }
  30. }
  31. //获取配置信息
  32. if (!function_exists('getConfigByCode')) {
  33. function getConfigByCode($code)
  34. {
  35. return Db::name('config')->where('code', $code)
  36. ->value('value');
  37. }
  38. }
  39. /*
  40. * 百度编辑器内容 多个编辑器单独引入js,重复会导致出错
  41. */
  42. if (!function_exists('build_ueditor_js')) {
  43. function build_ueditor_js(){
  44. /* 配置界面语言 */
  45. switch (config('default_lang')) {
  46. case 'zh-cn':
  47. $lang = '/static/plugins/ueditor/lang/zh-cn/zh-cn.js';
  48. break;
  49. case 'en-us':
  50. $lang = '/static/plugins/ueditor/lang/en/en.js';
  51. break;
  52. default:
  53. $lang = '/static/plugins/ueditor/lang/zh-cn/zh-cn.js';
  54. break;
  55. }
  56. return $include_js = '<script type="text/javascript" charset="utf-8" src="/static/plugins/ueditor/ueditor.config.js"></script> <script type="text/javascript" charset="utf-8" src="/static/plugins/ueditor/ueditor.all.min.js""> </script><script type="text/javascript" charset="utf-8" src="' . $lang . '"></script>';
  57. }
  58. }
  59. if (!function_exists('build_ueditor')) {
  60. function build_ueditor($params = array())
  61. {
  62. $name = isset($params['name']) ? $params['name'] : null;
  63. $theme = isset($params['theme']) ? $params['theme'] : 'normal';
  64. $content = isset($params['content']) ? $params['content'] : null;
  65. //http://fex.baidu.com/ueditor/#start-toolbar
  66. /* 指定使用哪种主题 */
  67. $themes = array(
  68. 'normal' => "[
  69. 'fullscreen', 'source', '|', 'undo', 'redo', '|',
  70. 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
  71. 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
  72. 'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
  73. 'directionalityltr', 'directionalityrtl', 'indent', '|',
  74. 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
  75. 'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
  76. 'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertvideo', 'music', 'attachment', 'map', 'gmap', 'insertframe', 'insertcode', 'webapp', 'pagebreak', 'template', 'background', '|',
  77. 'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|',
  78. 'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|',
  79. 'print', 'preview', 'searchreplace', 'drafts', 'help'
  80. ]", 'simple' => " ['fullscreen', 'source', 'undo', 'redo', 'bold']",
  81. );
  82. switch ($theme) {
  83. case 'simple':
  84. $theme_config = $themes['simple'];
  85. break;
  86. case 'normal':
  87. $theme_config = $themes['normal'];
  88. break;
  89. default:
  90. $theme_config = $themes['normal'];
  91. break;
  92. }
  93. if(!isset($params['type']) || (isset($params['type']) && $params['type']==0) ){
  94. $include_js = build_ueditor_js();
  95. }elseif($params['type']==1 ){
  96. $include_js = '';
  97. }
  98. $content = json_encode($content);
  99. $str = <<<EOT
  100. $include_js
  101. <script type="text/javascript">
  102. var ue_{$name} = UE.getEditor('{$name}',{
  103. toolbars:[{$theme_config}],
  104. });
  105. if($content){
  106. ue_{$name}.ready(function() {
  107. this.setContent($content);
  108. })
  109. }
  110. </script>
  111. EOT;
  112. return $str;
  113. }
  114. }
  115. if (!function_exists('p')) {
  116. function p($var, $die = 0) {
  117. print_r($var);
  118. $die && die();
  119. }
  120. }
  121. if (!function_exists('isMobile')) {
  122. function isMobile()
  123. {
  124. if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) {
  125. return true;
  126. }
  127. if (isset ($_SERVER['HTTP_VIA'])) {
  128. return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
  129. }
  130. if (isset ($_SERVER['HTTP_USER_AGENT'])) {
  131. $clientkeywords = array('nokia',
  132. 'sony',
  133. 'ericsson',
  134. 'mot',
  135. 'samsung',
  136. 'htc',
  137. 'sgh',
  138. 'lg',
  139. 'sharp',
  140. 'sie-',
  141. 'philips',
  142. 'panasonic',
  143. 'alcatel',
  144. 'lenovo',
  145. 'iphone',
  146. 'ipod',
  147. 'blackberry',
  148. 'meizu',
  149. 'android',
  150. 'netfront',
  151. 'symbian',
  152. 'ucweb',
  153. 'windowsce',
  154. 'palm',
  155. 'operamini',
  156. 'operamobi',
  157. 'openwave',
  158. 'nexusone',
  159. 'cldc',
  160. 'midp',
  161. 'wap',
  162. 'mobile'
  163. );
  164. if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
  165. return true;
  166. }
  167. }
  168. if (isset ($_SERVER['HTTP_ACCEPT'])) {
  169. if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. }
  176. if (!function_exists("json_result")){
  177. /** 返回接送 接口
  178. * @param int $code
  179. * @param string $msg
  180. * @param $data
  181. */
  182. function json_result($code=200,$msg="",$data=""){
  183. $dat["code"]=$code;
  184. $dat["msg"]=$msg;
  185. $dat["data"]=$data;
  186. $json=json_encode($dat);
  187. echo $json;exit();
  188. }
  189. }
  190. /**
  191. * 生成操作按钮
  192. * @param array $operate 操作按钮数组
  193. */
  194. if (!function_exists("showOperate")){
  195. function showOperate($operate = [])
  196. {
  197. if(empty($operate)){
  198. return '';
  199. }
  200. $option = <<<EOT
  201. <div class="btn-group">
  202. <button class="btn btn-primary btn-sm dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
  203. 操作 <span class="caret"></span>
  204. </button>
  205. <ul class="dropdown-menu">
  206. EOT;
  207. foreach($operate as $key=>$vo){
  208. $option .= '<li><a href="'.$vo.'">'.$key.'</a></li>';
  209. }
  210. $option .= '</ul></div>';
  211. return $option;
  212. }
  213. }
  214. if (!function_exists("time2string")){
  215. function time2string($second){
  216. $day = floor($second/(3600*24));
  217. $second = $second%(3600*24);//除去整天之后剩余的时间
  218. $hour = floor($second/3600);
  219. $second = $second%3600;//除去整小时之后剩余的时间
  220. $minute = floor($second/60);
  221. $second = $second%60;//除去整分钟之后剩余的时间
  222. //返回字符串
  223. return $day.'天'.$hour.'小时';
  224. }
  225. }
  226. if (!function_exists("is_admin")){
  227. function is_admin(){
  228. $session= \think\facade\Session::get("admin");
  229. $admin=$session->toArray();
  230. if ($admin["project"]){
  231. return $admin["project"];
  232. }else{
  233. return false;
  234. }
  235. }
  236. }
  237. // 成绩转换 成数字
  238. if (!function_exists("achievement_switch")){
  239. function achievement_switch($name){
  240. $age=0;
  241. switch ($name){
  242. case "A":$age=4;break;
  243. case "a":$age=4;break;
  244. case "B":$age=3;break;
  245. case "b":$age=3;break;
  246. case "C":$age=2;break;
  247. case "c":$age=2;break;
  248. case "D":$age=1;break;
  249. case "d":$age=1;break;
  250. case "F":$age=0;break;
  251. case "f":$age=0;break;
  252. }
  253. return $age;
  254. }
  255. }
  256. // 成绩转换成 单词
  257. if (!function_exists("achievement_change")){
  258. function achievement_change($name){
  259. $age="F";
  260. switch ($name){
  261. case 4: $age="A";break;
  262. case 3: $age="B";break;
  263. case 2: $age="C";break;
  264. case 1: $age="D";break;
  265. case 0: $age="F";break;
  266. case -1: $age="录入中";break;
  267. }
  268. return $age;
  269. }
  270. }
  271. // 等级换算
  272. if (!function_exists("star_deng")){
  273. function star_deng($name){
  274. $age="初出茅庐";
  275. switch ($name){
  276. case 0: $age="初出茅庐";break;
  277. case 1: $age="心令神会";break;
  278. case 2: $age="出类拔萃";break;
  279. case 3: $age="名列前茅";break;
  280. case 4: $age="百里挑一";break;
  281. }
  282. return $age;
  283. }
  284. }
  285. // 升星标准
  286. if (!function_exists("star_class")){
  287. function star_class($name){
  288. $age=0;
  289. if ($name>=3.4){
  290. $age=5;
  291. }elseif ($name>=2.8&&$name<3.4){
  292. $age=4;
  293. }elseif ($name>=2.2&&$name<2.8){
  294. $age=3;
  295. }elseif ($name>=1.6&&$name<2.2){
  296. $age=2;
  297. }elseif ($name>=1&&$name<1.5){
  298. $age=1;
  299. }
  300. return $age;
  301. }
  302. }
  303. // 专业等级
  304. if (!function_exists("major_levels")){
  305. function major_levels($num,$subject_id){
  306. $where[]=["max",">=",$num];
  307. $where[]=["subject_id","=",$subject_id];
  308. $type=Db::name("major_level")->order("type asc")->where($where)->find();
  309. if (!$type){
  310. if ($num>0){
  311. return 4;
  312. }else{
  313. return 0;
  314. }
  315. }
  316. return $type["type"];
  317. }
  318. }
  319. function sortArr($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){
  320. $key_arrays =array();
  321. if(is_array($arrays)){
  322. foreach ($arrays as $array){
  323. if(is_array($array)){
  324. $key_arrays[] = $array[$sort_key];
  325. }else{
  326. return false;
  327. }
  328. }
  329. }else{
  330. return false;
  331. }
  332. array_multisort($key_arrays,$sort_order,$sort_type,$arrays);
  333. return $arrays;
  334. }