1
0
Files
helper/application/mobile/controller/Login.php
2020-08-06 14:58:51 +08:00

158 lines
4.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情等我喝醉了再说吧 |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace app\mobile\controller;
use app\common\model\Member as MemberModel;
use app\common\service\Member as MemberService;
use app\common\service\Sms as SmsService;
use app\common\validate\Member as MemberValidate;
use cjango\Wechat\Oauth;
class Login extends _Init
{
public function _initialize()
{
// parent::fenxiang();
$this->showWx = 1;
}
public function index()
{
if (IS_POST) {
$user = $this->request->post('username');
$pass = $this->request->post('password');
$res = MemberService::login($user, $pass);
if ($res === true) {
return $this->success('登录成功', 'center/index');
} else {
return $this->error($res);
}
} else {
if (parent::isLogin()) {
$this->redirect('center/index');
}
return $this->fetch();
}
}
/**
* 注册
* @return [type] [description]
*/
public function reg($invite = 0)
{
if (IS_POST) {
$user = $this->request->post('username');
$pass = $this->request->post('password');
$repass = $this->request->post('repassword');
$code = $this->request->post('mobile_code');
//校验注册数据
$data = [
'username' => $user,
'password' => $pass,
'repass' => $repass,
];
$validate = new MemberValidate();
if (!$validate->scene('mobileRegister')->check($data)) {
return $this->error($validate->getError());
}
//验证码校验
$checkCode = SmsService::check($user, $code);
if ($checkCode !== true) {
return $this->error($checkCode);
}
$res = MemberService::register($user, $pass, 1, $invite);
if ($res === true) {
return $this->success('注册成功', 'login/index');
} else {
return $this->error($res);
}
} else {
return $this->fetch();
}
}
/**
* 忘记密码
* @return [type] [description]
*/
public function forget()
{
if (IS_POST) {
$mobile = $this->request->post('mobile');
$pass = $this->request->post('password');
$repass = $this->request->post('repassword');
$code = $this->request->post('mobile_code');
//验证码校验
$checkCode = SmsService::check($mobile, $code);
if ($checkCode !== true) {
return $this->error($checkCode);
}
$userInfo = MemberModel::where('username', $mobile)->find();
$data = [
'newpass' => $pass,
'repass' => $repass,
];
$validate = new MemberValidate();
if (!$validate->scene('forget')->check(['newpass' => $pass, 'repass' => $repass])) {
return $this->error($validate->getError());
}
if (!$userInfo) {
return '没有这个用户';
} elseif ($userInfo->save(['password' => $pass])) {
return $this->success('', url('login/index'));
} else {
return $this->error('更新失败');
}
}
return $this->fetch();
}
// 自动登录
public function autoLogin()
{
parent::initWechat();
$token = Oauth::token();
if (!$token) {
$url = Oauth::url(url('login/autoLogin'));
$this->redirect($url);
} else {
$openid = $token['openid'];
$res = MemberService::openid_login($openid);
if ($res === true) {
$this->redirect(url('center/index'));
} else {
$this->redirect(url('login/info', ['title' => $res]));
}
}
}
/**
* 信息提示 错误提示
* @param [type] $title [description]
* @return [type] [description]
*/
public function info($title)
{
$this->info = $title;
return $this->fetch();
}
public function logout()
{
MemberService::logout();
return $this->success('退出成功', 'login/index');
}
}