Users.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. Users::AZUpdate($user['id']);
  58. return Base::retSuccess('success', $user);
  59. }
  60. /**
  61. * 临时身份标识
  62. * @param int $length
  63. * @return mixed|string
  64. */
  65. public static function tmpID($length = 8)
  66. {
  67. if (strlen(Request::input("tmpid")) == $length) {
  68. return Request::input("tmpid");
  69. }
  70. $tmpID = Session::get('user::tmpID');
  71. if (strlen($tmpID) != $length) {
  72. $tmpID = Base::generatePassword($length);
  73. Session::put('user::tmpID', $tmpID);
  74. }
  75. return $tmpID;
  76. }
  77. /**
  78. * id获取用户名
  79. * @param $id
  80. * @return mixed
  81. */
  82. public static function id2username($id) {
  83. return DB::table('users')->where('id', intval($id))->value('username');
  84. }
  85. /**
  86. * 用户名获取id
  87. * @param $username
  88. * @return mixed
  89. */
  90. public static function username2id($username) {
  91. return intval(DB::table('users')->where('username', $username)->value('id'));
  92. }
  93. /**
  94. * token获取会员ID
  95. * @return int
  96. */
  97. public static function token2userid()
  98. {
  99. $authorization = Base::getToken();
  100. $id = 0;
  101. if ($authorization) {
  102. list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($authorization) . "@@@@");
  103. }
  104. return intval($id);
  105. }
  106. /**
  107. * token获取会员手机号
  108. * @return int
  109. */
  110. public static function token2username()
  111. {
  112. $authorization = Base::getToken();
  113. $username = '';
  114. if ($authorization) {
  115. list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($authorization) . "@@@@");
  116. }
  117. return Base::isMobile($username) ? $username : '';
  118. }
  119. /**
  120. * token获取encrypt
  121. * @return mixed|string
  122. */
  123. public static function token2encrypt()
  124. {
  125. $authorization = Base::getToken();
  126. $encrypt = '';
  127. if ($authorization) {
  128. list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($authorization) . "@@@@");
  129. }
  130. return $encrypt ?: '';
  131. }
  132. /**
  133. * 用户身份认证(获取用户信息)
  134. * @return array|mixed
  135. */
  136. public static function auth()
  137. {
  138. global $_A;
  139. if (isset($_A["__static_auth"])) {
  140. return $_A["__static_auth"];
  141. }
  142. $authorization = Base::getToken();
  143. if ($authorization) {
  144. list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($authorization) . "@@@@");
  145. if (intval($id) > 0 && intval($timestamp) + 2592000 > Base::time()) {
  146. $userinfo = DB::table('users')->where(['id' => $id, 'username' => $username, 'encrypt' => $encrypt])->first();
  147. Base::coll2array($userinfo);
  148. if ($userinfo['token']) {
  149. $upArray = [];
  150. if (Base::getIp() && $userinfo['lineip'] != Base::getIp()) {
  151. $upArray['lineip'] = Base::getIp();
  152. }
  153. if ($userinfo['linedate'] + 30 < Base::time()) {
  154. $upArray['linedate'] = Base::time();
  155. }
  156. if ($upArray) {
  157. DB::table('users')->where('id', $userinfo['id'])->update($upArray);
  158. }
  159. return $_A["__static_auth"] = Users::retInfo($userinfo);
  160. }
  161. }
  162. }
  163. return $_A["__static_auth"] = false;
  164. }
  165. /**
  166. * 用户身份认证(获取用户信息)
  167. * @return array|mixed
  168. */
  169. public static function authE()
  170. {
  171. $user = Users::auth();
  172. if (!$user) {
  173. $authorization = Base::getToken();
  174. if ($authorization) {
  175. return Base::retError('身份已失效,请重新登录!', [], -1);
  176. } else {
  177. return Base::retError('请登录后继续...', [], -1);
  178. }
  179. }
  180. return Base::retSuccess("auth", $user);
  181. }
  182. /**
  183. * 生成token
  184. * @param $userinfo
  185. * @return bool|string
  186. */
  187. public static function token($userinfo)
  188. {
  189. return base64_encode($userinfo['id'] . '@' . $userinfo['username'] . '@' . $userinfo['encrypt'] . '@' . Base::time() . '@' . Base::generatePassword(6));
  190. }
  191. /**
  192. * 判断用户权限(身份)
  193. * @param $identity
  194. * @return array
  195. */
  196. public static function identity($identity)
  197. {
  198. $user = Users::auth();
  199. if (is_array($user['identity'])
  200. && in_array($identity, $user['identity'])) {
  201. return Base::retSuccess("权限通过。");
  202. }
  203. return Base::retError("权限不足!");
  204. }
  205. /**
  206. * 判断用户权限(身份)
  207. * @param $identity
  208. * @param $userIdentity
  209. * @return bool
  210. */
  211. public static function identityRaw($identity, $userIdentity)
  212. {
  213. $userIdentity = is_array($userIdentity) ? $userIdentity : explode(",", trim($userIdentity, ","));
  214. return $identity && in_array($identity, $userIdentity);
  215. }
  216. /**
  217. * 筛选用户信息
  218. * @param $userinfo
  219. * @return mixed
  220. */
  221. public static function retInfo($userinfo)
  222. {
  223. //是否设置密码
  224. if (!isset($userinfo['setpass'])) {
  225. $userinfo['setpass'] = $userinfo['userpass'] ? 1 : 0;
  226. }
  227. //
  228. $userinfo['id'] = intval($userinfo['id']);
  229. $userinfo['changepass'] = intval($userinfo['changepass']);
  230. $userinfo['setting'] = Base::string2array($userinfo['setting']);
  231. $userinfo['userimg'] = self::userimg($userinfo['userimg']);
  232. $userinfo['identity'] = is_array($userinfo['identity']) ? $userinfo['identity'] : explode(",", trim($userinfo['identity'], ","));
  233. unset($userinfo['encrypt']);
  234. unset($userinfo['userpass']);
  235. return $userinfo;
  236. }
  237. /**
  238. * userid 获取 基本信息
  239. * @param int $userid 会员ID
  240. * @return array
  241. */
  242. public static function userid2basic($userid)
  243. {
  244. if (empty($userid)) {
  245. return [];
  246. }
  247. $fields = ['id AS userid', 'username', 'nickname', 'userimg', 'profession'];
  248. $userInfo = DBCache::table('users')->where('id', $userid)->select($fields)->cacheMinutes(1)->first();
  249. if ($userInfo) {
  250. $userInfo['userimg'] = Users::userimg($userInfo['userimg']);
  251. }
  252. return $userInfo ?: [];
  253. }
  254. /**
  255. * username 获取 基本信息
  256. * @param string $username 用户名
  257. * @param bool $clearCache 清理缓存
  258. * @return array
  259. */
  260. public static function username2basic($username, $clearCache = false)
  261. {
  262. if (empty($username)) {
  263. return [];
  264. }
  265. $fields = ['id AS userid', 'username', 'nickname', 'userimg', 'profession'];
  266. $builder = DBCache::table('users')->where('username', $username)->select($fields)->cacheMinutes(1);
  267. if ($clearCache) {
  268. $builder->removeCache()->first();
  269. return [];
  270. } else {
  271. $userInfo = $builder->first();
  272. if ($userInfo) {
  273. $userInfo['userimg'] = Users::userimg($userInfo['userimg']);
  274. }
  275. return $userInfo ?: [];
  276. }
  277. }
  278. /**
  279. * 获取会员昵称
  280. * @param $username
  281. * @return mixed
  282. */
  283. public static function nickname($username)
  284. {
  285. $info = self::username2basic($username);
  286. if (empty($info)) {
  287. return $username;
  288. }
  289. return $info['nickname'] ?: $info['username'];
  290. }
  291. /**
  292. * 用户头像,不存在时返回默认
  293. * @param string|array $params 头像地址 或 会员用户名
  294. * @return \Illuminate\Contracts\Routing\UrlGenerator|string
  295. */
  296. public static function userimg($params) {
  297. if (is_array($params)) {
  298. foreach ($params as $key => $val) {
  299. if (is_array($val)) {
  300. $val['userimg'] = self::userimg($val['userimg'] ?: $val['username']);
  301. } else {
  302. $val = self::userimg($val);
  303. }
  304. $params[$key] = $val;
  305. }
  306. return $params;
  307. }
  308. if (!Base::strExists($params, '.')) {
  309. if (empty($params)) {
  310. $params = "";
  311. } else {
  312. $userInfo = self::username2basic($params);
  313. $params = $userInfo['userimg'];
  314. }
  315. }
  316. return $params ? Base::fillUrl($params) : url('images/other/avatar.png');
  317. }
  318. /**
  319. * 更新首字母
  320. * @param $userid
  321. */
  322. public static function AZUpdate($userid) {
  323. $row = Base::DBC2A(DB::table('users')->where('id', $userid)->select(['username', 'nickname'])->first());
  324. if ($row) {
  325. DB::table('users')->where('id', $userid)->update([
  326. 'az' => Base::getFirstCharter($row['nickname'] ?: $row['username'])
  327. ]);
  328. }
  329. }
  330. }