ConfigController.php 33 KB

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