common.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. use think\Db;
  3. /**
  4. * 秒转换为天
  5. */
  6. function get_stay_time($remain_time, $is_hour = 1, $is_minutes = 1)
  7. {
  8. $day = floor($remain_time / (3600*24));
  9. $day = $day > 0 ? $day.'天' : '';
  10. $hour = floor(($remain_time % (3600*24)) / 3600);
  11. $hour = $hour > 0 ? $hour.'小时' : '';
  12. if($is_hour && $is_minutes) {
  13. $minutes = floor((($remain_time % (3600*24)) % 3600) / 60);
  14. $minutes = $minutes > 0 ? $minutes.'分钟' : '';
  15. return $day.$hour.$minutes;
  16. }
  17. if($hour) {
  18. return $day.$hour;
  19. }
  20. return $day;
  21. }
  22. function requestPost($url , $post_data = array() ){
  23. // 1. 初始化一个cURL会话
  24. $ch = curl_init();
  25. // 2. 设置请求选项, 包括具体的url
  26. curl_setopt($ch, CURLOPT_URL, $url);
  27. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  28. // 设置请求为post类型
  29. curl_setopt($ch, CURLOPT_POST, 1);
  30. // 添加post数据到请求中
  31. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  32. curl_setopt($ch, CURLOPT_HEADER, 0);
  33. // 3. 执行一个cURL会话并且获取相关回复
  34. $response = curl_exec($ch);
  35. // 4. 释放cURL句柄,关闭一个cURL会话
  36. curl_close($ch);
  37. return $response;
  38. }
  39. /**
  40. * get请求
  41. * @param $url
  42. * @param string $msg
  43. * @return mixed
  44. */
  45. function requestGet($url , $msg = ''){
  46. // 1. 初始化一个cURL会话
  47. $ch = curl_init();
  48. //设置选项,包括URL
  49. curl_setopt($ch, CURLOPT_URL, $url);
  50. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  51. curl_setopt($ch, CURLOPT_HEADER, 0);
  52. //执行并获取HTML文档内容
  53. $response = curl_exec($ch);
  54. // 4. 释放cURL句柄,关闭一个cURL会话
  55. curl_close($ch);
  56. return $response;
  57. }
  58. // 获取模板区域
  59. function get_city_area()
  60. {
  61. $field=['id','pid','name'];
  62. $list=Db::name('china_area')->where('pid',0)->field($field)->select();
  63. foreach ($list as $k=>&$v){
  64. $v['children']= Db::name('china_area')->where('pid',$v['id'])->field($field)->select();
  65. }
  66. return $list;
  67. }
  68. function http_curl($url,$type='get',$res='json',$arr=''){
  69. $headers = array();
  70. //根据API的要求,定义相对应的Content-Type
  71. array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8;application/json");
  72. $curl = curl_init();
  73. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $type);
  74. curl_setopt($curl, CURLOPT_URL, $url);
  75. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  76. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  77. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  78. curl_setopt($curl, CURLOPT_HEADER, false);
  79. $output = curl_exec($curl);
  80. curl_close($curl);
  81. if($res=='json'){
  82. if($output === false){
  83. //请求失败,返回错误信息
  84. return curl_error($curl);
  85. }else{
  86. //请求成功,返回信息
  87. return json_decode($output,true);
  88. }
  89. }
  90. }
  91. function curl_get($url)
  92. {
  93. $ch = curl_init();
  94. curl_setopt($ch, CURLOPT_URL, $url);
  95. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  96. $output = curl_exec($ch);
  97. curl_close($ch);
  98. if($output === false){
  99. return curl_error($ch);
  100. }else{
  101. return json_decode($output,true);
  102. }
  103. }
  104. /**
  105.  * 把返回的数据集转换成Tree
  106.  * @param array $list 要转换的数据集
  107.  * @param string $pk 自增字段(栏目id)
  108.  * @param string $pid parent标记字段
  109.  * @return array
  110.  */
  111. function make_tree($list,$pk='id',$pid='pid',$child='children',$root=0){
  112. if(is_object($list)) $list = $list->toArray();
  113. $tree=array();
  114. $packData=array();
  115. foreach ($list as $data) {
  116. $packData[$data[$pk]] = $data;
  117. }
  118. foreach ($packData as $key =>$val){
  119. if($val[$pid]==$root){//代表跟节点
  120. $tree[]=& $packData[$key];
  121. }else{
  122. $packData[$val[$pid]][$child][]=& $packData[$key]; //找到其父类
  123. }
  124. }
  125. return $tree;
  126. }
  127. //判断字段存在并不为空
  128. function isset_full($arr, $key)
  129. {
  130. if (isset($arr[$key]) && !empty($arr[$key])) {
  131. return true;
  132. } else {
  133. return false;
  134. }
  135. }
  136. //判断字段存在并不为空 并且等于验证值
  137. function isset_full_check($arr, $key,$check_val)
  138. {
  139. if (isset($arr[$key]) && !empty($arr[$key]) && $arr[$key] == $check_val) {
  140. return true;
  141. } else {
  142. return false;
  143. }
  144. }
  145. function all_pay_type()
  146. {
  147. return [ '--','微信[H5]','支付宝[H5]','微信[APP]','支付宝[APP]','微信[公众号]' ];
  148. }
  149. /**
  150. * 生成32位随机数
  151. */
  152. function get32Str($length='32'){
  153. $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  154. $len = strlen($str)-1;
  155. $randstr = '';
  156. for ($i=0;$i<$length;$i++) {
  157. $num=mt_rand(0,$len);
  158. $randstr .= $str[$num];
  159. }
  160. return $randstr;
  161. }
  162. /**
  163. * 用于测试打印数组数据
  164. **/
  165. function p($arr)
  166. {
  167. header('content-type:text/html;charset=utf-8');
  168. echo '<pre>';
  169. print_r($arr);
  170. echo '</pre>';
  171. }
  172. /**
  173. * 返回JSON统一格式
  174. *
  175. * @param int $code 返回状态
  176. * @param string $msg 返回提示信息
  177. * @param array $data 返回对象
  178. * @return array
  179. */
  180. function V($code, $msg, $data = array())
  181. {
  182. return array('code' => $code, 'msg' => $msg, 'data' => $data);
  183. }
  184. /**
  185. * 格式化表格导入的时间为php时间
  186. **/
  187. function excel_time_conversion($data){
  188. // excel软件中的日期是从 1900-01-01 开始计算的
  189. //但是php 是从 1970-01-01开始计算的。
  190. //这两者间有一个天数差 25569
  191. $d = 25569; // excel和php之间相差的时间
  192. $t = 24 * 60 * 60; // 一天24小时
  193. return gmdate('Y-m-d', ($data - $d) *$t);
  194. }
  195. /**
  196. * @desc 时间生成
  197. * @param $type 1、本日 2、本周 3、本月 4、本年 5、上个月 6、指定月份
  198. * @return array
  199. */
  200. function mk_time($type,$date){
  201. switch($type){
  202. case 1://本日
  203. $start = mktime(0,0,0,date('m'), date('d'), date('Y'));
  204. $end = mktime(23,59,59,date('m'),date('d'),date('Y'));
  205. break;
  206. case 2://本周
  207. $date_w = date('w');
  208. if($date_w == 0) $date_w = 7;
  209. $start = mktime(0,0,0,date('m'),date('d')-$date_w+1,date('Y'));
  210. $end = mktime(23,59,59,date('m'),date('d')-$date_w+7,date('Y'));
  211. break;
  212. case 3://本月
  213. $start = mktime(0,0,0,date('m'),1,date('Y'));
  214. $end = mktime(23,59,59,date('m'),date('t'),date('Y'));
  215. break;
  216. case 4://本年
  217. $start = mktime(0,0,0,1,1,date('Y'));
  218. $end = mktime(0,0,0,12,31,date('Y'));
  219. break;
  220. case 5://上个月
  221. $start = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
  222. $end = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d').'day')));
  223. break;
  224. case 6://指定月份
  225. $y = $date[0];
  226. $m = $date[1];
  227. $d = date('t', strtotime($y.'-'.$m));
  228. $start = strtotime($y.'-'.$m);
  229. $end = mktime(23,59,59,$m,$d,$y);
  230. break;
  231. default:
  232. $t = mk_time(1,[]);
  233. $start = $t['start'];
  234. $end = $t['end'];
  235. break;
  236. }
  237. return array('start' => $start, 'end' => $end);
  238. }
  239. /**
  240. * 生成log文件
  241. * @param $dir
  242. * @param $content
  243. */
  244. function user_log($dir,$content){
  245. $log_dir=RUNTIME_PATH.'/'.$dir;
  246. if(!is_dir($log_dir)){
  247. mkdir($log_dir,0777,true);
  248. }
  249. $filename=date('Y-m-d').'.log';
  250. if(is_array($content)){
  251. $content=json_encode($content,JSON_UNESCAPED_UNICODE);
  252. }
  253. file_put_contents($log_dir.'/'.$filename,date('Y-m-d H:i:s ').$content.PHP_EOL,FILE_APPEND);
  254. }
  255. /**
  256. * 获取订单号
  257. **/
  258. function get_order_sn($user_id)
  259. {
  260. $rand = $user_id < 9999 ? mt_rand(100000, 99999999) : mt_rand(100, 99999);
  261. $order_sn = date('Yhis') . $rand;
  262. $id = str_pad($user_id, (24 - strlen($order_sn)), '0', STR_PAD_BOTH);
  263. return $order_sn . $id;
  264. }
  265. /**
  266. * 一维数组转为二维数组
  267. *
  268. * @param array $arr
  269. * @param string $str1
  270. * @param string $str2
  271. * @return array
  272. */
  273. function get_one_two_array($arr, $str1, $str2)
  274. {
  275. $item = [];
  276. foreach ($arr as $k => $v) {
  277. $item[] = [
  278. $str1 => $k,
  279. $str2 => $v,
  280. ];
  281. }
  282. return $item;
  283. }