Merchant.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-04-16
  7. *
  8. *
  9. */
  10. namespace app\controller\admin\system\merchant;
  11. use app\common\repositories\store\product\ProductCopyRepository;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\system\merchant\MerchantAdminRepository;
  14. use app\common\repositories\system\merchant\MerchantCategoryRepository;
  15. use app\common\repositories\system\merchant\MerchantRepository;
  16. use app\validate\admin\MerchantValidate;
  17. use crmeb\jobs\ChangeMerchantStatusJob;
  18. use FormBuilder\Exception\FormBuilderException;
  19. use think\App;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\DbException;
  22. use think\db\exception\ModelNotFoundException;
  23. use think\facade\Db;
  24. use think\facade\Queue;
  25. /**
  26. * Class Merchant
  27. * @package app\controller\admin\system\merchant
  28. * @author xaboy
  29. * @day 2020-04-16
  30. */
  31. class Merchant extends BaseController
  32. {
  33. /**
  34. * @var MerchantRepository
  35. */
  36. protected $repository;
  37. /**
  38. * Merchant constructor.
  39. * @param App $app
  40. * @param MerchantRepository $repository
  41. */
  42. public function __construct(App $app, MerchantRepository $repository)
  43. {
  44. parent::__construct($app);
  45. $this->repository = $repository;
  46. }
  47. /**
  48. * @return mixed
  49. * @throws DataNotFoundException
  50. * @throws DbException
  51. * @throws ModelNotFoundException
  52. * @author xaboy
  53. * @day 2020-04-16
  54. */
  55. public function lst()
  56. {
  57. [$page, $limit] = $this->getPage();
  58. $where = $this->request->params(['keyword', 'date', 'status','mer_state','city', 'account']);
  59. return app('json')->success($this->repository->lstLeft($where, $page, $limit));
  60. // return app('json')->success($this->repository->lst($where, $page, $limit));
  61. }
  62. /**
  63. * @return mixed
  64. * @throws FormBuilderException
  65. * @author xaboy
  66. * @day 2020-04-16
  67. */
  68. public function createForm()
  69. {
  70. return app('json')->success(formToData($this->repository->form()));
  71. }
  72. /**
  73. * @param MerchantValidate $validate
  74. * @param MerchantCategoryRepository $merchantCategoryRepository
  75. * @param MerchantAdminRepository $adminRepository
  76. * @return mixed
  77. * @author xaboy
  78. * @day 2020/7/2
  79. */
  80. public function create(MerchantValidate $validate, MerchantCategoryRepository $merchantCategoryRepository, MerchantAdminRepository $adminRepository)
  81. {
  82. $data = $this->checkParam($validate);
  83. if ($this->repository->fieldExists('mer_name', $data['mer_name']))
  84. return app('json')->fail('商户名已存在');
  85. if ($data['mer_phone'] && isPhone($data['mer_phone']))
  86. return app('json')->fail('请输入正确的手机号');
  87. if (!$data['category_id'] || !$merchantCategoryRepository->exists($data['category_id']))
  88. return app('json')->fail('商户分类不存在');
  89. if ($adminRepository->fieldExists('account', $data['mer_account']))
  90. return app('json')->fail('账号已存在');
  91. $this->repository->createMerchant($data);
  92. return app('json')->success('添加成功');
  93. }
  94. /**
  95. * @param int $id
  96. * @return mixed
  97. * @throws FormBuilderException
  98. * @throws DataNotFoundException
  99. * @throws DbException
  100. * @throws ModelNotFoundException
  101. * @author xaboy
  102. * @day 2020-04-16
  103. */
  104. public function updateForm($id)
  105. {
  106. if (!$this->repository->exists($id))
  107. return app('json')->fail('数据不存在');
  108. return app('json')->success(formToData($this->repository->updateForm($id)));
  109. }
  110. /**
  111. * @param int $id
  112. * @param MerchantValidate $validate
  113. * @param MerchantCategoryRepository $merchantCategoryRepository
  114. * @return mixed
  115. * @throws DbException
  116. * @author xaboy
  117. * @day 2020-05-06
  118. */
  119. public function update($id, MerchantValidate $validate, MerchantCategoryRepository $merchantCategoryRepository)
  120. {
  121. $data = $this->checkParam($validate, true);
  122. if (!$this->repository->exists($id))
  123. return app('json')->fail('数据不存在');
  124. if ($this->repository->fieldExists('mer_name', $data['mer_name'], $id))
  125. return app('json')->fail('商户名已存在');
  126. if ($data['mer_phone'] && isPhone($data['mer_phone']))
  127. return app('json')->fail('请输入正确的手机号');
  128. if (!$data['category_id'] || !$merchantCategoryRepository->exists($data['category_id']))
  129. return app('json')->fail('商户分类不存在');
  130. unset($data['mer_account'], $data['mer_password']);
  131. unset($data['mer_type']);
  132. $this->repository->update($id, $data);
  133. return app('json')->success('编辑成功');
  134. }
  135. /**
  136. * @param int $id
  137. * @return mixed
  138. * @throws DbException
  139. * @author xaboy
  140. * @day 2020-04-17
  141. */
  142. public function delete($id)
  143. {
  144. if (!$this->repository->exists($id))
  145. return app('json')->fail('数据不存在');
  146. $this->repository->update($id, ['is_del' => 1]);
  147. //下架商品
  148. $this->repository->MerUpdate(['mer_id'=>$id],['status'=>-2]);
  149. return app('json')->success('编辑成功');
  150. }
  151. /**
  152. * @param MerchantValidate $validate
  153. * @param bool $isUpdate
  154. * @return array
  155. * @author xaboy
  156. * @day 2020-04-17
  157. */
  158. public function checkParam(MerchantValidate $validate, $isUpdate = false)
  159. {
  160. $data = $this->request->params([['category_id', 0], 'mer_type','mer_name', 'commission_rate', 'real_name', 'mer_phone', 'mer_keyword', 'mer_address', 'mark', ['sort', 0], ['status', 0], ['is_audit', 0], ['is_best', 0], ['is_bro_goods', 0], ['is_bro_room', 0]]);
  161. if (!$isUpdate)
  162. $data += $this->request->params(['mer_account', 'mer_password']);
  163. else
  164. $validate->isUpdate();
  165. $validate->check($data);
  166. return $data;
  167. }
  168. /**
  169. * @param int $id
  170. * @return mixed
  171. * @throws DbException
  172. * @author xaboy
  173. * @day 2020-03-31
  174. */
  175. public function switchStatus($id)
  176. {
  177. $is_best = $this->request->param('status', 0) == 1 ? 1 : 0;
  178. if (!$this->repository->exists($id))
  179. return app('json')->fail('数据不存在');
  180. $this->repository->update($id, compact('is_best'));
  181. Queue::push(ChangeMerchantStatusJob::class, $id);
  182. return app('json')->success('修改成功');
  183. }
  184. /**
  185. * @param $id
  186. * @param MerchantAdminRepository $adminRepository
  187. * @return mixed
  188. * @throws DataNotFoundException
  189. * @throws DbException
  190. * @throws ModelNotFoundException
  191. * @author xaboy
  192. * @day 2020/7/7
  193. */
  194. public function login($id, MerchantAdminRepository $adminRepository)
  195. {
  196. if (!$this->repository->exists($id))
  197. return app('json')->fail('数据不存在');
  198. $adminInfo = $adminRepository->merIdByAdmin($id);
  199. $tokenInfo = $adminRepository->createToken($adminInfo);
  200. $admin = $adminInfo->toArray();
  201. unset($admin['pwd']);
  202. $data = [
  203. 'token' => $tokenInfo['token'],
  204. 'exp' => $tokenInfo['out'],
  205. 'admin' => $admin,
  206. 'url' => '/' . config('admin.merchant_prefix')
  207. ];
  208. return app('json')->success($data);
  209. }
  210. /**
  211. * TODO 修改复制次数表单
  212. * @param $id
  213. * @return mixed
  214. * @author Qinii
  215. * @day 2020-08-06
  216. */
  217. public function changeCopyNumForm($id)
  218. {
  219. return app('json')->success(formToData($this->repository->copyForm($id)));
  220. }
  221. /**
  222. * TODO 修改复制次数
  223. * @param $id
  224. * @return mixed
  225. * @author Qinii
  226. * @day 2020-08-06
  227. */
  228. public function changeCopyNum($id)
  229. {
  230. $data = $this->request->params(['type','num']);
  231. $num = $data['num'];
  232. if($data['type'] == 2){
  233. $mer_num = $this->repository->getCopyNum($id);
  234. if(($mer_num - $num)<0) return app('json')->fail('剩余次数不足');
  235. $num = '-'.$data['num'];
  236. }
  237. $arr = [
  238. 'type' => 'sys',
  239. 'num' => $num,
  240. 'message' => '平台修改「'.$this->request->adminId().'」',
  241. ];
  242. app()->make(ProductCopyRepository::class)->add($arr,$id);
  243. return app('json')->success('修改成功');
  244. }
  245. /**
  246. * @param $id
  247. * @return mixed
  248. * @throws FormBuilderException
  249. * @author xaboy
  250. * @day 2020-05-07
  251. */
  252. public function changeProForm($id)
  253. {
  254. if (!$this->repository->exists($id))
  255. return app('json')->fail('数据不存在');
  256. return app('json')->success(formToData($this->repository->changeProForm($id)));
  257. }
  258. /*
  259. * 更改用户推荐信息
  260. * cabbage
  261. * 2020-11-04
  262. * */
  263. public function changePro($id,MerchantRepository $repository)
  264. {
  265. $spread_account = $this->request->param('spread_account');
  266. if (!$spread_account || !$id){
  267. return app('json')->fail('参数丢失');
  268. }
  269. if($repository->update_spread($id,$spread_account)){
  270. return app('json')->success("修改成功");
  271. }else{
  272. return app('json')->fail("失败");
  273. }
  274. }
  275. public function mechant_expire()
  276. {
  277. $data=Db::name('merchant')->where('status',1)->where('expire_time','<',time())->select();
  278. $num=0;
  279. $id='';
  280. if(!$data->isEmpty()){
  281. foreach ($data as $k=>$v){
  282. $num++;
  283. $id.=$v['mer_id'].',';
  284. Db::name('merchant')->where('mer_id',$v['mer_id'])->update(['status'=>0]);
  285. }
  286. }
  287. $id=rtrim($id,',');
  288. return app('json')->success('商户到期一共'.$num.'个'.'商户ID分别是'.$id);
  289. }
  290. public function update_dc(){
  291. $data = $this->request->params(['mer_id','dc_id']);
  292. if(in_array('',$data)){
  293. return app('json')->fail('参数丢失');
  294. }
  295. $res = Db::name('merchant') -> where('mer_id',$data['mer_id']) -> update(['dc_id'=>$data['dc_id'],'merchant_type'=>3]);
  296. if ($res){
  297. return app('json')->success('修改成功');
  298. }
  299. }
  300. public function update_level(){
  301. $data = $this->request->params(['mer_id','level']);
  302. if(in_array('',$data)){
  303. return app('json')->fail('参数丢失');
  304. }
  305. $res = Db::name('merchant') -> where('mer_id',$data['mer_id']) -> update(['merchant_level'=>$data['level']]);
  306. if ($res){
  307. return app('json')->success('修改成功');
  308. }
  309. }
  310. }