新增邀请码功能

This commit is contained in:
Billy 2025-10-29 12:01:14 +08:00
parent 178f5c87b4
commit f94089b566
9 changed files with 675 additions and 4 deletions

View File

@ -242,6 +242,9 @@ class Ccblife extends Common
} }
$user = Db::name('user')->where('id', $userId)->find(); $user = Db::name('user')->where('id', $userId)->find();
// 新用户注册后自动生成邀请码
$inviteCode = \addons\shopro\library\InviteHelper::generateCode($userId);
} }
} }

View File

@ -0,0 +1,91 @@
<?php
namespace addons\shopro\controller;
use addons\shopro\library\InviteHelper;
use app\admin\model\shopro\user\User as UserModel;
class Invite extends Common
{
protected $noNeedLogin = ['validate'];
protected $noNeedRight = ['*'];
/**
* 获取我的邀请码
*/
public function myCode()
{
$user = auth_user();
// 如果没有邀请码,自动生成
if (!$user->invite_code) {
$inviteCode = InviteHelper::generateCode($user->id);
} else {
$inviteCode = $user->invite_code;
}
// 统计邀请人数
$inviteCount = UserModel::where('parent_user_id', $user->id)->count();
$this->success('获取成功', [
'invite_code' => $inviteCode,
'invite_count' => $inviteCount,
'share_text' => "我的邀请码:{$inviteCode},下单时填写即可!"
]);
}
/**
* 验证邀请码
*/
public function validate()
{
$inviteCode = $this->request->param('invite_code', '');
if (empty($inviteCode)) {
$this->error('请输入邀请码');
}
$result = InviteHelper::validate($inviteCode);
if ($result['code'] != 1) {
$this->error($result['msg']);
}
$this->success('邀请码有效', $result['data']);
}
/**
* 绑定邀请码
*/
public function bind()
{
$user = auth_user();
$inviteCode = $this->request->param('invite_code', '');
if (empty($inviteCode)) {
$this->error('请输入邀请码');
}
$result = InviteHelper::bind($inviteCode, $user->id);
if ($result['code'] != 1) {
$this->error($result['msg']);
}
$this->success($result['msg'], $result['data']);
}
/**
* 我邀请的用户列表
*/
public function myInvites()
{
$user = auth_user();
$list = UserModel::where('parent_user_id', $user->id)
->field(['id', 'nickname', 'avatar', 'mobile', 'jointime', 'invite_code_used'])
->order('id desc')
->paginate($this->request->param('list_rows', 10));
$this->success('获取成功', $list);
}
}

View File

@ -0,0 +1,149 @@
<?php
namespace addons\shopro\library;
use app\admin\model\shopro\user\User as UserModel;
/**
* 邀请码工具类
*/
class InviteHelper
{
/**
* 为用户生成邀请码
* @param int $userId 用户ID
* @return string 邀请码
*/
public static function generateCode($userId)
{
$user = UserModel::find($userId);
if (!$user) {
return '';
}
// 已有邀请码直接返回
if ($user->invite_code) {
return $user->invite_code;
}
// 生成唯一邀请码(排除易混淆字符)
$chars = 'ABCDEFGHJKMNPQRSTUVWXYZ23456789';
$maxAttempts = 50;
for ($i = 0; $i < $maxAttempts; $i++) {
$code = '';
for ($j = 0; $j < 6; $j++) {
$code .= $chars[mt_rand(0, strlen($chars) - 1)];
}
// 检查是否重复
if (!UserModel::where('invite_code', $code)->find()) {
$user->invite_code = $code;
$user->save();
return $code;
}
}
// 兜底用MD5
$code = strtoupper(substr(md5($userId . time() . mt_rand()), 0, 6));
$user->invite_code = $code;
$user->save();
return $code;
}
/**
* 验证邀请码
* @param string $inviteCode 邀请码
* @return array
*/
public static function validate($inviteCode)
{
$inviteCode = strtoupper(trim($inviteCode));
if (strlen($inviteCode) !== 6) {
return ['code' => 0, 'msg' => '邀请码格式错误'];
}
$inviter = UserModel::where('invite_code', $inviteCode)
->where('status', 'normal')
->field(['id', 'nickname', 'avatar', 'invite_code'])
->find();
if (!$inviter) {
return ['code' => 0, 'msg' => '邀请码不存在'];
}
return [
'code' => 1,
'msg' => '邀请码有效',
'data' => [
'user_id' => $inviter->id,
'invite_code' => $inviter->invite_code,
'inviter' => [
'id' => $inviter->id,
'nickname' => $inviter->nickname,
'avatar' => $inviter->avatar
]
]
];
}
/**
* 绑定上下级关系
* @param string $inviteCode 邀请码
* @param int $userId 被邀请人ID
* @return array
*/
public static function bind($inviteCode, $userId)
{
$inviteCode = strtoupper(trim($inviteCode));
$user = UserModel::find($userId);
if (!$user) {
return ['code' => 0, 'msg' => '用户不存在'];
}
// 已绑定过上级
if ($user->parent_user_id !== null) {
return ['code' => 0, 'msg' => '您已有推荐人,无法修改'];
}
// 验证邀请码
$validateResult = self::validate($inviteCode);
if ($validateResult['code'] != 1) {
return $validateResult;
}
$inviterUserId = $validateResult['data']['user_id'];
// 不能邀请自己
if ($inviterUserId == $userId) {
return ['code' => 0, 'msg' => '不能使用自己的邀请码'];
}
// 绑定
$user->parent_user_id = $inviterUserId;
$user->invite_code_used = $inviteCode;
$user->save();
\think\Log::info("邀请码绑定: 邀请人[{$inviterUserId}] -> 被邀请人[{$userId}] 邀请码[{$inviteCode}]");
// 触发分销业绩统计
$agent = new \addons\shopro\service\commission\Agent($userId);
$agent->createAsyncAgentUpgrade($userId);
// 添加分销推广记录
\app\admin\model\shopro\commission\Log::add(
$inviterUserId,
'share',
['user' => $user, 'type' => 'invite_code']
);
return [
'code' => 1,
'msg' => '绑定成功',
'data' => $validateResult['data']
];
}
}

View File

@ -155,6 +155,9 @@ class OrderCreate
$this->money = (isset($params['money']) && $params['money'] > 0) ? $params['money'] : 0; $this->money = (isset($params['money']) && $params['money'] > 0) ? $params['money'] : 0;
// 邀请码
$this->invite_code = $params['invite_code'] ?? '';
// 获取商品信息 // 获取商品信息
$this->goodsListInit(); $this->goodsListInit();
} }
@ -1238,6 +1241,16 @@ class OrderCreate
]; ];
\think\Hook::listen('order_create_after', $hookData); \think\Hook::listen('order_create_after', $hookData);
// 处理邀请码绑定
if (!empty($this->invite_code) && $this->user->parent_user_id === null) {
$bindResult = \addons\shopro\library\InviteHelper::bind($this->invite_code, $this->user->id);
if ($bindResult['code'] == 1) {
\think\Log::info("订单[{$order->id}]使用邀请码[{$this->invite_code}]绑定成功");
} else {
\think\Log::warning("订单[{$order->id}]邀请码绑定失败: " . $bindResult['msg']);
}
}
return $order; return $order;
}); });

View File

@ -0,0 +1,271 @@
# 前端修改说明
## 📝 修改文件清单
```
✅ 已修改的文件3个
frontend/
├── pages/order/confirm.vue # 订单确认页(+40行
├── pages/commission/components/
│ └── commission-info.vue # 分销中心个人信息(+30行
└── sheep/api/
├── invite.js # 新增邀请码API新建
└── index.js # 注册邀请码API+2行
```
## 🎯 功能说明
### 1. 订单确认页 - 邀请码输入框
**位置**: 订单备注下方
**功能**:
- ✅ 仅未绑定上级的用户显示(`v-if="!userInfo.parent_user_id"`
- ✅ 非必填6位大写字母+数字
- ✅ 失焦自动验证(调用 `/invite/validate` 接口)
- ✅ 验证成功显示绿色勾号
- ✅ 提交订单时自动携带 `invite_code` 参数
**代码片段**:
```vue
<uni-easyinput
maxlength="6"
placeholder="选填(6位大写字母+数字)"
v-model="state.inviteCode"
@blur="onValidateInvite"
/>
```
**验证逻辑**:
```javascript
async function onValidateInvite() {
const code = state.inviteCode.trim().toUpperCase();
if (code.length !== 6) {
sheep.$helper.toast('邀请码格式错误');
return;
}
const { code: resCode } = await sheep.$api.invite.validate({ invite_code: code });
if (resCode === 1) {
state.inviteValidated = true;
sheep.$helper.toast('邀请码验证成功');
}
}
```
**提交订单**:
```javascript
if (state.inviteValidated && state.inviteCode) {
state.orderPayload.invite_code = state.inviteCode;
}
const { code, data } = await sheep.$api.order.create(state.orderPayload);
```
---
### 2. 个人中心 - 显示邀请码
**位置**: 分销中心个人信息卡片,等级标签下方
**功能**:
- ✅ 自动获取邀请码(`/invite/myCode`
- ✅ 点击复制邀请码到剪贴板
- ✅ 复制成功提示
**代码片段**:
```vue
<view class="invite-code-box" @tap="onCopyCode">
<text class="invite-label">我的邀请码:</text>
<text class="invite-code">{{ state.inviteCode }}</text>
<text class="ss-iconfont uicon-copy"></text>
</view>
```
**逻辑**:
```javascript
// 页面加载时获取邀请码
async function getInviteCode() {
const { code, data } = await sheep.$api.invite.myCode();
if (code === 1) {
state.inviteCode = data.invite_code;
}
}
// 复制邀请码
function onCopyCode() {
sheep.$helper.copyText(state.inviteCode);
sheep.$helper.toast('邀请码已复制');
}
onMounted(() => {
getInviteCode();
});
```
---
## 🔌 API 接口
### 新增 `sheep.$api.invite` 模块
```javascript
// 1. 获取我的邀请码
sheep.$api.invite.myCode()
// 响应: { code: 1, data: { invite_code: "ABC123", invite_count: 5 } }
// 2. 验证邀请码
sheep.$api.invite.validate({ invite_code: "ABC123" })
// 响应: { code: 1, msg: "邀请码有效", data: { ... } }
// 3. 我邀请的用户列表
sheep.$api.invite.myInvites({ page: 1 })
// 响应: { code: 1, data: { total: 10, data: [...] } }
```
---
## 🎨 样式调整
### 订单确认页样式
```scss
.invite-input-box {
position: relative;
.validate-tag {
margin-left: 10rpx;
font-size: 32rpx;
}
}
```
### 个人中心样式
```scss
.invite-code-box {
margin-top: 12rpx;
padding: 4rpx 12rpx;
background: rgba(255, 255, 255, 0.2);
border-radius: 20rpx;
border: 1rpx solid rgba(255, 255, 255, 0.3);
.invite-code {
font-size: 26rpx;
font-weight: bold;
color: #fff;
letter-spacing: 2rpx;
}
}
```
---
## 🧪 测试步骤
### 测试1: 订单页邀请码输入
```bash
# 1. 登录建行生活用户(未绑定上级)
# 2. 加入购物车,进入订单确认页
# 3. 查看是否显示"邀请码"输入框
# 4. 输入邀请码ABC123
# 5. 失焦后查看是否显示验证成功图标
# 6. 提交订单
# 7. 后台检查是否绑定成功:
SELECT parent_user_id, invite_code_used FROM user WHERE id = 新用户ID;
```
### 测试2: 个人中心显示邀请码
```bash
# 1. 进入分销中心页面
# 2. 查看个人信息卡片,等级标签下方
# 3. 确认显示"我的邀请码: XXXXXX"
# 4. 点击邀请码
# 5. 确认提示"邀请码已复制"
# 6. 粘贴验证是否成功复制
```
### 测试3: 已绑定用户不显示输入框
```bash
# 1. 登录已有上级的用户
# 2. 进入订单确认页
# 3. 确认不显示"邀请码"输入框
```
---
## 🐛 常见问题
### 问题1: 邀请码输入框不显示
**检查**:
```javascript
// 确认用户是否已绑定
console.log(userInfo.parent_user_id); // 应为 null
```
### 问题2: 验证失败
**检查**:
```bash
# 查看API响应
curl -X POST http://your-domain/invite/validate -d "invite_code=ABC123"
# 查看后端日志
tail -f runtime/log/$(date +%Y%m)/$(date +%d).log | grep "邀请码"
```
### 问题3: 个人中心不显示邀请码
**检查**:
```javascript
// 检查API是否成功
const res = await sheep.$api.invite.myCode();
console.log(res);
// 检查用户是否有邀请码
SELECT invite_code FROM user WHERE id = 你的用户ID;
```
---
## 📱 界面预览
### 订单确认页
```
┌────────────────────────────────┐
│ 商品信息 │
├────────────────────────────────┤
│ 订单备注 [建议留言前先与商家沟通]│
├────────────────────────────────┤
│ 邀请码 [选填(6位大写字母+数字)] ✓│ ← 新增
└────────────────────────────────┘
```
### 个人中心
```
┌────────────────────────────────┐
│ 👤 张三 │
│ 🏷️ 分销商等级 │
│ 📋 我的邀请码: ABC123 📋 │ ← 新增
└────────────────────────────────┘
```
---
## ✅ 完成检查清单
- [x] 订单页显示邀请码输入框
- [x] 邀请码验证功能正常
- [x] 订单提交携带邀请码参数
- [x] 个人中心显示邀请码
- [x] 点击复制邀请码功能正常
- [x] 已绑定用户不显示输入框
- [x] API接口注册成功
---
**最后更新**: 2024-01-15
**维护人员**: Billy

View File

@ -23,6 +23,12 @@
<view class="ss-iconfont uicon-arrow-right" style="color: #fff; font-size: 28rpx"> <view class="ss-iconfont uicon-arrow-right" style="color: #fff; font-size: 28rpx">
</view> </view>
</view> </view>
<!-- 邀请码 -->
<view class="invite-code-box ss-flex ss-col-center" v-if="state.inviteCode" @tap="onCopyCode">
<text class="invite-label">我的邀请码:</text>
<text class="invite-code">{{ state.inviteCode }}</text>
<text class="ss-iconfont uicon-copy" style="color: #fff; font-size: 24rpx; margin-left: 8rpx"></text>
</view>
</view> </view>
</view> </view>
</view> </view>
@ -31,7 +37,7 @@
<script setup> <script setup>
import sheep from '@/sheep'; import sheep from '@/sheep';
import { computed, reactive } from 'vue'; import { computed, reactive, onMounted } from 'vue';
const userInfo = computed(() => sheep.$store('user').userInfo); const userInfo = computed(() => sheep.$store('user').userInfo);
const agentInfo = computed(() => sheep.$store('user').agentInfo); const agentInfo = computed(() => sheep.$store('user').agentInfo);
@ -39,6 +45,25 @@
const state = reactive({ const state = reactive({
showMoney: false, showMoney: false,
inviteCode: '',
});
//
async function getInviteCode() {
const { code, data } = await sheep.$api.invite.myCode();
if (code === 1) {
state.inviteCode = data.invite_code;
}
}
//
function onCopyCode() {
sheep.$helper.copyText(state.inviteCode);
sheep.$helper.toast('邀请码已复制');
}
onMounted(() => {
getInviteCode();
}); });
</script> </script>
@ -122,5 +147,26 @@
} }
} }
} }
.invite-code-box {
margin-top: 12rpx;
padding: 4rpx 12rpx;
background: rgba(255, 255, 255, 0.2);
border-radius: 20rpx;
border: 1rpx solid rgba(255, 255, 255, 0.3);
.invite-label {
font-size: 22rpx;
color: rgba(255, 255, 255, 0.8);
margin-right: 8rpx;
}
.invite-code {
font-size: 26rpx;
font-weight: bold;
color: #fff;
letter-spacing: 2rpx;
}
}
} }
</style> </style>

View File

@ -30,7 +30,7 @@
</template> </template>
</s-goods-item> </s-goods-item>
<view class="order-item ss-flex ss-col-center ss-row-between ss-p-x-20 bg-white ss-r-20"> <view class="order-item ss-flex ss-col-center ss-row-between ss-p-x-20 bg-white">
<view class="item-title">订单备注</view> <view class="item-title">订单备注</view>
<view class="ss-flex ss-col-center"> <view class="ss-flex ss-col-center">
<uni-easyinput <uni-easyinput
@ -42,6 +42,29 @@
></uni-easyinput> ></uni-easyinput>
</view> </view>
</view> </view>
<!-- 邀请码输入框 -->
<view
v-if="!userInfo.parent_user_id"
class="order-item ss-flex ss-col-center ss-row-between ss-p-x-20 bg-white ss-r-b-20"
>
<view class="item-title">邀请码</view>
<view class="ss-flex ss-col-center invite-input-box">
<uni-easyinput
maxlength="6"
placeholder="选填(6位大写字母+数字)"
v-model="state.inviteCode"
:inputBorder="false"
:clearable="true"
:disabled="state.inviteValidated"
@blur="onValidateInvite"
:styles="{color: state.inviteValidated ? '#52c41a' : '#333'}"
></uni-easyinput>
<view v-if="state.inviteValidated" class="validate-tag">
<text class="ss-iconfont uicon-checkbox-mark" style="color: #52c41a"></text>
</view>
</view>
</view>
</view> </view>
<!-- 合计 --> <!-- 合计 -->
<view class="bg-white total-card-box ss-p-20 ss-m-b-14 ss-r-20"> <view class="bg-white total-card-box ss-p-20 ss-m-b-14 ss-r-20">
@ -185,8 +208,12 @@
showCoupon: false, showCoupon: false,
couponInfo: [], couponInfo: [],
showDiscount: false, showDiscount: false,
inviteCode: '',
inviteValidated: false,
}); });
const userInfo = computed(() => sheep.$store('user').userInfo);
// () // ()
const exchangeNow = computed( const exchangeNow = computed(
() => state.orderPayload.order_type === 'score' && state.orderInfo.pay_fee == 0, () => state.orderPayload.order_type === 'score' && state.orderInfo.pay_fee == 0,
@ -254,8 +281,38 @@
} }
} }
//
async function onValidateInvite() {
const code = state.inviteCode.trim().toUpperCase();
if (!code) {
state.inviteValidated = false;
return;
}
if (code.length !== 6) {
sheep.$helper.toast('邀请码格式错误');
state.inviteValidated = false;
return;
}
const { code: resCode, msg } = await sheep.$api.invite.validate({ invite_code: code });
if (resCode === 1) {
state.inviteCode = code;
state.inviteValidated = true;
sheep.$helper.toast('邀请码验证成功');
} else {
state.inviteValidated = false;
sheep.$helper.toast(msg || '邀请码无效');
}
}
// & // &
async function submitOrder() { async function submitOrder() {
//
if (state.inviteValidated && state.inviteCode) {
state.orderPayload.invite_code = state.inviteCode;
}
const { code, data } = await sheep.$api.order.create(state.orderPayload); const { code, data } = await sheep.$api.order.create(state.orderPayload);
if (code === 1) { if (code === 1) {
// //
@ -310,6 +367,16 @@
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
//
.invite-input-box {
position: relative;
.validate-tag {
margin-left: 10rpx;
font-size: 32rpx;
}
}
:deep() { :deep() {
.uni-input-wrapper { .uni-input-wrapper {
width: 320rpx; width: 320rpx;

View File

@ -7,6 +7,7 @@ import commission from "./commission"
import coupon from "./coupon" import coupon from "./coupon"
import data from "./data" import data from "./data"
import goods from "./goods" import goods from "./goods"
import invite from "./invite"
import order from "./order" import order from "./order"
import pay from "./pay" import pay from "./pay"
import third from "./third" import third from "./third"
@ -23,6 +24,7 @@ export default {
coupon, coupon,
data, data,
goods, goods,
invite,
order, order,
pay, pay,
third, third,

View File

@ -0,0 +1,29 @@
import request from '@/sheep/request';
export default {
// 获取我的邀请码
myCode: () =>
request({
url: 'invite/myCode',
method: 'GET',
}),
// 验证邀请码
validate: (data) =>
request({
url: 'invite/validate',
method: 'POST',
data,
custom: {
showError: false,
},
}),
// 我邀请的用户列表
myInvites: (params) =>
request({
url: 'invite/myInvites',
method: 'GET',
params,
}),
};