User.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. namespace app\index\controller;
  3. use addons\wechat\model\WechatCaptcha;
  4. use app\common\controller\Frontend;
  5. use app\common\library\Ems;
  6. use app\common\library\Sms;
  7. use app\common\model\Attachment;
  8. use fast\Random;
  9. use think\Config;
  10. use think\Cookie;
  11. use think\Hook;
  12. use think\Session;
  13. use think\Validate;
  14. /**
  15. * 会员中心
  16. */
  17. class User extends Frontend
  18. {
  19. protected $layout = 'default';
  20. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'third'];
  21. protected $noNeedRight = ['*'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $auth = $this->auth;
  26. if (!Config::get('fastadmin.usercenter')) {
  27. $this->error(__('User center already closed'), '/');
  28. }
  29. //监听注册登录退出的事件
  30. Hook::add('user_login_successed', function ($user) use ($auth) {
  31. $expire = input('post.keeplogin') ? 30 * 86400 : 0;
  32. Cookie::set('uid', $user->id, $expire);
  33. Cookie::set('token', $auth->getToken(), $expire);
  34. });
  35. Hook::add('user_register_successed', function ($user) use ($auth) {
  36. Cookie::set('uid', $user->id);
  37. Cookie::set('token', $auth->getToken());
  38. });
  39. Hook::add('user_delete_successed', function ($user) use ($auth) {
  40. Cookie::delete('uid');
  41. Cookie::delete('token');
  42. });
  43. Hook::add('user_logout_successed', function ($user) use ($auth) {
  44. Cookie::delete('uid');
  45. Cookie::delete('token');
  46. });
  47. }
  48. /**
  49. * 会员中心
  50. */
  51. public function index()
  52. {
  53. $this->view->assign('title', __('User center'));
  54. return $this->view->fetch();
  55. }
  56. /**
  57. * 注册会员
  58. */
  59. public function register()
  60. {
  61. $url = $this->request->request('url', '', 'trim,xss_clean');
  62. if ($this->auth->id) {
  63. $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index'));
  64. }
  65. if ($this->request->isPost()) {
  66. $username = $this->request->post('username');
  67. $password = $this->request->post('password');
  68. $email = $this->request->post('email');
  69. $mobile = $this->request->post('mobile', '');
  70. $captcha = $this->request->post('captcha');
  71. $token = $this->request->post('__token__');
  72. $rule = [
  73. 'username' => 'require|length:3,30',
  74. 'password' => 'require|length:6,30',
  75. 'email' => 'require|email',
  76. 'mobile' => 'regex:/^1\d{10}$/',
  77. '__token__' => 'require|token',
  78. ];
  79. $msg = [
  80. 'username.require' => 'Username can not be empty',
  81. 'username.length' => 'Username must be 3 to 30 characters',
  82. 'password.require' => 'Password can not be empty',
  83. 'password.length' => 'Password must be 6 to 30 characters',
  84. 'email' => 'Email is incorrect',
  85. 'mobile' => 'Mobile is incorrect',
  86. ];
  87. $data = [
  88. 'username' => $username,
  89. 'password' => $password,
  90. 'email' => $email,
  91. 'mobile' => $mobile,
  92. '__token__' => $token,
  93. ];
  94. //验证码
  95. $captchaResult = true;
  96. $captchaType = config("fastadmin.user_register_captcha");
  97. if ($captchaType) {
  98. if ($captchaType == 'mobile') {
  99. $captchaResult = Sms::check($mobile, $captcha, 'register');
  100. } elseif ($captchaType == 'email') {
  101. $captchaResult = Ems::check($email, $captcha, 'register');
  102. } elseif ($captchaType == 'wechat') {
  103. $captchaResult = WechatCaptcha::check($captcha, 'register');
  104. } elseif ($captchaType == 'text') {
  105. $captchaResult = \think\Validate::is($captcha, 'captcha');
  106. }
  107. }
  108. if (!$captchaResult) {
  109. $this->error(__('Captcha is incorrect'));
  110. }
  111. $validate = new Validate($rule, $msg);
  112. $result = $validate->check($data);
  113. if (!$result) {
  114. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  115. }
  116. if ($this->auth->register($username, $password, $email, $mobile)) {
  117. $this->success(__('Sign up successful'), $url ? $url : url('user/index'));
  118. } else {
  119. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  120. }
  121. }
  122. //判断来源
  123. $referer = $this->request->server('HTTP_REFERER', '', 'url_clean');
  124. if (!$url && $referer && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  125. $url = $referer;
  126. }
  127. $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
  128. $this->view->assign('url', $url);
  129. $this->view->assign('title', __('Register'));
  130. return $this->view->fetch();
  131. }
  132. /**
  133. * 会员登录
  134. */
  135. public function login()
  136. {
  137. $url = $this->request->request('url', '', 'trim,xss_clean');
  138. if ($this->auth->id) {
  139. $this->success(__('You\'ve logged in, do not login again'), $url ?: url('user/index'));
  140. }
  141. if ($this->request->isPost()) {
  142. $account = $this->request->post('account');
  143. $password = $this->request->post('password');
  144. $keeplogin = (int)$this->request->post('keeplogin');
  145. $token = $this->request->post('__token__');
  146. $rule = [
  147. 'account' => 'require|length:3,50',
  148. 'password' => 'require|length:6,30',
  149. '__token__' => 'require|token',
  150. ];
  151. $msg = [
  152. 'account.require' => 'Account can not be empty',
  153. 'account.length' => 'Account must be 3 to 50 characters',
  154. 'password.require' => 'Password can not be empty',
  155. 'password.length' => 'Password must be 6 to 30 characters',
  156. ];
  157. $data = [
  158. 'account' => $account,
  159. 'password' => $password,
  160. '__token__' => $token,
  161. ];
  162. $validate = new Validate($rule, $msg);
  163. $result = $validate->check($data);
  164. if (!$result) {
  165. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  166. }
  167. if ($this->auth->login($account, $password)) {
  168. $this->success(__('Logged in successful'), $url ? $url : url('user/index'));
  169. } else {
  170. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  171. }
  172. }
  173. //判断来源
  174. $referer = $this->request->server('HTTP_REFERER', '', 'url_clean');
  175. if (!$url && $referer && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  176. $url = $referer;
  177. }
  178. $this->view->assign('url', $url);
  179. $this->view->assign('title', __('Login'));
  180. return $this->view->fetch();
  181. }
  182. /**
  183. * 手机号验证码登录
  184. */
  185. public function mobilelogin()
  186. {
  187. $url = $this->request->request('url', '', 'trim,xss_clean');
  188. if ($this->request->isPost()) {
  189. $mobile = $this->request->post('mobile');
  190. $captcha = $this->request->post('captcha');
  191. if (!$mobile || !$captcha) {
  192. $this->error(__('Invalid parameters'));
  193. }
  194. if (!Validate::regex($mobile, "^1\d{10}$")) {
  195. $this->error(__('Mobile is incorrect'));
  196. }
  197. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  198. $this->error(__('Captcha is incorrect'));
  199. }
  200. $user = \app\common\model\User::getByMobile($mobile);
  201. if ($user) {
  202. if ($user->status != 'normal') {
  203. $this->error(__('Account is locked'));
  204. }
  205. //如果已经有账号则直接登录
  206. $ret = $this->auth->direct($user->id);
  207. } else {
  208. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  209. }
  210. if ($ret) {
  211. Sms::flush($mobile, 'mobilelogin');
  212. $data = ['userinfo' => $this->auth->getUserinfo()];
  213. $this->success(__('Logged in successful'), $url);
  214. } else {
  215. $this->error($this->auth->getError());
  216. }
  217. }
  218. //判断来源
  219. $referer = $this->request->server('HTTP_REFERER');
  220. if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
  221. && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  222. $url = $referer;
  223. }
  224. $this->view->assign('url', $url);
  225. $this->view->assign('title', __('Login'));
  226. return $this->view->fetch();
  227. }
  228. /**
  229. * 退出登录
  230. */
  231. public function logout()
  232. {
  233. if ($this->request->isPost()) {
  234. $this->token();
  235. //退出本站
  236. $this->auth->logout();
  237. $this->success(__('Logout successful'), url('user/index'));
  238. }
  239. $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
  240. $html .= "<script>document.forms['logout_submit'].submit();</script>";
  241. return $html;
  242. }
  243. /**
  244. * 个人信息
  245. */
  246. public function profile()
  247. {
  248. $this->view->assign('title', __('Profile'));
  249. return $this->view->fetch();
  250. }
  251. /**
  252. * 修改密码
  253. */
  254. public function changepwd()
  255. {
  256. if ($this->request->isPost()) {
  257. $oldpassword = $this->request->post("oldpassword");
  258. $newpassword = $this->request->post("newpassword");
  259. $renewpassword = $this->request->post("renewpassword");
  260. $token = $this->request->post('__token__');
  261. $rule = [
  262. 'oldpassword' => 'require|regex:\S{6,30}',
  263. 'newpassword' => 'require|regex:\S{6,30}',
  264. 'renewpassword' => 'require|regex:\S{6,30}|confirm:newpassword',
  265. '__token__' => 'token',
  266. ];
  267. $msg = [
  268. 'renewpassword.confirm' => __('Password and confirm password don\'t match')
  269. ];
  270. $data = [
  271. 'oldpassword' => $oldpassword,
  272. 'newpassword' => $newpassword,
  273. 'renewpassword' => $renewpassword,
  274. '__token__' => $token,
  275. ];
  276. $field = [
  277. 'oldpassword' => __('Old password'),
  278. 'newpassword' => __('New password'),
  279. 'renewpassword' => __('Renew password')
  280. ];
  281. $validate = new Validate($rule, $msg, $field);
  282. $result = $validate->check($data);
  283. if (!$result) {
  284. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  285. return false;
  286. }
  287. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  288. if ($ret) {
  289. $this->success(__('Reset password successful'), url('user/login'));
  290. } else {
  291. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  292. }
  293. }
  294. $this->view->assign('title', __('Change password'));
  295. return $this->view->fetch();
  296. }
  297. public function attachment()
  298. {
  299. //设置过滤方法
  300. $this->request->filter(['strip_tags']);
  301. if ($this->request->isAjax()) {
  302. $mimetypeQuery = [];
  303. $where = [];
  304. $filter = $this->request->request('filter');
  305. $filterArr = (array)json_decode($filter, true);
  306. if (isset($filterArr['mimetype']) && preg_match("/(\/|\,|\*)/", $filterArr['mimetype'])) {
  307. $this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['mimetype' => '']))]);
  308. $mimetypeQuery = function ($query) use ($filterArr) {
  309. $mimetypeArr = array_filter(explode(',', $filterArr['mimetype']));
  310. foreach ($mimetypeArr as $index => $item) {
  311. $query->whereOr('mimetype', 'like', '%' . str_replace("/*", "/", $item) . '%');
  312. }
  313. };
  314. } elseif (isset($filterArr['mimetype'])) {
  315. $where['mimetype'] = ['like', '%' . $filterArr['mimetype'] . '%'];
  316. }
  317. if (isset($filterArr['filename'])) {
  318. $where['filename'] = ['like', '%' . $filterArr['filename'] . '%'];
  319. }
  320. if (isset($filterArr['createtime'])) {
  321. $timeArr = explode(' - ', $filterArr['createtime']);
  322. $where['createtime'] = ['between', [strtotime($timeArr[0]), strtotime($timeArr[1])]];
  323. }
  324. $search = $this->request->get('search');
  325. if ($search) {
  326. $where['filename'] = ['like', '%' . $search . '%'];
  327. }
  328. $model = new Attachment();
  329. $offset = $this->request->get("offset", 0);
  330. $limit = $this->request->get("limit", 0);
  331. $total = $model
  332. ->where($where)
  333. ->where($mimetypeQuery)
  334. ->where('user_id', $this->auth->id)
  335. ->order("id", "DESC")
  336. ->count();
  337. $list = $model
  338. ->where($where)
  339. ->where($mimetypeQuery)
  340. ->where('user_id', $this->auth->id)
  341. ->order("id", "DESC")
  342. ->limit($offset, $limit)
  343. ->select();
  344. $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
  345. foreach ($list as $k => &$v) {
  346. $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
  347. }
  348. unset($v);
  349. $result = array("total" => $total, "rows" => $list);
  350. return json($result);
  351. }
  352. $mimetype = $this->request->get('mimetype', '');
  353. $mimetype = substr($mimetype, -1) === '/' ? $mimetype . '*' : $mimetype;
  354. $this->view->assign('mimetype', $mimetype);
  355. $this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList());
  356. return $this->view->fetch();
  357. }
  358. }