User.php 13 KB

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