Merchant.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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([['longitude', ''], ['latitude', '']]);
  139. var_dump($data);
  140. if (!$this->repository->exists($id))
  141. return app('json')->fail('数据不存在');
  142. if ($data['longitude'] == '' || !$data['latitude'] == '')
  143. return app('json')->fail('请选择经纬度');
  144. $this->repository->update($id, $data);
  145. return app('json')->success('编辑成功');
  146. }
  147. /**
  148. * @param int $id
  149. * @return mixed
  150. * @throws DbException
  151. * @author xaboy
  152. * @day 2020-04-17
  153. */
  154. public function delete($id)
  155. {
  156. if (!$merchant = $this->repository->get(intval($id)))
  157. return app('json')->fail('数据不存在');
  158. if ($merchant->status)
  159. return app('json')->fail('请先关闭该商户');
  160. $this->repository->delete($id);
  161. return app('json')->success('编辑成功');
  162. }
  163. /**
  164. * @param MerchantValidate $validate
  165. * @param bool $isUpdate
  166. * @return array
  167. * @author xaboy
  168. * @day 2020-04-17
  169. */
  170. public function checkParam(MerchantValidate $validate, $isUpdate = false)
  171. {
  172. $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]]);
  173. if (!$isUpdate)
  174. $data += $this->request->params(['mer_account', 'mer_password']);
  175. else {
  176. $validate->isUpdate();
  177. unset($data['status']);
  178. }
  179. $validate->check($data);
  180. return $data;
  181. }
  182. /**
  183. * @param int $id
  184. * @return mixed
  185. * @throws DbException
  186. * @author xaboy
  187. * @day 2020-03-31
  188. */
  189. public function switchStatus($id)
  190. {
  191. $is_best = $this->request->param('status', 0) == 1 ? 1 : 0;
  192. if (!$this->repository->exists($id))
  193. return app('json')->fail('数据不存在');
  194. $this->repository->update($id, compact('is_best'));
  195. return app('json')->success('修改成功');
  196. }
  197. /**
  198. * @param int $id
  199. * @return mixed
  200. * @throws DbException
  201. * @author xaboy
  202. * @day 2020-03-31
  203. */
  204. public function switchClose($id)
  205. {
  206. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  207. if (!$this->repository->exists($id))
  208. return app('json')->fail('数据不存在');
  209. $this->repository->update($id, compact('status'));
  210. Queue::push(ChangeMerchantStatusJob::class, $id);
  211. return app('json')->success('修改成功');
  212. }
  213. /**
  214. * @param $id
  215. * @param MerchantAdminRepository $adminRepository
  216. * @return mixed
  217. * @throws DataNotFoundException
  218. * @throws DbException
  219. * @throws ModelNotFoundException
  220. * @author xaboy
  221. * @day 2020/7/7
  222. */
  223. public function login($id, MerchantAdminRepository $adminRepository)
  224. {
  225. if (!$this->repository->exists($id))
  226. return app('json')->fail('数据不存在');
  227. $adminInfo = $adminRepository->merIdByAdmin($id);
  228. $tokenInfo = $adminRepository->createToken($adminInfo);
  229. $admin = $adminInfo->toArray();
  230. unset($admin['pwd']);
  231. $data = [
  232. 'token' => $tokenInfo['token'],
  233. 'exp' => $tokenInfo['out'],
  234. 'admin' => $admin,
  235. 'url' => '/' . config('admin.merchant_prefix')
  236. ];
  237. return app('json')->success($data);
  238. }
  239. /**
  240. * TODO 修改复制次数表单
  241. * @param $id
  242. * @return mixed
  243. * @author Qinii
  244. * @day 2020-08-06
  245. */
  246. public function changeCopyNumForm($id)
  247. {
  248. return app('json')->success(formToData($this->repository->copyForm($id)));
  249. }
  250. /**
  251. * TODO 修改复制次数
  252. * @param $id
  253. * @return mixed
  254. * @author Qinii
  255. * @day 2020-08-06
  256. */
  257. public function changeCopyNum($id)
  258. {
  259. $data = $this->request->params(['type', 'num']);
  260. $num = $data['num'];
  261. if (!is_int($num) || $num <= 0) return app('json')->fail('次数必须为正整数');
  262. if ($data['type'] == 2) {
  263. $mer_num = $this->repository->getCopyNum($id);
  264. if (($mer_num - $num) < 0) return app('json')->fail('剩余次数不足');
  265. $num = '-' . $data['num'];
  266. }
  267. $arr = [
  268. 'type' => 'sys',
  269. 'num' => $num,
  270. 'message' => '平台修改「' . $this->request->adminId() . '」',
  271. ];
  272. app()->make(ProductCopyRepository::class)->add($arr, $id);
  273. return app('json')->success('修改成功');
  274. }
  275. }