123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- namespace App\Service;
- use App\Models\Redis\RedisModel;
- use App\Models\SysFeedback;
- use App\Models\UserInterestGoods;
- use App\Models\UserShareRecord;
- use App\Models\WxUser;
- use Illuminate\Support\Facades\DB;
- class UserService
- {
- const USER_SEARCH_HISTORY = 'yhq-smallapp::User_Search_History_';
- /**
- * 商品收藏/浏览历史
- * @param $userId integer 用户id
- * @param $itemId integer 宝贝id
- * @param $params array 商品其它属性
- * */
- public static function goodsInterest($userId, $itemId, $type, $params)
- {
- if($type == 1) {
- # 判断是否已收藏过该商品
- $isCollect = UserInterestGoods::hadInterest($userId, $itemId);
- if($isCollect) return 1003;
- }
- # 保存用户感兴趣商品信息
- $result = UserInterestGoods::updateOrCreate([
- 'user_id' => $userId, 'item_id' => $itemId, 'type' => $type
- ], $params);
- return $result ? 0 : 1004;
- }
- /**
- * 商品收藏/浏览历史列表
- * */
- public static function getUserInterestList($userId, $type, $page, $pageSize)
- {
- # 获取商品收藏/浏览历史列表
- list($list, $count) = UserInterestGoods::interestListOfUser($userId, $type, $page, $pageSize);
- if(!$count) return [[], 0];
- foreach ($list as $item) {
- # 在售价、券后价和优惠券金额单位换算
- $item->item_price = $item->item_price / 100;
- $item->item_end_price = $item->item_end_price / 100;
- $item->coupon_money = $item->coupon_money / 100;
- # 更新日期
- $item->date = date('Y-m-d', strtotime($item->updated_at));
- }
- if($type == 2) { // 浏览历史按日期分组
- $data = $midData = [];
- foreach ($list as $datum) {
- $midData[$datum->date][] = $datum;
- }
- foreach ($midData as $key=>$val) {
- $data[] = [
- 'date' => $key,
- 'list' => $val
- ];
- }
- return [$data, $count];
- }
- return [$list, $count];
- }
- /**
- * 收藏/浏览商品批量移除
- * */
- public static function removeInterest($userId, $ids, $type)
- {
- $ids = explode(',', $ids);
- return UserInterestGoods::removeInterest($userId, $ids, $type);
- }
- /**
- * 记录用户搜索历史
- * */
- public static function userSearch($keyword, $userId)
- {
- # 获取已存储的用户搜索历史信息
- $list = RedisModel::getAfterDecode(UserService::USER_SEARCH_HISTORY . $userId);
- if(empty($list)) $list = [];
- array_unshift($list, $keyword);
- $list = array_unique($list);
- if(count($list) > 50) $list = array_slice($list, 0, 50);
- # 存储用户搜索历史
- return RedisModel::setAfterEncode(UserService::USER_SEARCH_HISTORY . $userId, array_values($list));
- }
- /**
- * 获取用户搜索历史
- * */
- public static function getSearchHistory($userId)
- {
- $list = RedisModel::getAfterDecode(UserService::USER_SEARCH_HISTORY . $userId);
- return $list ? : [];
- }
- /**
- * 删除用户搜索历史
- * */
- public static function delSearchHistory($userId)
- {
- $result = RedisModel::del(UserService::USER_SEARCH_HISTORY . $userId);
- return $result ? 0 : 1008;
- }
- /**
- * 保存用户意见反馈
- * */
- public static function saveFeedback($userId, $desc)
- {
- return SysFeedback::saveFeedback($userId, $desc);
- }
- /**
- * 保存用户分享记录
- * */
- public static function saveShareRecord($userId, $ids, $type, &$errno)
- {
- $shareId = UserShareRecord::saveInfo($userId, $ids, $type);
- if(!$shareId) $errno = 1006;
- return $shareId;
- }
- /**
- * 获取用户分享记录
- * */
- public static function getShareInfo($userId, $shareId, &$errno)
- {
- # 查询分享基本信息
- $shareInfo = UserShareRecord::getShareInfo($userId, $shareId);
- # 获取分享商品信息
- $interestGoodsIdsStr = $shareInfo->interest_ids ?? '';
- if(empty($interestGoodsIdsStr)) {
- $errno = 1007;
- return [];
- }
- $interestGoodsIds = explode(',', $interestGoodsIdsStr);
- list($list, $count) = UserInterestGoods::interestListOfUser($userId, $shareInfo->type, 1, 100, $interestGoodsIds);
- if(!$count) {
- $shareInfo->goods_list = [];
- return $shareInfo;
- }
- foreach ($list as $item) {
- # 在售价、券后价和优惠券金额单位换算
- $item->item_price = $item->item_price / 10;
- $item->item_end_price = $item->item_end_price / 10;
- $item->coupon_money = $item->coupon_money / 10;
- }
- $shareInfo->goods_list = $list;
- return $shareInfo;
- }
- /**
- * 设置用户头像、昵称
- * */
- public static function setUserInfo($userId, $type, $avatarUrl, $nickname, &$errno)
- {
- if($type == 2) {
- # 上传用户头像
- $fileUrl = FileUploadService::uploadFile($avatarUrl);
- if(is_numeric($fileUrl)) {
- $errno = $fileUrl;
- return [];
- }
- } elseif($type == 1) {
- $fileUrl = $avatarUrl;
- }
- # 存储用户信息
- $result = WxUser::where('id', $userId)->where('enable', 1)->update([
- 'nickname' => $nickname, 'avatar_url' => $fileUrl
- ]);
- $errno = $result ? 0 : 1013;
- return ['nickname' => $nickname, 'avatar_url' => $fileUrl];
- }
- }
|