model = new \app\admin\model\lottery\Record; set_addon_config('epay', ['version' => 'v3'], false); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { $this->relationSearch = true; $this->searchFields = "user.nickname,lottery.name,prize.name,id"; if ($this->request->isAjax()) { list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $total = $this->model ->with(['lottery','prize','user']) ->where($where) ->order($sort, $order) ->count(); $list = $this->model ->with(['lottery','prize','user']) ->where($where) ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list, "extend" => ['money' => 1024, 'price' => 888]); return json($result); } return $this->view->fetch(); } public function deliver() { if (!$this->request->isAjax()) { return $this->view->fetch(); } $params = $this->request->param(); $id = $params['ids'] ?? 0; if (intval($id) <= 0){ $this->error(__('请上传正确的ID')); } // 查询第三方登录数据 $withdraw = $this->model->lock(true)->where('id', $id)->find(); if (!$withdraw) { $this->error(__('No Results were found')); } $objThird = Third::where('user_id', $withdraw->user_id) ->where('platform','wechat') ->where('apptype','mp') ->find(); if (empty($objThird)){ $this->error(__('用户三方数据不存在')); } Db::startTrans(); try { $withdraw = $this->handleWithdraw($withdraw,$objThird); Db::commit(); } catch (HttpResponseException $e) { $data = $e->getResponse()->getData(); $message = $data ? ($data['msg'] ?? '') : $e->getMessage(); $this->error($message); } catch (\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('处理成功'); } // 处理打款 public function handleWithdraw($withdraw, $objThird) { $withDrawStatus = false; if ($withdraw->is_deliver == 1) { $this->error('请勿重复操作'); } $withDrawStatus = $this->handleTransfer($withdraw,$objThird); if ($withDrawStatus) { $withdraw->is_deliver = 1; $withdraw->deliver_time = time(); $withdraw->save(); return $this->handleLog($withdraw, '已打款'); } return $withdraw; } // 企业付款提现 private function handleTransfer($withdraw,$objThird) { $type = 'wechat'; $platform = 'WechatOfficialAccount'; $payService = new PayService($type, $platform); $sn = $this->get_sn($withdraw->user_id, 'W'); $emoji_pattern = "/[\x{1F600}-\x{1F64F}|[\x{1F300}-\x{1F5FF}\x{1F680}-\x{1F6FF}\x{2600}-\x{26FF}\x{2700}-\x{27BF}]/u"; // Emoji表情Unicode范围 $objThird->openname = preg_replace($emoji_pattern, '', $objThird->openname); $payload = [ 'out_batch_no' => $sn, 'batch_name' => '商家转账到零钱', 'batch_remark' => "用户[" . ($objThird->openname ?? '') . "]中奖红包", //'total_amount' => $withdraw->amount, 'total_amount' =>$withdraw->num, 'total_num' => 1, 'transfer_detail_list' => [ [ 'out_detail_no' => $sn, //'transfer_amount' => $withdraw->amount, 'transfer_amount' => $withdraw->num, 'transfer_remark' => "用户[" . ($objThird->openname ?? '') . "]中奖红包", 'openid' => $objThird->openid?? '', //'user_name' => $objThird->openname ?? '', ], ], ]; try { list($code, $response) = $payService->transfer($payload); Log::write('transfer-origin-data:' . json_encode($response)); if ($code === 1) { $withdraw->deliver_info = json_encode($response, JSON_UNESCAPED_UNICODE); return true; } throw new RedbagException(json_encode($response, JSON_UNESCAPED_UNICODE)); } catch (HttpResponseException $e) { $data = $e->getResponse()->getData(); $message = $data ? ($data['msg'] ?? '') : $e->getMessage(); Log::write('http-error-reponData:' .$message); throw new RedbagException($message); } catch (\Exception $e) { \think\Log::error('抽奖发红包失败:' . ' 行号:' . $e->getLine() . '文件:' . $e->getFile() . '错误信息:' . $e->getMessage()); $this->handleLog($withdraw, '抽奖发红包失败:' . $e->getMessage()); throw new RedbagException($e->getMessage()); } return false; } private function handleLog($withdraw, $oper_info) { $oper = $this->auth->id; RedbagLog::insert([ 'lottery_record_id' => $withdraw->id, 'content' => $oper_info, 'oper_type' => 'admin', 'oper_id' => $oper, 'createtime' => time() ]); return $withdraw; } /** * 获取唯一编号 * * @param mixed $id 唯一标识 * @param string $type 类型 * @return string */ public function get_sn($id, $type = '') { $id = (string)$id; $rand = $id < 9999 ? mt_rand(100000, 99999999) : mt_rand(100, 99999); $sn = date('Yhis') . $rand; $id = str_pad($id, (24 - strlen($sn)), '0', STR_PAD_BOTH); return $type . $sn . $id; } }