Browse Source

restapi user add or update

Oleg K 2 years ago
parent
commit
23da078822

+ 2 - 1
composer.json

@@ -22,7 +22,8 @@
         "rmrevin/yii2-fontawesome": "^3.4",
         "kartik-v/yii2-widget-select2": "dev-master",
         "vlucas/phpdotenv": "^5.3",
-        "dektrium/yii2-user": "^0.9.14"
+        "dektrium/yii2-user": "^0.9.14",
+        "udokmeci/yii2-phone-validator": "^1.0"
     },
     "require-dev": {
         "yiisoft/yii2-debug": "~2.1.0",

File diff suppressed because it is too large
+ 491 - 179
composer.lock


+ 1 - 0
config/web.php

@@ -76,6 +76,7 @@ $config = [
                 '/' => 'site/index',
                 'pricing/<action:[\w\-]+>' => 'pricing/<action>',
                 'profile/<action:[\w\-]+>' => 'profile/<action>',
+                'pay/<action:[\w\-]+>' => 'pay/<action>',
                 //'user/admin/<action:\w+>' => 'user/admin/<action>',
                 'user/<controller:[\w\-]+>/<action:[\w\-]+>' => 'user/<controller>/<action>',
                 /*'<controller>/<action>' => '<controller>/<action>',

+ 26 - 0
controllers/PayController.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace app\controllers;
+
+use Yii;
+use app\models\PayForm;
+
+class PayController extends \yii\web\Controller
+{
+    public function actionIndex()
+    {
+        $model = new PayForm();
+        if ($model->load(Yii::$app->request->post())) {
+            Yii::$app->session->setFlash('success', 'This is the message');
+
+            return $this->refresh();
+        }
+
+        $cc = $model->getAmount();
+
+        return $this->render('index', [
+            'model' => $model,
+        ]);
+    }
+
+}

+ 4 - 0
messages/ru/user.php

@@ -5,4 +5,8 @@ return [
     'Company name' => 'Название компании',
     'Phone' => 'Телефон',
     'Address' => 'Адрес',
+    'Balance' => 'Баланс',
+    'Plan title' => 'Тарифный план',
+    'Plan price' => 'Цена',
+    'Payment online' => 'Оплачено онлайн',
 ];

+ 32 - 0
migrations/m210809_132319_balance_fields.php

@@ -0,0 +1,32 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m210809_132319_balance_fields
+ */
+class m210809_132319_balance_fields extends Migration
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+        $this->addColumn('{{%user}}', 'balance', $this->integer(11)->notNull()->after('address'));
+        $this->addColumn('{{%user}}', 'plan_title', $this->string(255)->null()->after('balance'));
+        $this->addColumn('{{%user}}', 'plan_price', $this->integer(11)->notNull()->after('plan_title'));
+        $this->addColumn('{{%user}}', 'payment_online', $this->smallInteger(3)->null()->after('plan_price'));
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+        $this->dropColumn('{{%user}}', 'balance');
+        $this->dropColumn('{{%user}}', 'plan_title');
+        $this->dropColumn('{{%user}}', 'plan_price');
+        $this->dropColumn('{{%user}}', 'payment_online');
+    }
+
+}

+ 49 - 0
models/PayForm.php

@@ -0,0 +1,49 @@
+<?php
+
+namespace app\models;
+
+use Yii;
+use yii\base\Model;
+
+class PayForm extends Model{
+
+    public $name;
+    public $email;
+    public $subject;
+    public $body;
+    public $verifyCode;
+
+    public function rules()
+    {
+        return [
+            // name, email, subject and body are required
+            [['name', 'email', 'subject', 'body'], 'required'],
+            // email has to be a valid email address
+            ['email', 'email'],
+            // verifyCode needs to be entered correctly
+            ['verifyCode', 'captcha'],
+        ];
+    }
+
+    public function getAmount(){
+        $balance = (int)\Yii::$app->user->identity->balance;
+        $plan_price = (int)\Yii::$app->user->identity->plan_price;
+
+        $price = $balance - $plan_price;
+
+        switch ($price):
+            case $price > 0:
+                $amount = $plan_price; break;
+            case $price < 0:
+                $amount = $plan_price - $balance + $plan_price;
+
+                $msg = '('.$balance.')balance - ('.$plan_price.')plan_price = ' . $price;
+                Yii::$app->session->setFlash('success', $msg, false);
+                break;
+        endswitch;
+
+        return $amount;
+
+    }
+
+}

+ 30 - 6
models/user/User.php

@@ -13,7 +13,7 @@ use yii\helpers\Html;
 use yii\helpers\Url;
 
 use dektrium\user\models\User as BaseUser;
-
+use yii\web\Application as WebApplication;
 
 
 /**
@@ -26,14 +26,18 @@ class User extends BaseUser
     public function attributeLabels()
     {
         return [
-            'uuid'          => \Yii::t('user', 'Nomer Dogovora'),
+            'uuid'              => \Yii::t('user', 'Nomer Dogovora'),
             'username'          => \Yii::t('user', 'Username'),
-            'fio'          => \Yii::t('user', 'Fio'),
-            'cname'          => \Yii::t('user', 'Company name'),
-            'type'          => \Yii::t('user', 'Role'),
+            'fio'               => \Yii::t('user', 'Fio'),
+            'cname'             => \Yii::t('user', 'Company name'),
+            'type'              => \Yii::t('user', 'Role'),
             'email'             => \Yii::t('user', 'Email'),
             'phone'             => \Yii::t('user', 'Phone'),
-            'address'             => \Yii::t('user', 'Address'),
+            'address'           => \Yii::t('user', 'Address'),
+            'balance'           => \Yii::t('user', 'Balance'),
+            'plan_title'        => \Yii::t('user', 'Plan title'),
+            'plan_price'        => \Yii::t('user', 'Plan price'),
+            'payment_online'    => \Yii::t('user', 'Payment online'),
             'registration_ip'   => \Yii::t('user', 'Registration ip'),
             'unconfirmed_email' => \Yii::t('user', 'New email'),
             'password'          => \Yii::t('user', 'Password'),
@@ -75,6 +79,14 @@ class User extends BaseUser
                 'message' => \Yii::t('user', 'This username has already been taken')
             ],
 
+            'fioTrim'     => ['fio', 'trim'],
+            'cnameTrim'     => ['cname', 'trim'],
+            'balanceInt'     => [['balance', 'plan_price', 'payment_online'], 'integer'],
+            'planLength'   => ['plan_title', 'string', 'max' => 255],
+            'addressLength'   => ['address', 'string', 'max' => 255],
+
+            'phone'   =>  ['phone', 'string'],
+
             'roleIsEmpty' => ['type', 'default', 'value' => function($model){return empty($model->type) ? 'client': '';}],
 
             // email rules
@@ -112,5 +124,17 @@ class User extends BaseUser
         return static::findOne(['auth_key' => $token]);
     }
 
+    /** @inheritdoc */
+    public function beforeSave($insert)
+    {
+        if(!$insert){
+            if ($this->getAttribute('balance') > $this->getOldAttribute('balance')){
+                $this->setAttribute('payment_online', NULL);
+            }
+        }
+
+        return parent::beforeSave($insert);
+    }
+
 }
 

+ 7 - 14
modules/api/controllers/UserController.php

@@ -19,22 +19,15 @@ class UserController extends ActiveController
         $errs['err'] = array();
 
         foreach ($requestParams as $param){
-            $user = new User();
-            $user->setScenario('api');
-            $user->uuid = $param['uuid'];
-            $user->fio = $param['fio'];
-            $user->cname = $param['cname'];
-            $user->phone = $param['phone'];
-            $user->address = $param['address'];
-            $user->email = $param['email'];
-            $user->username = $param['username'];
-            $user->password = $param['password'];
-
-            $user->save();
-        }
+            $model = $this->modelClass::find()->where(['uuid'=>$param['uuid']])->one() ?? new $this->modelClass;
 
+            //$model->setScenario('api');
+            $model->attributes = $param;
+            if (!$model->save()) {
+                $errs['err'][$param['uuid']] = $model->getErrors();
+            }
+        }
 
-        $result = $this->modelClass::find()->all();
         return $errs;
     }
 

+ 12 - 4
views/layouts/main.php

@@ -96,13 +96,21 @@ Html::addCssClass($bodyClass, ['hold-transition', 'sidebar-mini', 'layout-fixed'
         <!-- Sidebar -->
         <div class="sidebar">
             <!-- Sidebar user panel (optional) -->
-            <div class="user-panel mt-3 pb-3 mb-3 d-flex">
+            <div class="user-panel mt-3 pb-3 mb-3 d-flex justify-content-center">
                 <div class="image d-none">
                     <!--<img src="../dist/img/user2-160x160.jpg" class="img-circle elevation-2" alt="User Image">-->
                     <?= Html::img('@web/img/logo.png', ['alt' => 'User Image', 'style' => '', 'class'=>'d-none img-circle elevation-2']); ?>
                 </div>
-                <div class="info d-none">
-                    <a href="#" class="d-block">Alexander Pierce</a>
+                <div class="info">
+                    <a href="#" class="d-block" data-toggle="tooltip" title="Баланс грн"><i class="far fa-money-bill-alt text-success mr-1"></i> <?=\Yii::$app->user->identity->balance ?> </a>
+                </div>
+                <div class="info d-flex align-items-center">
+                    <a href="#" class="d-block" data-toggle="tooltip" title="Тариф: <?= \Yii::$app->user->identity->plan_title ?>"><i class="fas fa-puzzle-piece text-success"></i> <?=\Yii::$app->user->identity->plan_price ?></a>
+                    <?php if (\Yii::$app->user->identity->payment_online): ?>
+                        <i class="fas fa-info-circle ml-1 text-warning" data-toggle="tooltip" title="Очікуємо зарахування коштів"></i>
+                    <div class="spinner-border spinner-border-sm text-success ml-1 d-none" role="status" data-toggle="tooltip" title="Очікуємо зарахування коштів">
+                    </div>
+                    <?php endif; ?>
                 </div>
             </div>
 
@@ -130,7 +138,7 @@ Html::addCssClass($bodyClass, ['hold-transition', 'sidebar-mini', 'layout-fixed'
                          [
                              'icon' => FAS::icon('hand-holding-usd', ['class' => ['nav-icon']]),
                              'label' => 'Пополнить счет',
-                             'url' => Yii::$app->homeUrl,
+                             'url' => ['/pay/index'],
                              //'active' => Yii::$app->controller->id === 'site',
                          ],
                          [

+ 24 - 0
views/pay/index.php

@@ -0,0 +1,24 @@
+<?php
+/* @var $this yii\web\View */
+/* @var $form yii\bootstrap4\ActiveForm */
+/* @var $model app\models\ContactForm */
+
+use yii\bootstrap4\ActiveForm;
+use yii\bootstrap4\Html;
+use yii\captcha\Captcha;
+
+$this->title = 'Поповнити рахунок';
+$this->params['breadcrumbs'][] = $this->title;
+?>
+
+
+<?php $form = ActiveForm::begin(['id' => 'payment-form']); ?>
+
+<?= $form->field($model, 'amount')->textInput(['autofocus' => true]) ?>
+
+
+<div class="form-group">
+    <?= Html::submitButton('Сплатити', ['class' => 'btn btn-success', 'name' => 'pay-button']) ?>
+</div>
+
+<?php ActiveForm::end(); ?>

+ 29 - 13
views/profile/index.php

@@ -1,4 +1,6 @@
 <?php
+/** @var $profile \Yii::$app->user->identity */
+
 $this->title = 'Профіль';
 $this->params['breadcrumbs'][] = $this->title;
 ?>
@@ -10,23 +12,33 @@ $this->params['breadcrumbs'][] = $this->title;
         <div class="card card-primary ">
             <div class="card-header">
                 <h3 class="card-title">Загальна інформація</h3>
-                <div class="card-tools"><span data-toggle="tooltip" title="Номер договору" class="badge bg-white"><?= $profile->uuid ?></span></div>
+                <div class="card-tools"><span data-toggle="tooltip" title="Номер договору" class="badge bg-white"># <?= $profile->uuid ?></span></div>
             </div>
             <div class="card-body box-profile">
-                <h3 class="profile-username text-center"><?= $profile->fio ?></h3>
+                <h3 class="profile-username text-center">
+                    <?= $profile->fio ? $profile->fio : '<small>Номер договора: </small><strong>' . $profile->uuid . '</strong>'; ?>
+                </h3>
 
+                <?php if ($profile->cname): ?>
                 <p class="text-muted text-center"><?= $profile->cname ?></p>
+                <?php endif; ?>
 
                 <ul class="list-group list-group-unbordered mb-0">
+                    <?php if ($profile->address): ?>
                     <li class="list-group-item">
                         <?= \rmrevin\yii\fontawesome\FAS::icon('home', ['class' => 'mr-2'])?> <?= $profile->address ?>
                     </li>
+                    <?php endif; ?>
+                    <?php if ($profile->phone): ?>
                     <li class="list-group-item">
                         <?= \rmrevin\yii\fontawesome\FAS::icon('phone-square', ['class' => 'mr-2'])?> <?= $profile->phone ?>
                     </li>
-                    <li class="list-group-item border-0">
+                    <?php endif; ?>
+                    <?php if ($profile->email): ?>
+                    <li class="list-group-item pb-0 border-0">
                         <?= \rmrevin\yii\fontawesome\FAS::icon('envelope', ['class' => 'mr-2'])?> <?= $profile->email ?>
                     </li>
+                    <?php endif; ?>
                 </ul>
             </div>
             <!-- /.card-body -->
@@ -35,31 +47,35 @@ $this->params['breadcrumbs'][] = $this->title;
     </div>
     <!-- /.col -->
     <div class="col-md-4">
-
-
         <!-- Financial Box -->
         <div class="card card-success ">
             <div class="card-header">
                 <h3 class="card-title">Фінансова інформація</h3>
-                <div class="card-tools"><span data-toggle="tooltip" title="Баланс" class="badge bg-white">41 410 грн</span></div>
+                <div class="card-tools"><span data-toggle="tooltip" title="Баланс" class="badge bg-white"><?= $profile->balance ?> грн</span></div>
             </div>
             <!-- /.card-header -->
             <div class="card-body">
-                <h3 class="profile-username text-uppercase text-center text-bold">Базовий</h3>
+                <h3 class="profile-username text-center">
+                    <span class="text-uppercase text-bold"><?= $profile->plan_title ?></span> <small>(<?= $profile->plan_price ?> грн)</small>
+                </h3>
 
                 <p class="text-muted text-center">тарифний план</p>
 
-                <hr>
-
-                <strong><i class="far fa-file-alt mr-1"></i> Опис</strong>
-
-                <p class="text-muted mb-0">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam fermentum enim neque.</p>
+                <ul class="list-group list-group-unbordered mb-0">
+                    <?php if ($profile->payment_online): ?>
+                        <li class="list-group-item border-bottom-0 pb-0 text-danger">
+                            <?= \rmrevin\yii\fontawesome\FAS::icon('credit-card', ['class' => 'mr-2'])?> Очікується поповнення балансу
+                        </li>
+                    <?php endif; ?>
+                </ul>
 
             </div>
             <!-- /.card-body -->
+            <?php if (!$profile->payment_online): ?>
             <div class="card-footer">
-                <?= \yii\bootstrap4\Html::a('Змінити', \yii\helpers\Url::to('/pricing/index'), ['class'=>'btn btn-success btn-block']); ?>
+                <?= \yii\bootstrap4\Html::a('Пополнить', \yii\helpers\Url::to('/pricing/index'), ['class'=>'btn btn-success btn-block']); ?>
             </div>
+            <?php endif; ?>
             <!-- /.card-footer -->
         </div>
         <!-- /.card -->

+ 10 - 25
views/site/index.php

@@ -7,59 +7,44 @@ $this->title = 'Кабінет користувача';
 <div class="site-index">
     <div class="body-content">
 
-        <div class="row">
-            <div class="col-md-4">
+        <div class="row d-none">
+            <div class="col-md-4 col-lg-3">
                 <div class="info-box bg-gradient-warning">
                     <span class="info-box-icon"><i class="fas fa-hryvnia text-xl"></i></span>
 
                     <div class="info-box-content">
                         <span class="info-box-text">Баланс</span>
-                        <p class="info-box-number h3 m-0">41 410.0</p>
+                        <p class="info-box-number h3 m-0"><?= \Yii::$app->user->identity->balance; ?> грн</p>
 
-                        <div class="progress">
+                        <div class="progress d-none">
                             <div class="progress-bar" style="width: 100%"></div>
                         </div>
-                        <p class="progress-description">
+                        <p class="progress-description d-none">
                             <a href="#" class="text-muted">Поповнити рахунок <i class="fas fa-arrow-circle-right"></i></a>
                         </p>
                     </div>
                     <!-- /.info-box-content -->
                 </div>
             </div>
-            <div class="col-md-4">
+            <div class="col-md-4 col-lg-3">
                 <div class="info-box bg-gradient-success">
                     <span class="info-box-icon"><i class="fas fa-puzzle-piece text-xl"></i></span>
 
                     <div class="info-box-content">
                         <span class="info-box-text">Тарифний план</span>
-                        <p class="info-box-number m-0"><span class="h3 text-uppercase text-bold">Базовий</span></p>
+                        <p class="info-box-number m-0"><span class="h3 text-uppercase text-bold"><?= \Yii::$app->user->identity->plan_title; ?></span> <small>(<?= \Yii::$app->user->identity->plan_price; ?> грн)</small></p>
 
-                        <div class="progress">
+                        <div class="progress d-none">
                             <div class="progress-bar" style="width: 70%"></div>
                         </div>
-                        <p class="progress-description">Закінчується</p>
+                        <p class="progress-description d-none">Закінчується</p>
                     </div>
                     <!-- /.info-box-content -->
                 </div>
             </div>
-            <div class="col-md-4 d-none">
-                <div class="small-box bg-success">
-                    <div class="inner">
-                        <h3 class="text-uppercase">Базовий</h3>
-
-                        <p>Тарифний план</p>
-                    </div>
-                    <div class="icon">
-                        <i class="fas fa-puzzle-piece "></i>
-                    </div>
-                    <a href="#" class="small-box-footer">
-                        Змінити <i class="fas fa-arrow-circle-right"></i>
-                    </a>
-                </div>
-            </div>
         </div>
 
-        <hr>
+        <hr class="d-none">
 
         <div class="row">
             <div class="col-lg-4">

+ 5 - 5
widgets/Alert.php

@@ -31,11 +31,11 @@ class Alert extends \yii\bootstrap4\Widget
      * - value: the bootstrap alert type (i.e. danger, success, info, warning)
      */
     public $alertTypes = [
-        'error'   => 'alert-danger',
-        'danger'  => 'alert-danger',
-        'success' => 'alert-success',
-        'info'    => 'alert-info',
-        'warning' => 'alert-warning'
+        'error'   => 'alert-danger show',
+        'danger'  => 'alert-danger show',
+        'success' => 'alert-success show',
+        'info'    => 'alert-info show',
+        'warning' => 'alert-warning show'
     ];
     /**
      * @var array the options for rendering the close button tag.