AgentController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace Modules\AgentApi\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Models\Mysql\AgentModel;
  5. use App\Repositories\AgentRepository;
  6. use App\Repositories\ChannelRepository;
  7. use App\Repositories\ConfigRepoistory;
  8. use App\Repositories\Criterias\AgentPagesCriteria;
  9. use App\Repositories\Criterias\OrderLimitCriteria;
  10. use App\Repositories\ManagerRepository;
  11. use Illuminate\Support\Facades\Validator;
  12. use Modules\AgentApi\Responses\AgentDetailResponse;
  13. use Modules\AgentApi\Responses\AgentPagesResponse;
  14. use Modules\AgentApi\Responses\CommonSuccessUidResponse;
  15. class AgentController extends BaseController
  16. {
  17. /**
  18. * 代理列表
  19. *
  20. * @return \Illuminate\Http\JsonResponse|AgentPagesResponse
  21. */
  22. public function lists()
  23. {
  24. $inputData = $this->getJsonData();
  25. $rules = [
  26. 'parent_uid' => 'nullable|string',
  27. 'page' => 'nullable|integer',
  28. 'page_size' => 'nullable|integer',
  29. 'status' => 'nullable|integer'
  30. ];
  31. $validator = Validator::make($inputData, $rules);
  32. if ($validator->fails()) {
  33. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  34. return json_fail($validator->errors()->first());
  35. }
  36. $accountInfo = $this->getCurrentAgent();
  37. // 针对layui table自动分页请求参数,优先GET值.
  38. $queryPage = request()->has('page') ? request()->input('page', 1) : null;
  39. $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
  40. if (!is_null($queryPage)) {
  41. $inputData['page'] = $queryPage;
  42. }
  43. if (!is_null($queryPageSize)) {
  44. $inputData['page_size'] = $queryPageSize;
  45. }
  46. $orders = [
  47. ['id', 'DESC']
  48. ];
  49. if (!isset($inputData['page_size']) || $inputData['page_size'] > 500) {
  50. $pageSize = CommonConstant::PAGE_SIZE;
  51. } else {
  52. $pageSize = $inputData['page_size'];
  53. }
  54. $page = format_page($inputData['page'] ?? 0);
  55. //默认上级代理
  56. if (empty($inputData['parent_uid'])) {
  57. $inputData['parent_uid'] = $accountInfo['uid'];
  58. } else {
  59. // 查询的判断搜否是自己代理下的
  60. $agentModel = new AgentModel();
  61. $agentUids = $agentModel->findAllChildUids($accountInfo['uid']);
  62. $agentUids = array_merge($agentUids, [$accountInfo['uid']]);
  63. if (!in_array($inputData['parent_uid'], $agentUids)) {
  64. return json_fail('无数据');
  65. }
  66. }
  67. /**
  68. * @var AgentRepository $agentRepo
  69. */
  70. $agentRepo = app(AgentRepository::class);
  71. $field = [
  72. 'uid',
  73. 'nick',
  74. 'username',
  75. 'parent_uid',
  76. 'commission_ratio',
  77. 'status',
  78. 'created_at',
  79. 'agent_level',
  80. 'deduction_switch',
  81. 'own_app_uids'
  82. ];
  83. $res = $agentRepo->pushCriteria(new AgentPagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize,
  84. $field, $page);
  85. return new AgentPagesResponse($res);
  86. }
  87. /**
  88. * 创建渠道
  89. *
  90. * @param AgentRepository $objRepo
  91. * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
  92. * @throws \App\Exceptions\ApiException
  93. * @throws \Psr\SimpleCache\InvalidArgumentException
  94. */
  95. public function create(AgentRepository $objRepo)
  96. {
  97. $inputData = $this->getJsonData();
  98. $rules = [
  99. 'username' => 'unique:agent,username|required|max:16',
  100. 'password' => 'required|min:6|max:32',
  101. 'two_password' => 'required|string',
  102. 'commission_ratio' => 'required|integer',
  103. 'own_app_uids' => 'required|string',
  104. ];
  105. $validator = Validator::make($inputData, $rules);
  106. if ($validator->fails()) {
  107. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  108. return json_fail($validator->errors()->first());
  109. }
  110. if ($inputData['password'] != $inputData['two_password']) {
  111. return json_fail('确认密码错误,请重新输入!');
  112. }
  113. $accountInfo = $this->getCurrentAgent();
  114. /**
  115. * @var ConfigRepoistory $confRepo
  116. */
  117. $confRepo = app(ConfigRepoistory::class);
  118. if (empty($accountInfo['parent_uids'])) {
  119. $parentUids = $accountInfo['uid'];
  120. } else {
  121. $parentUids = $accountInfo['parent_uids'] . ',' . $accountInfo['uid'];
  122. }
  123. $setData = [
  124. 'username' => $inputData['username'],
  125. 'nick' => $inputData['username'],
  126. 'password' => $inputData['password'],
  127. 'status' => 1,
  128. 'parent_uid' => $accountInfo['uid'],
  129. 'parent_uids' => $parentUids,
  130. 'withdraw_pwd' => $confRepo->get('withdraw_pwd', 'sys'),
  131. 'own_app_uids' => $inputData['own_app_uids'],
  132. 'commission_ratio' => $inputData['commission_ratio'],
  133. 'deduction_switch' => 0,
  134. 'jdata' => json_encode([])
  135. ];
  136. $agentObj = $objRepo->createOne($setData, $accountInfo['uid']);
  137. $respData = [
  138. 'uid' => $agentObj->uid,
  139. ];
  140. if (empty($accountInfo['child_uids'])) {
  141. $childUids = $agentObj->uid;
  142. } else {
  143. $childUids = $accountInfo['child_uids'] . ',' . $agentObj->uid;
  144. }
  145. //新增子集集合
  146. $objRepo->update(['child_uids' => $childUids], $accountInfo['uid'], 'uid');
  147. //创建渠道
  148. $chanRepo = app(ChannelRepository::class);
  149. $chanRepo->createAgentChannel($agentObj->uid);
  150. return new CommonSuccessUidResponse($respData);
  151. }
  152. /**
  153. * 账户信息修改
  154. *
  155. * @param AgentRepository $agentRepo
  156. * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
  157. * @throws \App\Exceptions\ApiException
  158. */
  159. public function modify(AgentRepository $agentRepo)
  160. {
  161. $inputData = $this->getJsonData();
  162. $rules = [
  163. 'nick' => 'nullable|max:16',
  164. 'old_password' => 'nullable|string',
  165. 'password' => 'nullable|min:6|max:32',
  166. 'withdraw_pwd' => 'nullable|min:6|max:32',
  167. ];
  168. $validator = Validator::make($inputData, $rules);
  169. if ($validator->fails()) {
  170. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  171. return json_fail($validator->errors()->first());
  172. }
  173. $accountInfo = $this->getCurrentAgent();
  174. if (!empty($inputData['password'])) {
  175. /**
  176. * @var ManagerRepository $managerRepo
  177. */
  178. $managerRepo = app(ManagerRepository::class);
  179. $md5password = $managerRepo->generatePassword($inputData['old_password'], $accountInfo['salt']);
  180. if ($md5password != $accountInfo['password']) {
  181. return json_fail('历史密码错误,请确认后再进行修改');
  182. }
  183. }
  184. $setData = [];
  185. if (!empty($inputData['nick'])) {
  186. $setData['nick'] = $inputData['nick'];
  187. }
  188. if (!empty($inputData['password'])) {
  189. $setData['password'] = $inputData['password'];
  190. }
  191. if (!empty($inputData['withdraw_pwd'])) {
  192. $setData['withdraw_pwd'] = $inputData['withdraw_pwd'];
  193. }
  194. $saveResult = $agentRepo->agentEdit($setData, $accountInfo['uid']);
  195. if (!$saveResult) {
  196. return json_fail('修改失败');
  197. }
  198. return new CommonSuccessUidResponse($accountInfo['uid']);
  199. }
  200. /**
  201. * 匹配用户名
  202. *
  203. * @return \Illuminate\Http\JsonResponse
  204. */
  205. public function checkUsername()
  206. {
  207. $inputData = $this->getJsonData();
  208. $rules = [
  209. 'username' => 'unique:agent,username|required|max:16',
  210. ];
  211. $message = [
  212. 'username.unique' => '账号已存在!',
  213. 'username.required' => '用户名不能为空!',
  214. 'username.max' => '用户名不能大于16字节!'
  215. ];
  216. $validator = Validator::make($inputData, $rules, $message);
  217. if ($validator->fails()) {
  218. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  219. return json_fail($validator->errors()->first());
  220. }
  221. return json_fail('用户名可用!', 200, []);
  222. }
  223. /**
  224. * 我的详情.
  225. * @return mixed|AgentDetailResponse
  226. */
  227. public function detailMe()
  228. {
  229. $accountInfo = $this->getCurrentAgent();
  230. if (empty($accountInfo)) {
  231. return json_fail(trans('common.err_resource_not_found'));
  232. }
  233. return new AgentDetailResponse($accountInfo);
  234. }
  235. public function editLevelAgent()
  236. {
  237. $inputData = $this->getJsonData();
  238. $rules = [
  239. 'uid' => 'required',
  240. 'own_app_uids' => 'required',
  241. 'status' => 'required',
  242. 'commission_ratio' => 'required'
  243. ];
  244. $message = [
  245. 'uid.required' => 'UID出错!',
  246. 'own_app_uids.required' => '应用不能为空!',
  247. 'status.required' => '状态不能为空!',
  248. 'commission_ratio.required' => '分成比例不能为空!'
  249. ];
  250. $validator = Validator::make($inputData, $rules, $message);
  251. if ($validator->fails()) {
  252. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  253. return json_fail($validator->errors()->first());
  254. }
  255. $accountInfo = $this->getCurrentAgent();
  256. $agentModel = new AgentModel();
  257. // 查询修改的用户下级最高的分成比例
  258. $commission_ratio_min = $agentModel->where('parent_uid', $inputData['uid'])
  259. ->orderByDesc('commission_ratio')
  260. ->pluck('commission_ratio')->first();
  261. // 修改的不能小于他下级的
  262. if ($commission_ratio_min) {
  263. if ($inputData['commission_ratio'] < $commission_ratio_min) {
  264. return json_fail('比例不能小于他下级的!');
  265. }
  266. }
  267. if ($inputData['commission_ratio'] > $accountInfo->commission_ratio) {
  268. return json_fail('比例不能高出自己的!');
  269. }
  270. // 查询是否是自己
  271. $agent = $agentModel->where('uid', $inputData['uid'])->first();
  272. if ($agent->parent_uid !== $accountInfo->uid) {
  273. return json_fail('不属于一级代理!');
  274. }
  275. if (!empty($inputData['own_app_uids'])) {
  276. $agent->own_app_uids = $inputData['own_app_uids'];
  277. }
  278. if (!empty($inputData['status'])) {
  279. $agent->status = $inputData['status'];
  280. }
  281. if (!empty($inputData['commission_ratio'])) {
  282. $agent->commission_ratio = $inputData['commission_ratio'];
  283. }
  284. if (!$agent->save()) {
  285. return json_fail('更新失败!');
  286. }
  287. return json_success('更新成功!');
  288. }
  289. }