UserCollect.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * 用户收藏模型
  4. * wesmiler
  5. */
  6. namespace app\weixin\model;
  7. use app\weixin\service\PRedis;
  8. use think\Model;
  9. class UserCollect extends Model
  10. {
  11. protected $table = 'sg_user_collect';
  12. /**
  13. * 获取用户收藏数量
  14. * @param $userId 用户ID
  15. * @return float|string
  16. */
  17. public static function getUserCount($userId, $isAuth=0){
  18. $where = ['user_id'=> $userId, 'type'=> 1, 'status'=> 1];
  19. $collectExpire = Member::where(['id'=> $userId])->value('collect_expire');
  20. if(!$isAuth && $collectExpire - time() <= 0){
  21. return 0;
  22. }
  23. $count = UserCollect::where($where)->count('id');
  24. return intval($count);
  25. }
  26. /**
  27. * 获取关注用户列表
  28. * @param $params
  29. * @param $pageSize
  30. * @param string $field
  31. * @return $this
  32. */
  33. public static function getUserList($userId, $params=[], $pageSize=20, $field=''){
  34. // 验证有效时间,会员不限制
  35. $where = ['uc.user_id'=> $userId,'uc.status'=> 1,'uc.type'=>1];
  36. $memberInfo = Member::where(['id'=> $userId])->field('collect_expire,vip_auth,vip_expire')->find();
  37. $vipAuth = isset($memberInfo['vip_auth']) ? intval($memberInfo['vip_auth']) : 0;
  38. $vipExpire = isset($memberInfo['vip_expire']) ? $memberInfo['vip_expire'] : 0;
  39. $collectExpire = isset($memberInfo['collect_expire'])? intval($memberInfo['collect_expire']) : 0;
  40. if((!$vipAuth || $vipExpire < time()) && $collectExpire - time() <= 0){
  41. UserCollect::where(['user_id' => $userId])
  42. ->update(['status' => 2, 'remark'=> '关注过期', 'updated_at' => date('Y-m-d H:i:s')]);
  43. return false;
  44. }
  45. $field = $field? $field : 'uc.id,uc.user_id,uc.type,uc.source_id,u.real_name,u.user_nickname,u.avatar,u.sex,u.birthday,mp.graduate,mp.education,mp.height,mp.weight,mp.show_graduate,mp.show_company,mp.company,mp.occupation,mp.province,mp.city,mp.home_province,mp.home_city';
  46. return UserCollect::alias('uc')
  47. ->join('user u','u.id=uc.source_id','left')
  48. ->join('user_profile mp', 'mp.userid=u.id', 'left')
  49. ->where($where)
  50. ->field($field)
  51. ->paginate($pageSize)
  52. ->each(function($item, $k){
  53. $item['avatar'] = isset($item['avatar']) ? cmf_get_image_preview_url($item['avatar']) : '';
  54. if (isset($item['birthday'])) {
  55. $birthday = isset($item['birthday']) ? $item['birthday'] : 0;
  56. $item['birthday_txt'] = $birthday ? date('Y年m月d日', $birthday) : '无';
  57. $item['birthday_code'] = $birthday ? date('Y-m-d', $birthday) : '';
  58. $item['birthday_day'] = $birthday ? date('y年', $birthday) : '';
  59. }
  60. $item['height_txt'] = '无';
  61. if (isset($item['height'])) {
  62. $height = isset($item['height']) ? intval($item['height']) : 0;
  63. $item['height_txt'] = $height ? intval($item['height']) . 'CM' : '无';
  64. }
  65. $item['weight_txt'] = '无';
  66. if (isset($item['weight'])) {
  67. $weight = isset($item['weight']) ? intval($item['weight']) : 0;
  68. $item['weight_txt'] = $weight ? intval($item['weight']) . 'KG' : '无';
  69. }
  70. // 学历
  71. $educations = config('weixin.educations');
  72. $edu = isset($item['education']) ? $item['education'] : 0;
  73. $item['education_txt'] = $edu && isset($educations[$edu]) ? $educations[$edu] : '';
  74. // 地址
  75. $province = isset($item['province']) ? trim($item['province']) : '';
  76. $city = isset($item['city']) ? trim($item['city']) : '';
  77. $homeProvince = isset($item['home_province']) ? trim($item['home_province']) : '';
  78. $homeCity = isset($item['home_city']) ? trim($item['home_city']) : '';
  79. $item['now_address'] = trim($province . ' ' . $city);
  80. $item['home_address'] = trim($homeProvince . ' ' . $homeCity);
  81. /* // 学历
  82. $item['education'] = '无';
  83. if (isset($item['education'])) {
  84. $educations = config('weixin.educations');
  85. $edu = isset($item['education']) ? $item['education'] : 0;
  86. $item['education_txt'] = isset($educations[$edu]) ? $educations[$edu] : '无';
  87. }
  88. // 地址
  89. $item['now_address'] = '无';
  90. if (isset($item['province'])) {
  91. $province = isset($item['province']) ? trim($item['province']) : '';
  92. $city = isset($item['city']) ? trim($item['city']) : '';
  93. $item['now_address'] = trim($province . ' ' . $city);
  94. }
  95. $item['home_address'] = '无';
  96. if (isset($item['home_province'])) {
  97. $homeProvince = isset($item['home_province']) ? trim($item['home_province']) : '';
  98. $homeCity = isset($item['home_city']) ? trim($item['home_city']) : '';
  99. $item['home_address'] = trim($homeProvince . ' ' . $homeCity);
  100. }*/
  101. // 格式化婚姻状况
  102. $item['married_txt'] = '无';
  103. if (isset($item['married'])) {
  104. $marrieds = config('weixin.marrieds');
  105. $married = $item['married'] ? intval($item['married']) : 0;
  106. $item['married_txt'] = $married > 0 && isset($marrieds[$married - 1]) ? $marrieds[$married - 1] : '无';
  107. }
  108. return $item;
  109. });
  110. }
  111. /**
  112. * 获取关注我的用户列表
  113. * @param $params
  114. * @param $pageSize
  115. * @param string $field
  116. * @return $this
  117. */
  118. public static function getFollowUserList($userId, $params=[], $pageSize=20, $field=''){
  119. // 验证有效时间,会员不限制
  120. $page = input('page', 1);
  121. $where = ['uc.source_id'=> $userId,'uc.status'=> 1,'uc.type'=>1];
  122. $cacheKey = "cache:collect:follow_".$userId.'_'.$page.'_'.md5(json_encode($params).$pageSize.$field);
  123. $dataList = PRedis::get($cacheKey);
  124. if($dataList){
  125. return $dataList;
  126. }
  127. $field = $field? $field : 'uc.id,uc.user_id,uc.type,uc.source_id,u.real_name,u.user_nickname,u.avatar,u.sex,u.birthday,mp.graduate,mp.education,mp.height,mp.weight,mp.show_graduate,mp.show_company,mp.company,mp.occupation,mp.province,mp.city,mp.home_province,mp.home_city';
  128. $query = UserCollect::alias('uc')
  129. ->distinct(true)
  130. ->join('user u','u.id=uc.user_id','left')
  131. ->join('user_profile mp', 'mp.userid=u.id', 'left')
  132. ->field($field)
  133. ->where($where)
  134. ->group('user_id');
  135. $dataList = $query? $query->paginate($pageSize)
  136. ->each(function($item, $k){
  137. $item['avatar'] = isset($item['avatar']) ? cmf_get_image_preview_url($item['avatar']) : '';
  138. if (isset($item['birthday'])) {
  139. $birthday = isset($item['birthday']) ? $item['birthday'] : 0;
  140. $item['birthday_txt'] = $birthday ? date('Y年m月d日', $birthday) : '无';
  141. $item['birthday_code'] = $birthday ? date('Y-m-d', $birthday) : '';
  142. $item['birthday_day'] = $birthday ? date('y年', $birthday) : '';
  143. }
  144. $item['height_txt'] = '无';
  145. if (isset($item['height'])) {
  146. $height = isset($item['height']) ? intval($item['height']) : 0;
  147. $item['height_txt'] = $height ? intval($item['height']) . 'CM' : '无';
  148. }
  149. $item['weight_txt'] = '无';
  150. if (isset($item['weight'])) {
  151. $weight = isset($item['weight']) ? intval($item['weight']) : 0;
  152. $item['weight_txt'] = $weight ? intval($item['weight']) . 'KG' : '无';
  153. }
  154. // 学历
  155. $educations = config('weixin.educations');
  156. $edu = isset($item['education']) ? $item['education'] : 0;
  157. $item['education_txt'] = $edu && isset($educations[$edu]) ? $educations[$edu] : '';
  158. // 地址
  159. $province = isset($item['province']) ? trim($item['province']) : '';
  160. $city = isset($item['city']) ? trim($item['city']) : '';
  161. $homeProvince = isset($item['home_province']) ? trim($item['home_province']) : '';
  162. $homeCity = isset($item['home_city']) ? trim($item['home_city']) : '';
  163. $item['now_address'] = trim($province . ' ' . $city);
  164. $item['home_address'] = trim($homeProvince . ' ' . $homeCity);
  165. /* // 学历
  166. $item['education'] = '无';
  167. if (isset($item['education'])) {
  168. $educations = config('weixin.educations');
  169. $edu = isset($item['education']) ? $item['education'] : 0;
  170. $item['education_txt'] = isset($educations[$edu]) ? $educations[$edu] : '无';
  171. }
  172. // 地址
  173. $item['now_address'] = '无';
  174. if (isset($item['province'])) {
  175. $province = isset($item['province']) ? trim($item['province']) : '';
  176. $city = isset($item['city']) ? trim($item['city']) : '';
  177. $item['now_address'] = trim($province . ' ' . $city);
  178. }
  179. $item['home_address'] = '无';
  180. if (isset($item['home_province'])) {
  181. $homeProvince = isset($item['home_province']) ? trim($item['home_province']) : '';
  182. $homeCity = isset($item['home_city']) ? trim($item['home_city']) : '';
  183. $item['home_address'] = trim($homeProvince . ' ' . $homeCity);
  184. }*/
  185. // 格式化婚姻状况
  186. $item['married_txt'] = '无';
  187. if (isset($item['married'])) {
  188. $marrieds = config('weixin.marrieds');
  189. $married = $item['married'] ? intval($item['married']) : 0;
  190. $item['married_txt'] = $married > 0 && isset($marrieds[$married - 1]) ? $marrieds[$married - 1] : '无';
  191. }
  192. return $item;
  193. }) : [];
  194. $dataList = $dataList? $dataList->toArray() : [];
  195. if($dataList){
  196. $dataList['total'] = $query->count();
  197. }
  198. PRedis::set($cacheKey, $dataList, rand(2,5));
  199. return $dataList;
  200. }
  201. /**
  202. * 验证是否收藏过
  203. * @param $userId 当前有用户
  204. * @param $sourceId 收藏对象ID
  205. * @param int $type 收藏类型:1-用户,2-活动
  206. * @return mixed
  207. */
  208. public static function checkCollect($userId, $sourceId, $type = 1){
  209. $where = ['user_id'=> $userId, 'type'=> $type, 'source_id'=> $sourceId,'status'=> 1];
  210. return UserCollect::where($where)->value('id');
  211. }
  212. /**
  213. * 获取关注用户列表
  214. * @param $params
  215. * @param $pageSize
  216. * @param string $field
  217. * @return $this
  218. */
  219. public static function getActivityList($params=[], $pageSize=20, $field=''){
  220. $where = ['uc.status'=> 1,'uc.type'=>2];
  221. $field = $field? $field : 'uc.id,uc.user_id,uc.type,uc.source_id,a.title,a.cover_img as thumb,a.price,a.nums,a.addtime,a.starttime,a.endtime';
  222. return UserCollect::alias('uc')
  223. ->join('activity a','a.id=uc.source_id','left')
  224. ->where($where)
  225. ->field($field)
  226. ->paginate($pageSize)
  227. ->each(function($item, $k){
  228. $item['thumb'] = isset($item['thumb']) ? cmf_get_image_preview_url($item['thumb']) : '';
  229. $startTime = isset($item['starttime'])? $item['starttime'] : 0;
  230. $endTime = isset($item['endtime'])? $item['endtime'] : 0;
  231. $addTime = isset($item['addtime'])? $item['addtime'] : 0;
  232. if($startTime<=time() && $endTime>= time()){
  233. $item['act_status'] = 1; // 进行中
  234. }else if($endTime < time()){
  235. $item['act_status'] = 2; // 已结束
  236. }else if($startTime>time()){
  237. $item['act_status'] = 0; // 未开始
  238. }
  239. $item['thumb'] = $item['thumb']? cmf_get_image_preview_url($item['thumb']) : '';
  240. $item['publish_at'] = $addTime? date('Y-m-d H:i:s', $addTime) : date('Y-m-d H:i:s');
  241. $item['start_at'] = $startTime? date('Y-m-d H:i:s', $startTime) : date('Y-m-d H:i:s');
  242. $item['end_at'] = $endTime? date('Y-m-d H:i:s', $endTime) : date('Y-m-d H:i:s');
  243. // 报名人列表和人数
  244. $showNum = config('activity.showNum');
  245. $showNum = $showNum? $showNum : 6;
  246. $id = isset($item['id'])? $item['id'] : 0;
  247. $result = Books::getBookList($id, $showNum);
  248. $item['book_num'] = isset($result['total'])? intval($result['total']) : 0;
  249. $item['book_list'] = isset($result['data'])? $result['data'] : [];
  250. return $item;
  251. });
  252. }
  253. }