User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. //echo "<h1>操作被禁止!<h1>";
  25. // exit(0);
  26. $auth = $this->auth;
  27. if (!Config::get('fastadmin.usercenter')) {
  28. $this->error(__('User center already closed'));
  29. }
  30. //监听注册登录退出的事件
  31. Hook::add('user_login_successed', function ($user) use ($auth) {
  32. $expire = input('post.keeplogin') ? 30 * 86400 : 0;
  33. Cookie::set('uid', $user->id, $expire);
  34. Cookie::set('token', $auth->getToken(), $expire);
  35. });
  36. Hook::add('user_register_successed', function ($user) use ($auth) {
  37. Cookie::set('uid', $user->id);
  38. Cookie::set('token', $auth->getToken());
  39. });
  40. Hook::add('user_delete_successed', function ($user) use ($auth) {
  41. Cookie::delete('uid');
  42. Cookie::delete('token');
  43. });
  44. Hook::add('user_logout_successed', function ($user) use ($auth) {
  45. Cookie::delete('uid');
  46. Cookie::delete('token');
  47. });
  48. }
  49. /**
  50. * 会员中心
  51. */
  52. public function index()
  53. {
  54. $this->view->assign('title', __('User center'));
  55. return $this->view->fetch();
  56. }
  57. /**
  58. * 注册会员
  59. */
  60. public function register()
  61. {
  62. $url = $this->request->request('url', '', 'trim');
  63. if ($this->auth->id) {
  64. $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index'));
  65. }
  66. if ($this->request->isPost()) {
  67. $username = $this->request->post('username');
  68. $password = $this->request->post('password');
  69. $email = $this->request->post('email');
  70. $mobile = $this->request->post('mobile', '');
  71. $captcha = $this->request->post('captcha');
  72. $token = $this->request->post('__token__');
  73. $rule = [
  74. 'username' => 'require|length:3,30',
  75. 'password' => 'require|length:6,30',
  76. 'email' => 'require|email',
  77. 'mobile' => 'regex:/^1\d{10}$/',
  78. '__token__' => 'require|token',
  79. ];
  80. $msg = [
  81. 'username.require' => 'Username can not be empty',
  82. 'username.length' => 'Username must be 3 to 30 characters',
  83. 'password.require' => 'Password can not be empty',
  84. 'password.length' => 'Password must be 6 to 30 characters',
  85. 'email' => 'Email is incorrect',
  86. 'mobile' => 'Mobile is incorrect',
  87. ];
  88. $data = [
  89. 'username' => $username,
  90. 'password' => $password,
  91. 'email' => $email,
  92. 'mobile' => $mobile,
  93. '__token__' => $token,
  94. ];
  95. //验证码
  96. $captchaResult = true;
  97. $captchaType = config("fastadmin.user_register_captcha");
  98. if ($captchaType) {
  99. if ($captchaType == 'mobile') {
  100. $captchaResult = Sms::check($mobile, $captcha, 'register');
  101. } elseif ($captchaType == 'email') {
  102. $captchaResult = Ems::check($email, $captcha, 'register');
  103. } elseif ($captchaType == 'wechat') {
  104. $captchaResult = WechatCaptcha::check($captcha, 'register');
  105. } elseif ($captchaType == 'text') {
  106. $captchaResult = \think\Validate::is($captcha, 'captcha');
  107. }
  108. }
  109. if (!$captchaResult) {
  110. $this->error(__('Captcha is incorrect'));
  111. }
  112. $validate = new Validate($rule, $msg);
  113. $result = $validate->check($data);
  114. if (!$result) {
  115. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  116. }
  117. if ($this->auth->register($username, $password, $email, $mobile)) {
  118. $this->success(__('Sign up successful'), $url ? $url : url('user/index'));
  119. } else {
  120. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  121. }
  122. }
  123. //判断来源
  124. $referer = $this->request->server('HTTP_REFERER');
  125. if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
  126. && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  127. $url = $referer;
  128. }
  129. $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
  130. $this->view->assign('url', $url);
  131. $this->view->assign('title', __('Register'));
  132. return $this->view->fetch();
  133. }
  134. /**
  135. * 会员登录
  136. */
  137. public function login()
  138. {
  139. $url = $this->request->request('url', '', 'trim');
  140. if ($this->auth->id) {
  141. $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index'));
  142. }
  143. if ($this->request->isPost()) {
  144. $account = $this->request->post('account');
  145. $password = $this->request->post('password');
  146. $keeplogin = (int)$this->request->post('keeplogin');
  147. $token = $this->request->post('__token__');
  148. $rule = [
  149. 'account' => 'require|length:3,50',
  150. 'password' => 'require|length:6,30',
  151. '__token__' => 'require|token',
  152. ];
  153. $msg = [
  154. 'account.require' => 'Account can not be empty',
  155. 'account.length' => 'Account must be 3 to 50 characters',
  156. 'password.require' => 'Password can not be empty',
  157. 'password.length' => 'Password must be 6 to 30 characters',
  158. ];
  159. $data = [
  160. 'account' => $account,
  161. 'password' => $password,
  162. '__token__' => $token,
  163. ];
  164. $validate = new Validate($rule, $msg);
  165. $result = $validate->check($data);
  166. if (!$result) {
  167. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  168. return false;
  169. }
  170. if ($this->auth->login($account, $password)) {
  171. $this->success(__('Logged in successful'), $url ? $url : url('user/index'));
  172. } else {
  173. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  174. }
  175. }
  176. //判断来源
  177. $referer = $this->request->server('HTTP_REFERER');
  178. if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
  179. && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  180. $url = $referer;
  181. }
  182. $this->view->assign('url', $url);
  183. $this->view->assign('title', __('Login'));
  184. return $this->view->fetch();
  185. }
  186. /**
  187. * 退出登录
  188. */
  189. public function logout()
  190. {
  191. //退出本站
  192. $this->auth->logout();
  193. $this->success(__('Logout successful'), url('user/index'));
  194. }
  195. /**
  196. * 个人信息
  197. */
  198. public function profile()
  199. {
  200. $this->view->assign('title', __('Profile'));
  201. return $this->view->fetch();
  202. }
  203. /**
  204. * 修改密码
  205. */
  206. public function changepwd()
  207. {
  208. if ($this->request->isPost()) {
  209. $oldpassword = $this->request->post("oldpassword");
  210. $newpassword = $this->request->post("newpassword");
  211. $renewpassword = $this->request->post("renewpassword");
  212. $token = $this->request->post('__token__');
  213. $rule = [
  214. 'oldpassword' => 'require|length:6,30',
  215. 'newpassword' => 'require|length:6,30',
  216. 'renewpassword' => 'require|length:6,30|confirm:newpassword',
  217. '__token__' => 'token',
  218. ];
  219. $msg = [
  220. 'renewpassword.confirm' => __('Password and confirm password don\'t match')
  221. ];
  222. $data = [
  223. 'oldpassword' => $oldpassword,
  224. 'newpassword' => $newpassword,
  225. 'renewpassword' => $renewpassword,
  226. '__token__' => $token,
  227. ];
  228. $field = [
  229. 'oldpassword' => __('Old password'),
  230. 'newpassword' => __('New password'),
  231. 'renewpassword' => __('Renew password')
  232. ];
  233. $validate = new Validate($rule, $msg, $field);
  234. $result = $validate->check($data);
  235. if (!$result) {
  236. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  237. return false;
  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 = explode(',', $filterArr['mimetype']);
  262. foreach ($mimetypeArr as $index => $item) {
  263. if (stripos($item, "/*") !== false) {
  264. $query->whereOr('mimetype', 'like', str_replace("/*", "/", $item) . '%');
  265. } else {
  266. $query->whereOr('mimetype', 'like', '%' . $item . '%');
  267. }
  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. $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. $this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList());
  305. return $this->view->fetch();
  306. }
  307. }