User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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', '', 'url_clean');
  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', '', null);
  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', '', 'url_clean');
  123. if (!$url && $referer && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  124. $url = $referer;
  125. }
  126. $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
  127. $this->view->assign('url', $url);
  128. $this->view->assign('title', __('Register'));
  129. return $this->view->fetch();
  130. }
  131. /**
  132. * 会员登录
  133. */
  134. public function login()
  135. {
  136. $url = $this->request->request('url', '', 'url_clean');
  137. if ($this->auth->id) {
  138. $this->success(__('You\'ve logged in, do not login again'), $url ?: url('user/index'));
  139. }
  140. if ($this->request->isPost()) {
  141. $account = $this->request->post('account');
  142. $password = $this->request->post('password', '', null);
  143. $keeplogin = (int)$this->request->post('keeplogin');
  144. $token = $this->request->post('__token__');
  145. $rule = [
  146. 'account' => 'require|length:3,50',
  147. 'password' => 'require|length:6,30',
  148. '__token__' => 'require|token',
  149. ];
  150. $msg = [
  151. 'account.require' => 'Account can not be empty',
  152. 'account.length' => 'Account must be 3 to 50 characters',
  153. 'password.require' => 'Password can not be empty',
  154. 'password.length' => 'Password must be 6 to 30 characters',
  155. ];
  156. $data = [
  157. 'account' => $account,
  158. 'password' => $password,
  159. '__token__' => $token,
  160. ];
  161. $validate = new Validate($rule, $msg);
  162. $result = $validate->check($data);
  163. if (!$result) {
  164. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  165. }
  166. if ($this->auth->login($account, $password)) {
  167. $this->success(__('Logged in successful'), $url ? $url : url('user/index'));
  168. } else {
  169. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  170. }
  171. }
  172. //判断来源
  173. $referer = $this->request->server('HTTP_REFERER', '', 'url_clean');
  174. if (!$url && $referer && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  175. $url = $referer;
  176. }
  177. $this->view->assign('url', $url);
  178. $this->view->assign('title', __('Login'));
  179. return $this->view->fetch();
  180. }
  181. /**
  182. * 退出登录
  183. */
  184. public function logout()
  185. {
  186. if ($this->request->isPost()) {
  187. $this->token();
  188. //退出本站
  189. $this->auth->logout();
  190. $this->success(__('Logout successful'), url('user/index'));
  191. }
  192. $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
  193. $html .= "<script>document.forms['logout_submit'].submit();</script>";
  194. return $html;
  195. }
  196. /**
  197. * 个人信息
  198. */
  199. public function profile()
  200. {
  201. $this->view->assign('title', __('Profile'));
  202. return $this->view->fetch();
  203. }
  204. /**
  205. * 修改密码
  206. */
  207. public function changepwd()
  208. {
  209. if ($this->request->isPost()) {
  210. $oldpassword = $this->request->post("oldpassword", '', null);
  211. $newpassword = $this->request->post("newpassword", '', null);
  212. $renewpassword = $this->request->post("renewpassword", '', null);
  213. $token = $this->request->post('__token__');
  214. $rule = [
  215. 'oldpassword' => 'require|regex:\S{6,30}',
  216. 'newpassword' => 'require|regex:\S{6,30}',
  217. 'renewpassword' => 'require|regex:\S{6,30}|confirm:newpassword',
  218. '__token__' => 'token',
  219. ];
  220. $msg = [
  221. 'renewpassword.confirm' => __('Password and confirm password don\'t match')
  222. ];
  223. $data = [
  224. 'oldpassword' => $oldpassword,
  225. 'newpassword' => $newpassword,
  226. 'renewpassword' => $renewpassword,
  227. '__token__' => $token,
  228. ];
  229. $field = [
  230. 'oldpassword' => __('Old password'),
  231. 'newpassword' => __('New password'),
  232. 'renewpassword' => __('Renew password')
  233. ];
  234. $validate = new Validate($rule, $msg, $field);
  235. $result = $validate->check($data);
  236. if (!$result) {
  237. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  238. }
  239. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  240. if ($ret) {
  241. $this->success(__('Reset password successful'), url('user/login'));
  242. } else {
  243. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  244. }
  245. }
  246. $this->view->assign('title', __('Change password'));
  247. return $this->view->fetch();
  248. }
  249. public function attachment()
  250. {
  251. //设置过滤方法
  252. $this->request->filter(['strip_tags']);
  253. if ($this->request->isAjax()) {
  254. $mimetypeQuery = [];
  255. $where = [];
  256. $filter = $this->request->request('filter');
  257. $filterArr = (array)json_decode($filter, true);
  258. if (isset($filterArr['mimetype']) && preg_match("/(\/|\,|\*)/", $filterArr['mimetype'])) {
  259. $this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['mimetype' => '']))]);
  260. $mimetypeQuery = function ($query) use ($filterArr) {
  261. $mimetypeArr = array_filter(explode(',', $filterArr['mimetype']));
  262. foreach ($mimetypeArr as $index => $item) {
  263. $query->whereOr('mimetype', 'like', '%' . str_replace("/*", "/", $item) . '%');
  264. }
  265. };
  266. } elseif (isset($filterArr['mimetype'])) {
  267. $where['mimetype'] = ['like', '%' . $filterArr['mimetype'] . '%'];
  268. }
  269. if (isset($filterArr['filename'])) {
  270. $where['filename'] = ['like', '%' . $filterArr['filename'] . '%'];
  271. }
  272. if (isset($filterArr['createtime'])) {
  273. $timeArr = explode(' - ', $filterArr['createtime']);
  274. $where['createtime'] = ['between', [strtotime($timeArr[0]), strtotime($timeArr[1])]];
  275. }
  276. $search = $this->request->get('search');
  277. if ($search) {
  278. $where['filename'] = ['like', '%' . $search . '%'];
  279. }
  280. $model = new Attachment();
  281. $offset = $this->request->get("offset", 0);
  282. $limit = $this->request->get("limit", 0);
  283. $total = $model
  284. ->where($where)
  285. ->where($mimetypeQuery)
  286. ->where('user_id', $this->auth->id)
  287. ->order("id", "DESC")
  288. ->count();
  289. $list = $model
  290. ->where($where)
  291. ->where($mimetypeQuery)
  292. ->where('user_id', $this->auth->id)
  293. ->order("id", "DESC")
  294. ->limit($offset, $limit)
  295. ->select();
  296. $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
  297. foreach ($list as $k => &$v) {
  298. $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
  299. }
  300. unset($v);
  301. $result = array("total" => $total, "rows" => $list);
  302. return json($result);
  303. }
  304. $mimetype = $this->request->get('mimetype', '');
  305. $mimetype = substr($mimetype, -1) === '/' ? $mimetype . '*' : $mimetype;
  306. $this->view->assign('mimetype', $mimetype);
  307. $this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList());
  308. return $this->view->fetch();
  309. }
  310. }