common.func.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * 通用函数库
  4. */
  5. use Hashids\Hashids;
  6. /**
  7. * json响应成功
  8. * @param mixed $data 数据
  9. * @param string $message 消息
  10. * @return mixed
  11. */
  12. function json_success($data, $message = '操作成功')
  13. {
  14. $handshake = '';
  15. $data = [
  16. 'code' => 200,
  17. 'message' => $message,
  18. 'data' => $data,
  19. 'handshake' => $handshake,
  20. ];
  21. return response()->json($data);
  22. }
  23. /**
  24. * json响应失败
  25. * @param string $message 消息
  26. * @param int $code 代码
  27. * @return \Illuminate\Http\JsonResponse
  28. */
  29. function json_fail($message, $code = 500, $data = [])
  30. {
  31. $handshake = '';
  32. $data = [
  33. 'code' => $code,
  34. 'message' => $message,
  35. 'data' => $data,
  36. 'handshake' => $handshake,
  37. ];
  38. return response()->json($data);
  39. }
  40. /**
  41. * 生成密码.
  42. * @param string $plain 明文密码.
  43. * @param string $salt 盐.
  44. * @return string
  45. */
  46. function generatePassword($plain, $salt)
  47. {
  48. return md5(md5($plain) . $salt);
  49. }
  50. /**
  51. * 随机生成字符串
  52. * @param int $length 长度
  53. * @param int $numeric 纯数字
  54. * @return string
  55. */
  56. function random($length, $numeric = 0) : string
  57. {
  58. if (version_compare(PHP_VERSION, '4.2.0', '<')) {
  59. mt_srand((double)microtime() * 1000000);
  60. } else {
  61. mt_srand();
  62. }
  63. $seed = base_convert(md5(print_r($_SERVER, 1) . microtime()), 16, $numeric ? 10 : 35);
  64. $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  65. $hash = '';
  66. $max = strlen($seed) - 1;
  67. for ($i = 0; $i < $length; $i++) {
  68. $hash .= $seed[mt_rand(0, $max)];
  69. }
  70. return $hash;
  71. }
  72. /**
  73. * 格式化页码入参
  74. * @param $page
  75. * @return int
  76. */
  77. function format_page($page)
  78. {
  79. $page = intval($page);
  80. if ($page <= 0) {
  81. $page = 1;
  82. }
  83. if ($page > PHP_INT_MAX) {
  84. $page = PHP_INT_MAX;
  85. }
  86. return $page;
  87. }
  88. /**
  89. * 加密ID.
  90. * @param $id
  91. * @param int $length 长度
  92. * @return string
  93. */
  94. function hashEncodeId($id, $length = 6)
  95. {
  96. $hashids = new Hashids(config('hashids.salt', ''), 6);
  97. return $hashids->encode($id);
  98. }
  99. /**
  100. * 解密ID.
  101. * @param $str
  102. * @return string
  103. */
  104. function hashDecodeId($str)
  105. {
  106. $hashids = new Hashids(config('hashids.salt', ''), 6);
  107. $arr = $hashids->decode($str);
  108. return implode('', $arr);
  109. }
  110. /**
  111. * 生成订单流水号、可指定前缀
  112. * @param string $prefix
  113. * @return string
  114. */
  115. function createOrderSn($prefix = '', $mark = '')
  116. {
  117. if (strlen($mark) <= 2 && !empty($mark)) {
  118. $prefix .= '0' . $mark;
  119. } else {
  120. $prefix .= $mark;
  121. }
  122. return $prefix . date('YmdHis') . mt_rand(1111, 9999);
  123. }
  124. /**
  125. * 检测支付链接协议
  126. *
  127. * @param $payUrl
  128. * @return mixed|string
  129. */
  130. function checkPayUrl($payUrl) {
  131. if (strpos($payUrl, 'alipays://') !== false) {
  132. $payUrl = 'https://r.tool.py163.xyz/dk.html?_r_=' . urlencode($payUrl);
  133. }
  134. return $payUrl;
  135. }
  136. /**
  137. * 生成key
  138. *
  139. * @return string
  140. */
  141. function createKey()
  142. {
  143. $pwd = '';
  144. for ($i = 0; $i < 10; $i++) {
  145. $pwd .= chr(mt_rand(33, 126));
  146. }
  147. return md5($pwd . time());
  148. }
  149. /**
  150. * 签名
  151. *
  152. * @param $data
  153. * @param $publicKey
  154. * @return string
  155. */
  156. function build_sign($data, $publicKey)
  157. {
  158. ksort($data);
  159. $md5str = "";
  160. foreach ($data as $key => $val) {
  161. $md5str = $md5str . $key . "=" . $val . "&";
  162. }
  163. $md5str = strtoupper(md5($md5str . 'key=' . $publicKey));
  164. return $md5str;
  165. }