HomeController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Modes\AccountLog;
  4. use App\Modes\BusinessGoal;
  5. use App\Modes\Config;
  6. use App\Modes\Proxy;
  7. use App\Modes\Slider;
  8. use App\Modes\User;
  9. use App\Modes\Order;
  10. use App\Rules\Phone;
  11. use Carbon\Carbon;
  12. use Encore\Admin\Form\Field\Date;
  13. use Illuminate\Http\Request;
  14. use App\Http\Controllers\Controller;
  15. class HomeController extends Controller
  16. {
  17. /**
  18. * 获取收益
  19. * @author lyh
  20. * @date 2019/3/27
  21. * @param $uid
  22. * @param $start_time
  23. * @param $end_timel
  24. * @return mixed
  25. * @description
  26. * * 'uv' => $resItem['uv'], // pv代理收益次数
  27. * 'earnings' => $resItem['earnings'],// 总收益
  28. * 'inviteNum' => $resItem['proxyNum'], // 下级代理数量
  29. * 'proxyNum' => $resItem['proxyNum'], // 代理数量
  30. * 'proxyMoney' => $resItem['proxyMoney'], // 代理收益
  31. * 'adverNum' => $resItem['adverNum'], // 广告数量
  32. * 'adverMoney' => $resItem['adverMoney'] // 广告收益
  33. */
  34. private static function getEarnings($uid, $start_time, $end_time)
  35. {
  36. if (\Cache::has('getEarnings' . $start_time)) {
  37. return \Cache::get('getEarnings' . $start_time);
  38. }
  39. $type = [10, 11, 12, 13, 20, 21, 22, 23];
  40. \DB::connection()->enableQueryLog();
  41. $ids = User::getAllInvite($uid, null, $end_time, false);
  42. $inviteNum = count($ids);
  43. $uv = AccountLog::getUv($ids, $type, $start_time, $end_time);
  44. $earnings = AccountLog::getEarngings($ids, $type, $start_time, $end_time);
  45. $proxyNum = AccountLog::getInviteUv($ids, [20, 21, 22, 23], $start_time, $end_time, $ids);
  46. $proxyMoney = AccountLog::getEarngings($ids, [20, 21, 22, 23], $start_time, $end_time);
  47. $adverNum = AccountLog::getInviteUv($ids, [10, 11, 12, 13], $start_time, $end_time, $ids);
  48. $adverMoney = AccountLog::getEarngings($ids, [10, 11, 12, 13], $start_time, $end_time);
  49. $res = [
  50. 'uv' => $uv,
  51. 'earnings' => $earnings,
  52. 'inviteNum' => $inviteNum,
  53. 'proxyNum' => $proxyNum,
  54. 'proxyMoney' => $proxyMoney,
  55. 'adverNum' => $adverNum,
  56. 'adverMoney' => $adverMoney,
  57. 'uid' => $ids
  58. ];
  59. if (strtotime($start_time) + 86400 * 31 < time()) {
  60. \Cache::put('getEarnings' . $start_time, $res, 3600);
  61. }
  62. return $res;
  63. }
  64. public function getSms(Request $request)
  65. {
  66. $validator = \Validator::make($param = $request->post(), [
  67. 'mobile' => [
  68. 'required',
  69. // 'string',
  70. 'regex:/^[1][3,4,5,6,7,8,9][0-9]{9}$/',
  71. new Phone()
  72. ],
  73. 'type' => 'required|between:1,10'
  74. ]);
  75. if ($validator->fails()) {
  76. return showJson(102, $validator->errors()->first());
  77. }
  78. //检测用户的mobile是否存在
  79. if(!empty($param['send_type'])&&$param['send_type']>0){
  80. $user = User::whereMobile($param['mobile'])->first();
  81. if(empty($user) && $param['send_type']==1)return showJson('102','用户不存在,请扫码注册后再登录');
  82. if(empty($user) && $param['send_type']==2)return showJson('102','手机号不存在,请重新输入');
  83. }
  84. // var_dump('6666666666');exit;
  85. $smsProvider = \App::make('sms');
  86. $sms = $smsProvider->getSms($param['mobile'], 0);
  87. if ($sms == false) {
  88. return showJson(102, '获取短信验证码失败');
  89. }
  90. return showJsonSucc('获取验证码成功,请注意查收', [
  91. // 'sms' => $sms,
  92. ]);
  93. }
  94. /**
  95. * 收益记录
  96. * @author lyh
  97. * @date 2019/3/26
  98. * @description
  99. * 代理收益次数
  100. * 总收益
  101. * 下级代理数量
  102. * N 个 带来代理佣金总额
  103. * N 个 带来广告佣金收益总额
  104. */
  105. public function statistics(Request $request)
  106. {
  107. $res = [];
  108. $year = $request->post('year', 0);
  109. if ($year < 2018 && $year > 2040) {
  110. return showJsonErr('请选择查询的年份');
  111. }
  112. $month = $request->post('month', '');
  113. $day = $request->post('day', '');
  114. if (!empty($day)) {
  115. $time = [
  116. 'date' => "{$year}-{$month}-{$day}",
  117. 'start_time' => Carbon::create($year, $month, $day)->startOfDay(),
  118. 'end_time' => Carbon::create($year, $month, $day)->endOfDay()
  119. ];
  120. //获取下级总人数
  121. $chidsarr=User::getAllChilds(\Auth::id(),$time['end_time']);
  122. $resItem=AccountLog::getShouYi($chidsarr,$time['start_time'],$time['end_time']);
  123. $resItem['month']=$time['date'];
  124. $res[]=$resItem;
  125. return showJsonSucc(1001, $res);
  126. }
  127. //月
  128. if (!empty($month)) {
  129. $time = [
  130. 'date' => "{$year}-{$month}",
  131. 'start_time' => Carbon::create($year, $month)->firstOfMonth(),
  132. 'end_time' => Carbon::create($year, $month)->endOfMonth()
  133. ];
  134. //获取下级总人数
  135. $chidsarr=User::getAllChilds(\Auth::id(),$time['end_time']);
  136. $resItem=AccountLog::getShouYi($chidsarr,$time['start_time'],$time['end_time']);
  137. $resItem['month']=$time['date'];
  138. $res[]=$resItem;
  139. return showJsonSucc(1001, $res);
  140. }
  141. //年
  142. $time = [];
  143. $monthcoun=intval(date('m'));
  144. $curyear=intval(date('Y'));
  145. if($year<$curyear)$monthcoun=12;
  146. for ($i = 1; $i <= $monthcoun; $i++) {
  147. $time[] = [
  148. 'date' => "{$year}-{$i}",
  149. 'start_time' => Carbon::create($year, $i)->firstOfMonth(),
  150. 'end_time' => Carbon::create($year, $i)->subMonth(-1)->firstOfMonth()->subSecond(1)
  151. ];
  152. }
  153. //年
  154. foreach ($time as $item) {
  155. //获取下级总人数
  156. //$item['end_time']=date('Y-m-d H:i:S');
  157. //$item['start_time']='2019-07-01';
  158. $chidsarr=User::getAllChilds(\Auth::id(),$item['end_time']);
  159. $resItem=AccountLog::getShouYi($chidsarr,$item['start_time'],$item['end_time']);
  160. $resItem['month']=Carbon::instance($item['start_time'])->month;
  161. $res[]=$resItem;
  162. }
  163. return showJsonSucc(1001, $res);
  164. }
  165. /**
  166. * 任务进度轴
  167. * @author lyh
  168. * @date 2019/3/26
  169. * @description
  170. * edit by wsl
  171. * 20190628
  172. *
  173. */
  174. public function progress()
  175. {
  176. $goal = BusinessGoal::whereUid(\Auth::id())->orderByDesc('id')->paginate(perPage());
  177. //根据用户等级找到相关配置
  178. $userinfo=User::whereId(\Auth::id())->first()->toArray();
  179. if($userinfo['level']==7){
  180. $levels=7;
  181. }else $levels=$userinfo['level']+1;
  182. $setting=Proxy::getSettingByAreaAndLevel($levels,$userinfo['province'],$userinfo['city'],$userinfo['district']);
  183. //计算当月达到的任务量
  184. $tasks=$this->getCurTask($userinfo['id']);
  185. $goal = BusinessGoal::whereUid(\Auth::id())->orderByDesc('id')->paginate(perPage());
  186. $month = [];
  187. if ($goal->currentPage() == 1) {
  188. $month = [
  189. 'time' => date('Y-m'),
  190. 'goal' => $setting['upgrade_business_month'],
  191. 'actual' => round($tasks['month'],3),
  192. ];
  193. }
  194. return showJsonSucc(1001, [
  195. 'goal' => $setting['upgrade_business_year'], // 当前年度目标业绩
  196. 'actual' => round($tasks['year'],3),
  197. 'currentMonth' => $month,
  198. 'month' => $goal
  199. ]);
  200. }
  201. public function slider(){
  202. $data = Slider::orderBy('sort','desc')->get();
  203. collect($data)->each(function ($item, $key) {
  204. $item->img = \Storage::disk('api')->url($item->img);
  205. // $item->img = $item->url;
  206. });
  207. return showJsonSucc(1001, $data);
  208. }
  209. //计算年和月的任务总量,addbywsl,20190628
  210. function getCurTask($uid){
  211. $childarr=User::where('invitor','=',$uid)->get(['id']);
  212. $childarr=$childarr->toArray();
  213. $arr=[];
  214. if(!empty($childarr)){
  215. foreach ($childarr as $ke=>$ve){
  216. $arr[]=$ve['id'];
  217. }
  218. }
  219. // dd($arr);
  220. $montstat=Order::getMonthOrYearStat($uid,$arr,'month');
  221. $yearstat=Order::getMonthOrYearStat($uid,$arr,'year');
  222. return ['month'=>$montstat,'year'=>$yearstat];
  223. }
  224. }