User.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. $this->view->assign('title', __('User center'));
  54. return $this->view->fetch();
  55. }
  56. /**
  57. * 注册会员
  58. */
  59. public function register()
  60. {
  61. $url = $this->request->request('url', '', 'url_clean');
  62. if ($this->auth->id) {
  63. $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index'));
  64. }
  65. if ($this->request->isPost()) {
  66. $username = $this->request->post('username');
  67. $password = $this->request->post('password', '', null);
  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', '', 'url_clean');
  124. if (!$url && $referer && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  125. $url = $referer;
  126. }
  127. $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
  128. $this->view->assign('url', $url);
  129. $this->view->assign('title', __('Register'));
  130. return $this->view->fetch();
  131. }
  132. /**
  133. * 会员登录
  134. */
  135. public function login()
  136. {
  137. $url = $this->request->request('url', '', 'url_clean');
  138. if ($this->auth->id) {
  139. $this->success(__('You\'ve logged in, do not login again'), $url ?: url('user/index'));
  140. }
  141. if ($this->request->isPost()) {
  142. $account = $this->request->post('account');
  143. $password = $this->request->post('password', '', null);
  144. $keeplogin = (int)$this->request->post('keeplogin');
  145. $token = $this->request->post('__token__');
  146. $rule = [
  147. 'account' => 'require|length:3,50',
  148. 'password' => 'require|length:6,30',
  149. '__token__' => 'require|token',
  150. ];
  151. $msg = [
  152. 'account.require' => 'Account can not be empty',
  153. 'account.length' => 'Account must be 3 to 50 characters',
  154. 'password.require' => 'Password can not be empty',
  155. 'password.length' => 'Password must be 6 to 30 characters',
  156. ];
  157. $data = [
  158. 'account' => $account,
  159. 'password' => $password,
  160. '__token__' => $token,
  161. ];
  162. $validate = new Validate($rule, $msg);
  163. $result = $validate->check($data);
  164. if (!$result) {
  165. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  166. }
  167. if ($this->auth->login($account, $password)) {
  168. $this->success(__('Logged in successful'), $url ? $url : url('user/index'));
  169. } else {
  170. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  171. }
  172. }
  173. //判断来源
  174. $referer = $this->request->server('HTTP_REFERER', '', 'url_clean');
  175. if (!$url && $referer && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  176. $url = $referer;
  177. }
  178. $this->view->assign('url', $url);
  179. $this->view->assign('title', __('Login'));
  180. return $this->view->fetch();
  181. }
  182. /**
  183. * 退出登录
  184. */
  185. public function logout()
  186. {
  187. if ($this->request->isPost()) {
  188. $this->token();
  189. //退出本站
  190. $this->auth->logout();
  191. $this->success(__('Logout successful'), url('user/index'));
  192. }
  193. $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
  194. $html .= "<script>document.forms['logout_submit'].submit();</script>";
  195. return $html;
  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", '', null);
  212. $newpassword = $this->request->post("newpassword", '', null);
  213. $renewpassword = $this->request->post("renewpassword", '', null);
  214. $token = $this->request->post('__token__');
  215. $rule = [
  216. 'oldpassword' => 'require|regex:\S{6,30}',
  217. 'newpassword' => 'require|regex:\S{6,30}',
  218. 'renewpassword' => 'require|regex:\S{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. }
  240. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  241. if ($ret) {
  242. $this->success(__('Reset password successful'), url('user/login'));
  243. } else {
  244. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  245. }
  246. }
  247. $this->view->assign('title', __('Change password'));
  248. return $this->view->fetch();
  249. }
  250. public function attachment()
  251. {
  252. //设置过滤方法
  253. $this->request->filter(['strip_tags']);
  254. if ($this->request->isAjax()) {
  255. $mimetypeQuery = [];
  256. $where = [];
  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 = array_filter(explode(',', $filterArr['mimetype']));
  263. foreach ($mimetypeArr as $index => $item) {
  264. $query->whereOr('mimetype', 'like', '%' . str_replace("/*", "/", $item) . '%');
  265. }
  266. };
  267. } elseif (isset($filterArr['mimetype'])) {
  268. $where['mimetype'] = ['like', '%' . $filterArr['mimetype'] . '%'];
  269. }
  270. if (isset($filterArr['filename'])) {
  271. $where['filename'] = ['like', '%' . $filterArr['filename'] . '%'];
  272. }
  273. if (isset($filterArr['createtime'])) {
  274. $timeArr = explode(' - ', $filterArr['createtime']);
  275. $where['createtime'] = ['between', [strtotime($timeArr[0]), strtotime($timeArr[1])]];
  276. }
  277. $search = $this->request->get('search');
  278. if ($search) {
  279. $where['filename'] = ['like', '%' . $search . '%'];
  280. }
  281. $model = new Attachment();
  282. $offset = $this->request->get("offset", 0);
  283. $limit = $this->request->get("limit", 0);
  284. $total = $model
  285. ->where($where)
  286. ->where($mimetypeQuery)
  287. ->where('user_id', $this->auth->id)
  288. ->order("id", "DESC")
  289. ->count();
  290. $list = $model
  291. ->where($where)
  292. ->where($mimetypeQuery)
  293. ->where('user_id', $this->auth->id)
  294. ->order("id", "DESC")
  295. ->limit($offset, $limit)
  296. ->select();
  297. $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
  298. foreach ($list as $k => &$v) {
  299. $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
  300. }
  301. unset($v);
  302. $result = array("total" => $total, "rows" => $list);
  303. return json($result);
  304. }
  305. $mimetype = $this->request->get('mimetype', '');
  306. $mimetype = substr($mimetype, -1) === '/' ? $mimetype . '*' : $mimetype;
  307. $this->view->assign('mimetype', $mimetype);
  308. $this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList());
  309. return $this->view->fetch();
  310. }
  311. public function template(){
  312. // 获取搜索参数
  313. $keyword = input('param.keyword', '', 'trim');
  314. $type = input('param.type', '', 'trim');
  315. // 构建查询条件
  316. $where = [];
  317. if (!empty($keyword)) {
  318. $where['template_name|type'] = ['like', '%' . $keyword . '%'];
  319. }
  320. if (!empty($type)) {
  321. $where['type'] = ['like', '%' . $type . '%'];
  322. }
  323. // 查询模版表
  324. $products = Db::name('product_template')->where($where)->order('id desc')->select();
  325. // // 检查并修正图片URL
  326. // foreach ($products as &$val) {
  327. // // 如果URL不包含http,添加baseUrl
  328. // if (!empty($val['template_image_url']) && strpos($val['template_image_url'], 'http') !== 0) {
  329. // // 使用相对路径或根据实际情况设置正确的URL
  330. // // 这里假设图片存储在public/uploads目录下
  331. // $val['template_image_url'] = '/uploads' . $val['template_image_url'];
  332. // }
  333. // }
  334. // 判断是否为AJAX请求
  335. if (request()->isAjax()) {
  336. return json([
  337. 'code' => 0,
  338. 'msg' => 'success',
  339. 'data' => $products
  340. ]);
  341. }
  342. // 分配数据到视图
  343. $this->view->assign('products', $products);
  344. $this->view->assign('keyword', $keyword);
  345. $this->view->assign('type', $type);
  346. return $this->view->fetch();
  347. }
  348. public function text_to_video(){
  349. return $this->view->fetch();
  350. }
  351. }