UserController.php 51 KB

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