Home  >  Article  >  Backend Development  >  Using PHP and Vue to implement the method of redeeming member points for gifts after payment

Using PHP and Vue to implement the method of redeeming member points for gifts after payment

PHPz
PHPzOriginal
2023-09-24 10:37:021524browse

Using PHP and Vue to implement the method of redeeming member points for gifts after payment

Using PHP and Vue to realize the method of redeeming member points for gifts after payment

With the rapid development of e-commerce, more and more companies are trying to attract and retain For customers, a membership points system was launched. Member points can be obtained through shopping, reviews, activities, etc. Customers can use points to redeem gifts, offset order amounts, etc. This article will introduce how to use PHP and Vue to implement the method of redeeming member points for gifts after payment, and provide specific code examples.

1. Preparation

Before starting, we need to prepare the following environment and tools:

  1. PHP server: You can use XAMPP, WAMP, etc. to build a local development environment ;
  2. Vue.js: You can use npm to install Vue.js, or you can use vue-cli to quickly build a Vue project.

2. Database design

We need to design a database table to save members’ points and gift information. The following is a simple database table design:

  1. members table: saves member information, including member ID, name, points and other fields;
  2. gifts table: saves gift information, Including fields such as gift ID, name, required points;
  3. orders table: saves order information, including order ID, member ID, payment amount and other fields;
  4. order_gifts table: saves orders and Gift related information, including order ID, gift ID and other fields.

3. PHP code writing

  1. Get member points: Create a PHP function to query the member's current points.
function getMemberPoints($memberId) {
    // 连接数据库
    $conn = new mysqli('localhost', 'username', 'password', 'dbname');
    if ($conn->connect_error) {
        die("数据库连接失败:" . $conn->connect_error);
    }

    // 查询会员积分
    $sql = "SELECT points FROM members WHERE member_id = $memberId";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $points = $row["points"];
    } else {
        $points = 0;
    }

    // 关闭数据库连接
    $conn->close();

    return $points;
}
  1. Query redeemable gifts: Create a PHP function to query the gifts currently redeemable by members.
function getAvailableGifts($memberId) {
    // 连接数据库
    $conn = new mysqli('localhost', 'username', 'password', 'dbname');
    if ($conn->connect_error) {
        die("数据库连接失败:" . $conn->connect_error);
    }

    // 查询可兑换礼品
    $sql = "SELECT * FROM gifts WHERE points <= (SELECT points FROM members WHERE member_id = $memberId)";
    $result = $conn->query($sql);

    // 构造礼品数组
    $gifts = array();
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $gifts[] = $row;
        }
    }

    // 关闭数据库连接
    $conn->close();

    return $gifts;
}
  1. Redeem gifts: Create a PHP function to handle members' requests to redeem gifts.
function exchangeGift($memberId, $giftId) {
    // 连接数据库
    $conn = new mysqli('localhost', 'username', 'password', 'dbname');
    if ($conn->connect_error) {
        die("数据库连接失败:" . $conn->connect_error);
    }

    // 查询礼品所需积分
    $sql = "SELECT points FROM gifts WHERE gift_id = $giftId";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $requiredPoints = $row["points"];
    } else {
        die("礼品不存在");
    }

    // 查询会员当前积分
    $sql = "SELECT points FROM members WHERE member_id = $memberId";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $memberPoints = $row["points"];
    } else {
        die("会员不存在");
    }

    // 检查会员积分是否足够
    if ($memberPoints < $requiredPoints) {
        die("积分不足,无法兑换该礼品");
    }

    // 扣除会员积分
    $updatedPoints = $memberPoints - $requiredPoints;
    $sql = "UPDATE members SET points = $updatedPoints WHERE member_id = $memberId";
    $conn->query($sql);

    // 关联订单和礼品
    // 生成订单ID,可以根据业务需求自行设计
    $orderId = generateOrderId();
    $sql = "INSERT INTO order_gifts (order_id, gift_id) VALUES ($orderId, $giftId)";
    $conn->query($sql);

    // 关闭数据库连接
    $conn->close();

    return $orderId;
}

4. Vue code writing

  1. Get member points: Call PHP's getMemberPoints function in the Vue component to get the member's current points.
<template>
  <div>
    <p>当前积分:{{ memberPoints }}</p>
    <button @click="getMemberPoints">刷新积分</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      memberPoints: 0
    }
  },
  methods: {
    getMemberPoints() {
      axios.get('api/getMemberPoints.php')
        .then(response => {
          this.memberPoints = response.data.points;
        })
        .catch(error => {
          console.error(error);
        });
    }
  },
  mounted() {
    this.getMemberPoints();
  }
}
</script>
  1. Query redeemable gifts: Call PHP’s getAvailableGifts function in the Vue component to obtain the gifts currently redeemable by members.
<template>
  <div>
    <h2>可兑换礼品</h2>
    <ul>
      <li v-for="gift in availableGifts" :key="gift.gift_id">
        {{ gift.name }} (所需积分:{{ gift.points }})
        <button @click="exchangeGift(gift.gift_id)">兑换</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      availableGifts: []
    }
  },
  methods: {
    getAvailableGifts() {
      axios.get('api/getAvailableGifts.php')
        .then(response => {
          this.availableGifts = response.data;
        })
        .catch(error => {
          console.error(error);
        });
    },
    exchangeGift(giftId) {
      axios.post('api/exchangeGift.php', { gift_id: giftId })
        .then(response => {
          console.log("兑换成功,订单ID:" + response.data.order_id);
          // 刷新可兑换礼品列表
          this.getAvailableGifts();
        })
        .catch(error => {
          console.error(error);
        });
    }
  },
  mounted() {
    this.getAvailableGifts();
  }
}
</script>

The above is how to use PHP and Vue to redeem member points for gifts after payment. Through PHP's database operation function, member points and gift information can be easily read from the database, and member points can be processed accordingly. The Vue component obtains membership points and redeemable gifts by calling the PHP interface, and displays and interacts with them on the front end. In actual development, appropriate modifications and extensions can be made according to business needs to improve functions.

(The above code is only an example and needs to be adjusted and improved accordingly according to the actual situation.)

The above is the detailed content of Using PHP and Vue to implement the method of redeeming member points for gifts after payment. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn