| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- namespace Modules\AgentApi\Controllers;
- use App\Constants\CommonConstant;
- use App\Models\Mysql\AgentModel;
- use App\Repositories\AgentRepository;
- use App\Repositories\ChannelRepository;
- use App\Repositories\ConfigRepoistory;
- use App\Repositories\Criterias\AgentPagesCriteria;
- use App\Repositories\Criterias\OrderLimitCriteria;
- use App\Repositories\ManagerRepository;
- use Illuminate\Support\Facades\Validator;
- use Modules\AgentApi\Responses\AgentDetailResponse;
- use Modules\AgentApi\Responses\AgentPagesResponse;
- use Modules\AgentApi\Responses\CommonSuccessUidResponse;
- class AgentController extends BaseController
- {
- /**
- * 代理列表
- *
- * @return \Illuminate\Http\JsonResponse|AgentPagesResponse
- */
- public function lists()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'parent_uid' => 'nullable|string',
- 'page' => 'nullable|integer',
- 'page_size' => 'nullable|integer',
- 'status' => 'nullable|integer'
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $accountInfo = $this->getCurrentAgent();
- // 针对layui table自动分页请求参数,优先GET值.
- $queryPage = request()->has('page') ? request()->input('page', 1) : null;
- $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
- if (!is_null($queryPage)) {
- $inputData['page'] = $queryPage;
- }
- if (!is_null($queryPageSize)) {
- $inputData['page_size'] = $queryPageSize;
- }
- $orders = [
- ['id', 'DESC']
- ];
- if (!isset($inputData['page_size']) || $inputData['page_size'] > 500) {
- $pageSize = CommonConstant::PAGE_SIZE;
- } else {
- $pageSize = $inputData['page_size'];
- }
- $page = format_page($inputData['page'] ?? 0);
- //默认上级代理
- if (empty($inputData['parent_uid'])) {
- $inputData['parent_uid'] = $accountInfo['uid'];
- } else {
- // 查询的判断搜否是自己代理下的
- $agentModel = new AgentModel();
- $agentUids = $agentModel->findAllChildUids($accountInfo['uid']);
- $agentUids = array_merge($agentUids, [$accountInfo['uid']]);
- if (!in_array($inputData['parent_uid'], $agentUids)) {
- return json_fail('无数据');
- }
- }
- /**
- * @var AgentRepository $agentRepo
- */
- $agentRepo = app(AgentRepository::class);
- $field = [
- 'uid',
- 'nick',
- 'username',
- 'parent_uid',
- 'commission_ratio',
- 'status',
- 'created_at',
- 'agent_level',
- 'deduction_switch',
- 'own_app_uids'
- ];
- $res = $agentRepo->pushCriteria(new AgentPagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize,
- $field, $page);
- return new AgentPagesResponse($res);
- }
- /**
- * 创建渠道
- *
- * @param AgentRepository $objRepo
- * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
- * @throws \App\Exceptions\ApiException
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function create(AgentRepository $objRepo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'username' => 'unique:agent,username|required|max:16',
- 'password' => 'required|min:6|max:32',
- 'two_password' => 'required|string',
- 'commission_ratio' => 'required|integer',
- 'own_app_uids' => 'required|string',
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- if ($inputData['password'] != $inputData['two_password']) {
- return json_fail('确认密码错误,请重新输入!');
- }
- $accountInfo = $this->getCurrentAgent();
- /**
- * @var ConfigRepoistory $confRepo
- */
- $confRepo = app(ConfigRepoistory::class);
- if (empty($accountInfo['parent_uids'])) {
- $parentUids = $accountInfo['uid'];
- } else {
- $parentUids = $accountInfo['parent_uids'] . ',' . $accountInfo['uid'];
- }
- $setData = [
- 'username' => $inputData['username'],
- 'nick' => $inputData['username'],
- 'password' => $inputData['password'],
- 'status' => 1,
- 'parent_uid' => $accountInfo['uid'],
- 'parent_uids' => $parentUids,
- 'withdraw_pwd' => $confRepo->get('withdraw_pwd', 'sys'),
- 'own_app_uids' => $inputData['own_app_uids'],
- 'commission_ratio' => $inputData['commission_ratio'],
- 'deduction_switch' => 0,
- 'jdata' => json_encode([])
- ];
- $agentObj = $objRepo->createOne($setData, $accountInfo['uid']);
- $respData = [
- 'uid' => $agentObj->uid,
- ];
- if (empty($accountInfo['child_uids'])) {
- $childUids = $agentObj->uid;
- } else {
- $childUids = $accountInfo['child_uids'] . ',' . $agentObj->uid;
- }
- //新增子集集合
- $objRepo->update(['child_uids' => $childUids], $accountInfo['uid'], 'uid');
- //创建渠道
- $chanRepo = app(ChannelRepository::class);
- $chanRepo->createAgentChannel($agentObj->uid);
- return new CommonSuccessUidResponse($respData);
- }
- /**
- * 账户信息修改
- *
- * @param AgentRepository $agentRepo
- * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
- * @throws \App\Exceptions\ApiException
- */
- public function modify(AgentRepository $agentRepo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'nick' => 'nullable|max:16',
- 'old_password' => 'nullable|string',
- 'password' => 'nullable|min:6|max:32',
- 'withdraw_pwd' => 'nullable|min:6|max:32',
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $accountInfo = $this->getCurrentAgent();
- if (!empty($inputData['password'])) {
- /**
- * @var ManagerRepository $managerRepo
- */
- $managerRepo = app(ManagerRepository::class);
- $md5password = $managerRepo->generatePassword($inputData['old_password'], $accountInfo['salt']);
- if ($md5password != $accountInfo['password']) {
- return json_fail('历史密码错误,请确认后再进行修改');
- }
- }
- $setData = [];
- if (!empty($inputData['nick'])) {
- $setData['nick'] = $inputData['nick'];
- }
- if (!empty($inputData['password'])) {
- $setData['password'] = $inputData['password'];
- }
- if (!empty($inputData['withdraw_pwd'])) {
- $setData['withdraw_pwd'] = $inputData['withdraw_pwd'];
- }
- $saveResult = $agentRepo->agentEdit($setData, $accountInfo['uid']);
- if (!$saveResult) {
- return json_fail('修改失败');
- }
- return new CommonSuccessUidResponse($accountInfo['uid']);
- }
- /**
- * 匹配用户名
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function checkUsername()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'username' => 'unique:agent,username|required|max:16',
- ];
- $message = [
- 'username.unique' => '账号已存在!',
- 'username.required' => '用户名不能为空!',
- 'username.max' => '用户名不能大于16字节!'
- ];
- $validator = Validator::make($inputData, $rules, $message);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- return json_fail('用户名可用!', 200, []);
- }
- /**
- * 我的详情.
- * @return mixed|AgentDetailResponse
- */
- public function detailMe()
- {
- $accountInfo = $this->getCurrentAgent();
- if (empty($accountInfo)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- return new AgentDetailResponse($accountInfo);
- }
- public function editLevelAgent()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'uid' => 'required',
- 'own_app_uids' => 'required',
- 'status' => 'required',
- 'commission_ratio' => 'required'
- ];
- $message = [
- 'uid.required' => 'UID出错!',
- 'own_app_uids.required' => '应用不能为空!',
- 'status.required' => '状态不能为空!',
- 'commission_ratio.required' => '分成比例不能为空!'
- ];
- $validator = Validator::make($inputData, $rules, $message);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $accountInfo = $this->getCurrentAgent();
- $agentModel = new AgentModel();
- // 查询修改的用户下级最高的分成比例
- $commission_ratio_min = $agentModel->where('parent_uid', $inputData['uid'])
- ->orderByDesc('commission_ratio')
- ->pluck('commission_ratio')->first();
- // 修改的不能小于他下级的
- if ($commission_ratio_min) {
- if ($inputData['commission_ratio'] < $commission_ratio_min) {
- return json_fail('比例不能小于他下级的!');
- }
- }
- if ($inputData['commission_ratio'] > $accountInfo->commission_ratio) {
- return json_fail('比例不能高出自己的!');
- }
- // 查询是否是自己
- $agent = $agentModel->where('uid', $inputData['uid'])->first();
- if ($agent->parent_uid !== $accountInfo->uid) {
- return json_fail('不属于一级代理!');
- }
- if (!empty($inputData['own_app_uids'])) {
- $agent->own_app_uids = $inputData['own_app_uids'];
- }
- if (!empty($inputData['status'])) {
- $agent->status = $inputData['status'];
- }
- if (!empty($inputData['commission_ratio'])) {
- $agent->commission_ratio = $inputData['commission_ratio'];
- }
- if (!$agent->save()) {
- return json_fail('更新失败!');
- }
- return json_success('更新成功!');
- }
- }
|