WechatMinprogramUserService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. *
  4. * @file:WechatMinprogramUserService.php
  5. * @Date: 2023/5/19
  6. * @Time: 11:07
  7. */
  8. namespace Modules\Channel\Services\WechatMinprogram;
  9. use Carbon\Carbon;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Redis;
  12. use Illuminate\Support\Str;
  13. use Modules\Channel\Models\MiniprogramUserVip;
  14. use Modules\Channel\Models\Order;
  15. use Modules\Channel\Models\Videos;
  16. use Modules\Channel\Models\VideoSeries;
  17. use Modules\Manage\Models\Miniprogram;
  18. class WechatMinprogramUserService
  19. {
  20. public const WATCH_RECORD_REDIS_KEY = 'watchrecord:uid:%s';
  21. public const WATCH_RECORD_REDIS_FIELD_PREFIX = 'video_id:';
  22. /**
  23. * 获取微信小程序用户信息
  24. * name: userInfoDetail
  25. * @param $uid
  26. * date 2023/05/19 11:08
  27. */
  28. public static function userInfoDetail($uid)
  29. {
  30. $userInfo = DB::table(getMiniProgramTableName(1, 'users'))->where('id', $uid)->first();
  31. if (!$userInfo) {
  32. return $userInfo;
  33. }
  34. $ju_chang = Miniprogram::where('id', $userInfo->miniprogram_id)->value('play_name');
  35. $result = [
  36. 'uid' => $userInfo->id,
  37. 'openid' => $userInfo->openid,
  38. 'yu_chang' => $ju_chang,
  39. 'ranse_start_at' => $userInfo->ranse_start_at ?: "",
  40. 'charge_coin' => $userInfo->charge_coin,
  41. 'reward_coin' => $userInfo->reward_coin,
  42. ];
  43. return array_merge($result, self::getLevelText($uid));
  44. }
  45. /**
  46. * 用户等级文字
  47. * name: getLevelText
  48. * @param $uid
  49. * @return array
  50. * date 2023/05/26 15:41
  51. */
  52. private static function getLevelText($uid)
  53. {
  54. $record = self::userVipRecord($uid);
  55. if ($record && Carbon::now()->lt(Carbon::createFromTimestamp(strtotime($record->end_time)))) {
  56. return ['is_vip' => 1, 'vip_text' => "vip会员", 'vip_end' => $record->end_time];
  57. }
  58. return ['is_vip' => 0, 'vip_text' => "-", 'vip_end' => ""];
  59. }
  60. public static function isVipUser(int $uid)
  61. {
  62. $record = self::userVipRecord($uid);
  63. if (!$record) {
  64. return false;
  65. }
  66. return Carbon::now()->lt(Carbon::createFromTimestamp(strtotime($record->end_time)));
  67. }
  68. private static function userVipRecord(int $uid)
  69. {
  70. return MiniprogramUserVip::where('uid', $uid)->first();
  71. }
  72. /**
  73. * 根据用户查充值记录
  74. * name: getUserOrderList
  75. * @param int $uid
  76. * date 2023/05/19 14:57
  77. */
  78. public static function getUserOrderList(int $uid, $param)
  79. {
  80. $pool = [
  81. 'MONTH' => '包月',
  82. 'QUARTER' => '包季度',
  83. 'YEAR' => '包年',
  84. ];
  85. $list = Order::join('pay_products', 'pay_products.id', '=', 'orders.pay_product_id')
  86. ->select('orders.price', 'orders.trade_no', "orders.video_id", 'orders.pay_end_at', 'pay_products.type', 'pay_products.price as product_price', 'pay_products.given')
  87. ->where('orders.status', 'PAID')
  88. ->where('orders.uid', $uid);
  89. if (getProp($param, 'puser_id', 0) > 0) {
  90. $list->where('orders.puser_id', $param['puser_id']);
  91. }
  92. if (getProp($param, 'user_id', 0) > 0) {
  93. $list->where('orders.user_id', $param['user_id']);
  94. }
  95. $list = $list->orderBy('orders.id', 'desc')
  96. ->paginate(getProp($param,'limit',15));
  97. foreach ($list as $item) {
  98. $item->pay_name = '微信支付';
  99. $item->status = '已完成';
  100. if ($item->type == 'COIN') {
  101. $item->rechare_coin = $item->product_price * 100;
  102. $item->pay_result =- $item->product_price * 100 + $item->given;
  103. } elseif (isset($pool[$item->type])) {
  104. $item->rechare_coin = "-";
  105. $item->pay_result = $pool[$item->type];
  106. } else {
  107. $item->rechare_coin = "-";
  108. $item->pay_result = '充值';
  109. }
  110. $item->from_page = $item->video_id > 0 ? "播放页" : "充值页";
  111. }
  112. return $list;
  113. }
  114. /**
  115. * 观看记录
  116. * name: getUserWatchRecord
  117. * @param mixed $uid 用户id
  118. * @return array
  119. * date 2023/05/19 15:57
  120. */
  121. public static function getUserWatchRecord($uid)
  122. {
  123. $key = sprintf(self::WATCH_RECORD_REDIS_KEY, $uid);
  124. $record = Redis::hgetall($key);
  125. $result = [];
  126. foreach ($record as $video_field => $watch_info) {
  127. if (!Str::startsWith($video_field, self::WATCH_RECORD_REDIS_FIELD_PREFIX)) {
  128. continue;
  129. }
  130. $video_id = Str::replace(self::WATCH_RECORD_REDIS_FIELD_PREFIX, '', $video_field);
  131. $info = explode('_', $watch_info);
  132. $result[] = [
  133. 'video_id' => $video_id,
  134. "video_name" => Videos::where('id',$video_id)->value('name'),
  135. 'video_series_sequence' => $info[0],
  136. 'watch_at' => get_date($info[1]),
  137. 'watch_time' => $info[1]
  138. ];
  139. }
  140. usort($result, function ($item1, $item2) {
  141. return $item1['watch_time'] > $item2['watch_time'];
  142. });
  143. return $result;
  144. }
  145. /***
  146. * 用户消费记录
  147. * name: getUserConsumeRecord
  148. * @param mixed $uid
  149. * @param int $limit
  150. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  151. * date 2023/05/26 15:40
  152. */
  153. public static function getUserConsumeRecord(mixed $uid,$limit = 15)
  154. {
  155. $tableName = 'coin_cost_record_' . ($uid % 8);
  156. $result = DB::table($tableName)->where('uid', $uid)->orderBy('id', 'desc')->paginate($limit);
  157. foreach ($result as $item) {
  158. $item->series_name = VideoSeries::where('video_id', $item->video_id)->where('series_sequence', $item->sequence)->select('series_name')->first()->series_name;
  159. $item->video_name = Videos::where('id', $item->video_id)->value('name');
  160. $item->coin_cost = $item->charge_coin_cost + $item->reward_coin_cost;
  161. }
  162. return $result;
  163. }
  164. }