MerchantController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace addons\Store\api\modules\v1\controllers;
  3. use addons\Store\client\controllers\BaseController;
  4. use addons\Store\common\enums\StoreEnum;
  5. use addons\Store\common\models\Store;
  6. use addons\Store\common\services\MerchantReduceProfitService;
  7. use addons\Store\common\services\MerchantService;
  8. use api\controllers\OnAuthController;
  9. use common\enums\StatusEnum;
  10. use yii\helpers\Json;
  11. /**
  12. * 商户
  13. * @package addons\Store\client\controllers
  14. */
  15. class MerchantController extends OnAuthController
  16. {
  17. public $modelClass = '';
  18. /**
  19. * 不用进行登录验证的方法
  20. *
  21. * 例如: ['index', 'update', 'create', 'view', 'delete']
  22. * 默认全部需要验证
  23. *
  24. * @var array
  25. */
  26. protected $authOptional = [];
  27. protected $authStore = ['yin-dian','reduce-profit'];
  28. protected $store_id = 0;
  29. public function beforeAction($action)
  30. {
  31. parent::beforeAction($action);
  32. //检测门店身份
  33. if(in_array($action->id,$this->authStore)){
  34. $store = Store::find()
  35. ->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED])
  36. ->one();
  37. if (empty($store)) {
  38. \Yii::$app->response->data = ['code' => 1, 'msg' => '您非门店身份'];
  39. \Yii::$app->response->send();
  40. return;
  41. }
  42. $this->store_id = $store->id;
  43. }
  44. return $action;
  45. }
  46. /**
  47. * 商户号设置
  48. *
  49. * @return void
  50. */
  51. public function actionYinDian()
  52. {
  53. if ($this->request->isGet) {
  54. $service = new MerchantService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]);
  55. $tips = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'tips');
  56. return $this->success('获取成功', [
  57. 'setting' => $service->getSetting(),
  58. 'link' => $service->getSignLink(),
  59. 'link_tips' => $tips['link_tips'] ?? '此链接用作申请银典子商户号,子商户号用作接收用户贷款,请您务必进行账号申请;进行账号申请后,请将申请的商户编号填入下方,为确保交易无误,请确保填入的商户号无误!否则因为填写错误造成的损失将由您自行承担!',
  60. 'bank_account_tips' => $tips['bank_account_tips'] ?? '用于快速到账,银行卡账户账户必须是和商户编号对应的账号,如填写错误会导致无法快速到账'
  61. ]);
  62. }
  63. if ($this->request->isPost) {
  64. $setting = $this->request->post();
  65. // 数据校验
  66. $res = \Yii::$app->services->validate->validate($setting, [
  67. [['mcht_cd','bank_account', 'name', 'bank_account_type'], 'required'],
  68. [['bank_account_type'], 'integer'],
  69. ], [
  70. 'mcht_cd' => '商户号',
  71. 'bank_account' => '银行账户',
  72. 'name' => '账户名称',
  73. 'bank_account_type' => '账户类型',
  74. ]);
  75. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  76. $service = new MerchantService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]);
  77. return $service->setSetting($setting)
  78. ? $this->success('设置成功')
  79. : $this->error('保存失败');
  80. }
  81. }
  82. public function actionReduceProfit()
  83. {
  84. $service = new MerchantReduceProfitService();
  85. $setting = $service->profit($this->mall_id, $this->user_id, $this->store_id);
  86. if ($this->request->isGet) {
  87. return $this->success('获取成功', $setting);
  88. }
  89. if ($this->request->isPost) {
  90. $params = $this->request->post();
  91. // 数据校验
  92. $res = \Yii::$app->services->validate->validate($params, [
  93. [['new_rate'], 'required'],
  94. [['new_rate'], 'number'],
  95. ], [
  96. 'new_rate' => '让利百分比',
  97. ]);
  98. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  99. // 校验
  100. if ($params['new_rate'] < $setting['min'] || $params['new_rate'] > $setting['max']) {
  101. return $this->error("让利百分比必须在 {$setting['min']} - {$setting['max']} 之间");
  102. }
  103. $ret = $service->save($this->mall_id, $this->user_id, $this->store_id, $params['new_rate']);
  104. return is_string($ret) ? $this->error($ret) : $this->success('提交成功');
  105. }
  106. }
  107. }