MerchantController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace addons\Store\client\controllers;
  3. use addons\Store\common\services\MerchantReduceProfitService;
  4. use addons\Store\common\services\MerchantService;
  5. use common\enums\ApiStatusEnum;
  6. use yii\helpers\Json;
  7. /**
  8. * 商户
  9. * @package addons\Store\client\controllers
  10. */
  11. class MerchantController extends BaseController
  12. {
  13. /**
  14. * 商户号设置
  15. *
  16. * @return void
  17. */
  18. public function actionSetting()
  19. {
  20. if ($this->request->isAjax) {
  21. $service = new MerchantService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]);
  22. return $this->success('ok', [
  23. 'setting' => $service->getSetting(),
  24. 'link' => $service->getSignLink(),
  25. ], ApiStatusEnum::CODE_SUCCESS, 0);
  26. }
  27. return $this->render('setting');
  28. }
  29. /**
  30. * 保存设置
  31. *
  32. * @return void
  33. */
  34. public function actionSaveSetting()
  35. {
  36. if ($this->request->isAjax) {
  37. $setting = $this->request->post();
  38. unset($setting['_csrf']);
  39. $service = new MerchantService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]);
  40. return $service->setSetting($setting)
  41. ? $this->success('ok')
  42. : $this->error('保存失败');
  43. }
  44. }
  45. /**
  46. * 让利设置
  47. * @return mixed
  48. */
  49. public function actionProfitSetting()
  50. {
  51. if (\Yii::$app->request->isAjax) {
  52. $service = new MerchantReduceProfitService();
  53. if (\Yii::$app->request->isPost) {
  54. $setting = $this->request->post();
  55. // 数据校验
  56. $res = \Yii::$app->services->validate->validate($setting, [
  57. [['new_rate'], 'required'],
  58. [['new_rate'], 'number'],
  59. [['id'], 'integer'],
  60. ], [
  61. 'rate' => '让利比例',
  62. ]);
  63. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  64. $ret = $service->save($this->mall_id, 0, $this->store_id, $setting['new_rate'], intval($setting['id'] ?? 0));
  65. return is_string($ret) ? $this->error($ret) : $this->success('提交成功');
  66. } else { // 当前让利, 需要从higo插件取
  67. $ret = $service->profit($this->mall_id, 0, $this->store_id);
  68. return $this->success('获取成功', $ret);
  69. }
  70. }
  71. return $this->render($this->action->id);
  72. }
  73. }