In the AnQi CMS template, how to obtain and display the user's account balance and cumulative earnings?

For operators, flexibly displaying user-specific information on the website, such as their account balance or cumulative earnings, is a key step in improving user experience and managing the member system.AutoCMS is a powerful and practical content management system that provides a very convenient way to obtain these user data.userDetailEasily obtain and display the user's tags.Balance(account balance) andTotalReward(Cumulative earnings).

Deeply UnderstanduserDetailtags

userDetailIt is a very practical tag in the AnQin CMS template engine, specifically used to obtain detailed information of a specified user.It is like a data interface that allows you to directly retrieve various properties of a user in the frontend template based on the user ID.userDetailAll can be easily realized for you.

When usinguserDetailWhen labeling, the most critical parameter isid. Thisidrepresented the user identity identifier you want to retrieve information for. Typically, this would be the ID of the currently logged-in user, or the ID of a specific user you want to display in a particular scenario.

Its basic usage form is like this:

{% userDetail 变量名称 with name="字段名称" id="用户ID" %}

Here, 'variable name' is a temporary name you specify for the data you obtain, which is convenient for later reference in the template; 'field name' is the specific user attribute you wish to obtain, such asBalanceorTotalReward;“User ID” as the name implies, is the unique identifier for the user to be queried.

Get the user's account balance (Balance)

The account balance usually refers to the available funds of the user on the platform, which can be used for consumption, purchasing services, or withdrawals.In membership, points system, or e-commerce websites, displaying the user's account balance is an essential function.

PassuserDetailTags, we can get the user's account balance like this:

{# 假设我们已经通过某种方式获取到了当前登录用户的ID,并存储在名为 `currentUser.Id` 的变量中 #}

<p>
    <strong>当前账户余额:</strong>
    {% userDetail userBalance with name="Balance" id=currentUser.Id %}
    {{ userBalance }} 元
</p>

In this code, we useuserDetailLabel, specifyname="Balance"to get the user's balance, and assign the result touserBalancethis temporary variable. Subsequently,{{ userBalance }}It can display the balance value on the page. If the user does not have a balance, or the amount is zero, it will usually display “0” or “0.00”.

Get the cumulative income of the user (TotalReward)

累计收益则更侧重于用户在平台上通过某种行为(如内容创作奖励、推广佣金、邀请返利等)所累积的全部收入。This is very helpful for motivating users and showcasing the platform's value.

The method to get the cumulative income of the user is similar to getting the balance:

{# 假设我们已经通过某种方式获取到了当前登录用户的ID,并存储在名为 `currentUser.Id` 的变量中 #}

<p>
    <strong>累计收益:</strong>
    {% userDetail totalReward with name="TotalReward" id=currentUser.Id %}
    {{ totalReward }} 元
</p>

Similarly, we specifyname="TotalReward"to get the cumulative income and store it intotalRewardIn the variable, then display it. This value is usually a continuously increasing cumulative value that can bring users a direct sense of achievement.

Combined with the actual application scenario

In practical applications, you may want to display these financial information on a user center page or a personal dashboard at the same time.Combine them, and users can easily understand their financial situation.

For example, you might write template code like this in a user profile overview module on a personal center page:

<div class="user-financial-overview">
    <h2>我的财务概览</h2>

    {# 假设 currentUser.Id 已经由后端传递到模板上下文中,代表当前登录用户的ID #}

    {% userDetail userBalance with name="Balance" id=currentUser.Id %}
    {% userDetail totalReward with name="TotalReward" id=currentUser.Id %}

    <p>
        <strong>当前可用余额:</strong>
        {# 如果希望余额为0时显示特定文本,可以结合if标签判断 #}
        {% if userBalance > 0 %}
            <span class="highlight-balance">{{ userBalance }}</span> 元
        {% else %}
            <span>0.00</span> 元
        {% endif %}
    </p>

    <p>
        <strong>历史累计收益:</strong>
        {# 同样,如果累计收益为0时显示特定文本 #}
        {% if totalReward > 0 %}
            <span class="highlight-reward">{{ totalReward }}</span> 元
        {% else %}
            <span>0.00</span> 元
        {% endif %}
    </p>

    <div class="actions">
        <a href="/user/recharge">充值</a>
        <a href="/user/withdraw">提现</a>
        <a href="/user/rewards">收益明细</a>
    </div>
</div>

By this method, you not only demonstrate the data, but also can present it in different styles based on the specific values of the data (such as whether it is greater than 0), and even guide the user to take the next step, making the entire financial module more vivid and practical.

Summary

userDetailLabels provide a great deal of flexibility for Anqi CMS users, allowing easy display of dynamic user-related data in front-end templates. By masteringBalanceandTotalRewardThese fields, your website will be able to better serve a variety of business scenarios such as member management, content monetization, e-commerce operations, etc., providing users with a more personalized and transparent experience.


Common Questions (FAQ)

1.userDetailthe tag inidHow should the parameter be obtained? idParameters usually represent the ID of the currently logged-in user. In the normal development process of Anqi CMS, the backend controller will pass the ID of the currently logged-in user through the template context (Context) to the front-end template, and you can directly refer to this variable in the template (for example, as mentioned in the above text).currentUser.Id)。If it is necessary to display the ID of other users in a specific scenario, then the user's ID should be obtained through URL parameters, database queries, and passed to the template.

2. If the user does notBalanceorTotalRewarddata, what will the template display?ForBalanceandTotalRewardfor this type of numeric field, if the user has no related records or the data is zero,userDetailthe label will usually return directly0In the template, you can combine as shown in the example code,{% if ... %}The label makes a judgment, when the value is 0, it displays "0.00" or other more friendly prompt text, such as "No balance" or "No earnings have been generated".

3. In addition to the account balance and cumulative earnings,userDetailWhat information can tags obtain about users? userDetailThe functions of tags are very rich, in addition toBalanceandTotalRewardIt can also obtain many other important information about the user, such as:

  • UserNameUsername
  • RealNameReal Name
  • EmailUser Email
  • PhoneUser Phone Number
  • AvatarURL:User avatar link
  • GroupId:User group ID of the user
  • IsRetailer:Is the user a distributor
  • InviteCode:Invitation code
  • LastLogin:Latest login time (timestamp, needs to be配合 withstampToDateformat)
  • ExpireTime:VIP expiration time (timestamp, needs to be paired withstampToDateformatting) You can flexibly call these fields according to your actual needs to build a more comprehensive user page.