优惠券小程序

UserService.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Service;
  3. use App\Models\Redis\RedisModel;
  4. use App\Models\SysFeedback;
  5. use App\Models\UserInterestGoods;
  6. use App\Models\UserShareRecord;
  7. use App\Models\WxUser;
  8. use Illuminate\Support\Facades\DB;
  9. class UserService
  10. {
  11. const USER_SEARCH_HISTORY = 'yhq-smallapp::User_Search_History_';
  12. /**
  13. * 商品收藏/浏览历史
  14. * @param $userId integer 用户id
  15. * @param $itemId integer 宝贝id
  16. * @param $params array 商品其它属性
  17. * */
  18. public static function goodsInterest($userId, $itemId, $type, $params)
  19. {
  20. if($type == 1) {
  21. # 判断是否已收藏过该商品
  22. $isCollect = UserInterestGoods::hadInterest($userId, $itemId);
  23. if($isCollect) return 1003;
  24. }
  25. # 保存用户感兴趣商品信息
  26. $result = UserInterestGoods::updateOrCreate([
  27. 'user_id' => $userId, 'item_id' => $itemId, 'type' => $type
  28. ], $params);
  29. return $result ? 0 : 1004;
  30. }
  31. /**
  32. * 商品收藏/浏览历史列表
  33. * */
  34. public static function getUserInterestList($userId, $type, $page, $pageSize)
  35. {
  36. # 获取商品收藏/浏览历史列表
  37. list($list, $count) = UserInterestGoods::interestListOfUser($userId, $type, $page, $pageSize);
  38. if(!$count) return [[], 0];
  39. foreach ($list as $item) {
  40. # 在售价、券后价和优惠券金额单位换算
  41. $item->item_price = $item->item_price / 100;
  42. $item->item_end_price = $item->item_end_price / 100;
  43. $item->coupon_money = $item->coupon_money / 100;
  44. # 更新日期
  45. $item->date = date('Y-m-d', strtotime($item->updated_at));
  46. }
  47. if($type == 2) { // 浏览历史按日期分组
  48. $data = $midData = [];
  49. foreach ($list as $datum) {
  50. $midData[$datum->date][] = $datum;
  51. }
  52. foreach ($midData as $key=>$val) {
  53. $data[] = [
  54. 'date' => $key,
  55. 'list' => $val
  56. ];
  57. }
  58. return [$data, $count];
  59. }
  60. return [$list, $count];
  61. }
  62. /**
  63. * 收藏/浏览商品批量移除
  64. * */
  65. public static function removeInterest($userId, $ids, $type)
  66. {
  67. $ids = explode(',', $ids);
  68. return UserInterestGoods::removeInterest($userId, $ids, $type);
  69. }
  70. /**
  71. * 记录用户搜索历史
  72. * */
  73. public static function userSearch($keyword, $userId)
  74. {
  75. # 获取已存储的用户搜索历史信息
  76. $list = RedisModel::getAfterDecode(UserService::USER_SEARCH_HISTORY . $userId);
  77. if(empty($list)) $list = [];
  78. array_unshift($list, $keyword);
  79. $list = array_unique($list);
  80. if(count($list) > 50) $list = array_slice($list, 0, 50);
  81. # 存储用户搜索历史
  82. return RedisModel::setAfterEncode(UserService::USER_SEARCH_HISTORY . $userId, array_values($list));
  83. }
  84. /**
  85. * 获取用户搜索历史
  86. * */
  87. public static function getSearchHistory($userId)
  88. {
  89. $list = RedisModel::getAfterDecode(UserService::USER_SEARCH_HISTORY . $userId);
  90. return $list ? : [];
  91. }
  92. /**
  93. * 删除用户搜索历史
  94. * */
  95. public static function delSearchHistory($userId)
  96. {
  97. $result = RedisModel::del(UserService::USER_SEARCH_HISTORY . $userId);
  98. return $result ? 0 : 1008;
  99. }
  100. /**
  101. * 保存用户意见反馈
  102. * */
  103. public static function saveFeedback($userId, $desc)
  104. {
  105. return SysFeedback::saveFeedback($userId, $desc);
  106. }
  107. /**
  108. * 保存用户分享记录
  109. * */
  110. public static function saveShareRecord($userId, $ids, $type, &$errno)
  111. {
  112. $shareId = UserShareRecord::saveInfo($userId, $ids, $type);
  113. if(!$shareId) $errno = 1006;
  114. return $shareId;
  115. }
  116. /**
  117. * 获取用户分享记录
  118. * */
  119. public static function getShareInfo($userId, $shareId, &$errno)
  120. {
  121. # 查询分享基本信息
  122. $shareInfo = UserShareRecord::getShareInfo($userId, $shareId);
  123. # 获取分享商品信息
  124. $interestGoodsIdsStr = $shareInfo->interest_ids ?? '';
  125. if(empty($interestGoodsIdsStr)) {
  126. $errno = 1007;
  127. return [];
  128. }
  129. $interestGoodsIds = explode(',', $interestGoodsIdsStr);
  130. list($list, $count) = UserInterestGoods::interestListOfUser($userId, $shareInfo->type, 1, 100, $interestGoodsIds);
  131. if(!$count) {
  132. $shareInfo->goods_list = [];
  133. return $shareInfo;
  134. }
  135. foreach ($list as $item) {
  136. # 在售价、券后价和优惠券金额单位换算
  137. $item->item_price = $item->item_price / 10;
  138. $item->item_end_price = $item->item_end_price / 10;
  139. $item->coupon_money = $item->coupon_money / 10;
  140. }
  141. $shareInfo->goods_list = $list;
  142. return $shareInfo;
  143. }
  144. /**
  145. * 设置用户头像、昵称
  146. * */
  147. public static function setUserInfo($userId, $type, $avatarUrl, $nickname, &$errno)
  148. {
  149. if($type == 2) {
  150. # 上传用户头像
  151. $fileUrl = FileUploadService::uploadFile($avatarUrl);
  152. if(is_numeric($fileUrl)) {
  153. $errno = $fileUrl;
  154. return [];
  155. }
  156. } elseif($type == 1) {
  157. $fileUrl = $avatarUrl;
  158. }
  159. # 存储用户信息
  160. $result = WxUser::where('id', $userId)->where('enable', 1)->update([
  161. 'nickname' => $nickname, 'avatar_url' => $fileUrl
  162. ]);
  163. $errno = $result ? 0 : 1013;
  164. return ['nickname' => $nickname, 'avatar_url' => $fileUrl];
  165. }
  166. }