UserController.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\ServerChan;
  4. use App\Http\Models\Article;
  5. use App\Http\Models\Coupon;
  6. use App\Http\Models\CouponLog;
  7. use App\Http\Models\Goods;
  8. use App\Http\Models\GoodsLabel;
  9. use App\Http\Models\Invite;
  10. use App\Http\Models\Level;
  11. use App\Http\Models\Order;
  12. use App\Http\Models\ReferralApply;
  13. use App\Http\Models\ReferralLog;
  14. use App\Http\Models\SsConfig;
  15. use App\Http\Models\SsGroup;
  16. use App\Http\Models\SsNodeInfo;
  17. use App\Http\Models\Ticket;
  18. use App\Http\Models\TicketReply;
  19. use App\Http\Models\User;
  20. use App\Http\Models\UserBalanceLog;
  21. use App\Http\Models\UserLabel;
  22. use App\Http\Models\UserScoreLog;
  23. use App\Http\Models\UserSubscribe;
  24. use App\Http\Models\UserTrafficDaily;
  25. use App\Http\Models\UserTrafficHourly;
  26. use App\Http\Models\Verify;
  27. use App\Mail\activeUser;
  28. use App\Mail\newTicket;
  29. use App\Mail\replyTicket;
  30. use App\Mail\resetPassword;
  31. use Illuminate\Http\Request;
  32. use Redirect;
  33. use Response;
  34. use Session;
  35. use Cache;
  36. use Mail;
  37. use Log;
  38. use DB;
  39. class UserController extends Controller
  40. {
  41. public function index(Request $request)
  42. {
  43. $user = Session::get('user');
  44. $user = User::query()->where('id', $user['id'])->first();
  45. $user->totalTransfer = flowAutoShow($user->transfer_enable);
  46. $user->usedTransfer = flowAutoShow($user->u + $user->d);
  47. $user->usedPercent = $user->transfer_enable > 0 ? round(($user->u + $user->d) / $user->transfer_enable, 2) : 1;
  48. $user->levelName = Level::query()->where('level', $user['level'])->first()['level_name'];
  49. $view['info'] = $user->toArray();
  50. $view['notice'] = Article::query()->where('type', 2)->where('is_del', 0)->orderBy('id', 'desc')->first();
  51. $view['articleList'] = Article::query()->where('type', 1)->where('is_del', 0)->orderBy('sort', 'desc')->orderBy('id', 'desc')->paginate(5);
  52. $view['wechat_qrcode'] = $this->systemConfig['wechat_qrcode'];
  53. $view['alipay_qrcode'] = $this->systemConfig['alipay_qrcode'];
  54. $view['login_add_score'] = $this->systemConfig['login_add_score'];
  55. $view['website_logo'] = $this->systemConfig['website_logo'];
  56. $view['website_analytics'] = $this->systemConfig['website_analytics'];
  57. $view['website_customer_service'] = $this->systemConfig['website_customer_service'];
  58. $view['is_push_bear'] = $this->systemConfig['is_push_bear'];
  59. $view['push_bear_qrcode'] = $this->systemConfig['push_bear_qrcode'];
  60. // 推广返利是否可见
  61. if (!Session::has('referral_status')) {
  62. Session::put('referral_status', $this->systemConfig['referral_status']);
  63. }
  64. // 节点列表
  65. $userLabelIds = UserLabel::query()->where('user_id', $user['id'])->pluck('label_id');
  66. if (empty($userLabelIds)) {
  67. $view['nodeList'] = [];
  68. return Response::view('user/index', $view);
  69. }
  70. $nodeList = DB::table('ss_node')
  71. ->leftJoin('ss_node_label', 'ss_node.id', '=', 'ss_node_label.node_id')
  72. ->whereIn('ss_node_label.label_id', $userLabelIds)
  73. ->where('ss_node.status', 1)
  74. ->groupBy('ss_node.id')
  75. ->orderBy('sort', 'desc')
  76. ->get();
  77. foreach ($nodeList as &$node) {
  78. // 获取分组名称
  79. $group = SsGroup::query()->where('id', $node->group_id)->first();
  80. // 生成ssr scheme
  81. $obfs_param = $user->obfs_param ? $user->obfs_param : $node->obfs_param;
  82. $protocol_param = $node->single ? $user->port . ':' . $user->passwd : $user->protocol_param;
  83. $ssr_str = '';
  84. $ssr_str .= ($node->server ? $node->server : $node->ip) . ':' . ($node->single ? $node->single_port : $user->port);
  85. $ssr_str .= ':' . ($node->single ? $node->single_protocol : $user->protocol) . ':' . ($node->single ? $node->single_method : $user->method);
  86. $ssr_str .= ':' . ($node->single ? $node->single_obfs : $user->obfs) . ':' . ($node->single ? base64url_encode($node->single_passwd) : base64url_encode($user->passwd));
  87. $ssr_str .= '/?obfsparam=' . base64url_encode($obfs_param);
  88. $ssr_str .= '&protoparam=' . ($node->single ? base64url_encode($user->port . ':' . $user->passwd) : base64url_encode($protocol_param));
  89. $ssr_str .= '&remarks=' . base64url_encode($node->name);
  90. $ssr_str .= '&group=' . base64url_encode(empty($group) ? '' : $group->name);
  91. $ssr_str .= '&udpport=0';
  92. $ssr_str .= '&uot=0';
  93. $ssr_str = base64url_encode($ssr_str);
  94. $ssr_scheme = 'ssr://' . $ssr_str;
  95. // 生成ss scheme
  96. $ss_str = '';
  97. $ss_str .= $user->method . ':' . $user->passwd . '@';
  98. $ss_str .= $node->server . ':' . $user->port;
  99. $ss_str = base64url_encode($ss_str) . '#' . 'VPN';
  100. $ss_scheme = 'ss://' . $ss_str;
  101. // 生成文本配置信息
  102. $txt = "服务器:" . ($node->server ? $node->server : $node->ip) . "\r\n";
  103. if ($node->ipv6) {
  104. $txt .= "IPv6:" . $node->ipv6 . "\r\n";
  105. }
  106. $txt .= "远程端口:" . ($node->single ? $node->single_port : $user->port) . "\r\n";
  107. $txt .= "密码:" . ($node->single ? $node->single_passwd : $user->passwd) . "\r\n";
  108. $txt .= "加密方法:" . ($node->single ? $node->single_method : $user->method) . "\r\n";
  109. $txt .= "协议:" . ($node->single ? $node->single_protocol : $user->protocol) . "\r\n";
  110. $txt .= "协议参数:" . ($node->single ? $user->port . ':' . $user->passwd : $user->protocol_param) . "\r\n";
  111. $txt .= "混淆方式:" . ($node->single ? $node->single_obfs : $user->obfs) . "\r\n";
  112. $txt .= "混淆参数:" . ($user->obfs_param ? $user->obfs_param : $node->obfs_param) . "\r\n";
  113. $txt .= "本地端口:1080\r\n路由:绕过局域网及中国大陆地址";
  114. $node->txt = $txt;
  115. $node->ssr_scheme = $ssr_scheme;
  116. $node->ss_scheme = $node->compatible ? $ss_scheme : ''; // 节点兼容原版才显示
  117. // 节点在线状态
  118. $nodeInfo = SsNodeInfo::query()->where('node_id', $node->node_id)->where('log_time', '>=', strtotime("-10 minutes"))->orderBy('id', 'desc')->first();
  119. $node->online_status = empty($nodeInfo) || empty($nodeInfo->load) ? 0 : 1;
  120. }
  121. $view['nodeList'] = $nodeList;
  122. return Response::view('user/index', $view);
  123. }
  124. // 公告详情
  125. public function article(Request $request)
  126. {
  127. $id = $request->get('id');
  128. $view['info'] = Article::query()->where('is_del', 0)->where('id', $id)->first();
  129. if (empty($view['info'])) {
  130. return Redirect::to('user');
  131. }
  132. $view['website_logo'] = $this->systemConfig['website_logo'];
  133. $view['website_analytics'] = $this->systemConfig['website_analytics'];
  134. $view['website_customer_service'] = $this->systemConfig['website_customer_service'];
  135. return Response::view('user/article', $view);
  136. }
  137. // 修改个人资料
  138. public function profile(Request $request)
  139. {
  140. $user = Session::get('user');
  141. if ($request->method() == 'POST') {
  142. $old_password = $request->get('old_password');
  143. $new_password = $request->get('new_password');
  144. $wechat = $request->get('wechat');
  145. $qq = $request->get('qq');
  146. $passwd = trim($request->get('passwd'));
  147. $method = $request->get('method');
  148. $protocol = $request->get('protocol');
  149. $obfs = $request->get('obfs');
  150. // 修改密码
  151. if ($old_password && $new_password) {
  152. $old_password = md5(trim($old_password));
  153. $new_password = md5(trim($new_password));
  154. $user = User::query()->where('id', $user['id'])->first();
  155. if ($user->password != $old_password) {
  156. Session::flash('errorMsg', '旧密码错误,请重新输入');
  157. return Redirect::to('user/profile#tab_1');
  158. } else if ($user->password == $new_password) {
  159. Session::flash('errorMsg', '新密码不可与旧密码一样,请重新输入');
  160. return Redirect::to('user/profile#tab_1');
  161. }
  162. $ret = User::query()->where('id', $user['id'])->update(['password' => $new_password]);
  163. if (!$ret) {
  164. Session::flash('errorMsg', '修改失败');
  165. return Redirect::to('user/profile#tab_1');
  166. } else {
  167. Session::flash('successMsg', '修改成功');
  168. return Redirect::to('user/profile#tab_1');
  169. }
  170. }
  171. // 修改联系方式
  172. if ($wechat || $qq) {
  173. if (empty(clean($wechat)) && empty(clean($qq))) {
  174. Session::flash('errorMsg', '修改失败');
  175. return Redirect::to('user/profile#tab_2');
  176. }
  177. $ret = User::query()->where('id', $user['id'])->update(['wechat' => $wechat, 'qq' => $qq]);
  178. if (!$ret) {
  179. Session::flash('errorMsg', '修改失败');
  180. return Redirect::to('user/profile#tab_2');
  181. } else {
  182. Session::flash('successMsg', '修改成功');
  183. return Redirect::to('user/profile#tab_2');
  184. }
  185. }
  186. // 修改SSR(R)设置
  187. if ($method || $protocol || $obfs) {
  188. if (empty($passwd)) {
  189. Session::flash('errorMsg', '密码不能为空');
  190. return Redirect::to('user/profile#tab_3');
  191. }
  192. // 加密方式、协议、混淆必须存在
  193. $existMethod = SsConfig::query()->where('type', 1)->where('name', $method)->first();
  194. $existProtocol = SsConfig::query()->where('type', 2)->where('name', $protocol)->first();
  195. $existObfs = SsConfig::query()->where('type', 3)->where('name', $obfs)->first();
  196. if (!$existMethod || !$existProtocol || !$existObfs) {
  197. Session::flash('errorMsg', '非法请求');
  198. return Redirect::to('user/profile#tab_3');
  199. }
  200. $data = [
  201. 'passwd' => $passwd,
  202. 'method' => $method,
  203. 'protocol' => $protocol,
  204. 'obfs' => $obfs
  205. ];
  206. $ret = User::query()->where('id', $user['id'])->update($data);
  207. if (!$ret) {
  208. Session::flash('errorMsg', '修改失败');
  209. return Redirect::to('user/profile#tab_3');
  210. } else {
  211. // 更新session
  212. $user = User::query()->where('id', $user['id'])->first()->toArray();
  213. Session::remove('user');
  214. Session::put('user', $user);
  215. Session::flash('successMsg', '修改成功');
  216. return Redirect::to('user/profile#tab_3');
  217. }
  218. }
  219. } else {
  220. // 加密方式、协议、混淆
  221. $view['method_list'] = $this->methodList();
  222. $view['protocol_list'] = $this->protocolList();
  223. $view['obfs_list'] = $this->obfsList();
  224. $view['info'] = User::query()->where('id', $user['id'])->first();
  225. $view['website_logo'] = $this->systemConfig['website_logo'];
  226. $view['website_analytics'] = $this->systemConfig['website_analytics'];
  227. $view['website_customer_service'] = $this->systemConfig['website_customer_service'];
  228. return Response::view('user/profile', $view);
  229. }
  230. }
  231. // 流量日志
  232. public function trafficLog(Request $request)
  233. {
  234. $user = Session::get('user');
  235. $dailyData = [];
  236. $hourlyData = [];
  237. // 节点一个月内的流量
  238. $userTrafficDaily = UserTrafficDaily::query()->where('user_id', $user['id'])->where('node_id', 0)->where('created_at', '>=', date('Y-m', time()))->orderBy('created_at', 'asc')->pluck('total')->toArray();
  239. $dailyTotal = date('d', time()) - 1; // 今天不算,减一
  240. $dailyCount = count($userTrafficDaily);
  241. for ($x = 0; $x < ($dailyTotal - $dailyCount); $x++) {
  242. $dailyData[$x] = 0;
  243. }
  244. for ($x = ($dailyTotal - $dailyCount); $x < $dailyTotal; $x++) {
  245. $dailyData[$x] = round($userTrafficDaily[$x - ($dailyTotal - $dailyCount)] / (1024 * 1024 * 1024), 3);
  246. }
  247. // 节点一天内的流量
  248. $userTrafficHourly = UserTrafficHourly::query()->where('user_id', $user['id'])->where('node_id', 0)->where('created_at', '>=', date('Y-m-d', time()))->orderBy('created_at', 'asc')->pluck('total')->toArray();
  249. $hourlyTotal = date('H', time());
  250. $hourlyCount = count($userTrafficHourly);
  251. for ($x = 0; $x < ($hourlyTotal - $hourlyCount); $x++) {
  252. $hourlyData[$x] = 0;
  253. }
  254. for ($x = ($hourlyTotal - $hourlyCount); $x < $hourlyTotal; $x++) {
  255. $hourlyData[$x] = round($userTrafficHourly[$x - ($hourlyTotal - $hourlyCount)] / (1024 * 1024 * 1024), 3);
  256. }
  257. $view['trafficDaily'] = "'" . implode("','", $dailyData) . "'";
  258. $view['trafficHourly'] = "'" . implode("','", $hourlyData) . "'";
  259. $view['website_logo'] = $this->systemConfig['website_logo'];
  260. $view['website_analytics'] = $this->systemConfig['website_analytics'];
  261. $view['website_customer_service'] = $this->systemConfig['website_customer_service'];
  262. return Response::view('user/trafficLog', $view);
  263. }
  264. // 商品列表
  265. public function goodsList(Request $request)
  266. {
  267. $goodsList = Goods::query()->where('status', 1)->where('is_del', 0)->orderBy('type', 'desc')->paginate(10)->appends($request->except('page'));
  268. foreach ($goodsList as $goods) {
  269. $goods->traffic = flowAutoShow($goods->traffic * 1048576);
  270. }
  271. $view['goodsList'] = $goodsList;
  272. $view['website_logo'] = $this->systemConfig['website_logo'];
  273. $view['website_analytics'] = $this->systemConfig['website_analytics'];
  274. $view['website_customer_service'] = $this->systemConfig['website_customer_service'];
  275. return Response::view('user/goodsList', $view);
  276. }
  277. // 工单
  278. public function ticketList(Request $request)
  279. {
  280. $user = Session::get('user');
  281. $view['website_logo'] = $this->systemConfig['website_logo'];
  282. $view['website_analytics'] = $this->systemConfig['website_analytics'];
  283. $view['website_customer_service'] = $this->systemConfig['website_customer_service'];
  284. $view['ticketList'] = Ticket::query()->where('user_id', $user['id'])->paginate(10)->appends($request->except('page'));
  285. return Response::view('user/ticketList', $view);
  286. }
  287. // 订单
  288. public function orderList(Request $request)
  289. {
  290. $user = Session::get('user');
  291. $view['orderList'] = Order::query()->with(['user', 'goods', 'coupon', 'payment'])->where('user_id', $user['id'])->orderBy('oid', 'desc')->paginate(10)->appends($request->except('page'));
  292. $view['website_logo'] = $this->systemConfig['website_logo'];
  293. $view['website_analytics'] = $this->systemConfig['website_analytics'];
  294. $view['website_customer_service'] = $this->systemConfig['website_customer_service'];
  295. return Response::view('user/orderList', $view);
  296. }
  297. // 添加工单
  298. public function addTicket(Request $request)
  299. {
  300. $title = $request->get('title');
  301. $content = clean($request->get('content'));
  302. $user = Session::get('user');
  303. if (empty($title) || empty($content)) {
  304. return Response::json(['status' => 'fail', 'data' => '', 'message' => '请输入标题和内容']);
  305. }
  306. $obj = new Ticket();
  307. $obj->user_id = $user['id'];
  308. $obj->title = $title;
  309. $obj->content = $content;
  310. $obj->status = 0;
  311. $obj->created_at = date('Y-m-d H:i:s');
  312. $obj->save();
  313. if ($obj->id) {
  314. $emailTitle = "新工单提醒";
  315. $content = "标题:【" . $title . "】<br>内容:" . $content;
  316. // 发邮件通知管理员
  317. try {
  318. if ($this->systemConfig['crash_warning_email']) {
  319. Mail::to($this->systemConfig['crash_warning_email'])->send(new newTicket($this->systemConfig['website_name'], $emailTitle, $content));
  320. $this->sendEmailLog(1, $emailTitle, $content);
  321. }
  322. } catch (\Exception $e) {
  323. $this->sendEmailLog(1, $emailTitle, $content, 0, $e->getMessage());
  324. }
  325. // 通过ServerChan发微信消息提醒管理员
  326. if ($this->systemConfig['is_server_chan'] && $this->systemConfig['server_chan_key']) {
  327. $serverChan = new ServerChan();
  328. $serverChan->send($emailTitle, $content);
  329. }
  330. return Response::json(['status' => 'success', 'data' => '', 'message' => '提交成功']);
  331. } else {
  332. return Response::json(['status' => 'fail', 'data' => '', 'message' => '提交失败']);
  333. }
  334. }
  335. // 回复工单
  336. public function replyTicket(Request $request)
  337. {
  338. $id = intval($request->get('id'));
  339. $user = Session::get('user');
  340. if ($request->method() == 'POST') {
  341. $content = clean($request->get('content'));
  342. $obj = new TicketReply();
  343. $obj->ticket_id = $id;
  344. $obj->user_id = $user['id'];
  345. $obj->content = $content;
  346. $obj->created_at = date('Y-m-d H:i:s');
  347. $obj->save();
  348. if ($obj->id) {
  349. $ticket = Ticket::query()->where('id', $id)->first();
  350. $title = "工单回复提醒";
  351. $content = "标题:【" . $ticket->title . "】<br>用户回复:" . $content;
  352. // 发邮件通知管理员
  353. try {
  354. if ($this->systemConfig['crash_warning_email']) {
  355. Mail::to($this->systemConfig['crash_warning_email'])->send(new replyTicket($this->systemConfig['website_name'], $title, $content));
  356. $this->sendEmailLog(1, $title, $content);
  357. }
  358. } catch (\Exception $e) {
  359. $this->sendEmailLog(1, $title, $content, 0, $e->getMessage());
  360. }
  361. // 通过ServerChan发微信消息提醒管理员
  362. if ($this->systemConfig['is_server_chan'] && $this->systemConfig['server_chan_key']) {
  363. $serverChan = new ServerChan();
  364. $serverChan->send($title, $content);
  365. }
  366. return Response::json(['status' => 'success', 'data' => '', 'message' => '回复成功']);
  367. } else {
  368. return Response::json(['status' => 'fail', 'data' => '', 'message' => '回复失败']);
  369. }
  370. } else {
  371. $ticket = Ticket::query()->where('id', $id)->with('user')->first();
  372. if (empty($ticket) || $ticket->user_id != $user['id']) {
  373. return Redirect::to('user/ticketList');
  374. }
  375. $view['ticket'] = $ticket;
  376. $view['replyList'] = TicketReply::query()->where('ticket_id', $id)->with('user')->orderBy('id', 'asc')->get();
  377. $view['website_logo'] = $this->systemConfig['website_logo'];
  378. $view['website_analytics'] = $this->systemConfig['website_analytics'];
  379. $view['website_customer_service'] = $this->systemConfig['website_customer_service'];
  380. return Response::view('user/replyTicket', $view);
  381. }
  382. }
  383. // 关闭工单
  384. public function closeTicket(Request $request)
  385. {
  386. $id = $request->get('id');
  387. $user = Session::get('user');
  388. $ret = Ticket::query()->where('id', $id)->where('user_id', $user['id'])->update(['status' => 2]);
  389. if ($ret) {
  390. return Response::json(['status' => 'success', 'data' => '', 'message' => '关闭成功']);
  391. } else {
  392. return Response::json(['status' => 'fail', 'data' => '', 'message' => '关闭失败']);
  393. }
  394. }
  395. // 邀请码
  396. public function invite(Request $request)
  397. {
  398. $user = Session::get('user');
  399. // 已生成的邀请码数量
  400. $num = Invite::query()->where('uid', $user['id'])->count();
  401. $view['website_logo'] = $this->systemConfig['website_logo'];
  402. $view['website_analytics'] = $this->systemConfig['website_analytics'];
  403. $view['website_customer_service'] = $this->systemConfig['website_customer_service'];
  404. $view['num'] = $this->systemConfig['invite_num'] - $num <= 0 ? 0 : $this->systemConfig['invite_num'] - $num; // 还可以生成的邀请码数量
  405. $view['inviteList'] = Invite::query()->where('uid', $user['id'])->with(['generator', 'user'])->paginate(10); // 邀请码列表
  406. $view['referral_traffic'] = flowAutoShow($this->systemConfig['referral_traffic'] * 1048576);
  407. $view['referral_percent'] = $this->systemConfig['referral_percent'];
  408. return Response::view('user/invite', $view);
  409. }
  410. // 公开的邀请码列表
  411. public function free(Request $request)
  412. {
  413. $view['website_logo'] = $this->systemConfig['website_logo'];
  414. $view['website_analytics'] = $this->systemConfig['website_analytics'];
  415. $view['website_customer_service'] = $this->systemConfig['website_customer_service'];
  416. $view['is_invite_register'] = $this->systemConfig['is_invite_register'];
  417. $view['is_free_code'] = $this->systemConfig['is_free_code'];
  418. $view['inviteList'] = Invite::query()->where('uid', 1)->where('status', 0)->paginate();
  419. return Response::view('user/free', $view);
  420. }
  421. // 生成邀请码
  422. public function makeInvite(Request $request)
  423. {
  424. $user = Session::get('user');
  425. // 已生成的邀请码数量
  426. $num = Invite::query()->where('uid', $user['id'])->count();
  427. if ($num >= $this->systemConfig['invite_num']) {
  428. return Response::json(['status' => 'fail', 'data' => '', 'message' => '生成失败:最多只能生成' . $this->systemConfig['invite_num'] . '个邀请码']);
  429. }
  430. $obj = new Invite();
  431. $obj->uid = $user['id'];
  432. $obj->fuid = 0;
  433. $obj->code = strtoupper(mb_substr(md5(microtime() . makeRandStr()), 8, 12));
  434. $obj->status = 0;
  435. $obj->dateline = date('Y-m-d H:i:s', strtotime("+7 days"));
  436. $obj->save();
  437. return Response::json(['status' => 'success', 'data' => '', 'message' => '生成成功']);
  438. }
  439. // 激活账号页
  440. public function activeUser(Request $request)
  441. {
  442. if ($request->method() == 'POST') {
  443. $username = trim($request->get('username'));
  444. // 是否开启账号激活
  445. if (!$this->systemConfig['is_active_register']) {
  446. Session::flash('errorMsg', '系统未开启账号激活功能,请联系管理员');
  447. return Redirect::back()->withInput();
  448. }
  449. // 查找账号
  450. $user = User::query()->where('username', $username)->first();
  451. if (!$user) {
  452. Session::flash('errorMsg', '账号不存在,请重试');
  453. return Redirect::back();
  454. } else if ($user->status < 0) {
  455. Session::flash('errorMsg', '账号已禁止登陆,无需激活');
  456. return Redirect::back();
  457. } else if ($user->status > 0) {
  458. Session::flash('errorMsg', '账号无需激活');
  459. return Redirect::back();
  460. }
  461. // 24小时内激活次数限制
  462. $activeTimes = 0;
  463. if (Cache::has('activeUser_' . md5($username))) {
  464. $activeTimes = Cache::get('activeUser_' . md5($username));
  465. if ($activeTimes >= $this->systemConfig['active_times']) {
  466. Session::flash('errorMsg', '同一个账号24小时内只能请求激活' . $this->systemConfig['active_times'] . '次,请勿频繁操作');
  467. return Redirect::back();
  468. }
  469. }
  470. // 生成激活账号的地址
  471. $token = md5($this->systemConfig['website_name'] . $username . microtime());
  472. $verify = new Verify();
  473. $verify->user_id = $user->id;
  474. $verify->username = $username;
  475. $verify->token = $token;
  476. $verify->status = 0;
  477. $verify->save();
  478. // 发送邮件
  479. $activeUserUrl = $this->systemConfig['website_url'] . '/active/' . $token;
  480. $title = '重新激活账号';
  481. $content = '请求地址:' . $activeUserUrl;
  482. try {
  483. Mail::to($user->username)->send(new activeUser($this->systemConfig['website_name'], $activeUserUrl));
  484. $this->sendEmailLog($user->id, $title, $content);
  485. } catch (\Exception $e) {
  486. $this->sendEmailLog($user->id, $title, $content, 0, $e->getMessage());
  487. }
  488. Cache::put('activeUser_' . md5($username), $activeTimes + 1, 1440);
  489. Session::flash('successMsg', '邮件已发送,请查看邮箱');
  490. return Redirect::back();
  491. } else {
  492. $view['is_active_register'] = $this->systemConfig['is_active_register'];
  493. return Response::view('user/activeUser', $view);
  494. }
  495. }
  496. // 激活账号
  497. public function active(Request $request, $token)
  498. {
  499. if (empty($token)) {
  500. return Redirect::to('login');
  501. }
  502. $verify = Verify::query()->where('token', $token)->with('user')->first();
  503. if (empty($verify)) {
  504. return Redirect::to('login');
  505. } else if (empty($verify->user)) {
  506. Session::flash('errorMsg', '该链接已失效');
  507. return Response::view('user/active');
  508. } else if ($verify->status == 1) {
  509. Session::flash('errorMsg', '该链接已失效');
  510. return Response::view('user/active');
  511. } else if ($verify->user->status != 0) {
  512. Session::flash('errorMsg', '该账号无需激活.');
  513. return Response::view('user/active');
  514. } else if (time() - strtotime($verify->created_at) >= 1800) {
  515. Session::flash('errorMsg', '该链接已过期');
  516. // 置为已失效
  517. $verify->status = 2;
  518. $verify->save();
  519. return Response::view('user/active');
  520. }
  521. // 更新账号状态
  522. $ret = User::query()->where('id', $verify->user_id)->update(['status' => 1]);
  523. if (!$ret) {
  524. Session::flash('errorMsg', '账号激活失败');
  525. return Redirect::back();
  526. }
  527. // 置为已使用
  528. $verify->status = 1;
  529. $verify->save();
  530. // 账号激活后给邀请人送流量
  531. if ($verify->user->referral_uid) {
  532. $transfer_enable = $this->systemConfig['referral_traffic'] * 1048576;
  533. User::query()->where('id', $verify->user->referral_uid)->increment('transfer_enable', $transfer_enable);
  534. User::query()->where('id', $verify->user->referral_uid)->update(['enable' => 1]);
  535. }
  536. Session::flash('successMsg', '账号激活成功');
  537. return Response::view('user/active');
  538. }
  539. // 重设密码页
  540. public function resetPassword(Request $request)
  541. {
  542. if ($request->method() == 'POST') {
  543. $username = trim($request->get('username'));
  544. // 是否开启重设密码
  545. if (!$this->systemConfig['is_reset_password']) {
  546. Session::flash('errorMsg', '系统未开启重置密码功能,请联系管理员');
  547. return Redirect::back()->withInput();
  548. }
  549. // 查找账号
  550. $user = User::query()->where('username', $username)->first();
  551. if (!$user) {
  552. Session::flash('errorMsg', '账号不存在,请重试');
  553. return Redirect::back();
  554. }
  555. // 24小时内重设密码次数限制
  556. $resetTimes = 0;
  557. if (Cache::has('resetPassword_' . md5($username))) {
  558. $resetTimes = Cache::get('resetPassword_' . md5($username));
  559. if ($resetTimes >= $this->systemConfig['reset_password_times']) {
  560. Session::flash('errorMsg', '同一个账号24小时内只能重设密码' . $this->systemConfig['reset_password_times'] . '次,请勿频繁操作');
  561. return Redirect::back();
  562. }
  563. }
  564. // 生成取回密码的地址
  565. $token = md5($this->systemConfig['website_name'] . $username . microtime());
  566. $verify = new Verify();
  567. $verify->user_id = $user->id;
  568. $verify->username = $username;
  569. $verify->token = $token;
  570. $verify->status = 0;
  571. $verify->save();
  572. // 发送邮件
  573. $resetPasswordUrl = $this->systemConfig['website_url'] . '/reset/' . $token;
  574. $title = '重置密码';
  575. $content = '请求地址:' . $resetPasswordUrl;
  576. try {
  577. Mail::to($user->username)->send(new resetPassword($this->systemConfig['website_name'], $resetPasswordUrl));
  578. $this->sendEmailLog($user->id, $title, $content);
  579. } catch (\Exception $e) {
  580. $this->sendEmailLog($user->id, $title, $content, 0, $e->getMessage());
  581. }
  582. Cache::put('resetPassword_' . md5($username), $resetTimes + 1, 1440);
  583. Session::flash('successMsg', '重置成功,请查看邮箱');
  584. return Redirect::back();
  585. } else {
  586. $view['website_logo'] = $this->systemConfig['website_logo'];
  587. $view['website_analytics'] = $this->systemConfig['website_analytics'];
  588. $view['website_customer_service'] = $this->systemConfig['website_customer_service'];
  589. $view['is_reset_password'] = $this->systemConfig['is_reset_password'];
  590. return Response::view('user/resetPassword', $view);
  591. }
  592. }
  593. // 重设密码
  594. public function reset(Request $request, $token)
  595. {
  596. if ($request->method() == 'POST') {
  597. $password = trim($request->get('password'));
  598. $repassword = trim($request->get('repassword'));
  599. if (empty($token)) {
  600. return Redirect::to('login');
  601. } else if (empty($password) || empty($repassword)) {
  602. Session::flash('errorMsg', '密码不能为空');
  603. return Redirect::back();
  604. } else if (md5($password) != md5($repassword)) {
  605. Session::flash('errorMsg', '两次输入密码不一致,请重新输入');
  606. return Redirect::back();
  607. }
  608. // 校验账号
  609. $verify = Verify::query()->where('token', $token)->with('User')->first();
  610. if (empty($verify)) {
  611. return Redirect::to('login');
  612. } else if ($verify->status == 1) {
  613. Session::flash('errorMsg', '该链接已失效');
  614. return Redirect::back();
  615. } else if ($verify->user->status < 0) {
  616. Session::flash('errorMsg', '账号已被禁用');
  617. return Redirect::back();
  618. } else if (md5($password) == $verify->user->password) {
  619. Session::flash('errorMsg', '新旧密码一样,请重新输入');
  620. return Redirect::back();
  621. }
  622. // 更新密码
  623. $ret = User::query()->where('id', $verify->user_id)->update(['password' => md5($password)]);
  624. if (!$ret) {
  625. Session::flash('errorMsg', '重设密码失败');
  626. return Redirect::back();
  627. }
  628. // 置为已使用
  629. $verify->status = 1;
  630. $verify->save();
  631. Session::flash('successMsg', '新密码设置成功,请自行登录');
  632. return Redirect::back();
  633. } else {
  634. if (empty($token)) {
  635. return Redirect::to('login');
  636. }
  637. $verify = Verify::query()->where('token', $token)->with('user')->first();
  638. if (empty($verify)) {
  639. return Redirect::to('login');
  640. } else if (time() - strtotime($verify->created_at) >= 1800) {
  641. Session::flash('errorMsg', '该链接已过期');
  642. // 置为已失效
  643. $verify->status = 2;
  644. $verify->save();
  645. // 重新获取一遍verify
  646. $view['verify'] = Verify::query()->where('token', $token)->with('user')->first();
  647. return Response::view('user/reset', $view);
  648. }
  649. $view['verify'] = $verify;
  650. return Response::view('user/reset', $view);
  651. }
  652. }
  653. // 使用优惠券
  654. public function redeemCoupon(Request $request)
  655. {
  656. $coupon_sn = $request->get('coupon_sn');
  657. if (empty($coupon_sn)) {
  658. return Response::json(['status' => 'fail', 'data' => '', 'message' => '优惠券不能为空']);
  659. }
  660. $coupon = Coupon::query()->where('sn', $coupon_sn)->whereIn('type', [1, 2])->where('is_del', 0)->first();
  661. if (!$coupon) {
  662. return Response::json(['status' => 'fail', 'data' => '', 'message' => '该优惠券不存在']);
  663. } else if ($coupon->status == 1) {
  664. return Response::json(['status' => 'fail', 'data' => '', 'message' => '该优惠券已使用,请换一个试试']);
  665. } else if ($coupon->status == 2) {
  666. return Response::json(['status' => 'fail', 'data' => '', 'message' => '该优惠券已失效,请换一个试试']);
  667. } else if ($coupon->available_start > time() || $coupon->available_end < time()) {
  668. $coupon->status = 2;
  669. $coupon->save();
  670. return Response::json(['status' => 'fail', 'data' => '', 'message' => '该优惠券已失效,请换一个试试']);
  671. }
  672. $data = [
  673. 'type' => $coupon->type,
  674. 'amount' => $coupon->amount,
  675. 'discount' => $coupon->discount
  676. ];
  677. return Response::json(['status' => 'success', 'data' => $data, 'message' => '该优惠券有效']);
  678. }
  679. // 购买服务
  680. public function addOrder(Request $request)
  681. {
  682. $goods_id = intval($request->get('goods_id'));
  683. $coupon_sn = $request->get('coupon_sn');
  684. $user = Session::get('user');
  685. if ($request->method() == 'POST') {
  686. $goods = Goods::query()->with(['label'])->where('id', $goods_id)->where('is_del', 0)->where('status', 1)->first();
  687. if (empty($goods)) {
  688. return Response::json(['status' => 'fail', 'data' => '', 'message' => '支付失败:商品或服务已下架']);
  689. }
  690. // 检查配置是否启用了限购:all-所有商品限购, free-价格为0的商品限购, none-不限购(默认)
  691. if (!isset($this->systemConfig['goods_purchase_limit_strategy'])) {
  692. $this->systemConfig['goods_purchase_limit_strategy'] = 'none';
  693. }
  694. $strategy = $this->systemConfig['goods_purchase_limit_strategy'];
  695. if ($strategy == 'all' || ($strategy == 'free' && $goods->price == 0)) {
  696. // 判断是否已经购买过该商品
  697. $none_expire_good_exist = Order::query()->where('user_id', $user['id'])->where('goods_id', $goods_id)->where('is_expire', 0)->where('status', '>=', 0)->exists();
  698. if ($none_expire_good_exist) {
  699. return Response::json(['status' => 'fail', 'data' => '', 'message' => '支付失败:商品不可重复购买']);
  700. }
  701. }
  702. // 使用优惠券
  703. if (!empty($coupon_sn)) {
  704. $coupon = Coupon::query()->where('sn', $coupon_sn)->whereIn('type', [1, 2])->where('is_del', 0)->where('status', 0)->first();
  705. if (empty($coupon)) {
  706. return Response::json(['status' => 'fail', 'data' => '', 'message' => '支付失败:优惠券不存在']);
  707. }
  708. // 计算实际应支付总价
  709. $amount = $coupon->type == 2 ? $goods->price * $coupon->discount / 10 : $goods->price - $coupon->amount;
  710. $amount = $amount > 0 ? $amount : 0;
  711. } else {
  712. $amount = $goods->price;
  713. }
  714. // 验证账号余额是否充足
  715. $user = User::query()->where('id', $user['id'])->first();
  716. if ($user->balance < $amount) {
  717. return Response::json(['status' => 'fail', 'data' => '', 'message' => '支付失败:您的余额不足,请先充值']);
  718. }
  719. DB::beginTransaction();
  720. try {
  721. // 生成订单
  722. $order = new Order();
  723. $order->order_sn = date('ymdHis') . mt_rand(100000, 999999);
  724. $order->user_id = $user->id;
  725. $order->goods_id = $goods_id;
  726. $order->coupon_id = !empty($coupon) ? $coupon->id : 0;
  727. $order->origin_amount = $goods->price;
  728. $order->amount = $amount;
  729. $order->expire_at = date("Y-m-d H:i:s", strtotime("+" . $goods->days . " days"));
  730. $order->is_expire = 0;
  731. $order->pay_way = 1;
  732. $order->status = 2;
  733. $order->save();
  734. // 扣余额
  735. User::query()->where('id', $user->id)->decrement('balance', $amount * 100);
  736. // 记录余额操作日志
  737. $userBalanceLog = new UserBalanceLog();
  738. $userBalanceLog->user_id = $user->id;
  739. $userBalanceLog->order_id = $order->oid;
  740. $userBalanceLog->before = $user->balance;
  741. $userBalanceLog->after = $user->balance - $amount;
  742. $userBalanceLog->amount = -1 * $amount;
  743. $userBalanceLog->desc = '购买服务:' . $goods->name;
  744. $userBalanceLog->created_at = date('Y-m-d H:i:s');
  745. $userBalanceLog->save();
  746. // 优惠券置为已使用
  747. if (!empty($coupon)) {
  748. if ($coupon->usage == 1) {
  749. $coupon->status = 1;
  750. $coupon->save();
  751. }
  752. // 写入日志
  753. $couponLog = new CouponLog();
  754. $couponLog->coupon_id = $coupon->id;
  755. $couponLog->goods_id = $goods_id;
  756. $couponLog->order_id = $order->oid;
  757. $couponLog->save();
  758. }
  759. // 如果买的是套餐,则先将之前购买的所有套餐置都无效,并扣掉之前所有套餐的流量,并移除之前所有套餐的标签
  760. if ($goods->type == 2) {
  761. $existOrderList = Order::query()->with('goods')->whereHas('goods', function ($q) {
  762. $q->where('type', 2);
  763. })->where('user_id', $user->id)->where('oid', '<>', $order->oid)->where('is_expire', 0)->get();
  764. foreach ($existOrderList as $vo) {
  765. Order::query()->where('oid', $vo->oid)->update(['is_expire' => 1]);
  766. User::query()->where('id', $user->id)->decrement('transfer_enable', $vo->goods->traffic * 1048576);
  767. //todo:移除之前套餐的标签(需要注意:有些套餐和流量包用同一个标签,所以移除完套餐的标签后需要补齐流量包的标签)
  768. }
  769. // 重置已用流量
  770. User::query()->where('id', $user->id)->update(['u' => 0, 'd' => 0]);
  771. }
  772. // 把商品的流量加到账号上
  773. User::query()->where('id', $user->id)->increment('transfer_enable', $goods->traffic * 1048576);
  774. // 更新账号过期时间、流量重置日
  775. if ($goods->type == 2) {
  776. $traffic_reset_day = in_array(date('d'), [29, 30, 31]) ? 28 : abs(date('d'));
  777. User::query()->where('id', $user->id)->update(['traffic_reset_day' => $traffic_reset_day, 'expire_time' => date('Y-m-d', strtotime("+" . $goods->days . " days")), 'enable' => 1]);
  778. } else {
  779. $lastCanUseDays = floor(round(strtotime($user->expire_time) - strtotime(date('Y-m-d H:i:s'))) / 3600 / 24);
  780. if ($lastCanUseDays < $goods->days) {
  781. User::query()->where('id', $user->id)->update(['expire_time' => date('Y-m-d', strtotime("+" . $goods->days . " days")), 'enable' => 1]);
  782. }
  783. }
  784. // 写入用户标签
  785. if ($goods->label) {
  786. // 取出现有的标签
  787. $userLabels = UserLabel::query()->where('user_id', $user->id)->pluck('label_id')->toArray();
  788. $goodsLabels = GoodsLabel::query()->where('goods_id', $goods_id)->pluck('label_id')->toArray();
  789. $newUserLabels = array_merge($userLabels, $goodsLabels);
  790. // 删除用户所有标签
  791. UserLabel::query()->where('user_id', $user->id)->delete();
  792. // 生成标签
  793. foreach ($newUserLabels as $vo) {
  794. $obj = new UserLabel();
  795. $obj->user_id = $user->id;
  796. $obj->label_id = $vo;
  797. $obj->save();
  798. }
  799. }
  800. // 写入返利日志
  801. if ($user->referral_uid) {
  802. $referralLog = new ReferralLog();
  803. $referralLog->user_id = $user->id;
  804. $referralLog->ref_user_id = $user->referral_uid;
  805. $referralLog->order_id = $order->oid;
  806. $referralLog->amount = $amount;
  807. $referralLog->ref_amount = $amount * $this->systemConfig['referral_percent'];
  808. $referralLog->status = 0;
  809. $referralLog->save();
  810. }
  811. DB::commit();
  812. return Response::json(['status' => 'success', 'data' => '', 'message' => '支付成功']);
  813. } catch (\Exception $e) {
  814. DB::rollBack();
  815. Log::error('支付订单失败:' . $e->getMessage());
  816. return Response::json(['status' => 'fail', 'data' => '', 'message' => '支付失败:' . $e->getMessage()]);
  817. }
  818. } else {
  819. $goods = Goods::query()->where('id', $goods_id)->where('is_del', 0)->where('status', 1)->first();
  820. if (empty($goods)) {
  821. return Redirect::to('user/goodsList');
  822. }
  823. $goods->traffic = flowAutoShow($goods->traffic * 1048576);
  824. $view['goods'] = $goods;
  825. $view['is_youzan'] = $this->systemConfig['is_youzan'];
  826. $view['website_logo'] = $this->systemConfig['website_logo'];
  827. $view['website_analytics'] = $this->systemConfig['website_analytics'];
  828. $view['website_customer_service'] = $this->systemConfig['website_customer_service'];
  829. return Response::view('user/addOrder', $view);
  830. }
  831. }
  832. // 积分兑换流量
  833. public function exchange(Request $request)
  834. {
  835. $user = Session::get('user');
  836. // 积分满100才可以兑换
  837. if ($user['score'] < 100) {
  838. return Response::json(['status' => 'fail', 'data' => '', 'message' => '兑换失败:满100才可以兑换,请继续累计吧']);
  839. }
  840. DB::beginTransaction();
  841. try {
  842. // 写入积分操作日志
  843. $userScoreLog = new UserScoreLog();
  844. $userScoreLog->user_id = $user['id'];
  845. $userScoreLog->before = $user['score'];
  846. $userScoreLog->after = 0;
  847. $userScoreLog->score = -1 * $user['score'];
  848. $userScoreLog->desc = '积分兑换流量';
  849. $userScoreLog->created_at = date('Y-m-d H:i:s');
  850. $userScoreLog->save();
  851. // 扣积分加流量
  852. if ($userScoreLog->id) {
  853. User::query()->where('id', $user['id'])->update(['score' => 0]);
  854. User::query()->where('id', $user['id'])->increment('transfer_enable', $user['score'] * 1048576);
  855. }
  856. DB::commit();
  857. // 更新session
  858. $user = User::query()->where('id', $user['id'])->first()->toArray();
  859. Session::remove('user');
  860. Session::put('user', $user);
  861. return Response::json(['status' => 'success', 'data' => '', 'message' => '兑换成功']);
  862. } catch (\Exception $e) {
  863. DB::rollBack();
  864. return Response::json(['status' => 'fail', 'data' => '', 'message' => '兑换失败:' . $e->getMessage()]);
  865. }
  866. }
  867. // 推广返利
  868. public function referral(Request $request)
  869. {
  870. // 生成个人推广链接
  871. $user = Session::get('user');
  872. $view['website_logo'] = $this->systemConfig['website_logo'];
  873. $view['website_analytics'] = $this->systemConfig['website_analytics'];
  874. $view['website_customer_service'] = $this->systemConfig['website_customer_service'];
  875. $view['referral_traffic'] = flowAutoShow($this->systemConfig['referral_traffic'] * 1048576);
  876. $view['referral_percent'] = $this->systemConfig['referral_percent'];
  877. $view['referral_money'] = $this->systemConfig['referral_money'];
  878. $view['totalAmount'] = ReferralLog::query()->where('ref_user_id', $user['id'])->sum('ref_amount') / 100;
  879. $view['canAmount'] = ReferralLog::query()->where('ref_user_id', $user['id'])->where('status', 0)->sum('ref_amount') / 100;
  880. $view['link'] = $this->systemConfig['website_url'] . '/register?aff=' . $user['id'];
  881. $view['referralLogList'] = ReferralLog::query()->where('ref_user_id', $user['id'])->with('user')->paginate(10);
  882. $view['referralApplyList'] = ReferralApply::query()->where('user_id', $user['id'])->with('user')->paginate(10);
  883. return Response::view('user/referral', $view);
  884. }
  885. // 申请提现
  886. public function extractMoney(Request $request)
  887. {
  888. $user = Session::get('user');
  889. // 判断是否已存在申请
  890. $referralApply = ReferralApply::query()->where('user_id', $user['id'])->whereIn('status', [0, 1])->first();
  891. if ($referralApply) {
  892. return Response::json(['status' => 'fail', 'data' => '', 'message' => '申请失败:已存在申请,请等待之前的申请处理完']);
  893. }
  894. // 校验可以提现金额是否超过系统设置的阀值
  895. $ref_amount = ReferralLog::query()->where('ref_user_id', $user['id'])->where('status', 0)->sum('ref_amount');
  896. if ($ref_amount / 100 < $this->systemConfig['referral_money']) {
  897. return Response::json(['status' => 'fail', 'data' => '', 'message' => '申请失败:满' . $this->systemConfig['referral_money'] . '元才可以提现,继续努力吧']);
  898. }
  899. // 取出本次申请关联返利日志ID
  900. $link_logs = '';
  901. $referralLog = ReferralLog::query()->where('ref_user_id', $user['id'])->where('status', 0)->get();
  902. foreach ($referralLog as $log) {
  903. $link_logs .= $log->id . ',';
  904. }
  905. $link_logs = rtrim($link_logs, ',');
  906. $obj = new ReferralApply();
  907. $obj->user_id = $user['id'];
  908. $obj->before = $ref_amount;
  909. $obj->after = 0;
  910. $obj->amount = $ref_amount;
  911. $obj->link_logs = $link_logs;
  912. $obj->status = 0;
  913. $obj->save();
  914. return Response::json(['status' => 'success', 'data' => '', 'message' => '申请成功,请等待管理员审核']);
  915. }
  916. // 节点订阅
  917. public function subscribe(Request $request)
  918. {
  919. $user = Session::get('user');
  920. // 如果没有唯一码则生成一个
  921. $subscribe = UserSubscribe::query()->where('user_id', $user['id'])->first();
  922. if (!$subscribe) {
  923. $code = $this->makeSubscribeCode();
  924. $obj = new UserSubscribe();
  925. $obj->user_id = $user['id'];
  926. $obj->code = $code;
  927. $obj->times = 0;
  928. $obj->save();
  929. } else {
  930. $code = $subscribe->code;
  931. }
  932. $view['website_logo'] = $this->systemConfig['website_logo'];
  933. $view['website_analytics'] = $this->systemConfig['website_analytics'];
  934. $view['website_customer_service'] = $this->systemConfig['website_customer_service'];
  935. $view['subscribe_status'] = !$subscribe ? 1 : $subscribe->status;
  936. $view['link'] = $this->systemConfig['subscribe_domain'] ? $this->systemConfig['subscribe_domain'] . '/s/' . $code : $this->systemConfig['website_url'] . '/s/' . $code;
  937. return Response::view('/user/subscribe', $view);
  938. }
  939. // 更换订阅地址
  940. public function exchangeSubscribe(Request $request)
  941. {
  942. $user = Session::get('user');
  943. DB::beginTransaction();
  944. try {
  945. // 更换订阅地址
  946. $code = $this->makeSubscribeCode();
  947. UserSubscribe::query()->where('user_id', $user['id'])->update(['code' => $code]);
  948. // 更换连接密码
  949. User::query()->where('id', $user['id'])->update(['passwd' => makeRandStr()]);
  950. DB::commit();
  951. return Response::json(['status' => 'success', 'data' => '', 'message' => '更换成功']);
  952. } catch (\Exception $e) {
  953. DB::rollBack();
  954. Log::info("更换订阅地址异常:" . $e->getMessage());
  955. return Response::json(['status' => 'fail', 'data' => '', 'message' => '更换失败' . $e->getMessage()]);
  956. }
  957. }
  958. // 转换成管理员的身份
  959. public function switchToAdmin(Request $request)
  960. {
  961. if (!Session::has('admin') || !Session::has('user')) {
  962. return Response::json(['status' => 'fail', 'data' => '', 'message' => '非法请求']);
  963. }
  964. $admin = Session::get('admin');
  965. $user = User::query()->where('id', $admin['id'])->first();
  966. if (!$user) {
  967. return Response::json(['status' => 'fail', 'data' => '', 'message' => "非法请求"]);
  968. }
  969. // 管理员信息重新写入user
  970. Session::put('user', Session::get('admin'));
  971. return Response::json(['status' => 'success', 'data' => '', 'message' => "身份切换成功"]);
  972. }
  973. // 卡券余额充值
  974. public function charge(Request $request)
  975. {
  976. $user = Session::get('user');
  977. $coupon_sn = trim($request->get('coupon_sn'));
  978. if (empty($coupon_sn)) {
  979. return Response::json(['status' => 'fail', 'data' => '', 'message' => '券码不能为空']);
  980. }
  981. $coupon = Coupon::query()->where('sn', $coupon_sn)->where('type', 3)->where('is_del', 0)->where('status', 0)->first();
  982. if (!$coupon) {
  983. return Response::json(['status' => 'fail', 'data' => '', 'message' => '该券不可用']);
  984. }
  985. DB::beginTransaction();
  986. try {
  987. $user = User::query()->where('id', $user['id'])->first();
  988. // 写入日志
  989. $log = new UserBalanceLog();
  990. $log->user_id = $user->id;
  991. $log->order_id = 0;
  992. $log->before = $user->balance;
  993. $log->after = $user->balance + $coupon->amount;
  994. $log->amount = $coupon->amount;
  995. $log->desc = '用户手动充值 - [充值券:' . $coupon_sn . ']';
  996. $log->created_at = date('Y-m-d H:i:s');
  997. $log->save();
  998. // 余额充值
  999. $user->balance = $user->balance + $coupon->amount;
  1000. $user->save();
  1001. // 更改卡券状态
  1002. $coupon->status = 1;
  1003. $coupon->save();
  1004. // 写入卡券日志
  1005. $couponLog = new CouponLog();
  1006. $couponLog->coupon_id = $coupon->id;
  1007. $couponLog->goods_id = 0;
  1008. $couponLog->order_id = 0;
  1009. $couponLog->save();
  1010. DB::commit();
  1011. return Response::json(['status' => 'success', 'data' => '', 'message' => '充值成功']);
  1012. } catch (\Exception $e) {
  1013. Log::error($e->getMessage());
  1014. DB::rollBack();
  1015. return Response::json(['status' => 'fail', 'data' => '', 'message' => '充值失败']);
  1016. }
  1017. }
  1018. public function switchLang(Request $request, $locale)
  1019. {
  1020. Session::put("locale", $locale);
  1021. return Redirect::back();
  1022. }
  1023. }