common.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. //获取全图片地址 $image_data
  23. function image_path($image_data){
  24. if(empty($image_data)){
  25. return $image_data;
  26. }
  27. if (strpos($image_data,'|')!==false){
  28. $image_res = explode('|',$image_data);
  29. }elseif(strpos($image_data,',')!==false){
  30. $image_res = explode(',',$image_data);
  31. }else{
  32. $image_res = array($image_data);
  33. }
  34. return $image_res;
  35. }
  36. /**
  37. * @param $id_card
  38. * @return false|string
  39. */
  40. function get_age($id_card){
  41. # 1.从身份证中获取出生日期
  42. $birth_Date = strtotime(substr($id_card, 6, 8));//截取日期并转为时间戳
  43. # 2.格式化[出生日期]
  44. $Year = date('Y', $birth_Date);//yyyy
  45. $Month = date('m', $birth_Date);//mm
  46. $Day = date('d', $birth_Date);//dd
  47. # 3.格式化[当前日期]
  48. $current_Y = date('Y');//yyyy
  49. $current_M = date('m');//mm
  50. $current_D = date('d');//dd
  51. # 4.计算年龄()
  52. $age = $current_Y - $Year;//今年减去生日年
  53. if($Month > $current_M || $Month == $current_M && $Day > $current_D){//深层判断(日)
  54. $age--;//如果出生月大于当前月或出生月等于当前月但出生日大于当前日则减一岁
  55. }
  56. # 返回
  57. return $age;
  58. }
  59. function create_invite_code($user_id){
  60. // 生成12位邀请码
  61. $code_str = '';
  62. $base_code = explode(',',"A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z");
  63. $code_key = array_rand($base_code, 10 - strlen($user_id));
  64. array_map(function ($val)use (&$code_str,$base_code){
  65. $code_str .=$base_code[$val] ;
  66. },$code_key);
  67. return $code_str.$user_id;
  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. function http_curl($url,$type='get',$res='json',$arr=''){
  80. $headers = array();
  81. //根据API的要求,定义相对应的Content-Type
  82. array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8;application/json");
  83. $curl = curl_init();
  84. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $type);
  85. curl_setopt($curl, CURLOPT_URL, $url);
  86. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  87. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  88. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  89. curl_setopt($curl, CURLOPT_HEADER, false);
  90. $output = curl_exec($curl);
  91. curl_close($curl);
  92. if($res=='json'){
  93. if($output === false){
  94. //请求失败,返回错误信息
  95. return curl_error($curl);
  96. }else{
  97. //请求成功,返回信息
  98. return json_decode($output,true);
  99. }
  100. }
  101. }
  102. function curl_get($url)
  103. {
  104. $ch = curl_init();
  105. curl_setopt($ch, CURLOPT_URL, $url);
  106. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  107. $output = curl_exec($ch);
  108. curl_close($ch);
  109. if($output === false){
  110. return curl_error($ch);
  111. }else{
  112. return json_decode($output,true);
  113. }
  114. }
  115. // 获取物流信息
  116. function get_delivery($send_no = 'JD0053309649641',$express_code=''){
  117. error_reporting(E_ALL || ~E_NOTICE);
  118. $AppCode ='5f96216347b547579e2417685ee8e647';//开通服务后 买家中心-查看AppCode
  119. $host = "https://wuliu.market.alicloudapi.com";//api访问链接
  120. $path = "/kdi";//API访问后缀
  121. $method = "GET";
  122. $body ='';
  123. $headers = array();
  124. array_push($headers, "Authorization:APPCODE " . $AppCode);
  125. $querys = "no={$send_no}&type={$express_code}"; //参数写在这里
  126. $url = $host . $path . "?" . $querys;
  127. $curl = curl_init();
  128. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  129. curl_setopt($curl, CURLOPT_URL, $url);
  130. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  131. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  132. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  133. curl_setopt($curl, CURLOPT_HEADER, true);
  134. if (1 == strpos("$" . $host, "https://")) {
  135. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  136. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  137. }
  138. $out_put = curl_exec($curl);
  139. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  140. list($header, $body) = explode("\r\n\r\n", $out_put, 2);
  141. if ($httpCode == 200) {
  142. return json_decode($body,true)['result'];
  143. } else {
  144. return [];
  145. }
  146. }
  147. // 实名认证
  148. function user_certification($id_card,$name){
  149. $host = "http://checkone.market.alicloudapi.com";
  150. $path = "/chinadatapay/1882";
  151. $method = "POST";
  152. $appcode = "30be8bdcc65842919980a8276ffc4995";
  153. $headers = array();
  154. array_push($headers, "Authorization:APPCODE " . $appcode);
  155. //根据API的要求,定义相对应的Content-Type
  156. array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
  157. $bodys = "idcard=".$id_card."&name=".$name;
  158. $url = $host . $path;
  159. $curl = curl_init();
  160. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  161. curl_setopt($curl, CURLOPT_URL, $url);
  162. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  163. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  164. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  165. curl_setopt($curl, CURLOPT_HEADER, false);
  166. if (1 == strpos("$".$host, "https://"))
  167. {
  168. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  169. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  170. }
  171. curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
  172. $res = curl_exec($curl);
  173. if($res){
  174. $res = json_decode($res,true);
  175. if($res['data']['result'] == 1){
  176. return 1;
  177. }else{
  178. return 0;
  179. }
  180. }else{
  181. return 0;
  182. }
  183. }
  184. /**
  185.  * 把返回的数据集转换成Tree
  186.  * @param array $list 要转换的数据集
  187.  * @param string $pk 自增字段(栏目id)
  188.  * @param string $pid parent标记字段
  189.  * @return array
  190.  */
  191. function make_tree($list,$pk='id',$pid='pid',$child='children',$root=0){
  192. if(is_object($list)) $list = $list->toArray();
  193. $tree=array();
  194. $packData=array();
  195. foreach ($list as $data) {
  196. $packData[$data[$pk]] = $data;
  197. }
  198. foreach ($packData as $key =>$val){
  199. if($val[$pid]==$root){//代表跟节点
  200. $tree[]=& $packData[$key];
  201. }else{
  202. $packData[$val[$pid]][$child][]=& $packData[$key]; //找到其父类
  203. }
  204. }
  205. return $tree;
  206. }
  207. //判断字段存在并不为空
  208. function isset_full($arr, $key)
  209. {
  210. if (isset($arr[$key]) && !empty($arr[$key])) {
  211. return true;
  212. } else {
  213. return false;
  214. }
  215. }
  216. //判断字段存在并不为空 并且等于验证值
  217. function isset_full_check($arr, $key,$check_val)
  218. {
  219. if (isset($arr[$key]) && !empty($arr[$key]) && $arr[$key] == $check_val) {
  220. return true;
  221. } else {
  222. return false;
  223. }
  224. }
  225. function all_pay_type()
  226. {
  227. return [ '--','微信[H5]','支付宝[H5]','微信[APP]','支付宝[APP]','微信[公众号]' ];
  228. }
  229. /**
  230. * @param $start 验证开始时间
  231. * @param $end 验证结束时间
  232. * @param $check_st 已有活动开始时间
  233. * @param $check_end 已有活动结束时间
  234. * @return bool true 可以创建活动
  235. */
  236. function check_act_time($start,$end,$check_st,$check_end)
  237. {
  238. $check_val = false;
  239. if($start > $check_end || $end < $check_st) $check_val = true;
  240. return $check_val;
  241. }
  242. /**
  243. * 生成32位随机数
  244. */
  245. function get32Str($length='32'){
  246. $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  247. $len = strlen($str)-1;
  248. $randstr = '';
  249. for ($i=0;$i<$length;$i++) {
  250. $num=mt_rand(0,$len);
  251. $randstr .= $str[$num];
  252. }
  253. return $randstr;
  254. }
  255. function hx_register($username){
  256. $data = array(
  257. 'grant_type' => 'client_credentials',
  258. 'client_id' => 'YXA6NS5H0GDGEe20Q9VWb7Fpew',
  259. 'client_secret' => 'YXA6zkYKRQMJbxi_6cN1ERHuFn9QUpI'
  260. );
  261. $token_res = requestPost('https://a1-vip5.easemob.com/1414221110068467/kefuchannelapp104968/token',json_encode($data));
  262. $token_res = json_decode($token_res,true);
  263. $param = array(
  264. 'username' => 'act'.$username.rand(0000,9999),
  265. 'password' => 'act'.$username.rand(00000,99999)
  266. );
  267. $hx_account = http_post_json('https://a1-vip5.easemob.com/1414221110068467/kefuchannelapp104968/users',json_encode($param),$token_res['access_token']);
  268. if(!isset($hx_account['entities'][0]['uuid'])){
  269. return false;
  270. }
  271. Db::name('store_member')->where('id',$username)->update(array('hx_username'=>$param['username'],'hx_password'=>$param['password'],'hx_uuid'=>$hx_account['entities'][0]['uuid']));
  272. return true;
  273. }
  274. function requestPost($url , $post_data = array(),$headers=[] ){
  275. // 1. 初始化一个cURL会话
  276. //根据API的要求,定义相对应的Content-Type
  277. array_push($headers, "Content-Type".":"."application/json");
  278. $ch = curl_init();
  279. // 2. 设置请求选项, 包括具体的url
  280. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  281. curl_setopt($ch, CURLOPT_URL, $url);
  282. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  283. // 设置请求为post类型
  284. curl_setopt($ch, CURLOPT_POST, 1);
  285. // 添加post数据到请求中
  286. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  287. curl_setopt($ch, CURLOPT_HEADER, 0);
  288. // 3. 执行一个cURL会话并且获取相关回复
  289. $response = curl_exec($ch);
  290. // 4. 释放cURL句柄,关闭一个cURL会话
  291. curl_close($ch);
  292. return $response;
  293. }
  294. function http_post_json($url, $jsonStr,$token_res)
  295. {
  296. $headers = ['Authorization:Bearer '.$token_res,'Content-Type: application/json; charset=utf-8',
  297. 'Content-Length: ' . strlen($jsonStr)];
  298. $ch = curl_init();
  299. curl_setopt($ch, CURLOPT_POST, 1);
  300. curl_setopt($ch, CURLOPT_URL, $url);
  301. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
  302. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  303. curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
  304. $response = curl_exec($ch);
  305. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  306. curl_close($ch);
  307. return json_decode($response,true);
  308. }
  309. function pdfCurl($file_name)
  310. {
  311. $ch = curl_init();
  312. curl_setopt($ch, CURLOPT_URL, 'https://pdf.gyxqcdz.com/v1/pdf/upload');
  313. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  314. curl_setopt($ch, CURLOPT_POST, 1);
  315. // curl_setopt($ch, CURLOPT_POSTFIELDS, 'file=@'.$file_name);
  316. curl_setopt($ch, CURLOPT_POSTFIELDS, ['file'=>new \CURLFile(realpath($file_name))]);
  317. $headers = array();
  318. // $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  319. // $headers[] = 'Content-Type: multipart/form-data';
  320. // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  321. $result = curl_exec($ch);
  322. if (curl_errno($ch)) {
  323. echo 'Error:' . curl_error($ch);
  324. }
  325. curl_close($ch);
  326. return json_decode($result,true);
  327. }
  328. function curl_post($url,$post_data)
  329. {
  330. // 1. 初始化一个cURL会话
  331. //根据API的要求,定义相对应的Content-Type
  332. $headers = [];
  333. array_push($headers, "Content-Type".":"."application/json");
  334. $ch = curl_init();
  335. // 2. 设置请求选项, 包括具体的url
  336. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  337. curl_setopt($ch, CURLOPT_URL, $url);
  338. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  339. // 设置请求为post类型
  340. curl_setopt($ch, CURLOPT_POST, 1);
  341. // 添加post数据到请求中
  342. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  343. curl_setopt($ch, CURLOPT_HEADER, 0);
  344. // 3. 执行一个cURL会话并且获取相关回复
  345. $response = curl_exec($ch);
  346. // 4. 释放cURL句柄,关闭一个cURL会话
  347. curl_close($ch);
  348. return json_decode($response,true);
  349. }
  350. function getVideoTime($ali_id)
  351. {
  352. $res = (new \app\api\controller\VideoDemand())->getVideoInfo($ali_id);
  353. if(empty($res['videoBase'])) return ['duration'=>0 ,'duration_str'=>''];
  354. return ['duration'=>intval($res['videoBase']['duration']) ,'duration_str'=>get_stay_time(intval($res['videoBase']['duration']))];
  355. }
  356. function dispose_recommend($list,$needle= 'is_recommend')
  357. {
  358. list($l1,$l2) = [[],[]];
  359. foreach ($list as $v) $v[$needle] == 1 ? $l1[] = $v : $l2[] =$v;
  360. shuffle($l1);
  361. shuffle($l2);
  362. return array_merge($l1,$l2);
  363. }
  364. // 生成二维码
  365. function create_qrcode($id,$module = 'activity')
  366. {
  367. $qrCode= new \Endroid\QrCode\QrCode();
  368. $qrCode->setText($id);
  369. $qrCode->setSize(200);
  370. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/'.$module;
  371. $filename = $dir.'/'.$id.'.png';
  372. $qrCode->writeFile($filename);
  373. $url = 'https://'.$_SERVER['SERVER_NAME']."/$module/".$id.'.png';
  374. return $url;
  375. }