コード例 #1
0
 /**
  * Register billing account
  *
  * <b>Request Type</b>: POST<br/><br/>
  * <b>Request Endpoint</b>:http://{server-domain}/api/site/register<br/><br/>
  * <b>Content-type</b>: application/json<br/><br/>
  * <b>Summary</b>: This api is used for registering user.
  * <br/><br/>
  *
  * <b>Request Params</b>:<br/>
  *     name: string, the user name<br/>
  *     email: string, the user email<br/>
  *     password: string, the user password<br/>
  *     <br/><br/>
  *
  * <b>Response Params:</b><br/>
  *     ack: integer, mark the create result, 1 means create successfully, 0 means create fail<br/>
  *     message: string, if create fail, it contains the error message<br/>
  *     data: array, json array to describe all users detail information<br/>
  *     <br/><br/>
  *
  * <b>Request Example:</b><br/>
  * <pre>
  * {
  *     "name" : "harrysun",
  *     "email" : "*****@*****.**",
  *     "company" : "Augmentum",
  *     "phone" : "13027785897",
  *     "captcha" : "123456"
  * }
  * </pre>
  * <br/><br/>
  *
  * <b>Response Example</b>:<br/>
  * <pre>
  * {
  *    'message': 'Register success.'
  * }
  * </pre>
  */
 public function actionRegister()
 {
     //set language zh_cn when sign up
     \Yii::$app->language = LanguageUtil::LANGUAGE_ZH;
     $data = $this->getParams();
     if (empty($data['name']) || empty($data['email']) || empty($data['company']) || empty($data['phone']) || empty($data['captcha']) || empty($data['position'])) {
         throw new BadRequestHttpException('missing param');
     }
     $phone = $data['phone'];
     $this->attachBehavior('CaptchaBehavior', new CaptchaBehavior());
     $this->checkCaptcha($phone, $data['captcha']);
     $email = mb_strtolower($data['email']);
     $user = User::getByEmail($email);
     if (!empty($user)) {
         //check if user active fail, send email again
         if (!$user->isActivated) {
             $validation = Validation::getByUserId($user->_id);
             return $this->_sendRegistEmail($user->_id, $data, $validation);
         } else {
             throw new InvalidParameterException(['email' => \Yii::t('common', 'unique_feild_email')]);
         }
     }
     $accountId = Account::create($data['company'], $phone, $data['name']);
     $user = new User();
     $user->email = $email;
     $user->position = $data['position'];
     $user->accountId = $accountId;
     $user->role = User::ROLE_ADMIN;
     $user->isActivated = User::NOT_ACTIVATED;
     $user->avatar = Yii::$app->params['defaultAvatar'];
     $user->language = LanguageUtil::DEFAULT_LANGUAGE;
     if ($user->save()) {
         return $this->_sendRegistEmail($user->_id, $data);
     } else {
         Account::deleteAll(['_id' => $accountId]);
         if ($user->getErrors('email')) {
             throw new InvalidParameterException(['email' => \Yii::t('common', 'unique_feild_email')]);
         }
         throw new ServerErrorHttpException('regist fail');
     }
 }