UserController.php 53 KB

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