common.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. // 应用公共文件
  3. if(!function_exists("json_show")){
  4. function json_show(int $code = 0, string $message = '请求成功', array $data = [])
  5. {
  6. return json(['code' => $code, 'message' => $message, 'data' => $data]);
  7. }
  8. }
  9. /**手机号验证
  10. * @param $mobile
  11. * @return bool
  12. */
  13. if(!function_exists("checkMobile")){
  14. function checkMobile($mobile){
  15. if (!is_numeric($mobile)) {
  16. return false;
  17. }
  18. return preg_match('#^1[3,4,5,6,7,8,9]{1}[\d]{9}$#', $mobile) ? true : false;
  19. }
  20. }
  21. /**邮箱验证
  22. * @param $email
  23. * @return bool
  24. */
  25. if(!function_exists("checkEmail")){
  26. function checkEmail($email){
  27. if (!$email) {
  28. return false;
  29. }
  30. return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email) ? true : false;
  31. }
  32. }
  33. /**
  34. * @param
  35. * @return int
  36. */
  37. if(!function_exists("makeSalt")){
  38. function makeSalt(){
  39. $salt = rand(10000000,99999999);
  40. return $salt;
  41. }
  42. }
  43. /**
  44. * @param $account
  45. * @param float|int $expire
  46. * @return string
  47. */
  48. if(!function_exists("makeToken")){
  49. function makeToken($account){
  50. return sha1($account['username'].$account['mobile'].time());
  51. }
  52. }
  53. if(!function_exists("checkToken")){
  54. /**
  55. * @param $token
  56. * @param int $expire
  57. * @return array|bool|float|int|mixed|\stdClass|string|null
  58. * @throws \Psr\SimpleCache\InvalidArgumentException
  59. */
  60. function checkToken($token,$expire=0){
  61. $getToken = think\facade\Cache::store("redis")->get("user:info:{$token}");
  62. if($getToken!=false){
  63. $cache=think\facade\Cache::store("redis")->set("user:info:{$token}",$getToken,$expire);
  64. if($cache!=false) return $getToken;
  65. }
  66. return false;
  67. }
  68. }