Users.php 8.7 KB

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