common.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * @param $uid
  15. * @return mixed
  16. */
  17. function getUserNickname($uid){
  18. return \app\admin\model\user\User::where('uid',$uid)->value('nickname');
  19. }
  20. /**删除缓存hash数据
  21. * @param $name
  22. * @param $key
  23. * @return bool
  24. */
  25. function del_redis_hash($name, $key)
  26. {
  27. if (!$name || !$key) return false;
  28. $redisModel = new \think\cache\driver\Redis();
  29. $site_url = \service\SystemConfigService::get('site_url');
  30. $subjectUrl=GetUrlToDomain($site_url);
  31. $name=$subjectUrl.$subjectUrl;
  32. $exists_recommend_redis = $redisModel->HEXISTS($name,$key);
  33. if ($exists_recommend_redis) $redisModel->hdel($name,$key);
  34. return true;
  35. }
  36. /**
  37. * 获取产品名称
  38. * @param $id
  39. * @return mixed
  40. */
  41. function getProductName($id){
  42. return \app\admin\model\store\StoreProduct::where('id',$id)->value('store_name');
  43. }
  44. /**
  45. * 获取订单编号
  46. * @param $id
  47. */
  48. function getOrderId($id){
  49. return \app\admin\model\order\StoreOrder::where('id',$id)->value('order_id');
  50. }
  51. /**
  52. * 根据用户uid获取订单数
  53. * @param $uid
  54. * @return int|string
  55. */
  56. function getOrderCount($uid){
  57. return \app\admin\model\order\StoreOrder::where('uid',$uid)->where('paid',1)->where('refund_status',0)->where('status',2)->count();
  58. }
  59. /**
  60. * 格式化属性
  61. * @param $arr
  62. * @return array
  63. */
  64. function attrFormat($arr){
  65. $data = [];
  66. $res = [];
  67. if(count($arr) > 1){
  68. for ($i=0; $i < count($arr)-1; $i++) {
  69. if($i == 0) $data = $arr[$i]['detail'];
  70. //替代变量1
  71. $rep1 = [];
  72. foreach ($data as $v) {
  73. foreach ($arr[$i+1]['detail'] as $g) {
  74. //替代变量2
  75. $rep2 = ($i!=0?'':$arr[$i]['value']."_").$v."-".$arr[$i+1]['value']."_".$g;
  76. $tmp[] = $rep2;
  77. if($i==count($arr)-2){
  78. foreach (explode('-', $rep2) as $k => $h) {
  79. //替代变量3
  80. $rep3 = explode('_', $h);
  81. //替代变量4
  82. $rep4['detail'][$rep3[0]] = $rep3[1];
  83. }
  84. $res[] = $rep4;
  85. }
  86. }
  87. }
  88. $data = $tmp;
  89. }
  90. }else{
  91. $dataArr = [];
  92. foreach ($arr as $k=>$v){
  93. foreach ($v['detail'] as $kk=>$vv){
  94. $dataArr[$kk] = $v['value'].'_'.$vv;
  95. $res[$kk]['detail'][$v['value']] = $vv;
  96. }
  97. }
  98. $data[] = implode('-',$dataArr);
  99. }
  100. return [$data,$res];
  101. }
  102. /**
  103. * 格式化月份
  104. * @param string $time
  105. * @param int $ceil
  106. * @return array
  107. */
  108. function getMonth($time='',$ceil=0){
  109. if(empty($time)){
  110. $firstday = date("Y-m-01",time());
  111. $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day"));
  112. }else if($time=='n'){
  113. if($ceil!=0)
  114. $season = ceil(date('n') /3)-$ceil;
  115. else
  116. $season = ceil(date('n') /3);
  117. $firstday=date('Y-m-01',mktime(0,0,0,($season - 1) *3 +1,1,date('Y')));
  118. $lastday=date('Y-m-t',mktime(0,0,0,$season * 3,1,date('Y')));
  119. }else if($time=='y'){
  120. $firstday=date('Y-01-01');
  121. $lastday=date('Y-12-31');
  122. }else if($time=='h'){
  123. $firstday = date('Y-m-d', strtotime('this week +'.$ceil.' day')) . ' 00:00:00';
  124. $lastday = date('Y-m-d', strtotime('this week +'.($ceil+1).' day')) . ' 23:59:59';
  125. }
  126. return array($firstday,$lastday);
  127. }
  128. /**删除目录下所有文件
  129. * @param $path 目录或者文件路径
  130. * @param string $ext
  131. * @return bool
  132. */
  133. function clearfile($path,$ext = '*.log')
  134. {
  135. $files = (array) glob($path.DS.'*');
  136. foreach ($files as $path) {
  137. if (is_dir($path)) {
  138. $matches = glob($path . '/'.$ext);
  139. if (is_array($matches)) {
  140. array_map('unlink', $matches);
  141. }
  142. rmdir($path);
  143. } else {
  144. unlink($path);
  145. }
  146. }
  147. return true;
  148. }
  149. function get_server_ip(){
  150. if(isset($_SERVER)){
  151. if(array_key_exists("SERVER_ADDR",$_SERVER)){
  152. $server_ip=$_SERVER['SERVER_ADDR'];
  153. }else{
  154. $server_ip=$_SERVER['LOCAL_ADDR'];
  155. }
  156. }else{
  157. $server_ip = getenv('SERVER_ADDR');
  158. }
  159. return $server_ip;
  160. }
  161. /**
  162. * 获取CRMEB系统版本号
  163. * @param string $default
  164. * @return string
  165. */
  166. function get_crmeb_version($default = 'v1.0.0')
  167. {
  168. try {
  169. $version = parse_ini_file(app()->getRootPath() . '.version');
  170. return $version['version'] ?? $default;
  171. } catch (\Throwable $e) {
  172. return $default;
  173. }
  174. }
  175. if (!function_exists('check_phone')) {
  176. /**
  177. * 手机号验证
  178. * @param $phone
  179. * @return false|int
  180. */
  181. function check_phone($phone)
  182. {
  183. return preg_match( "/^1[3456789]\d{9}$/", $phone);
  184. }
  185. }