心理咨询网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MemberController.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2020年06月26日
  7. * 会员前台控制器
  8. */
  9. namespace app\home\controller;
  10. use core\basic\Controller;
  11. use app\home\model\MemberModel;
  12. use core\basic\Url;
  13. class MemberController extends Controller
  14. {
  15. protected $parser;
  16. protected $model;
  17. protected $htmldir;
  18. public function __construct()
  19. {
  20. $this->model = new MemberModel();
  21. $this->parser = new ParserController();
  22. $this->htmldir = $this->config('tpl_html_dir') ? $this->config('tpl_html_dir') . '/' : '';
  23. }
  24. // 会员登录页面
  25. public function login()
  26. {
  27. // 已经登录时跳转到用户中心
  28. if (session('pboot_uid')) {
  29. location(Url::home('member/ucenter'));
  30. }
  31. // 执行登录验证
  32. if ($_POST) {
  33. if ($this->config('login_status') === '0') {
  34. error('系统已经关闭登录功能,请到后台开启再试!');
  35. }
  36. // 验证码验证
  37. $checkcode = strtolower(post('checkcode', 'var'));
  38. if ($this->config('login_check_code') !== '0') {
  39. if (! $checkcode) {
  40. alert_back('验证码不能为空!');
  41. }
  42. if ($checkcode != session('checkcode')) {
  43. alert_back('验证码错误!');
  44. }
  45. }
  46. $username = post('username');
  47. $password = post('password');
  48. if (! $username) {
  49. alert_back('用户账号不能为空!');
  50. }
  51. // 检查用户名
  52. if (! $this->model->checkUsername("username='$username' or useremail='$username' or usermobile='$username'")) {
  53. alert_back('用户账号不存在!');
  54. }
  55. // 检查密码
  56. if (! $password) {
  57. alert_back('用户密码不能为空!');
  58. } else {
  59. $password = md5(md5($password));
  60. }
  61. // 登录验证
  62. if (! ! $login = $this->model->login("(username='$username' or useremail='$username' or usermobile='$username') AND password='$password'")) {
  63. if (! $login->status) {
  64. alert_back('您的账号待审核,请联系管理员!');
  65. }
  66. session('pboot_uid', $login->id);
  67. session('pboot_ucode', $login->ucode);
  68. session('pboot_username', $login->username);
  69. session('pboot_useremail', $login->seremail);
  70. session('pboot_usermobile', $login->usermobile);
  71. session('pboot_gid', $login->gid);
  72. session('pboot_gcode', $login->gcode);
  73. session('pboot_gname', $login->gname);
  74. if (! ! $backurl = get('backurl')) {
  75. alert_location('登录成功!', $backurl, 1);
  76. } else {
  77. alert_location('登录成功!', Url::home('member/ucenter'), 1);
  78. }
  79. } else {
  80. alert_back('账号密码错误,请核对后重试!', 0);
  81. }
  82. } else {
  83. $content = parent::parser($this->htmldir . 'member/login.html'); // 框架标签解析
  84. $content = $this->parser->parserBefore($content); // CMS公共标签前置解析
  85. $content = str_replace('{pboot:pagetitle}', $this->config('login_title') ?: '会员登录-{pboot:sitetitle}-{pboot:sitesubtitle}', $content);
  86. $content = $this->parser->parserPositionLabel($content, 0, '会员登录', Url::home('member/login')); // CMS当前位置标签解析
  87. $content = $this->parser->parserSpecialPageSortLabel($content, - 2, '会员登录', Url::home('member/login')); // 解析分类标签
  88. $content = $this->parser->parserAfter($content); // CMS公共标签后置解析
  89. echo $content;
  90. exit();
  91. }
  92. }
  93. // 会员注册页面
  94. public function register()
  95. {
  96. // 已经登录时跳转到用户中心
  97. if (session('pboot_uid')) {
  98. location(Url::home('member/ucenter'));
  99. }
  100. // 执行注册
  101. if ($_POST) {
  102. if ($this->config('register_status') === '0') {
  103. error('系统已经关闭注册功能,请到后台开启再试!');
  104. }
  105. if (time() - session('lastreg') < 10) {
  106. alert_back('您注册太频繁了,请稍后再试!');
  107. }
  108. // 验证码验证
  109. $checkcode = strtolower(post('checkcode', 'var'));
  110. if ($this->config('register_check_code') !== '0') {
  111. if (! $checkcode) {
  112. alert_back('验证码不能为空!');
  113. }
  114. if ($checkcode != session('checkcode')) {
  115. alert_back('验证码错误!');
  116. }
  117. }
  118. $ucode = get_auto_code($this->model->getLastUcode(), 1);
  119. $username = post('username'); // 接受用户名、邮箱、手机三种方式
  120. $nickname = post('nickname');
  121. $password = post('password');
  122. $rpassword = post('rpassword');
  123. $useremail = '';
  124. $usermobile = '';
  125. // 注册类型判断
  126. if ($this->config('register_type') == 2) { // 邮箱注册
  127. $useremail = $username;
  128. if (! $useremail) {
  129. alert_back('账号不能为空,请输入注册的邮箱账号!');
  130. }
  131. if (! preg_match('/^[\w]+@[\w\.]+\.[a-zA-Z]+$/', $useremail)) {
  132. alert_back('账号格式不正确,请输入正确的邮箱账号!');
  133. }
  134. if ($this->model->checkUsername("useremail='$useremail' OR username='$useremail'")) {
  135. alert_back('您输入的邮箱已被注册!');
  136. }
  137. } elseif ($this->config('register_type') == 3) { // 手机注册
  138. $usermobile = $username;
  139. if (! $usermobile) {
  140. alert_back('账号不能为空,请输入注册的手机号码!');
  141. }
  142. if (! preg_match('/^1[0-9]{10}$/', $usermobile)) {
  143. alert_back('账号格式不正确,请输入正确的手机号码!');
  144. }
  145. if ($this->model->checkUsername("usermobile='$usermobile' OR username='$usermobile'")) {
  146. alert_back('您输入的手机号码已被注册!');
  147. }
  148. } else { // 账号注册
  149. if (! $username) {
  150. alert_back('用户名不能为空!');
  151. }
  152. if (! preg_match('/^[\w\@\.]+$/', $username)) {
  153. alert_back('用户账号含有不允许的特殊字符!');
  154. }
  155. // 检查用户名
  156. if ($this->model->checkUsername("username='$username' OR useremail='$username' OR usermobile='$username'")) {
  157. alert_back('您输入的账号已被注册!');
  158. }
  159. }
  160. if ($password != $rpassword) {
  161. alert_back('确认密码不正确!');
  162. }
  163. if (! $password) {
  164. alert_back('密码不能为空!');
  165. } else {
  166. $password = md5(md5($password));
  167. }
  168. // 默认值设置
  169. $status = $this->config('register_verify') ? 0 : 1; // 默认不需要审核
  170. $score = $this->config('register_score') ?: 0;
  171. $group = $this->model->getFirstGroup();
  172. $gid = $this->model->getGroupID($this->config('register_gcode')) ?: $group->id;
  173. // 构建数据
  174. $data = array(
  175. 'ucode' => $ucode,
  176. 'username' => $username,
  177. 'useremail' => $useremail,
  178. 'usermobile' => $usermobile,
  179. 'nickname' => $nickname,
  180. 'password' => $password,
  181. 'headpic' => '',
  182. 'status' => $status,
  183. 'gid' => $gid,
  184. 'wxid' => '',
  185. 'qqid' => '',
  186. 'wbid' => '',
  187. 'activation' => 1,
  188. 'score' => $score,
  189. 'register_time' => get_datetime(),
  190. 'login_count' => 0,
  191. 'last_login_ip' => 0,
  192. 'last_login_time' => 0
  193. );
  194. // 读取字段
  195. if (! ! $field = $this->model->getField()) {
  196. foreach ($field as $value) {
  197. $field_data = post($value->name);
  198. if (is_array($field_data)) { // 如果是多选等情况时转换
  199. $field_data = implode(',', $field_data);
  200. }
  201. $field_data = preg_replace_r('pboot:if', '', $field_data);
  202. if ($value->required && ! $field_data) {
  203. alert_back($value->description . '不能为空!');
  204. } else {
  205. $data[$value->name] = $field_data;
  206. }
  207. }
  208. }
  209. // 执行注册
  210. if ($this->model->register($data)) {
  211. session('lastreg', time()); // 记录最后提交时间
  212. if ($status) {
  213. alert_location('注册成功!', Url::home('member/login'), 1);
  214. } else {
  215. alert_location('注册成功,请等待管理员审核!', Url::home('member/login'), 1);
  216. }
  217. } else {
  218. error('会员注册失败!', - 1);
  219. }
  220. } else {
  221. $content = parent::parser($this->htmldir . 'member/register.html'); // 框架标签解析
  222. $content = $this->parser->parserBefore($content); // CMS公共标签前置解析
  223. $content = str_replace('{pboot:pagetitle}', $this->config('register_title') ?: '会员注册-{pboot:sitetitle}-{pboot:sitesubtitle}', $content);
  224. $content = $this->parser->parserPositionLabel($content, 0, '会员注册', Url::home('member/register')); // CMS当前位置标签解析
  225. $content = $this->parser->parserSpecialPageSortLabel($content, - 3, '会员注册', Url::home('member/register')); // 解析分类标签
  226. $content = $this->parser->parserAfter($content); // CMS公共标签后置解析
  227. echo $content;
  228. exit();
  229. }
  230. }
  231. //找回密码
  232. public function retrieve(){
  233. if($_POST){
  234. // 验证码验证
  235. $checkcode = strtolower(post('checkcode', 'var'));
  236. $email = post('email');
  237. $username = post('username');
  238. $password = post('password');
  239. if (! $checkcode) {
  240. alert_back('验证码不能为空!');
  241. }
  242. if ($checkcode != session('checkcode')) {
  243. alert_back('验证码错误!');
  244. }
  245. $where = ['username' => $username];
  246. $userInfo = object_to_array($this->model->checkUsername($where));
  247. if(!$userInfo){
  248. alert_back('该用户不存在!');
  249. }
  250. if(!empty($userInfo['useremail']) && $userInfo['useremail'] != $email){
  251. alert_back('与注册邮箱不匹配,请联系管理员!');
  252. }
  253. $data = [
  254. 'useremail' => $email,
  255. 'password' => md5(md5($password))
  256. ];
  257. $this->model->updatePassword($where,$data);
  258. alert_location('修改成功!', Url::home('member/login'), 1);
  259. } else {
  260. $content = parent::parser($this->htmldir . 'member/retrieve.html'); // 框架标签解析
  261. $content = $this->parser->parserBefore($content); // CMS公共标签前置解析
  262. $content = str_replace('{pboot:pagetitle}', $this->config('register_title') ?: '找回密码-{pboot:sitetitle}-{pboot:sitesubtitle}', $content);
  263. $content = $this->parser->parserPositionLabel($content, 0, '找回密码', Url::home('member/retrieve')); // CMS当前位置标签解析
  264. $content = $this->parser->parserSpecialPageSortLabel($content, - 3, '找回密码', Url::home('member/retrieve')); // 解析分类标签
  265. $content = $this->parser->parserAfter($content); // CMS公共标签后置解析
  266. echo $content;
  267. exit();
  268. }
  269. }
  270. // 用户中心
  271. public function ucenter()
  272. {
  273. // 未登录时跳转到用户登录
  274. if (! session('pboot_uid')) {
  275. location(Url::home('member/login'));
  276. }
  277. $content = parent::parser($this->htmldir . 'member/ucenter.html'); // 框架标签解析
  278. $content = $this->parser->parserBefore($content); // CMS公共标签前置解析
  279. $content = str_replace('{pboot:pagetitle}', $this->config('ucenter_title') ?: '个人中心-{pboot:sitetitle}-{pboot:sitesubtitle}', $content);
  280. $content = $this->parser->parserPositionLabel($content, 0, '个人中心', Url::home('member/ucenter')); // CMS当前位置标签解析
  281. $content = $this->parser->parserSpecialPageSortLabel($content, - 4, '个人中心', Url::home('member/ucenter')); // 解析分类标签
  282. $content = $this->parser->parserAfter($content); // CMS公共标签后置解析
  283. echo $content;
  284. exit();
  285. }
  286. // 用户修改
  287. public function umodify()
  288. {
  289. // 未登录时跳转到用户登录
  290. if (! session('pboot_uid')) {
  291. location(Url::home('member/login'));
  292. }
  293. // 执行资料修改
  294. if ($_POST && session('pboot_uid')) {
  295. $nickname = post('nickname');
  296. $useremail = post('useremail');
  297. $usermobile = post('usermobile');
  298. $opassword = post('opassword');
  299. $password = post('password');
  300. $rpassword = post('rpassword');
  301. $headpic = str_replace(SITE_DIR, '', post('headpic'));
  302. if (! $opassword) {
  303. alert_back('请输入当前密码!');
  304. } else {
  305. if (! $this->model->checkUsername(" password='" . md5(md5($opassword)) . "' AND id='" . session('pboot_uid') . "'")) {
  306. alert_back('您输入的当前密码不正确!');
  307. }
  308. }
  309. if ($useremail) { // 邮箱校验
  310. if (! preg_match('/^[\w]+@[\w\.]+\.[a-zA-Z]+$/', $useremail)) {
  311. alert_back('邮箱格式不正确,请输入正确的邮箱账号!');
  312. }
  313. if ($this->model->checkUsername("(useremail='$useremail' OR username='$useremail') AND id<>'" . session('pboot_uid') . "'")) {
  314. alert_back('您输入的邮箱已被注册!');
  315. }
  316. }
  317. if ($usermobile) { // 手机检验
  318. if (! preg_match('/^1[0-9]{10}$/', $usermobile)) {
  319. alert_back('手机格式不正确,请输入正确的手机号码!');
  320. }
  321. if ($this->model->checkUsername("(usermobile='$usermobile' OR username='$usermobile') AND id<>'" . session('pboot_uid') . "'")) {
  322. alert_back('您输入的手机号码已被注册!');
  323. }
  324. }
  325. // 构建数据
  326. $data = array(
  327. 'nickname' => $nickname,
  328. 'useremail' => $useremail,
  329. 'usermobile' => $usermobile,
  330. 'headpic' => $headpic
  331. );
  332. // 密码修改
  333. if ($password) {
  334. if ($password != $rpassword) {
  335. alert_back('确认密码不正确!');
  336. } else {
  337. $data['password'] = md5(md5($password));
  338. }
  339. }
  340. // 读取字段
  341. if (! ! $field = $this->model->getField()) {
  342. foreach ($field as $value) {
  343. $field_data = post($value->name);
  344. if (is_array($field_data)) { // 如果是多选等情况时转换
  345. $field_data = implode(',', $field_data);
  346. }
  347. $field_data = preg_replace_r('pboot:if', '', $field_data);
  348. if ($value->required && ! $field_data) {
  349. alert_back($value->description . '不能为空!');
  350. } else {
  351. $data[$value->name] = $field_data;
  352. }
  353. }
  354. }
  355. // 不允许修改的字段
  356. unset($data['id']);
  357. unset($data['ucode']);
  358. unset($data['username']);
  359. unset($data['status']);
  360. unset($data['gid']);
  361. unset($data['wxid']);
  362. unset($data['qqid']);
  363. unset($data['wbid']);
  364. unset($data['score']);
  365. unset($data['register_time']);
  366. unset($data['login_count']);
  367. unset($data['last_login_ip']);
  368. unset($data['last_login_time']);
  369. // 执行修改
  370. if ($this->model->modUser($data)) {
  371. alert_location('修改成功!', Url::home('member/umodify'), 1);
  372. } else {
  373. error('资料修改失败!', - 1);
  374. }
  375. } else {
  376. $content = parent::parser($this->htmldir . 'member/umodify.html'); // 框架标签解析
  377. $content = $this->parser->parserBefore($content); // CMS公共标签前置解析
  378. $content = str_replace('{pboot:pagetitle}', $this->config('umodify_title') ?: '资料修改-{pboot:sitetitle}-{pboot:sitesubtitle}', $content);
  379. $content = $this->parser->parserPositionLabel($content, 0, '资料修改', Url::home('member/umodify')); // CMS当前位置标签解析
  380. $content = $this->parser->parserSpecialPageSortLabel($content, - 5, '资料修改', Url::home('member/umodify')); // 解析分类标签
  381. $content = $this->parser->parserAfter($content); // CMS公共标签后置解析
  382. echo $content;
  383. exit();
  384. }
  385. }
  386. // 退出登录
  387. public function logout()
  388. {
  389. session('pboot_uid', '');
  390. session('pboot_ucode', '');
  391. session('pboot_username', '');
  392. session('pboot_useremail', '');
  393. session('pboot_usermobile', '');
  394. session('pboot_gid', '');
  395. session('pboot_gcode', '');
  396. session('pboot_gname', '');
  397. location(Url::home('member/login'));
  398. }
  399. // 文件上传方法(Ajax)
  400. public function upload()
  401. {
  402. // 必须登录
  403. if (! session('pboot_uid')) {
  404. json(0, '请先登录!');
  405. }
  406. $ext = $this->config('home_upload_ext') ?: "jpg,jpeg,png,gif,xls,xlsx,doc,docx,ppt,pptx,rar,zip,pdf,txt";
  407. $upload = upload('upload', $ext);
  408. if (is_array($upload)) {
  409. json(1, $upload);
  410. } else {
  411. json(0, $upload);
  412. }
  413. }
  414. // 发送邮件
  415. public function sendEmail()
  416. {
  417. $retrieve = post('retrieve');
  418. //$retrieve存在时为找回密码邮箱验证,不进行验证码模式判断
  419. if(!$retrieve){
  420. if ($this->config('register_check_code') != 2) {
  421. json(0, '发送失败,后台配置非邮箱验证码模式!');
  422. }
  423. }
  424. if (time() - session('lastsend') < 10) {
  425. json(0, '您提交太频繁了,请稍后再试!');
  426. }
  427. if (! session('sendemail')) {
  428. json(0, '非法提交发送邮件!');
  429. }
  430. // 发送邮箱参数
  431. if (! ! $to = post('to')) {
  432. if (! preg_match('/^[\w]+@[\w]+\.[a-zA-Z]+$/', $to)) {
  433. json(0, '邮箱格式不正确,请输入正确的邮箱账号!');
  434. }
  435. } else {
  436. json(0, '发送失败,缺少发送对象参数to!');
  437. }
  438. // 检查邮箱注册
  439. if(!$retrieve) {
  440. if ($this->model->checkUsername("useremail='$to' OR username='$to'")) {
  441. alert_back('您输入的邮箱已被注册!');
  442. }
  443. }
  444. $rs = false;
  445. if ($to) {
  446. session('lastsend', time()); // 记录最后提交时间
  447. $mail_subject = "【" . CMSNAME . "】您有新的验证码信息,请注意查收!";
  448. $code = create_code(4);
  449. session('checkcode', strtolower($code));
  450. $mail_body = "您的验证码为:" . $code;
  451. $mail_body .= '<br>来自网站 ' . get_http_url() . ' (' . date('Y-m-d H:i:s') . ')';
  452. $rs = sendmail($this->config(), $to, $mail_subject, $mail_body);
  453. }
  454. if ($rs === true) {
  455. json(1, '发送成功!');
  456. } else {
  457. json(0, '发送失败,' . $rs);
  458. }
  459. }
  460. // 检查用户是否注册
  461. public function isRegister()
  462. {
  463. // 接受用户名、邮箱、手机三种方式
  464. $info = '';
  465. if (! $username = post('username')) {
  466. $err = '账号不能为空!';
  467. }
  468. // 注册类型判断
  469. if ($this->config('register_type') == 2) { // 邮箱注册
  470. if (! preg_match('/^[\w]+@[\w\.]+\.[a-zA-Z]+$/', $username)) {
  471. $err = '账号格式不正确,请输入正确的邮箱账号!';
  472. }
  473. if ($this->model->checkUsername("useremail='$username' OR username='$username'")) {
  474. $err = '您输入的邮箱已被注册!';
  475. } else {
  476. $suc = '您输入的邮箱可以使用!';
  477. }
  478. } elseif ($this->config('register_type') == 3) { // 手机注册
  479. if (! preg_match('/^1[0-9]{10}$/', $username)) {
  480. $err = '账号格式不正确,请输入正确的手机号码!';
  481. }
  482. if ($this->model->checkUsername("usermobile='$username' OR username='$username'")) {
  483. $err = '您输入的手机号码已被注册!';
  484. } else {
  485. $suc = '您输入的手机号码可以使用!';
  486. }
  487. } else { // 账号注册
  488. if (! preg_match('/^[\w\@\.]+$/', $username)) {
  489. $err = '用户账号含有不允许的特殊字符!';
  490. }
  491. // 检查用户名
  492. if ($this->model->checkUsername("username='$username' OR useremail='$username' OR usermobile='$username'")) {
  493. $err = '您输入的账号已被注册!';
  494. } else {
  495. $suc = '您输入的账号可以使用!';
  496. }
  497. }
  498. if ($err) {
  499. json(1, $err);
  500. } else {
  501. json(0, $suc);
  502. }
  503. }
  504. public function _empty()
  505. {
  506. _404('您访问的地址不存在,请核对再试!');
  507. }
  508. }