|
@@ -4,12 +4,15 @@ namespace app\api\controller;
|
|
|
|
|
|
use app\api\model\UsersModel;
|
|
use app\api\model\UsersModel;
|
|
use app\common\controller\Api;
|
|
use app\common\controller\Api;
|
|
|
|
+use fast\Random;
|
|
use think\Cache;
|
|
use think\Cache;
|
|
|
|
+use think\Controller;
|
|
|
|
+use think\Db;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 注册接口
|
|
* 注册接口
|
|
*/
|
|
*/
|
|
-class Register extends Api
|
|
|
|
|
|
+class Register extends Controller
|
|
{
|
|
{
|
|
protected $noNeedRight = '*';
|
|
protected $noNeedRight = '*';
|
|
protected $noNeedLogin = '*';
|
|
protected $noNeedLogin = '*';
|
|
@@ -21,10 +24,7 @@ class Register extends Api
|
|
* @param string $code 验证码
|
|
* @param string $code 验证码
|
|
* @param string $user_tjtel 推荐人手机号
|
|
* @param string $user_tjtel 推荐人手机号
|
|
* @param string $user_pwd 密码
|
|
* @param string $user_pwd 密码
|
|
- * @param string $type 注册方式0手机号1QQ或微信
|
|
|
|
- * @param string $user_avatar 头像
|
|
|
|
- * @param string $user_openID QQ或微信开放id
|
|
|
|
- * @param string $user_nickname QQ或微信开放姓名
|
|
|
|
|
|
+ * @param string $type 默认0
|
|
*/
|
|
*/
|
|
public function register()
|
|
public function register()
|
|
{
|
|
{
|
|
@@ -35,21 +35,22 @@ class Register extends Api
|
|
if (isset($params['user_tjtel'])) { //验证推荐人手机号
|
|
if (isset($params['user_tjtel'])) { //验证推荐人手机号
|
|
$issetTjTel = UsersModel::where('user_tel', $params['user_tjtel'])->find();
|
|
$issetTjTel = UsersModel::where('user_tel', $params['user_tjtel'])->find();
|
|
if (!$issetTjTel) {
|
|
if (!$issetTjTel) {
|
|
- return $this->result('未找到推荐人手机号', '', 100);
|
|
|
|
|
|
+ return json(['code' => 100, 'msg' => '未找到推荐人手机号','data' => []]);
|
|
}
|
|
}
|
|
|
|
+ $addUserMember = Db::name('Users')->where('user_tel', $params['user_tjtel'])->setInc('user_member_count', 1); // 给上机的下级成员总数加1
|
|
} else {
|
|
} else {
|
|
- return $this->result('请填写推荐人手机号', [], 100);
|
|
|
|
|
|
+ return json(['code' => 100, 'msg' => '请填写推荐人手机号','data' => []]);
|
|
}
|
|
}
|
|
if (isset($params['user_tel'])) { //验证用户手机号
|
|
if (isset($params['user_tel'])) { //验证用户手机号
|
|
$issetTjTel = UsersModel::where('user_tel', $params['user_tel'])->find();
|
|
$issetTjTel = UsersModel::where('user_tel', $params['user_tel'])->find();
|
|
if ($issetTjTel) {
|
|
if ($issetTjTel) {
|
|
- return $this->result('此手机号已被注册', '', 100);
|
|
|
|
|
|
+ return json(['code' => 100, 'msg' => '此手机号已被注册','data' => []]);
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- return $this->result('请填写手机号', [], 100);
|
|
|
|
|
|
+ return json(['code' => 100, 'msg' => '请填写手机号','data' => []]);
|
|
}
|
|
}
|
|
if (!isset($params['code']) || !Cache::get($params['code'])) { //验证验证码是否错误
|
|
if (!isset($params['code']) || !Cache::get($params['code'])) { //验证验证码是否错误
|
|
- return $this->result('验证码错误', '', 100);
|
|
|
|
|
|
+ return json(['code' => 100, 'msg' => '验证码错误','data' => []]);
|
|
}
|
|
}
|
|
$userModel = new UsersModel(); //实例化usermodel
|
|
$userModel = new UsersModel(); //实例化usermodel
|
|
if ($params['type'] == 0) { //使用手机号注册
|
|
if ($params['type'] == 0) { //使用手机号注册
|
|
@@ -66,13 +67,13 @@ class Register extends Api
|
|
];
|
|
];
|
|
$validata = $this->validate($params, $rules, $msg);
|
|
$validata = $this->validate($params, $rules, $msg);
|
|
if (is_string($validata)) {
|
|
if (is_string($validata)) {
|
|
- return $this->result($validata, [], 100);
|
|
|
|
|
|
+ return json(['code' => 100, 'msg' => $validata,'data' => []]);
|
|
}
|
|
}
|
|
$data = array(
|
|
$data = array(
|
|
'user_nickname' => '优-' . rand(10000, 99999),
|
|
'user_nickname' => '优-' . rand(10000, 99999),
|
|
'user_tel' => $params['user_tel'],
|
|
'user_tel' => $params['user_tel'],
|
|
'user_pwd' => sha1(md5($params['user_pwd'])),
|
|
'user_pwd' => sha1(md5($params['user_pwd'])),
|
|
- 'user_avatar' => config('site.httpurl') . '/uploads/logo.img',
|
|
|
|
|
|
+ 'user_avatar' => config('site.httpurl') . '/uploads/logo.jpg',
|
|
'create_time' => date('Y-m-d H:i:s', time()),
|
|
'create_time' => date('Y-m-d H:i:s', time()),
|
|
'user_tjtel' => $params['user_tjtel'],
|
|
'user_tjtel' => $params['user_tjtel'],
|
|
'type' => $params['type'],
|
|
'type' => $params['type'],
|
|
@@ -80,51 +81,129 @@ class Register extends Api
|
|
$addUser = $userModel->allowField(true)->save($data);
|
|
$addUser = $userModel->allowField(true)->save($data);
|
|
if ($addUser) {
|
|
if ($addUser) {
|
|
Cache::rm($params['code']);//删除验证码缓存
|
|
Cache::rm($params['code']);//删除验证码缓存
|
|
- return $this->result('注册成功', [], 200);
|
|
|
|
|
|
+ return json(['code' => 200, 'msg' => '注册成功','data' => []]);
|
|
} else {
|
|
} else {
|
|
- return $this->result('注册失败', [], 100);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if ($params['type'] > 1) {
|
|
|
|
- $rules = [
|
|
|
|
- 'user_nickname' => 'require',
|
|
|
|
- 'user_avatar' => 'require',
|
|
|
|
- 'user_openid' => 'require',
|
|
|
|
- ];
|
|
|
|
- $msg = [
|
|
|
|
- 'user_nickname.require' => '昵称未获取',
|
|
|
|
- 'user_avatar.require' => '头像未获取',
|
|
|
|
- 'user_openid.require' => '开放id未获取',
|
|
|
|
- ];
|
|
|
|
- $validata = $this->validate($params, $rules, $msg);
|
|
|
|
- if (is_string($validata)) {
|
|
|
|
- return $this->result($validata, [], 100);
|
|
|
|
- }
|
|
|
|
- $data = array(
|
|
|
|
- 'user_nickname' => $params['user_nickname'],
|
|
|
|
- 'user_tel' => $params['user_tel'],
|
|
|
|
- 'user_pwd' => '无',
|
|
|
|
- 'user_avatar' => $params['user_avatar'],
|
|
|
|
- 'create_time' => date('Y-m-d H:i:s', time()),
|
|
|
|
- 'user_tjtel' => $params['user_tjtel'],
|
|
|
|
- 'user_openid' => $params['user_openid'],
|
|
|
|
- 'type' => $params['type'],
|
|
|
|
- );
|
|
|
|
- $addUser = $userModel->allowField(true)->save($data);
|
|
|
|
- if ($addUser) {
|
|
|
|
- Cache::rm($params['code']);//删除验证码缓存
|
|
|
|
- return $this->result('注册成功', [], 200);
|
|
|
|
- } else {
|
|
|
|
- return $this->result('注册失败', [], 100);
|
|
|
|
|
|
+ return json(['code' => 100, 'msg' => '注册失败','data' => []]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- return $this->result('手机号不合法', '', 100);
|
|
|
|
|
|
+ return json(['code' => 100, 'msg' => '手机号不合法','data' => []]);
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- return $this->result('手机号不存在', '', 100);
|
|
|
|
|
|
+ return json(['code' => 100, 'msg' => '手机号不存在','data' => []]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * QQ微信注册
|
|
|
|
+ * @ApiMethod (POST)
|
|
|
|
+ * @param string $type 默认1
|
|
|
|
+ * @param string $user_avatar 头像
|
|
|
|
+ * @param string $user_uniodid QQ或微信开放id
|
|
|
|
+ * @param string $user_nickname QQ或微信开放姓名
|
|
|
|
+ */
|
|
|
|
+ public function wechatRegister()
|
|
|
|
+ {
|
|
|
|
+ $params = $this->request->post();
|
|
|
|
+ $rules = [
|
|
|
|
+ 'user_nickname' => 'require',
|
|
|
|
+ 'user_avatar' => 'require',
|
|
|
|
+ 'user_unionid' => 'require',
|
|
|
|
+ 'type' => 'require',
|
|
|
|
+ ];
|
|
|
|
+ $msg = [
|
|
|
|
+ 'user_nickname.require' => '昵称未获取',
|
|
|
|
+ 'user_avatar.require' => '头像未获取',
|
|
|
|
+ 'user_unionid.require' => '开放id未获取',
|
|
|
|
+ 'type.require' => '开放id未获取',
|
|
|
|
+ ];
|
|
|
|
+ $validata = $this->validate($params, $rules, $msg);
|
|
|
|
+ if (is_string($validata)) {
|
|
|
|
+ return json(['code' => 200, 'msg' => '','data' => $validata]);
|
|
|
|
+ }
|
|
|
|
+ $data = array(
|
|
|
|
+ 'user_nickname' => $params['user_nickname'],
|
|
|
|
+ 'user_avatar' => $params['user_avatar'],
|
|
|
|
+ 'create_time' => date('Y-m-d H:i:s', time()),
|
|
|
|
+ 'user_unionid' => $params['user_unionid'],
|
|
|
|
+ 'type' => $params['type'],
|
|
|
|
+ );
|
|
|
|
+ $addUser = Db::name('Users')->insertGetId($data);
|
|
|
|
+ $token = self::settoken($addUser);
|
|
|
|
+ $res = array(
|
|
|
|
+ 'user_id' => $addUser,
|
|
|
|
+ 'token' => $token,
|
|
|
|
+ );
|
|
|
|
+ if ($addUser) {
|
|
|
|
+ return json(['code' => 20, 'msg' => '请绑定手机号','data' => []]);
|
|
|
|
+ } else {
|
|
|
|
+ return json(['code' => 100, 'msg' => '注册失败','data' => []]);
|
|
}
|
|
}
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 注册添加手机号
|
|
|
|
+ * @ApiMethod (POST)
|
|
|
|
+ * @param string $user_id 用户ID
|
|
|
|
+ * @param string $user_tel 用户手机号
|
|
|
|
+ * @param string $code 验证码
|
|
|
|
+ * @param string $user_tjtel 用户手机号
|
|
|
|
+ */
|
|
|
|
+ public function addTel()
|
|
|
|
+ {
|
|
|
|
+ $params = $this->request->post();
|
|
|
|
+ $rules = [
|
|
|
|
+ 'user_id' => 'require|number',
|
|
|
|
+ 'code' => 'require|number',
|
|
|
|
+ 'user_tel' => 'require|number',
|
|
|
|
+ 'user_tjtel' => 'require|number',
|
|
|
|
+ ];
|
|
|
|
+ $msg = [
|
|
|
|
+ 'user_id.require' => '网络错误1',
|
|
|
|
+ 'code .require' => '网络错误2',
|
|
|
|
+ 'user_tel.require' => '手机号不能为空',
|
|
|
|
+ 'user_tjtel.require' => '推荐人手机号不能为空',
|
|
|
|
+ 'user_id.number' => '网络错误4',
|
|
|
|
+ 'code.number' => '网络错误5',
|
|
|
|
+ 'user_tel.number' => '网络错误6',
|
|
|
|
+ 'user_tjtel.number' => '网络错误7',
|
|
|
|
+ ];
|
|
|
|
+ $validate = $this->validate($params, $rules, $msg);
|
|
|
|
+ if (is_string($validate)) {
|
|
|
|
+ return json(['code' => 100, 'msg' => $validate,'data' => []]);
|
|
|
|
+ }
|
|
|
|
+ $check = '/^(1(([35789][0-9])|(47)))\d{8}$/';
|
|
|
|
+ if (!preg_match($check, $params['user_tel'])) {
|
|
|
|
+ return json(['code' => 100, 'msg' => '手机号不合法','data' => []]);
|
|
|
|
+ }
|
|
|
|
+ //检查数据库是否有此推荐人手机号
|
|
|
|
+ $user_tjtel = UsersModel::where('user_tel', $params['user_tjtel'])->find();
|
|
|
|
+ if (!$user_tjtel) {
|
|
|
|
+ return json(['code' => 100, 'msg' => '无此推荐人','data' => []]);
|
|
|
|
+ }
|
|
|
|
+ //判断验证码是否正确
|
|
|
|
+ if (!Cache::get($params['code'])) {
|
|
|
|
+ return json(['code' => 100, 'msg' => '验证码不正确','data' => []]);
|
|
|
|
+ }
|
|
|
|
+ Cache::rm($params['code']);
|
|
|
|
+ $upddata = array(
|
|
|
|
+ 'user_tel' => $params['user_tel'],
|
|
|
|
+ 'user_tjtel' => $params['user_tjtel'],
|
|
|
|
+ 'type' => 1,
|
|
|
|
+ );
|
|
|
|
+ $updUser_tel = Db::name('users')->where('user_id', $params['user_id'])->update($upddata);
|
|
|
|
+ if ($updUser_tel) {
|
|
|
|
+ $token = self::settoken($params['user_id']);
|
|
|
|
+ $res = array(
|
|
|
|
+ "user_id" => $params['user_id'],
|
|
|
|
+ "token" => $token,
|
|
|
|
+ );
|
|
|
|
+ return json(['code' => 200, 'msg' => '绑定成功','data' => []]);
|
|
|
|
+ } else {
|
|
|
|
+ Db::name('users')->where('user_id', $params['user_id'])->delete(); // 绑定失败删除此用户
|
|
|
|
+ return json(['code' => 100, 'msg' => '绑定失败','data' => []]);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -140,9 +219,6 @@ class Register extends Api
|
|
if (isset($params['user_tel'])) {
|
|
if (isset($params['user_tel'])) {
|
|
if (preg_match($check, $params['user_tel'])) {
|
|
if (preg_match($check, $params['user_tel'])) {
|
|
$issettel = UsersModel::where('user_tel', $params['user_tel'])->find(); //判断手机号是否存在
|
|
$issettel = UsersModel::where('user_tel', $params['user_tel'])->find(); //判断手机号是否存在
|
|
- if ($issettel) {
|
|
|
|
- return $this->result('该手机号已存在', [], 100);
|
|
|
|
- }
|
|
|
|
$code = $this->setCode();
|
|
$code = $this->setCode();
|
|
$tpl_value = '#code#=' . $code . '&#company#=优享街';
|
|
$tpl_value = '#code#=' . $code . '&#company#=优享街';
|
|
$smsConf = array(
|
|
$smsConf = array(
|
|
@@ -156,22 +232,21 @@ class Register extends Api
|
|
$result = json_decode($content, true);
|
|
$result = json_decode($content, true);
|
|
$error_code = $result['error_code'];
|
|
$error_code = $result['error_code'];
|
|
if ($error_code == 0) {
|
|
if ($error_code == 0) {
|
|
- return $this->result('发送成功', $code, '200');
|
|
|
|
|
|
+ return json(['code' => 200, 'msg' => '发送成功','data' => $code]);
|
|
} else {
|
|
} else {
|
|
- return $this->result('请求失败', [], '100');
|
|
|
|
|
|
+ return json(['code' => 100, 'msg' => '请求失败','data' => []]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- return $this->result('手机号不合法', [], 100);
|
|
|
|
|
|
+ return json(['code' => 100, 'msg' => '手机号不合法','data' => []]);
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- return $this->result('手机号不能为空', [], 100);
|
|
|
|
|
|
+ return json(['code' => 100, 'msg' => '手机号不能为空','data' => []]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- //发送短信
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 请求接口返回内容
|
|
* 请求接口返回内容
|
|
|
|
+ * @ApiInternal
|
|
* @param string $url [请求的URL地址]
|
|
* @param string $url [请求的URL地址]
|
|
* @param string $params [请求的参数]
|
|
* @param string $params [请求的参数]
|
|
* @param int $ipost [是否采用POST形式]
|
|
* @param int $ipost [是否采用POST形式]
|
|
@@ -207,8 +282,13 @@ class Register extends Api
|
|
curl_close($ch);
|
|
curl_close($ch);
|
|
return $response;
|
|
return $response;
|
|
}
|
|
}
|
|
-
|
|
|
|
//生成唯一六位随机数
|
|
//生成唯一六位随机数
|
|
|
|
+ /**
|
|
|
|
+ * 生成唯一六位随机数
|
|
|
|
+ *
|
|
|
|
+ * 可以通过@ApiInternal忽略请求的方法
|
|
|
|
+ * @ApiInternal
|
|
|
|
+ */
|
|
public function setCode()
|
|
public function setCode()
|
|
{
|
|
{
|
|
$code = rand('1000', '9999');
|
|
$code = rand('1000', '9999');
|
|
@@ -218,5 +298,16 @@ class Register extends Api
|
|
Cache::set($code, $code, 600);
|
|
Cache::set($code, $code, 600);
|
|
return $code;
|
|
return $code;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 生成token
|
|
|
|
+ *
|
|
|
|
+ * 可以通过@ApiInternal忽略请求的方法
|
|
|
|
+ * @ApiInternal
|
|
|
|
+ */
|
|
|
|
+ public function settoken($user_id)
|
|
|
|
+ {
|
|
|
|
+ $token = Random::uuid();
|
|
|
|
+ \app\common\library\Token::set($token, $user_id, 0);
|
|
|
|
+ return $tokenInfo = \app\common\library\Token::get($token);
|
|
|
|
+ }
|
|
}
|
|
}
|