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

269 lines
8.8 KiB
PHTML

5 years ago
<?php
namespace Mobile\Controller;
use Org\Ipa365SDK\Ipa365;
use User\Api\MemberApi;
use User\Api\SuserApi;
5 years ago
use Think\Log;
use Base\Service\ApplyService;
use Base\Service\UserService;
use Base\Tool\TaskClient;
use Base\Facade\Request;
5 years ago
class CommonController extends BaseController {
const USER_NOT_ILLEGAL = -1; //用户名不合法
const USER_HAVE_SENSITIVE_STR = -2; //包含敏感字符
const USER_HAS_REGISTERED = -3; //用户已存在
const USER_PROMOTE_NATURAL = 0; //自然注册
const EMPTY_DATA = -100; //数据为空
const SIGN_ERROR = -99; //验签失败
const USER_NOT_EXIST = -1000; //用户不存在
const USER_FORBIDDEN = -1001; //被禁用
const USER_PWD_ERROR = -10021; //密码错误
const UNKNOWN_ERROR = -1100; //未知错误
const CODE_TIMEOUT = -98; //验证码超时
const CODE_ERROR = -97; //验证码错误
const RETURN_SUCCESS = 1;
const RETURN_FALSE = 2;
//用户登录
public function login()
{
5 years ago
$account = I('account', '');
$mobile = I('mobile', '');
5 years ago
$password = I('password');
$verifyCode = I('verify_code', '');
5 years ago
$promoteId = I('promote_id', 0);
5 years ago
$skipVerify = I('skip_verify', 0);
5 years ago
5 years ago
if (!$promoteId) {
5 years ago
$this->respondError('参数非法');
}
5 years ago
if ($skipVerify != 1) {
$verify = new \Think\Verify();
if (!$verify->check($verifyCode)) {
$this->respondError('验证码错误');
}
}
if ($account == '' && $mobile == '') {
$this->respondError('请输入账号');
}
$loginType = 1;
5 years ago
if ($mobile != '') {
$user = M('user', 'tab_')->field(['id', 'account'])->where(['phone' => $mobile])->find();
if (!$user) {
$this->respondError('手机号错误');
}
$account = $user['account'];
if (!$this->smsVerify($mobile, $verifyCode)) {
5 years ago
$this->respondError('验证失败');
}
$loginType = -1;
5 years ago
}
$promote = M('promote', 'tab_')->where(['id' => $promoteId])->find();
$suserApi = new SuserApi();
$userId = $suserApi->login($account, $password, $loginType); //调用登录
5 years ago
$resMsg = "登录成功";
if ($userId <= 0) {
switch ($userId) {
case -1000 :
5 years ago
$resMsg = "账号或密码错误";
5 years ago
break;
case -10021 :
5 years ago
$resMsg = "账号或密码错误";
5 years ago
break;
default :
$resMsg = "未知错误";
}
$this->respondError($resMsg);
}
5 years ago
$_SESSION['user_id'] = $userId;
5 years ago
$user = M('user', 'tab_')->where(['id' => $userId])->find();
if (!$user['promote_id'] && $promote) {
M('user', 'tab_')->where(['id' => $userId])->save([
'promote_id' => $promoteId,
'promote_account' => $promote['account']
]);
}
$this->setData(['user_id' => $userId])->respondSuccess($resMsg);
5 years ago
}
//验证码
public function verify($vid = '')
{
$config = array(
'seKey' => 'ThinkPHP.CN', //验证码加密密钥
'fontSize' => 16, // 验证码字体大小(px)
'imageH' => 42, // 验证码图片高度
'imageW' => 107, // 验证码图片宽度
'length' => 4, // 验证码位数
'fontttf' => '4.ttf', // 验证码字体,不设置随机获取
'useCurve' => false, // 是否画混淆曲线
'useNoise' => false, // 是否添加杂点
'useCurve' => false,
);
ob_clean();
$verify = new \Think\Verify($config);
$verify->codeSet = '0123456789';
$verify->entry($vid);
}
//注册
public function phoneRegister()
{
//添加用户
C(api('Config/lists'));
$params = $_POST;
#判断数据是否为空
if (empty($params)) {
$this->respondError('注册数据不能为空');
}
$promoteId = $params['promote_id'] ?? 0;
$gameId = $params['game_id'] ?? 0;
#验证短信验证码
if (!$this->smsVerify($params['account'], $params['verify_code'])) {
$this->respondError('验证失败');
}
5 years ago
$res = $this->doRegister($params['account'], $params['password'], $params['account'], $promoteId, 4, 2, $gameId);
if(empty($res)){
$this->respondError('添加失败');
}
//添加自动登录
$suserApi = new SuserApi();
$userId = $suserApi->login($params['account'], $params['password'], 1);
5 years ago
$_SESSION['user_id'] = $userId;
$this->setData(['user_id' => $userId])->respondSuccess('添加成功');
5 years ago
}
//忘记密码
public function forgetPassword()
{
$params = $_POST;
if (empty($params)) {
$this->respondError('基础信息不能为空');
}
if (!$this->smsVerify($params['account'], $params['verify_code'])) {
$this->respondError('验证失败');
}
5 years ago
//更新密码
$userApi = new MemberApi();
$userInfo = M("user", "tab_")->where("account = '".$params['account']."'")->find();
if(empty($userInfo)){
$this->respondError('用户不存在');
}
//更新用户
5 years ago
$upres = $userApi->updatePassword($userInfo['id'], $params['password']);
5 years ago
if($upres){
//自动登陆
5 years ago
// $userId = $userApi->login($params['account'], $params['password'], 1);
5 years ago
$this->respondSuccess('修改成功');
}else{
$this->respondError('密码更新错误,请刷新后再次尝试');
}
}
//普通注册
public function userRegister()
{
$account = I('account');
$password = I('password');
$promoteId = I('promote_id', 0);
$res = $this->doRegister($account, $password, '', $promoteId, 4, 1);
if(empty($res)){
$this->respondError('注册失败');
}
//添加自动登录
$suserApi = new SuserApi();
$userId = $suserApi->login($account, $password, 1);
5 years ago
$_SESSION['user_id'] = $userId;
$this->setData(['user_id' => $userId])->respondSuccess('注册成功');
5 years ago
}
//真正注册代码
public function doRegister($account, $password ,$phone, $promote_id, $register_way, $register_type, $game_id = 0)
{
//验证账号
$userService = new UserService();
if ($userService->isAccountExist($account)) {
5 years ago
$this->respondError('用户名已存在');
}
5 years ago
$data = [
5 years ago
'account' => $account,
'password' => think_ucenter_md5($password, UC_AUTH_KEY),
'phone' => $phone,
'head_img' => '',
5 years ago
'promote_id' => $promote_id,
5 years ago
'promote_account' => get_promote_account($promote_id),
5 years ago
'register_way' => $register_way,
'register_type' => $register_type,
5 years ago
'register_ip' => get_client_ip(),
'parent_id' => get_fu_id($promote_id),
'parent_name' => get_parent_name($promote_id),
'register_time' => time(),
5 years ago
'check_time' => time(),
];
if (Request::isIOS()) {
$data['device_type'] = 2;
} elseif (Request::isAndroid()) {
$data['device_type'] = 1;
}
5 years ago
if ($game_id) {//关联游戏
$game = M('game', 'tab_')->where(['id' => $game_id])->find();
if ($game) {
$data['fgame_id'] = $game_id;
$data['fgame_name'] = $game['game_name'];
}
}
/* 添加用户 */
$res = M('user', 'tab_') ->add($data);
if ($res) {
$taskClient = new TaskClient();
$taskClient->registerEvent($res, 'TestFlight');
}
5 years ago
return $res;
}
//发送验证码
public function sendPhoneCode()
{
$phone = I('phone');
$taskClient = new TaskClient();
$result = $taskClient->sendSmsCode($phone, get_client_ip());
$data = [];
if ($result['code'] == TaskClient::SUCCESS) {
$this->respondSuccess('发送成功');
5 years ago
} else {
5 years ago
$this->respondError('发送失败');
5 years ago
}
}
public function smsVerify($phone = '' , $code = '')
{
$taskClient = new TaskClient();
$result = $taskClient->checkSms($phone, $code);
$data = [];
if ($result['code'] == TaskClient::SUCCESS) {
return true;
5 years ago
} else {
return false;
5 years ago
}
}
}