Users.php 8.8 KB

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