ConfigForm.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Date;
  5. use common\libs\logging\operate\AdminOperate;
  6. use common\models\Config;
  7. use yii\db\Exception;
  8. use yii\helpers\Json;
  9. /**
  10. * Login form
  11. */
  12. class ConfigForm extends Model
  13. {
  14. const BONUS_TO_STRING_FIELD = [
  15. //共享奖
  16. 'shareRecIncomeFrom',
  17. 'shareIncomeFrom',
  18. //管理奖
  19. 'parentOddIncomeFrom_0',
  20. 'parentOddIncomeFrom_1',
  21. 'parentOddIncomeFrom_2',
  22. 'parentOddIncomeFrom_3',
  23. //共享奖-复消
  24. 'fxShareRecIncomeFrom',
  25. 'fxShareIncomeFrom',
  26. //管理奖
  27. 'fxParentOddIncomeFrom_0',
  28. 'fxParentOddIncomeFrom_1',
  29. 'fxParentOddIncomeFrom_2',
  30. 'fxParentOddIncomeFrom_3',
  31. //服务奖
  32. 'decRoleBonusFrom',
  33. ];
  34. const BONUS_JSON_FIELD = [
  35. 'jxStandard'
  36. ];
  37. public function init() {
  38. parent::init();
  39. $this->adminOperateLogger = new AdminOperate([
  40. 'fetchClass' => Config::class,
  41. ]);
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function rules()
  47. {
  48. return [
  49. //[['postData'], 'required'],
  50. ];
  51. }
  52. public function attributeLabels()
  53. {
  54. return [
  55. //'postData' => '传入数据',
  56. ];
  57. }
  58. /**
  59. * 更新配置
  60. * @return bool|null
  61. * @throws Exception
  62. */
  63. public function updateBase(){
  64. if(!$this->validate()){
  65. return null;
  66. }
  67. $configs = Config::find()->where("TYPE='base'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  68. $configNames = [];
  69. foreach ($configs as $value){
  70. $configNames[] = $value['CONFIG_NAME'];
  71. }
  72. $beforeData = Config::getConfigByType('base');
  73. $this->adminOperateLogger->saveBeforeContent=$beforeData;
  74. $postData = \Yii::$app->request->post();
  75. $transaction = \Yii::$app->db->beginTransaction();
  76. try{
  77. foreach ($configs as $key => $value) {
  78. if (array_key_exists($key, $postData)) {
  79. if (!$postData[$key]) $postData[$key] = 0;
  80. Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
  81. }
  82. }
  83. $transaction->commit();
  84. } catch (Exception $e){
  85. $transaction->rollBack();
  86. $this->addError('updateBase', $e->getMessage());
  87. return null;
  88. }
  89. $afterData = Config::getConfigByType('base');
  90. // 关闭会员端,清空redis
  91. if (is_array($postData) && isset($postData['siteClose']) && $postData['siteClose'] === true) {
  92. \Yii::$app->redis->flushdb();
  93. }
  94. $this->adminOperateLogger->saveAfterContent=$afterData;
  95. unset($beforeData,$afterData);
  96. $this->adminOperateLogger->clean()->save([
  97. 'optType' => '更新站点设置',
  98. ]);
  99. return true;
  100. }
  101. /**
  102. * 更新奖金配置
  103. * @return bool|null
  104. * @throws Exception
  105. */
  106. public function updateBonus(){
  107. if(!$this->validate()){
  108. return null;
  109. }
  110. $configs = Config::find()->where("TYPE='bonus'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  111. $configNames = [];
  112. foreach ($configs as $value){
  113. $configNames[] = $value['CONFIG_NAME'];
  114. }
  115. $beforeData = Config::getConfigByType('bonus');
  116. $this->adminOperateLogger->saveBeforeContent=$beforeData;
  117. $postData = \Yii::$app->request->post();
  118. $transaction = \Yii::$app->db->beginTransaction();
  119. try{
  120. foreach ($configs as $key => $value) {
  121. if (array_key_exists($key, $postData)) {
  122. if (!$postData[$key]) $postData[$key] = 0;
  123. if( in_array($key, self::BONUS_TO_STRING_FIELD) ) $postData[$key] = implode(",", $postData[$key]);
  124. if( in_array($key, self::BONUS_JSON_FIELD) ) $postData[$key] = JSON::encode($postData[$key]);
  125. Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
  126. }
  127. }
  128. $transaction->commit();
  129. } catch (Exception $e){
  130. $transaction->rollBack();
  131. $this->addError('updateBase', $e->getMessage());
  132. return null;
  133. }
  134. $afterData = Config::getConfigByType('bonus');
  135. $this->adminOperateLogger->saveAfterContent=$afterData;
  136. unset($beforeData,$afterData);
  137. $this->adminOperateLogger->clean()->save([
  138. 'optType' => '更新奖金配置',
  139. ]);
  140. return true;
  141. }
  142. /**
  143. * 更新短信配置
  144. * @return bool|null
  145. * @throws Exception
  146. */
  147. public function updateSms(){
  148. if(!$this->validate()){
  149. return null;
  150. }
  151. $configs = Config::find()->where("TYPE='sms'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  152. $configNames = [];
  153. foreach ($configs as $value){
  154. $configNames[] = $value['CONFIG_NAME'];
  155. }
  156. $beforeData = Config::getConfigByType('sms');
  157. $this->adminOperateLogger->saveBeforeContent=$beforeData;
  158. $postData = \Yii::$app->request->post();
  159. $transaction = \Yii::$app->db->beginTransaction();
  160. try{
  161. foreach ($configs as $key => $value) {
  162. if (array_key_exists($key, $postData)) {
  163. if (!$postData[$key]) $postData[$key] = 0;
  164. if(is_array($postData[$key])) $postData[$key] = implode(",", $postData[$key]);
  165. Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
  166. }
  167. }
  168. $transaction->commit();
  169. } catch (Exception $e){
  170. $transaction->rollBack();
  171. $this->addError('updateSms', $e->getMessage());
  172. return null;
  173. }
  174. $afterData = Config::getConfigByType('sms');
  175. $this->adminOperateLogger->saveAfterContent=$afterData;
  176. unset($beforeData,$afterData);
  177. $this->adminOperateLogger->clean()->save([
  178. 'optType' => '更新短信配置',
  179. ]);
  180. return true;
  181. }
  182. /**
  183. * 更新转账配置
  184. * @return bool|null
  185. * @throws Exception
  186. */
  187. public function updateTransfer(){
  188. if(!$this->validate()){
  189. return null;
  190. }
  191. $configs = Config::find()->where("TYPE='transfer'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  192. $configNames = [];
  193. foreach ($configs as $value){
  194. $configNames[] = $value['CONFIG_NAME'];
  195. }
  196. $beforeData = Config::getConfigByType('transfer');
  197. $this->adminOperateLogger->saveBeforeContent=$beforeData;
  198. $postData = \Yii::$app->request->post();
  199. $transaction = \Yii::$app->db->beginTransaction();
  200. try{
  201. foreach ($configs as $key => $value) {
  202. if (array_key_exists($key, $postData)) {
  203. if (!$postData[$key]) $postData[$key] = 0;
  204. if(is_array($postData[$key])) $postData[$key] = Json::encode($postData[$key]);
  205. Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
  206. }
  207. }
  208. $transaction->commit();
  209. } catch (Exception $e){
  210. $transaction->rollBack();
  211. $this->addError('updateTransfer', $e->getMessage());
  212. return null;
  213. }
  214. $afterData = Config::getConfigByType('transfer');
  215. $this->adminOperateLogger->saveAfterContent=$afterData;
  216. unset($beforeData,$afterData);
  217. $this->adminOperateLogger->clean()->save([
  218. 'optType' => '更新转账配置',
  219. ]);
  220. return true;
  221. }
  222. /**
  223. * 更新分数配置
  224. * @return bool|null
  225. * @throws Exception
  226. */
  227. public function updateScore(){
  228. if(!$this->validate()){
  229. return null;
  230. }
  231. $configs = Config::find()->where("TYPE='score'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  232. $configNames = [];
  233. foreach ($configs as $value){
  234. $configNames[] = $value['CONFIG_NAME'];
  235. }
  236. $beforeData = Config::getConfigByType('score');
  237. $this->adminOperateLogger->saveBeforeContent=$beforeData;
  238. $postData = \Yii::$app->request->post();
  239. $transaction = \Yii::$app->db->beginTransaction();
  240. try{
  241. foreach ($configs as $key => $value) {
  242. if (array_key_exists($key, $postData)) {
  243. if (!$postData[$key]) $postData[$key] = 0;
  244. if(is_array($postData[$key])) $postData[$key] = Json::encode($postData[$key]);
  245. Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
  246. }
  247. }
  248. $transaction->commit();
  249. } catch (Exception $e){
  250. $transaction->rollBack();
  251. $this->addError('updateScore', $e->getMessage());
  252. return null;
  253. }
  254. $afterData = Config::getConfigByType('score');
  255. $this->adminOperateLogger->saveAfterContent=$afterData;
  256. unset($beforeData,$afterData);
  257. $this->adminOperateLogger->clean()->save([
  258. 'optType' => '更新分数配置',
  259. ]);
  260. return true;
  261. }
  262. /**
  263. * 更新其他配置
  264. * @return bool|null
  265. * @throws Exception
  266. */
  267. public function updateOther(){
  268. if(!$this->validate()){
  269. return null;
  270. }
  271. $configs = Config::find()->where("TYPE='other'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  272. $configNames = [];
  273. foreach ($configs as $value){
  274. $configNames[] = $value['CONFIG_NAME'];
  275. }
  276. $beforeData = Config::getConfigByType('other');
  277. $this->adminOperateLogger->saveBeforeContent=$beforeData;
  278. $postData = \Yii::$app->request->post();
  279. $transaction = \Yii::$app->db->beginTransaction();
  280. try{
  281. foreach ($configs as $key => $value) {
  282. if (array_key_exists($key, $postData)) {
  283. if (!$postData[$key]) $postData[$key] = 0;
  284. if(is_array($postData[$key])) $postData[$key] = implode(",", $postData[$key]);
  285. Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
  286. }
  287. }
  288. $transaction->commit();
  289. } catch (Exception $e){
  290. $transaction->rollBack();
  291. $this->addError('updateOther', $e->getMessage());
  292. return null;
  293. }
  294. $afterData = Config::getConfigByType('other');
  295. $this->adminOperateLogger->saveAfterContent=$afterData;
  296. unset($beforeData,$afterData);
  297. $this->adminOperateLogger->clean()->save([
  298. 'optType' => '更新其他配置',
  299. ]);
  300. return true;
  301. }
  302. }