275 lines
8.5 KiB
PHP
275 lines
8.5 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: sunny
|
|
* Date: 2019/2/26
|
|
* Time: 11:35 AM
|
|
*/
|
|
|
|
namespace App\Api\Controllers;
|
|
|
|
use App\Models\Cdkey;
|
|
use App\Models\KeysApply;
|
|
use App\Models\KeysOrder;
|
|
use App\User;
|
|
use Auth;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Image;
|
|
use QrCode;
|
|
|
|
class KeysController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
// $this->middleware('auth.api');
|
|
// $this->user = \Auth::guard('api')->user();
|
|
// $this->uid = \Auth::guard('api')->id();
|
|
$this->user = User::find(824);
|
|
$this->uid = 824;
|
|
}
|
|
public function index(Request $request)
|
|
{
|
|
$user = $this->user;
|
|
|
|
if ($user->identity->identity_id == 0) {
|
|
return $this->failed('请开通VIP后继续操作');
|
|
}
|
|
// 已使用 USED 未使用 EMPTY
|
|
$status = $request->status ?? 'EMPTY';
|
|
|
|
$keys = Cdkey::where('belong_uid', $user->id)
|
|
->when($status, function ($query) use ($status) {
|
|
if ($status == 'USED') {
|
|
$query->whereNotNull('used_at');
|
|
} elseif ($status == 'EMPTY') {
|
|
$query->whereNull('used_at');
|
|
}
|
|
})
|
|
->get();
|
|
|
|
$useNum = Cdkey::where('belong_uid', $user->id)
|
|
->whereNotNull('used_at')
|
|
->count();
|
|
$notUseNum = Cdkey::where('belong_uid', $user->id)
|
|
->whereNull('used_at')
|
|
->count();
|
|
return view('keys.index', compact('keys', 'status', 'useNum', 'notUseNum'));
|
|
}
|
|
|
|
public function show(Cdkey $cdkey)
|
|
{
|
|
if ($cdkey->belong_uid != $this->uid) {
|
|
return $this->failed('没有权限查看激活码');
|
|
}
|
|
$code = \App\Helpers\CDKEY::generator($cdkey->code, 'svg', 129);
|
|
return view('keys.show', compact('code', 'cdkey'));
|
|
}
|
|
|
|
public function manage(Request $request)
|
|
{
|
|
$user = $this->user;
|
|
if ($user->id == 1757) {
|
|
$user = User::find(62);
|
|
}
|
|
if ($user->identity->identity_id < 2) {
|
|
return $this->failed('没有激活码管理权限');
|
|
}
|
|
$count = KeysApply::where('user_id', $user->id)->whereIn('status', [0, 1])->sum('num');
|
|
return view('keys.manage', compact('count'));
|
|
}
|
|
|
|
|
|
public function doManage(Request $request)
|
|
{
|
|
$user = $this->user;
|
|
if ($user->id == 1757) {
|
|
$user = User::find(62);
|
|
}
|
|
if ($user->identity->identity_id < 2) {
|
|
return $this->failed('没有激活码管理权限');
|
|
}
|
|
|
|
$data = $request->all();
|
|
$data['user_id'] = $user->id;
|
|
$apply = KeysApply::create($data);
|
|
if ($apply) {
|
|
if ($apply->is_print == 0) {
|
|
if (self::callback($apply->user_id)) {
|
|
$apply->status = 1;
|
|
$apply->save();
|
|
return $this->success('申请成功');
|
|
}
|
|
}
|
|
return $this->success('申请成功');
|
|
} else {
|
|
return $this->failed('申请失败');
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 支付完成之后,直接生产虚拟卡
|
|
* @Author:<C.Jason>
|
|
* @Date:2018-12-23T17:47:51+0800
|
|
* @return function [description]
|
|
*/
|
|
public function callback($user_id)
|
|
{
|
|
$codes = [];
|
|
for ($i = 0; $i < 100; $i++) {
|
|
$codes[$i]['belong_uid'] = $user_id;
|
|
$codes[$i]['code'] = $this->random();
|
|
$codes[$i]['is_print'] = 0;
|
|
$codes[$i]['effective_at'] = now();
|
|
$codes[$i]['created_at'] = now();
|
|
$codes[$i]['updated_at'] = now();
|
|
}
|
|
return Cdkey::insert($codes);
|
|
}
|
|
|
|
/**
|
|
* 产生随机字串,可用来自动生成密码 17010
|
|
* @Author:<C.Jason>
|
|
* @Date:2018-09-14T17:00:34+0800
|
|
* @param integer $len [description]
|
|
* @return [type] [description]
|
|
*/
|
|
private function random($len = 10): string
|
|
{
|
|
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789';
|
|
$chars = str_repeat($chars, 4);
|
|
$chars = str_shuffle($chars);
|
|
$str = substr($chars, 0, $len);
|
|
return $str;
|
|
}
|
|
|
|
public function getCode(Cdkey $cdkey)
|
|
{
|
|
$user = $this->user;
|
|
$URL = \App\Helpers\CDKEY::encode($cdkey->code);
|
|
|
|
$qrCode = QrCode::format('png')->size(235)->margin(0)->generate($URL);
|
|
$fileUrl = '/home/wwwroot/Wow/storage/app/public/headimgurl/' . $user->id . '.jpg';
|
|
if (!file_exists($fileUrl)) {
|
|
$headimgurl = file_get_contents($user->info->headimgurl);
|
|
file_put_contents($fileUrl, $headimgurl);
|
|
}
|
|
|
|
$headimg = self::getCircle(Image::make('storage/headimgurl/' . $user->id . '.jpg'));
|
|
$image = Image::make('img/share_bg2018.jpg'); //获取背景图片
|
|
// ->insert(sprintf('%010d', $cdkey->id), 'bottom-left', 314, 200)
|
|
$image->text('VIP' . sprintf('%09d', $cdkey->id), 270, 880, function ($font) {
|
|
$font->file('fonts/yahei.ttf')->color('#333')->size(30);
|
|
});
|
|
return $image
|
|
->insert($qrCode, 'top-left', 257, 610)
|
|
->insert($headimg, 'bottom-left', 314, 200)
|
|
->response('jpg');
|
|
}
|
|
|
|
public function getCircle($img)
|
|
{
|
|
$r = $img->width() / 2;
|
|
$new = Image::canvas(164, 164);
|
|
for ($x = 0; $x < $img->width(); $x++) {
|
|
for ($y = 0; $y < $img->height(); $y++) {
|
|
$c = $img->pickColor($x, $y, 'array');
|
|
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
|
|
$new->pixel($c, $x, $y);
|
|
}
|
|
}
|
|
}
|
|
return $new;
|
|
}
|
|
|
|
public function order()
|
|
{
|
|
$user = $this->user;
|
|
|
|
if ($user->identity->identity_id == 0) {
|
|
return $this->failed('请开通VIP后继续操作');
|
|
}
|
|
|
|
$order = KeysOrder::create([
|
|
'user_id' => $user->id,
|
|
'type' => '',
|
|
'amount' => 990,
|
|
]);
|
|
|
|
return $this->success(['orderid' => $order->id]);
|
|
|
|
}
|
|
|
|
public function giving(Request $request)
|
|
{
|
|
$user_id = $request->user_id ?? '';
|
|
if (!$user_id || !is_numeric($user_id)) {
|
|
return $this->failed('请输入正确的编号');
|
|
}
|
|
$user = User::find($user_id);
|
|
if (!$user || $user->identity->identity_id == 0) {
|
|
return $this->failed('用户不存在或不是会员');
|
|
}
|
|
$order = KeysOrder::create([
|
|
'user_id' => $user->id,
|
|
'type' => '',
|
|
'amount' => 990,
|
|
]);
|
|
return $this->success(['orderid' => $order->id, 'nickname' => $user->info->nickname, 'user_id' => $user->id]);
|
|
}
|
|
|
|
public function balance(Request $request, KeysOrder $order)
|
|
{
|
|
if ($this->user->paypass) {
|
|
if ($this->user->paypass !== md5($request->paypass)) {
|
|
return $this->failed('支付密码验证失败');
|
|
}
|
|
} else {
|
|
return $this->failed('请您先设置支付密码', route('settings.pwd'));
|
|
}
|
|
|
|
$order = KeysOrder::where('trade_no', $request->trade_no)->first();
|
|
if ($this->user->account->cash < $order->amount) {
|
|
return $this->failed('账户余额不足');
|
|
}
|
|
|
|
try {
|
|
DB::transaction(function () use ($order) {
|
|
$this->user->rule('balance_pay', -$order->amount, false, $order->trade_no);
|
|
$order->state = 'SUCCESS';
|
|
$order->paid_at = Carbon::now();
|
|
$order->save();
|
|
event(new \App\Events\KeysPaid($order));
|
|
});
|
|
} catch (\Exception $e) {
|
|
return $this->failed($e->getmessage());
|
|
}
|
|
return $this->success('支付成功');
|
|
}
|
|
|
|
public function wechat(Request $request, KeysOrder $order)
|
|
{
|
|
$user = $this->user;
|
|
$openid = $user->openid;
|
|
if ($user->main_id > 0) {
|
|
$openid = $user->mainuser->openid;
|
|
}
|
|
|
|
$app = app('wechat.payment');
|
|
$result = $app->order->unify([
|
|
'body' => '申请激活码',
|
|
'out_trade_no' => $order->trade_no,
|
|
'total_fee' => $order->amount * 100,
|
|
'notify_url' => route('notify.keys'),
|
|
'trade_type' => 'JSAPI',
|
|
'openid' => $openid,
|
|
]);
|
|
|
|
$json = $app->jssdk->bridgeConfig($result['prepay_id']);
|
|
|
|
return view('keys.pay', compact('order', 'json'));
|
|
}
|
|
|
|
} |