User.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 think\Config;
  8. use think\Cookie;
  9. use think\Db;
  10. use think\Exception;
  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. * @param $name
  51. * @return mixed
  52. */
  53. public function _empty($name)
  54. {
  55. $data = Hook::listen("user_request_empty", $name);
  56. foreach ($data as $index => $datum) {
  57. $this->view->assign($datum);
  58. }
  59. return $this->view->fetch('user/' . $name);
  60. }
  61. /**
  62. * 会员中心
  63. */
  64. public function index()
  65. {
  66. $this->view->assign('title', __('User center'));
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 注册会员
  71. */
  72. public function register()
  73. {
  74. $url = $this->request->request('url', '');
  75. if ($this->auth->id) {
  76. $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index'));
  77. }
  78. if ($this->request->isPost()) {
  79. $username = $this->request->post('username');
  80. $password = $this->request->post('password');
  81. $email = $this->request->post('email');
  82. $mobile = $this->request->post('mobile', '');
  83. $captcha = $this->request->post('captcha');
  84. $token = $this->request->post('__token__');
  85. $rule = [
  86. 'username' => 'require|length:3,30',
  87. 'password' => 'require|length:6,30',
  88. 'email' => 'require|email',
  89. 'mobile' => 'regex:/^1\d{10}$/',
  90. '__token__' => 'require|token',
  91. ];
  92. $msg = [
  93. 'username.require' => 'Username can not be empty',
  94. 'username.length' => 'Username must be 3 to 30 characters',
  95. 'password.require' => 'Password can not be empty',
  96. 'password.length' => 'Password must be 6 to 30 characters',
  97. 'email' => 'Email is incorrect',
  98. 'mobile' => 'Mobile is incorrect',
  99. ];
  100. $data = [
  101. 'username' => $username,
  102. 'password' => $password,
  103. 'email' => $email,
  104. 'mobile' => $mobile,
  105. '__token__' => $token,
  106. ];
  107. //验证码
  108. $captchaResult = true;
  109. $captchaType = config("fastadmin.user_register_captcha");
  110. if ($captchaType) {
  111. if ($captchaType == 'mobile') {
  112. $captchaResult = Sms::check($mobile, $captcha, 'register');
  113. } elseif ($captchaType == 'email') {
  114. $captchaResult = Ems::check($email, $captcha, 'register');
  115. } elseif ($captchaType == 'wechat') {
  116. $captchaResult = WechatCaptcha::check($captcha, 'register');
  117. } elseif ($captchaType == 'text') {
  118. $captchaResult = \think\Validate::is($captcha, 'captcha');
  119. }
  120. }
  121. if (!$captchaResult) {
  122. $this->error(__('Captcha is incorrect'));
  123. }
  124. $validate = new Validate($rule, $msg);
  125. $result = $validate->check($data);
  126. if (!$result) {
  127. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  128. }
  129. if ($this->auth->register($username, $password, $email, $mobile)) {
  130. $site = Config::get("site");
  131. $traffic = $site['gift_traffic'];
  132. \app\common\model\User::update(['total_traffic'=>$traffic],['id'=>$this->auth->id]);
  133. $this->success(__('Sign up successful'), $url ? $url : url('user/index'));
  134. } else {
  135. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  136. }
  137. }
  138. //判断来源
  139. $referer = $this->request->server('HTTP_REFERER');
  140. if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
  141. && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  142. $url = $referer;
  143. }
  144. $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
  145. $this->view->assign('url', $url);
  146. $this->view->assign('title', __('Register'));
  147. return $this->view->fetch();
  148. }
  149. /**
  150. * 会员登录
  151. */
  152. public function login()
  153. {
  154. $url = $this->request->request('url', '');
  155. if ($this->auth->id) {
  156. $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index'));
  157. }
  158. if ($this->request->isPost()) {
  159. $account = $this->request->post('account');
  160. $password = $this->request->post('password');
  161. $keeplogin = (int)$this->request->post('keeplogin');
  162. $token = $this->request->post('__token__');
  163. $rule = [
  164. 'account' => 'require|length:3,50',
  165. 'password' => 'require|length:6,30',
  166. '__token__' => 'require|token',
  167. ];
  168. $msg = [
  169. 'account.require' => 'Account can not be empty',
  170. 'account.length' => 'Account must be 3 to 50 characters',
  171. 'password.require' => 'Password can not be empty',
  172. 'password.length' => 'Password must be 6 to 30 characters',
  173. ];
  174. $data = [
  175. 'account' => $account,
  176. 'password' => $password,
  177. '__token__' => $token,
  178. ];
  179. $validate = new Validate($rule, $msg);
  180. $result = $validate->check($data);
  181. if (!$result) {
  182. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  183. return false;
  184. }
  185. if ($this->auth->login($account, $password)) {
  186. $this->success(__('Logged in successful'), $url ? $url : url('user/index'));
  187. } else {
  188. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  189. }
  190. }
  191. //判断来源
  192. $referer = $this->request->server('HTTP_REFERER');
  193. if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
  194. && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  195. $url = $referer;
  196. }
  197. $this->view->assign('url', $url);
  198. $this->view->assign('title', __('Login'));
  199. return $this->view->fetch();
  200. }
  201. /**
  202. * 注销登录
  203. */
  204. public function logout()
  205. {
  206. //注销本站
  207. $this->auth->logout();
  208. $this->success(__('Logout successful'), url('user/index'));
  209. }
  210. /**
  211. * 个人信息
  212. */
  213. public function profile()
  214. {
  215. $this->view->assign('title', __('Profile'));
  216. return $this->view->fetch();
  217. }
  218. /**
  219. * 修改密码
  220. */
  221. public function changepwd()
  222. {
  223. if ($this->request->isPost()) {
  224. $oldpassword = $this->request->post("oldpassword");
  225. $newpassword = $this->request->post("newpassword");
  226. $renewpassword = $this->request->post("renewpassword");
  227. $token = $this->request->post('__token__');
  228. $rule = [
  229. 'oldpassword' => 'require|length:6,30',
  230. 'newpassword' => 'require|length:6,30',
  231. 'renewpassword' => 'require|length:6,30|confirm:newpassword',
  232. '__token__' => 'token',
  233. ];
  234. $msg = [
  235. ];
  236. $data = [
  237. 'oldpassword' => $oldpassword,
  238. 'newpassword' => $newpassword,
  239. 'renewpassword' => $renewpassword,
  240. '__token__' => $token,
  241. ];
  242. $field = [
  243. 'oldpassword' => __('Old password'),
  244. 'newpassword' => __('New password'),
  245. 'renewpassword' => __('Renew password')
  246. ];
  247. $validate = new Validate($rule, $msg, $field);
  248. $result = $validate->check($data);
  249. if (!$result) {
  250. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  251. return false;
  252. }
  253. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  254. if ($ret) {
  255. $this->success(__('Reset password successful'), url('user/login'));
  256. } else {
  257. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  258. }
  259. }
  260. $this->view->assign('title', __('Change password'));
  261. return $this->view->fetch();
  262. }
  263. /**
  264. * 购买流量套餐
  265. */
  266. public function goods()
  267. {
  268. $config = get_addon_config('recharge');
  269. $moneyList = [];
  270. $moneyList = [
  271. ['value'=>10,'text'=>'¥10','default'=>true],
  272. ['value'=>20,'text'=>'¥20','default'=>false],
  273. ['value'=>30,'text'=>'¥30','default'=>false],
  274. ['value'=>50,'text'=>'¥50','default'=>false],
  275. ['value'=>100,'text'=>'¥100','default'=>false],
  276. ];
  277. $this->view->assign('addonConfig', $config);
  278. $this->view->assign('moneyList', $moneyList);
  279. $this->view->assign('title', '商城');
  280. return $this->view->fetch();
  281. }
  282. /**
  283. * 购买流量
  284. */
  285. public function buy_traffic()
  286. {
  287. $money = $this->request->post("money");
  288. if($this->auth->money < $money){return $this->error('余额不足,请充值有以后购买!');}
  289. $site = Config::get("site");
  290. $traffic = intval($money) * $site['rate_traffic'];
  291. Db::startTrans();
  292. try {
  293. \app\common\model\User::where(['id'=>$this->auth->id])->setInc('total_traffic',$traffic);
  294. \app\common\model\User::where(['id'=>$this->auth->id])->setDec('money',$money);
  295. Db::commit();
  296. return $this->success('购买成功,刷新个人中心查看流量');
  297. }catch (Exception $ex){
  298. Db::rollback();
  299. return $this->error('购买失败!');
  300. }
  301. }
  302. }