コード例 #1
0
 /**
  * Creates a new Staff model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Staff();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->staff_id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #2
0
 /**
  * when crate a staff successful,and send sms fail,we need to delete the staff
  */
 public function actionCreate()
 {
     $params = $this->getParams();
     if (empty($params['phone']) || empty($params['channel']['channelId']) || empty($params['badge']) || empty($params['storeId'])) {
         throw new BadRequestHttpException('params missing');
     }
     $accountId = $this->getAccountId();
     $params['accountId'] = $accountId;
     $existsEmpID = Staff::checkUnique($params['badge'], $accountId);
     if ($existsEmpID) {
         throw new InvalidParameterException(['badge' => Yii::t("store", "badge_exists")]);
     }
     $storeId = $params['storeId'];
     $params['storeId'] = new \MongoId($storeId);
     if (false === Staff::checkPhone($params['storeId'], $params['phone'])) {
         throw new InvalidParameterException(['phone' => Yii::t("store", 'phone_exists')]);
     }
     $data = Staff::setQrcodeParam($params['channel']['channelId']);
     $params = array_merge($params, $data);
     $params['salt'] = StringUtil::rndString(6, 1);
     $staff = new Staff();
     $staff->load($params, '');
     $result = 'success';
     if ($staff->save()) {
         if (!empty($params['useWebhook'])) {
             $eventData = ['type' => Webhook::EVENT_STAFF_CREATED, 'store_id' => $storeId, 'staff_id' => (string) $staff->_id, 'phone' => $params['phone'], 'badge' => $params['badge'], 'channel' => ['id' => $params['channel']['channelId'], 'name' => $params['channel']['channelName'], 'type' => $params['channel']['channelType']], 'origin' => Member::PORTAL, 'account_id' => (string) $accountId, 'created_at' => MongodbUtil::MongoDate2String($staff->createdAt, \DateTime::ATOM)];
             Yii::$app->webhook->triggerEvent($eventData);
         } else {
             //send mobile message
             $template = Staff::getMobileTemplate($accountId);
             $status = MessageUtil::sendMobileMessage($params['phone'], $template);
             if (false === $status) {
                 $result = 'fail';
                 //delete the staff
                 Staff::getCollection()->remove(['_id' => $staff->_id]);
                 LogUtil::error(['message' => 'Faild to send message', 'template' => $template, 'params' => $params], 'staff');
             }
         }
     } else {
         throw new ServerErrorHttpException(Yii::t('store', 'fail_to_create'));
     }
     return ['result' => $result];
 }