Kaynağa Gözat

汇率配置升级修改

kevin_zhangl 3 yıl önce
ebeveyn
işleme
f2a68c4d0c

+ 4 - 1
backendApi/modules/v1/controllers/ConfigController.php

@@ -855,8 +855,11 @@ class ConfigController extends BaseController {
      * @throws \yii\web\HttpException
      */
     public function actionExchangeRate() {
+        $post = Yii::$app->request->post();
+        unset($post['synchronize']);
+
         $form = new ConfigForm();
-        if (Yii::$app->request->post() && $form->load(Yii::$app->request->post(), '')) {
+        if (\Yii::$app->request->isPost && $form->load($post, '')) {
             if ($form->updateExchangeRate()) {
                 Config::updateToCache();
                 return static::notice('更新汇率配置成功');

+ 38 - 16
backendEle/src/views/config/exchange-rate.vue

@@ -3,15 +3,18 @@
         <div class="white-box">
             <el-form ref="form" :model="form" label-width="250px" class="form-page" style="margin-top: 25px;">
                 <el-form-item :label="form.TITLE" :key="form.CONFIG_NAME">
-                    <el-input v-model="form.VALUE" placeholder="请输入内容">
+                    <el-input v-model="form.VALUE">
                         <template slot="append">naira / $</template>
                     </el-input>
                 </el-form-item>
-                <el-form-item>
-                    <span style="font-weight: bold; color: red; font-size: 12px;">提示:修改汇率会刷新商品的销售价格(naira),请谨慎操作!!</span>
+                <el-form-item label="刷新商品价格">
+                    <el-switch v-model="synchronize"></el-switch>
+                </el-form-item>
+                <el-form-item label="" v-show="notice">
+                    <span style="font-weight: bold; color: red; font-size: 14px;">开启后会根据最新汇率刷新商品的奈拉价格,请谨慎操作!!</span>
                 </el-form-item>
                 <el-form-item>
-                    <el-button type="primary" size="small" @click="onSubmit" :loading="submitButtonStat">保存并刷新商品价格</el-button>
+                    <el-button :type="btnType" size="medium" @click="onSubmit" :loading="submitButtonStat">{{ btnTxt }}</el-button>
                 </el-form-item>
             </el-form>
         </div>
@@ -20,9 +23,10 @@
 
 <script>
 import network from '@/utils/network'
+import locale from "element-ui/lib/locale/lang/en";
 export default {
     name: 'config_exchange_rate',
-    mounted() {
+    created() {
         this.getData()
     },
     data() {
@@ -34,30 +38,36 @@ export default {
             },
             loading: true,
             submitButtonStat: false,
+            btnType: 'primary',
+            btnTxt: '保存汇率',
+            notice: false,
+            synchronize: false,
         }
     },
     methods: {
         onSubmit() {
-            let _this = this
-            _this.$confirm('修改汇率会刷新商品销售价格(naira), 是否继续?', '提示', {
+            this.$confirm(this.btnTxt, '提示', {
                 confirmButtonText: '确定',
                 cancelButtonText: '取消',
                 type: 'warning'
             }).then(() => {
-                _this.submitButtonStat = true
-                network.postData('config/exchange-rate', this.form).then(response => {
-                    _this.$message({
+                this.submitButtonStat = true
+
+                let param = this.form
+                param.synchronize = this.synchronize
+                return network.postData('config/exchange-rate', param).then(response => {
+                    this.$message({
                         message: response,
                         type: 'success'
                     })
-                    _this.submitButtonStat = false
-                    _this.getData()
-                }).catch(response => {
-                    _this.submitButtonStat = false
-                    _this.getData()
+                    this.submitButtonStat = false
+                    location.reload()
+                }).catch(_ => {
+                    this.submitButtonStat = false
+                    this.getData()
                 })
             }).catch(() => {
-                _this.submitButtonStat = false
+                this.submitButtonStat = false
             })
         },
         getData() {
@@ -66,6 +76,18 @@ export default {
                 this.loading = false
             })
         }
+    },
+    watch: {
+        // 监听是否同步刷新商品价格
+        synchronize: {
+            handler(modern, origin) {
+                this.btnType = (modern === true) ? 'danger' : 'primary'
+                this.btnTxt = (modern === true) ? '保存并刷新商品价格' : '保存汇率'
+                this.notice = (modern === true)
+            },
+            // 深度监听
+            deep: true,
+        }
     }
 }
 </script>

+ 8 - 5
common/models/forms/ConfigForm.php

@@ -314,7 +314,7 @@ class ConfigForm extends Model
      * 更新汇率配置
      * @return bool|null
      */
-    public function updateExchangeRate()
+    public function updateExchangeRate(): ?bool
     {
         if(!$this->validate()){
             return null;
@@ -328,10 +328,13 @@ class ConfigForm extends Model
         try{
             Config::updateAll(['VALUE' => $postData['VALUE'], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => 'exchangeRate']);
 
-            // 异步处理添加任务
-            $taskKey = \Yii::$app->swooleAsyncTimer->asyncHandle('config/update-exchange-rate', \Yii::$app->request->post());
-            if($taskKey === false){
-                throw new Exception('请求异步服务器失败');
+            if ($postData['synchronize']) {
+                unset($postData['synchronize']);
+                // 异步属性商品价格
+                $taskKey = \Yii::$app->swooleAsyncTimer->asyncHandle('config/update-exchange-rate', $postData);
+                if($taskKey === false){
+                    throw new Exception('请求异步服务器失败');
+                }
             }
 
             $transaction->commit();