User.php 12 KB

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