ApplyController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace addons\OperationCenter\api\modules\v1\controllers;
  3. use addons\OperationCenter\common\services\ApplyService;
  4. use addons\OperationCenter\common\services\SettingService;
  5. use api\controllers\OnAuthController;
  6. use Yii;
  7. /**
  8. * 申请控制器
  9. *
  10. * ApplyController class
  11. * @package addons\OperationCenter\api\modules\v1\controllers
  12. */
  13. class ApplyController extends OnAuthController
  14. {
  15. /**
  16. * @var string
  17. */
  18. public $modelClass = '';
  19. /**
  20. * @var boolean
  21. */
  22. public $enableCsrfValidation = false;
  23. /**
  24. * 不用进行登录验证的方法
  25. *
  26. * 例如: ['index', 'update', 'create', 'view', 'delete']
  27. * 默认全部需要验证
  28. *
  29. * @var array
  30. */
  31. protected $authOptional = ['index'];
  32. /**
  33. * 获取申请状态
  34. *
  35. * @return string
  36. */
  37. public function actionGetApplyStatus()
  38. {
  39. $service = new ApplyService(['mall_id' => $this->mall_id]);
  40. return $this->success('ok', $service->getApplyStatus($this->user_id));
  41. }
  42. /**
  43. * 获取申请配置
  44. *
  45. * @return string
  46. */
  47. public function actionGetApplyConfig()
  48. {
  49. $service = new SettingService(['mall_id' => $this->mall_id]);
  50. return $this->success('ok', $service->getApplyConfig());
  51. }
  52. /**
  53. * 添加申请
  54. *
  55. * @return string
  56. */
  57. public function actionAddApply()
  58. {
  59. $data = $this->request->post();
  60. $service = new ApplyService(['mall_id' => $this->mall_id]);
  61. // 申请项
  62. $apply_for = $service->getApplyingFor();
  63. $apply_for = array_merge($apply_for, [
  64. 'province', 'province_id', 'city', 'city_id', 'district', 'district_id', 'logo'
  65. ]);
  66. // 验证
  67. $validate = Yii::$app->services->validate->validate($data, [
  68. [$apply_for, 'required'],
  69. [['town', 'town_id'], 'safe']
  70. ]);
  71. if ($validate === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  72. return $service->addApply($this->user_id, $data)
  73. ? $this->success('ok')
  74. : $this->error(sprintf('申请失败: %s', $service->getError()));
  75. }
  76. }