Merchant.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\admin\system\merchant;
  12. use app\common\repositories\store\product\ProductCopyRepository;
  13. use crmeb\basic\BaseController;
  14. use app\common\repositories\system\merchant\MerchantAdminRepository;
  15. use app\common\repositories\system\merchant\MerchantCategoryRepository;
  16. use app\common\repositories\system\merchant\MerchantRepository;
  17. use app\validate\admin\MerchantValidate;
  18. use crmeb\jobs\ChangeMerchantStatusJob;
  19. use FormBuilder\Exception\FormBuilderException;
  20. use think\App;
  21. use think\db\exception\DataNotFoundException;
  22. use think\db\exception\DbException;
  23. use think\db\exception\ModelNotFoundException;
  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. public function count()
  48. {
  49. return app('json')->success($this->repository->count());
  50. }
  51. /**
  52. * @return mixed
  53. * @throws DataNotFoundException
  54. * @throws DbException
  55. * @throws ModelNotFoundException
  56. * @author xaboy
  57. * @day 2020-04-16
  58. */
  59. public function lst()
  60. {
  61. [$page, $limit] = $this->getPage();
  62. $where = $this->request->params(['keyword', 'date', 'status', 'statusTag', 'is_trader']);
  63. return app('json')->success($this->repository->lst($where, $page, $limit));
  64. }
  65. /**
  66. * @return mixed
  67. * @throws FormBuilderException
  68. * @author xaboy
  69. * @day 2020-04-16
  70. */
  71. public function createForm()
  72. {
  73. return app('json')->success(formToData($this->repository->form()));
  74. }
  75. /**
  76. * @param MerchantValidate $validate
  77. * @param MerchantCategoryRepository $merchantCategoryRepository
  78. * @param MerchantAdminRepository $adminRepository
  79. * @return mixed
  80. * @author xaboy
  81. * @day 2020/7/2
  82. */
  83. public function create(MerchantValidate $validate)
  84. {
  85. $data = $this->checkParam($validate);
  86. $this->repository->createMerchant($data);
  87. return app('json')->success('添加成功');
  88. }
  89. /**
  90. * @param int $id
  91. * @return mixed
  92. * @throws FormBuilderException
  93. * @throws DataNotFoundException
  94. * @throws DbException
  95. * @throws ModelNotFoundException
  96. * @author xaboy
  97. * @day 2020-04-16
  98. */
  99. public function updateForm($id)
  100. {
  101. if (!$this->repository->exists($id))
  102. return app('json')->fail('数据不存在');
  103. return app('json')->success(formToData($this->repository->updateForm($id)));
  104. }
  105. /**
  106. * @param int $id
  107. * @param MerchantValidate $validate
  108. * @param MerchantCategoryRepository $merchantCategoryRepository
  109. * @return mixed
  110. * @throws DbException
  111. * @author xaboy
  112. * @day 2020-05-06
  113. */
  114. public function update($id, MerchantValidate $validate, MerchantCategoryRepository $merchantCategoryRepository)
  115. {
  116. $data = $this->checkParam($validate, true);
  117. if (!$this->repository->exists($id))
  118. return app('json')->fail('数据不存在');
  119. if ($this->repository->fieldExists('mer_name', $data['mer_name'], $id))
  120. return app('json')->fail('商户名已存在');
  121. if ($data['mer_phone'] && isPhone($data['mer_phone']))
  122. return app('json')->fail('请输入正确的手机号');
  123. if (!$data['category_id'] || !$merchantCategoryRepository->exists($data['category_id']))
  124. return app('json')->fail('商户分类不存在');
  125. unset($data['mer_account'], $data['mer_password']);
  126. $this->repository->update($id, $data);
  127. return app('json')->success('编辑成功');
  128. }
  129. /**
  130. * @param int $id
  131. * @return mixed
  132. * @throws DbException
  133. * @author xaboy
  134. * @day 2020-05-06
  135. */
  136. public function updateLocal($id)
  137. {
  138. $data = $this->request->params([['long', ''], ['lat', '']]);
  139. if (!$this->repository->exists($id))
  140. return app('json')->fail('数据不存在');
  141. if ($data['long'] == '' || $data['lat'] == '')
  142. return app('json')->fail('请选择经纬度');
  143. $this->repository->update($id, $data);
  144. return app('json')->success('编辑成功');
  145. }
  146. /**
  147. * @param int $id
  148. * @return mixed
  149. * @throws DbException
  150. * @author xaboy
  151. * @day 2020-04-17
  152. */
  153. public function delete($id)
  154. {
  155. if (!$merchant = $this->repository->get(intval($id)))
  156. return app('json')->fail('数据不存在');
  157. if ($merchant->status)
  158. return app('json')->fail('请先关闭该商户');
  159. $this->repository->delete($id);
  160. return app('json')->success('编辑成功');
  161. }
  162. /**
  163. * @param MerchantValidate $validate
  164. * @param bool $isUpdate
  165. * @return array
  166. * @author xaboy
  167. * @day 2020-04-17
  168. */
  169. public function checkParam(MerchantValidate $validate, $isUpdate = false)
  170. {
  171. $data = $this->request->params([['category_id', 0], '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], ['is_trader', 0]]);
  172. if (!$isUpdate)
  173. $data += $this->request->params(['mer_account', 'mer_password']);
  174. else {
  175. $validate->isUpdate();
  176. unset($data['status']);
  177. }
  178. $validate->check($data);
  179. return $data;
  180. }
  181. /**
  182. * @param int $id
  183. * @return mixed
  184. * @throws DbException
  185. * @author xaboy
  186. * @day 2020-03-31
  187. */
  188. public function switchStatus($id)
  189. {
  190. $is_best = $this->request->param('status', 0) == 1 ? 1 : 0;
  191. if (!$this->repository->exists($id))
  192. return app('json')->fail('数据不存在');
  193. $this->repository->update($id, compact('is_best'));
  194. return app('json')->success('修改成功');
  195. }
  196. /**
  197. * @param int $id
  198. * @return mixed
  199. * @throws DbException
  200. * @author xaboy
  201. * @day 2020-03-31
  202. */
  203. public function switchClose($id)
  204. {
  205. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  206. if (!$this->repository->exists($id))
  207. return app('json')->fail('数据不存在');
  208. $this->repository->update($id, compact('status'));
  209. Queue::push(ChangeMerchantStatusJob::class, $id);
  210. return app('json')->success('修改成功');
  211. }
  212. /**
  213. * @param $id
  214. * @param MerchantAdminRepository $adminRepository
  215. * @return mixed
  216. * @throws DataNotFoundException
  217. * @throws DbException
  218. * @throws ModelNotFoundException
  219. * @author xaboy
  220. * @day 2020/7/7
  221. */
  222. public function login($id, MerchantAdminRepository $adminRepository)
  223. {
  224. if (!$this->repository->exists($id))
  225. return app('json')->fail('数据不存在');
  226. $adminInfo = $adminRepository->merIdByAdmin($id);
  227. $tokenInfo = $adminRepository->createToken($adminInfo);
  228. $admin = $adminInfo->toArray();
  229. unset($admin['pwd']);
  230. $data = [
  231. 'token' => $tokenInfo['token'],
  232. 'exp' => $tokenInfo['out'],
  233. 'admin' => $admin,
  234. 'url' => '/' . config('admin.merchant_prefix')
  235. ];
  236. return app('json')->success($data);
  237. }
  238. /**
  239. * TODO 修改复制次数表单
  240. * @param $id
  241. * @return mixed
  242. * @author Qinii
  243. * @day 2020-08-06
  244. */
  245. public function changeCopyNumForm($id)
  246. {
  247. return app('json')->success(formToData($this->repository->copyForm($id)));
  248. }
  249. /**
  250. * TODO 修改复制次数
  251. * @param $id
  252. * @return mixed
  253. * @author Qinii
  254. * @day 2020-08-06
  255. */
  256. public function changeCopyNum($id)
  257. {
  258. $data = $this->request->params(['type', 'num']);
  259. $num = $data['num'];
  260. if (!is_int($num) || $num <= 0) return app('json')->fail('次数必须为正整数');
  261. if ($data['type'] == 2) {
  262. $mer_num = $this->repository->getCopyNum($id);
  263. if (($mer_num - $num) < 0) return app('json')->fail('剩余次数不足');
  264. $num = '-' . $data['num'];
  265. }
  266. $arr = [
  267. 'type' => 'sys',
  268. 'num' => $num,
  269. 'message' => '平台修改「' . $this->request->adminId() . '」',
  270. ];
  271. app()->make(ProductCopyRepository::class)->add($arr, $id);
  272. return app('json')->success('修改成功');
  273. }
  274. }