common.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. /**
  13. * 敏感词过滤
  14. *
  15. * @param string
  16. * @return string
  17. */
  18. function sensitive_words_filter($str)
  19. {
  20. header('content-type:text/html;charset=utf-8');
  21. if (!$str) return '';
  22. $file = ROOT_PATH . 'public/static/plug/censorwords/CensorWords';
  23. $words = file($file);
  24. foreach ($words as $word) {
  25. $word = str_replace(array("\r\n", "\r", "\n", " "), '', $word);
  26. if (!$word) continue;
  27. $ret = @preg_match("/$word/", $str, $match);
  28. if ($ret) {
  29. return $match[0];
  30. }
  31. }
  32. return '';
  33. }
  34. function getController()
  35. {
  36. return strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_', think\Request::instance()->controller()));
  37. }
  38. function getModule()
  39. {
  40. return strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_', think\Request::instance()->module()));
  41. }
  42. /**
  43. * 获取图片库链接地址
  44. * @param $key
  45. * @return string
  46. */
  47. function get_image_Url($key)
  48. {
  49. return think\Url::build('admin/widget.images/index', ['fodder' => $key]);
  50. }
  51. /**
  52. * 获取链接对应的key
  53. * @param $value
  54. * @param bool $returnType
  55. * @param string $rep
  56. * @return array|string
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. * @throws \think\exception\DbException
  60. */
  61. function get_key_attr($value, $returnType = true, $rep = '')
  62. {
  63. if (!$value) return '';
  64. $inif = \app\admin\model\system\SystemAttachment::where('att_dir', $value)->find();
  65. if ($inif) {
  66. return [
  67. 'key' => $inif->name,
  68. 'pic' => $value,
  69. ];
  70. } else {
  71. if ($returnType) {
  72. return [
  73. 'key' => '',
  74. 'pic' => $value,
  75. ];
  76. } else {
  77. return [
  78. 'key' => '',
  79. 'pic' => '',
  80. ];
  81. }
  82. }
  83. }
  84. /**
  85. * 获取系统配置内容
  86. * @param $name
  87. * @param string $default
  88. * @return string
  89. */
  90. function get_config_content($name, $default = '')
  91. {
  92. try {
  93. return \app\admin\model\system\SystemConfigContent::getValue($name);
  94. } catch (\Throwable $e) {
  95. return $default;
  96. }
  97. }
  98. /**
  99. * 打印日志
  100. * @param $name
  101. * @param $data
  102. * @param int $type
  103. */
  104. function live_log($name, $data, $type = 8)
  105. {
  106. file_put_contents($name . '.txt', '[' . date('Y-m-d H:i:s', time()) . ']' . print_r($data, true) . "\r\n", $type);
  107. }
  108. /**获取当前登录用户的角色信息
  109. * @return mixed
  110. */
  111. function get_login_role() {
  112. $role['role_id'] = \think\Session::get("adminInfo")['roles'];
  113. $role['role_sign'] = \think\Session::get("adminInfo")['role_sign'];
  114. return $role;
  115. }
  116. /**获取登录用户账户信息
  117. * @return mixed
  118. */
  119. function get_login_id() {
  120. $admin['admin_id'] = \think\Session::get("adminId");
  121. return $admin;
  122. }
  123. /**全局修改数据
  124. * @param string $field要修改的字段
  125. * @param array $where条件 数组
  126. * @param string $value修改值
  127. * @param $model_type 数据库表 不带前缀
  128. */
  129. function set_field_value(array $update, array $where , $value = '', $model_type) {
  130. if (!$update || !$where || $model_type == '') {
  131. return \service\JsonService::fail('缺少参数');
  132. }
  133. $model_type = \service\ModeService::switch_model($model_type);
  134. if (!$model_type) return \service\JsonService::fail('缺少参数');
  135. $res = $model_type::where($where)->update($update);
  136. if ($res) return \service\JsonService::successful('保存成功');
  137. return \service\JsonService::fail('保存失败');
  138. }
  139. function money_rate_num($money, $type) {
  140. if (!$money) $money = 0;
  141. if (!$type) return \service\JsonService::fail('非法参数2');
  142. switch ($type) {
  143. case "gold":
  144. $goldRate = \service\SystemConfigService::get("gold_rate");
  145. $num = $money * (int) $goldRate;
  146. return $num;
  147. default:
  148. return \service\JsonService::fail('汇率类型缺失');
  149. }
  150. }
  151. function getUrlToDomain($domain) {
  152. if($domain=='') $domain=$_SERVER['PHP_SELF'];
  153. $arr = parse_url($domain);
  154. if (!isset($arr['host'])) $arr['host'] = $arr['path'];
  155. return $arr['host'];
  156. }
  157. if (!function_exists('filter_emoji')) {
  158. // 过滤掉emoji表情
  159. function filter_emoji($str)
  160. {
  161. $str = preg_replace_callback( //执行一个正则表达式搜索并且使用一个回调进行替换
  162. '/./u',
  163. function (array $match) {
  164. return strlen($match[0]) >= 4 ? '' : $match[0];
  165. },
  166. $str);
  167. return $str;
  168. }
  169. }