function.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // 为方便系统核心升级,二次开发中需要用到的公共函数请写在这个文件,不要去修改common.php文件
  12. //取数组每个键值
  13. if(!function_exists('array_v')){
  14. function array_v($key,$arr=array())
  15. {
  16. return $arr?(array_key_exists($key,$arr)?$arr[$key]:$arr[0]):'';
  17. }
  18. }
  19. //静态文本选择器
  20. if(!function_exists('staticText')){
  21. function staticText($data='',$type='view',$pp='Y-m-d H:i'){
  22. if($type=='view'){
  23. return "<div>{$data}</div>";
  24. }elseif($type=='url'){
  25. if(!$data) return no_font('暂无');
  26. return "<a href='{$data}' target='_bank' title='点击打开'>{$data}</a>";
  27. }elseif($type=='pic'){
  28. if(!$data) return no_font('暂无');
  29. return "<style>img.dj_img{max-width:300px;max-height:300px;}</style><a href='".get_file_path($data)."' target='_bank' title='点击查看原图'><img src='".get_file_path($data)."' class='dj_img'></a>";
  30. }elseif ($type=='pics') {
  31. if(!$data) return no_font('暂无');
  32. $html="";
  33. if(!is_array($data)){
  34. $data=explode(',',$data);
  35. }
  36. foreach ($data as $k => $v) {
  37. if(!$data) return no_font('暂无');
  38. $html.="<style>img.dj_img{max-width:300px;max-height:300px;}</style><a href='".get_file_path($v)."' target='_bank' title='点击查看原图'><img src='".get_file_path($v)."' class='dj_img'></a>";
  39. }
  40. return $html;
  41. }elseif($type=='file'){
  42. if(!$data) return no_font('暂无');
  43. return "<a href='".get_file_path($data)."' title='".get_file_name($data)."'>".get_file_name($data)."</a>";
  44. }elseif ($type=='files') {
  45. if(!$data) return no_font('暂无');
  46. $html="";
  47. if(!is_array($data)){
  48. $data=explode(',',$data);
  49. }
  50. foreach ($data as $k => $v) {
  51. $html.="<li><a href='".get_file_path($v)."' title='".get_file_name($v)."'>".get_file_name($v)."</a></li>";
  52. }
  53. return $html;
  54. }elseif($type=='time'){
  55. if(!$data) return no_font('未知');
  56. return date($pp,$data);
  57. }elseif($type=='admin_username'){
  58. if(!$data) return no_font('未知');
  59. return admin_username($data);
  60. }elseif($type=='img'){
  61. if(!$data) return no_font('暂无');
  62. return "<style>img.dj_img{max-width:300px;max-height:300px;}</style><a href='".config('mall.public_url').$data."' target='_bank' title='点击查看原图'><img src='".config('mall.public_url').$data."' class='dj_img'></a>";
  63. }elseif ($type=='imgs') {
  64. if(!$data) return no_font('暂无');
  65. $html="";
  66. if(!is_array($data)){
  67. $data=explode(',',$data);
  68. }
  69. foreach ($data as $k => $v) {
  70. if(!$data) return no_font('暂无');
  71. $html.="<style>img.dj_img{max-width:300px;max-height:300px;}</style><a href='".config('mall.public_url').$v."' target='_bank' title='点击查看原图'><img src='".config('mall.public_url').$v."' class='dj_img'></a>";
  72. }
  73. return $html;
  74. }else{
  75. if(!$data) return no_font('暂无');
  76. return $data;
  77. }
  78. }
  79. }
  80. //判断是否有此菜单权限
  81. if(!function_exists('if_menu_auth')){
  82. function if_menu_auth($menu_id,$admin_id=UID){
  83. $role=db('admin_user')->where('id',$admin_id)->value('role');
  84. if($role=='1') return true;
  85. $menu_auth=db('admin_role')->where('id',$role)->value('menu_auth');
  86. if(strpos($menu_auth,'"'.$menu_id.'"')===false){
  87. return false;
  88. }else{
  89. return true;
  90. }
  91. }
  92. }
  93. //树形数组结构
  94. if(!function_exists('get_arr_tree')){
  95. function get_arr_tree($items,$pid ="parent_id") {
  96. $map = [];
  97. $tree = [];
  98. foreach ($items as &$it){ //数据的ID名生成新的引用索引树
  99. $map[$it['id']] = &$it;
  100. }
  101. foreach ($items as &$it){
  102. $parent = &$map[$it[$pid]];
  103. if($parent) {
  104. $parent['son'][] = &$it;
  105. }else{
  106. $tree[] = &$it;
  107. }
  108. }
  109. return $tree;
  110. }
  111. }
  112. //普通数组结构
  113. if(!function_exists('get_tree_ids')){
  114. function get_tree_ids($arr,$pid='0',$pix=0){
  115. global $tree_ids;
  116. foreach($arr as $key=>$value) {
  117. if($value['parent_id'] == $pid) {
  118. $tree_ids[$value['id']]= str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $value['level']-$pix).$value['name'];
  119. get_tree_ids($arr , $value['id'] );
  120. }
  121. }
  122. return $tree_ids;
  123. }
  124. }
  125. //生成供应商编号
  126. if(!function_exists('get_supplier_serial_num')){
  127. function get_supplier_serial_num(){
  128. $serial_num=mt_rand(0,99999999999);
  129. $serial_num=str_pad($serial_num,11,"0",STR_PAD_LEFT);
  130. $supplier=db('suppliers')->field('id')->where('serial_num',$serial_num)->find();
  131. if($supplier){
  132. get_supplier_serial_num();
  133. }else{
  134. return $serial_num;
  135. }
  136. }
  137. }
  138. //平台流水号随机生成
  139. if(!function_exists('get_platform_water_sn')){
  140. function get_platform_water_sn(){
  141. $sn=mt_rand(0,9999999999);
  142. $sn=str_pad($sn,10,"0",STR_PAD_LEFT);
  143. $sn=date('YmdHis').$sn;
  144. $platform_warte=db('platform_waters')->field('id')->where('sn',$sn)->find();
  145. if($platform_warte){
  146. get_platform_water_sn();
  147. }else{
  148. return $sn;
  149. }
  150. }
  151. }
  152. /**
  153. * 清空/删除 文件夹
  154. * @param string $dirname 文件夹路径
  155. * @param bool $self 是否删除当前文件夹
  156. * @return bool
  157. */
  158. if(!function_exists('do_rmdir')){
  159. function do_rmdir($dirname, $self = true) {
  160. if (!file_exists($dirname)) {
  161. return false;
  162. }
  163. if (is_file($dirname) || is_link($dirname)) {
  164. return @unlink($dirname);
  165. }
  166. $dir = dir($dirname);
  167. if ($dir) {
  168. while (false !== $entry = $dir->read()) {
  169. if ($entry == '.' || $entry == '..') {
  170. continue;
  171. }
  172. do_rmdir($dirname . '/' . $entry);
  173. }
  174. }
  175. $dir->close();
  176. $self && @rmdir($dirname);
  177. }
  178. }