webman
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

functions.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. //token,填写用户信息
  3. if (!function_exists('setToken')) {
  4. function setToken($array)
  5. {
  6. try {
  7. $encode_token = \app\common\model\Jwt::encode($array);
  8. $redis = new \app\common\model\Redis100_model();
  9. $redis->hSet('oralcavity_token', $array['id'], $encode_token);
  10. $redis->close();
  11. return $encode_token;
  12. } catch (\Exception $e) {
  13. tojson('10000', $e->getMessage());
  14. }
  15. }
  16. }
  17. if (!function_exists('has_special_str')) {
  18. function has_special_str($str)
  19. {
  20. //$前要加反斜杠
  21. $specialChars = "~·`!!`@@&$##\$¥%^…&*()()—-_==×++[]{}*{}【】、|\\;:;:'\"“”‘’,./<>《》??~,。…";
  22. //特殊符号数组
  23. $specialArr = array();
  24. $len = mb_strlen($specialChars, 'UTF-8');
  25. for ($i = 0; $i < $len; $i++) {
  26. $specialArr[] = mb_substr($specialChars, $i, 1, 'UTF-8');
  27. }
  28. //待比较字符串数组
  29. $arr = array();
  30. $len = mb_strlen($str, 'UTF-8');
  31. for ($i = 0; $i < $len; $i++) {
  32. $arr[] = mb_substr($str, $i, 1, 'UTF-8');
  33. }
  34. foreach ($arr as $v) {
  35. if (in_array($v, $specialArr)) {
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. }
  42. if (!function_exists('uuid')) {
  43. function uuid()
  44. {
  45. $uuid = sprintf(
  46. '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  47. mt_rand(0, 0xffff),
  48. mt_rand(0, 0xffff),
  49. mt_rand(0, 0xffff),
  50. mt_rand(0, 0x0fff) | 0x4000,
  51. mt_rand(0, 0x3fff) | 0x8000,
  52. mt_rand(0, 0xffff),
  53. mt_rand(0, 0xffff),
  54. mt_rand(0, 0xffff)
  55. );
  56. return $uuid;
  57. }
  58. }
  59. // 应用公共文件
  60. if (!function_exists('tojson')) {
  61. function tojson($code = 200, $msg = '请求成功', $arr = null)
  62. {
  63. $array = [
  64. 'code' => (int)$code,
  65. 'msg' => $msg,
  66. // 'data' => $arr,
  67. ];
  68. if ($arr) {
  69. $array = array_merge($array, $arr);
  70. }
  71. return json($array, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  72. }
  73. }
  74. //token,获取用户信息
  75. if (!function_exists('getToken')) {
  76. function getToken($token)
  77. {
  78. try {
  79. $data = \app\common\model\Jwt::decode($token);
  80. $redis = new support\Redis();
  81. $res = $redis->hGet('oralcavity_token', $data['id']);
  82. if ($res != $token) {
  83. return tojson(201, 'token不存在,请重试登录');
  84. }
  85. if (!empty($data)) {
  86. return $data;
  87. } else {
  88. return tojson(201, 'token不存在');
  89. }
  90. } catch (\Exception $e) {
  91. return tojson(201, 'token不存在');
  92. }
  93. }
  94. }
  95. if (!function_exists('nosecure')) {
  96. function nosecure($method, $nosecure, $function, $param)
  97. {
  98. return \app\common\model\Secure::secure($method, $nosecure, $function, $param);
  99. }
  100. }
  101. if (!function_exists('host')) {
  102. function host()//???
  103. {
  104. // $host = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_ADDR'] . ':' . $_SERVER['SERVER_PORT'];
  105. $host = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'];
  106. return $host;
  107. }
  108. }
  109. //验证器以及验签
  110. if (!function_exists('verifysign')) {
  111. function verifysign($param, $noSize, $request)
  112. {
  113. if (!isset($param['sign'])) {
  114. if (!nosign($noSize, $request)) {
  115. return tojson(10005, 'sign验签不存在');
  116. }
  117. }
  118. //验证签名
  119. $no = nosign($noSize, $request);
  120. if ($no) {
  121. if ($no == false) {
  122. unset($param['sign']);
  123. }
  124. } else {
  125. return checksign($param);
  126. }
  127. }
  128. }
  129. //验签
  130. if (!function_exists('checksign')) {
  131. function checksign($array)
  132. {
  133. ksort($array);
  134. $string = '';
  135. foreach ($array as $k => $v) {
  136. if ($k != 'sign' && $v !== '' && !is_array($v)) {
  137. $string .= $k . '=' . $v . '&';
  138. }
  139. }
  140. $string = trim($string, '&');
  141. if ($string == '') {
  142. $string = $string . 'key=' . config('apiconfig.key');
  143. } else {
  144. $string = $string . '&key=' . config('apiconfig.key');
  145. }
  146. $sign = md5($string);
  147. if ($sign != $array['sign']) {
  148. return tojson('10004', '验签不对');
  149. }
  150. return $sign;
  151. }
  152. }
  153. //检查是否签名
  154. if (!function_exists('nosign')) {
  155. function nosign($arr, $request)
  156. {
  157. $action = $request->action;
  158. $arr = is_array($arr) ? $arr : explode(',', $arr);
  159. if (!$arr) {
  160. return false;
  161. }
  162. $arr = array_map('strtolower', $arr);
  163. // 是否存在
  164. if (in_array(strtolower($action), $arr) || in_array('*', $arr)) {
  165. return true;
  166. }
  167. // 没找到匹配
  168. return false;
  169. }
  170. }