functions.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. use think\Db;
  3. use think\helper\Time;
  4. /**
  5. * @name post请求
  6. * @param $url
  7. * @param $data
  8. * @return bool|string
  9. */
  10. function curl_post($url, $data)
  11. {
  12. $ch = curl_init();
  13. curl_setopt($ch, CURLOPT_URL, $url);
  14. curl_setopt($ch, CURLOPT_POST, 1);
  15. curl_setopt($ch, CURLOPT_HEADER, 0);
  16. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  17. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  18. $return = curl_exec($ch);
  19. curl_close($ch);
  20. return $return;
  21. }
  22. /**
  23. * @name get请求
  24. * @param $url
  25. * @return mixed
  26. */
  27. function curl_get($url)
  28. {
  29. $ch = curl_init();
  30. // 设置选项,包括URL
  31. curl_setopt($ch, CURLOPT_URL, $url);
  32. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  34. curl_setopt($ch, CURLOPT_HEADER, 0);
  35. if (stripos($url, "https://") !== FALSE) {
  36. curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
  37. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  38. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  39. } else {
  40. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
  41. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);//严格校验
  42. }
  43. // 执行并获取HTML文档内容
  44. $output = curl_exec($ch);
  45. // 释放curl句柄
  46. $err_code = curl_errno($ch);
  47. curl_close($ch);
  48. return json_decode($output);
  49. }
  50. if(!function_exists('public_path')){
  51. function public_path($path){
  52. return CMF_ROOT."public/upload/".$path;
  53. }
  54. }
  55. /*************** 校验身份证 start *******************/
  56. if(!function_exists('checkIdCard')){
  57. function checkIdCard($idc){
  58. if(empty($idc)){
  59. return false;
  60. }
  61. $idcard = $idc;
  62. $City = array(11=>"北京",12=>"天津",13=>"河北",14=>"山西",15=>"内蒙古",21=>"辽宁",22=>"吉林",23=>"黑龙江",31=>"上海",32=>"江苏",33=>"浙江",34=>"安徽",35=>"福建",36=>"江西",37=>"山东",41=>"河南",42=>"湖北",43=>"湖南",44=>"广东",45=>"广西",46=>"海南",50=>"重庆",51=>"四川",52=>"贵州",53=>"云南",54=>"西藏",61=>"陕西",62=>"甘肃",63=>"青海",64=>"宁夏",65=>"新疆",71=>"台湾",81=>"香港",82=>"澳门",91=>"国外");
  63. $iSum = 0;
  64. $idCardLength = strlen($idcard);
  65. //长度验证
  66. if(!preg_match('/^\d{17}(\d|x)$/i',$idcard) and !preg_match('/^\d{15}$/i',$idcard))
  67. {
  68. return false;
  69. }
  70. //地区验证
  71. if(!array_key_exists(intval(substr($idcard,0,2)),$City))
  72. {
  73. return false;
  74. }
  75. // 15位身份证验证生日,转换为18位
  76. if ($idCardLength == 15)
  77. {
  78. $sBirthday = '19'.substr($idcard,6,2).'-'.substr($idcard,8,2).'-'.substr($idcard,10,2);
  79. // $d = new DateTime($sBirthday);
  80. // $dd = $d->format('Y-m-d');
  81. // if($sBirthday != $dd)
  82. if($sBirthday != $sBirthday)
  83. {
  84. return false;
  85. }
  86. $idcard = substr($idcard,0,6)."19".substr($idcard,6,9);//15to18
  87. $Bit18 = getVerifyBit($idcard);//算出第18位校验码
  88. $idcard = $idcard.$Bit18;
  89. }
  90. // 判断是否大于2078年,小于1900年
  91. $year = substr($idcard,6,4);
  92. if ($year<1900 || $year>2078 )
  93. {
  94. return false;
  95. }
  96. //18位身份证处理
  97. $sBirthday = substr($idcard,6,4).'-'.substr($idcard,10,2).'-'.substr($idcard,12,2);
  98. // var_dump($sBirthday);
  99. // $d = new DateTime($sBirthday);
  100. // $dd = $d->format('Y-m-d');
  101. // echo $dd;
  102. // die();
  103. // if($sBirthday != $dd)
  104. if($sBirthday != $sBirthday)
  105. {
  106. return false;
  107. }
  108. //身份证编码规范验证
  109. $idcard_base = substr($idcard,0,17);
  110. if(strtoupper(substr($idcard,17,1)) != getVerifyBit($idcard_base))
  111. {
  112. return false;
  113. }
  114. return true;
  115. }
  116. }
  117. // 计算身份证校验码,根据国家标准GB 11643-1999
  118. if(!function_exists('getVerifyBit')){
  119. function getVerifyBit($idcard_base)
  120. {
  121. if(strlen($idcard_base) != 17)
  122. {
  123. return false;
  124. }
  125. //加权因子
  126. $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  127. //校验码对应值
  128. $verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  129. $checksum = 0;
  130. for ($i = 0; $i < strlen($idcard_base); $i++)
  131. {
  132. $checksum += substr($idcard_base, $i, 1) * $factor[$i];
  133. }
  134. $mod = $checksum % 11;
  135. $verify_number = $verify_number_list[$mod];
  136. return $verify_number;
  137. }
  138. }
  139. function getTime($nian=0,$yue=0){
  140. if(empty($nian) || empty($yue)){
  141. $now = time();
  142. $nian = date("Y",$now);
  143. $yue = date("m",$now);
  144. }
  145. $time['begin'] = mktime(0,0,0,$yue,1,$nian);
  146. $time['end'] = mktime(23,59,59,($yue+1),0,$nian);
  147. return $time;
  148. }
  149. /**
  150. * 发送站内信息
  151. */
  152. function siteMessage($content='',$siteurl='',$user_id=0,$type=1,$distributor_id=0,$membership_id=0){
  153. if(empty($content))
  154. return;
  155. if(!isset($user_id)||empty($user_id)){
  156. return;
  157. }
  158. $data['user_id']=$user_id;
  159. $data['add_time']=time();
  160. $data['content']=$content;
  161. $data['siteurl']=$siteurl;
  162. $data['type']=$type;
  163. $data['distributor_id']=$distributor_id;
  164. $data['membership_id']=$membership_id;
  165. Db::name('user_message')->insert($data);
  166. return true;
  167. //}
  168. }
  169. /**
  170. * 获取七天记录
  171. */
  172. function week7(){
  173. $week=array();
  174. for($i=0;$i<7;$i++){
  175. array_push($week,Time::daysAfter(-$i));
  176. }
  177. return $week;
  178. }
  179. /**
  180. * 获取七天记录
  181. */
  182. function week7_day(){
  183. $week=array();
  184. for($i=1;$i<7;$i++){
  185. array_push($week,date('Y-m-d',Time::daysAfter(-$i)));
  186. }
  187. return $week;
  188. }
  189. function get_date1($start,$end){
  190. $start_time=strtotime($start);
  191. $end_time=strtotime($end);
  192. $date=array();
  193. $days=($end_time - $start_time) / 86400;
  194. // $weekArr=array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');
  195. for($i=0;$i<=$days;$i++){
  196. // $num_week=date("w",$start+($i*86400));
  197. // echo date('Y-m-d',$start+($i*86400)).'-----'.$weekArr[$num_week].'</br>';
  198. array_push($date,date('Y-m-d',$start_time+($i*86400)));
  199. }
  200. return $date;
  201. }
  202. /***
  203. * @param $fields 驼峰对象
  204. * @return array
  205. */
  206. function CamelToUnderLineArr($fields)
  207. {
  208. $newArr = [];
  209. if (!is_array($fields) || !array_values($fields)) return $newArr;
  210. foreach ($fields as $key => $v) {
  211. $keyTmp = strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_', $key));
  212. $newArr[$keyTmp] = $v;
  213. unset($fields->$key);
  214. }
  215. return $newArr;
  216. }
  217. /***
  218. * @param $fields 下划线数组
  219. * @return \stdClass
  220. */
  221. function underLineArrTOCamel($fields)
  222. {
  223. $newObj = new \stdClass();
  224. if(!is_array($fields) || !$fields) return null;
  225. foreach ($fields as $key => $v) {
  226. $keyTmp = array_reduce(explode('_',$key), function($v1, $v2) {
  227. return ucfirst($v1).ucfirst($v2);
  228. });
  229. $keyTmp = lcfirst($keyTmp);
  230. $newObj->$keyTmp = $v;
  231. unset($fields[$key]);
  232. }
  233. return $newObj;
  234. }
  235. /*
  236. * 下划线转驼峰
  237. */
  238. function convertUnderline($str)
  239. {
  240. $str = preg_replace_callback('/([-_]+([a-z]{1}))/i',function($matches){
  241. return strtoupper($matches[2]);
  242. },$str);
  243. return $str;
  244. }
  245. /*
  246. * 驼峰转下划线
  247. */
  248. function humpToLine($str){
  249. $str = preg_replace_callback('/([A-Z]{1})/',function($matches){
  250. return '_'.strtolower($matches[0]);
  251. },$str);
  252. return $str;
  253. }
  254. function xoc($xoc,&$res=0,$y=1){
  255. $a = str_split($xoc, 2);
  256. $num=count($a);
  257. //echo $y;
  258. $b = $a[$y];
  259. if($res==0){
  260. $i =hexdec($a[0]) ;
  261. }else{
  262. $i=$res;
  263. }
  264. //var_dump($i .'^'.hexdec($b));
  265. $c = $i ^ hexdec($b);
  266. if($y<$num-1){
  267. return xoc($xoc,$c,$y+1);
  268. }else{
  269. return dechex($c);
  270. }
  271. }
  272. /**
  273. **字符串转16进制
  274. **/
  275. function String2Hex($string){
  276. $hex='';
  277. for ($i=0; $i < strlen($string); $i++){
  278. $hex .= dechex(ord($string[$i]));
  279. }
  280. return $hex;
  281. }
  282. /**
  283. **16进制转字符串
  284. **/
  285. function Hex2String($hex){
  286. $string='';
  287. for ($i=0; $i < strlen($hex)-1; $i+=2){
  288. $string .= chr(hexdec($hex[$i].$hex[$i+1]));
  289. }
  290. return $string;
  291. }
  292. #@param check_code|校验码 为空是计算校验码,不为空为验证校验码
  293. function bcc($msg, $check_code = '') {
  294. //按两位字符切割字符串
  295. $check_str_array = str_split($msg, 2);
  296. $str_len = count($check_str_array);
  297. $xor = hexdec($check_str_array[0]);
  298. for ($i = 1; $i < $str_len; $i++) {
  299. $xor ^= hexdec($check_str_array[$i]);
  300. }
  301. $xor = dechex($xor);
  302. $xor = str_pad($xor, 2, 0, STR_PAD_LEFT); #不足两位前面填充0
  303. if (!$check_code) {
  304. return $xor;
  305. }
  306. return $xor == $check_code;
  307. }
  308. /**
  309. * @name 企业微信
  310. * @param $config
  311. */
  312. function work(array $config){
  313. return \EasyWeChat\Factory::work($config);
  314. }