common.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * 获取订单号
  174. **/
  175. function get_order_sn($user_id)
  176. {
  177. $rand = $user_id < 9999 ? mt_rand(100000, 99999999) : mt_rand(100, 99999);
  178. $order_sn = date('Yhis') . $rand;
  179. $id = str_pad($user_id, (24 - strlen($order_sn)), '0', STR_PAD_BOTH);
  180. return $order_sn . $id;
  181. }