User.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // | Original reference: https://gitee.com/karson/fastadmin
  12. // +----------------------------------------------------------------------
  13. // +----------------------------------------------------------------------
  14. // | 会员中心
  15. // +----------------------------------------------------------------------
  16. namespace app\index\controller;
  17. use app\common\controller\Frontend;
  18. use app\common\library\Ems;
  19. use app\common\library\Sms;
  20. use app\common\model\Attachment;
  21. use app\common\model\User as UserModel;
  22. use think\exception\ValidateException;
  23. use think\facade\Config;
  24. use think\facade\Cookie;
  25. use think\facade\Event;
  26. class User extends Frontend
  27. {
  28. protected $noNeedLogin = ['login', 'register', 'forget', 'captcha'];
  29. protected $noNeedRight = ['*'];
  30. public function initialize()
  31. {
  32. parent::initialize();
  33. $auth = $this->auth;
  34. if (!Config::get('yzn.user_center')) {
  35. $this->error('会员中心已经关闭', '/');
  36. }
  37. //监听注册登录退出的事件
  38. Event::listen('user_login_successed', function ($user) use ($auth) {
  39. $expire = $this->request->post('keeplogin') ? 30 * 86400 : 0;
  40. Cookie::set('uid', $user->id, $expire);
  41. Cookie::set('token', $auth->getToken(), $expire);
  42. });
  43. Event::listen('user_register_successed', function ($user) use ($auth) {
  44. Cookie::set('uid', $user->id);
  45. Cookie::set('token', $auth->getToken());
  46. });
  47. Event::listen('user_delete_successed', function ($user) use ($auth) {
  48. Cookie::delete('uid');
  49. Cookie::delete('token');
  50. });
  51. Event::listen('user_logout_successed', function ($user) use ($auth) {
  52. Cookie::delete('uid');
  53. Cookie::delete('token');
  54. });
  55. }
  56. /**
  57. * 会员中心
  58. */
  59. public function index()
  60. {
  61. $this->assign('title', '会员中心');
  62. return $this->fetch();
  63. }
  64. //登录页面
  65. public function login()
  66. {
  67. $forward = $this->request->request('forward', '', 'url_clean');
  68. if (!empty($this->auth->id)) {
  69. $this->success("您已经是登陆状态!", $forward ? $forward : url("user/index"));
  70. }
  71. if ($this->request->isPost()) {
  72. //登录验证
  73. $account = $this->request->param('account');
  74. $password = $this->request->param('password');
  75. $token = $this->request->param('__token__');
  76. $rule = [
  77. 'account|账户' => 'require|length:3,30',
  78. 'password|密码' => 'require|length:3,30',
  79. '__token__' => 'require|token',
  80. ];
  81. $data = [
  82. 'account' => $account,
  83. 'password' => $password,
  84. '__token__' => $token,
  85. ];
  86. //验证码
  87. if (config::get("yzn.user_login_captcha")) {
  88. $rule['verify|验证码'] = 'require|captcha';
  89. $data['verify'] = $this->request->param('verify');
  90. }
  91. try {
  92. $this->validate($data, $rule);
  93. } catch (ValidateException $e) {
  94. // 验证失败 输出错误信息
  95. $this->error($e->getError(), null, ['token' => $this->request->buildToken()]);
  96. }
  97. $userInfo = $this->auth->login($account, $password);
  98. if ($userInfo) {
  99. $this->success('登录成功!', $forward ?: url('user/index'));
  100. } else {
  101. //登陆失败
  102. $this->error($this->auth->getError() ?: '账号或者密码错误!', null, ['token' => $this->request->buildToken()]);
  103. }
  104. } else {
  105. //判断来源
  106. $referer = url_clean($this->request->server('HTTP_REFERER'));
  107. if (!$forward && $referer && !preg_match("/(user\/login|user\/register|user\/forget|user\/logout)/i", $referer)) {
  108. $forward = $referer;
  109. }
  110. $this->assign('title', '登录');
  111. $this->assign('forward', $forward);
  112. return $this->fetch();
  113. }
  114. }
  115. //注册页面
  116. public function register()
  117. {
  118. if (!config::get('yzn.user_register')) {
  119. $this->error('会员注册已经关闭');
  120. }
  121. $forward = $this->request->request('forward', '', 'url_clean');
  122. if ($this->auth->id) {
  123. $this->success("您已经是登陆状态,无需注册!", $forward ?: url("user/index"));
  124. }
  125. if ($this->request->isPost()) {
  126. $extend = [];
  127. $data = $this->request->post();
  128. $rule = [
  129. 'verify|验证码' => 'require|captcha',
  130. 'username|用户名' => 'unique:user|require|alphaDash|length:3,20',
  131. 'nickname|昵称' => 'unique:user|length:3,20',
  132. 'mobile|手机' => 'require|unique:user|mobile',
  133. 'password|密码' => 'require|length:3,20',
  134. 'email|邮箱' => 'unique:user|require|email',
  135. '__token__' => 'require|token',
  136. ];
  137. if (config::get('yzn.user_password_confirm')) {
  138. $rule['password|密码'] = "require|length:3,20|confirm";
  139. }
  140. if (config::get('yzn.user_mobile_verify')) {
  141. $rule['captcha_mobile|手机验证码'] = "require";
  142. }
  143. if (config::get('yzn.user_email_verify')) {
  144. $rule['captcha_email|邮箱验证码'] = "require";
  145. }
  146. try {
  147. $this->validate($data, $rule);
  148. } catch (ValidateException $e) {
  149. // 验证失败 输出错误信息
  150. $this->error($e->getError(), null, ['token' => $this->request->buildToken()]);
  151. }
  152. if (config::get('yzn.user_mobile_verify')) {
  153. $result = Sms::check($data['mobile'], $data['captcha_mobile'], 'register');
  154. if (!$result) {
  155. $this->error('手机验证码错误!');
  156. }
  157. $extend['ischeck_mobile'] = 1;
  158. }
  159. if (config::get('yzn.user_email_verify')) {
  160. $result = Ems::check($data['email'], $data['captcha_email'], 'register');
  161. if (!$result) {
  162. $this->error('邮箱验证码错误!');
  163. }
  164. $extend['ischeck_email'] = 1;
  165. }
  166. $extend['nickname'] = $data['nickname'] ?? $data['username'];
  167. if ($this->auth->register($data['username'], $data['password'], $data['email'], $data['mobile'], $extend)) {
  168. $this->success('会员注册成功!', $forward ? $forward : url('user/index'));
  169. } else {
  170. $this->error($this->auth->getError() ?: '帐号注册失败!', null, ['token' => $this->request->buildToken()]);
  171. }
  172. } else {
  173. //判断来源
  174. $referer = url_clean($this->request->server('HTTP_REFERER'));
  175. if (!$forward && $referer && !preg_match("/(user\/login|user\/register|user\/forget|user\/logout)/i", $referer)) {
  176. $forward = $referer;
  177. }
  178. $this->view->assign('title', '注册');
  179. $this->assign('forward', $forward);
  180. return $this->fetch();
  181. }
  182. }
  183. /**
  184. *忘记密码
  185. */
  186. public function forget()
  187. {
  188. if ($this->request->isPost()) {
  189. $type = $this->request->param("type");
  190. $mobile = $this->request->param("mobile");
  191. $email = $this->request->param("email");
  192. $newpassword = $this->request->param("newpassword");
  193. $captcha = $this->request->param("captcha");
  194. $token = $this->request->param('__token__');
  195. // 验证数据
  196. $data = [
  197. 'mobile' => $mobile,
  198. 'email' => $email,
  199. 'captcha' => $captcha,
  200. 'newpassword' => $newpassword,
  201. '__token__' => $token,
  202. ];
  203. $rule = [
  204. 'mobile|手机号' => 'require|mobile',
  205. 'email|邮箱' => 'require|email',
  206. 'captcha|验证码' => 'require|number',
  207. 'newpassword|新密码' => 'require|length:6,30',
  208. '__token__' => 'require|token',
  209. ];
  210. if ($type == "mobile") {
  211. unset($rule['email|邮箱']);
  212. } else {
  213. unset($rule['mobile|手机号']);
  214. }
  215. try {
  216. $this->validate($data, $rule);
  217. } catch (ValidateException $e) {
  218. // 验证失败 输出错误信息
  219. $this->error($e->getError(), null, ['token' => $this->request->buildToken()]);
  220. }
  221. if ($type == 'mobile') {
  222. $user = UserModel::where('mobile', $mobile)->find();
  223. if (!$user) {
  224. $this->error('用户不存在!', null, ['token' => $this->request->buildToken()]);
  225. }
  226. $result = Sms::check($mobile, $captcha, 'resetpwd');
  227. if (!$result) {
  228. $this->error('验证码错误!', null, ['token' => $this->request->buildToken()]);
  229. }
  230. } elseif ($type == 'email') {
  231. $user = UserModel::where('email', $email)->find();
  232. if (!$user) {
  233. $this->error('用户不存在!', null, ['token' => $this->request->buildToken()]);
  234. }
  235. $result = Ems::check($email, $captcha, 'resetpwd');
  236. if (!$result) {
  237. $this->error('验证码错误!', null, ['token' => $this->request->buildToken()]);
  238. }
  239. } else {
  240. $this->error('类型错误!', null, ['token' => $this->request->buildToken()]);
  241. }
  242. $this->auth->direct($user->id);
  243. $res = $this->auth->changepwd($newpassword, '', true);
  244. if (!$res) {
  245. $this->error($this->auth->getError());
  246. }
  247. $this->success('重置成功!');
  248. } else {
  249. $this->assign('title', '密码忘记');
  250. return $this->fetch();
  251. }
  252. }
  253. /**
  254. * 个人信息
  255. */
  256. public function profile()
  257. {
  258. $this->assign('title', '账户设置');
  259. return $this->fetch();
  260. }
  261. /**
  262. * 修改密码
  263. */
  264. public function changepwd()
  265. {
  266. if ($this->request->isPost()) {
  267. $oldpassword = $this->request->post("oldpassword", '', null);
  268. $newpassword = $this->request->post("newpassword", '', null);
  269. $renewpassword = $this->request->post("renewpassword", '', null);
  270. $token = $this->request->post('__token__');
  271. $rule = [
  272. 'oldpassword|旧密码' => 'require|regex:\S{6,30}',
  273. 'newpassword|新密码' => 'require|regex:\S{6,30}',
  274. 'renewpassword|确认密码' => 'require|regex:\S{6,30}|confirm:newpassword',
  275. '__token__' => 'token',
  276. ];
  277. $msg = [
  278. 'renewpassword.confirm' => '两次输入的密码不一致',
  279. ];
  280. $data = [
  281. 'oldpassword' => $oldpassword,
  282. 'newpassword' => $newpassword,
  283. 'renewpassword' => $renewpassword,
  284. '__token__' => $token,
  285. ];
  286. try {
  287. $this->validate($data, $rule, $msg);
  288. } catch (ValidateException $e) {
  289. // 验证失败 输出错误信息
  290. $this->error($e->getError(), null, ['token' => $this->request->buildToken()]);
  291. }
  292. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  293. if ($ret) {
  294. $this->success('修改成功!', url('user/login'));
  295. } else {
  296. $this->error($this->auth->getError(), null, ['token' => $this->request->buildToken()]);
  297. }
  298. }
  299. $this->assign('title', '修改密码');
  300. return $this->fetch();
  301. }
  302. //手动退出登录
  303. public function logout()
  304. {
  305. $this->auth->logout();
  306. $this->success('注销成功!', url("user/login"));
  307. }
  308. //验证码
  309. public function captcha()
  310. {
  311. return \think\captcha\facade\Captcha::create();
  312. }
  313. //附件列表页
  314. public function attachment()
  315. {
  316. $this->request->filter('trim,strip_tags');
  317. if ($this->request->isAjax()) {
  318. $mimetypeQuery = [];
  319. $where = [];
  320. $filter = $this->request->request('filter');
  321. $filterArr = (array) json_decode($filter, true);
  322. if (isset($filterArr['mime']) && preg_match("/(\/|\,|\*)/", $filterArr['mime'])) {
  323. $this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['mime' => '']))]);
  324. $mimetypeQuery = function ($query) use ($filterArr) {
  325. $mimetypeArr = array_filter(explode(',', $filterArr['mime']));
  326. foreach ($mimetypeArr as $index => $item) {
  327. $query->whereOr('mime', 'like', '%' . str_replace("/*", "/", $item) . '%');
  328. }
  329. };
  330. } elseif (isset($filterArr['mime'])) {
  331. $where[] = ['mime', 'like', '%' . $filterArr['mime'] . '%'];
  332. }
  333. if (isset($filterArr['name'])) {
  334. $where[] = ['name', 'like', '%' . $filterArr['name'] . '%'];
  335. }
  336. if (isset($filterArr['create_time'])) {
  337. $timeArr = explode(' - ', $filterArr['create_time']);
  338. $where[] = ['create_time', 'between', [strtotime($timeArr[0]), strtotime($timeArr[1])]];
  339. }
  340. $search = $this->request->get('search');
  341. if ($search) {
  342. $where[] = ['name', 'like', '%' . $search . '%'];
  343. }
  344. $page = $this->request->get("page/d", 1);
  345. $limit = $this->request->get("limit/d", 0);
  346. $total = Attachment::where($where)
  347. ->where($mimetypeQuery)
  348. ->where('user_id', $this->auth->id)
  349. ->order('id', 'desc')
  350. ->count();
  351. $list = Attachment::where($where)
  352. ->where($mimetypeQuery)
  353. ->where('user_id', $this->auth->id)
  354. ->page($page, $limit)
  355. ->order('id', 'desc')
  356. ->select();
  357. $result = ["code" => 0, "count" => $total, "data" => $list];
  358. return json($result);
  359. }
  360. $mimetype = $this->request->param('mime', '');
  361. $mimetype = substr($mimetype, -1) === '/' ? $mimetype . '*' : $mimetype;
  362. $this->assignconfig('mimetype', $mimetype);
  363. return $this->fetch();
  364. }
  365. }