ConfigController.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace backendApi\modules\v1\controllers;
  9. use backendApi\modules\v1\models\Admin;
  10. use backendApi\modules\v1\models\AdminCountry;
  11. use backendApi\modules\v1\models\AdminRole;
  12. use common\helpers\Cache;
  13. use common\helpers\Date;
  14. use common\helpers\Form;
  15. use common\models\Countries;
  16. use common\models\Currency;
  17. use common\models\CurrencyConversions;
  18. use common\models\DecRole;
  19. use common\models\forms\ConfigForm;
  20. use common\models\forms\ConfigPeriodForm;
  21. use common\models\forms\CurrenciesConversionsForm;
  22. use common\models\forms\DecLevelForm;
  23. use common\models\forms\DecRoleForm;
  24. use common\models\forms\FreeTemplateForm;
  25. use common\models\forms\RegTypeForm;
  26. use common\models\forms\DeclarationLevelForm;
  27. use common\models\forms\EmployLevelForm;
  28. use common\models\forms\OcrApiForm;
  29. use common\models\forms\SmsApiForm;
  30. use common\models\forms\SmsTemplateForm;
  31. use common\models\forms\WithdrawLevelForm;
  32. use common\models\FreeTemplate;
  33. use common\models\OcrApi;
  34. use common\models\RegType;
  35. use common\models\DeclarationLevel;
  36. use common\models\EmployLevel;
  37. use common\models\SmsApi;
  38. use common\models\SmsTemplate;
  39. use common\models\WithdrawLevel;
  40. use Yii;
  41. use common\models\Config;
  42. use yii\base\Exception;
  43. use yii\helpers\Json;
  44. use yii\web\HttpException;
  45. class ConfigController extends BaseController {
  46. public $modelClass = Config::class;
  47. public $currencyModelClass = Currency::class;
  48. public $currencyConversionsModelClass = CurrencyConversions::class;
  49. public $freeTemplateModelClass = FreeTemplate::class;
  50. public $countiesModelClass = Countries::class;
  51. public function behaviors() {
  52. return parent::behaviors();
  53. }
  54. /**
  55. * 站点设置
  56. * @return mixed
  57. * @throws \yii\db\Exception
  58. * @throws \yii\web\HttpException
  59. */
  60. public function actionBase() {
  61. // 获取priod的相关配置参数
  62. $configs = Config::find()->where("TYPE='base'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  63. $form = new ConfigForm();
  64. if (Yii::$app->request->post() && $form->load(Yii::$app->request->post(), '')) {
  65. if ($form->updateBase()) {
  66. Config::updateToCache();
  67. return static::notice(Yii::t('ctx', 'successfully'));
  68. } else {
  69. return static::notice(Form::formatErrorsForApi($form->getErrors()), 422);
  70. }
  71. }
  72. foreach ($configs as $key => $config) {
  73. if ($config['OPTIONS']) {
  74. $configs[$key]['OPTIONS'] = Json::decode($config['OPTIONS']);
  75. }
  76. if ($config['INPUT_TYPE'] == Config::INPUT_TYPE_SWITCH) {
  77. $configs[$key]['VALUE'] = boolval($config['VALUE']);
  78. }
  79. $configs[$key]['TITLE'] = Yii::t('ctx', $config['LANGUAGE_KEY']);
  80. }
  81. return static::notice($configs);
  82. }
  83. /**
  84. * 奖金相关配置
  85. * @return mixed
  86. * @throws \yii\db\Exception
  87. * @throws \yii\web\HttpException
  88. */
  89. public function actionBonusOpt() {
  90. $configs = Config::find()->where("TYPE='bonus'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  91. foreach ($configs as $key => $config) {
  92. if ($config['OPTIONS']) {
  93. $configs[$key]['OPTIONS'] = Json::decode($config['OPTIONS']);
  94. }
  95. if ($config['INPUT_TYPE'] == Config::INPUT_TYPE_SWITCH) {
  96. $configs[$key]['VALUE'] = boolval($config['VALUE']);
  97. }
  98. if ($config['INPUT_TYPE'] == Config::INPUT_TYPE_SELECT_MULTIPLE) {
  99. $configs[$key]['VALUE'] = explode(',', $config['VALUE']);
  100. }
  101. if($config['INPUT_TYPE'] == Config::INPUT_TYPE_TABLE) {
  102. $configs[$key]['VALUE'] = Json::decode($config['VALUE']);
  103. }
  104. $configs[$key]['TITLE'] = $config['LANGUAGE_KEY'] ? Yii::t('ctx', $config['LANGUAGE_KEY']) : $config['TITLE'];
  105. }
  106. return static::notice(['config' => $configs]);
  107. }
  108. /**
  109. * 基本奖金设置
  110. * @return mixed
  111. * @throws \yii\db\Exception
  112. * @throws \yii\web\HttpException
  113. */
  114. public function actionBonus() {
  115. $form = new ConfigForm();
  116. if (Yii::$app->request->post() && $form->load(Yii::$app->request->post(), '')) {
  117. if ($form->updateBonus()) {
  118. Config::updateToCache();
  119. return static::notice(Yii::t('ctx', 'successfully'));
  120. } else {
  121. return static::notice(Form::formatErrorsForApi($form->getErrors()), 422);
  122. }
  123. }
  124. }
  125. /**
  126. * 修改会员级别相关的奖金配置
  127. * @return mixed
  128. * @throws \yii\db\Exception
  129. * @throws \yii\web\HttpException
  130. */
  131. public function actionBonusDecLevel() {
  132. $form = new DeclarationLevelForm();
  133. if (Yii::$app->request->post() && $form->load(Yii::$app->request->post(), '')) {
  134. if ($form->updateBonus()) {
  135. DeclarationLevel::updateToCache();
  136. return static::notice(Yii::t('ctx', 'successfully'));
  137. } else {
  138. return static::notice(Form::formatErrorsForApi($form->getErrors()), 422);
  139. }
  140. }
  141. }
  142. /**
  143. * 修改奖金聘级相关的奖金配置
  144. * @return mixed
  145. * @throws \yii\db\Exception
  146. * @throws \yii\web\HttpException
  147. */
  148. public function actionBonusEmpLevel() {
  149. $form = new EmployLevelForm();
  150. if (Yii::$app->request->post() && $form->load(Yii::$app->request->post(), '')) {
  151. if ($form->updateBonus()) {
  152. Cache::updateEmpLevelConfig();
  153. return static::notice(Yii::t('ctx', 'successfully'));
  154. } else {
  155. return static::notice(Form::formatErrorsForApi($form->getErrors()), 422);
  156. }
  157. }
  158. }
  159. /**
  160. * 更新封期参数
  161. * @return mixed
  162. * @throws \yii\db\Exception
  163. * @throws \yii\web\HttpException
  164. */
  165. public function actionPeriod() {
  166. $form = new ConfigPeriodForm();
  167. if (Yii::$app->request->post() && $form->load(Yii::$app->request->post(), '')) {
  168. if ($form->update()) {
  169. Config::updateToCache();
  170. // Log::adminHandle('更新封期参数', 1);
  171. return static::notice(Yii::t('ctx', 'successfully'));
  172. } else {
  173. return static::notice(Form::formatErrorsForApi($form->getErrors()), 422);
  174. }
  175. }
  176. // 获取priod的相关配置参数
  177. $configs = Config::find()->where("TYPE='period'")->indexBy('CONFIG_NAME')->orderBy('SORT ASC')->asArray()->all();
  178. foreach ($configs as $key => $config) {
  179. if ($config['OPTIONS']) {
  180. $configs[$key]['OPTIONS'] = Json::decode($config['OPTIONS']);
  181. }
  182. if ($config['INPUT_TYPE'] == Config::INPUT_TYPE_SWITCH) {
  183. $configs[$key]['VALUE'] = boolval($config['VALUE']);
  184. }
  185. $configs[$key]['TITLE'] = Yii::t('ctx', $config['LANGUAGE_KEY']);
  186. }
  187. return static::notice($configs);
  188. }
  189. /**
  190. * 接口设置
  191. * @return mixed
  192. * @throws \yii\web\HttpException
  193. */
  194. public function actionApiOpt(){
  195. return static::notice('1');
  196. }
  197. /**
  198. * 查看OcrApi接口
  199. * @return mixed
  200. * @throws \yii\web\HttpException
  201. */
  202. public function actionOcrApi() {
  203. $condition = '';
  204. $params = [];
  205. $data = OcrApi::lists($condition, $params, [
  206. 'select' => 'OA.*,ADMC.ADMIN_NAME CREATE_ADMIN_NAME,ADMU.ADMIN_NAME UPDATE_ADMIN_NAME',
  207. 'from' => OcrApi::tableName() . ' AS OA',
  208. 'join' => [
  209. ['LEFT JOIN', Admin::tableName() . ' AS ADMC', 'ADMC.ID=OA.CREATE_ADMIN'],
  210. ['LEFT JOIN', Admin::tableName() . ' AS ADMU', 'ADMU.ID=OA.UPDATE_ADMIN'],
  211. ],
  212. 'orderBy' => 'OA.CREATED_AT ASC',
  213. ]);
  214. return static::notice($data);
  215. }
  216. /**
  217. * 修改OcrApi接口
  218. * @return mixed
  219. * @throws \yii\web\HttpException
  220. */
  221. public function actionOcrApiEdit() {
  222. $id = Yii::$app->request->get('id');
  223. if (Yii::$app->request->isPost) {
  224. return static::edit(OcrApiForm::class, Yii::t('ctx', 'successfully'), null, null, null, function () {
  225. OcrApi::updateToCache();
  226. // Log::adminHandle('编辑OCRAPI参数配置', 1);
  227. });
  228. }
  229. $oneData = OcrApi::find()->where('ID=:ID', [':ID' => $id])->asArray()->one();
  230. if ($oneData['CONFIG']) {
  231. $oneData['CONFIG'] = Json::decode($oneData['CONFIG']);
  232. }
  233. // 获取可供编辑的字段
  234. $oneData['apiType'] = '百度OCR';
  235. $oneData['CONFIGS'] = \common\helpers\ocr\OcrApi::apiConfigs($oneData['API_NAME']);
  236. return static::notice($oneData);
  237. }
  238. /**
  239. * 短信接口查看
  240. * @return mixed
  241. * @throws \yii\web\HttpException
  242. */
  243. public function actionSmsApi() {
  244. $condition = '';
  245. $params = [];
  246. $data = SmsApi::lists($condition, $params, [
  247. 'select' => 'OA.*,ADMC.ADMIN_NAME CREATE_ADMIN_NAME,ADMU.ADMIN_NAME UPDATE_ADMIN_NAME',
  248. 'from' => SmsApi::tableName() . ' AS OA',
  249. 'join' => [
  250. ['LEFT JOIN', Admin::tableName() . ' AS ADMC', 'ADMC.ID=OA.CREATE_ADMIN'],
  251. ['LEFT JOIN', Admin::tableName() . ' AS ADMU', 'ADMU.ID=OA.UPDATE_ADMIN'],
  252. ],
  253. 'orderBy' => 'OA.CREATED_AT ASC',
  254. ]);
  255. return static::notice($data);
  256. }
  257. /**
  258. * 短信接口编辑
  259. * @return mixed
  260. * @throws \yii\web\HttpException
  261. */
  262. public function actionSmsApiEdit() {
  263. $id = Yii::$app->request->get('id');
  264. if (Yii::$app->request->isPost) {
  265. return static::edit(SmsApiForm::class, Yii::t('ctx', 'successfully'), null, null, null, function () {
  266. SmsApi::updateToCache();
  267. // Log::adminHandle('编辑SMSAPI参数配置', 1);
  268. });
  269. }
  270. $oneData = SmsApi::find()->where('ID=:ID', [':ID' => $id])->asArray()->one();
  271. if ($oneData['CONFIG']) {
  272. $oneData['CONFIG'] = Json::decode($oneData['CONFIG']);
  273. }
  274. $oneData['apiType'] = '短信接口';
  275. // 获取可供编辑的字段
  276. $oneData['CONFIGS'] = \common\libs\api\sms\SmsApi::apiConfigs($oneData['API_NAME']);
  277. return static::notice($oneData);
  278. }
  279. /**
  280. * 短信配置
  281. * @return mixed
  282. * @throws \yii\db\Exception
  283. * @throws \yii\web\HttpException
  284. */
  285. public function actionSms() {
  286. // 获取priod的相关配置参数
  287. $configs = Config::find()->where("TYPE='sms'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  288. $form = new ConfigForm();
  289. if (Yii::$app->request->post() && $form->load(Yii::$app->request->post(), '')) {
  290. if ($form->updateSms()) {
  291. Config::updateToCache();
  292. return static::notice(Yii::t('ctx', 'successfully'));
  293. } else {
  294. return static::notice(Form::formatErrorsForApi($form->getErrors()), 422);
  295. }
  296. }
  297. foreach ($configs as $key => $config) {
  298. if ($config['OPTIONS']) {
  299. $configs[$key]['OPTIONS'] = Json::decode($config['OPTIONS']);
  300. }
  301. if ($config['INPUT_TYPE'] == Config::INPUT_TYPE_SWITCH) {
  302. $configs[$key]['VALUE'] = boolval($config['VALUE']);
  303. }
  304. if ($config['INPUT_TYPE'] == Config::INPUT_TYPE_SELECT) {
  305. $configs[$key]['VALUE'] = explode(",",$config['VALUE']);
  306. }
  307. }
  308. return static::notice($configs);
  309. }
  310. /**
  311. * 转账配置
  312. * @return mixed
  313. * @throws \yii\db\Exception
  314. * @throws \yii\web\HttpException
  315. */
  316. public function actionTransfer() {
  317. // 获取priod的相关配置参数
  318. $configs = Config::find()->where("TYPE='transfer'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  319. $form = new ConfigForm();
  320. if (Yii::$app->request->post() && $form->load(Yii::$app->request->post(), '')) {
  321. if ($form->updateTransfer()) {
  322. Config::updateToCache();
  323. return static::notice(Yii::t('ctx', 'successfully'));
  324. } else {
  325. return static::notice(Form::formatErrorsForApi($form->getErrors()), 422);
  326. }
  327. }
  328. foreach ($configs as $key => $config) {
  329. if ($config['OPTIONS']) {
  330. $configs[$key]['OPTIONS'] = Json::decode($config['OPTIONS']);
  331. }
  332. if ($config['INPUT_TYPE'] == Config::INPUT_TYPE_SWITCH) {
  333. $configs[$key]['VALUE'] = boolval($config['VALUE']);
  334. }
  335. if ($config['INPUT_TYPE'] == Config::INPUT_TYPE_TABLE) {
  336. $configs[$key]['VALUE'] = Json::decode($config['VALUE']);
  337. }
  338. $configs[$key]['TITLE'] = Yii::t('ctx', $config['LANGUAGE_KEY']);
  339. }
  340. return static::notice($configs);
  341. }
  342. /**
  343. * 分数配置
  344. * @return mixed
  345. * @throws \yii\db\Exception
  346. * @throws \yii\web\HttpException
  347. */
  348. public function actionScore() {
  349. // 获取priod的相关配置参数
  350. $configs = Config::find()->where("TYPE='score'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  351. $form = new ConfigForm();
  352. if (Yii::$app->request->post() && $form->load(Yii::$app->request->post(), '')) {
  353. if ($form->updateScore()) {
  354. Config::updateToCache();
  355. return static::notice(Yii::t('ctx', 'successfully'));
  356. } else {
  357. return static::notice(Form::formatErrorsForApi($form->getErrors()), 422);
  358. }
  359. }
  360. foreach ($configs as $key => $config) {
  361. if ($config['OPTIONS']) {
  362. $configs[$key]['OPTIONS'] = Json::decode($config['OPTIONS']);
  363. }
  364. if ($config['INPUT_TYPE'] == Config::INPUT_TYPE_SWITCH) {
  365. $configs[$key]['VALUE'] = boolval($config['VALUE']);
  366. }
  367. if ($config['INPUT_TYPE'] == Config::INPUT_TYPE_TABLE) {
  368. $configs[$key]['VALUE'] = Json::decode($config['VALUE']);
  369. }
  370. $configs[$key]['TITLE'] = Yii::t('ctx', $config['LANGUAGE_KEY']);
  371. }
  372. return static::notice($configs);
  373. }
  374. /**
  375. * 其他配置
  376. * @return mixed
  377. * @throws \yii\db\Exception
  378. * @throws \yii\web\HttpException
  379. */
  380. public function actionOther() {
  381. // 获取priod的相关配置参数
  382. $configs = Config::find()->where("TYPE='other'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  383. $form = new ConfigForm();
  384. if (Yii::$app->request->post() && $form->load(Yii::$app->request->post(), '')) {
  385. if ($form->updateOther()) {
  386. Config::updateToCache();
  387. return static::notice(Yii::t('ctx', 'successfully'));
  388. } else {
  389. return static::notice(Form::formatErrorsForApi($form->getErrors()), 422);
  390. }
  391. }
  392. if (Yii::$app->request->post()) {
  393. $postData = Yii::$app->request->post();
  394. $transaction = Yii::$app->db->beginTransaction();
  395. try {
  396. foreach ($configs as $key => $value) {
  397. if (array_key_exists($key, $postData)) {
  398. if (!$postData[$key]) $postData[$key] = 0;
  399. Config::updateAll(['VALUE' => $postData[$key], 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => $key]);
  400. }
  401. }
  402. Config::updateToCache();
  403. $transaction->commit();
  404. } catch (Exception $e) {
  405. $transaction->rollBack();
  406. return static::notice(Yii::t('ctx', 'failed'));
  407. }
  408. // Log::adminHandle('更新其他配置参数', 1);
  409. return static::notice(Yii::t('ctx', 'successfully'));
  410. }
  411. foreach ($configs as $key => $config) {
  412. if ($config['OPTIONS']) {
  413. $configs[$key]['OPTIONS'] = Json::decode($config['OPTIONS']);
  414. }
  415. if ($config['INPUT_TYPE'] == Config::INPUT_TYPE_SWITCH) {
  416. $configs[$key]['VALUE'] = boolval($config['VALUE']);
  417. }
  418. $configs[$key]['TITLE'] = Yii::t('ctx', $config['LANGUAGE_KEY']);
  419. }
  420. return static::notice($configs);
  421. }
  422. /**
  423. * 查看报单级别
  424. * @return mixed
  425. * @throws \yii\web\HttpException
  426. */
  427. public function actionDecLevel() {
  428. $data = Cache::getDecLevelConfig();
  429. return static::notice(['list' => $data]);
  430. }
  431. public function actionMonthLimit() {
  432. if (Yii::$app->request->isPost) {
  433. $postData = Yii::$app->request->post();
  434. $month = $postData['month'];
  435. $ret = Config::updateAll(['VALUE' => $month, 'UPDATED_AT' => Date::nowTime()], "CONFIG_NAME=:CONFIG_NAME", [':CONFIG_NAME' => 'observePeriodLimit']);
  436. if ($ret) {
  437. return static::notice(Yii::t('ctx', 'successfully'));
  438. } else {
  439. return static::notice(Yii::t('ctx', 'failed'));
  440. }
  441. }
  442. $data = Config::find()
  443. ->where("CONFIG_NAME='observePeriodLimit'")
  444. ->asArray()
  445. ->one();
  446. $isSwitchUpgrade = Config::find()
  447. ->where("CONFIG_NAME='isOpenUpgrade'")
  448. ->asArray()
  449. ->one();
  450. return static::notice([
  451. 'observe' => $data,
  452. 'isOpenUpgrade' => $isSwitchUpgrade
  453. ]);
  454. }
  455. // 开关会员升级单功能
  456. public function actionOpenUpgrade() {
  457. if (Yii::$app->request->isPost) {
  458. $postData = Yii::$app->request->post();
  459. $isOpen = $postData['isOpen'];
  460. $ret = Config::updateAll(
  461. ['VALUE' => $isOpen, 'UPDATED_AT' => Date::nowTime()],
  462. "CONFIG_NAME=:CONFIG_NAME",
  463. [':CONFIG_NAME' => 'isOpenUpgrade']
  464. );
  465. if ($ret) {
  466. $msg = $isOpen == 1 ? Yii::t('ctx', 'on') : Yii::t('ctx', 'off');
  467. return static::notice(Yii::t('ctx', 'successfully'));
  468. } else {
  469. return static::notice(Yii::t('ctx', 'failed'));
  470. }
  471. }
  472. return static::notice(Yii::t('ctx', 'illegalRequest'), 405);
  473. }
  474. /**
  475. * 查看报单中心级别
  476. * @return mixed
  477. * @throws \yii\web\HttpException
  478. */
  479. public function actionDecRole() {
  480. $data = Cache::getDecRoleConfig();
  481. return static::notice(['list' => $data]);
  482. }
  483. /**
  484. * 修改报单中心级别
  485. * @return mixed
  486. * @throws \yii\web\HttpException
  487. */
  488. public function actionDecRoleEdit() {
  489. $id = Yii::$app->request->get('id');
  490. if (Yii::$app->request->isPost) {
  491. return static::edit(DecRoleForm::class, Yii::t('ctx', 'successfully'), null, null, null, function () {
  492. DecRole::updateToCache();
  493. });
  494. }
  495. $oneData = DecRole::find()->where('ID=:ID', [':ID' => $id])->asArray()->one();
  496. return static::notice($oneData);
  497. }
  498. /**
  499. * 添加会员级别
  500. * @return mixed
  501. * @throws \yii\web\HttpException
  502. */
  503. public function actionDecLevelAdd() {
  504. if (Yii::$app->request->isPost) {
  505. return static::edit(DecLevelForm::class, Yii::t('ctx', 'successfully'), null, null, null, function () {
  506. DeclarationLevel::updateToCache();
  507. });
  508. }
  509. return static::notice(Yii::t('ctx', 'illegalRequest'), 405);
  510. }
  511. /**
  512. * 修改会员级别
  513. * @return mixed
  514. * @throws \yii\web\HttpException
  515. */
  516. public function actionDecLevelEdit() {
  517. $id = Yii::$app->request->get('id');
  518. if (Yii::$app->request->isPost) {
  519. return static::edit(DecLevelForm::class, Yii::t('ctx', 'successfully'), null, null, null, function () {
  520. DeclarationLevel::updateToCache();
  521. });
  522. }
  523. $oneData = DeclarationLevel::find()->where('ID=:ID', [':ID' => $id])->asArray()->one();
  524. return static::notice($oneData);
  525. }
  526. /**
  527. * 删除会员级别
  528. * @return mixed
  529. * @throws \yii\db\Exception
  530. * @throws \yii\web\HttpException
  531. */
  532. public function actionDecLevelDelete() {
  533. return static::delete(DeclarationLevel::class, '', function ($selected) {
  534. DeclarationLevel::updateToCache();
  535. });
  536. }
  537. /**
  538. * 查看会员聘级
  539. * @return mixed
  540. * @throws \yii\web\HttpException
  541. */
  542. public function actionEmpLevel() {
  543. $data = EmployLevel::lists('', [], [
  544. 'select' => 'E.*, ME.LEVEL_NAME AS MIN_EMPLOY_LEVEL_NAME',
  545. 'from' => EmployLevel::tableName() . ' AS E',
  546. 'join' => [
  547. ['LEFT JOIN', EmployLevel::tableName() . ' AS ME', 'ME.ID=E.MIN_EMPLOY_LEVEL']
  548. ],
  549. 'orderBy' => 'E.SORT ASC, E.CREATED_AT ASC'
  550. ]);
  551. return static::notice($data);
  552. }
  553. /**
  554. * 添加会员聘级
  555. * @return mixed
  556. * @throws \yii\web\HttpException
  557. */
  558. public function actionEmpLevelAdd() {
  559. if (Yii::$app->request->isPost) {
  560. return static::edit(EmployLevelForm::class, Yii::t('ctx', 'successfully'), null, null, null, function () {
  561. EmployLevel::updateToCache();
  562. });
  563. }
  564. // 获取所有聘级,以供选择
  565. $allLevel = EmployLevel::find()->where('1=1')->orderBy('SORT ASC')->asArray()->all();
  566. return static::notice($allLevel);
  567. }
  568. /**
  569. * 修改聘级
  570. * @return mixed
  571. * @throws \yii\web\HttpException
  572. */
  573. public function actionEmpLevelEdit() {
  574. $id = Yii::$app->request->get('id');
  575. if (Yii::$app->request->isPost) {
  576. return static::edit(EmployLevelForm::class, Yii::t('ctx', 'successfully'), null, null, null, function () {
  577. EmployLevel::updateToCache();
  578. // Log::adminHandle('编辑会员聘级', 1);
  579. });
  580. }
  581. $oneData = EmployLevel::find()->where('ID=:ID', [':ID' => $id])->asArray()->one();
  582. // 获取所有聘级,以供选择
  583. $allLevel = EmployLevel::find()->where('1=1')->orderBy('SORT ASC')->asArray()->all();
  584. $oneData['allLevel'] = $allLevel;
  585. return static::notice($oneData);
  586. }
  587. /**
  588. * 删除聘级
  589. * @return mixed
  590. * @throws \yii\db\Exception
  591. * @throws \yii\web\HttpException
  592. */
  593. public function actionEmpLevelDelete() {
  594. return static::delete(EmployLevel::class, '', function ($selected) {
  595. EmployLevel::updateToCache();
  596. // Log::adminHandle('删除会员聘级', 1);
  597. });
  598. }
  599. /**
  600. * 查看短信模板
  601. * @return mixed
  602. * @throws \yii\web\HttpException
  603. */
  604. public function actionSmsTemplate() {
  605. $data = SmsTemplate::lists('', [], ['orderBy' => 'CREATED_AT ASC']);
  606. return static::notice($data);
  607. }
  608. /**
  609. * 编辑短信模板
  610. * @return mixed
  611. * @throws \yii\web\HttpException
  612. */
  613. public function actionSmsTemplateEdit() {
  614. $id = Yii::$app->request->get('id');
  615. if (Yii::$app->request->isPost) {
  616. return parent::edit(SmsTemplateForm::class, Yii::t('ctx', 'successfully'), null, null, null, function () {
  617. SmsTemplate::updateDecToCache();
  618. SmsTemplate::updateEmpToCache();
  619. // Log::adminHandle('编辑短信模板', 1);
  620. });
  621. }
  622. $oneData = SmsTemplate::find()->where('ID=:ID', [':ID' => $id])->asArray()->one();
  623. return static::notice(['oneData' => $oneData]);
  624. }
  625. /**
  626. * 提现金额等级
  627. * @return mixed
  628. * @throws \yii\web\HttpException
  629. */
  630. public function actionWithdrawLevel() {
  631. $data = WithdrawLevel::lists('', [], ['orderBy' => 'MIN_AMOUNT ASC']);
  632. return static::notice($data);
  633. }
  634. /**
  635. * 添加提现金额等级
  636. * @return mixed
  637. * @throws \yii\web\HttpException
  638. */
  639. public function actionWithdrawLevelAdd() {
  640. if (Yii::$app->request->isPost) {
  641. return static::edit(WithdrawLevelForm::class, Yii::t('ctx', 'successfully'), null, null, null, function () {
  642. WithdrawLevel::updateToCache();
  643. // Log::adminHandle('添加提现金额等级', 1);
  644. });
  645. }
  646. }
  647. /**
  648. * 修改提现金额等级
  649. * @return mixed
  650. * @throws \yii\web\HttpException
  651. */
  652. public function actionWithdrawLevelEdit() {
  653. $id = Yii::$app->request->get('id');
  654. if (Yii::$app->request->isPost) {
  655. return static::edit(WithdrawLevelForm::class, Yii::t('ctx', 'successfully'), null, null, null, function () {
  656. WithdrawLevel::updateToCache();
  657. // Log::adminHandle('编辑提现金额等级', 1);
  658. });
  659. }
  660. $oneData = WithdrawLevel::find()->where('ID=:ID', [':ID' => $id])->asArray()->one();
  661. return static::notice($oneData);
  662. }
  663. /**
  664. * 删除提现金额等级
  665. * @return mixed
  666. * @throws \yii\db\Exception
  667. * @throws \yii\web\HttpException
  668. */
  669. public function actionWithdrawLevelDelete() {
  670. return static::delete(WithdrawLevel::class, '', function ($selected) {
  671. WithdrawLevel::updateToCache();
  672. });
  673. }
  674. /**
  675. * 注册类型
  676. * @return mixed
  677. * @throws \yii\web\HttpException
  678. */
  679. public function actionRegType() {
  680. $data = RegType::lists('', [], [
  681. 'orderBy' => 'SORT ASC, CREATED_AT ASC'
  682. ]);
  683. return static::notice($data);
  684. }
  685. /**
  686. * 获取注册类型
  687. * @return mixed
  688. * @throws \yii\web\HttpException
  689. */
  690. public function actionRegTypeGet() {
  691. $id = Yii::$app->request->get('id');
  692. $regType = RegType::findOneAsArray('ID=:ID', [':ID' => $id]);
  693. if (!$regType) {
  694. return static::notice(Yii::t('ctx', 'dataNotExists'), 400);
  695. }
  696. return static::notice(['id' => $regType['ID'], 'typeName' => $regType['TYPE_NAME'], 'isPact' => $regType['IS_PACT'], 'monthAmount' => $regType['MONTH_LIMIT_AMOUNT'], 'yearAmount' => $regType['YEAR_LIMIT_AMOUNT'], 'remark' => $regType['REMARK']]);
  697. }
  698. /**
  699. * 修改注册类型
  700. * @return mixed
  701. * @throws \yii\web\HttpException
  702. */
  703. public function actionRegTypeEdit() {
  704. $id = Yii::$app->request->get('id');
  705. if (Yii::$app->request->isPost) {
  706. $formModel = new RegTypeForm();
  707. $formModel->scenario = 'edit';
  708. $formModel->id = $id;
  709. if ($formModel->load(Yii::$app->request->post(), '') && $result = $formModel->edit()) {
  710. // Log::adminHandle('修改注册类型,ID为:' . $result['ID']);
  711. return static::notice(Yii::t('ctx', 'successfully'));
  712. } else {
  713. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  714. }
  715. }
  716. return static::notice(Yii::t('ctx', 'illegalRequest'), 405);
  717. }
  718. /**
  719. * 获取服务协议
  720. * @return mixed
  721. * @throws \yii\web\HttpException
  722. */
  723. public function actionPactGet() {
  724. $path = \Yii::getAlias('@common/runtime/datas/pact.php');
  725. if (!file_exists($path)) {
  726. $oneData = '';
  727. } else {
  728. $oneData = include $path;
  729. }
  730. return static::notice($oneData);
  731. }
  732. /**
  733. * 获取服务协议
  734. * @return mixed
  735. * @throws \yii\base\InvalidConfigException
  736. * @throws \yii\web\HttpException
  737. */
  738. public function actionPactEdit() {
  739. $path = \Yii::getAlias('@common/runtime/datas/pact.php');
  740. $post = Yii::$app->request->post();
  741. if (!isset($post['CONTENT']) || empty($post['CONTENT'])) {
  742. return static::notice('请输入协议内容', 400);
  743. }
  744. $now = Date::nowTime();
  745. $data = [
  746. 'UPDATED_AT' => $now,
  747. 'CONTENT' => addslashes($post['CONTENT']),
  748. 'ADM_NAME' => \Yii::$app->user->id,
  749. ];
  750. // Log::adminHandle('更新服务协议');
  751. $date = \Yii::$app->formatter->asDatetime($now);
  752. $content = "<?php" . PHP_EOL . "/**
  753. * 配置文件
  754. * @date {$date}
  755. */" . PHP_EOL;
  756. $content .= "return ";
  757. $content .= var_export($data, true);
  758. $content .= ";";
  759. file_put_contents($path, $content, LOCK_EX);
  760. return static::notice(Yii::t('ctx', 'successfully'));
  761. }
  762. public function actionCurrencies()
  763. {
  764. $data = $this->currencyModelClass::getFromCache();
  765. return static::notice(['data' => $data]);
  766. }
  767. public function actionCurrenciesConversions()
  768. {
  769. // 国家
  770. $countries = Cache::getCountries();
  771. // 货币
  772. $currencies = $this->currencyModelClass::getFromCache();
  773. // 汇率配置
  774. $currencyConversion = $this->currencyConversionsModelClass::getFromCache();
  775. $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
  776. if (!$isSuper) {
  777. $adminId = Yii::$app->getUser()->getUserInfo()['id'];
  778. $adminCountry = AdminCountry::getCountry($adminId);
  779. $countries = array_filter($countries, fn($country) => in_array($country['ID'], $adminCountry));
  780. $countries = array_values($countries);
  781. $countriesId = array_column($countries, 'ID');
  782. $currencies = array_filter($currencies, fn($currency) => in_array($currency['ID'], $countriesId));
  783. $currencies = array_values($currencies);
  784. $currenciesId = array_column($currencies, 'ID');
  785. $currencyConversion = array_filter($currencyConversion, fn($conversion) => in_array($conversion['TO_CURRENCY_ID'], $currenciesId));
  786. $currencyConversion = array_values($currencyConversion);
  787. }
  788. $currencyConversion = array_column($currencyConversion, NULL, 'TO_CURRENCY_ID');
  789. foreach ($currencies as &$currency) {
  790. $currency['PRODUCT_RATE'] = $currencyConversion[$currency['ID']]['PRODUCT_RATE'] ?? 0;
  791. $currency['BONUSES_RATE'] = $currencyConversion[$currency['ID']]['BONUSES_RATE'] ?? 0;
  792. }
  793. return static::notice(['data' => $currencies]);
  794. }
  795. /**
  796. * @throws \yii\db\Exception
  797. * @throws HttpException
  798. */
  799. public function actionSetCurrenciesConversions()
  800. {
  801. if (\Yii::$app->request->isPost) {
  802. $formModel = new CurrenciesConversionsForm();
  803. $formModel->scenario = 'setCurrenciesConversions';
  804. if ($formModel->load(\Yii::$app->request->post(), '') && $formModel->setCurrenciesConversions()) {
  805. // 更新缓存
  806. $this->currencyConversionsModelClass::updateToCache();
  807. return static::notice(\Yii::t('ctx', 'successfully'));
  808. } else {
  809. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  810. }
  811. }
  812. return static::notice(\Yii::t('ctx', 'illegalRequest'));
  813. }
  814. public function actionTransportation()
  815. {
  816. // 国家列表
  817. $countries = $this->countiesModelClass::getFromCache();
  818. // 运费列表
  819. $transportation = $this->freeTemplateModelClass::getFromCache();
  820. $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
  821. if (!$isSuper) {
  822. $adminId = Yii::$app->getUser()->getUserInfo()['id'];
  823. $adminCountry = AdminCountry::getCountry($adminId);
  824. $countries = array_filter($countries, fn($country) => in_array($country['ID'], $adminCountry));
  825. $countries = array_values($countries);
  826. $countriesId = array_column($countries, 'ID');
  827. $transportation = array_filter($transportation, fn($item) => in_array($item['country_id'], $countriesId));
  828. $transportation = array_values($transportation);
  829. }
  830. $transportation = array_column($transportation, NULL, 'country_id');
  831. foreach ($countries as &$country) {
  832. $country['freight'] = $transportation[$country['ID']]['freight'] ?? 0;
  833. $country['free_shipping'] = $transportation[$country['ID']]['free_shipping'] ?? 0;
  834. $country['currency'] = $this->currencyModelClass::getById($country['LOCAL_CURRENCY_ID']);
  835. }
  836. return static::notice(['data' => $countries]);
  837. }
  838. public function actionSetTransportation()
  839. {
  840. if (\Yii::$app->request->isPost) {
  841. $formModel = new FreeTemplateForm();
  842. $formModel->scenario = 'setTransportation';
  843. if ($formModel->load(\Yii::$app->request->post(), '') && $formModel->setTransportation()) {
  844. // 更新缓存
  845. $this->modelClass::updateToCache();
  846. return static::notice(\Yii::t('ctx', 'successfully'));
  847. } else {
  848. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  849. }
  850. }
  851. return static::notice(\Yii::t('ctx', 'illegalRequest'));
  852. }
  853. }