企微短剧业务系统

helpers.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. if (!function_exists('get_client_code_version')) {
  3. /**
  4. * 读取客户端代码版本号,用于客户端自动加载新提交的代码
  5. * @return string
  6. */
  7. function get_client_code_version()
  8. {
  9. // 前端版本号文件路径
  10. $versionFile = public_path('version.md');
  11. // 文件存在,且有读权限
  12. if (file_exists($versionFile) && is_readable($versionFile)) {
  13. $version = @file_get_contents($versionFile);
  14. if ($version) {
  15. return trim($version);
  16. }
  17. }
  18. return false;
  19. }
  20. }
  21. if (!function_exists('getInfoFromXml')) {
  22. /**
  23. * 读取xml字符串,获取对应数据
  24. * @return string
  25. */
  26. function getInfoFromXml($xmlStr, $field)
  27. {
  28. $xml_tree = new \DOMDocument();
  29. $xml_tree->loadXML($xmlStr);
  30. $array_e = $xml_tree->getElementsByTagName($field);
  31. $info = $array_e->item(0)->nodeValue;
  32. return $info;
  33. }
  34. }
  35. if(!function_exists('getDaysByMonth')) {
  36. /**
  37. * 根据年月获取所属月份的日期
  38. * @param $year int 年份
  39. * @param $month int 月份
  40. * @return array
  41. * */
  42. function getDaysByMonth($year, $month)
  43. {
  44. $days = date('t', strtotime($year. '-' . $month));
  45. $start_time = strtotime(date($year. '-' . $month. '-01'));
  46. $array = array();
  47. for($i=0; $i<$days;$i++) {
  48. $array[] = date('Y-m-d', $start_time + $i*86400);
  49. }
  50. return $array;
  51. }
  52. }
  53. /*
  54. * 获取时差
  55. * */
  56. if(!function_exists('date_diff_for_hour')) {
  57. /**
  58. * 获取时差
  59. * @param $dateOne datetime
  60. * @param $dateTwo datetime
  61. * @return int
  62. * */
  63. function date_diff_for_hour($dateOne, $dateTwo=null)
  64. {
  65. if(is_null($dateTwo)) $dateTwo = date('Y-m-d H:i:s');
  66. return abs(floor((strtotime($dateOne)-strtotime($dateTwo))%86400 / 3600));
  67. }
  68. }
  69. /*
  70. * 获取近七日日期
  71. * */
  72. if(!function_exists('get_recent_day')) {
  73. /**
  74. * 获取最近日期
  75. * @param $time
  76. * @param $format string
  77. * @return array
  78. * */
  79. function get_recent_day($time = '', $format='Y-m-d'){
  80. $time = empty($time) ? time() : $time;
  81. $date = [];
  82. for ($i=0; $i<=6; $i++){
  83. $timestamp = strtotime( '+' . ($i-7) .' days', $time);
  84. $yearInfo = date('Y', $timestamp);
  85. $date[$yearInfo][] = date($format, $timestamp);
  86. }
  87. return $date;
  88. }
  89. }
  90. /*
  91. * 将近N小时和日期转化为*年*月*日 *:*:*格式
  92. * */
  93. if(!function_exists('to_datetime')) {
  94. function to_datetime($param) {
  95. if(empty($param))
  96. return null;
  97. if(strtotime($param) === false) {
  98. $result = date('Y-m-d H:i:s', strtotime('-' . (int)$param. ' hour'));
  99. } else {
  100. $result = date('Y-m-d H:i:s', strtotime($param));
  101. }
  102. return $result;
  103. }
  104. }
  105. /*
  106. * 获取字符串对应的hash值
  107. * */
  108. if(!function_exists('get_hash_code')) {
  109. function get_hash_code($str, $remainder=0)
  110. {
  111. if(empty($str)) return false;
  112. $encrypt = md5($str);
  113. $number = base_convert($encrypt, 16, 10);
  114. $label = substr($number, -4);
  115. return $remainder > 0 ? abs($label % $remainder) : $number;
  116. }
  117. }
  118. /*
  119. * 获取日期对应的数字(0:星期日 1:星期一 2:星期二以此类推)
  120. * */
  121. if(!function_exists('get_week')) {
  122. function get_week($timestamp){
  123. $date_str=date('Y-m-d',$timestamp);
  124. $arr=explode("-", $date_str);
  125. $year=$arr[0];
  126. //月,输出2位整型,不够2位右对齐
  127. $month=sprintf('%02d',$arr[1]);
  128. //日,输出2位整型,不够2位右对齐
  129. $day=sprintf('%02d',$arr[2]);
  130. //时分秒默认赋值为0;
  131. $hour = $minute = $second = 0;
  132. //转换成时间戳
  133. $strap = mktime($hour,$minute,$second,$month,$day,$year);
  134. //获取数字型星期几
  135. return date("w",$strap);
  136. }
  137. }
  138. /*
  139. * 获取字符串对应的hash值
  140. * */
  141. if(!function_exists('get_token')) {
  142. function get_token($corpid, $userId, $timestamps, $extra='')
  143. {
  144. $str = 'wAAVfvfgR8uMIGIoRJquy66uw';
  145. return md5($corpid . $userId . $str . $timestamps . $extra);
  146. }
  147. }
  148. if(!function_exists('get_random_str')) {
  149. function get_random_str($length)
  150. {
  151. $nonceStr = "";
  152. $codeAlphabet = "ABCDEFGHIJKLMnopQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789";
  153. for ($i = 0; $i < $length; $i++) {
  154. $nonceStr .= $codeAlphabet[mt_rand(0, strlen($codeAlphabet) - 1)];
  155. }
  156. return $nonceStr;
  157. }
  158. }
  159. function h5_set_sign($params)
  160. {
  161. if(empty($params)){
  162. return false;
  163. }
  164. $str = '';
  165. ksort($params);
  166. foreach($params as $k=>$v){
  167. if(is_array($v)){
  168. $v = json_encode($v);
  169. }
  170. $str .= $k.'='.$v;
  171. }
  172. $solt = 'ur904wtr5pfctaw9';
  173. $sign = md5(md5($str) . $solt);
  174. return $sign;
  175. }
  176. function h5_verify_sign($params)
  177. {
  178. if( empty($params) || !isset($params['sign']) ){
  179. return false;
  180. }
  181. $sign = $params['sign'];
  182. unset($params['sign']);
  183. $_sign = h5_set_sign($params);
  184. if($sign == $_sign){
  185. return true;
  186. }
  187. return false;
  188. }
  189. /*
  190. * 将分钟转换为 x天y小时z分钟
  191. * */
  192. if(!function_exists('turn_minute')) {
  193. function turn_minute($minute)
  194. {
  195. $days = floor($minute/1440);
  196. $hours = floor(($minute - $days*1440)/60);
  197. $minutes = ($minute - $days*60*24 - $hours*60);
  198. $days = $days > 0 ? $days . '天' : '';
  199. $hours = $hours > 0 ? $hours . '小时' : '';
  200. $minutes = $minutes > 0 ? $minutes . '分钟' : '';
  201. return $days . $hours . $minutes;
  202. }
  203. }
  204. /**
  205. * 获取url参数
  206. * */
  207. if(!function_exists('getUrlParams')) {
  208. function getUrlParams($url, $param)
  209. {
  210. $pathData = parse_url($url);
  211. if(isset($pathData['fragment']) && $pathData['fragment']) {
  212. $pathData = parse_url($pathData['fragment']);
  213. }
  214. $value = '';
  215. if(isset($pathData['query']) && $pathData['query']) {
  216. parse_str($pathData['query'], $query);
  217. $value = $query[$param] ?? '';
  218. }
  219. return $value;
  220. }
  221. }
  222. /**
  223. * 获取url参数
  224. * */
  225. if(!function_exists('shouldMiss')) {
  226. /**
  227. * @param $currentValue integer 当前订单数
  228. * @param $missSetValue integer 间隔的订单数
  229. * @param $missValue integer 需要漏掉的订单数
  230. * @return boolean 应该漏单返回true,否则false
  231. **/
  232. function shouldMiss($currentValue, $missSetValue, $missValue)
  233. {
  234. for($i=0; $i<$missValue; $i++) {
  235. $remainder = ($missSetValue + $missValue - $i) % ($missSetValue + $missValue);
  236. if( $currentValue >1 && ($currentValue % ($missSetValue + $missValue)) == $remainder) {
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. }
  243. /**
  244. * 获取毫秒级时间戳
  245. */
  246. if(!function_exists('getMillisecond')) {
  247. function getMillisecond()
  248. {
  249. list($microsecond, $time) = explode(' ', microtime()); //' '中间是一个空格
  250. return (float)sprintf('%.0f', (floatval($microsecond) + floatval($time)) * 1000);
  251. }
  252. }