V1Controller.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. namespace App\Http\Controllers\Api\Client;
  3. use App\Components\Helpers;
  4. use App\Http\Controllers\Controller;
  5. use App\Http\Controllers\PaymentController;
  6. use App\Models\Coupon;
  7. use App\Models\Goods;
  8. use App\Models\GoodsCategory;
  9. use App\Models\Order;
  10. use App\Models\Payback;
  11. use App\Models\Payment;
  12. use App\Models\ReferralLog;
  13. use App\Models\User;
  14. use Hashids\Hashids;
  15. use Illuminate\Http\JsonResponse;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Cache;
  18. use Validator;
  19. class V1Controller extends Controller
  20. {
  21. private static $method;
  22. public function __construct()
  23. {
  24. $this->middleware('auth:api', ['except' => ['login', 'register', 'shop', 'config', 'getConfig']]);
  25. auth()->shouldUse('api');
  26. }
  27. public function login(Request $request)
  28. {
  29. $validator = Validator::make($request->all(), [
  30. 'username' => 'required|'.(sysConfig('username_type') ?? 'email'),
  31. 'password' => 'required|string|min:6',
  32. ]);
  33. if ($validator->fails()) {
  34. return response()->json(['ret' => 0, 'msg' => $validator->errors()->all()], 422);
  35. }
  36. if ($token = auth('api')->attempt($validator->validated())) {
  37. return $this->createNewToken($token);
  38. }
  39. return response()->json(['ret' => 0, 'msg' => '登录信息错误'], 401);
  40. }
  41. protected function createNewToken($token)
  42. {
  43. return response()->json([
  44. 'ret' => 1,
  45. 'data' => [
  46. 'access_token' => $token,
  47. 'token_type' => 'bearer',
  48. 'expires_in' => auth('api')->factory()->getTTL() * 60,
  49. 'user' => auth('api')->user()->profile(),
  50. ],
  51. ]);
  52. }
  53. public function register(Request $request)
  54. {
  55. $validator = Validator::make($request->all(), [
  56. 'name' => 'required|string|between:2,100',
  57. 'username' => 'required|'.(sysConfig('username_type') ?? 'email').'|max:100|unique:user,username',
  58. 'password' => 'required|string|confirmed|min:6',
  59. ]);
  60. if ($validator->fails()) {
  61. return response()->json($validator->errors()->all(), 400);
  62. }
  63. $user = User::create(array_merge(
  64. $validator->validated(),
  65. ['password' => $request->password]
  66. ));
  67. return response()->json(['ret' => 1, 'user' => $user], 201);
  68. }
  69. public function logout()
  70. {
  71. auth('api')->logout();
  72. return response()->json(['ret' => 1]);
  73. }
  74. public function refresh()
  75. {
  76. return $this->createNewToken(auth('api')->refresh());
  77. }
  78. public function userProfile()
  79. {
  80. $user = auth('api')->user();
  81. $userInfo = $user->profile();
  82. $userInfo['subUrl'] = $user->subUrl();
  83. $totalTransfer = $user->transfer_enable;
  84. $usedTransfer = $user->used_traffic;
  85. $unusedTraffic = $totalTransfer - $usedTransfer > 0 ? $totalTransfer - $usedTransfer : 0;
  86. $userInfo['unusedTraffic'] = flowAutoShow($unusedTraffic);
  87. return response()->json(['ret' => 1, 'data' => $userInfo]);
  88. }
  89. public function nodeList(int $id = null)
  90. {
  91. $user = auth('api')->user();
  92. $nodes = $user->nodes()->get();
  93. return response()->json(['ret' => 1, 'data' => $nodes]);
  94. }
  95. public function shop()
  96. {
  97. $shops = [
  98. 'keys' => [],
  99. 'data' => [],
  100. ];
  101. $shop_plan = GoodsCategory::query()->where('status', 1)->get();
  102. foreach ($shop_plan as $item) {
  103. array_push($shops['keys'], $item['name']);
  104. $shops['data'][$item['name']] = $item->goods()->get()->append('traffic_label')->toArray();
  105. }
  106. return response()->json(['ret' => 1, 'data' => $shops]);
  107. }
  108. public function getConfig()
  109. {
  110. $config = config('bobclient');
  111. $config['website_name'] = sysConfig('website_name');
  112. $config['website_url'] = sysConfig('website_url');
  113. $config['payment'] = [
  114. 'alipay' => sysConfig('is_AliPay'),
  115. 'wechat' => sysConfig('is_WeChatPay'),
  116. ];
  117. return response()->json(['ret' => 1, 'data' => $config]);
  118. }
  119. // 创建支付订单
  120. public function purchase(Request $request)
  121. {
  122. $goods_id = $request->input('goods_id');
  123. $coupon_sn = $request->input('coupon_sn');
  124. self::$method = $request->input('method');
  125. $credit = $request->input('amount');
  126. $pay_type = $request->input('pay_type');
  127. $amount = 0;
  128. // 充值余额
  129. if ($credit) {
  130. if (! is_numeric($credit) || $credit <= 0) {
  131. return response()->json(['ret' => 0, 'msg' => trans('user.payment.error')]);
  132. }
  133. $amount = $credit;
  134. // 购买服务
  135. } elseif ($goods_id && self::$method) {
  136. $goods = Goods::find($goods_id);
  137. if (! $goods || ! $goods->status) {
  138. return response()->json(['ret' => 0, 'msg' => '订单创建失败:商品已下架']);
  139. }
  140. $amount = $goods->price;
  141. // 是否有生效的套餐
  142. $activePlan = Order::userActivePlan()->doesntExist();
  143. // 无生效套餐,禁止购买加油包
  144. if ($goods->type === 1 && $activePlan) {
  145. return response()->json(['ret' => 0, 'msg' => '购买加油包前,请先购买套餐']);
  146. }
  147. // 单个商品限购
  148. if ($goods->limit_num) {
  149. $count = Order::uid()->where('status', '>=', 0)->whereGoodsId($goods_id)->count();
  150. if ($count >= $goods->limit_num) {
  151. return response()->json(['ret' => 0, 'msg' => '此商品限购'.$goods->limit_num.'次,您已购买'.$count.'次']);
  152. }
  153. }
  154. // 使用优惠券
  155. if ($coupon_sn) {
  156. $coupon = Coupon::whereStatus(0)->whereIn('type', [1, 2])->whereSn($coupon_sn)->first();
  157. if (! $coupon) {
  158. return response()->json(['ret' => 0, 'msg' => '订单创建失败:优惠券不存在']);
  159. }
  160. // 计算实际应支付总价
  161. $amount = $coupon->type === 2 ? $goods->price * $coupon->value / 100 : $goods->price - $coupon->value;
  162. $amount = $amount > 0 ? round($amount, 2) : 0; // 四舍五入保留2位小数,避免无法正常创建订单
  163. }
  164. //非余额付款下,检查在线支付是否开启
  165. if (self::$method !== 'credit') {
  166. // 判断是否开启在线支付
  167. if (! sysConfig('is_onlinePay')) {
  168. return response()->json(['ret' => 0, 'msg' => '订单创建失败:系统并未开启在线支付功能']);
  169. }
  170. // 判断是否存在同个商品的未支付订单
  171. if (Order::uid()->whereStatus(0)->exists()) {
  172. return response()->json(['ret' => 0, 'msg' => '订单创建失败:尚有未支付的订单,请先去支付']);
  173. }
  174. } elseif (Auth::getUser()->credit < $amount) { // 验证账号余额是否充足
  175. return response()->json(['ret' => 0, 'msg' => '您的余额不足,请先充值']);
  176. }
  177. // 价格异常判断
  178. if ($amount < 0) {
  179. return response()->json(['ret' => 0, 'msg' => '订单创建失败:订单总价异常']);
  180. }
  181. if ($amount === 0 && self::$method !== 'credit') {
  182. return response()->json(['ret' => 0, 'msg' => '订单创建失败:订单总价为0,无需使用在线支付']);
  183. }
  184. }
  185. // 生成订单
  186. try {
  187. $newOrder = Order::create([
  188. 'sn' => date('ymdHis').random_int(100000, 999999),
  189. 'user_id' => auth()->id(),
  190. 'goods_id' => $credit ? null : $goods_id,
  191. 'coupon_id' => $coupon->id ?? null,
  192. 'origin_amount' => $credit ?: $goods->price ?? 0,
  193. 'amount' => $amount,
  194. 'pay_type' => $pay_type,
  195. 'pay_way' => self::$method,
  196. ]);
  197. // 使用优惠券,减少可使用次数
  198. if (! empty($coupon)) {
  199. if ($coupon->usable_times > 0) {
  200. $coupon->decrement('usable_times', 1);
  201. }
  202. Helpers::addCouponLog('订单支付使用', $coupon->id, $goods_id, $newOrder->id);
  203. }
  204. $request->merge(['id' => $newOrder->id, 'type' => $pay_type, 'amount' => $amount]);
  205. PaymentController::$method = self::$method;
  206. // 生成支付单
  207. $data = PaymentController::getClient()->purchase($request);
  208. $data = $data->getData(true);
  209. $data['order_id'] = $newOrder->id;
  210. return response()->json($data);
  211. } catch (Exception $e) {
  212. Log::error('订单生成错误:'.$e->getMessage());
  213. }
  214. return response()->json(['ret' => 0, 'msg' => '订单创建失败']);
  215. }
  216. /**
  217. * @param Request $request
  218. * @return JsonResponse
  219. */
  220. public static function getStatus(Request $request): JsonResponse
  221. {
  222. $order_id = $request->input('order_id');
  223. $payment = Order::query()->find($order_id)->payment;
  224. if ($payment) {
  225. if ($payment->status === 1) {
  226. return response()->json(['ret' => 1, 'msg' => '支付成功']);
  227. }
  228. if ($payment->status === -1) {
  229. return response()->json(['ret' => 0, 'msg' => '订单超时未支付,已自动关闭']);
  230. }
  231. return response()->json(['ret' => 0, 'msg' => '等待支付']);
  232. }
  233. return response()->json(['ret' => 0, 'msg' => '未知订单']);
  234. }
  235. public function gift(Request $request)
  236. {
  237. $user = $request->user('api');
  238. $referral_traffic = flowAutoShow(sysConfig('referral_traffic') * MB);
  239. $referral_percent = sysConfig('referral_percent');
  240. // 邀请码
  241. $code = $user->invites()->whereStatus(1)->value('code');
  242. $data['invite_gift'] = trans('user.invite.promotion', [
  243. 'traffic' => $referral_traffic,
  244. 'referral_percent' => $referral_percent * 100,
  245. ]);
  246. $affSalt = sysConfig('aff_salt');
  247. if (isset($affSalt)) {
  248. $aff_link = route('register', ['aff' => (new Hashids($affSalt, 8))->encode($user->id)]);
  249. } else {
  250. $aff_link = route('register', ['aff' => $user->id]);
  251. }
  252. $data['invite_url'] = $aff_link;
  253. $data['invite_text'] = $aff_link.'&(复制整段文字到浏览器打开即可访问),找梯子最重要的就是稳定,这个已经上线三年了,一直稳定没有被封过,赶紧下载备用吧!安装后打开填写我的邀请码【'.$code.'】,你还能多得3天会员.';
  254. // 累计数据
  255. $data['back_sum'] = ReferralLog::query()->where('inviter_id', $user->id)->sum('commission') / 100;
  256. $data['user_sum'] = $user->invitees()->count();
  257. $data['list'] = $user->invitees()->selectRaw('username, UNIX_TIMESTAMP(created_at) as created_at')->limit(10)->get();
  258. return response()->json(['ret' => 1, 'data' => $data]);
  259. }
  260. public function checkIn(Request $request): JsonResponse
  261. {
  262. $user = $request->user();
  263. // 系统开启登录加积分功能才可以签到
  264. if (! sysConfig('is_checkin')) {
  265. return response()->json(['ret' => 0, 'title' => trans('common.failed'), 'msg' => trans('user.home.attendance.disable')]);
  266. }
  267. // 已签到过,验证是否有效
  268. if (Cache::has('userCheckIn_'.$user->id)) {
  269. return response()->json(['ret' => 0, 'title' => trans('common.success'), 'msg' => trans('user.home.attendance.done')]);
  270. }
  271. $traffic = random_int((int) sysConfig('min_rand_traffic'), (int) sysConfig('max_rand_traffic')) * MB;
  272. if (! $user->incrementData($traffic)) {
  273. return response()->json(['ret' => 0, 'title' => trans('common.failed'), 'msg' => trans('user.home.attendance.failed')]);
  274. }
  275. // 写入用户流量变动记录
  276. Helpers::addUserTrafficModifyLog($user->id, null, $user->transfer_enable, $user->transfer_enable + $traffic, trans('user.home.attendance.attribute'));
  277. // 多久后可以再签到
  278. $ttl = sysConfig('traffic_limit_time') ? sysConfig('traffic_limit_time') * Minute : Day;
  279. Cache::put('userCheckIn_'.$user->id, '1', $ttl);
  280. return response()->json(['ret' => 1, 'msg' => trans('user.home.attendance.success', ['data' => flowAutoShow($traffic)])]);
  281. }
  282. }