WechatMinprogramUserService.php 6.2 KB

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