123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <?php
- namespace app\api\controller;
- use app\api\model\UsersModel;
- use app\common\controller\Api;
- use fast\Random;
- use think\Cache;
- use think\Controller;
- use think\Db;
- /**
- * 注册接口
- */
- class Register extends Controller
- {
- protected $noNeedRight = '*';
- protected $noNeedLogin = '*';
- /**
- * 手机号注册
- * @ApiMethod (POST)
- * @param string $user_tel 账号
- * @param string $code 验证码
- * @param string $user_tjtel 推荐人手机号
- * @param string $user_pwd 密码
- * @param string $type 默认0
- */
- public function register()
- {
- $params = $this->request->post();
- if (isset($params['user_tel'])) { //验证手机号是否合法
- $check = '/^(1(([35789][0-9])|(47)))\d{8}$/';
- if (preg_match($check, $params['user_tel'])) {
- if (isset($params['user_tjtel'])) { //验证推荐人手机号
- $issetTjTel = UsersModel::where('user_tel', $params['user_tjtel'])->find();
- if (!$issetTjTel) {
- return json(['code' => 100, 'msg' => '未找到推荐人手机号','data' => []]);
- }
- $addUserMember = Db::name('Users')->where('user_tel', $params['user_tjtel'])->setInc('user_member_count', 1); // 给上机的下级成员总数加1
- } else {
- return json(['code' => 100, 'msg' => '请填写推荐人手机号','data' => []]);
- }
- if (isset($params['user_tel'])) { //验证用户手机号
- $issetTjTel = UsersModel::where('user_tel', $params['user_tel'])->find();
- if ($issetTjTel) {
- return json(['code' => 100, 'msg' => '此手机号已被注册','data' => []]);
- }
- } else {
- return json(['code' => 100, 'msg' => '请填写手机号','data' => []]);
- }
- if (!isset($params['code']) || !Cache::get($params['code'])) { //验证验证码是否错误
- return json(['code' => 100, 'msg' => '验证码错误','data' => []]);
- }
- $userModel = new UsersModel(); //实例化usermodel
- if ($params['type'] == 0) { //使用手机号注册
- $rules = [
- 'code' => 'require|number',
- 'user_pwd' => 'require|max:18|min:6'
- ];
- $msg = [
- 'code.require' => "验证码不能为空",
- 'code.number' => "验证码必须为数字",
- 'user_pwd.require' => '密码不能为空',
- 'user_pwd.max' => '密码长度过长',
- 'user_pwd.min' => '密码最少六位',
- ];
- $validata = $this->validate($params, $rules, $msg);
- if (is_string($validata)) {
- return json(['code' => 100, 'msg' => $validata,'data' => []]);
- }
- $user_avatar = config('site.httpurl') . '/uploads/logo.png';
- $data = array(
- 'user_nickname' => '优-' . rand(10000, 99999),
- 'user_tel' => $params['user_tel'],
- 'user_pwd' => sha1(md5($params['user_pwd'])),
- 'user_avatar' => $user_avatar,
- 'create_time' => date('Y-m-d H:i:s', time()),
- 'user_tjtel' => $params['user_tjtel'],
- 'type' => 0,
- );
- $addUser = $userModel->allowField(true)->save($data);
- if ($addUser) {
- Cache::rm($params['code']);//删除验证码缓存
- return json(['code' => 200, 'msg' => '注册成功','data' => []]);
- } else {
- return json(['code' => 100, 'msg' => '注册失败','data' => []]);
- }
- }
- } else {
- return json(['code' => 100, 'msg' => '手机号不合法','data' => []]);
- }
- } else {
- 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' => 10, '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' => []]);
- }
- }
- /**
- * 手机号短信发送
- * @ApiMethod (POST)
- * @param string $user_tel 账号
- */
- public function registerTel()
- {
- $sendUrl = config('site.sendurl'); //短信接口的URL
- $params = $this->request->post();
- $check = '/^(1(([35789][0-9])|(47)))\d{8}$/';
- if (isset($params['user_tel'])) {
- if (preg_match($check, $params['user_tel'])) {
- $issettel = UsersModel::where('user_tel', $params['user_tel'])->find(); //判断手机号是否存在
- $code = $this->setCode();
- $tpl_value = '#code#=' . $code . '&#company#=优享街';
- $smsConf = array(
- 'key' => config('site.key'), //您申请的APPKEY
- 'mobile' => $params['user_tel'], //接受短信的用户手机号码
- 'tpl_id' => '203667', //您申请的短信模板ID,根据实际情况修改
- 'tpl_value' => $tpl_value //您设置的模板变量,根据实际情况修改
- );
- $content = $this->juhecurl($sendUrl, $smsConf, 1); //请求发送短信
- if ($content) {
- $result = json_decode($content, true);
- $error_code = $result['error_code'];
- if ($error_code == 0) {
- return json(['code' => 200, 'msg' => '发送成功','data' => $code]);
- } else {
- return json(['code' => 100, 'msg' => '请求失败','data' => []]);
- }
- }
- } else {
- return json(['code' => 100, 'msg' => '手机号不合法','data' => []]);
- }
- } else {
- return json(['code' => 100, 'msg' => '手机号不能为空','data' => []]);
- }
- }
- /**
- * 请求接口返回内容
- * @ApiInternal
- * @param string $url [请求的URL地址]
- * @param string $params [请求的参数]
- * @param int $ipost [是否采用POST形式]
- * @return string
- */
- function juhecurl($url, $params = false, $ispost = 0)
- {
- $httpInfo = array();
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- if ($ispost) {
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
- curl_setopt($ch, CURLOPT_URL, $url);
- } else {
- if ($params) {
- curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
- } else {
- curl_setopt($ch, CURLOPT_URL, $url);
- }
- }
- $response = curl_exec($ch);
- if ($response === FALSE) {
- //echo "cURL Error: " . curl_error($ch);
- return false;
- }
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
- curl_close($ch);
- return $response;
- }
- //生成唯一六位随机数
- /**
- * 生成唯一六位随机数
- *
- * 可以通过@ApiInternal忽略请求的方法
- * @ApiInternal
- */
- public function setCode()
- {
- $code = rand('1000', '9999');
- if (Cache::get($code)) {
- $code = self::setCode();
- }
- Cache::set($code, $code, 600);
- 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);
- }
- }
|