Users.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. * @param $username
  16. * @param $userpass
  17. * @param array $other
  18. * @return array
  19. */
  20. public static function reg($username, $userpass, $other = [])
  21. {
  22. //用户名
  23. if (strlen($username) < 2) {
  24. return Base::retError('用户名不可以少于2个字符!');
  25. } elseif (strlen($username) > 16) {
  26. return Base::retError('用户名最多只能设置16个字符!');
  27. }
  28. if (!preg_match('/^[A-Za-z0-9_\x{4e00}-\x{9fa5}]+$/u', $username)) {
  29. return Base::retError('用户名由2-16位数字或字母、汉字、下划线组成!');
  30. }
  31. if (Users::username2id($username) > 0) {
  32. return Base::retError('用户名已存在!');
  33. }
  34. //密码
  35. if (strlen($userpass) < 6) {
  36. return Base::retError('密码设置不能小于6位数!');
  37. } elseif (strlen($userpass) > 32) {
  38. return Base::retError('密码最多只能设置32位数!');
  39. }
  40. //开始注册
  41. $encrypt = Base::generatePassword(6);
  42. $inArray = [
  43. 'encrypt' => $encrypt,
  44. 'username' => $username,
  45. 'userpass' => Base::md52($userpass, $encrypt),
  46. 'regip' => Base::getIp(),
  47. 'regdate' => Base::time()
  48. ];
  49. if ($other) {
  50. $inArray = array_merge($inArray, $other);
  51. }
  52. DB::table('users')->insert($inArray);
  53. $user = Base::DBC2A(DB::table('users')->where('username', $username)->first());
  54. if (empty($user)) {
  55. return Base::retError('注册失败,请稍后再试。');
  56. }
  57. return Base::retSuccess('success', $user);
  58. }
  59. /**
  60. * 临时身份标识
  61. * @param int $length
  62. * @return mixed|string
  63. */
  64. public static function tmpID($length = 8)
  65. {
  66. if (strlen(Request::input("tmpid")) == $length) {
  67. return Request::input("tmpid");
  68. }
  69. $tmpID = Session::get('user::tmpID');
  70. if (strlen($tmpID) != $length) {
  71. $tmpID = Base::generatePassword($length);
  72. Session::put('user::tmpID', $tmpID);
  73. }
  74. return $tmpID;
  75. }
  76. /**
  77. * id获取用户名
  78. * @param $id
  79. * @return mixed
  80. */
  81. public static function id2username($id) {
  82. return DB::table('users')->where('id', intval($id))->value('username');
  83. }
  84. /**
  85. * 用户名获取id
  86. * @param $username
  87. * @return mixed
  88. */
  89. public static function username2id($username) {
  90. return intval(DB::table('users')->where('username', $username)->value('id'));
  91. }
  92. /**
  93. * token获取会员ID
  94. * @return int
  95. */
  96. public static function token2userid()
  97. {
  98. $authorization = Base::getToken();
  99. $id = 0;
  100. if ($authorization) {
  101. list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($authorization) . "@@@@");
  102. }
  103. return intval($id);
  104. }
  105. /**
  106. * token获取会员手机号
  107. * @return int
  108. */
  109. public static function token2username()
  110. {
  111. $authorization = Base::getToken();
  112. $username = '';
  113. if ($authorization) {
  114. list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($authorization) . "@@@@");
  115. }
  116. return Base::isMobile($username) ? $username : '';
  117. }
  118. /**
  119. * token获取encrypt
  120. * @return mixed|string
  121. */
  122. public static function token2encrypt()
  123. {
  124. $authorization = Base::getToken();
  125. $encrypt = '';
  126. if ($authorization) {
  127. list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($authorization) . "@@@@");
  128. }
  129. return $encrypt ?: '';
  130. }
  131. /**
  132. * 用户身份认证(获取用户信息)
  133. * @return array|mixed
  134. */
  135. public static function auth()
  136. {
  137. global $_A;
  138. if (isset($_A["__static_auth"])) {
  139. return $_A["__static_auth"];
  140. }
  141. $authorization = Base::getToken();
  142. if ($authorization) {
  143. list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($authorization) . "@@@@");
  144. if (intval($id) > 0 && intval($timestamp) + 2592000 > Base::time()) {
  145. $userinfo = DB::table('users')->where(['id' => $id, 'username' => $username, 'encrypt' => $encrypt])->first();
  146. Base::coll2array($userinfo);
  147. if ($userinfo['token']) {
  148. $upArray = [];
  149. if (Base::getIp() && $userinfo['lineip'] != Base::getIp()) {
  150. $upArray['lineip'] = Base::getIp();
  151. }
  152. if ($userinfo['linedate'] + 30 < Base::time()) {
  153. $upArray['linedate'] = Base::time();
  154. }
  155. if ($upArray) {
  156. DB::table('users')->where('id', $userinfo['id'])->update($upArray);
  157. }
  158. return $_A["__static_auth"] = Users::retInfo($userinfo);
  159. }
  160. }
  161. }
  162. return $_A["__static_auth"] = false;
  163. }
  164. /**
  165. * 用户身份认证(获取用户信息)
  166. * @return array|mixed
  167. */
  168. public static function authE()
  169. {
  170. $user = Users::auth();
  171. if (!$user) {
  172. $authorization = Base::getToken();
  173. if ($authorization) {
  174. return Base::retError('身份已失效,请重新登录!', [], -1);
  175. } else {
  176. return Base::retError('请登录后继续...', [], -1);
  177. }
  178. }
  179. return Base::retSuccess("auth", $user);
  180. }
  181. /**
  182. * 生成token
  183. * @param $userinfo
  184. * @return bool|string
  185. */
  186. public static function token($userinfo)
  187. {
  188. return base64_encode($userinfo['id'] . '@' . $userinfo['username'] . '@' . $userinfo['encrypt'] . '@' . Base::time() . '@' . Base::generatePassword(6));
  189. }
  190. /**
  191. * 判断用户权限(身份)
  192. * @param $identity
  193. * @return array
  194. */
  195. public static function identity($identity)
  196. {
  197. $user = Users::auth();
  198. if (is_array($user['identity'])
  199. && in_array($identity, $user['identity'])) {
  200. return Base::retSuccess("权限通过。");
  201. }
  202. return Base::retError("权限不足!");
  203. }
  204. /**
  205. * 判断用户权限(身份)
  206. * @param $identity
  207. * @param $userIdentity
  208. * @return bool
  209. */
  210. public static function identityRaw($identity, $userIdentity)
  211. {
  212. $userIdentity = is_array($userIdentity) ? $userIdentity : explode(",", trim($userIdentity, ","));
  213. return $identity && in_array($identity, $userIdentity);
  214. }
  215. /**
  216. * 筛选用户信息
  217. * @param $userinfo
  218. * @return mixed
  219. */
  220. public static function retInfo($userinfo)
  221. {
  222. //是否设置密码
  223. if (!isset($userinfo['setpass'])) {
  224. $userinfo['setpass'] = $userinfo['userpass'] ? 1 : 0;
  225. }
  226. //
  227. $userinfo['setting'] = Base::string2array($userinfo['setting']);
  228. $userinfo['userimg'] = self::userimg($userinfo['userimg']);
  229. $userinfo['identity'] = is_array($userinfo['identity']) ? $userinfo['identity'] : explode(",", trim($userinfo['identity'], ","));
  230. unset($userinfo['encrypt']);
  231. unset($userinfo['userpass']);
  232. return $userinfo;
  233. }
  234. /**
  235. * userid 获取 基本信息
  236. * @param int $userid 会员ID
  237. * @return array
  238. */
  239. public static function userid2basic($userid)
  240. {
  241. if (empty($userid)) {
  242. return [];
  243. }
  244. $fields = ['username', 'nickname', 'userimg', 'profession'];
  245. $userInfo = DBCache::table('users')->where('id', $userid)->select($fields)->cacheMinutes(1)->first();
  246. if ($userInfo) {
  247. $userInfo['userimg'] = Users::userimg($userInfo['userimg']);
  248. }
  249. return $userInfo ?: [];
  250. }
  251. /**
  252. * username 获取 基本信息
  253. * @param string $username 用户名
  254. * @param bool $clearCache 清理缓存
  255. * @return array
  256. */
  257. public static function username2basic($username, $clearCache = false)
  258. {
  259. if (empty($username)) {
  260. return [];
  261. }
  262. $fields = ['username', 'nickname', 'userimg', 'profession'];
  263. $builder = DBCache::table('users')->where('username', $username)->select($fields)->cacheMinutes(1);
  264. if ($clearCache) {
  265. $builder->removeCache()->first();
  266. return [];
  267. } else {
  268. $userInfo = $builder->first();
  269. if ($userInfo) {
  270. $userInfo['userimg'] = Users::userimg($userInfo['userimg']);
  271. }
  272. return $userInfo ?: [];
  273. }
  274. }
  275. /**
  276. * 用户头像,不存在时返回默认
  277. * @param string $var 头像地址 或 会员用户名
  278. * @return \Illuminate\Contracts\Routing\UrlGenerator|string
  279. */
  280. public static function userimg($var) {
  281. if (!Base::strExists($var, '.')) {
  282. if (empty($var)) {
  283. $var = "";
  284. } else {
  285. $userInfo = self::username2basic($var);
  286. $var = $userInfo['userimg'];
  287. }
  288. }
  289. return $var ? Base::fillUrl($var) : url('images/other/avatar.png');
  290. }
  291. }