...
|
...
|
@@ -3,318 +3,253 @@ |
|
|
namespace app\api\controller;
|
|
|
|
|
|
use app\common\controller\Api;
|
|
|
use app\common\library\Ems;
|
|
|
use app\common\library\Sms;
|
|
|
use fast\Random;
|
|
|
use fast\Http;
|
|
|
use think\Db;
|
|
|
use think\Validate;
|
|
|
use wxapp\aes\WXBizDataCrypt;
|
|
|
|
|
|
/**
|
|
|
* 会员接口
|
|
|
* 登录接口
|
|
|
*/
|
|
|
class User extends Api
|
|
|
{
|
|
|
protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
|
|
|
protected $noNeedLogin = '*';
|
|
|
protected $noNeedRight = '*';
|
|
|
|
|
|
public function _initialize()
|
|
|
{
|
|
|
parent::_initialize();
|
|
|
}
|
|
|
// public function _initialize()
|
|
|
// {
|
|
|
// parent::_initialize();
|
|
|
// }
|
|
|
|
|
|
/**
|
|
|
* 会员中心
|
|
|
* @ApiTitle (获取sessionKey和openid)
|
|
|
* @ApiSummary (获取sessionKey和openid)
|
|
|
* @ApiMethod (POST)
|
|
|
* @ApiRoute (/api/user/getSessionKey)
|
|
|
* @ApiParams (name="code", type="string", required=true, description="小程序code")
|
|
|
* @ApiReturn({
|
|
|
"code": 1,
|
|
|
"msg": "获取成功",
|
|
|
"time": "1553839125",
|
|
|
"data": {
|
|
|
"session_key": "session_key",//token
|
|
|
"openid": "openid",//openid
|
|
|
},
|
|
|
})
|
|
|
*/
|
|
|
public function index()
|
|
|
public function getSessionKey()
|
|
|
{
|
|
|
$this->success('', ['welcome' => $this->auth->nickname]);
|
|
|
}
|
|
|
$validate = new Validate([
|
|
|
'code' => 'require',
|
|
|
]);
|
|
|
|
|
|
/**
|
|
|
* 会员登录
|
|
|
*
|
|
|
* @param string $account 账号
|
|
|
* @param string $password 密码
|
|
|
*/
|
|
|
public function login()
|
|
|
{
|
|
|
$account = $this->request->request('account');
|
|
|
$password = $this->request->request('password');
|
|
|
if (!$account || !$password) {
|
|
|
$this->error(__('Invalid parameters'));
|
|
|
$validate->message([
|
|
|
'code.require' => '缺少参数code!',
|
|
|
]);
|
|
|
|
|
|
$data = $this->request->param();
|
|
|
if (!$validate->check($data)) {
|
|
|
$this->error(['code' => '40003', 'msg' => $validate->getError()]);
|
|
|
}
|
|
|
$ret = $this->auth->login($account, $password);
|
|
|
if ($ret) {
|
|
|
$data = ['userinfo' => $this->auth->getUserinfo()];
|
|
|
$this->success(__('Logged in successful'), $data);
|
|
|
} else {
|
|
|
$this->error($this->auth->getError());
|
|
|
|
|
|
$code = $data['code'];
|
|
|
$appId = config('app_id');
|
|
|
$appSecret = config('app_secret');
|
|
|
|
|
|
$response = "https://api.weixin.qq.com/sns/jscode2session?appid=$appId&secret=$appSecret&js_code=$code&grant_type=authorization_code";
|
|
|
$response = $this->http_get($response);
|
|
|
$response = json_decode($response, true);
|
|
|
if(!empty($response['errcode'])) {
|
|
|
$this->error('操作失败',$response['errcode']);
|
|
|
}
|
|
|
$this->success('获取成功',$response);
|
|
|
}
|
|
|
//curl get请求
|
|
|
public function http_get($url){
|
|
|
$curl = curl_init();//启动一个CURL会话
|
|
|
curl_setopt($curl, CURLOPT_URL,$url);
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
|
|
|
curl_setopt($curl, CURLOPT_HEADER, false);//不开启header
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
|
|
|
$result = curl_exec($curl); //执行操作
|
|
|
curl_close($curl);
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 手机验证码登录
|
|
|
*
|
|
|
* @param string $mobile 手机号
|
|
|
* @param string $captcha 验证码
|
|
|
* @ApiTitle (小程序登录注册)
|
|
|
* @ApiSummary (小程序登录注册)
|
|
|
* @ApiMethod (POST)
|
|
|
* @ApiRoute (/api/user/login)
|
|
|
* @ApiParams (name="openid", type="string", required=true, description="openid")
|
|
|
* @ApiParams (name="session_key", type="string", required=true, description="session_key")
|
|
|
* @ApiParams (name="encrypted_data", type="string", required=true, description="encrypted_data")
|
|
|
* @ApiParams (name="iv", type="string", required=true, description="iv")
|
|
|
* @ApiReturn({
|
|
|
"code": 1,
|
|
|
"msg": "登陆成功",
|
|
|
"time": "1553839125",
|
|
|
"data": {
|
|
|
"token": "token",//登录唯一标识
|
|
|
},
|
|
|
})
|
|
|
*/
|
|
|
public function mobilelogin()
|
|
|
public function login()
|
|
|
{
|
|
|
$mobile = $this->request->request('mobile');
|
|
|
$captcha = $this->request->request('captcha');
|
|
|
if (!$mobile || !$captcha) {
|
|
|
$this->error(__('Invalid parameters'));
|
|
|
}
|
|
|
if (!Validate::regex($mobile, "^1\d{10}$")) {
|
|
|
$this->error(__('Mobile is incorrect'));
|
|
|
$validate = new Validate([
|
|
|
'openid' => 'require',
|
|
|
'session_key' => 'require',
|
|
|
'encrypted_data' => 'require',
|
|
|
'iv' => 'require',
|
|
|
]);
|
|
|
|
|
|
$validate->message([
|
|
|
'openid.require' => '缺少参数openid!',
|
|
|
'session_key.require' => '缺少参数session_key!',
|
|
|
'encrypted_data.require' => '缺少参数encrypted_data!',
|
|
|
'iv.require' => '缺少参数iv!',
|
|
|
]);
|
|
|
|
|
|
$data = $this->request->param();
|
|
|
if (!$validate->check($data)) {
|
|
|
$this->error(['code' => '40003', 'msg' => $validate->getError()]);
|
|
|
}
|
|
|
if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
|
|
|
$this->error(__('Captcha is incorrect'));
|
|
|
|
|
|
$appId = config('app_id');
|
|
|
|
|
|
$openid = $data['openid'];
|
|
|
$sessionKey = $data['session_key'];
|
|
|
|
|
|
$pc = new WXBizDataCrypt($appId, $sessionKey);
|
|
|
$errCode = $pc->decryptData($data['encrypted_data'], $data['iv'], $wxUserData);
|
|
|
|
|
|
if ($errCode != 0) {
|
|
|
$this->error('检验数据失败!', ['errCode' => $errCode, 'param' => $data]);
|
|
|
}
|
|
|
$user = \app\common\model\User::getByMobile($mobile);
|
|
|
if ($user) {
|
|
|
if ($user->status != 'normal') {
|
|
|
$this->error(__('Account is locked'));
|
|
|
|
|
|
$findThirdPartyUser = Db::name("third")
|
|
|
->where('openid', $openid)
|
|
|
->where('app_id', $appId)
|
|
|
->find();
|
|
|
|
|
|
$currentTime = time();
|
|
|
$ip = $this->request->ip(0, true);
|
|
|
|
|
|
$wxUserData['sessionKey'] = $sessionKey;
|
|
|
unset($wxUserData['watermark']);
|
|
|
|
|
|
if ($findThirdPartyUser) {
|
|
|
$token = generate_user_token($findThirdPartyUser['user_id']);
|
|
|
|
|
|
$userData = [
|
|
|
'loginip' => $ip,
|
|
|
'logintime' => $currentTime,
|
|
|
'login_times' => Db::raw('login_times+1'),
|
|
|
'more' => json_encode($wxUserData)
|
|
|
];
|
|
|
|
|
|
if (isset($wxUserData['unionId'])) {
|
|
|
$userData['union_id'] = $wxUserData['unionId'];
|
|
|
}
|
|
|
//如果已经有账号则直接登录
|
|
|
$ret = $this->auth->direct($user->id);
|
|
|
} else {
|
|
|
$ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
|
|
|
}
|
|
|
if ($ret) {
|
|
|
Sms::flush($mobile, 'mobilelogin');
|
|
|
$data = ['userinfo' => $this->auth->getUserinfo()];
|
|
|
$this->success(__('Logged in successful'), $data);
|
|
|
} else {
|
|
|
$this->error($this->auth->getError());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 注册会员
|
|
|
*
|
|
|
* @param string $username 用户名
|
|
|
* @param string $password 密码
|
|
|
* @param string $email 邮箱
|
|
|
* @param string $mobile 手机号
|
|
|
* @param string $code 验证码
|
|
|
*/
|
|
|
public function register()
|
|
|
{
|
|
|
$username = $this->request->request('username');
|
|
|
$password = $this->request->request('password');
|
|
|
$email = $this->request->request('email');
|
|
|
$mobile = $this->request->request('mobile');
|
|
|
$code = $this->request->request('code');
|
|
|
if (!$username || !$password) {
|
|
|
$this->error(__('Invalid parameters'));
|
|
|
}
|
|
|
if ($email && !Validate::is($email, "email")) {
|
|
|
$this->error(__('Email is incorrect'));
|
|
|
}
|
|
|
if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
|
|
|
$this->error(__('Mobile is incorrect'));
|
|
|
}
|
|
|
$ret = Sms::check($mobile, $code, 'register');
|
|
|
if (!$ret) {
|
|
|
$this->error(__('Captcha is incorrect'));
|
|
|
}
|
|
|
$ret = $this->auth->register($username, $password, $email, $mobile, []);
|
|
|
if ($ret) {
|
|
|
$data = ['userinfo' => $this->auth->getUserinfo()];
|
|
|
$this->success(__('Sign up successful'), $data);
|
|
|
Db::name("third")
|
|
|
->where('openid', $openid)
|
|
|
->where('app_id', $appId)
|
|
|
->update($userData);
|
|
|
$this->success("登录成功!", ['token' => $token]);
|
|
|
} else {
|
|
|
$this->error($this->auth->getError());
|
|
|
Db::startTrans();
|
|
|
$userId = Db::name("user")->insertGetId([
|
|
|
'status' => 'normal',
|
|
|
'id_num' => date('Ymd').str_pad(mt_rand(1, 99999),5,'0',STR_PAD_LEFT).'8',
|
|
|
'gender' => $wxUserData['gender'],
|
|
|
'nickname' => $wxUserData['nickName'],
|
|
|
'avatar' => $wxUserData['avatarUrl'],
|
|
|
'joinip' => $ip,
|
|
|
'jointime' => $currentTime,
|
|
|
'createtime' => $currentTime,
|
|
|
'updatetime' => $currentTime,
|
|
|
'loginip' => $ip,
|
|
|
'logintime' => $currentTime,
|
|
|
]);
|
|
|
$row = Db::name("third")->insert([
|
|
|
'openid' => $openid,
|
|
|
'user_id' => $userId,
|
|
|
'nickname' => $wxUserData['nickName'],
|
|
|
'app_id' => $appId,
|
|
|
'loginip' => $ip,
|
|
|
'union_id' => '',
|
|
|
'logintime' => $currentTime,
|
|
|
'createtime' => $currentTime,
|
|
|
'login_times' => 1,
|
|
|
'more' => json_encode($wxUserData)
|
|
|
]);
|
|
|
|
|
|
if ($userId && $row) {
|
|
|
Db::commit();
|
|
|
$token = generate_user_token($userId);
|
|
|
$this->success("登录成功!", ['token' => $token]);
|
|
|
} else {
|
|
|
Db::rollback();
|
|
|
$this->error('登录失败');
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 注销登录
|
|
|
*/
|
|
|
public function logout()
|
|
|
{
|
|
|
$this->auth->logout();
|
|
|
$this->success(__('Logout successful'));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改会员个人信息
|
|
|
*
|
|
|
* @param string $avatar 头像地址
|
|
|
* @param string $username 用户名
|
|
|
* @param string $nickname 昵称
|
|
|
* @param string $bio 个人简介
|
|
|
* @ApiTitle (通过code获取token)
|
|
|
* @ApiSummary (通过code获取token)
|
|
|
* @ApiMethod (POST)
|
|
|
* @ApiRoute (/api/user/getToken)
|
|
|
* @ApiParams (name="code", type="string", required=true, description="code")
|
|
|
* @ApiReturn({
|
|
|
"code": 1,
|
|
|
"msg": "SUCCESS",
|
|
|
"time": "1553839125",
|
|
|
"data": {
|
|
|
"token": "token",//登录唯一标识
|
|
|
},
|
|
|
})
|
|
|
*/
|
|
|
public function profile()
|
|
|
{
|
|
|
$user = $this->auth->getUser();
|
|
|
$username = $this->request->request('username');
|
|
|
$nickname = $this->request->request('nickname');
|
|
|
$bio = $this->request->request('bio');
|
|
|
$avatar = $this->request->request('avatar', '', 'trim,strip_tags,htmlspecialchars');
|
|
|
if ($username) {
|
|
|
$exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
|
|
|
if ($exists) {
|
|
|
$this->error(__('Username already exists'));
|
|
|
}
|
|
|
$user->username = $username;
|
|
|
}
|
|
|
$user->nickname = $nickname;
|
|
|
$user->bio = $bio;
|
|
|
$user->avatar = $avatar;
|
|
|
$user->save();
|
|
|
$this->success();
|
|
|
}
|
|
|
public function getToken(){
|
|
|
$validate = new Validate([
|
|
|
'code' => 'require',
|
|
|
]);
|
|
|
|
|
|
/**
|
|
|
* 修改邮箱
|
|
|
*
|
|
|
* @param string $email 邮箱
|
|
|
* @param string $captcha 验证码
|
|
|
*/
|
|
|
public function changeemail()
|
|
|
{
|
|
|
$user = $this->auth->getUser();
|
|
|
$email = $this->request->post('email');
|
|
|
$captcha = $this->request->request('captcha');
|
|
|
if (!$email || !$captcha) {
|
|
|
$this->error(__('Invalid parameters'));
|
|
|
}
|
|
|
if (!Validate::is($email, "email")) {
|
|
|
$this->error(__('Email is incorrect'));
|
|
|
}
|
|
|
if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
|
|
|
$this->error(__('Email already exists'));
|
|
|
}
|
|
|
$result = Ems::check($email, $captcha, 'changeemail');
|
|
|
if (!$result) {
|
|
|
$this->error(__('Captcha is incorrect'));
|
|
|
}
|
|
|
$verification = $user->verification;
|
|
|
$verification->email = 1;
|
|
|
$user->verification = $verification;
|
|
|
$user->email = $email;
|
|
|
$user->save();
|
|
|
|
|
|
Ems::flush($email, 'changeemail');
|
|
|
$this->success();
|
|
|
}
|
|
|
$validate->message([
|
|
|
'code.require' => '缺少参数code!',
|
|
|
]);
|
|
|
|
|
|
/**
|
|
|
* 修改手机号
|
|
|
*
|
|
|
* @param string $email 手机号
|
|
|
* @param string $captcha 验证码
|
|
|
*/
|
|
|
public function changemobile()
|
|
|
{
|
|
|
$user = $this->auth->getUser();
|
|
|
$mobile = $this->request->request('mobile');
|
|
|
$captcha = $this->request->request('captcha');
|
|
|
if (!$mobile || !$captcha) {
|
|
|
$this->error(__('Invalid parameters'));
|
|
|
}
|
|
|
if (!Validate::regex($mobile, "^1\d{10}$")) {
|
|
|
$this->error(__('Mobile is incorrect'));
|
|
|
$data = $this->request->param();
|
|
|
if (!$validate->check($data)) {
|
|
|
$this->error(['code'=>'40003','msg'=>$validate->getError()]);
|
|
|
}
|
|
|
if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
|
|
|
$this->error(__('Mobile already exists'));
|
|
|
}
|
|
|
$result = Sms::check($mobile, $captcha, 'changemobile');
|
|
|
if (!$result) {
|
|
|
$this->error(__('Captcha is incorrect'));
|
|
|
}
|
|
|
$verification = $user->verification;
|
|
|
$verification->mobile = 1;
|
|
|
$user->verification = $verification;
|
|
|
$user->mobile = $mobile;
|
|
|
$user->save();
|
|
|
|
|
|
Sms::flush($mobile, 'changemobile');
|
|
|
$this->success();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 第三方登录
|
|
|
*
|
|
|
* @param string $platform 平台名称
|
|
|
* @param string $code Code码
|
|
|
*/
|
|
|
public function third()
|
|
|
{
|
|
|
$url = url('user/index');
|
|
|
$platform = $this->request->request("platform");
|
|
|
$code = $this->request->request("code");
|
|
|
$config = get_addon_config('third');
|
|
|
if (!$config || !isset($config[$platform])) {
|
|
|
$this->error(__('Invalid parameters'));
|
|
|
$code = $data['code'];
|
|
|
$appId = config('app_id');
|
|
|
$appSecret = config('app_secret');
|
|
|
|
|
|
$response = Http::sendRequest("https://api.weixin.qq.com/sns/jscode2session?appid=$appId&secret=$appSecret&js_code=$code&grant_type=authorization_code");
|
|
|
|
|
|
if (!empty($response['errcode'])) {
|
|
|
$this->error('操作失败:',$response['errcode']);
|
|
|
}
|
|
|
$app = new \addons\third\library\Application($config);
|
|
|
//通过code换access_token和绑定会员
|
|
|
$result = $app->{$platform}->getUserInfo(['code' => $code]);
|
|
|
if ($result) {
|
|
|
$loginret = \addons\third\library\Service::connect($platform, $result);
|
|
|
if ($loginret) {
|
|
|
$data = [
|
|
|
'userinfo' => $this->auth->getUserinfo(),
|
|
|
'thirdinfo' => $result
|
|
|
];
|
|
|
$this->success(__('Logged in successful'), $data);
|
|
|
}
|
|
|
|
|
|
$third = Db::name('third')->where(['openid'=>json_decode($response['msg'],true)['openid']])->find();
|
|
|
if(empty($third)){
|
|
|
$this->error('查无此人');
|
|
|
}
|
|
|
$this->error(__('Operation failed'), $url);
|
|
|
$user_token = Db::name('user_token')->where('user_id',$third['user_id'])->find();
|
|
|
$data['token'] = $user_token['token'];
|
|
|
$this->success('SUCCESS',$data);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 重置密码
|
|
|
*
|
|
|
* @param string $mobile 手机号
|
|
|
* @param string $newpassword 新密码
|
|
|
* @param string $captcha 验证码
|
|
|
*/
|
|
|
public function resetpwd()
|
|
|
{
|
|
|
$type = $this->request->request("type");
|
|
|
$mobile = $this->request->request("mobile");
|
|
|
$email = $this->request->request("email");
|
|
|
$newpassword = $this->request->request("newpassword");
|
|
|
$captcha = $this->request->request("captcha");
|
|
|
if (!$newpassword || !$captcha) {
|
|
|
$this->error(__('Invalid parameters'));
|
|
|
}
|
|
|
if ($type == 'mobile') {
|
|
|
if (!Validate::regex($mobile, "^1\d{10}$")) {
|
|
|
$this->error(__('Mobile is incorrect'));
|
|
|
}
|
|
|
$user = \app\common\model\User::getByMobile($mobile);
|
|
|
if (!$user) {
|
|
|
$this->error(__('User not found'));
|
|
|
}
|
|
|
$ret = Sms::check($mobile, $captcha, 'resetpwd');
|
|
|
if (!$ret) {
|
|
|
$this->error(__('Captcha is incorrect'));
|
|
|
}
|
|
|
Sms::flush($mobile, 'resetpwd');
|
|
|
} else {
|
|
|
if (!Validate::is($email, "email")) {
|
|
|
$this->error(__('Email is incorrect'));
|
|
|
}
|
|
|
$user = \app\common\model\User::getByEmail($email);
|
|
|
if (!$user) {
|
|
|
$this->error(__('User not found'));
|
|
|
}
|
|
|
$ret = Ems::check($email, $captcha, 'resetpwd');
|
|
|
if (!$ret) {
|
|
|
$this->error(__('Captcha is incorrect'));
|
|
|
}
|
|
|
Ems::flush($email, 'resetpwd');
|
|
|
}
|
|
|
//模拟一次登录
|
|
|
$this->auth->direct($user->id);
|
|
|
$ret = $this->auth->changepwd($newpassword, '', true);
|
|
|
if ($ret) {
|
|
|
$this->success(__('Reset password successful'));
|
|
|
} else {
|
|
|
$this->error($this->auth->getError());
|
|
|
}
|
|
|
public function member(){
|
|
|
$this->success('SUCCESS',$this->user);
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|