Users.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Module;
  3. use DB;
  4. use Request;
  5. use Session;
  6. /**
  7. * Class Users
  8. * @package App\Module
  9. */
  10. class Users
  11. {
  12. /**
  13. * 临时身份标识
  14. * @return mixed|string
  15. */
  16. public static function tmpID()
  17. {
  18. if (strlen(Request::input("tmpid")) == 16) {
  19. return Request::input("tmpid");
  20. }
  21. $tmpID = Session::get('user::tmpID');
  22. if (strlen($tmpID) != 16) {
  23. $tmpID = Base::generatePassword(16);
  24. Session::put('user::tmpID', $tmpID);
  25. }
  26. return $tmpID;
  27. }
  28. /**
  29. * id获取用户名
  30. * @param $id
  31. * @return mixed
  32. */
  33. public static function id2username($id) {
  34. return DB::table('users')->where('id', intval($id))->value('username');
  35. }
  36. /**
  37. * 用户名获取id
  38. * @param $username
  39. * @return mixed
  40. */
  41. public static function username2id($username) {
  42. return intval(DB::table('users')->where('username', $username)->value('id'));
  43. }
  44. /**
  45. * token获取会员ID
  46. * @return int
  47. */
  48. public static function token2userid()
  49. {
  50. $authorization = Base::getToken();
  51. $id = 0;
  52. if ($authorization) {
  53. list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($authorization));
  54. }
  55. return intval($id);
  56. }
  57. /**
  58. * token获取会员手机号
  59. * @return int
  60. */
  61. public static function token2username()
  62. {
  63. $authorization = Base::getToken();
  64. $username = '';
  65. if ($authorization) {
  66. list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($authorization));
  67. }
  68. return Base::isMobile($username) ? $username : '';
  69. }
  70. /**
  71. * 用户身份认证(获取用户信息)
  72. * @return array|mixed
  73. */
  74. public static function auth()
  75. {
  76. global $_A;
  77. if (isset($_A["__static_auth"])) {
  78. return $_A["__static_auth"];
  79. }
  80. $authorization = Base::getToken();
  81. if ($authorization) {
  82. list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($authorization));
  83. if ($id > 0 && $timestamp + 2592000 > Base::time()) {
  84. $userinfo = DB::table('users')->where(['id' => $id, 'username' => $username, 'encrypt' => $encrypt])->first();
  85. Base::coll2array($userinfo);
  86. if ($userinfo['token']) {
  87. $upArray = [];
  88. if (Base::getIp() && $userinfo['lineip'] != Base::getIp()) {
  89. $upArray['lineip'] = Base::getIp();
  90. }
  91. if ($userinfo['linedate'] + 30 < Base::time()) {
  92. $upArray['linedate'] = Base::time();
  93. }
  94. if ($upArray) {
  95. DB::table('users')->where('id', $userinfo['id'])->update($upArray);
  96. }
  97. return $_A["__static_auth"] = Users::retInfo($userinfo);
  98. }
  99. }
  100. }
  101. return $_A["__static_auth"] = false;
  102. }
  103. /**
  104. * 用户身份认证, 身份丢失时exit输出(获取用户信息)
  105. * @return array|mixed
  106. */
  107. public static function authE()
  108. {
  109. $user = Users::auth();
  110. if (!$user) {
  111. $authorization = Base::getToken();
  112. if ($authorization) {
  113. return Base::retError('身份已失效,请重新登录!', [], -1);
  114. } else {
  115. return Base::retError('请登录后继续...', [], -1);
  116. }
  117. }
  118. return Base::retSuccess("auth", $user);
  119. }
  120. /**
  121. * 生成token
  122. * @param $userinfo
  123. * @return bool|string
  124. */
  125. public static function token($userinfo)
  126. {
  127. if (strlen($userinfo['encrypt']) < 6) {
  128. $userinfo['encrypt'] = Base::generatePassword(6);
  129. DB::table('users')->where('id', $userinfo['id'])->update(['encrypt' => $userinfo['encrypt']]);
  130. }
  131. return base64_encode($userinfo['id'] . '@' . $userinfo['username'] . '@' . $userinfo['encrypt'] . '@' . Base::time() . '@' . Base::generatePassword(6));
  132. }
  133. /**
  134. * 判断用户权限(身份)
  135. * @param $identity
  136. * @return array
  137. */
  138. public static function identity($identity)
  139. {
  140. $user = Users::auth();
  141. if (is_array($user['identity'])
  142. && in_array($identity, $user['identity'])) {
  143. return Base::retSuccess("权限通过");
  144. }
  145. return Base::retError("权限不足");
  146. }
  147. /**
  148. * 筛选用户信息
  149. * @param $userinfo
  150. * @return mixed
  151. */
  152. public static function retInfo($userinfo)
  153. {
  154. //是否设置密码
  155. if (!isset($userinfo['setpass'])) {
  156. $userinfo['setpass'] = $userinfo['userpass'] ? 1 : 0;
  157. }
  158. //
  159. $userinfo['setting'] = Base::string2array($userinfo['setting']);
  160. $userinfo['userimg'] = $userinfo['userimg'] ? Base::fillUrl($userinfo['userimg']) : url('images/avatar.png');
  161. $userinfo['identity'] = is_array($userinfo['identity']) ? $userinfo['identity'] : explode(",", trim($userinfo['identity'], ","));
  162. unset($userinfo['encrypt']);
  163. unset($userinfo['userpass']);
  164. return $userinfo;
  165. }
  166. }