User.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\model;
  20. use app\common\enum\OrderEnum;
  21. use app\common\enum\YesNoEnum;
  22. use app\common\service\FileService;
  23. use think\model\concern\SoftDelete;
  24. class User extends BaseModel
  25. {
  26. use SoftDelete;
  27. protected $deleteTime = 'delete_time';
  28. //多对多关联
  29. public function userLabelIndex()
  30. {
  31. return $this->belongsToMany(UserLabel::class,UserLabelIndex::class,'label_id','user_id')->field('name');
  32. }
  33. //用户信息搜索
  34. public function searchKeywordAttr($query, $value, $data)
  35. {
  36. if ($value) {
  37. $query->where('sn|nickname|mobile', 'like', '%' . $value . '%');
  38. }
  39. }
  40. //等级搜索
  41. public function searchLevelAttr($query, $value, $data)
  42. {
  43. if ($value) {
  44. $query->where('level', '=', $value);
  45. }
  46. }
  47. //标签搜索
  48. public function searchLabelIdAttr($query, $value, $data)
  49. {
  50. if ($value) {
  51. $userIds = UserLabelIndex::where(['label_id' => $value])->column('user_id');
  52. $query->where('id', 'in', $userIds);
  53. }
  54. }
  55. //注册来源
  56. public function searchSourceAttr($query, $value, $data)
  57. {
  58. if ($value) {
  59. $query->where('register_source', '=', $value);
  60. }
  61. }
  62. //消费金额搜索
  63. public function searchMinAmountAttr($query, $value, $data)
  64. {
  65. if ($value) {
  66. $query->where('total_order_amount', '>=', $value);
  67. }
  68. }
  69. //消费金额搜索
  70. public function searchMaxAmountAttr($query, $value, $data)
  71. {
  72. if ($value) {
  73. $query->where('total_order_amount', '<=', $value);
  74. }
  75. }
  76. //注册时间筛选
  77. public function searchCreateStartTimeAttr($query, $value, $data)
  78. {
  79. if ($value) {
  80. $query->where('create_time', '>=', $value);
  81. }
  82. }
  83. //注册时间筛选
  84. public function searchCreateEndTimeAttr($query, $value, $data)
  85. {
  86. if ($value) {
  87. $query->where('create_time', '<=', $value);
  88. }
  89. }
  90. //禁用状态搜索
  91. public function searchDisableAttr($query, $value, $data)
  92. {
  93. if (in_array($value, [0, 1])) {
  94. $query->where('disable', '=', $value);
  95. }
  96. }
  97. //关联用户授权模型
  98. public function userAuth()
  99. {
  100. return $this->hasOne(UserAuth::class, 'user_id');
  101. }
  102. //关联用户等级模型
  103. public function userLevel()
  104. {
  105. return $this->hasOne(UserLevel::class, 'id', 'level')->bind(['name', 'discount','rank']);
  106. }
  107. /**
  108. * @notes 头像获取器 - 用于头像地址拼接域名
  109. * @param $value
  110. * @return string
  111. * @author Tab
  112. * @date 2021/7/17 14:28
  113. */
  114. public function getAvatarAttr($value)
  115. {
  116. return trim($value) ? FileService::getFileUrl($value) : '';
  117. }
  118. //最后登录时间格式化
  119. public function getLoginTimeAttr($value)
  120. {
  121. if($value){
  122. return date('Y-m-d H:i:s', $value);
  123. }
  124. return '';
  125. }
  126. /**
  127. * @notes 一个用户都有一个分销表,一对一关联
  128. * @return \think\model\relation\HasOne
  129. * @author Tab
  130. * @date 2021/7/17 14:32
  131. */
  132. public function distribution()
  133. {
  134. return $this->hasOne(Distribution::class, 'user_id', 'id')
  135. ->field('user_id,first_leader,code,earnings');
  136. }
  137. /**
  138. * @notes 获取用户昵称
  139. * @param $userId
  140. * @return mixed|string
  141. * @author Tab
  142. * @date 2021/7/27 17:12
  143. */
  144. public static function getNickname($userId)
  145. {
  146. $user = self::findOrEmpty($userId)->toArray();
  147. if ($user) {
  148. return $user['nickname'];
  149. }
  150. return '';
  151. }
  152. /**
  153. * @notes 获取用户粉丝数量(一级/二级)
  154. * @param $userId
  155. * @return int
  156. * @author Tab
  157. * @date 2021/7/27 17:14
  158. */
  159. public static function getFans($userId)
  160. {
  161. return self::whereOr([
  162. 'first_leader' => $userId,
  163. 'second_leader' => $userId
  164. ])->count();
  165. }
  166. /**
  167. * @notes 粉丝中有多少人是分销商
  168. * @param $userId
  169. * @return int
  170. * @author Tab
  171. * @date 2021/9/14 17:44
  172. */
  173. public static function getFansDistribution($userId)
  174. {
  175. $userIds = self::whereOr([
  176. 'first_leader' => $userId,
  177. 'second_leader' => $userId
  178. ])->column('id');
  179. return Distribution::where([
  180. ['user_id', 'in', $userIds],
  181. ['is_distribution', '=', YesNoEnum::YES],
  182. ])->count();
  183. }
  184. /**
  185. * @notes 获取用户一级粉丝数量
  186. * @param $userId
  187. * @return int
  188. * @author Tab
  189. * @date 2021/8/5 15:07
  190. */
  191. public static function getLevelOneFans($userId)
  192. {
  193. return self::where('first_leader', $userId)->count();
  194. }
  195. /**
  196. * @notes 一级粉丝中有多少是分销商
  197. * @param $userId
  198. * @return int
  199. * @author Tab
  200. * @date 2021/9/14 17:49
  201. */
  202. public static function getLevelOneFansDistribution($userId)
  203. {
  204. $userIds = self::where('first_leader', $userId)->column('id');
  205. return Distribution::where([
  206. ['user_id', 'in', $userIds],
  207. ['is_distribution', '=', YesNoEnum::YES],
  208. ])->count();
  209. }
  210. /**
  211. * @notes 获取用户二级粉丝数量
  212. * @param $userId
  213. * @return int
  214. * @author Tab
  215. * @date 2021/8/5 15:07
  216. */
  217. public static function getLevelTwoFans($userId)
  218. {
  219. return self::where('second_leader', $userId)->count();
  220. }
  221. /**
  222. * @notes 二级粉丝中有多少是分销商
  223. * @param $userId
  224. * @return int
  225. * @author Tab
  226. * @date 2021/9/14 17:50
  227. */
  228. public static function getLevelTwoFansDistribution($userId)
  229. {
  230. $userIds = self::where('second_leader', $userId)->column('id');
  231. return Distribution::where([
  232. ['user_id', 'in', $userIds],
  233. ['is_distribution', '=', YesNoEnum::YES],
  234. ])->count();
  235. }
  236. /**
  237. * @notes 用户粉丝数量(一级/二级)获取器
  238. * @param $value
  239. * @return int
  240. * @author Tab
  241. * @date 2021/8/5 17:35
  242. */
  243. public function getFansAttr($userId)
  244. {
  245. return self::getFans($userId);
  246. }
  247. /** 用户已支付订单总金额获取器
  248. * @notes
  249. * @param $userId
  250. * @return float
  251. * @author Tab
  252. * @date 2021/8/5 18:03
  253. */
  254. public function getOrderAmountAttr($userId)
  255. {
  256. return Order::where(['user_id' => $userId, 'pay_status' => YesNoEnum::YES])->sum('order_amount');
  257. }
  258. /**
  259. * @notes 用户已支付订单总数量获取器
  260. * @param $userId
  261. * @return int
  262. * @author Tab
  263. * @date 2021/8/5 18:04
  264. */
  265. public function getOrderNumAttr($userId)
  266. {
  267. return Order::where(['user_id' => $userId, 'pay_status' => YesNoEnum::YES])->count();
  268. }
  269. /**
  270. * @notes 性别获取器
  271. * @param $value
  272. * @param $data
  273. * @return string
  274. * @author cjhao
  275. * @date 2021/8/18 11:47
  276. */
  277. public function getSexAttr($value, $data)
  278. {
  279. switch ($value) {
  280. case 0:
  281. return '未知';
  282. case 1:
  283. return '男';
  284. case 2:
  285. return '女';
  286. }
  287. }
  288. /**
  289. * @notes 生日获取器
  290. * @param $value
  291. * @param $data
  292. * @return false|string
  293. * @author cjhao
  294. * @date 2021/8/18 11:50
  295. */
  296. public function getBirthdayAttr($value, $data)
  297. {
  298. if ($value) {
  299. return date('Y-m-d', $value);
  300. }
  301. return '';
  302. }
  303. /**
  304. * @notes 头像修改器
  305. * @param $value
  306. * @return mixed
  307. * @author Tab
  308. * @date 2021/8/24 14:44
  309. */
  310. public function setAvatarAttr($value)
  311. {
  312. return FileService::setFileUrl($value);
  313. }
  314. /**
  315. * @notes 用户收益获取器
  316. * @param $value
  317. * @return int
  318. * @author Tab
  319. * @date 2021/9/7 15:41
  320. */
  321. public function getUserEarningsAttr($value)
  322. {
  323. return empty($value) ? 0 : $value;
  324. }
  325. /**
  326. * @notes 邀请人信息
  327. * @param $id
  328. * @return array
  329. * @author Tab
  330. * @date 2021/9/13 18:10
  331. */
  332. public static function getInviterInfo($inviterId, $userId)
  333. {
  334. $inviter = self::findOrEmpty($inviterId)->toArray();
  335. if (empty($inviter)) {
  336. $info = '';
  337. } else {
  338. $info = $inviter['nickname'] . '(' . $inviter['sn'] .')';
  339. }
  340. $count = self::where('inviter_id', $userId)->count();
  341. return [
  342. 'name' => $info,
  343. 'num' => $count
  344. ];
  345. }
  346. /**
  347. * @notes 获取上级分销商
  348. * @param $id
  349. * @return string[]
  350. * @author Tab
  351. * @date 2021/9/13 18:18
  352. */
  353. public static function getFirstLeader($userInfo)
  354. {
  355. $leader = self::findOrEmpty($userInfo['first_leader'] ?? 0)->toArray();
  356. if (! empty($leader)) {
  357. $info = $leader['nickname'] . '(' . $leader['sn'] .')';
  358. } else {
  359. if (isset($userInfo['admin_update_leader']) && $userInfo['admin_update_leader']) {
  360. $info = '系统';
  361. } else {
  362. $info = '-';
  363. }
  364. }
  365. return [
  366. 'name' => $info,
  367. 'nickname' => $info,
  368. ];
  369. }
  370. }