request->param(); $data = []; if (isset($getData['name']) && !empty($getData['name'])) { $data[] = ['name', 'like', "%{$getData['name']}%"]; } $user = $this->store['user']; /*if($user['is_super'] == 0) { $data[] = ['store_user_id', 'in', User::getScope($user)]; }*/ if(!empty($user['merchant_id']) && $user['is_super'] == 0){ $data[] = function ($query) use ($user) { $query->where('merchant_id', '=', $user['merchant_id']) ->whereOr('parent_merchant_id', '=', $user['merchant_id']); }; } $list = $model->getList($data)->toArray(); $newList = []; $MerchantOrderModel = new MerchantOrderModel(); $date = date("Y-m-d"); foreach ($list['data'] as $val){ $val['day_money'] =$MerchantOrderModel->getOrderTotalPrice($date, $date, $val); $val['total_money'] =$MerchantOrderModel->getOrderTotalPrice(null, null, $val); $newList[] = $val; } $list['data'] = $newList; return $this->renderSuccess(['list'=>$list,'user'=>$this->store['user']]); } /** * 获取所有商户 * @return mixed * @throws \think\exception\DbException */ public function all(): Json { $user = $this->store['user']; $list = MerchantModel::getAll($user); return $this->renderSuccess(compact('list','user')); } /** * 添加商户 * @return array|mixed * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function add() { $model = new MerchantModel; $data = $this->postForm(); // 新增记录 if ($model->add($data,$this->store['user'])) { return $this->renderSuccess('添加成功'); } return $this->renderError($model->getError() ?: '添加失败'); } /** * 商户详情 * @param int $merchantId * @return Json * @throws BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \cores\exception\BaseException */ public function detail(int $merchantId): Json { $merchantInfo = MerchantModel::detail($merchantId); return $this->renderSuccess(compact('merchantInfo')); } /** * 编辑商户 * @param int $merchantId * @return Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \cores\exception\BaseException */ public function edit(int $merchantId): Json { // 商品详情 $model = MerchantModel::detail($merchantId); $data = $this->postForm(); // 更新记录 if ($model->edit($data)) { return $this->renderSuccess('更新成功'); } return $this->renderError($model->getError() ?: '更新失败'); } /** * 用户充值 * @param int $merchantId * @param string $target * @return array */ public function recharge(int $merchantId, string $target) { // 用户详情 $model = MerchantModel::detail($merchantId); if ($model->recharge($target, $this->postForm())) { return $this->renderSuccess('操作成功'); } return $this->renderError($model->getError() ?: '操作失败'); } /** * 删除商户 * @return Json * @throws \think\exception\DbException */ public function delete(): Json { $arr = $this->postData(); $userModel = new User(); foreach ((array)$arr as $val){ $model = MerchantModel::detail($val); if ($model->setDelete()) { $userModel->where([ ['level','=',1], ['merchant_id','like','%'.$val.'%'], ])->delete(); } } return $this->renderSuccess('删除成功'); } /** * 获取推广码 * @param int $merchantId * @return Json * @throws BaseException */ public function qrCode(int $merchantId): Json { // 生成二维码 try { $file = public_path().'uploads/qrcode/'.$merchantId.'.png'; if(!file_exists($file)) { // 小程序配置信息 $wxConfig = WxappSettingModel::getWxappConfig(); // 请求API数据 $WechatQrcodeApi = new WechatQrcodeApi($wxConfig['app_id'], $wxConfig['app_secret']); $data = $WechatQrcodeApi->getQrcode('uid:'.$merchantId, 'pages/category/index'); // pages/index/index file_put_contents($file, $data); // $base64Image = base64_encode($data); $this->createShare($merchantId); } $fp = fopen($file,"rb"); $gambar = fread($fp,filesize($file)); fclose($fp); $base64Image = base64_encode($gambar); // 生成 data URI $dataUri = 'data:image/png;base64,' . $base64Image; // 返回生成的二维码链接 return $this->renderSuccess(['data'=>$dataUri]); } catch (\Exception $e) { return $this->renderError($e->getMessage()); } } public function createShare($merchantId){ $model = MerchantModel::detail($merchantId); // 设置路径 $bgPath = public_path().'assets/store/img/qrcode/bg.jpg'; // 背景图路径 $qrPath = public_path().'uploads/qrcode/'.$merchantId.'.png'; // 小程序二维码路径 // $outputPath = public_path().'uploads/qrcode/share_image.jpg'; // 输出路径 $text = $model['name']; // 要写入的文字 // 创建背景图资源 $bg = imagecreatefromjpeg($bgPath); // 创建二维码图资源(可能是 png) $qr = imagecreatefromjpeg($qrPath); // 如果是 png 用 imagecreatefrompng // 获取背景图尺寸 $bgWidth = imagesx($bg); $bgHeight = imagesy($bg); // 设置二维码尺寸和位置(例如宽高 200px,放在右下角) $qrSize = 200; $qrResized = imagecreatetruecolor($qrSize, $qrSize); imagecopyresampled($qrResized, $qr, 0, 0, 0, 0, $qrSize, $qrSize, imagesx($qr), imagesy($qr)); // 合并二维码到背景图(右下角,留边距 20px) imagecopy($bg, $qrResized, $bgWidth - $qrSize, $bgHeight - $qrSize, 0, 0, $qrSize, $qrSize); // 添加文字(使用 TrueType 字体) $fontPath = public_path().'assets/store/fonts/sourcehansans.otf'; // 微软雅黑等 TTF 字体路径 $fontSize = 20; // 字号 $fontColor = imagecolorallocate($bg, 255, 255, 255); // 黑色 // 写文字(底部中间) $textBox = imagettfbbox($fontSize, 0, $fontPath, $text); $textWidth = $textBox[2] - $textBox[0]; $textX = (int)(($bgWidth - $textWidth) / 2); $textY = (int)($bgHeight - 20); imagettftext($bg, $fontSize, 0, $textX, 60, $fontColor, $fontPath, $text); // 保存最终图像 imagejpeg($bg, $qrPath, 90); // 释放资源 imagedestroy($bg); imagedestroy($qr); imagedestroy($qrResized); } /** * 代理商发券 * @param int $merchantId * @return array */ public function sendCoupon(int $merchantId) { // 用户详情 $model = MerchantModel::detail($merchantId); if ($model->sendCoupon($this->postForm())) { return $this->renderSuccess('操作成功'); } return $this->renderError($model->getError() ?: '操作失败'); } }