User.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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\Db;
  11. use think\Hook;
  12. use think\Session;
  13. use think\Validate;
  14. /**
  15. * 会员中心
  16. */
  17. class User extends Frontend
  18. {
  19. protected $layout = 'default';
  20. protected $noNeedLogin = ['login', 'register', 'third'];
  21. protected $noNeedRight = ['*'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $auth = $this->auth;
  26. if (!Config::get('fastadmin.usercenter')) {
  27. $this->error(__('User center already closed'), '/');
  28. }
  29. //监听注册登录退出的事件
  30. Hook::add('user_login_successed', function ($user) use ($auth) {
  31. $expire = input('post.keeplogin') ? 30 * 86400 : 0;
  32. Cookie::set('uid', $user->id, $expire);
  33. Cookie::set('token', $auth->getToken(), $expire);
  34. });
  35. Hook::add('user_register_successed', function ($user) use ($auth) {
  36. Cookie::set('uid', $user->id);
  37. Cookie::set('token', $auth->getToken());
  38. });
  39. Hook::add('user_delete_successed', function ($user) use ($auth) {
  40. Cookie::delete('uid');
  41. Cookie::delete('token');
  42. });
  43. Hook::add('user_logout_successed', function ($user) use ($auth) {
  44. Cookie::delete('uid');
  45. Cookie::delete('token');
  46. });
  47. }
  48. /**
  49. * 会员中心
  50. */
  51. public function index()
  52. {
  53. // // 打印用户信息到页面(调试用)
  54. // $userInfo = $this->auth->getUser();
  55. // echo '<pre>';
  56. // echo '用户ID: ' . $this->auth->id . '<br>';
  57. // echo '用户信息(通过auth->getUser()获取): <br>';
  58. // print_r($userInfo->toArray());
  59. // echo '<br>用户信息(通过auth->getUserinfo()获取): <br>';
  60. // print_r($this->auth->getUserinfo());
  61. // echo '</pre>';
  62. $this->view->assign('title', __('User center'));
  63. return $this->view->fetch();
  64. }
  65. /**
  66. * 注册会员
  67. */
  68. public function register()
  69. {
  70. $url = $this->request->request('url', '', 'url_clean');
  71. if ($this->auth->id) {
  72. $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/template'));
  73. }
  74. if ($this->request->isPost()) {
  75. $username = $this->request->post('username');
  76. $password = $this->request->post('password', '', null);
  77. $email = $this->request->post('email');
  78. $mobile = $this->request->post('mobile', '');
  79. $captcha = $this->request->post('captcha');
  80. $token = $this->request->post('__token__');
  81. $rule = [
  82. 'username' => 'require|length:3,30',
  83. 'password' => 'require|length:6,30',
  84. 'email' => 'require|email',
  85. 'mobile' => 'regex:/^1\d{10}$/',
  86. '__token__' => 'require|token',
  87. ];
  88. $msg = [
  89. 'username.require' => 'Username can not be empty',
  90. 'username.length' => 'Username must be 3 to 30 characters',
  91. 'password.require' => 'Password can not be empty',
  92. 'password.length' => 'Password must be 6 to 30 characters',
  93. 'email' => 'Email is incorrect',
  94. 'mobile' => 'Mobile is incorrect',
  95. ];
  96. $data = [
  97. 'username' => $username,
  98. 'password' => $password,
  99. 'email' => $email,
  100. 'mobile' => $mobile,
  101. '__token__' => $token,
  102. ];
  103. //验证码
  104. $captchaResult = true;
  105. $captchaType = config("fastadmin.user_register_captcha");
  106. if ($captchaType) {
  107. if ($captchaType == 'mobile') {
  108. $captchaResult = Sms::check($mobile, $captcha, 'register');
  109. } elseif ($captchaType == 'email') {
  110. $captchaResult = Ems::check($email, $captcha, 'register');
  111. } elseif ($captchaType == 'wechat') {
  112. $captchaResult = WechatCaptcha::check($captcha, 'register');
  113. } elseif ($captchaType == 'text') {
  114. $captchaResult = \think\Validate::is($captcha, 'captcha');
  115. }
  116. }
  117. if (!$captchaResult) {
  118. $this->error(__('Captcha is incorrect'));
  119. }
  120. $validate = new Validate($rule, $msg);
  121. $result = $validate->check($data);
  122. if (!$result) {
  123. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  124. }
  125. if ($this->auth->register($username, $password, $email, $mobile)) {
  126. $this->success(__('Sign up successful'), $url ? $url : url('user/template'));
  127. } else {
  128. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  129. }
  130. }
  131. //判断来源
  132. $referer = $this->request->server('HTTP_REFERER', '', 'url_clean');
  133. if (!$url && $referer && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  134. $url = $referer;
  135. }
  136. $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
  137. $this->view->assign('url', $url);
  138. $this->view->assign('title', __('Register'));
  139. return $this->view->fetch();
  140. }
  141. /**
  142. * 会员登录
  143. */
  144. public function login()
  145. {
  146. $url = $this->request->request('url', '', 'url_clean');
  147. if ($this->auth->id) {
  148. $this->success(__('You\'ve logged in, do not login again'), $url ?: url('user/index'));
  149. }
  150. if ($this->request->isPost()) {
  151. $account = $this->request->post('account');
  152. $password = $this->request->post('password', '', null);
  153. $keeplogin = (int)$this->request->post('keeplogin');
  154. $token = $this->request->post('__token__');
  155. $rule = [
  156. 'account' => 'require|length:3,50',
  157. 'password' => 'require|length:6,30',
  158. '__token__' => 'require|token',
  159. ];
  160. $msg = [
  161. 'account.require' => 'Account can not be empty',
  162. 'account.length' => 'Account must be 3 to 50 characters',
  163. 'password.require' => 'Password can not be empty',
  164. 'password.length' => 'Password must be 6 to 30 characters',
  165. ];
  166. $data = [
  167. 'account' => $account,
  168. 'password' => $password,
  169. '__token__' => $token,
  170. ];
  171. $validate = new Validate($rule, $msg);
  172. $result = $validate->check($data);
  173. if (!$result) {
  174. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  175. }
  176. if ($this->auth->login($account, $password)) {
  177. $this->success(__('Logged in successful'), $url ? $url : url('user/index'));
  178. } else {
  179. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  180. }
  181. }
  182. //判断来源
  183. $referer = $this->request->server('HTTP_REFERER', '', 'url_clean');
  184. if (!$url && $referer && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  185. $url = $referer;
  186. }
  187. $this->view->assign('url', $url);
  188. $this->view->assign('title', __('Login'));
  189. return $this->view->fetch();
  190. }
  191. /**
  192. * 退出登录
  193. */
  194. public function logout()
  195. {
  196. if ($this->request->isPost()) {
  197. $this->token();
  198. //退出本站
  199. $this->auth->logout();
  200. $this->success(__('Logout successful'), url('user/index'));
  201. }
  202. $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
  203. $html .= "<script>document.forms['logout_submit'].submit();</script>";
  204. return $html;
  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", '', null);
  221. $newpassword = $this->request->post("newpassword", '', null);
  222. $renewpassword = $this->request->post("renewpassword", '', null);
  223. $token = $this->request->post('__token__');
  224. $rule = [
  225. 'oldpassword' => 'require|regex:\S{6,30}',
  226. 'newpassword' => 'require|regex:\S{6,30}',
  227. 'renewpassword' => 'require|regex:\S{6,30}|confirm:newpassword',
  228. '__token__' => 'token',
  229. ];
  230. $msg = [
  231. 'renewpassword.confirm' => __('Password and confirm password don\'t match')
  232. ];
  233. $data = [
  234. 'oldpassword' => $oldpassword,
  235. 'newpassword' => $newpassword,
  236. 'renewpassword' => $renewpassword,
  237. '__token__' => $token,
  238. ];
  239. $field = [
  240. 'oldpassword' => __('Old password'),
  241. 'newpassword' => __('New password'),
  242. 'renewpassword' => __('Renew password')
  243. ];
  244. $validate = new Validate($rule, $msg, $field);
  245. $result = $validate->check($data);
  246. if (!$result) {
  247. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  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. $where = [];
  266. $filter = $this->request->request('filter');
  267. $filterArr = (array)json_decode($filter, true);
  268. if (isset($filterArr['mimetype']) && preg_match("/(\/|\,|\*)/", $filterArr['mimetype'])) {
  269. $this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['mimetype' => '']))]);
  270. $mimetypeQuery = function ($query) use ($filterArr) {
  271. $mimetypeArr = array_filter(explode(',', $filterArr['mimetype']));
  272. foreach ($mimetypeArr as $index => $item) {
  273. $query->whereOr('mimetype', 'like', '%' . str_replace("/*", "/", $item) . '%');
  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. $search = $this->request->get('search');
  287. if ($search) {
  288. $where['filename'] = ['like', '%' . $search . '%'];
  289. }
  290. $model = new Attachment();
  291. $offset = $this->request->get("offset", 0);
  292. $limit = $this->request->get("limit", 0);
  293. $total = $model
  294. ->where($where)
  295. ->where($mimetypeQuery)
  296. ->where('user_id', $this->auth->id)
  297. ->order("id", "DESC")
  298. ->count();
  299. $list = $model
  300. ->where($where)
  301. ->where($mimetypeQuery)
  302. ->where('user_id', $this->auth->id)
  303. ->order("id", "DESC")
  304. ->limit($offset, $limit)
  305. ->select();
  306. $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
  307. foreach ($list as $k => &$v) {
  308. $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
  309. }
  310. unset($v);
  311. $result = array("total" => $total, "rows" => $list);
  312. return json($result);
  313. }
  314. $mimetype = $this->request->get('mimetype', '');
  315. $mimetype = substr($mimetype, -1) === '/' ? $mimetype . '*' : $mimetype;
  316. $this->view->assign('mimetype', $mimetype);
  317. $this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList());
  318. return $this->view->fetch();
  319. }
  320. public function template(){
  321. // 获取搜索参数
  322. $keyword = input('param.keyword', '', 'trim');
  323. $type = input('param.type', '', 'trim');
  324. // 构建查询条件
  325. $where = [];
  326. if (!empty($keyword)) {
  327. $where['template_name|type'] = ['like', '%' . $keyword . '%'];
  328. }
  329. if (!empty($type)) {
  330. $where['type'] = ['like', '%' . $type . '%'];
  331. }
  332. // 查询模版表
  333. $products = Db::name('product_template')->where($where)->order('id desc')->where('mod_rq', null)->select();
  334. // // 检查并修正图片URL
  335. // foreach ($products as &$val) {
  336. // // 如果URL不包含http,添加baseUrl
  337. // if (!empty($val['template_image_url']) && strpos($val['template_image_url'], 'http') !== 0) {
  338. // // 使用相对路径或根据实际情况设置正确的URL
  339. // // 这里假设图片存储在public/uploads目录下
  340. // $val['template_image_url'] = '/uploads' . $val['template_image_url'];
  341. // }
  342. // }
  343. // 判断是否为AJAX请求
  344. if (request()->isAjax()) {
  345. return json([
  346. 'code' => 0,
  347. 'msg' => 'success',
  348. 'data' => $products
  349. ]);
  350. }
  351. // 分配数据到视图
  352. $this->view->assign('products', $products);
  353. $this->view->assign('keyword', $keyword);
  354. $this->view->assign('type', $type);
  355. return $this->view->fetch();
  356. }
  357. public function text_to_image(){
  358. return $this->view->fetch();
  359. }
  360. public function text_to_video(){
  361. return $this->view->fetch();
  362. }
  363. public function diagrams_list(){
  364. // 获取作品ID
  365. $id = input('param.id', 0, 'intval');
  366. if (empty($id)) {
  367. $this->error(__('Invalid parameter'));
  368. }
  369. //获取用户信息
  370. $user = $this->auth->getUserinfo();
  371. $user_info = Db::name('user')->where('id',$user['id'])->find();
  372. // 根据ID查询作品信息,允许查询已删除的作品(mod_rq不为null)
  373. $product = Db::name('product_template')->where('id', $id)->find();
  374. if (empty($product)) {
  375. $this->error(__('Product not found'));
  376. }
  377. // 将作品信息传递给视图
  378. $this->view->assign('user_info', $user_info);
  379. $this->view->assign('product', $product);
  380. return $this->view->fetch();
  381. }
  382. public function diagrams(){
  383. $user = $this->auth->getUserinfo();
  384. //获取用户信息
  385. // $user_info = Db::name('user')->where('id',$user['id'])->find();
  386. // 构建查询条件
  387. $where = [];
  388. if (!empty($keyword)) {
  389. $where['template_name|type'] = ['like', '%' . $keyword . '%'];
  390. }
  391. if (!empty($type)) {
  392. $where['type'] = ['like', '%' . $type . '%'];
  393. }
  394. // 查询模糊的模版信息
  395. $products = Db::name('product_template')
  396. ->where('user_id',$user['id'])
  397. ->where('mod_rq', null)
  398. ->order('id desc')->select();
  399. $this->view->assign('products', $products);
  400. return $this->view->fetch();
  401. }
  402. /**
  403. * 软删除 保留数据
  404. * 只修改 mod_rq
  405. * */
  406. public function diagrams_del(){
  407. $params = $this->request->param();
  408. $updata_products = Db::name('product_template')
  409. ->where('id',$params['id'])
  410. ->update(['mod_rq' => date('Y-m-d H:i:s')]);
  411. if ($updata_products) {
  412. return json(['code' => 0, 'msg' => '删除成功,可在回收站恢复']);
  413. } else {
  414. return json(['code' => 1, 'msg' => '删除失败']);
  415. }
  416. }
  417. /**
  418. * 真删除 删除回收站
  419. * */
  420. public function diagrams_delete(){
  421. $params = $this->request->param();
  422. $user = $this->auth->getUserinfo();
  423. // 检查权限
  424. $product = Db::name('product_template')->where('id', $params['id'])->where('user_id', $user['id'])->find();
  425. if (!$product) {
  426. return json(['code' => 1, 'msg' => '无权限操作此作品']);
  427. }
  428. $delete_result = Db::name('product_template')
  429. ->where('id', $params['id'])
  430. ->where('user_id', $user['id'])
  431. ->delete();
  432. if ($delete_result) {
  433. return json(['code' => 0, 'msg' => '彻底删除成功']);
  434. } else {
  435. return json(['code' => 1, 'msg' => '删除失败']);
  436. }
  437. }
  438. /**
  439. * 查询回收站数据
  440. * */
  441. public function recycle(){
  442. $user = $this->auth->getUserinfo();
  443. $products = Db::name('product_template')
  444. ->where('user_id',$user['id'])
  445. ->whereNotNull('mod_rq')
  446. ->order('id desc')->select();
  447. $this->view->assign('products', $products);
  448. return $this->view->fetch();
  449. }
  450. /**
  451. * 恢复回收站数据
  452. * */
  453. public function diagrams_restore(){
  454. $params = $this->request->param();
  455. $user = $this->auth->getUserinfo();
  456. // 检查权限
  457. $product = Db::name('product_template')->where('id', $params['id'])->where('user_id', $user['id'])->find();
  458. if (!$product) {
  459. return json(['code' => 1, 'msg' => '无权限操作此作品']);
  460. }
  461. $update_result = Db::name('product_template')
  462. ->where('id', $params['id'])
  463. ->where('user_id', $user['id'])
  464. ->update(['mod_rq' => null]);
  465. if ($update_result) {
  466. return json(['code' => 0, 'msg' => '恢复成功']);
  467. } else {
  468. return json(['code' => 1, 'msg' => '恢复失败']);
  469. }
  470. }
  471. }