ProxyController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. <?php
  2. namespace App\Http\Controllers\Admins;
  3. use App\Modes\AdminArea;
  4. use App\Modes\Area;
  5. use App\Modes\Proxy;
  6. use App\Modes\ProxyInvite;
  7. use App\Modes\User;
  8. use Illuminate\Http\Request;
  9. use App\Http\Controllers\Controller;
  10. class ProxyController extends Controller
  11. {
  12. /**
  13. * 开放城区列表
  14. * @author lyh
  15. * @date 2019/4/15
  16. * @description
  17. * 显示:省 市 区/县
  18. * 超级管理员可查看并操作各开放区域,城区运行商,只可查看并操作所管辖的城区
  19. */
  20. public function index(Request $request)
  21. {
  22. $proxy = Proxy::select(['id', 'province', 'city', 'district', 'status','user_level'])
  23. ->where('province', '!=', 0)
  24. ->where('area_type',3);
  25. //管理员查看对应的代理
  26. $district=AdminArea::getAdminArea();
  27. if(!empty($district)){
  28. if(!empty($district['city'])){
  29. $proxy->whereCity($district['city']);
  30. }else{
  31. $proxy->whereDistrict($district['district']);
  32. }
  33. }
  34. $res=$proxy->groupBy(['province', 'city', 'district'])
  35. ->orderByDesc('created_at')
  36. ->paginate(perPage());
  37. if ($res->isNotEmpty()) {
  38. collect($res->items())->each(function ($item, $key) {
  39. $item->provinceMsg = Area::getName($item->province);
  40. $item->cityMsg = Area::getName($item->city);
  41. $item->districtMsg = Area::getName($item->district);
  42. });
  43. }
  44. return showJsonSucc(1001, $res);
  45. }
  46. /**
  47. * 添加代理区域
  48. * @author lyh
  49. * @date 2019/4/15
  50. * @param Request $request
  51. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  52. * @description
  53. */
  54. public function add(Request $request)
  55. {
  56. $validator = \Validator::make($param = $request->all(), [
  57. 'province' => 'required|exists:area,id',
  58. 'city' => 'required',
  59. 'district' => 'required',
  60. ]);
  61. if ($validator->fails()) {
  62. return showJsonErr($validator->errors()->first());
  63. }
  64. if (\Auth::user()->is_super != 1) {
  65. return showJsonErr('抱歉,您没有权限添加代理地区');
  66. }
  67. \DB::enableQueryLog();
  68. // 区域类型 0-所有区域 1-省 2-市 3-区 4-个人
  69. $areaType = 0;
  70. if (!empty($param['province'])) {
  71. $areaType = 1;
  72. }
  73. if (!empty($param['city'])) {
  74. $areaType = 2;
  75. }
  76. if (!empty($param['district'])) {
  77. $areaType = 3;
  78. }
  79. $proxy = Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereDistrict($param['district']);
  80. if ($proxy->exists()) {
  81. return showJsonErr('请勿重复添加该地区', $proxy->with('proxyInviteModel')->get());
  82. }
  83. // 获取全局配置
  84. $proxyConfig = Proxy::whereAreaType(0)->with('proxyInviteModel')->get();
  85. if ($proxyConfig->isEmpty()) {
  86. return showJsonErr('系统问题,请联系技术添加初始配置');
  87. }
  88. \DB::beginTransaction();
  89. try {
  90. // 添加配置
  91. collect($proxyConfig)->each(function ($pc, $pc_key) use ($areaType, $param) {
  92. //add by wsl start当前区域的G级代理费统一
  93. $apply_money=0;
  94. //更改所有属于当前市的G级城市代理金额
  95. $curproxy=Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereUserLevel(7)->first();
  96. if(!empty($curproxy)&&$pc_key==6){
  97. $apply_money=$curproxy->apply_money;
  98. }
  99. //add by wsl end
  100. $pc_id = Proxy::insertGetId([
  101. 'uid' => 0,
  102. 'user_level' => $pc->user_level,
  103. 'province' => $param['province'],
  104. 'city' => $param['city'],
  105. 'district' => $param['district'],
  106. 'area_type' => $areaType,//区域类型 0-所有区域 1-省 2-市 3-区 4-个人
  107. 'upgrade_money' => $pc->upgrade_money,
  108. 'upgrade_business_month' => $pc->upgrade_business_month,
  109. 'upgrade_business_year' => $pc->upgrade_business_year,
  110. 'upgrade_invite' => $pc->upgrade_invite,
  111. 'proxy_invite' => $pc->proxy_invite,
  112. 'proxy_invite_global' => $pc->proxy_invite_global,
  113. 'adver_invite' => $pc->adver_invite,
  114. 'adver_invite_global' => $pc->adver_invite_global,
  115. 'money' => $pc->money,
  116. 'min_money' => $pc->min_money,
  117. 'coin' => $pc->coin,
  118. 'apply_money'=>$apply_money
  119. ]);
  120. collect($pc->proxyInviteModel)->each(function ($pi, $pi_key) use ($pc_id) {
  121. ProxyInvite::insert([
  122. 'proxy_id' => $pc_id,
  123. 'title' => $pi->title,
  124. 'min' => $pi->min,
  125. 'max' => $pi->max,
  126. 'pct' => $pi->pct,
  127. 'type' => $pi->type,
  128. 'proxy_type' => $pi->proxy_type,
  129. 'position' => $pi->position
  130. ]);
  131. });
  132. });
  133. \DB::commit();
  134. return showJsonSucc('添加代理地区成功', $proxy->with('proxyInviteModel')->get());
  135. } catch (\Exception $exception) {
  136. \DB::rollBack();
  137. return showJsonErr($exception->getMessage());
  138. }
  139. }
  140. /**
  141. * 修改状态
  142. * @author lyh
  143. * @date 2019/4/15
  144. * @param Request $request
  145. * @description
  146. * 关闭后,用户将不能申请为该区域的代理(后台假删除)且已申请该城区代理的账号设为冻结状态
  147. */
  148. public function modifyStatus(Request $request)
  149. {
  150. $validator = \Validator::make($param = $request->all(), [
  151. 'id' => 'required|exists:proxy,id',
  152. ]);
  153. if ($validator->fails()) {
  154. return showJsonErr($validator->errors()->first());
  155. }
  156. $proxy = Proxy::whereId($param['id'])->where('area_type', '!=', 0)->first();
  157. if (empty($proxy)) {
  158. return showJsonErr('该地区不能修改');
  159. }
  160. \DB::beginTransaction();
  161. try {
  162. $status = $proxy->status == 1 ? 0 : 1;
  163. $userStatus = $status == 1 ?: 2;
  164. $proxyConfig = Proxy::whereProvince($proxy->province)->whereCity($proxy->city);
  165. $user = User::whereProvince($proxy->province)->whereCity($proxy->city);
  166. if (!empty($proxy->district)) {
  167. $user->whereDistrict($proxy->district);
  168. $proxyConfig->whereDistrict($proxy->district);
  169. }
  170. // 更改地区设置状态
  171. $proxyConfig->update(['status' => $status]);
  172. // 更改用户状态
  173. $user->update(['status' => $userStatus]);
  174. \DB::commit();
  175. return showJsonSucc('修改地区状态成功');
  176. } catch (\Exception $exception) {
  177. \DB::rollBack();
  178. return showJsonErr($exception->getMessage());
  179. }
  180. }
  181. /**
  182. * 显示开放地区信息
  183. * @author lyh
  184. * @date 2019/4/15
  185. * @param Request $request
  186. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  187. * @description
  188. */
  189. public function show(Request $request)
  190. {
  191. $validator = \Validator::make($param = $request->all(), [
  192. 'province' => 'required|exists:area,id',
  193. 'city' => 'required|exists:area,id',
  194. 'district' => 'required|integer',
  195. ]);
  196. if ($validator->fails()) {
  197. return showJsonErr($validator->errors()->first());
  198. }
  199. if (\Auth::user()->is_super == 3) {
  200. if (\Auth::user()->province != $param['province'] || \Auth::user()->city != $param['city'] || \Auth::user()->district != $param['district']) {
  201. return showJsonErr('抱歉,该地区信息您不能修改');
  202. }
  203. }
  204. $res = Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereDistrict($param['district'])->whereUid(0)->with('proxyInviteModel')->get();
  205. if (empty($res)) {
  206. return showJsonErr('当前地区未开放');
  207. }
  208. return showJsonSucc(1001, $res);
  209. }
  210. /**
  211. * 代理区设置-城区代理设置
  212. * @author lyh
  213. * @date 2019/4/16
  214. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  215. * @description
  216. */
  217. public function global()
  218. {
  219. $res = Proxy::whereAreaType(0)->with('proxyInviteModel')->get();
  220. return showJsonSucc(1001, $res);
  221. }
  222. /**
  223. * [global_set 区代理设置-获取全局设置]
  224. * @author lgs
  225. * @DateTime 2019-04-30T09:47:13+0800
  226. * @return [type] [全局设置的字段]
  227. */
  228. public function global_set(Request $request)
  229. {
  230. $validator = \Validator::make($param = $request->all(), [
  231. 'province' => 'required|integer',
  232. 'city' => 'required|integer',
  233. 'district' => 'required|integer',
  234. ]);
  235. if ($validator->fails()) {
  236. return showJsonErr($validator->errors()->first());
  237. }
  238. $f = Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereDistrict($param['district'])->whereUid(0)->where('user_level',6)->select('proxy_invite_global as f_proxy_invite_global','adver_invite_global as f_adver_invite_global','min_money','money','guarantee_min_money')->first()->toArray();//F级区县代理分佣、广告分佣
  239. $g = Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereDistrict($param['district'])->whereUid(0)->where('user_level',7)->select('proxy_invite_global as g_proxy_invite_global','adver_invite_global as g_adver_invite_global')->first()->toArray();//G级市代理分佣、广告分佣
  240. $res = array_merge($f,$g);
  241. return showJsonSucc(1001, $res);
  242. }
  243. /**
  244. * [modify_global 城区代理全局修改]
  245. * @author lgs
  246. * @DateTime 2019-04-30T11:19:39+0800
  247. * @return [type] [description]
  248. */
  249. public function modify_global(Request $request){
  250. $validator = \Validator::make($param = $request->all(), [
  251. 'f_proxy_invite_global' => 'required|numeric',
  252. 'f_adver_invite_global' => 'required|numeric',
  253. 'money' => 'required|numeric',
  254. 'min_money' => 'required|numeric',
  255. 'g_proxy_invite_global' => 'required|numeric',
  256. 'g_adver_invite_global' => 'required|numeric',
  257. 'province' => 'required|integer',
  258. 'city' => 'required|integer',
  259. 'district' => 'required|integer',
  260. ]);
  261. if ($validator->fails()) {
  262. return showJsonErr($validator->errors()->first());
  263. }
  264. \DB::beginTransaction();
  265. try {
  266. Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereDistrict($param['district'])->whereUid(0)->where('user_level',6)->update([
  267. 'proxy_invite_global' => $param['f_proxy_invite_global'],
  268. 'adver_invite_global' => $param['f_adver_invite_global'],
  269. ]);
  270. Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereDistrict($param['district'])->whereUid(0)->where('user_level',7)->update([
  271. 'proxy_invite_global' => $param['g_proxy_invite_global'],
  272. 'adver_invite_global' => $param['g_adver_invite_global'],
  273. ]);
  274. Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereDistrict($param['district'])->whereUid(0)->update([
  275. 'money' => $param['money'],
  276. 'min_money' => $param['min_money'],
  277. 'guarantee_min_money' => empty($param['guarantee_min_money'])?0:$param['guarantee_min_money'],
  278. ]);
  279. \DB::commit();
  280. return showJsonSucc('设置成功');
  281. } catch (\Exception $e) {
  282. \DB::rollBack();
  283. return showJsonErr($e->getMessage());
  284. }
  285. }
  286. /**
  287. * 修改信息
  288. * @author lyh
  289. * @date 2019/4/15
  290. * @param Request $request
  291. * @description
  292. */
  293. public function modify(Request $request)
  294. {
  295. $validator = \Validator::make($param = $request->all(), [
  296. 'id' => 'required|exists:proxy,id',
  297. 'upgrade_money' => 'required|numeric',
  298. 'upgrade_business_month' => 'required|numeric',
  299. 'upgrade_business_year' => 'required|numeric',
  300. 'upgrade_invite' => 'required|numeric',
  301. 'proxy_invite' => 'required|numeric',
  302. 'proxy_invite_global' => 'required|numeric',
  303. 'adver_invite' => 'required|numeric',
  304. 'adver_invite_global' => 'required|numeric',
  305. 'money' => 'required|numeric',
  306. 'min_money' => 'required|numeric',
  307. 'coin' => 'required|numeric',
  308. 'proxyInvite' => 'array',
  309. 'apply_money' => 'required|numeric', //申请区县代理费用
  310. ]);
  311. if ($validator->fails()) {
  312. return showJsonErr($validator->errors()->first());
  313. }
  314. // 然后根据省市区id和等级查询出符合条件的数据(单条)然后进行数据更新操作
  315. $proxyData = array_filter($param, function ($item, $key) {
  316. return in_array($key, [
  317. 'upgrade_money',
  318. 'upgrade_business_month',
  319. 'upgrade_business_year',
  320. 'upgrade_invite',
  321. 'proxy_invite',
  322. 'proxy_invite_global',
  323. 'adver_invite',
  324. 'adver_invite_global',
  325. 'money',
  326. 'min_money',
  327. 'coin',
  328. 'apply_money',
  329. 'protocol'
  330. ]);
  331. }, ARRAY_FILTER_USE_BOTH);
  332. \DB::beginTransaction();
  333. try {
  334. Proxy::whereId($param['id'])->update($proxyData);
  335. //addby wsl start
  336. // if(!empty($proxyData['apply_money'])&&$proxyData['apply_money']>0){
  337. // //更改所有属于当前市的G级城市代理金额
  338. // $curproxy=Proxy::whereId($param['id'])->first();
  339. // $proxyGarr=Proxy::whereProvince($curproxy->province)
  340. // ->whereCity($curproxy->city)->whereUserLevel(7)->get();
  341. // $parr=[];
  342. // if(!empty($proxyGarr)){
  343. // foreach ($proxyGarr as $gk=>$gv){
  344. // $parr[]=$gv->id;
  345. // }
  346. // }
  347. // if(!empty($parr))Proxy::whereIn('id',$parr)->update(['apply_money'=>$param['apply_money']]);
  348. // }
  349. //addby wsl end
  350. // 更新代理区域百分比信息
  351. foreach ($param['proxyInvite'] as $pi) {
  352. $proxyInvite = ProxyInvite::find($pi['id']);
  353. // if ($proxyInvite->position == 2 && ($pi['max'] < $pi['min'] || $pi['max'] < 0 || $pi['min'] < 0)) { // 区间范围
  354. // return showJsonErr('请输入正确的数据');
  355. // } else if ($proxyInvite->position == 1) { // 小于
  356. // $pi['max'] = 0;
  357. // } else if ($proxyInvite->position == 3) { // 大于
  358. // $pi['min'] = 0;
  359. // }else if ($proxyInvite->position == 4) {
  360. // $pi['max'] = 0;
  361. // }
  362. ProxyInvite::whereId($pi['id'])->update([
  363. 'pct' => $pi['pct'],
  364. 'min' => $pi['min'],
  365. 'max' => $pi['max'],
  366. ]);
  367. }
  368. \DB::commit();
  369. return showJsonSucc('设置成功');
  370. } catch (\Exception $e) {
  371. \DB::rollBack();
  372. return showJsonErr($e->getMessage());
  373. }
  374. }
  375. /**
  376. * 设置用户配置
  377. * @author lyh
  378. * @date 2019/4/9
  379. * @param Request $request
  380. * @description
  381. */
  382. public function setByUser(Request $request)
  383. {
  384. $validator = \Validator::make($param = $request->all(), [
  385. 'id' => 'required|exists:user,id',
  386. 'coin' => 'numeric',
  387. 'type' => 'required|integer|between:0,2', //0-其他设置 1-代理 2-广告
  388. 'proxy' => 'required_if:type,1,2',// 直推
  389. 'proxy_one' => 'required_if:type,1,2', // 间一
  390. 'proxy_one_left' => 'required_if:type,1,2', // 间一
  391. 'proxy_one_right' => 'required_if:type,1,2', // 间一
  392. 'proxy_two' => 'required_if:type,1,2', // 间二
  393. 'proxy_two_left' => 'required_if:type,1,2', // 间二
  394. 'proxy_two_right' => 'required_if:type,1,2', // 间二
  395. //'money' => 'numeric', // 广告计费金额
  396. //'min_money' => 'numeric', // 广告起投
  397. 'free_num' => 'integer', // 免费次数
  398. ]);
  399. if ($validator->fails()) {
  400. return showJsonErr($validator->errors()->first());
  401. }
  402. $user = User::find($param['id']);
  403. $proxyData = [];
  404. // 点币汇率
  405. if (isset($param['coin'])) {
  406. $proxyData['coin'] = $param['coin'];
  407. }
  408. // 广告每次/元
  409. if (!empty($param['money'])) {
  410. // $proxyData['money'] = $param['money'];
  411. }
  412. //广告发布最低额度
  413. if (!empty($param['min_money'])) {
  414. //$proxyData['min_money'] = $param['min_money'];
  415. }
  416. // 广告免费次数
  417. if (!empty($param['free_num'])) {
  418. $proxyData['free_num'] = $param['free_num'];
  419. }
  420. // 代理、广告直推
  421. if ($param['type'] == 2) {
  422. $proxyData['adver_invite'] = $param['proxy'];
  423. } else if ($param['type'] == 1) {
  424. $proxyData['proxy_invite'] = $param['proxy'];
  425. }
  426. \DB::beginTransaction();
  427. try {
  428. $proxy = Proxy::whereUid($param['id'])->whereUserLevel($user->level)->first();
  429. if ($proxy) {
  430. if (!!isset($proxyData)) {
  431. $result = Proxy::whereId($proxy->id)->update($proxyData);
  432. }
  433. $id = $proxy->id;
  434. } else {
  435. $proxyConfig = Proxy::getConfig($param['id']);
  436. $proxyOldData = [
  437. 'uid' => $param['id'],
  438. 'user_level' => $proxyConfig->user_level,
  439. 'province' => $proxyConfig->province,
  440. 'city' => $proxyConfig->city,
  441. 'district' => $proxyConfig->district, //区域类型 0-所有区域 1-省 2-市 3-区 4-个人
  442. 'area_type' => 4,
  443. 'upgrade_money' => $proxyConfig->upgrade_money,
  444. 'upgrade_business_month' => $proxyConfig->upgrade_business_month,
  445. 'upgrade_business_year' => $proxyConfig->upgrade_business_year,
  446. 'upgrade_invite' => $proxyConfig->upgrade_invite,
  447. 'proxy_invite' => $proxyConfig->proxy_invite,
  448. 'proxy_invite_global' => $proxyConfig->proxy_invite_global,
  449. 'adver_invite' => $proxyConfig->adver_invite,
  450. 'adver_invite_global' => $proxyConfig->adver_invite_global,
  451. 'money' => $proxyConfig->money,
  452. 'min_money' => $proxyConfig->min_money,
  453. 'coin' => $proxyConfig->coin,
  454. 'free_num' => 0, // 广告免费次数不继承
  455. ];
  456. $proxyData = array_merge($proxyOldData, $proxyData);
  457. $result = $id = Proxy::insertGetId($proxyData);
  458. // 将广告/代理的直推/间一/间二的数据迁移成为独立的一份
  459. $proxyInvite = $proxyConfig->invite;
  460. if (empty($proxyInvite)) {
  461. \DB::rollBack();
  462. return showJsonErr('比例配置为空');
  463. }
  464. foreach ($proxyInvite as $item) {
  465. $PIresult = ProxyInvite::insert([
  466. 'proxy_id' => $id,
  467. 'title' => $item->title,
  468. 'min' => $item->min,
  469. 'max' => $item->max,
  470. 'pct' => $item->pct,
  471. 'type' => $item->type,
  472. 'proxy_type' => $item->type,
  473. 'position' => $item->position
  474. ]);
  475. if (!$PIresult) {
  476. \DB::rollBack();
  477. return showJsonErr('迁移比例配置失败');
  478. }
  479. }
  480. User::whereId($param['id'])->update(['commission_status' => 2]);
  481. }
  482. // 1-代理 2-广告 比例设置,则更新比例
  483. if (in_array($param['type'], [1, 2])) {
  484. // 间推一
  485. if ($param['proxy_one_left']) {
  486. ProxyInvite::whereProxyId($id)->where('position', '=', 1)->whereType(1)->update(['pct' => $param['proxy_one_left']]);
  487. }
  488. if ($param['proxy_one']) {
  489. ProxyInvite::whereProxyId($id)->where('position', '=', 2)->whereType(1)->update(['pct' => $param['proxy_one']]);
  490. }
  491. if ($param['proxy_one_right']) {
  492. ProxyInvite::whereProxyId($id)->where('position', '=', 3)->whereType(1)->update(['pct' => $param['proxy_one_right']]);
  493. }
  494. // 间推二
  495. if ($param['proxy_one_left']) {
  496. ProxyInvite::whereProxyId($id)->where('position', '=', 1)->whereType(2)->update(['pct' => $param['proxy_one_left']]);
  497. }
  498. if ($param['proxy_one']) {
  499. ProxyInvite::whereProxyId($id)->where('position', '=', 2)->whereType(2)->update(['pct' => $param['proxy_one']]);
  500. }
  501. if ($param['proxy_one_right']) {
  502. ProxyInvite::whereProxyId($id)->where('position', '=', 3)->whereType(2)->update(['pct' => $param['proxy_one_right']]);
  503. }
  504. }
  505. if (empty($result)) {
  506. \DB::rollBack();
  507. return showJsonErr('设置信息失败');
  508. }
  509. \DB::commit();
  510. return showJsonSucc('设置信息成功', Proxy::getConfig($param['id']));
  511. } catch (\Exception $exception) {
  512. \DB::rollBack();
  513. return showJsonErr($exception->getMessage());
  514. }
  515. }
  516. }