Commit aa4189f3 by yink

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

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