ConfigController.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/12/5/005
  6. * Time: 9:50
  7. */
  8. namespace backend\controllers;
  9. use backend\models\Config;
  10. class ConfigController extends BaseController
  11. {
  12. const EVENT_CMD_DEL = 'del';
  13. /**
  14. * 获取配置详情
  15. */
  16. public function actionGetConfig()
  17. {
  18. $cache = \Yii::$app->cache;
  19. $config = $cache->get('config');
  20. if ($config == null) {
  21. $config = Config::findOne(1);
  22. $cache->set('config', $config, 86400);
  23. }
  24. return $this->outJson(1, $config);
  25. }
  26. /**
  27. * 更新配置
  28. */
  29. public function actionUpdate()
  30. {
  31. $request = \Yii::$app->request->getBodyParams();
  32. $config = Config::findOne(1);
  33. $config->setAttributes($request, false);
  34. if ($config->save()) {
  35. $cache = \Yii::$app->cache;
  36. $cache->set('config', Config::findOne(1));
  37. // 更新我们 redis 中的数据
  38. $cache = \Yii::$app->redis;
  39. $cache->set('exchangeRate', $config['exchangerate']);
  40. }
  41. $configModel = new Config();
  42. // 事件的绑定
  43. $this->on(self::EVENT_CMD_DEL, [$configModel, 'updateEvent']);
  44. // 事件的触发
  45. $this->trigger(self::EVENT_CMD_DEL);
  46. return $this->outJson(1, $config, '操作成功');
  47. }
  48. /**
  49. * 清除缓存
  50. */
  51. public function actionClearCache()
  52. {
  53. $cache = \Yii::$app->cache;
  54. $cache->flush();
  55. $config = Config::findOne(1);
  56. $cache->set('CacheDepositJob', false);
  57. $cache->set('CacheVolumeJob', false);
  58. $cache->set('CommissionJobRun', false);
  59. $cache->set('CommissionJobRunTime', $config->commission_deal_time);
  60. $cache->set('config', $config);
  61. return $this->outJson(1, $cache, '操作成功');
  62. }
  63. }