UserLogic.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace App\Http\Logic\User;
  3. use App\Exceptions\CommonException;
  4. use App\Http\Enum\ErrorEnum;
  5. use App\Http\Logic\BaseLogic;
  6. use App\Http\Utils\BaseUtil;
  7. use App\Http\Utils\WechatAccountUtil;
  8. use App\Models\UserFinanceModel;
  9. use App\Models\UserModel;
  10. use App\Models\UserOrderCommissionModel;
  11. use Illuminate\Support\Facades\DB;
  12. class UserLogic extends BaseLogic
  13. {
  14. /**
  15. * 获取用户信息逻辑
  16. */
  17. public static function getUserInfoLogic()
  18. {
  19. $userId = BaseUtil::getUserId();
  20. $user = UserModel::query()
  21. ->where("id",$userId)
  22. ->select(["user_head_img_url","user_nickname","user_sex","user_balance","user_unreceive_balance","created_at","id"])
  23. ->first();
  24. $user["user_fans_num"] = UserModel::query()->where("invite_user_id",$userId)->count();
  25. $user["user_crash_balance"] = 0;//累计提现金额
  26. $user["user_unreceive_balance"] = UserOrderCommissionModel::query()
  27. ->where("user_id",$userId)
  28. ->where("commission_status",0)
  29. ->sum("order_commission");//带结算佣金
  30. return $user;
  31. }
  32. /**
  33. * 获取用户粉丝逻辑
  34. */
  35. public static function getFansListLogic()
  36. {
  37. $userId = BaseUtil::getUserId();
  38. $params = request()->all();
  39. $query = UserModel::query()
  40. ->where("invite_user_id",$userId)
  41. ->select(
  42. [
  43. "user_nickname",
  44. "user_head_img_url",
  45. "created_at"
  46. ]
  47. )->orderBy("id","desc");
  48. switch ($params["type"]){
  49. case 1:
  50. //未激活
  51. $query->where("user_order_type",0);
  52. break;
  53. case 2:
  54. //已激活
  55. $query->where("user_order_type",1);
  56. break;
  57. }
  58. $res = $query->paginate($params["page_size"]);
  59. return [
  60. "total"=>$res->total(),
  61. "list"=>$res->items()
  62. ];
  63. }
  64. /**
  65. * 收支明细逻辑
  66. */
  67. public static function getFinanceListLogic()
  68. {
  69. $userId = BaseUtil::getUserId();
  70. $params = request()->all();
  71. $query = UserFinanceModel::query()
  72. ->where("user_id",$userId);
  73. switch ($params["finance_type"]){
  74. case 1://收入
  75. $query->where("finance_type",$params["finance_type"]);
  76. break;
  77. case 2://提现
  78. $query->where("finance_type",$params["finance_type"]);
  79. break;
  80. }
  81. $res = $query
  82. ->select(
  83. [
  84. "finance_balance",
  85. "finance_type",
  86. "finance_number",
  87. "created_at",
  88. "cash_status",
  89. "id",
  90. "order_number"
  91. ]
  92. )
  93. ->paginate($params["page_size"]);
  94. return [
  95. "total"=>$res->total(),
  96. "list"=>$res->items()
  97. ];
  98. }
  99. /**
  100. * 流水详情逻辑
  101. */
  102. public static function getFinanceDetailLogic()
  103. {
  104. $userId = BaseUtil::getUserId();
  105. $params = request()->all();
  106. $finance = UserFinanceModel::query()
  107. ->where("user_id",$userId)
  108. ->select(
  109. [
  110. "finance_balance",
  111. "finance_remark",
  112. "finance_number",
  113. "created_at",
  114. "cash_status",
  115. "order_number"
  116. ]
  117. )->find($params["id"]);
  118. return $finance;
  119. }
  120. /**
  121. * 提现逻辑
  122. */
  123. public static function getCashLogic()
  124. {
  125. $params = request()->all();
  126. $crashBalance = $params["amount"];
  127. $userId = BaseUtil::getUserId();
  128. $user = UserModel::query()->find($userId);
  129. if($user->user_balance < $crashBalance){
  130. throw new CommonException(ErrorEnum::ERROR_CASH_ENUGH);
  131. }
  132. $key = "cashMoeny:".$userId;
  133. BaseUtil::setRedisLock($key,60);
  134. //开启事务
  135. DB::beginTransaction();
  136. //1.修改用户可提现金额
  137. $user = UserModel::query()->lockForUpdate()->find($userId);
  138. $userBalance = $user->user_balance;
  139. $user->user_balance -= $crashBalance;
  140. $userRes = $user->save();
  141. if(!$userRes){
  142. DB::rollBack();
  143. throw new CommonException(ErrorEnum::ERROR_CASH);
  144. }
  145. //2.写入提现记录
  146. $financeRes = UserFinanceModel::query()->insert(
  147. [
  148. "user_id"=>$userId,
  149. "user_before_balance"=>$userBalance,
  150. "user_after_balance"=>$userBalance-$crashBalance,
  151. "finance_balance"=>$crashBalance,
  152. "finance_remark"=>"用户提现",
  153. "finance_type"=>2,
  154. "finance_number"=>time(),
  155. "created_at"=>date("Y-m-d H:i:s"),
  156. "updated_at"=>date("Y-m-d H:i:s"),
  157. ]
  158. );
  159. if(!$financeRes){
  160. DB::rollBack();
  161. throw new CommonException(ErrorEnum::ERROR_CASH);
  162. }
  163. DB::commit();
  164. }
  165. /**
  166. * 获取用户推广二维码逻辑
  167. */
  168. public static function getRecommendUrlLogic()
  169. {
  170. $userId = BaseUtil::getUserId();
  171. $params = request()->all();
  172. $app = WechatAccountUtil::getApp($params["code"]);
  173. return $app->qrcode->forever($userId);
  174. }
  175. }