388 lines
14 KiB
PHP
388 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Server\SaasMemberVipGain;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\FellowingCard as Card;
|
|
use App\Models\FellowingCardHistory;
|
|
use App\Models\Rank;
|
|
use App\Models\RankHistory;
|
|
use App\Models\User;
|
|
use App\Models\ProfileCourtship;
|
|
use App\Models\Wechat;
|
|
use App\Contracts\OrderContract;
|
|
use App\Models\Configs;
|
|
use Illuminate\Support\Facades\Redis;
|
|
class FellowingCardController extends Controller
|
|
{
|
|
|
|
protected $orderCon;
|
|
public function __construct(OrderContract $orderCon){
|
|
$this->orderCon = $orderCon;
|
|
}
|
|
/**
|
|
* 团契会员卡详情(指定人可看)
|
|
* @param Request $request [description]
|
|
* @param [type] $card_id [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function fellowingCard(Request $request, $card_id)
|
|
{
|
|
$user = auth()->user();
|
|
$card = Card::findOrFail($card_id);
|
|
$card->used_num = $card->num - $card->remain_num;
|
|
//指定人信息
|
|
// $from_openid = $request->input('from_openid');
|
|
$share_user = null;
|
|
// if ($from_openid) {
|
|
// $from_wechat = Wechat::where('openid', $from_openid)->first();
|
|
// if (!empty($from_wechat)) {
|
|
if ($card->share_user_id) {
|
|
$share_user = User::where('id', $card->share_user_id)->select('id', 'name', 'mobile', 'sex', 'type', 'circle_avatar')->first();
|
|
}else{
|
|
$share_user = User::where('id', $card->user_id)->select('id', 'name', 'mobile', 'sex', 'type', 'circle_avatar')->first();
|
|
}
|
|
// if (!empty($share_user)) {
|
|
// $share_user->avatar = $from_wechat->avatar2?$from_wechat->avatar2:$from_wechat->avatar;
|
|
// }
|
|
// }
|
|
// }
|
|
$card->share_user = $share_user;
|
|
$is_share_user = 1;
|
|
if ($card->share_user_id && $user->id != $card->share_user_id) {
|
|
$is_share_user = 0;
|
|
}
|
|
$card->is_share_user = $is_share_user;
|
|
$is_gained = 0;
|
|
//是否已领取
|
|
$history = FellowingCardHistory::where('user_id', $user->id)->where('card_id', $card_id)->first();
|
|
if ($history) {
|
|
$is_gained = 1;
|
|
}
|
|
$card->is_gained = $is_gained;
|
|
$card->is_admin = $user->is_admin;
|
|
$histories = $this->fellowingCardHistories($card_id);
|
|
$card->histories = $histories;
|
|
//会员名称
|
|
$rank_name = Rank::where('id', $card->rank_id)->value('name');
|
|
$card->rank_name = $rank_name;
|
|
|
|
return $this->success('ok', $card);
|
|
}
|
|
|
|
/**
|
|
* 领取页面展示
|
|
* @return [type] [description]
|
|
*/
|
|
public function showFellowingCard(Request $request, $card_id)
|
|
{
|
|
//\Log::info('团契卡');
|
|
//\Log::info($request->input('from_user_id').'---'.$request->input('from_openid'));
|
|
$card = Card::findOrFail($card_id);
|
|
$user_id = auth()->id();
|
|
if (empty($user_id)) {
|
|
$user = $this->authCheck();
|
|
if ($user) {
|
|
$user_id = $user->id;
|
|
}
|
|
}
|
|
$history_count = FellowingCardHistory::where('user_id', $user_id)->where('card_id', $card_id)->count();
|
|
$card->history_count = $history_count?:0;
|
|
//会员名称
|
|
$rank_name = Rank::where('id', $card->rank_id)->value('name');
|
|
$card->rank_name = $rank_name;
|
|
if($request->number){
|
|
$has_receive = Redis::SISMEMBER('fellowinf_card_number', $request->number);
|
|
if($has_receive){
|
|
$card->remain_num = 0;
|
|
}
|
|
}
|
|
//特权
|
|
$value_json = Configs::where('key', 'fellowing_card_privileges')->value('value');
|
|
$privileges = json_decode($value_json, true);
|
|
$card->privileges = $privileges;
|
|
//是否展示图片
|
|
$card->is_show_image = (int)Configs::where('key', 'fellowing_card_is_show_image')->value('value')?:0;
|
|
//会员时间文字
|
|
if ($card->month) {
|
|
$month_text = $card->month.'个月';
|
|
$day_text = $card->day?'+'.$card->day.'天':'';
|
|
}else {
|
|
$month_text = '';
|
|
$day_text = $card->day?$card->day.'天':'';
|
|
}
|
|
$card_text = $month_text.$day_text.'尊享';
|
|
$card->card_text = $card_text;
|
|
//卡片图片
|
|
$card->card_image = Configs::where('key', 'fellowing_card_image')->value('value');
|
|
return $this->success('ok', $card);
|
|
}
|
|
|
|
/**
|
|
* 团契会员卡详情(成员可见)
|
|
* @param Request $request [description]
|
|
* @param [type] $card_id [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function fellowingCardForMember(Request $request, $card_id)
|
|
{
|
|
$user = auth()->user();
|
|
dd($user);
|
|
$card = Card::findOrFail($card_id);
|
|
$card->used_num = $card->num - $card->remain_num;
|
|
//指定人信息
|
|
$from_openid = $request->input('from_openid');
|
|
$share_user = null;
|
|
if ($from_openid) {
|
|
$frome_wechat = Wechat::where('openid', $from_openid)->first();
|
|
if (!empty($from_wechat)) {
|
|
$share_user = User::where('id', $from_wechat->user_id)->select('id', 'name', 'mobile', 'sex', 'type', 'circle_avatar')->first();
|
|
$share_user->avatar = $from_wechat->avatar2?$from_wechat->avatar2:$from_wechat->avatar;
|
|
}
|
|
}
|
|
$card->share_user = $share_user;
|
|
//是否已领取
|
|
$history = FellowingCardHistory::where('user_id', $user->id)->where('card_id', $card_id)->first();
|
|
$is_gained = 0;
|
|
if ($history) {
|
|
$is_gained = 1;
|
|
}
|
|
$card->is_gained = $is_gained;
|
|
$histories = $this->fellowingCardHistories($card_id);
|
|
$card->histories = $histories;
|
|
return $this->success('ok', $card);
|
|
}
|
|
|
|
public function fellowingCardHistories($card_id)
|
|
{
|
|
$card = Card::findOrFail($card_id);
|
|
$histories = FellowingCardHistory::with('user')->where('card_id', $card_id)->orderBy('id', 'desc')->paginate();
|
|
return $histories;
|
|
}
|
|
|
|
/**
|
|
* 领取会员卡
|
|
* @param Request $request [description]
|
|
* @param [type] $card_id [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function gainFellowingCard(Request $request, $card_id)
|
|
{ /**
|
|
* result
|
|
* 0 成功
|
|
* 1 领取完
|
|
* 2 已领取过
|
|
*/
|
|
$card = Card::findOrFail($card_id);
|
|
$result = 0;
|
|
$user = auth()->user();
|
|
if (empty($user)) {
|
|
$user = $this->authCheck();
|
|
}
|
|
//判断该二维码是否被领取
|
|
if($request->number){
|
|
$has_receive = Redis::SISMEMBER('fellowinf_card_number', $request->number);
|
|
if($has_receive){
|
|
return $this->failure('该奖励已被领取');
|
|
}
|
|
}
|
|
if (empty($user)) {//未注册
|
|
$openid = $request->input('openid');
|
|
if (empty($openid)) {
|
|
return $this->failure('服务错误,请重试');
|
|
}
|
|
$history = FellowingCardHistory::where('openid', $openid)->where('card_id', $card_id)->first();
|
|
if ($history) {
|
|
$result = 2;
|
|
return $this->success('ok', compact('result'));
|
|
}
|
|
//生成领取记录
|
|
$history = new FellowingCardHistory();
|
|
$history->card_id = $card_id;
|
|
$history->openid = $openid;
|
|
$history->save();
|
|
$card->decrement('remain_num', 1);
|
|
}else{
|
|
//介绍人不能领取
|
|
if ($user->type != "single") {
|
|
return $this->failure('介绍人不需要会员');
|
|
}
|
|
$user_id = $user->id;
|
|
//是否领取完
|
|
if (empty($card->remain_num)) {
|
|
$result = 1;
|
|
return $this->success('ok', compact('result'));
|
|
}
|
|
//检查是够已领取
|
|
$history = FellowingCardHistory::where('user_id', $user_id)->where('card_id', $card_id)->first();
|
|
if ($history) {
|
|
$result = 2;
|
|
return $this->success('ok', compact('result'));
|
|
}
|
|
//生成领取记录
|
|
$history = new FellowingCardHistory();
|
|
$history->user_id = $user_id;
|
|
$history->card_id = $card_id;
|
|
$history->save();
|
|
$month = $card->month;
|
|
$day = $card->day;
|
|
$user->addSuperRank($day, $month, 'card');
|
|
//赠送认证次数
|
|
if (empty($user->is_approve)) {
|
|
User::where('id', $user->id)->update(['approve_time'=>3]);
|
|
}
|
|
//更新会员卡剩余数
|
|
$card->decrement('remain_num', 1);
|
|
}
|
|
if($request->number){
|
|
Redis::sadd('fellowinf_card_number', $request->number);
|
|
}
|
|
//saas天路合一购买会员赠送福恋小程序vip 领取状态更改
|
|
if($result === 0){
|
|
$where = [
|
|
['type_id','=',$card_id],
|
|
['type','=','vip']
|
|
];
|
|
$saas_vip_gain = SaasMemberVipGain::where($where)->first();
|
|
if($saas_vip_gain){
|
|
$data = [
|
|
'status' => 1,
|
|
'receive_datetime' => date('Y-m-d H:i:s')
|
|
];
|
|
SaasMemberVipGain::where($where)->update($data);
|
|
}
|
|
}
|
|
return $this->success('ok', compact('result'));
|
|
}
|
|
|
|
/**
|
|
* 团契会员卡列表
|
|
* @param Request $request [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function fellowingCards(Request $request)
|
|
{
|
|
$user = auth()->user();
|
|
$cards = Card::where('user_id', $user->id)->orderBy('id', 'desc')->paginate();
|
|
return $this->success('ok', $cards);
|
|
}
|
|
|
|
/**
|
|
* 添加团契会员卡
|
|
* @param Request $request [description]
|
|
*/
|
|
public function addFellowingCard(Request $request)
|
|
{
|
|
$user = auth()->user();
|
|
$user_id = $user->id;
|
|
$share_user_id = $request->input('share_user_id', 0);
|
|
$name = $request->input("name");
|
|
if (empty($name)) {
|
|
return $this->failure('请输入团契会员卡名称');
|
|
}
|
|
$num = $request->input("num", 0);
|
|
if (empty($num)) {
|
|
return $this->failure('请输入团契会员卡数量');
|
|
}
|
|
$remain_num = $num;
|
|
$rank_id = $request->input('rank_id', 0);
|
|
if (empty($rank_id)) {
|
|
return $this->failure('请选择会员级别');
|
|
}
|
|
$month = $request->input('month', 0);
|
|
if (empty($month)) {
|
|
return $this->failure('请选择会员期限');
|
|
}
|
|
$card = new Card();
|
|
$card->user_id = $user_id;
|
|
$card->share_user_id = $share_user_id;
|
|
$card->name = $name;
|
|
$card->num = $num;
|
|
$card->remain_num = $remain_num;
|
|
$card->rank_id = $rank_id;
|
|
$card->month = $month;
|
|
$card->save();
|
|
return $this->success('ok', $card);
|
|
}
|
|
|
|
public function makerQrcode($card_id, $avatar, $name, $rank_id, $num)
|
|
{
|
|
$app = \EasyWechat::miniProgram();
|
|
$data = [];
|
|
$data['is_hyaline'] = true;
|
|
$response = $app->app_code->get('/pages/users/register?card_id='.$card_id, $data);
|
|
$time = time();
|
|
$path = $time.'card_qrcode.png';
|
|
$filename = $response->saveAs(storage_path('qrcode'), $path);
|
|
$image1 = 'http://images.ufutx.com/201807/16/6826a6e371cc6a38e99217c7bb3e3449.jpeg';
|
|
$image2 = storage_path('qrcode/'.$path);
|
|
$new_image = $this->imageAddImage($image1, $image2, 371, 371, 208, 818, 'top-leftt');
|
|
//头像
|
|
if ($avatar) {
|
|
$new_image = $this->imageAddImage($new_image, $avatar, 183, 183, 302, 75, 'top-left');
|
|
}
|
|
// $new_image = $this->textAddImage($new_image, $name, 183, 183, 302, 75, 'top-left');
|
|
$new_image = $this->textAddImage($new_image, $name, 30, 393.5, 276, '#001100', 'center');
|
|
$file_path = storage_path("/qrcode/".$time."invite_qrcode.png");
|
|
$new_image->save($file_path);
|
|
$pic = '';
|
|
if(file_exists($file_path)){
|
|
$pic = $this->uploadFile($file_path);
|
|
try{
|
|
unlink($file_path);
|
|
|
|
}catch(Exception $e) {
|
|
return $this->failure($e->getMessage());
|
|
exit();
|
|
}
|
|
if (file_exists($path)) {
|
|
unlink($path);
|
|
}
|
|
}
|
|
|
|
return $pic;
|
|
}
|
|
|
|
/**
|
|
* 修改团契会员卡
|
|
* @param Request $request [description]
|
|
* @param [type] $card_id [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function updateFellowingCard(Request $request, $card_id)
|
|
{
|
|
$card = Card::findOrFail($card_id);
|
|
if ($request->has('share_user_id') && $request->input('share_user_id') != $card->share_user_id) {
|
|
$card->share_user_id = $request->share_user_id;
|
|
}
|
|
if ($request->has('name') && $request->input('name') != $card->name) {
|
|
$card->name = $request->name;
|
|
}
|
|
// if ($request->has('num') && $request->input('num') != $card->num) {
|
|
// $card->num = $request->num;
|
|
// }
|
|
if ($request->has('rank_id') && $request->input('rank_id') != $card->rank_id) {
|
|
$card->rank_id = $request->rank_id;
|
|
}
|
|
if ($request->has('month') && $request->input('month') != $card->month) {
|
|
$card->month = $request->month;
|
|
}
|
|
$card->save();
|
|
return $this->success('ok', $card);
|
|
}
|
|
|
|
/**
|
|
* 删除团契会员卡
|
|
* @param Request $request [description]
|
|
* @param [type] $card_id [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function deleteFellowingCard(Request $request, $card_id)
|
|
{
|
|
$card = Card::findOrFail($card_id);
|
|
$card->delete();
|
|
return $this->success('ok');
|
|
}
|
|
}
|