participant = $array['participant']; $this->packet = $array['packet']; $container = new Container(); $this->sms = new Sms($container); $this->mobile = '15872844805'; } /** * Execute the job. * * @return void */ public function handle() { try { //更新数据 $packet = $this->updateData(); //发红包 $this->sendRedPacket($packet); } catch (\Exception $e) { $code = $e->getCode(); if ($code == 1) {//微信转账失败 //TODO通知管理员 $message = "用户【".$this->participant->nickname."】领取终极大奖失败,原因:".$e->getMessage(); $this->sms->sentMessage($this->mobile, $message); } } } public function sendRedPacket($packet) { //红包金额 $price = $packet->price; $amount = $packet->price * 100; $desc = '九果终极红包'; //订单号 $trade_no = \CommonUtilsService::getTradeNO(); $packet->trade_no = $trade_no; $packet->save(); $red_packet_data = [ 'official_openid'=>$this->participant->official_openid, 'type'=>'FRUIT', 'trade_no'=>$trade_no, 'ip'=>$this->participant->ip, 'amount'=>$this->packet->price, 'trigged' => 0, 'created_at'=>date('Y-m-d H:i:s'), 'updated_at'=>date('Y-m-d H:i:s'), ]; RedPacketOrder::insert($red_packet_data); $official_openid = $this->participant->official_openid; //触发红包 $result = \WechatService::officialUserTransfer($trade_no, $official_openid, $amount, $desc); if ($result) {//转账红包失败 $return_msg = $result['return_msg']; $packet->err_message = $return_msg; $packet->is_hook = -1; $packet->save(); //抛出错误 throw new \Exception("微信转账失败", 1); }else{ $packet->is_hook = 1; $packet->save(); } } /** * 更新数据 */ public function updateData() { // \DB::beginTransaction(); //查询 try { $packet = $this->packet; //记录 $packet->participant_id = $this->participant->id; $packet->save(); $this->participant->is_final_pick_up = 1; $this->participant->save(); //更新缓存 $key = Participant::PART_KEY; $key = $key . $this->participant->official_openid; Cache::forever($key, $this->participant); // \DB::commit(); return $packet; }catch (\Exception $e) { // \DB::rollBack(); return $packet; } } }