User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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', '');
  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', '');
  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|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. 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. //退出本站
  190. $this->auth->logout();
  191. $this->success(__('Logout successful'), url('user/index'));
  192. }
  193. /**
  194. * 个人信息
  195. */
  196. public function profile()
  197. {
  198. $this->view->assign('title', __('Profile'));
  199. return $this->view->fetch();
  200. }
  201. /**
  202. * 修改密码
  203. */
  204. public function changepwd()
  205. {
  206. if ($this->request->isPost()) {
  207. $oldpassword = $this->request->post("oldpassword");
  208. $newpassword = $this->request->post("newpassword");
  209. $renewpassword = $this->request->post("renewpassword");
  210. $token = $this->request->post('__token__');
  211. $rule = [
  212. 'oldpassword' => 'require|length:6,30',
  213. 'newpassword' => 'require|length:6,30',
  214. 'renewpassword' => 'require|length:6,30|confirm:newpassword',
  215. '__token__' => 'token',
  216. ];
  217. $msg = [
  218. 'renewpassword.confirm' => __('Password and confirm password don\'t match')
  219. ];
  220. $data = [
  221. 'oldpassword' => $oldpassword,
  222. 'newpassword' => $newpassword,
  223. 'renewpassword' => $renewpassword,
  224. '__token__' => $token,
  225. ];
  226. $field = [
  227. 'oldpassword' => __('Old password'),
  228. 'newpassword' => __('New password'),
  229. 'renewpassword' => __('Renew password')
  230. ];
  231. $validate = new Validate($rule, $msg, $field);
  232. $result = $validate->check($data);
  233. if (!$result) {
  234. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  235. return false;
  236. }
  237. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  238. if ($ret) {
  239. $this->success(__('Reset password successful'), url('user/login'));
  240. } else {
  241. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  242. }
  243. }
  244. $this->view->assign('title', __('Change password'));
  245. return $this->view->fetch();
  246. }
  247. public function attachment()
  248. {
  249. //设置过滤方法
  250. $this->request->filter(['strip_tags']);
  251. if ($this->request->isAjax()) {
  252. $mimetypeQuery = [];
  253. $filter = $this->request->request('filter');
  254. $filterArr = (array)json_decode($filter, true);
  255. if (isset($filterArr['mimetype']) && preg_match("/[]\,|\*]/", $filterArr['mimetype'])) {
  256. $this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['mimetype' => '']))]);
  257. $mimetypeQuery = function ($query) use ($filterArr) {
  258. $mimetypeArr = explode(',', $filterArr['mimetype']);
  259. foreach ($mimetypeArr as $index => $item) {
  260. if (stripos($item, "/*") !== false) {
  261. $query->whereOr('mimetype', 'like', str_replace("/*", "/", $item) . '%');
  262. } else {
  263. $query->whereOr('mimetype', 'like', '%' . $item . '%');
  264. }
  265. }
  266. };
  267. }
  268. $model = new Attachment();
  269. $offset = $this->request->get("offset", 0);
  270. $limit = $this->request->get("limit", 0);
  271. $total = $model
  272. ->where($mimetypeQuery)
  273. ->where('user_id', $this->auth->id)
  274. ->order("id", "DESC")
  275. ->count();
  276. $list = $model
  277. ->where($mimetypeQuery)
  278. ->where('user_id', $this->auth->id)
  279. ->order("id", "DESC")
  280. ->limit($offset, $limit)
  281. ->select();
  282. $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
  283. foreach ($list as $k => &$v) {
  284. $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
  285. }
  286. unset($v);
  287. $result = array("total" => $total, "rows" => $list);
  288. return json($result);
  289. }
  290. $this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList());
  291. return $this->view->fetch();
  292. }
  293. }