Пример #1
0
 public function onReceive(swoole_server $serv, $fd, $fromId, $data)
 {
     //echo "onReceive, worker_id" . $serv->worker_id . "\n";
     /**
      * Handle the request
      */
     $this->sysconfig = (require __DIR__ . '/../../config/sysconfig.php');
     $this->sdkconfig = (require __DIR__ . '/oauth/sdkconfig.php');
     if (!$this->usermodel) {
         $this->usermodel = new UsersModel();
     }
     if (!$this->captchamodel) {
         $this->captchamodel = new CaptchaModel();
     }
     if (!$this->emailverifymodel) {
         $this->emailverifymodel = new EmailVerifyModel();
     }
     if (!$this->userwalletmodel) {
         $this->userwalletmodel = new UserWalletsModel();
     }
     // 非文件上传
     if (empty($this->files[$fd])) {
         $req = json_decode($data, true);
         if ($req === false) {
             return $this->msg($fd, 400, 'Error Request');
         } else {
             if (!in_array($req['cmd'], $this->method)) {
                 return $this->msg($fd, 400, 'Error Method');
             }
         }
         try {
             $res = array();
             echo date("Y-m-d H:i:s") . ", client[{$fd}], cmd: " . $req['cmd'] . "\n";
             switch ($req['cmd']) {
                 case 'checkMobi':
                     // 验证注册手机号
                     $res = $this->checkMobi($req['args']['0']);
                     break;
                 case 'upload':
                     //上传图片
                     $filePath = __DIR__ . '/../../public/' . $req['path'];
                     $file = $req['path'] . '/' . $req['name'];
                     $file_real = $filePath . '/' . $req['name'];
                     //尝试创建目录
                     if (!Inputs::createdir($filePath, 0777)) {
                         return $this->msg($fd, 5001, 'can not create catalog.');
                     }
                     $fp = fopen($file_real, 'w');
                     if (!$fp) {
                         return $this->msg($fd, 504, 'can not open file.');
                     } else {
                         $res = 'transmission start';
                         $this->files[$fd] = array('fp' => $fp, 'name' => $file, 'size' => $req['size'], 'recv' => 0);
                     }
                     break;
                 case 'regFromSwoole':
                     //注册
                     $res = $this->regFromSwoole($req['args']['0'], $req['args']['1'], $req['args']['2'], $req['args']['3'], $req['args']['4']);
                     break;
                 case 'makeCaptcha':
                     //发送验证码
                     $res = $this->makeCaptcha($req['args']['0'], $req['args']['1'], $req['args']['2']);
                     break;
                 case 'checkCaptcha':
                     //验证码验证
                     $res = $this->checkCaptcha($req['args']['0'], $req['args']['1'], $req['args']['2'], $req['args']['3']);
                     break;
                 case 'capValid':
                     //验证完成
                     $res = $this->capValid($req['args']['0'], $req['args']['1'], $req['args']['2'], $req['args']['3']);
                     break;
                 case 'getUserInfoByMobi':
                     //根据手机号获取用户信息
                     $res = $this->getUserInfoByMobi($req['args']['0']);
                     break;
                 case 'userInfoByIds':
                     //根据手机号获取用户信息
                     $res = $this->userInfoByIds($req['args']['0']);
                     break;
                 case 'emailverify':
                     //验证完成
                     $res = $this->emailverify($req['args']['0'], $req['args']['1'], $req['args']['2'], $req['args']['3']);
                     break;
                 case 'createWallet':
                     $res = $this->createWallet($req['args']['0']);
                     break;
                 case 'coinsInfo':
                     $res = $this->coinsInfo($req['args']['0']);
                     break;
                 case 'reset':
                     $res = $this->reset($req['args']['0'], $req['args']['1'], $req['args']['2']);
                     break;
                 case 'checkUserName':
                     $res = $this->checkUserName($req['args']['0'], $req['args']['1']);
                     break;
                 case 'setUserName':
                     $res = $this->setUserName($req['args']['0'], $req['args']['1']);
                     break;
                 case 'isExistEmail':
                     $res = $this->isExistEmail($req['args']['0']);
                     break;
                 case 'updateLevel':
                     $res = $this->updateLevel($req['args']['0']);
                     break;
                 case 'getUserInfo':
                     $res = $this->getUserInfo($req['args']['0']);
                     break;
                 case 'checkInReceive':
                     $res = $this->checkInReceive($req['args']['0'], $req['args']['1']);
                     break;
                 case 'checkThirdLogin':
                     $res = $this->checkThirdLogin($req['args']['0'], $req['args']['1']);
                     break;
                 case 'unbindThird':
                     $res = $this->unbindThird($req['args']['0'], $req['args']['1']);
                     break;
                 case 'bindThird':
                     $res = $this->bindThird($req['args']['0'], $req['args']['1'], $req['args']['2'], $req['args']['3']);
                     break;
                 case 'modifyUser':
                     $res = $this->modifyUser($req['args']['0'], $req['args']['1'], $req['args']['2']);
                     break;
                 case 'sinaOauth':
                     $res = $this->sinaOauth($req['args']['0'], $req['args']['1'], $req['args']['2']);
                     break;
                 case 'qqOauth':
                     $res = $this->qqOauth($req['args']['0'], $req['args']['1']);
                     break;
                 case 'oauthReg':
                     $res = $this->oauthReg($req['args']['0'], $req['args']['1'], $req['args']['2'], $req['args']['3'], $req['args']['4'], $req['args']['5'], $req['args']['6'], $req['args']['7']);
                     break;
             }
             return $this->msg($fd, 200, $res);
         } catch (SwooleExcept $e) {
             return $this->msg($fd, 503, $e->getMessage());
         }
     } else {
         $info =& $this->files[$fd];
         $fp = $info['fp'];
         $file = $info['name'];
         if (!fwrite($fp, $data)) {
             $this->msg($fd, 600, 'fwrite failed. transmission stop.');
             unlink($file);
             unset($this->files[$fd]);
         } else {
             $info['recv'] += strlen($data);
             if ($info['recv'] >= $info['size']) {
                 $this->msg($fd, 200, 'Success, transmission finish. Close connection.');
                 unset($this->files[$fd]);
             }
         }
     }
 }