User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 (1) {
  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', '', 'trim');
  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. $this->error('功能已禁用');
  66. /*$username = $this->request->post('username');
  67. $password = $this->request->post('password');
  68. $email = $this->request->post('email');
  69. $mobile = $this->request->post('mobile', '');
  70. $captcha = $this->request->post('captcha');
  71. $token = $this->request->post('__token__');
  72. $rule = [
  73. 'username' => 'require|length:3,30',
  74. 'password' => 'require|length:6,30',
  75. 'email' => 'require|email',
  76. 'mobile' => 'regex:/^1\d{10}$/',
  77. '__token__' => 'require|token',
  78. ];
  79. $msg = [
  80. 'username.require' => 'Username can not be empty',
  81. 'username.length' => 'Username must be 3 to 30 characters',
  82. 'password.require' => 'Password can not be empty',
  83. 'password.length' => 'Password must be 6 to 30 characters',
  84. 'email' => 'Email is incorrect',
  85. 'mobile' => 'Mobile is incorrect',
  86. ];
  87. $data = [
  88. 'username' => $username,
  89. 'password' => $password,
  90. 'email' => $email,
  91. 'mobile' => $mobile,
  92. '__token__' => $token,
  93. ];
  94. //验证码
  95. $captchaResult = true;
  96. $captchaType = config("fastadmin.user_register_captcha");
  97. if ($captchaType) {
  98. if ($captchaType == 'mobile') {
  99. $captchaResult = Sms::check($mobile, $captcha, 'register');
  100. } elseif ($captchaType == 'email') {
  101. $captchaResult = Ems::check($email, $captcha, 'register');
  102. } elseif ($captchaType == 'wechat') {
  103. $captchaResult = WechatCaptcha::check($captcha, 'register');
  104. } elseif ($captchaType == 'text') {
  105. $captchaResult = \think\Validate::is($captcha, 'captcha');
  106. }
  107. }
  108. if (!$captchaResult) {
  109. $this->error(__('Captcha is incorrect'));
  110. }
  111. $validate = new Validate($rule, $msg);
  112. $result = $validate->check($data);
  113. if (!$result) {
  114. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  115. }
  116. if ($this->auth->register($username, $password, $email, $mobile)) {
  117. $this->success(__('Sign up successful'), $url ? $url : url('user/index'));
  118. } else {
  119. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  120. }*/
  121. }
  122. //判断来源
  123. $referer = $this->request->server('HTTP_REFERER');
  124. if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
  125. && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  126. $url = $referer;
  127. }
  128. $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
  129. $this->view->assign('url', $url);
  130. $this->view->assign('title', __('Register'));
  131. return $this->view->fetch();
  132. }
  133. /**
  134. * 会员登录
  135. */
  136. public function login()
  137. {
  138. $url = $this->request->request('url', '', 'trim');
  139. if ($this->auth->id) {
  140. $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index'));
  141. }
  142. if ($this->request->isPost()) {
  143. $this->error('登录已禁用');
  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. if ($this->request->isPost()) {
  192. $this->token();
  193. //退出本站
  194. $this->auth->logout();
  195. $this->success(__('Logout successful'), url('user/index'));
  196. }
  197. $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
  198. $html .= "<script>document.forms['logout_submit'].submit();</script>";
  199. return $html;
  200. }
  201. /**
  202. * 个人信息
  203. */
  204. public function profile()
  205. {
  206. $this->view->assign('title', __('Profile'));
  207. return $this->view->fetch();
  208. }
  209. /**
  210. * 修改密码
  211. */
  212. public function changepwd()
  213. {
  214. if ($this->request->isPost()) {
  215. $oldpassword = $this->request->post("oldpassword");
  216. $newpassword = $this->request->post("newpassword");
  217. $renewpassword = $this->request->post("renewpassword");
  218. $token = $this->request->post('__token__');
  219. $rule = [
  220. 'oldpassword' => 'require|length:6,30',
  221. 'newpassword' => 'require|length:6,30',
  222. 'renewpassword' => 'require|length:6,30|confirm:newpassword',
  223. '__token__' => 'token',
  224. ];
  225. $msg = [
  226. 'renewpassword.confirm' => __('Password and confirm password don\'t match')
  227. ];
  228. $data = [
  229. 'oldpassword' => $oldpassword,
  230. 'newpassword' => $newpassword,
  231. 'renewpassword' => $renewpassword,
  232. '__token__' => $token,
  233. ];
  234. $field = [
  235. 'oldpassword' => __('Old password'),
  236. 'newpassword' => __('New password'),
  237. 'renewpassword' => __('Renew password')
  238. ];
  239. $validate = new Validate($rule, $msg, $field);
  240. $result = $validate->check($data);
  241. if (!$result) {
  242. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  243. return false;
  244. }
  245. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  246. if ($ret) {
  247. $this->success(__('Reset password successful'), url('user/login'));
  248. } else {
  249. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  250. }
  251. }
  252. $this->view->assign('title', __('Change password'));
  253. return $this->view->fetch();
  254. }
  255. public function attachment()
  256. {
  257. //设置过滤方法
  258. $this->request->filter(['strip_tags']);
  259. if ($this->request->isAjax()) {
  260. $mimetypeQuery = [];
  261. $where = [];
  262. $filter = $this->request->request('filter');
  263. $filterArr = (array)json_decode($filter, true);
  264. if (isset($filterArr['mimetype']) && preg_match("/[]\,|\*]/", $filterArr['mimetype'])) {
  265. $this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['mimetype' => '']))]);
  266. $mimetypeQuery = function ($query) use ($filterArr) {
  267. $mimetypeArr = explode(',', $filterArr['mimetype']);
  268. foreach ($mimetypeArr as $index => $item) {
  269. if (stripos($item, "/*") !== false) {
  270. $query->whereOr('mimetype', 'like', str_replace("/*", "/", $item) . '%');
  271. } else {
  272. $query->whereOr('mimetype', 'like', '%' . $item . '%');
  273. }
  274. }
  275. };
  276. } elseif (isset($filterArr['mimetype'])) {
  277. $where['mimetype'] = ['like', '%' . $filterArr['mimetype'] . '%'];
  278. }
  279. if (isset($filterArr['filename'])) {
  280. $where['filename'] = ['like', '%' . $filterArr['filename'] . '%'];
  281. }
  282. if (isset($filterArr['createtime'])) {
  283. $timeArr = explode(' - ', $filterArr['createtime']);
  284. $where['createtime'] = ['between', [strtotime($timeArr[0]), strtotime($timeArr[1])]];
  285. }
  286. $model = new Attachment();
  287. $offset = $this->request->get("offset", 0);
  288. $limit = $this->request->get("limit", 0);
  289. $total = $model
  290. ->where($where)
  291. ->where($mimetypeQuery)
  292. ->where('user_id', $this->auth->id)
  293. ->order("id", "DESC")
  294. ->count();
  295. $list = $model
  296. ->where($where)
  297. ->where($mimetypeQuery)
  298. ->where('user_id', $this->auth->id)
  299. ->order("id", "DESC")
  300. ->limit($offset, $limit)
  301. ->select();
  302. $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
  303. foreach ($list as $k => &$v) {
  304. $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
  305. }
  306. unset($v);
  307. $result = array("total" => $total, "rows" => $list);
  308. return json($result);
  309. }
  310. $this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList());
  311. return $this->view->fetch();
  312. }
  313. }