common.php 10 KB

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