搜索
首页php框架Laravel基于 Laravel 开发会员分销系统

基于 Laravel 开发会员分销系统

May 28, 2020 am 10:16 AM
laravel

基于 Laravel 开发会员分销系统

最近,在 Sitesauce 现有基础上新增会员系统,就具体实现细节写了这篇文章。

笔记:我将从零开始构建该程序,这样无论你处于什么阶段都可以读懂该文章。 当然如果你已经非常熟悉 Laravel ,你可以在此 Rewardful 之类的平台上完成该工作,这样会节省不少时间。

为了概念明确,下文中 邀请者用上级替代,被邀请者使用下级替代

首先,我们要明确我们的需求:首先用户可以通过程序分享链接邀请好友注册,被邀请者可以通过链接注册,从而绑定邀请关系,其次在下级消费的时候,上级都可以获得相应的佣金。

现在我们要确定如何实现注册。我原本打算使用 Fathom 的方法,只要将用户引导到特定页面,该用户将会被标记为 special referral page ,待用户完注册,并将关系绑定。 但最终采用的是 Rewardful 的做法,通过向链接添加参数 ?via=miguel 来实现推荐页面的构建。

好了,现在让我们创建我们的注册页面,在注册页面程序会通过链接参数 via 匹配上级。 代码很简单,如果 via 存在那么将其存储到 Cookie 30 天,由于我们有几个不同子域名都需要该操作,所以我们将其添加到主域名下,这样所有子域均可使用该 Cookie。下面视具体代码:

import Cookies from 'js-cookie'
const via = new URL(location.href).searchParams.get('via')
if (via) {
    Cookies.set('sitesauce_affiliate', via, {
        expires: 30,
        domain: '.sitesauce.app',
        secure: true,
        sameSite: 'lax'
    })
}

这样做的好处是当会员未通过此次分享注册,而是事后自己注册的时候,可以明确地知道该会员是通过那个上级的引荐而来的。我想更进一步,在新会员注册的时候通过显示一些标语以及上级者信息,从而使用户明确知道这是来自会员(好友)的一个引荐链接,从而使注册成功率更高,所以我添加了提示弹窗。效果如下:

想要实现上面效果的话,现在我们需要的不仅仅是上级标签,还需要上级详细信息,所以我们需要一个 API,该 API 会通过 via 匹配并提供上级详细信息。

import axios from 'axios'
import Cookies from 'js-cookie'
const via = new URL(location.href).searchParams.get('via')
if (via) {
    axios.post(`https://app.sitesauce.app/api/affiliate/${encodeURIcomponent(this.via)}`).then(response => {
        Cookies.set('sitesauce_affiliate', response.data, { expires: 30, domain: '.sitesauce.app', secure: true, sameSite: 'lax' })
    }).catch(error => {
        if (!error.response || error.response.status !== 404) return console.log('Something went wrong')
        console.log('Affiliate does not exist. Register for our referral program here: https://app.sitesauce.app/affiliate')
    })
}

在 URL 中你可以看到 encodeURIComponent,他的作用是保护我们不在受 Path Traversal vulnerability 的影响。 在我们发送请求到 /api/referral/:via,如果过有人恶意修改链接参数,像这样: ?via=../../logout ,用户在点击之后可能会注销,当然这可能没有什么影响,但是不可避免得会有些其他的会带来不可预期影响的操作。

由于 Sitesauce 在一些地方使用了 Alpine,所以我们在此基础之上,将弹窗修改为 Alpine 组件,这样有更好的扩展性。 这里要感谢下 Ryan ,在我的转换无法正常工作时,给我提出了宝贵建议。

<div x-data="{ ...component() } x-cloak x-init="init()">
    <template x-if="affiliate">
        <div>
            <img :src="affiliate.avatar" class="h-8 w-8 rounded-full mr-2">
            <p>Your friend <span x-text="affiliate.name"></span> has invited you to try Sitesauce</p>
            <button>Start your trial</button>
        </div>
    </template>
</div>
<script>
import axios from &#39;axios&#39;
import Cookies from &#39;js-cookie&#39;
// 使用模板标签 $nextTick ,进行代码转换,这里特别感谢下我的朋友 Ryan 
window.component = () => ({
    affiliate: null,
    via: new URL(location.href).searchParams.get(&#39;via&#39;)
    init() {
        if (! this.via) return this.$nextTick(() => this.affiliate = Cookies.getJSON(&#39;sitesauce.affiliate&#39;))
        axios.post(`https://app.sitesauce.app/api/affiliate/${encodeURIComponent(this.via)}`).then(response => {
            this.$nextTick(() => this.affiliate = response.data)
            Cookies.set(&#39;sitesauce.affiliate&#39;, response.data, {
                expires: 30, domain: &#39;.sitesauce.app&#39;, secure: true, sameSite: &#39;lax&#39;
            })
        }).catch(error => {
            if (!error.response || error.response.status !== 404) return console.log(&#39;Something went wrong&#39;)
            console.log(&#39;Affiliate does not exist. Register for our referral program here: https://app.sitesauce.app/affiliate&#39;)
        })
    }
})
</script>

现在,完善 API,使其可以获取有效数据。 除此之外,我们还需要新增几个个字段到我们现有的数据库,这个我们将在之后说明。下面是迁移文件:

class AddAffiliateColumnsToUsersTable extends Migration
{
    /**
     * 迁移执行
     *
     * @return void
     */
    public function up()
    {
        Schema::table(&#39;users&#39;, function (Blueprint $table) {
            $table->string(&#39;affiliate_tag&#39;)->nullable();
            $table->string(&#39;referred_by&#39;)->nullable();
            $table->string(&#39;paypal_email&#39;)->nullable();
            $table->timestamp(&#39;cashed_out_at&#39;)->nullable();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table(&#39;users&#39;, function (Blueprint $table) {
            $table->dropColumn(&#39;affiliate_tag&#39;, &#39;referred_by&#39;, &#39;paypal_email&#39;, &#39;cashed_out_at&#39;);
        });
    }
}

通过路由自定义键名功能实现参数绑定 (可在 Laravel 7.X 上使用),构建我们的 API 路由。

Route::post(&#39;api/affiliate/{user:affiliate_tag}&#39;, function (User $user) {
    return $user->only(&#39;id&#39;, &#39;name&#39;, &#39;avatar&#39;, &#39;affiliate_tag&#39;);
})->middleware(&#39;throttle:30,1&#39;);

在开始注册操作之前,首先读取我们的 Cookie,由于未进行加密,所以我们需要将其加入到 EncryptCookies 的 except 字段中,将其排除。

// 中间件:app/Http/Middleware/EncryptCookies.php
use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;
class EncryptCookies extends Middleware
{
    /**
    * The names of the cookies that should not be encrypted
    *
    * @var array
    */
    protected $except = [
        &#39;sitesauce_referral&#39;,
    ];
}

现在通过 RegisterController 的 authenticated 方法执行注册相应的逻辑,其间,我们通过上面的方式获取 Cooke,并通过该 Cooke 找到相应的上级,最终实现将下级与上级关联。

/**
 * 上级用户已经注册
 *
 * @param \Illuminate\Http\Request $request
 * @param \App\User $user
 */
protected function registered(Request $request, User $user)
{
    if (! $request->hasCookie(&#39;sitesauce.referral&#39;)) return;
    $referral = json_decode($request->cookie(&#39;sitesauce_referral&#39;), true)[&#39;affiliate_tag&#39;];
    if (! User::where(&#39;affiliate_tag&#39;, $referral)->exists()) return;
    $user->update([
        &#39;referred_by&#39; => $referral,
    ]);
}

我们还需要一个方法来获取我的下级用户;列表,通过 ORM 的 hasMany 方法很简单的就实现了。

class User extends Model
{
    public function referred()
    {
        return $this->hasMany(self::class, &#39;referred_by&#39;, &#39;affiliate_tag&#39;);
    }
}

现在,让我们构建我们的注册页面,在用户注册时候,他们可以选择喜好标签,并需要他们提供 PayPal 电子邮件 以以便之后的提现操作。下面是效果预览:

会员注册后,我们还需要提供电子邮箱的变更,以及标签变更的相关入口。在其变更标签之后,我们需要级联更新其下级用户的的标签,以确保两者的统一。这涉及到了数据库的事务操作,为保证操作的原子性,我们需要在事务中完成以上两个操作。代码如下:

public function update(Request $request)
    {
        $request->validate([
            &#39;affiliate_tag&#39; => [&#39;required&#39;, &#39;string&#39;, &#39;min:3&#39;, &#39;max:255&#39;, Rule::unique(&#39;users&#39;)->ignoreModel($request->user())],
            &#39;paypal_email&#39; => [&#39;required&#39;, &#39;string&#39;, &#39;max:255&#39;, &#39;email&#39;],
        ]);
        DB::transaction(function () use ($request) {
            if ($request->input(&#39;affiliate_tag&#39;) != $request->user()->affiliate_tag) {
                User::where(&#39;referred_by&#39;, $request->user()->affiliate_tag)
                    ->update([&#39;referred_by&#39; => $request->input(&#39;affiliate_tag&#39;)]);
            }
            $request->user()->update([
                &#39;affiliate_tag&#39; => $request->input(&#39;affiliate_tag&#39;),
                &#39;paypal_email&#39; => $request->input(&#39;paypal_email&#39;),
            ]);
        });
        return redirect()->route(&#39;affiliate&#39;);
    }

下面我们需要确定会员收益的计算方式,可以通过下级用户的所有消费金额的百分比作为佣金返给上级,为了计算方便,我们仅仅计算自上次结款日期之后的数据。我们使用扩展 Mattias’ percentages package 使计算简单明了。

use Mattiasgeniar\Percentage\Percentage;
const COMMISSION_PERCENTAGE = 20;
public function getReferralBalance() : int
{
    return Percentage::of(static::COMISSION_PERCENTAGE,
        $this->referred
            ->map(fn (User $user) => $user->invoices(false, [&#39;created&#39; => [&#39;gte&#39; => optional($this->cashed_out_at)->timestamp]]))
            ->flatten()
            ->map(fn (\Stripe\Invoice $invoice) => $invoice->subtotal)
            ->sum()
    );
}

最后我们以月结的方式向上级发放佣金,并更新 cashed_out_at 字段为本次佣金发放时间。效果如下:

至此,希望以上文档对你有帮助。祝你快乐每天。

推荐教程:《Laravel教程

以上是基于 Laravel 开发会员分销系统的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:learnku。如有侵权,请联系admin@php.cn删除
Laravel的全堆栈开发:管理API和前端逻辑Laravel的全堆栈开发:管理API和前端逻辑Apr 28, 2025 am 12:22 AM

在Laravel全栈开发中,管理API和前端逻辑的有效方法包括:1)使用RESTful控制器和资源路由管理API;2)通过Blade模板和Vue.js或React处理前端逻辑;3)通过API版本控制和分页优化性能;4)保持后端和前端逻辑分离,确保可维护性和可扩展性。

翻译失落:分布式团队中的文化细微差别和误解翻译失落:分布式团队中的文化细微差别和误解Apr 28, 2025 am 12:22 AM

TotackleculturalIntricaciesIndistributedTeams,fosteranenvironmentcelebratingDifferences,BemindfulofCommunication,andusetoolsforclarity.1)enmulcultulalexchangessessionStossessessionStosharestories andraditions.2)

测量连接:分析和见解远程通信有效性测量连接:分析和见解远程通信有效性Apr 28, 2025 am 12:16 AM

Toassesstheeffectivenessofremotecommunication,focuson:1)Engagementmetricslikemessagefrequencyandresponsetime,2)Sentimentanalysistogaugeemotionaltone,3)Meetingeffectivenessthroughattendanceandactionitems,and4)Networkanalysistounderstandcommunicationpa

分布式团队中的安全风险:保护偏远世界中的数据分布式团队中的安全风险:保护偏远世界中的数据Apr 28, 2025 am 12:11 AM

toprotectSentiveDatainDistributedTeams,实现amulti-faceTedEblect:1)使用EndEnd-to-endencryptignterforsecurocommunication,2)基于applyrole的acccessControl(rbac)tomanagepermissions,3)

超越电子邮件:探索现代沟通平台以进行远程协作超越电子邮件:探索现代沟通平台以进行远程协作Apr 28, 2025 am 12:03 AM

不,emailisnotthebostforremotecollaborationtoday.modern PlatformLack,Microsoft Teams,Zoom,Asana和Trellofferreal时间通信,项目管理,项目管理和IntintegrationFeatureSthanCteAncteAncteAmworkworkesseffiquice。

协作文档编辑:简化分布式团队中的工作流程协作文档编辑:简化分布式团队中的工作流程Apr 27, 2025 am 12:21 AM

协作文档编辑是分布式团队优化工作流程的有效工具。它通过实时协作和反馈循环提升沟通和项目进度,常用工具包括GoogleDocs、MicrosoftTeams和Notion。使用时需注意版本控制和学习曲线等挑战。

以前的Laravel版本将得到多长时间?以前的Laravel版本将得到多长时间?Apr 27, 2025 am 12:17 AM

ThepreviousversionofLaravelissupportedwithbugfixesforsixmonthsandsecurityfixesforoneyearafteranewmajorversion'srelease.Understandingthissupporttimelineiscrucialforplanningupgrades,ensuringprojectstability,andleveragingnewfeaturesandsecurityenhancemen

利用Laravel的功能来为前端开发和后端开发利用Laravel的功能来为前端开发和后端开发Apr 27, 2025 am 12:16 AM

Laravelcanbeeffectivelyusedforbothfrontendandbackenddevelopment.1)Backend:UtilizeLaravel'sEloquentORMforsimplifieddatabaseinteractions.2)Frontend:LeverageBladetemplatesforcleanHTMLandintegrateVue.jsfordynamicSPAs,ensuringseamlessfrontend-backendinteg

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能