input('type_id'); $type = $request->input('type'); $level_id = $request->input('level_id'); $merchant_user_id = $request->merchant_user_id; $merchant_id = $request->merchant_id; if (!$type_id) { return $this->failure(); } if (!$type) { return $this->failure(); } if (!$level_id) { return $this->failure(); } $shop_data = []; if ($type == 'shop') { $name = $request->input('name') ?? ''; $mobile = $request->input('mobile') ?? ''; $address = $request->input('address') ?? ''; if (!$name || !$mobile || !$address) { return $this->failure('商品收货参数数据不全'); } $shop_data = [ 'name' => $name, 'mobile' => $mobile, 'address' => $address, ]; } //检查是否购买VIP if (!$s1->checkBuyVip($merchant_id, $merchant_user_id, $level_id)) { return $this->failure('您未购买该VIP,无法领取'); } $order = $s1->getBuyVipOrder($merchant_id, $merchant_user_id, $level_id); $where = [ ['type', '=', $type], ['type_id', '=', $type_id], ['level_id', '=', $level_id], // ['status', '=', 0], ['merchant_user_id', '=', $merchant_user_id], ['order_id', '=', $order->id] ]; $data = SaasMemberVipGain::where($where)->first(); $status = $data->status ?? 0; if ($status == 1) { return $this->failure('该权益已领取'); } DB::beginTransaction(); if (!$data) { if (!$order) return $this->failure('购买订单未找到'); $extend = json_decode($order->extend); $data = new SaasMemberVipGain(); $data->merchant_user_id = $merchant_user_id; $data->merchant_id = $merchant_id; $data->spread_merchant_id = $extend->spread_merchant_id ?? 0; $data->level_id = $level_id; $data->type = $type; $data->type_id = $type_id; $data->order_id = $order->id; } $data->status = 1; $data->receive_datetime = date('Y-m-d H:i:s'); $data->save(); //免费购买对应类目 创建免费订单 $data = [ 'type_id' => $type_id, 'merchant_user_id' => $merchant_user_id, 'spread_merchant_id' => $data->spread_merchant_id, 'level_id' => $level_id, 'order_id' => $data->order_id, 'shop_data' => $shop_data ]; $s2->useGain($type, $data); DB::commit(); return $this->success('领取成功'); } catch (\Exception $e) { DB::rollBack(); $this->getError($e); return $this->failure($e->getMessage()); } } /** * 跳转到福恋小程序领取vip的短链 * @param Request $request * @param SaasVipService $s1 * @return \Illuminate\Http\JsonResponse|void */ public function appletShortLink(Request $request, SaasVipService $s1) { try { $level_id = $request->input('level_id'); $merchant_user_id = $request->merchant_user_id; $merchant_id = $request->merchant_id; if (!$level_id) { return $this->failure($level_id); } //检查是否购买VIP if (!$s1->checkBuyVip($merchant_id, $merchant_user_id, $level_id)) { return $this->failure('您未购买该VIP,无法领取'); } $order = $s1->getBuyVipOrder($merchant_id, $merchant_user_id, $level_id); $where = [ ['type', '=', 'vip'], ['level_id', '=', $level_id], ['merchant_user_id', '=', $merchant_user_id], ['order_id', '=', $order->id] ]; $data = SaasMemberVipGain::where($where)->first(); if (!$data) { if (!$order) return $this->failure('购买订单未找到'); $extend = json_decode($order->extend); DB::beginTransaction(); $card = new FellowingCard(); $card->share_user_id = 0; $card->user_id = 69938; $card->name = '【天路合一】购买会员权益系统自动赠送:' . $order->account_id; $card->num = 1; $card->remain_num = 1; $card->rank_id = 9; $card->month = 1; $card->day = 0; $card->save(); $data = new SaasMemberVipGain(); $data->merchant_user_id = $merchant_user_id; $data->merchant_id = $merchant_id; $data->spread_merchant_id = $extend->spread_merchant_id ?? 0; $data->level_id = $level_id; $data->type = 'vip'; $data->type_id = $card->id; $data->order_id = $order->id; $data->save(); DB::commit(); } $card = FellowingCard::find($data->type_id); if (!$card->remain_num) { return $this->failure('已领取啦'); } $cache_key = "saas:give_vip_short_link:level_id{$level_id}_m_user_id{$merchant_user_id}"; $url_link = Cache::get($cache_key); if (empty($url_link)) { // $app = \EasyWeChat::miniProgram(); // $token = $app->access_token->getToken(); // $access_token = $token['access_token']; $access_token = ""; $resp = Http::get("https://love.ufutx.com/go/api/mp/access_token?token=go_love"); if ($resp) { if ($resp) { $res = json_decode($resp, true); $access_token = $res["data"]['access_token']; } } if (empty($access_token)) { return $this->failure("缺少access_token"); } $url = "https://api.weixin.qq.com/wxa/generate_urllink?access_token=" . $access_token; $data = [ 'path' => 'pages/users/groupMember', 'query' => "id={$data->type_id}", 'expire_time' => time() + (60 * 60 * 24) //有效期一天 ]; $result = Http::post($url, json_encode($data, JSON_UNESCAPED_UNICODE)); $result = json_decode($result, true); $code = $result['errCode'] ?? ''; $url_link = $result['url_link'] ?? ''; if ($code != 0 && empty($url_link)) return $this->failure('获取跳转小程序短链失败'); Cache::put($cache_key, $url_link, 60 * 24); //有效期一天 } return $this->success('ok', compact('url_link')); } catch (\Exception $e) { $this->getError($e); return $this->failure($e->getMessage()); } } }