MerchantIntention.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\store\merchant;
  12. use app\common\repositories\system\merchant\MerchantAdminRepository;
  13. use app\common\repositories\system\merchant\MerchantCategoryRepository;
  14. use app\common\repositories\system\merchant\MerchantRepository;
  15. use app\common\repositories\system\merchant\MerchantTypeRepository;
  16. use app\validate\api\MerchantIntentionValidate;
  17. use crmeb\services\SmsService;
  18. use crmeb\services\SwooleTaskService;
  19. use crmeb\services\YunxinSmsService;
  20. use think\App;
  21. use crmeb\basic\BaseController;
  22. use app\common\repositories\system\merchant\MerchantIntentionRepository as repository;
  23. use think\exception\ValidateException;
  24. class MerchantIntention extends BaseController
  25. {
  26. protected $repository;
  27. protected $userInfo;
  28. public function __construct(App $app, repository $repository)
  29. {
  30. parent::__construct($app);
  31. $this->repository = $repository;
  32. $this->userInfo = $this->request->isLogin() ? $this->request->userInfo() : null;
  33. }
  34. public function create()
  35. {
  36. $data = $this->checkParams();
  37. if (!systemConfig('mer_intention_open')) {
  38. return app('json')->fail('未开启商户入驻');
  39. }
  40. if ($this->userInfo) $data['uid'] = $this->userInfo->uid;
  41. $make = app()->make(MerchantRepository::class);
  42. if ($make->fieldExists('mer_name', $data['mer_name']))
  43. throw new ValidateException('商户名称已存在,不可申请');
  44. if ($make->fieldExists('mer_phone', $data['phone']))
  45. throw new ValidateException('手机号已存在,不可申请');
  46. $adminRepository = app()->make(MerchantAdminRepository::class);
  47. if ($adminRepository->fieldExists('account', $data['phone']))
  48. throw new ValidateException('手机号已是管理员,不可申请');
  49. $intention = $this->repository->create($data);
  50. SwooleTaskService::admin('notice', [
  51. 'type' => 'new_intention',
  52. 'data' => [
  53. 'title' => '商户入驻申请',
  54. 'message' => '您有一个新的商户入驻申请',
  55. 'id' => $intention->mer_intention_id
  56. ]
  57. ]);
  58. return app('json')->success('提交成功');
  59. }
  60. public function update($id)
  61. {
  62. if (!$this->repository->getWhere(['mer_intention_id' => (int)$id, 'uid' => $this->userInfo->uid, 'is_del' => 0]))
  63. return app('json')->fail('数据不存在');
  64. $data = $this->checkParams();
  65. if (!systemConfig('mer_intention_open')) {
  66. return app('json')->fail('未开启商户入驻');
  67. }
  68. $data['create_time'] = date('Y-m-d H:i:s', time());
  69. $this->repository->updateIntention((int)$id, $data);
  70. SwooleTaskService::admin('notice', [
  71. 'type' => 'new_intention',
  72. 'data' => [
  73. 'title' => '商户入驻申请',
  74. 'message' => '您有一个新的商户入驻申请',
  75. 'id' => $id
  76. ]
  77. ]);
  78. return app('json')->success('修改成功');
  79. }
  80. public function lst()
  81. {
  82. [$page, $limit] = $this->getPage();
  83. $data = $this->repository->getList(['uid' => $this->userInfo->uid], $page, $limit);
  84. return app('json')->success($data);
  85. }
  86. function detail($id)
  87. {
  88. $data = $this->repository->detail((int)$id, $this->userInfo->uid);
  89. if (!$data) {
  90. return app('json')->fail('数据不存在');
  91. }
  92. if ($data->status == 1) {
  93. $data['login_url'] = rtrim(systemConfig('site_url'), '/') . '/' . config('admin.merchant_prefix');
  94. }
  95. return app('json')->success($data);
  96. }
  97. protected function checkParams()
  98. {
  99. $data = $this->request->params(['phone', 'mer_name', 'name', 'code', 'images', 'merchant_category_id', 'mer_type_id']);
  100. app()->make(MerchantIntentionValidate::class)->check($data);
  101. $check = app()->make(SmsService::class)->checkSmsCode($data['phone'], $data['code'], 'intention');
  102. $data['mer_type_id'] = (int)$data['mer_type_id'];
  103. if (!$check) throw new ValidateException('验证码不正确');
  104. if (!app()->make(MerchantCategoryRepository::class)->get($data['merchant_category_id'])) throw new ValidateException('商户分类不存在');
  105. if ($data['mer_type_id'] && !app()->make(MerchantTypeRepository::class)->exists($data['mer_type_id']))
  106. throw new ValidateException('店铺类型不存在');
  107. unset($data['code']);
  108. return $data;
  109. }
  110. /**
  111. * 商户分类
  112. * @Author:Qinii
  113. * @Date: 2020/9/15
  114. * @return mixed
  115. */
  116. public function cateLst()
  117. {
  118. $lst = app()->make(MerchantCategoryRepository::class)->getSelect();
  119. return app('json')->success($lst);
  120. }
  121. public function typeLst()
  122. {
  123. $lst = app()->make(MerchantTypeRepository::class)->getSelect();
  124. return app('json')->success($lst);
  125. }
  126. }