User.php 10 KB

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