MemberController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 ucenter()
  233. {
  234. // 未登录时跳转到用户登录
  235. if (! session('pboot_uid')) {
  236. location(Url::home('member/login'));
  237. }
  238. $content = parent::parser($this->htmldir . 'member/ucenter.html'); // 框架标签解析
  239. $content = $this->parser->parserBefore($content); // CMS公共标签前置解析
  240. $content = str_replace('{pboot:pagetitle}', $this->config('ucenter_title') ?: '个人中心-{pboot:sitetitle}-{pboot:sitesubtitle}', $content);
  241. $content = $this->parser->parserPositionLabel($content, 0, '个人中心', Url::home('member/ucenter')); // CMS当前位置标签解析
  242. $content = $this->parser->parserSpecialPageSortLabel($content, - 4, '个人中心', Url::home('member/ucenter')); // 解析分类标签
  243. $content = $this->parser->parserAfter($content); // CMS公共标签后置解析
  244. echo $content;
  245. exit();
  246. }
  247. // 用户修改
  248. public function umodify()
  249. {
  250. // 未登录时跳转到用户登录
  251. if (! session('pboot_uid')) {
  252. location(Url::home('member/login'));
  253. }
  254. // 执行资料修改
  255. if ($_POST && session('pboot_uid')) {
  256. $nickname = post('nickname');
  257. $useremail = post('useremail');
  258. $usermobile = post('usermobile');
  259. $opassword = post('opassword');
  260. $password = post('password');
  261. $rpassword = post('rpassword');
  262. $headpic = str_replace(SITE_DIR, '', post('headpic'));
  263. if (! $opassword) {
  264. alert_back('请输入当前密码!');
  265. } else {
  266. if (! $this->model->checkUsername(" password='" . md5(md5($opassword)) . "' AND id='" . session('pboot_uid') . "'")) {
  267. alert_back('您输入的当前密码不正确!');
  268. }
  269. }
  270. if ($useremail) { // 邮箱校验
  271. if (! preg_match('/^[\w]+@[\w\.]+\.[a-zA-Z]+$/', $useremail)) {
  272. alert_back('邮箱格式不正确,请输入正确的邮箱账号!');
  273. }
  274. if ($this->model->checkUsername("(useremail='$useremail' OR username='$useremail') AND id<>'" . session('pboot_uid') . "'")) {
  275. alert_back('您输入的邮箱已被注册!');
  276. }
  277. }
  278. if ($usermobile) { // 手机检验
  279. if (! preg_match('/^1[0-9]{10}$/', $usermobile)) {
  280. alert_back('手机格式不正确,请输入正确的手机号码!');
  281. }
  282. if ($this->model->checkUsername("(usermobile='$usermobile' OR username='$usermobile') AND id<>'" . session('pboot_uid') . "'")) {
  283. alert_back('您输入的手机号码已被注册!');
  284. }
  285. }
  286. // 构建数据
  287. $data = array(
  288. 'nickname' => $nickname,
  289. 'useremail' => $useremail,
  290. 'usermobile' => $usermobile,
  291. 'headpic' => $headpic
  292. );
  293. // 密码修改
  294. if ($password) {
  295. if ($password != $rpassword) {
  296. alert_back('确认密码不正确!');
  297. } else {
  298. $data['password'] = md5(md5($password));
  299. }
  300. }
  301. // 读取字段
  302. if (! ! $field = $this->model->getField()) {
  303. foreach ($field as $value) {
  304. $field_data = post($value->name);
  305. if (is_array($field_data)) { // 如果是多选等情况时转换
  306. $field_data = implode(',', $field_data);
  307. }
  308. $field_data = preg_replace_r('pboot:if', '', $field_data);
  309. if ($value->required && ! $field_data) {
  310. alert_back($value->description . '不能为空!');
  311. } else {
  312. $data[$value->name] = $field_data;
  313. }
  314. }
  315. }
  316. // 不允许修改的字段
  317. unset($data['id']);
  318. unset($data['ucode']);
  319. unset($data['username']);
  320. unset($data['status']);
  321. unset($data['gid']);
  322. unset($data['wxid']);
  323. unset($data['qqid']);
  324. unset($data['wbid']);
  325. unset($data['score']);
  326. unset($data['register_time']);
  327. unset($data['login_count']);
  328. unset($data['last_login_ip']);
  329. unset($data['last_login_time']);
  330. // 执行修改
  331. if ($this->model->modUser($data)) {
  332. alert_location('修改成功!', Url::home('member/umodify'), 1);
  333. } else {
  334. error('资料修改失败!', - 1);
  335. }
  336. } else {
  337. $content = parent::parser($this->htmldir . 'member/umodify.html'); // 框架标签解析
  338. $content = $this->parser->parserBefore($content); // CMS公共标签前置解析
  339. $content = str_replace('{pboot:pagetitle}', $this->config('umodify_title') ?: '资料修改-{pboot:sitetitle}-{pboot:sitesubtitle}', $content);
  340. $content = $this->parser->parserPositionLabel($content, 0, '资料修改', Url::home('member/umodify')); // CMS当前位置标签解析
  341. $content = $this->parser->parserSpecialPageSortLabel($content, - 5, '资料修改', Url::home('member/umodify')); // 解析分类标签
  342. $content = $this->parser->parserAfter($content); // CMS公共标签后置解析
  343. echo $content;
  344. exit();
  345. }
  346. }
  347. // 退出登录
  348. public function logout()
  349. {
  350. session('pboot_uid', '');
  351. session('pboot_ucode', '');
  352. session('pboot_username', '');
  353. session('pboot_useremail', '');
  354. session('pboot_usermobile', '');
  355. session('pboot_gid', '');
  356. session('pboot_gcode', '');
  357. session('pboot_gname', '');
  358. location(Url::home('member/login'));
  359. }
  360. // 文件上传方法(Ajax)
  361. public function upload()
  362. {
  363. // 必须登录
  364. if (! session('pboot_uid')) {
  365. json(0, '请先登录!');
  366. }
  367. $ext = $this->config('home_upload_ext') ?: "jpg,jpeg,png,gif,xls,xlsx,doc,docx,ppt,pptx,rar,zip,pdf,txt";
  368. $upload = upload('upload', $ext);
  369. if (is_array($upload)) {
  370. json(1, $upload);
  371. } else {
  372. json(0, $upload);
  373. }
  374. }
  375. // 发送邮件
  376. public function sendEmail()
  377. {
  378. if ($this->config('register_check_code') != 2) {
  379. json(0, '发送失败,后台配置非邮箱验证码模式!');
  380. }
  381. if (time() - session('lastsend') < 10) {
  382. json(0, '您提交太频繁了,请稍后再试!');
  383. }
  384. if (! session('sendemail')) {
  385. json(0, '非法提交发送邮件!');
  386. }
  387. // 发送邮箱参数
  388. if (! ! $to = post('to')) {
  389. if (! preg_match('/^[\w]+@[\w]+\.[a-zA-Z]+$/', $to)) {
  390. json(0, '邮箱格式不正确,请输入正确的邮箱账号!');
  391. }
  392. } else {
  393. json(0, '发送失败,缺少发送对象参数to!');
  394. }
  395. // 检查邮箱注册
  396. if ($this->model->checkUsername("useremail='$to' OR username='$to'")) {
  397. alert_back('您输入的邮箱已被注册!');
  398. }
  399. $rs = false;
  400. if ($to) {
  401. session('lastsend', time()); // 记录最后提交时间
  402. $mail_subject = "【" . CMSNAME . "】您有新的验证码信息,请注意查收!";
  403. $code = create_code(4);
  404. session('checkcode', strtolower($code));
  405. $mail_body = "您的验证码为:" . $code;
  406. $mail_body .= '<br>来自网站 ' . get_http_url() . ' (' . date('Y-m-d H:i:s') . ')';
  407. $rs = sendmail($this->config(), $to, $mail_subject, $mail_body);
  408. }
  409. if ($rs === true) {
  410. json(1, '发送成功!');
  411. } else {
  412. json(0, '发送失败,' . $rs);
  413. }
  414. }
  415. // 检查用户是否注册
  416. public function isRegister()
  417. {
  418. // 接受用户名、邮箱、手机三种方式
  419. $info = '';
  420. if (! $username = post('username')) {
  421. $err = '账号不能为空!';
  422. }
  423. // 注册类型判断
  424. if ($this->config('register_type') == 2) { // 邮箱注册
  425. if (! preg_match('/^[\w]+@[\w\.]+\.[a-zA-Z]+$/', $username)) {
  426. $err = '账号格式不正确,请输入正确的邮箱账号!';
  427. }
  428. if ($this->model->checkUsername("useremail='$username' OR username='$username'")) {
  429. $err = '您输入的邮箱已被注册!';
  430. } else {
  431. $suc = '您输入的邮箱可以使用!';
  432. }
  433. } elseif ($this->config('register_type') == 3) { // 手机注册
  434. if (! preg_match('/^1[0-9]{10}$/', $username)) {
  435. $err = '账号格式不正确,请输入正确的手机号码!';
  436. }
  437. if ($this->model->checkUsername("usermobile='$username' OR username='$username'")) {
  438. $err = '您输入的手机号码已被注册!';
  439. } else {
  440. $suc = '您输入的手机号码可以使用!';
  441. }
  442. } else { // 账号注册
  443. if (! preg_match('/^[\w\@\.]+$/', $username)) {
  444. $err = '用户账号含有不允许的特殊字符!';
  445. }
  446. // 检查用户名
  447. if ($this->model->checkUsername("username='$username' OR useremail='$username' OR usermobile='$username'")) {
  448. $err = '您输入的账号已被注册!';
  449. } else {
  450. $suc = '您输入的账号可以使用!';
  451. }
  452. }
  453. if ($err) {
  454. json(1, $err);
  455. } else {
  456. json(0, $suc);
  457. }
  458. }
  459. public function _empty()
  460. {
  461. _404('您访问的地址不存在,请核对再试!');
  462. }
  463. }