common.php 13 KB

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