User.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\model\user;
  12. use app\model\agent\AgentLevel;
  13. use app\model\order\StoreOrder;
  14. use app\model\system\SystemUserLevel;
  15. use crmeb\basic\BaseModel;
  16. use crmeb\traits\ModelTrait;
  17. use think\Model;
  18. /**
  19. * Class User
  20. * @package app\model\user
  21. */
  22. class User extends BaseModel
  23. {
  24. use ModelTrait;
  25. /**
  26. * @var string
  27. */
  28. protected $pk = 'uid';
  29. protected $name = 'user';
  30. protected $insert = ['add_time', 'add_ip', 'last_time', 'last_ip'];
  31. protected $hidden = [
  32. 'add_ip', 'account', 'clean_time', 'last_ip', 'pwd'
  33. ];
  34. /**
  35. * 自动转类型
  36. * @var string[]
  37. */
  38. protected $type = [
  39. 'birthday' => 'int'
  40. ];
  41. protected $updateTime = false;
  42. protected function setAddTimeAttr($value)
  43. {
  44. return time();
  45. }
  46. protected function setAddIpAttr($value)
  47. {
  48. return app('request')->ip();
  49. }
  50. protected function setLastTimeAttr($value)
  51. {
  52. return time();
  53. }
  54. protected function setLastIpAttr($value)
  55. {
  56. return app('request')->ip();
  57. }
  58. // protected function getPhoneAttr($value)
  59. // {
  60. // return $value && app('request')->hasMacro('adminInfo') && app('request')->adminInfo()['level'] != 0 ? substr_replace($value, '****', 3, 4) : $value;
  61. // }
  62. /**
  63. * 链接会员登陆设置表
  64. * @return \think\model\relation\HasOne
  65. */
  66. public function systemUserLevel()
  67. {
  68. return $this->hasOne(SystemUserLevel::class, 'id', 'level');
  69. }
  70. /**
  71. * 关联用户分组
  72. * @return \think\model\relation\HasOne
  73. */
  74. public function userGroup()
  75. {
  76. return $this->hasOne(UserGroup::class, 'id', 'group_id');
  77. }
  78. /**
  79. * 关联自己
  80. * @return \think\model\relation\HasOne
  81. */
  82. public function spreadUser()
  83. {
  84. return $this->hasOne(self::class, 'uid', 'spread_uid');
  85. }
  86. /**
  87. * 关联自己
  88. * @return \think\model\relation\HasOne
  89. */
  90. public function spreadCount()
  91. {
  92. return $this->hasMany(User::class, 'spread_uid', 'uid');
  93. }
  94. /**
  95. * 关联用户标签关系
  96. * @return \think\model\relation\HasMany
  97. */
  98. public function LabelRelation()
  99. {
  100. return $this->hasMany(UserLabelRelation::class, 'uid', 'uid');
  101. }
  102. /**
  103. * 关联用户标签
  104. * @return \think\model\relation\HasManyThrough
  105. */
  106. public function label()
  107. {
  108. return $this->hasManyThrough(UserLabel::class, UserLabelRelation::class, 'uid', 'id', 'uid', 'label_id');
  109. }
  110. /**
  111. * 关联用户地址
  112. * @return \think\model\relation\HasMany
  113. */
  114. public function address()
  115. {
  116. return $this->hasMany(UserAddress::class, 'uid', 'uid');
  117. }
  118. /**
  119. * 关联提现
  120. * @return \think\model\relation\HasMany
  121. */
  122. public function extract()
  123. {
  124. return $this->hasMany(UserExtract::class, 'uid', 'uid');
  125. }
  126. /**
  127. * 关联订单
  128. * @return User|\think\model\relation\HasMany
  129. */
  130. public function order()
  131. {
  132. return $this->hasMany(StoreOrder::class, 'uid', 'uid');
  133. }
  134. /**
  135. * 关联分销等级
  136. * @return \think\model\relation\HasOne
  137. */
  138. public function agentLevel()
  139. {
  140. return $this->hasOne(AgentLevel::class, 'id', 'agent_level')->where('is_del', 0)->where('status', 1);
  141. }
  142. /**
  143. * 关联佣金数据
  144. * @return \think\model\relation\HasMany
  145. */
  146. public function bill()
  147. {
  148. return $this->hasMany(UserBill::class, 'uid', 'uid');
  149. }
  150. /**
  151. * 用户uid
  152. * @param Model $query
  153. * @param $value
  154. */
  155. public function searchUidAttr($query, $value)
  156. {
  157. if (is_array($value))
  158. $query->whereIn('uid', $value);
  159. else
  160. $query->where('uid', $value);
  161. }
  162. /**
  163. * 账号搜索器
  164. * @param Model $query
  165. * @param $value
  166. */
  167. public function searchAccountAttr($query, $value)
  168. {
  169. $query->where('account', $value);
  170. }
  171. /**
  172. * 密码搜索器
  173. * @param Model $query
  174. * @param $value
  175. */
  176. public function searchPwdAttr($query, $value)
  177. {
  178. $query->where('pwd', $value);
  179. }
  180. /**
  181. * uid范围查询搜索器
  182. * @param Model $query
  183. * @param $value
  184. */
  185. public function searchUidsAttr($query, $value)
  186. {
  187. $query->whereIn('uid', $value);
  188. }
  189. /**
  190. * 模糊条件搜索器
  191. * @param Model $query
  192. * @param $value
  193. */
  194. public function searchLikeAttr($query, $value)
  195. {
  196. $query->where('account|nickname|phone|real_name|uid', 'like', '%' . $value . '%');
  197. }
  198. /**
  199. * 手机号搜索器
  200. * @param Model $query
  201. * @param $value
  202. */
  203. public function searchPhoneAttr($query, $value)
  204. {
  205. $query->where('phone', $value);
  206. }
  207. /**
  208. * 分组搜索器
  209. * @param Model $query
  210. * @param $value
  211. */
  212. public function searchGroupIdAttr($query, $value)
  213. {
  214. $query->where('group_id', $value);
  215. }
  216. /**
  217. * 是否推广人搜索器
  218. * @param Model $query
  219. * @param $value
  220. */
  221. public function searchIsPromoterAttr($query, $value)
  222. {
  223. $query->where('is_promoter', $value);
  224. }
  225. /**
  226. * 状态搜索器
  227. * @param Model $query
  228. * @param $value
  229. */
  230. public function searchStatusAttr($query, $value)
  231. {
  232. $query->where('status', $value);
  233. }
  234. /**
  235. * 会员等级搜索器
  236. * @param Model $query
  237. * @param $value
  238. */
  239. public function searchLevelAttr($query, $value)
  240. {
  241. $query->where('level', $value);
  242. }
  243. /**
  244. * 推广人uid搜索器
  245. * @param Model $query
  246. * @param $value
  247. */
  248. public function searchSpreadUidAttr($query, $value)
  249. {
  250. $query->where('spread_uid', $value);
  251. }
  252. /**
  253. * 推广人uid不等于搜索器
  254. * @param Model $query
  255. * @param $value
  256. */
  257. public function searchNotSpreadUidAttr($query, $value)
  258. {
  259. $query->where('spread_uid', '<>', $value);
  260. }
  261. /**
  262. * 推广人时间搜索器
  263. * @param Model $query
  264. * @param $value
  265. */
  266. public function searchSpreadTimeAttr($query, $value)
  267. {
  268. if ($value) {
  269. if (is_array($value)) {
  270. if (count($value) == 2) $query->where('spread_time', $value[0], $value[1]);
  271. } else {
  272. $query->where('spread_time', $value);
  273. }
  274. }
  275. }
  276. /**
  277. * 用户类型搜索器
  278. * @param Model $query
  279. * @param $value
  280. */
  281. public function searchUserTypeAttr($query, $value)
  282. {
  283. if ($value != '') $query->where('user_type', $value);
  284. }
  285. /**
  286. * 购买次数搜索器
  287. * @param Model $query
  288. * @param $value
  289. */
  290. public function searchPayCountAttr($query, $value)
  291. {
  292. $query->where('pay_count', $value);
  293. }
  294. /**
  295. * 用户推广资格
  296. * @param Model $query
  297. * @param $value
  298. */
  299. public function searchSpreadOpenAttr($query, $value)
  300. {
  301. if ($value != '') $query->where('spread_open', $value);
  302. }
  303. /**
  304. * nickname搜索器
  305. * @param $query
  306. * @param $value
  307. */
  308. public function searchNicknameAttr($query, $value)
  309. {
  310. $query->where('nickname', "like", "%" . $value . "%");
  311. }
  312. /**
  313. * division_type搜索器
  314. * @param $query
  315. * @param $value
  316. */
  317. public function searchDivisionTypeAttr($query, $value)
  318. {
  319. if ($value !== '') $query->where('division_type', $value);
  320. }
  321. /**
  322. * division_id搜索器
  323. * @param $query
  324. * @param $value
  325. */
  326. public function searchDivisionIdAttr($query, $value)
  327. {
  328. if ((int)$value !== 0) $query->where('division_id', $value);
  329. }
  330. /**
  331. * agent_id搜索器
  332. * @param $query
  333. * @param $value
  334. */
  335. public function searchAgentIdAttr($query, $value)
  336. {
  337. if ($value !== '') $query->where('agent_id', $value);
  338. }
  339. /**
  340. * staff_id搜索器
  341. * @param $query
  342. * @param $value
  343. */
  344. public function searchStaffIdAttr($query, $value)
  345. {
  346. if ($value !== '') $query->where('staff_id', $value);
  347. }
  348. /**
  349. * is_division搜索器
  350. * @param $query
  351. * @param $value
  352. */
  353. public function searchIsDivisionAttr($query, $value)
  354. {
  355. if ($value !== '') $query->where('is_division', $value);
  356. }
  357. /**
  358. * is_agent搜索器
  359. * @param $query
  360. * @param $value
  361. */
  362. public function searchIsAgentAttr($query, $value)
  363. {
  364. if ($value !== '') $query->where('is_agent', $value);
  365. }
  366. /**
  367. * is_staff搜索器
  368. * @param $query
  369. * @param $value
  370. */
  371. public function searchIsStaffAttr($query, $value)
  372. {
  373. if ($value !== '') $query->where('is_staff', $value);
  374. }
  375. /**
  376. * @param $query
  377. * @param $value
  378. */
  379. public function searchKeywordAttr($query, $value)
  380. {
  381. if ($value !== '') $query->where('uid|nickname', 'like', '%' . $value . '%');
  382. }
  383. /**
  384. * 注销搜索器
  385. * @param $query
  386. * @param $value
  387. */
  388. public function searchIsDelAttr($query, $value)
  389. {
  390. if ($value !== '') $query->where('is_del', $value);
  391. }
  392. /**
  393. * 不等于uid搜索器
  394. * @param $query
  395. * @param $value
  396. */
  397. public function searchNotUidAttr($query, $value)
  398. {
  399. if ($value !== '') $query->where('uid', '<>', $value);
  400. }
  401. }