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:
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:
3. PHP code writing
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; }
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; }
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
<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>
<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!