Commit aa4189f3 by yink

refactor: 优化用户积分计算逻辑并调整商品价格计算方式

- 在 `UserPointChangeRec` 模型中添加 `order` 关联方法,便于查询订单信息
- 将 `UserController` 中的佣金计算逻辑改为基于积分的计算,提升代码可维护性
- 在 `GoodController` 中引入 `buycode` 参数,替代原有的商户 ID 逻辑,简化商品价格计算
- 删除无用的缓存文件
parent 033d5f6c
......@@ -77,12 +77,13 @@ public function getList(Request $request)
$page = $request->page ?? 1;
$limit = $request->limit ?? 10;
$islogin = $request->islogin ?? 0;
$mer_id = 0;
// 获取已认证的用户,如果未认证则返回 null
$userObj = optional(Auth::user());
if ($userObj) {
$mer_id = $userObj->merchant_id;
}
$buycode = $request->buycode ?? null; //直购码,不传即原价购买
// $mer_id = 0;
// // 获取已认证的用户,如果未认证则返回 null
// $userObj = optional(Auth::user());
// if ($userObj) {
// $mer_id = $userObj->merchant_id;
// }
$data = [
'current_page' => $page,
'total' => 0,
......@@ -125,7 +126,7 @@ public function getList(Request $request)
'goods_name' => $item->goods_name,
'dg_price' => sprintf("%.2f", $cg_price),
'market_price' => sprintf("%.2f", $market_price),
'goods_price' => $mer_id ? sprintf("%.2f", $cg_price) : sprintf("%.2f", $market_price),
'goods_price' => $buycode ? sprintf("%.2f", $cg_price) : sprintf("%.2f", $market_price),
'tags' => $tags,
'cover_img' => (isset($goodsObj->cover_img) ? togetherFilePath($goodsObj->cover_img) : ''),
];
......@@ -142,14 +143,15 @@ public function getIndexGoods(Request $request)
$sort = $request->sort ?? 'desc';
$page = $request->page ?? 1;
$limit = $request->limit ?? 10;
$buycode = $request->buycode ?? null; //直购码,不传即原价购买
//$islogin = $request->islogin ?? 0;
$mer_id = 0;
$data = [];
// 获取已认证的用户,如果未认证则返回 null
$userObj = optional(Auth::user());
if ($userObj) {
$mer_id = $userObj->merchant_id;
}
// $userObj = optional(Auth::user());
// if ($userObj) {
// $mer_id = $userObj->merchant_id;
// }
$model = GoodModel::select(['id', 'goods_name', 'is_hot', 'cover_img', 'goods_brief', 'sku', 'tags'])
->where(['is_show' => 1]);
......@@ -192,7 +194,7 @@ public function getIndexGoods(Request $request)
'is_hot' => $datum->is_hot,
'dg_price' => sprintf('%.1f', $dg_price),
'market_price' => sprintf('%.1f', $market_price),
'goods_price' => $mer_id ? sprintf('%.1f', $dg_price) : sprintf('%.1f', $market_price),
'goods_price' => $buycode ? sprintf('%.1f', $dg_price) : sprintf('%.1f', $market_price),
'tags' => $tags,
'cover_img' => togetherFilePath($datum->cover_img),
];
......@@ -206,6 +208,7 @@ public function getDetail(Request $request)
{
$mer_id = 0; //获取绑定的商户ID
$goods_id = $request->goods_id ?? null;
$buycode = $request->buycode ?? null; //直购码,不传即原价购买
$goods = GoodModel::find($goods_id);
if (!$goods) {
return $this->JsonResponse('', '参数错误', 201);
......@@ -256,7 +259,7 @@ public function getDetail(Request $request)
//标签
$tags = $goods->tags ? json_decode($goods->tags, true) : [];
$skuOne = [
'goods_price' => $mer_id ? $dg_price : $market_price,
'goods_price' => $buycode ? $dg_price : $market_price,
'stock' => $stock,
'pic' => isset($skuOneData['pic']) ? $skuOneData['pic'] : ''
];
......@@ -264,7 +267,7 @@ public function getDetail(Request $request)
'id' => $goods->id,
'goods_img' => $cover,
'dg_price' => sprintf('%.2f', $dg_price),
'goods_price' => $mer_id ? sprintf('%.2f', $dg_price) : sprintf('%.2f', $market_price),
'goods_price' => $buycode ? sprintf('%.2f', $dg_price) : sprintf('%.2f', $market_price),
'market_price' => sprintf('%.2f', $market_price),
'stock' => $stock,
'goods_name' => $goods->goods_name,
......@@ -287,16 +290,17 @@ public function getAttrKey(Request $request)
{
$goods_id = $request->goods_id ?? null;
$attr_name = $request->attr_name ?? null; //22寸,蓝色,
$buycode = $request->buycode ?? null; //直购码,不传即原价购买
$mer_id = 0;
$goods = GoodModel::find($goods_id);
if (!$goods) {
return $this->JsonResponse('', '参数错误', 201);
}
// 获取已认证的用户,如果未认证则返回 null
$userObj = optional(Auth::user());
if ($userObj) {
$mer_id = $userObj->merchant_id;
}
// // 获取已认证的用户,如果未认证则返回 null
// $userObj = optional(Auth::user());
// if ($userObj) {
// $mer_id = $userObj->merchant_id;
// }
$attr_sn = '';
if ($attr_name) {
......@@ -320,7 +324,7 @@ public function getAttrKey(Request $request)
$data = [
// 'market_price' => sprintf('%.2f', $market_price),
// 'dg_price' => sprintf('%.2f', $cg_price),
'goods_price' => $mer_id ? sprintf('%.2f', $cg_price) : sprintf('%.2f', $market_price),
'goods_price' => $buycode ? sprintf('%.2f', $cg_price) : sprintf('%.2f', $market_price),
'stock' => $stock,
'pic' => $attrObj->pic ? togetherFilePath($attrObj->pic) : ''
];
......
......@@ -170,6 +170,8 @@ private function codeToSession($code)
}
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret=' . $secret . '&js_code=' . $code . '&grant_type=authorization_code';
$wx_data = json_decode(file_get_contents($url), true);
//Log::add('url:', $url);
if (isset($wx_data['errcode'])) {
Log::add('codeToSession请求微信接口异常', $wx_data);
//return $this->JsonResponse('', '请求微信接口异常', 201);
......
......@@ -175,9 +175,9 @@ public function info(Request $request)
$frozen_balance = UserPointChangeRec::where('user_id', $user_id)->where('change_type', 1)->where('point_state', 3)->sum('point_amount');
$thawing_balance = UserPointChangeRec::where('user_id', $user_id)->where('change_type', 1)->where('point_state', 2)->sum('point_amount');
$balance = number_format($user->balance, 2, '.', '');// 余额
$frozen_balance = number_format($frozen_balance, 2, '.', '');// 冻结中
$thawing_balance = number_format($thawing_balance, 2, '.', '');// 解冻中
$balance = number_format($user->balance, 2, '.', ''); // 余额
$frozen_balance = number_format($frozen_balance, 2, '.', ''); // 冻结中
$thawing_balance = number_format($thawing_balance, 2, '.', ''); // 解冻中
return $this->JsonResponse([
'user_id' => $user->id,
......@@ -188,8 +188,8 @@ public function info(Request $request)
//'status' => $user->status,
'merchant_id' => $merchant_id,
'buycode' => $user->buycode ?? '',
'balance' => $balance ?? 0,// 可用积分余额
'frozen_balance' => $frozen_balance ?? 0,// 冻结中的积分
'balance' => $balance ?? 0, // 可用积分余额
'frozen_balance' => $frozen_balance ?? 0, // 冻结中的积分
'thawing_balance' => $thawing_balance ?? 0 // 解冻中的积分
]);
}
......@@ -386,7 +386,7 @@ public function share(Request $request)
$user_time = strtotime($userObj->created_at);
//用户注册时间大于分享人注册时间 才可以分享
Log::add('share','分享人注册时间:'. $sp_time .';当前用户注册时间:'. $user_time,$request);
Log::add('share', '分享人注册时间:' . $sp_time . ';当前用户注册时间:' . $user_time, $request);
if ($user_time > $sp_time) {
$user_id = $userObj->id;
$spuid = $userObj->spuid; //是否已有推荐人
......@@ -445,19 +445,38 @@ public function getMyFriend(Request $request)
'indirectAmount' => 0,
'list' => []
];
//直推佣金总额 indirect
$directAmount = OrderDivideRecord::where(['sh_type' => 1, 'um_id' => $user_id])->sum('divide_price');
$data['directAmount'] = (float)$directAmount;
//直推佣金总额 indirect【改成总积分了】
$directAmount = UserPointChangeRec::where('change_type', 1)
->where('point_state', 1)
->where('source', 1)
->where('user_id', $user_id)
->selectRaw('sum(point_amount) as sum_point')
->first();
$data['directAmount'] = (float)($directAmount->sum_point ?? 0);
//间推佣金总额 indirect
$indirectAmount = OrderDivideRecord::where(['sh_type' => 2, 'um_id' => $user_id])->sum('divide_price');
$data['indirectAmount'] = (float)$indirectAmount;
$indirectAmount = UserPointChangeRec::where('change_type', 1)
->where('point_state', 1)
->where('source', 2)
->where('user_id', $user_id)
->selectRaw('sum(point_amount) as sum_point')
->first();
$data['indirectAmount'] = (float)($indirectAmount->sum_point ?? 0);
$listData = $sql->offset(($page - 1) * $limit)->limit($limit)->orderBy('created_at', 'DESC')->get();
if ($listData->toArray()) {
foreach ($listData as $item) {
//获取会员给我产生的佣金
$uid = $item->id;
$divide_price = OrderDivideRecord::where(['sh_type' => $type, 'um_id' => $user_id, 'user_id' => $uid])->sum('divide_price');
$divide_price = UserPointChangeRec::where('change_type', 1)
->where('point_state', 1)
->where('source', 1)
->whereHas('order', function($query) use ($item) {
$query->where('user_id', $item->id)
->where('pay_type', 1)
->where('pay_status', 1)
->where('order_status', '!=', 7);
})
->sum('point_amount');
$data['list'][] = [
'name' => $item->name,
......@@ -476,7 +495,7 @@ public function bindBuycode(Request $request)
{
$userObj = $request->user();
if ($userObj->buycode) {
return $this->JsonResponse('已经绑定过直购码');//直接返回成功
return $this->JsonResponse('已经绑定过直购码'); //直接返回成功
}
$code = $request->code ?? '';
......@@ -562,24 +581,4 @@ public function getUserPointList(Request $request)
'list' => $list
]);
}
}
......@@ -8,6 +8,7 @@
use Illuminate\Support\Facades\DB;
use Exception;
use App\Models\User as UserModel;
use App\Models\OrderInfo;
class UserPointChangeRec extends Model
{
......@@ -19,6 +20,13 @@ public function user()
return $this->belongsTo(User::class, 'user_id');
}
public function order()
{
return $this->belongsTo(OrderInfo::class, 'order_id');
}
//积分来源定义【source】
public const source = [
1 => '直推',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment