Users.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace App\Module;
  3. use App\Model\DBCache;
  4. use DB;
  5. use Request;
  6. use Session;
  7. /**
  8. * Class Users
  9. * @package App\Module
  10. */
  11. class Users
  12. {
  13. /**
  14. * 临时身份标识
  15. * @return mixed|string
  16. */
  17. public static function tmpID()
  18. {
  19. if (strlen(Request::input("tmpid")) == 16) {
  20. return Request::input("tmpid");
  21. }
  22. $tmpID = Session::get('user::tmpID');
  23. if (strlen($tmpID) != 16) {
  24. $tmpID = Base::generatePassword(16);
  25. Session::put('user::tmpID', $tmpID);
  26. }
  27. return $tmpID;
  28. }
  29. /**
  30. * id获取用户名
  31. * @param $id
  32. * @return mixed
  33. */
  34. public static function id2username($id) {
  35. return DB::table('users')->where('id', intval($id))->value('username');
  36. }
  37. /**
  38. * 用户名获取id
  39. * @param $username
  40. * @return mixed
  41. */
  42. public static function username2id($username) {
  43. return intval(DB::table('users')->where('username', $username)->value('id'));
  44. }
  45. /**
  46. * token获取会员ID
  47. * @return int
  48. */
  49. public static function token2userid()
  50. {
  51. $authorization = Base::getToken();
  52. $id = 0;
  53. if ($authorization) {
  54. list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($authorization) . "@@@@");
  55. }
  56. return intval($id);
  57. }
  58. /**
  59. * token获取会员手机号
  60. * @return int
  61. */
  62. public static function token2username()
  63. {
  64. $authorization = Base::getToken();
  65. $username = '';
  66. if ($authorization) {
  67. list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($authorization) . "@@@@");
  68. }
  69. return Base::isMobile($username) ? $username : '';
  70. }
  71. /**
  72. * 用户身份认证(获取用户信息)
  73. * @return array|mixed
  74. */
  75. public static function auth()
  76. {
  77. global $_A;
  78. if (isset($_A["__static_auth"])) {
  79. return $_A["__static_auth"];
  80. }
  81. $authorization = Base::getToken();
  82. if ($authorization) {
  83. list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($authorization) . "@@@@");
  84. if (intval($id) > 0 && intval($timestamp) + 2592000 > Base::time()) {
  85. $userinfo = DB::table('users')->where(['id' => $id, 'username' => $username, 'encrypt' => $encrypt])->first();
  86. Base::coll2array($userinfo);
  87. if ($userinfo['token']) {
  88. $upArray = [];
  89. if (Base::getIp() && $userinfo['lineip'] != Base::getIp()) {
  90. $upArray['lineip'] = Base::getIp();
  91. }
  92. if ($userinfo['linedate'] + 30 < Base::time()) {
  93. $upArray['linedate'] = Base::time();
  94. }
  95. if ($upArray) {
  96. DB::table('users')->where('id', $userinfo['id'])->update($upArray);
  97. }
  98. return $_A["__static_auth"] = Users::retInfo($userinfo);
  99. }
  100. }
  101. }
  102. return $_A["__static_auth"] = false;
  103. }
  104. /**
  105. * 用户身份认证, 身份丢失时exit输出(获取用户信息)
  106. * @return array|mixed
  107. */
  108. public static function authE()
  109. {
  110. $user = Users::auth();
  111. if (!$user) {
  112. $authorization = Base::getToken();
  113. if ($authorization) {
  114. return Base::retError('身份已失效,请重新登录!', [], -1);
  115. } else {
  116. return Base::retError('请登录后继续...', [], -1);
  117. }
  118. }
  119. return Base::retSuccess("auth", $user);
  120. }
  121. /**
  122. * 生成token
  123. * @param $userinfo
  124. * @return bool|string
  125. */
  126. public static function token($userinfo)
  127. {
  128. if (strlen($userinfo['encrypt']) < 6) {
  129. $userinfo['encrypt'] = Base::generatePassword(6);
  130. DB::table('users')->where('id', $userinfo['id'])->update(['encrypt' => $userinfo['encrypt']]);
  131. }
  132. return base64_encode($userinfo['id'] . '@' . $userinfo['username'] . '@' . $userinfo['encrypt'] . '@' . Base::time() . '@' . Base::generatePassword(6));
  133. }
  134. /**
  135. * 判断用户权限(身份)
  136. * @param $identity
  137. * @return array
  138. */
  139. public static function identity($identity)
  140. {
  141. $user = Users::auth();
  142. if (is_array($user['identity'])
  143. && in_array($identity, $user['identity'])) {
  144. return Base::retSuccess("权限通过");
  145. }
  146. return Base::retError("权限不足");
  147. }
  148. /**
  149. * 筛选用户信息
  150. * @param $userinfo
  151. * @return mixed
  152. */
  153. public static function retInfo($userinfo)
  154. {
  155. //是否设置密码
  156. if (!isset($userinfo['setpass'])) {
  157. $userinfo['setpass'] = $userinfo['userpass'] ? 1 : 0;
  158. }
  159. //
  160. $userinfo['setting'] = Base::string2array($userinfo['setting']);
  161. $userinfo['userimg'] = $userinfo['userimg'] ? Base::fillUrl($userinfo['userimg']) : url('images/other/avatar.png');
  162. $userinfo['identity'] = is_array($userinfo['identity']) ? $userinfo['identity'] : explode(",", trim($userinfo['identity'], ","));
  163. unset($userinfo['encrypt']);
  164. unset($userinfo['userpass']);
  165. return $userinfo;
  166. }
  167. /**
  168. * userid 获取 基本信息
  169. * @param int $userid 会员ID
  170. * @return array
  171. */
  172. public static function userid2basic($userid)
  173. {
  174. if (empty($userid)) {
  175. return [];
  176. }
  177. $fields = ['username', 'nickname', 'userimg', 'profession'];
  178. $userInfo = DBCache::table('users')->where('id', $userid)->select($fields)->cacheMinutes(1)->first();
  179. if ($userInfo) {
  180. $userInfo['userimg'] = Users::userimg($userInfo['userimg']);
  181. }
  182. return $userInfo ?: [];
  183. }
  184. /**
  185. * username 获取 基本信息
  186. * @param string $username 用户名
  187. * @param bool $clearCache 清理缓存
  188. * @return array
  189. */
  190. public static function username2basic($username, $clearCache = false)
  191. {
  192. if (empty($username)) {
  193. return [];
  194. }
  195. $fields = ['username', 'nickname', 'userimg', 'profession'];
  196. $builder = DBCache::table('users')->where('username', $username)->select($fields)->cacheMinutes(1);
  197. if ($clearCache) {
  198. $builder->removeCache()->first();
  199. return [];
  200. } else {
  201. $userInfo = $builder->first();
  202. if ($userInfo) {
  203. $userInfo['userimg'] = Users::userimg($userInfo['userimg']);
  204. }
  205. return $userInfo ?: [];
  206. }
  207. }
  208. /**
  209. * 用户头像,不存在时返回默认
  210. * @param string $var 头像地址 或 会员用户名
  211. * @return \Illuminate\Contracts\Routing\UrlGenerator|string
  212. */
  213. public static function userimg($var) {
  214. if (!Base::strExists($var, '.')) {
  215. if (empty($var)) {
  216. $var = "";
  217. } else {
  218. $userInfo = self::username2basic($var);
  219. $var = $userInfo['userimg'];
  220. }
  221. }
  222. return $var ? Base::fillUrl($var) : url('images/other/avatar.png');
  223. }
  224. }