CsmUtils.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // Csmmeet [ CSM系列公共源码 ]
  4. // Author: chensm <chenshiming0802@163.com>
  5. // Create by chensm at 2020-02-26
  6. // +----------------------------------------------------------------------
  7. namespace addons\csmadmin\library;
  8. use think\Config;
  9. use think\Request;
  10. use think\Response;
  11. use think\Url;
  12. use think\View as ViewTemplate;
  13. use think\exception\HttpResponseException;
  14. class CsmUtils
  15. {
  16. /**
  17. * 根据dao的select内容匹配到现有的list中,keyname为匹配对象
  18. */
  19. public static function fillListColumn(&$list,$fieldname,$daoselect,$daoselectfieldname){
  20. $keyvalues = [];
  21. foreach($list as $item){
  22. $keyvalues[] = $item->$fieldname;
  23. }
  24. $newlist = $daoselect->where($daoselectfieldname,'in',$keyvalues)->select();
  25. //echo $dao->getLastSql();
  26. $newlist2 = [];
  27. foreach($newlist as $item){
  28. $newlist2[$item->$daoselectfieldname] = $item;
  29. }
  30. foreach($list as $k=>$item){
  31. if(isset($newlist2[$item->$fieldname])){
  32. $list[$k] = array_merge(json_decode($newlist2[$item->$fieldname],true),json_decode($item,true));
  33. }
  34. }
  35. }
  36. /**
  37. * 转换list中的id字段,(根据dao中的表id对应的name)
  38. * 通常用于查询列表中用
  39. */
  40. public static function convertListColumn(&$list, $fieldname, $dao, $daokeyname = 'name')
  41. {
  42. $ids = [];
  43. foreach ($list as $item) {
  44. $ids[] = $item->$fieldname;
  45. }
  46. $keylist = $dao->where("id", "in", $ids)
  47. ->field("id,{$daokeyname}")
  48. ->select();
  49. // echo $dao->getLastSql();
  50. $keynames = [];
  51. foreach ($keylist as $keyrow) {
  52. $keynames['D' . $keyrow->id] = $keyrow->$daokeyname;
  53. }
  54. // var_dump($keynames);
  55. foreach ($list as $i => $item) {
  56. if (array_key_exists('D' . $item->$fieldname, $keynames)) {
  57. $list[$i][$fieldname] = $keynames['D' . $item->$fieldname];
  58. } else {
  59. $list[$i][$fieldname] = '';
  60. }
  61. }
  62. }
  63. /**
  64. * 邮件发送
  65. */
  66. public static function notifyUser($email, $title, $content)
  67. {
  68. $site = Config::get("site");
  69. $obj = \app\common\library\Email::instance();
  70. $obj->to($email)
  71. ->subject("[" . $site['name'] . "]" . $title)
  72. ->message($content)
  73. ->send();
  74. }
  75. public static function isNullOrBlank($str)
  76. {
  77. if ($str == null || $str == "") {
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. }
  83. public static function unescape($str)
  84. {
  85. $ret = '';
  86. $len = strlen($str);
  87. for ($i = 0; $i < $len; $i ++) {
  88. if ($str[$i] == '%' && $str[$i + 1] == 'u') {
  89. $val = hexdec(substr($str, $i + 2, 4));
  90. if ($val < 0x7f)
  91. $ret .= chr($val);
  92. else if ($val < 0x800)
  93. $ret .= chr(0xc0 | ($val >> 6)) . chr(0x80 | ($val & 0x3f));
  94. else
  95. $ret .= chr(0xe0 | ($val >> 12)) . chr(0x80 | (($val >> 6) & 0x3f)) . chr(0x80 | ($val & 0x3f));
  96. $i += 5;
  97. } else if ($str[$i] == '%') {
  98. $ret .= urldecode(substr($str, $i, 3));
  99. $i += 2;
  100. } else
  101. $ret .= $str[$i];
  102. }
  103. return $ret;
  104. }
  105. /**
  106. * Copy from traits\controller\Jump.php#error function
  107. */
  108. public static function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
  109. {
  110. if (is_null($url)) {
  111. $url = Request::instance()->isAjax() ? '' : 'javascript:history.back(-1);';
  112. } elseif ('' !== $url && ! strpos($url, '://') && 0 !== strpos($url, '/')) {
  113. $url = Url::build($url);
  114. }
  115. $type = Request::instance()->isAjax() ? Config::get('default_ajax_return') : Config::get('default_return_type');
  116. $result = [
  117. 'code' => 0,
  118. 'msg' => $msg,
  119. 'data' => $data,
  120. 'url' => $url,
  121. 'wait' => $wait
  122. ];
  123. if ('html' == strtolower($type)) {
  124. $template = Config::get('template');
  125. $view = Config::get('view_replace_str');
  126. $result = ViewTemplate::instance($template, $view)->fetch(Config::get('dispatch_error_tmpl'), $result);
  127. }
  128. $response = Response::create($result, $type)->header($header);
  129. throw new HttpResponseException($response);
  130. }
  131. /**
  132. * Copy from traits\controller\Jump.php#success function
  133. */
  134. public static function success($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
  135. {
  136. if (is_null($url) && ! is_null(Request::instance()->server('HTTP_REFERER'))) {
  137. $url = Request::instance()->server('HTTP_REFERER');
  138. } elseif ('' !== $url && ! strpos($url, '://') && 0 !== strpos($url, '/')) {
  139. $url = Url::build($url);
  140. }
  141. $type = Request::instance()->isAjax() ? Config::get('default_ajax_return') : Config::get('default_return_type');
  142. $result = [
  143. 'code' => 1,
  144. 'msg' => $msg,
  145. 'data' => $data,
  146. 'url' => $url,
  147. 'wait' => $wait
  148. ];
  149. if ('html' == strtolower($type)) {
  150. $template = Config::get('template');
  151. $view = Config::get('view_replace_str');
  152. $result = ViewTemplate::instance($template, $view)->fetch(Config::get('dispatch_success_tmpl'), $result);
  153. }
  154. $response = Response::create($result, $type)->header($header);
  155. throw new HttpResponseException($response);
  156. }
  157. }