User.php 12 KB

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