From 6288bb8ecc8c62d53589405063d6550fd57fbca6 Mon Sep 17 00:00:00 2001 From: xucong <850806214@qq.com> Date: Sat, 25 Apr 2026 15:46:31 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/logic/setting/BasicLogic.php | 4 +- app/api/controller/Distribution.php | 14 ++- app/api/logic/DistributionLogic.php | 122 ++++++++++++++++++++++++- 3 files changed, 134 insertions(+), 6 deletions(-) diff --git a/app/admin/logic/setting/BasicLogic.php b/app/admin/logic/setting/BasicLogic.php index a723ea0c..b0bd1957 100644 --- a/app/admin/logic/setting/BasicLogic.php +++ b/app/admin/logic/setting/BasicLogic.php @@ -176,8 +176,8 @@ class BasicLogic extends Logic try { $width = 430; // 'env_version' => 'trial', - $envVersion = 'develop'; -// $envVersion = 'release'; +// $envVersion = 'develop'; + $envVersion = 'release'; $page = "bundle/pages/user_spread/user_spread"; $save_dir = public_path() . 'uploads/wxqrcode/'; // 确保目录存在 diff --git a/app/api/controller/Distribution.php b/app/api/controller/Distribution.php index f0f0f966..f85e2b37 100644 --- a/app/api/controller/Distribution.php +++ b/app/api/controller/Distribution.php @@ -107,7 +107,19 @@ class Distribution extends Api } return JsonServer::error(DistributionLogic::getError(), [], 0, 0); } - + /** + * 填写邀请码 + */ + public function wxCode() + { + $user_id = $this->user_id; + $post = $this->request->post(); + $result = DistributionLogic::wxCode($post,$user_id); + if($result) { + return JsonServer::success('绑定上级成功'); + } + return JsonServer::error(DistributionLogic::getError(), [], 0, 0); + } /** * 分销订单 */ diff --git a/app/api/logic/DistributionLogic.php b/app/api/logic/DistributionLogic.php index fc3f91b6..4cbc87d6 100644 --- a/app/api/logic/DistributionLogic.php +++ b/app/api/logic/DistributionLogic.php @@ -36,6 +36,9 @@ use app\common\server\UrlServer; use app\common\basics\Logic; use app\common\logic\AccountLogLogic; use app\common\model\AccountLog; +use app\common\server\WeChatServer; +use EasyWeChat\Factory; +use EasyWeChat\Kernel\Http\StreamResponse; use think\Exception; use think\facade\Db; @@ -90,6 +93,7 @@ class DistributionLogic extends Logic */ public static function apply($post) { + $time = time(); $data = [ 'user_id' => $post['user_id'], @@ -101,9 +105,12 @@ class DistributionLogic extends Logic 'reason' => $post['reason'], 'status' => 0, // 待审核 'create_time' => $time, - 'update_time' => $time, + 'update_time' => $time ]; - return DistributionMemberApply::create($data); + $result = DistributionMemberApply::create($data); + $url = self::makeMpWechatQrcode($post['user_id']); + User::update(['url'=>$url],['id'=>$post['user_id']]); + return $result; } /** * 申请分销会员 @@ -234,7 +241,7 @@ class DistributionLogic extends Logic */ public static function myLeader($userId) { - $field = 'nickname,avatar,is_distribution,mobile,first_leader,distribution_code,earnings'; + $field = 'url,nickname,avatar,is_distribution,mobile,first_leader,distribution_code,earnings'; $user = User::field($field)->where(['id' => $userId, 'del'=>0])->findOrEmpty(); @@ -354,7 +361,52 @@ class DistributionLogic extends Logic return false; } } + /** + * 扫码微信二维码 + */ + public static function wxCode($post,$user_id) + { + try { + Db::startTrans(); + $firstLeader = User::field(['id', 'first_leader', 'second_leader', 'third_leader', 'ancestor_relation','user_integral']) + ->where(['id' => $post['user_id']]) + ->findOrEmpty(); + if($firstLeader->isEmpty()) { + throw new \think\Exception('无效的二维码'); + } + $user = User::where(['user_id' => $user_id]) + ->find(); + if($user['first_leader'] != 0) { + throw new \think\Exception('已扫码'); + } + // 上级 + $first_leader_id = $firstLeader['id']; + $data = [ + 'first_leader' => $first_leader_id, + 'update_time' => time() + ]; + + // 更新当前用户的分销关系 + User::where(['id' => $post['user_id']])->update($data); + //通知用户 + event('Notice', [ + 'scene' => NoticeEnum::INVITE_SUCCESS_NOTICE, + 'params' => [ + 'user_id' => $first_leader_id, + 'lower_id' => $post['user_id'], + 'join_time' => date('Y-m-d H:i:s', time()) + ] + ]); + + Db::commit(); + return true; + } catch (\Exception $e) { + Db::rollback(); + self::$error = $e->getMessage(); + return false; + } + } /** * 分销订单 */ @@ -606,4 +658,68 @@ class DistributionLogic extends Logic return ['poster'=>$poster]; } + // 生成分销推广二维码 + public static function makeMpWechatQrcode($user_id, string $type = 'url', array $extra = []) + { + try { + $width = 430; + // 'env_version' => 'trial', + // $envVersion = 'develop'; + $envVersion = 'release'; + $page = "pages/index/index"; + $save_dir = public_path() . 'uploads/wxqrcode/'; + // 确保目录存在 + if (!is_dir($save_dir)) { + mkdir($save_dir, 0755, true); + } + $file_name = time() . '.png'; + $config = WeChatServer::getMnpConfig(); + $app = Factory::miniProgram($config); + + $response = $app->app_code->getUnlimit('user_id='.$user_id, [ +// 'scene' => 'type=1', + 'page' => $page, + 'width' => $width, + 'auto_color' => false, + 'line_color' => ['r' => 0, 'g' => 0, 'b' => 0], + 'is_hyaline' => false, + 'env_version' => $envVersion, + 'check_path' => false, + ]); + + if(is_array($response) && 41030 === $response['errcode']){ + + //开启错误提示,小程序未发布和页面不存在,返回提示 + if (41030 === $response['errcode']) { + return '所传page页面不存在,或者小程序没有发布'; + } + return $response['errmsg']; + } + + $contents = $response->getBody()->getContents(); + switch ($type){ + case 'file': + if ($response instanceof StreamResponse) { + $file_name = $response->saveAs($save_dir, $file_name); + $contents = $save_dir . $file_name; + } + break; + case 'url': // 保存并返回完整URL + $full_path = $save_dir . $file_name; + file_put_contents($full_path, $contents); + // 生成完整的可访问URL(根据实际域名调整) + $contents = request()->domain() . '/uploads/wxqrcode/' . $file_name; + break; + case 'base64': + $mp_base64 = chunk_split(base64_encode($contents)); + $contents = 'data:image/png;base64,' . $mp_base64; + break; + } + return data_success('',['qr_code'=>$contents, 'extra' => $extra]); + + + } catch (\EasyWeChat\Kernel\Exceptions\Exception $e){ + return data_error($e->getMessage()); + } + } } \ No newline at end of file