控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 会员首页管理
  13. // +----------------------------------------------------------------------
  14. namespace app\member\controller;
  15. use app\common\library\Ems;
  16. use app\common\library\Sms;
  17. use app\member\model\Member as MemberModel;
  18. use think\facade\Cookie;
  19. use think\facade\Hook;
  20. use think\facade\Validate;
  21. class Index extends MemberBase
  22. {
  23. protected $noNeedLogin = ['login', 'register', 'logout', 'forget'];
  24. //初始化
  25. protected function initialize()
  26. {
  27. parent::initialize();
  28. $auth = $this->auth;
  29. //监听注册登录退出的事件
  30. Hook::add('user_login_successed', function ($user) use ($auth) {
  31. $expire = $this->request->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. public function index()
  50. {
  51. return $this->fetch('/index');
  52. }
  53. //登录页面
  54. public function login()
  55. {
  56. $forward = $this->request->request('forward', '', 'url_clean');
  57. if (!empty($this->auth->id)) {
  58. $this->success("您已经是登陆状态!", $forward ? $forward : url("index"));
  59. }
  60. if ($this->request->isPost()) {
  61. //登录验证
  62. $account = $this->request->param('account');
  63. $password = $this->request->param('password');
  64. $verify = $this->request->param('verify');
  65. $token = $this->request->param('__token__');
  66. $rule = [
  67. 'account|账户' => 'require|length:3,30',
  68. 'password|密码' => 'require|length:3,30',
  69. '__token__' => 'require|token',
  70. ];
  71. $data = [
  72. 'account' => $account,
  73. 'password' => $password,
  74. '__token__' => $token,
  75. ];
  76. //验证码
  77. if (empty($verify) && $this->memberConfig['openverification']) {
  78. $this->error('验证码错误!');
  79. }
  80. if ($this->memberConfig['openverification'] && !captcha_check($verify)) {
  81. $this->error('验证码错误!');
  82. }
  83. $result = $this->validate($data, $rule);
  84. if (true !== $result) {
  85. $this->error($result, null, ['token' => $this->request->token()]);
  86. }
  87. $userInfo = $this->auth->loginLocal($account, $password);
  88. if ($userInfo) {
  89. $this->success('登录成功!', $forward ? $forward : url('index'));
  90. } else {
  91. //登陆失败
  92. $this->error($this->auth->getError() ?: '账号或者密码错误!', null, ['token' => $this->request->token()]);
  93. }
  94. } else {
  95. //判断来源
  96. $referer = url_clean($this->request->server('HTTP_REFERER'));
  97. if (!$forward && $referer && !preg_match("/(index\/login|index\/register|index\/forget|index\/logout)/i", $referer)) {
  98. $forward = $referer;
  99. }
  100. $this->assign('forward', $forward);
  101. return $this->fetch('/login');
  102. }
  103. }
  104. //注册页面
  105. public function register()
  106. {
  107. $forward = $this->request->request('forward', '', 'url_clean');
  108. if (empty($this->memberConfig['allowregister'])) {
  109. $this->error("系统不允许新会员注册!");
  110. }
  111. if ($this->auth->id) {
  112. $this->success("您已经是登陆状态,无需注册!", $forward ? $forward : url("index"));
  113. }
  114. if ($this->request->isPost()) {
  115. $extend = [];
  116. $data = $this->request->post();
  117. //验证码
  118. if (!captcha_check($data['verify'])) {
  119. $this->error('验证码输入错误!');
  120. return false;
  121. }
  122. $rule = [
  123. 'username|用户名' => 'unique:member|require|alphaDash|length:3,20',
  124. 'nickname|昵称' => 'unique:member|length:3,20',
  125. 'mobile|手机' => 'require|unique:member|mobile',
  126. 'password|密码' => 'require|length:3,20',
  127. 'email|邮箱' => 'unique:member|require|email',
  128. '__token__' => 'require|token',
  129. ];
  130. if ($this->memberConfig['password_confirm']) {
  131. $rule['password|密码'] = "require|length:3,20|confirm";
  132. }
  133. if ($this->memberConfig['register_mobile_verify']) {
  134. $rule['captcha_mobile|手机验证码'] = "require";
  135. }
  136. if ($this->memberConfig['register_email_verify']) {
  137. $rule['captcha_email|邮箱验证码'] = "require";
  138. }
  139. $result = $this->validate($data, $rule);
  140. if (true !== $result) {
  141. $this->error($result, null, ['token' => $this->request->token()]);
  142. }
  143. if ($this->memberConfig['register_mobile_verify']) {
  144. $result = Sms::check($data['mobile'], $data['captcha_mobile'], 'register');
  145. if (!$result) {
  146. $this->error('手机验证码错误!');
  147. }
  148. $extend['ischeck_mobile'] = 1;
  149. }
  150. if ($this->memberConfig['register_email_verify']) {
  151. $result = Ems::check($data['email'], $data['captcha_email'], 'register');
  152. if (!$result) {
  153. $this->error('邮箱验证码错误!');
  154. }
  155. $extend['ischeck_email'] = 1;
  156. }
  157. if ($this->auth->userRegister($data['username'], $data['password'], $data['email'], $data['mobile'], $extend)) {
  158. $this->success('会员注册成功!', $forward ? $forward : url('index'));
  159. } else {
  160. $this->error($this->auth->getError() ?: '帐号注册失败!', null, ['token' => $this->request->token()]);
  161. }
  162. } else {
  163. //判断来源
  164. $referer = url_clean($this->request->server('HTTP_REFERER'));
  165. if (!$forward && $referer && !preg_match("/(index\/login|index\/register|index\/forget|index\/logout)/i", $referer)) {
  166. $forward = $referer;
  167. }
  168. $this->assign('forward', $forward);
  169. return $this->fetch('/register');
  170. }
  171. }
  172. /**
  173. * 个人资料
  174. */
  175. public function profile()
  176. {
  177. if ($this->request->isPost()) {
  178. $data = $this->request->only(['id', 'avatar', 'username', 'nickname', 'gender', 'birthday', 'motto']);
  179. //验证数据合法性
  180. $rule = [
  181. 'nickname|昵称' => 'length:3,20',
  182. 'birthday' => 'date',
  183. ];
  184. $result = $this->validate($data, $rule);
  185. if (true !== $result) {
  186. $this->error($result);
  187. }
  188. $user = $this->auth->getUser();
  189. if (isset($data['nickname'])) {
  190. $exists = MemberModel::where('nickname', $data['nickname'])->where('id', '<>', $this->auth->id)->find();
  191. if ($exists) {
  192. $this->error('昵称已存在');
  193. }
  194. }
  195. $user->save($data);
  196. $this->success("基本信息修改成功!");
  197. } else {
  198. return $this->fetch('/profile');
  199. }
  200. }
  201. /**
  202. * 更改密码
  203. */
  204. public function changepwd()
  205. {
  206. if ($this->request->isPost()) {
  207. $oldPassword = $this->request->post("oldpassword");
  208. $newPassword = $this->request->post("newpassword");
  209. $renewPassword = $this->request->post("renewpassword");
  210. // 验证数据
  211. $data = [
  212. 'oldpassword' => $oldPassword,
  213. 'newpassword' => $newPassword,
  214. 'renewpassword' => $renewPassword,
  215. ];
  216. $rule = [
  217. 'oldpassword|旧密码' => 'require|length:6,30',
  218. 'newpassword|新密码' => 'require|length:6,30',
  219. 'renewpassword|确认密码' => 'require|length:6,30|confirm:newpassword',
  220. ];
  221. $result = $this->validate($data, $rule);
  222. if (true !== $result) {
  223. $this->error($result);
  224. }
  225. $res = $this->auth->changepwd($newPassword, $oldPassword);
  226. if (!$res) {
  227. $this->error($this->auth->getError());
  228. }
  229. $this->success('修改成功!');
  230. //注销当前登陆
  231. $this->logout();
  232. }
  233. }
  234. /**
  235. * 修改邮箱
  236. */
  237. public function changeemail()
  238. {
  239. if ($this->request->isPost()) {
  240. $user = $this->auth->getUser();
  241. $email = $this->request->post('email');
  242. $captcha = $this->request->param('captcha');
  243. if (!$email || !$captcha) {
  244. $this->error('参数不得为空!');
  245. }
  246. if (!Validate::is($email, "email")) {
  247. $this->error('邮箱格式不正确!');
  248. }
  249. if (MemberModel::where('email', $email)->where('id', '<>', $user->id)->find()) {
  250. $this->error('邮箱已占用');
  251. }
  252. $result = Ems::check($email, $captcha, 'changeemail');
  253. if (!$result) {
  254. $this->error('验证码错误!');
  255. }
  256. //只修改邮箱
  257. $user->ischeck_email = 1;
  258. $user->email = $email;
  259. $user->save();
  260. Ems::flush($email, 'changeemail');
  261. $this->success();
  262. } else {
  263. return $this->fetch('/changeemail');
  264. }
  265. }
  266. /**
  267. * 修改手机号
  268. */
  269. public function changemobile()
  270. {
  271. if ($this->request->isPost()) {
  272. $user = $this->auth->getUser();
  273. $mobile = $this->request->param('mobile');
  274. $captcha = $this->request->param('captcha');
  275. if (!$mobile || !$captcha) {
  276. $this->error('参数不得为空!');
  277. }
  278. if (!Validate::isMobile($mobile)) {
  279. $this->error('手机号格式不正确!');
  280. }
  281. if (MemberModel::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  282. $this->error('手机号已占用');
  283. }
  284. $result = Sms::check($mobile, $captcha, 'changemobile');
  285. if (!$result) {
  286. $this->error('验证码错误!');
  287. }
  288. //只修改手机号
  289. $user->mobile = $mobile;
  290. $user->ischeck_mobile = 1;
  291. $user->save();
  292. Sms::flush($mobile, 'changemobile');
  293. $this->success();
  294. } else {
  295. return $this->fetch('/changemobile');
  296. }
  297. }
  298. /**
  299. * 激活邮箱
  300. */
  301. public function actemail()
  302. {
  303. if ($this->request->isPost()) {
  304. $user = $this->auth->getUser();
  305. $captcha = $this->request->param('captcha');
  306. if (!$captcha) {
  307. $this->error('参数不得为空!');
  308. }
  309. $result = Ems::check($user->email, $captcha, 'actemail');
  310. if (!$result) {
  311. $this->error('验证码错误!');
  312. }
  313. $user->ischeck_email = 1;
  314. $user->save();
  315. Ems::flush($user->email, 'actemail');
  316. $this->success('激活成功!');
  317. } else {
  318. return $this->fetch('/actemail');
  319. }
  320. }
  321. /**
  322. * 激活手机号
  323. */
  324. public function actmobile()
  325. {
  326. if ($this->request->isPost()) {
  327. $user = $this->auth->getUser();
  328. $captcha = $this->request->param('captcha');
  329. if (!$captcha) {
  330. $this->error('参数不得为空!');
  331. }
  332. $result = Sms::check($user->mobile, $captcha, 'actmobile');
  333. if (!$result) {
  334. $this->error('验证码错误!');
  335. }
  336. $user->ischeck_mobile = 1;
  337. $user->save();
  338. Sms::flush($user->mobile, 'actmobile');
  339. $this->success('激活成功!');
  340. } else {
  341. return $this->fetch('/actmobile');
  342. }
  343. }
  344. /**
  345. *忘记密码
  346. */
  347. public function forget()
  348. {
  349. if ($this->request->isPost()) {
  350. $type = $this->request->param("type");
  351. $mobile = $this->request->param("mobile");
  352. $email = $this->request->param("email");
  353. $newpassword = $this->request->param("newpassword");
  354. $captcha = $this->request->param("captcha");
  355. $token = $this->request->param('__token__');
  356. // 验证数据
  357. $data = [
  358. 'mobile' => $mobile,
  359. 'email' => $email,
  360. 'captcha' => $captcha,
  361. 'newpassword' => $newpassword,
  362. '__token__' => $token,
  363. ];
  364. $rule = [
  365. 'mobile|手机号' => 'require|mobile',
  366. 'email|邮箱' => 'require|email',
  367. 'captcha|验证码' => 'require|number',
  368. 'newpassword|新密码' => 'require|length:6,30',
  369. '__token__' => 'require|token',
  370. ];
  371. if ($type == "mobile") {
  372. unset($rule['email|邮箱']);
  373. } else {
  374. unset($rule['mobile|手机号']);
  375. }
  376. $result = $this->validate($data, $rule);
  377. if (true !== $result) {
  378. $this->error($result, null, ['token' => $this->request->token()]);
  379. }
  380. if ($type == 'mobile') {
  381. $user = MemberModel::where('mobile', $mobile)->find();
  382. if (!$user) {
  383. $this->error('用户不存在!', null, ['token' => $this->request->token()]);
  384. }
  385. $result = Sms::check($mobile, $captcha, 'resetpwd');
  386. if (!$result) {
  387. $this->error('验证码错误!', null, ['token' => $this->request->token()]);
  388. }
  389. } elseif ($type == 'email') {
  390. $user = MemberModel::where('email', $email)->find();
  391. if (!$user) {
  392. $this->error('用户不存在!', null, ['token' => $this->request->token()]);
  393. }
  394. $result = Ems::check($email, $captcha, 'resetpwd');
  395. if (!$result) {
  396. $this->error('验证码错误!', null, ['token' => $this->request->token()]);
  397. }
  398. } else {
  399. $this->error('类型错误!', null, ['token' => $this->request->token()]);
  400. }
  401. $this->auth->direct($user->id);
  402. $res = $this->auth->changepwd($newpassword, '', true);
  403. if (!$res) {
  404. $this->error($this->auth->getError());
  405. }
  406. $this->success('重置成功!');
  407. } else {
  408. return $this->fetch('/forget');
  409. }
  410. }
  411. //会员组升级
  412. public function upgrade()
  413. {
  414. if (empty($this->memberGroup[$this->auth->groupid]['allowupgrade'])) {
  415. $this->error('此会员组不允许升级!');
  416. }
  417. if ($this->request->isPost()) {
  418. $groupid = $this->request->param("groupid/d", 0);
  419. if (empty($groupid) || in_array($groupid, [8, 1, 7])) {
  420. $this->error('会员组类型错误!');
  421. }
  422. $upgrade_type = $this->request->param("upgrade_type/d", 0);
  423. $upgrade_date = $this->request->param("upgrade_date/d", 1);
  424. if (0 >= intval($upgrade_date)) {
  425. $this->error('购买时限必须大于0!');
  426. }
  427. //消费类型,包年、包月、包日,价格
  428. $typearr = [$this->memberGroup[$groupid]['price_y'], $this->memberGroup[$groupid]['price_m'], $this->memberGroup[$groupid]['price_d']];
  429. //消费类型,包年、包月、包日,时间
  430. $typedatearr = ['366', '31', '1'];
  431. //消费的价格
  432. $cost = $typearr[$upgrade_type] * $upgrade_date;
  433. //购买时间
  434. $buydate = $typedatearr[$upgrade_type] * $upgrade_date * 86400;
  435. $overduedate = $this->auth->overduedate > time() ? ($this->auth->overduedate + $buydate) : (time() + $buydate);
  436. if ($this->auth->amount >= $cost) {
  437. MemberModel::where('id', $this->auth->id)->update(['groupid' => $groupid, 'overduedate' => $overduedate, 'vip' => 1]);
  438. //消费记录
  439. MemberModel::amount(-$cost, $this->auth->id, '升级用户组');
  440. $this->success('购买成功!', url('upgrade'));
  441. } else {
  442. $this->error('余额不足,请先充值!');
  443. }
  444. } else {
  445. $groupid = $this->request->param("groupid/d", 0);
  446. $grouppoint = $this->memberGroup[$this->auth->groupid]['point'];
  447. unset($this->memberGroup[$this->auth->groupid]);
  448. $this->assign([
  449. 'memberGroup' => $this->memberGroup,
  450. 'groupid' => $groupid,
  451. 'grouppoint' => $grouppoint,
  452. ]);
  453. return $this->fetch('/upgrade');
  454. }
  455. }
  456. //手动退出登录
  457. public function logout()
  458. {
  459. $this->auth->logout();
  460. $this->success('注销成功!', url("index/login"));
  461. }
  462. }