User.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. <?php
  2. namespace app\models\user;
  3. use app\admin\model\user\UserBonus;
  4. use app\models\store\StoreOrder;
  5. use app\models\store\StoreProduct;
  6. use crmeb\services\SystemConfigService;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. use think\Exception;
  11. use think\facade\Session;
  12. use crmeb\traits\ModelTrait;
  13. use crmeb\basic\BaseModel;
  14. use crmeb\traits\JwtAuthModelTrait;
  15. /**
  16. * TODO 用户Model
  17. * Class User
  18. * @package app\models\user
  19. */
  20. class User extends BaseModel
  21. {
  22. use JwtAuthModelTrait;
  23. use ModelTrait;
  24. protected $pk = 'uid';
  25. protected $name = 'user';
  26. protected $insert = ['add_time', 'add_ip', 'last_time', 'last_ip'];
  27. protected $hidden = [
  28. 'add_ip', 'add_time', 'account', 'clean_time', 'last_ip', 'last_time', 'pwd', 'status', 'mark', 'pwd'
  29. ];
  30. protected function setAddTimeAttr($value)
  31. {
  32. return time();
  33. }
  34. protected function setAddIpAttr($value)
  35. {
  36. return app('request')->ip();
  37. }
  38. protected function setLastTimeAttr($value)
  39. {
  40. return time();
  41. }
  42. protected function setLastIpAttr($value)
  43. {
  44. return app('request')->ip();
  45. }
  46. /**
  47. * @param $wechatUser
  48. * @param int $spread_uid
  49. * @return bool
  50. * @throws DataNotFoundException
  51. * @throws DbException
  52. * @throws ModelNotFoundException
  53. */
  54. public static function setWechatUser($wechatUser, $spread_uid = 0)
  55. {
  56. $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
  57. $res = self::create([
  58. 'account' => 'wx' . $wechatUser['uid'] . time(),
  59. 'pwd' => md5(123456),
  60. 'nickname' => $wechatUser['nickname'] ?: '',
  61. 'avatar' => $wechatUser['headimgurl'] ?: '',
  62. 'add_time' => time(),
  63. 'add_ip' => app('request')->ip(),
  64. 'last_time' => time(),
  65. 'last_ip' => app('request')->ip(),
  66. 'uid' => $wechatUser['uid'],
  67. 'is_promoter' => $storeBrokerageStatu != 1 ? 1 : 0,
  68. 'user_type' => 'wechat'
  69. ]);
  70. return $res && UserSpread::setSpread($wechatUser['uid'], $spread_uid);
  71. }
  72. /**
  73. * TODO 获取会员是否被清除过的时间
  74. * @param $uid
  75. * @return int|mixed
  76. * @throws DataNotFoundException
  77. * @throws ModelNotFoundException
  78. * @throws DbException
  79. */
  80. public static function getCleanTime($uid)
  81. {
  82. $user = self::where('uid', $uid)->field(['add_time', 'clean_time'])->find();
  83. if (!$user) return 0;
  84. return $user['clean_time'] ? $user['clean_time'] : $user['add_time'];
  85. }
  86. /**
  87. * 更新用户信息
  88. * @param array $wechatUser 用户信息
  89. * @param int $uid 用户uid
  90. * @return bool|void
  91. * @throws DataNotFoundException
  92. * @throws ModelNotFoundException
  93. * @throws DbException
  94. */
  95. public static function updateWechatUser($wechatUser, $uid)
  96. {
  97. $userInfo = self::where('uid', $uid)->find();
  98. if (!$userInfo) return;
  99. //增加成为分销权限
  100. if (!$userInfo->is_promoter) {
  101. $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $uid])->sum('pay_price');
  102. $status = is_brokerage_statu($price);
  103. } else {
  104. $status = false;
  105. }
  106. if ($userInfo->spread_uid) {
  107. return self::edit([
  108. 'is_promoter' => $status ? 1 : $userInfo->is_promoter,
  109. 'phone' => $wechatUser['phone'],
  110. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  111. ], $uid, 'uid');
  112. } else {
  113. $data = [
  114. 'is_promoter' => $status ? 1 : $userInfo->is_promoter,
  115. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  116. // 'spread_uid' => 0,
  117. // 'spread_time' => 0,
  118. 'last_time' => time(),
  119. 'phone' => $wechatUser['phone'],
  120. 'last_ip' => request()->ip(),
  121. ];
  122. //TODO 获取后台分销类型
  123. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  124. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  125. // if (isset($wechatUser['code']) && $wechatUser['code'] && $wechatUser['code'] != $uid && $uid != self::where('uid', $wechatUser['code'])->value('spread_uid')) {
  126. // if ($storeBrokerageStatus == 1) {
  127. // $spreadCount = self::where('uid', $wechatUser['code'])->count();
  128. // if ($spreadCount) {
  129. // $spreadInfo = self::where('uid', $wechatUser['code'])->find();
  130. // if ($spreadInfo->is_promoter) {
  131. // //TODO 只有扫码才可以获得推广权限
  132. //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
  133. // }
  134. // }
  135. // }
  136. // $data['spread_uid'] = $wechatUser['code'];
  137. // $data['spread_time'] = time();
  138. // }
  139. return self::edit($data, $uid, 'uid') && UserSpread::setSpread($uid, $wechatUser['code'] ?? 0);
  140. }
  141. }
  142. /**
  143. * 设置推广关系
  144. * @param $spread
  145. * @param $uid
  146. * @return bool
  147. * @throws DataNotFoundException
  148. * @throws ModelNotFoundException
  149. * @throws DbException
  150. */
  151. public static function setSpread($spread, $uid)
  152. {
  153. return UserSpread::setSpread($uid, $spread);
  154. //当前用户信息
  155. // $userInfo = self::where('uid', $uid)->find();
  156. // if (!$userInfo) return true;
  157. // //当前用户有上级直接返回
  158. // if ($userInfo->spread_uid) return true;
  159. // //没有推广编号直接返回
  160. // if (!$spread) return true;
  161. // if ($spread == $uid) return true;
  162. // if ($uid == self::where('uid', $spread)->value('spread_uid')) return true;
  163. // //TODO 获取后台分销类型
  164. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  165. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  166. // if ($storeBrokerageStatus == 1) {
  167. // $spreadCount = self::where('uid', $spread)->count();
  168. // if ($spreadCount) {
  169. // $spreadInfo = self::where('uid', $spread)->find();
  170. // if ($spreadInfo->is_promoter) {
  171. // //TODO 只有扫码才可以获得推广权限
  172. //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
  173. // }
  174. // }
  175. // }
  176. // $data['spread_uid'] = $spread;
  177. // $data['spread_time'] = time();
  178. // return self::edit($data, $uid, 'uid');
  179. }
  180. /**
  181. * 小程序用户添加
  182. * @param $routineUser
  183. * @param int $spread_uid
  184. * @return object
  185. * @throws DataNotFoundException
  186. * @throws DbException
  187. * @throws ModelNotFoundException
  188. */
  189. public static function setRoutineUser($routineUser, $spread_uid = 0)
  190. {
  191. self::beginTrans();
  192. $res1 = true;
  193. if ($spread_uid) $res1 = self::where('uid', $spread_uid)->inc('spread_count', 1)->update();
  194. $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
  195. $res2 = self::create([
  196. 'account' => 'rt' . $routineUser['uid'] . time(),
  197. 'pwd' => md5(123456),
  198. 'nickname' => $routineUser['nickname'] ?: '微信用户',
  199. 'avatar' => $routineUser['headimgurl'] ?: '',
  200. // 'spread_uid' => $spread_uid,
  201. 'is_promoter' => $storeBrokerageStatu != 1 ? 1 : 0,
  202. // 'spread_time' => $spread_uid ? time() : 0,
  203. 'uid' => $routineUser['uid'],
  204. 'add_time' => $routineUser['add_time'],
  205. 'add_ip' => request()->ip(),
  206. 'last_time' => time(),
  207. 'last_ip' => request()->ip(),
  208. 'user_type' => $routineUser['user_type']
  209. ]);
  210. $res = $res1 && $res2 && UserSpread::setSpread($routineUser['uid'], $spread_uid);
  211. self::checkTrans($res);
  212. return $res2;
  213. }
  214. /**
  215. * 获得当前登陆用户UID
  216. * @return int $uid
  217. */
  218. public static function getActiveUid()
  219. {
  220. $uid = null;
  221. $uid = Session::get('LoginUid');
  222. if ($uid) return $uid;
  223. else return 0;
  224. }
  225. /**
  226. * TODO 查询当前用户信息
  227. * @param $uid $uid 用户编号
  228. * @param string $field $field 查询的字段
  229. * @return array
  230. * @throws DataNotFoundException
  231. * @throws ModelNotFoundException
  232. * @throws DbException
  233. */
  234. public static function getUserInfo($uid, $field = '')
  235. {
  236. if (strlen(trim($field))) $userInfo = self::where('uid', $uid)->field($field)->find();
  237. else $userInfo = self::where('uid', $uid)->find();
  238. if (!$userInfo) return [];
  239. return $userInfo->toArray();
  240. }
  241. /**
  242. * 判断当前用户是否推广员
  243. * @param int $uid
  244. * @return bool
  245. */
  246. public static function isUserSpread($uid = 0)
  247. {
  248. if (!$uid) return false;
  249. $status = (int)sys_config('store_brokerage_statu');
  250. $isPromoter = true;
  251. if ($status == 1) $isPromoter = self::where('uid', $uid)->value('is_promoter');
  252. if ($isPromoter) return true;
  253. else return false;
  254. }
  255. /**
  256. * TODO 一级返佣
  257. * @param $orderInfo
  258. * @return bool
  259. * @throws DataNotFoundException
  260. * @throws ModelNotFoundException
  261. * @throws DbException
  262. */
  263. public static function backOrderBrokerage($orderInfo, bool $open = true)
  264. {
  265. //TODO 营销产品不返佣金
  266. if (isset($orderInfo['combination_id']) && $orderInfo['combination_id']) return true;
  267. if (isset($orderInfo['seckill_id']) && $orderInfo['seckill_id']) return true;
  268. if (isset($orderInfo['bargain_id']) && $orderInfo['bargain_id']) return true;
  269. $userInfo = User::getUserInfo($orderInfo['uid']);
  270. //TODO 当前用户不存在 没有上级 或者 当用用户上级时自己 直接返回
  271. if (!$userInfo || !$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid']) return true;
  272. if (!User::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return self::backOrderBrokerageTwo($orderInfo, $open);
  273. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  274. list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId);
  275. //TODO 返佣金额小于等于0 直接返回不返佣金
  276. if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
  277. //TODO 获取上级推广员信息
  278. // $open && self::beginTrans();
  279. $res = true;
  280. if ($virtualBrokeragePrice > 0) {
  281. $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
  282. //TODO 上级推广员返佣之后的金额
  283. $balance = bcadd($spreadUserInfo['brokerage_price'], $virtualBrokeragePrice, 2);
  284. $mark = $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
  285. //TODO 添加推广记录
  286. $res1 = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
  287. //TODO 添加用户余额
  288. $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
  289. //TODO 一级返佣成功 跳转二级返佣
  290. $res = $res1 && $res2;
  291. }
  292. if ($realBrokeragePrice > 0) {
  293. $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
  294. //TODO 上级推广员返佣之后的金额
  295. $mark = $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
  296. //TODO 添加推广记录
  297. $res = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfo['brokerage_price'], $mark, 0);
  298. }
  299. $res = $res && self::backOrderBrokerageTwo($orderInfo);
  300. // $open && self::checkTrans($res);
  301. return $res;
  302. }
  303. /**
  304. * TODO 二级推广
  305. * @param $orderInfo
  306. * @return bool
  307. * @throws DataNotFoundException
  308. * @throws ModelNotFoundException
  309. * @throws DbException
  310. */
  311. public static function backOrderBrokerageTwo($orderInfo, bool $open = true)
  312. {
  313. //TODO 获取购买商品的用户
  314. $userInfo = User::getUserInfo($orderInfo['uid']);
  315. //TODO 获取上推广人
  316. $userInfoTwo = User::getUserInfo($userInfo['spread_uid']);
  317. //TODO 上推广人不存在 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
  318. if (!$userInfoTwo || !$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid']) return true;
  319. //TODO 获取后台分销类型 1 指定分销 2 人人分销
  320. if (!User::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
  321. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  322. list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId, false);
  323. //TODO 返佣金额小于等于0 直接返回不返佣金
  324. if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
  325. // $open && self::beginTrans();
  326. $res = true;
  327. if ($virtualBrokeragePrice > 0) {
  328. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  329. //TODO 上级推广员返佣之后的金额
  330. $balance = bcadd($spreadUserInfoTwo['brokerage_price'], $virtualBrokeragePrice, 2);
  331. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
  332. //TODO 添加推广记录
  333. $res1 = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
  334. //TODO 添加用户余额
  335. $res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
  336. //TODO 一级返佣成功 跳转二级返佣
  337. $res = $res1 && $res2;
  338. }
  339. if ($realBrokeragePrice > 0) {
  340. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  341. //TODO 上级推广员返佣之后的金额
  342. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
  343. //TODO 添加推广记录
  344. $res = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfoTwo['brokerage_price'], $mark, 0);
  345. }
  346. // $res = $res && self::backOrderBrokerageTwo($orderInfo);
  347. // $open && self::checkTrans($res);
  348. return $res;
  349. }
  350. /**
  351. * 发放实际商品佣金
  352. * @param $orderInfo
  353. * @return bool
  354. * @throws DataNotFoundException
  355. * @throws DbException
  356. * @throws ModelNotFoundException
  357. */
  358. public static function sendBackOrderBrokerage($orderInfo)
  359. {
  360. $list = UserBill::where(['link_id' => $orderInfo['id'], 'type' => 'brokerage', 'category' => 'now_money', 'pm' => 1, 'status' => 0])->select();
  361. if ($list) {
  362. self::beginTrans();
  363. try {
  364. foreach ($list as $v) {
  365. self::bcInc($v['uid'], 'brokerage_price', $v['number'], 'uid');
  366. $balance = self::getUserInfo($v['uid'])['brokerage_price'];
  367. UserBill::where('id', $v['id'])->update(['add_time' => time(), 'status' => 1, 'balance' => $balance]);
  368. }
  369. self::commitTrans();
  370. return true;
  371. } catch (Exception $e) {
  372. self::rollbackTrans();
  373. return false;
  374. }
  375. }
  376. return true;
  377. }
  378. /**
  379. * 获取推荐人 暂无使用
  380. * @param $uid
  381. * @param $page
  382. * @param $limit
  383. * @return mixed
  384. */
  385. public static function getSpreadList($uid, $page, $limit)
  386. {
  387. $list = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->page($page, $limit)->order('add_time DESC')->select();
  388. foreach ($list as $k => $user) {
  389. $list[$k]['add_time'] = date('Y/m/d', $user['add_time']);
  390. $list[$k]['price'] = StoreOrder::getUserPrice($user['uid']);
  391. }
  392. $count = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->count();
  393. $data['count'] = $count;
  394. $data['list'] = $list;
  395. return $data;
  396. }
  397. /**
  398. * 获取某个用户的下级uid
  399. * @param $uid
  400. * @return array
  401. */
  402. public static function getOneSpreadUid($uid)
  403. {
  404. return self::where('spread_uid', $uid)->column('uid');
  405. }
  406. /**
  407. * 修改个人信息
  408. * @param $avatar 头像
  409. * @param $nickname 昵称
  410. * @param $uid 用户uid
  411. * @return bool
  412. */
  413. public static function editUser($avatar, $nickname, $uid)
  414. {
  415. return self::edit(['avatar' => $avatar, 'nickname' => $nickname], $uid, 'uid');
  416. }
  417. /**
  418. * TODO 获取推广人数 一级
  419. * @param int $uid
  420. * @return bool|int|string
  421. */
  422. public static function getSpreadCount($uid = 0)
  423. {
  424. if (!$uid) return false;
  425. return self::where('spread_uid', $uid)->count();
  426. }
  427. /**
  428. * 修改当前用户的推广人数
  429. * @param $uid
  430. */
  431. public static function setUserSpreadCount($uid)
  432. {
  433. if (!$uid) return true;
  434. if (self::getSpreadCount($uid) > 0) {
  435. self::where('uid', $uid)->update(['spread_count' => 0]);
  436. } else {
  437. self::where('uid', $uid)->update(['spread_count' => 0]);
  438. return true;
  439. }
  440. return self::where('uid', $uid)->update(['spread_count' => self::getSpreadCount($uid)]);
  441. }
  442. /**
  443. * TODO 获取推广人数 二级
  444. * @param int $uid
  445. * @return bool|int|string
  446. */
  447. public static function getSpreadLevelCount($uid = 0)
  448. {
  449. if (!$uid) return false;
  450. $uidSubordinate = self::where('spread_uid', $uid)->column('uid');
  451. if (!count($uidSubordinate)) return 0;
  452. return self::where('spread_uid', 'IN', implode(',', $uidSubordinate))->count();
  453. }
  454. /**
  455. * 获取用户下级推广人
  456. * @param int $uid 当前用户
  457. * @param int $grade 等级 0 一级 1 二级
  458. * @param string $orderBy 排序
  459. * @param string $keyword
  460. * @param int $page
  461. * @param int $limit
  462. * @return array|bool
  463. */
  464. public static function getUserSpreadGrade($uid = 0, $grade = 0, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  465. {
  466. if (!$uid) return [];
  467. $gradeGroup = [0, 1];
  468. if (!in_array($grade, $gradeGroup)) return self::setErrorInfo('等级错误');
  469. $userStair = self::where('spread_uid', $uid)->column('uid');
  470. if (!count($userStair)) return [];
  471. if ($grade == 0) return self::getUserSpreadCountList(implode(',', $userStair), $orderBy, $keyword, $page, $limit);
  472. // $userSecondary = self::where('spread_uid', 'in', implode(',', $userStair))->column('uid');
  473. $userSecondary = get_downline(User::select(), $uid);
  474. return self::getUserSpreadCountList(implode(',', $userSecondary), $orderBy, $keyword, $page, $limit);
  475. }
  476. /**
  477. * 获取团队信息
  478. * @param $uid
  479. * @param string $orderBy
  480. * @param string $keyword
  481. * @param int $page
  482. * @param int $limit
  483. * @return array
  484. */
  485. public static function getUserSpreadCountList($uid, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  486. {
  487. $model = new self;
  488. if ($orderBy === '') $orderBy = 'u.add_time desc';
  489. $model = $model->alias(' u');
  490. $sql = StoreOrder::where('o.paid', 1)->group('o.uid')->field(['SUM(o.pay_price) as numberCount', 'o.uid', 'o.order_id'])
  491. ->where('o.is_del', 0)->where('o.is_system_del', 0)->alias('o')->fetchSql(true)->select();
  492. $model = $model->join("(" . $sql . ") p", 'u.uid = p.uid', 'LEFT');
  493. $model = $model->where('u.uid', 'IN', $uid);
  494. $model = $model->field("u.uid,u.nickname,u.avatar,from_unixtime(u.add_time,'%Y/%m/%d') as time,u.spread_count as childCount,u.pay_count as orderCount,p.numberCount");
  495. if (strlen(trim($keyword))) $model = $model->where('u.nickname|u.phone', 'like', "%$keyword%");
  496. $model = $model->group('u.uid');
  497. $model = $model->order($orderBy);
  498. $model = $model->page($page, $limit);
  499. $list = $model->select();
  500. foreach ($list as &$item)
  501. {
  502. $item['price'] = StoreOrder::where('uid', $item['uid'])->where('paid', 1)->sum('pay_price');
  503. }
  504. if ($list) return $list->toArray();
  505. else return [];
  506. }
  507. /**
  508. * 设置用户的上级关系
  509. * @param $uid 用户uid
  510. * @param $spreadUid 上级用户uid
  511. * @return User|bool
  512. * @throws DataNotFoundException
  513. * @throws ModelNotFoundException
  514. * @throws DbException
  515. */
  516. public static function setSpreadUid($uid, $spreadUid)
  517. {
  518. return UserSpread::setSpread($uid, $spreadUid);
  519. // 自己不能绑定自己为上级
  520. // if ($uid == $spreadUid) return false;
  521. // //TODO 获取后台分销类型
  522. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  523. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  524. // if ($storeBrokerageStatus == 1) {
  525. // $spreadCount = self::where('uid', $spreadUid)->count();
  526. // if ($spreadCount) {
  527. // $spreadInfo = self::where('uid', $spreadUid)->find();
  528. // if ($spreadInfo->is_promoter) {
  529. // //TODO 只有扫码才可以获得推广权限
  530. // if (isset($wechatUser['isPromoter'])) $data['is_promoter'] = 1;
  531. // }
  532. // }
  533. // }
  534. // return self::where('uid', $uid)->update(['spread_uid' => $spreadUid, 'spread_time' => time()]);
  535. }
  536. /**
  537. * 判断上下级关系是否存在
  538. * @param $uid
  539. * @param $spreadUid
  540. * @return bool|int
  541. */
  542. public static function validSpread($uid, $spreadUid)
  543. {
  544. if (!$uid || !$spreadUid) return false;
  545. return self::where('uid', $uid)->where('spread_uid', $spreadUid)->count();
  546. }
  547. /**
  548. * H5用户注册
  549. * @param $account
  550. * @param $password
  551. * @param $spread
  552. * @return bool
  553. * @throws DataNotFoundException
  554. * @throws DbException
  555. * @throws ModelNotFoundException
  556. */
  557. public static function register($account, $password, $spread)
  558. {
  559. $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
  560. if (self::be(['account' => $account])) return self::setErrorInfo('用户已存在');
  561. $phone = $account;
  562. $data['account'] = $account;
  563. $data['pwd'] = md5($password);
  564. $data['phone'] = $phone;
  565. // if ($spread) {
  566. // $data['spread_uid'] = $spread;
  567. // $data['spread_time'] = time();
  568. // }
  569. $data['real_name'] = '';
  570. $data['birthday'] = 0;
  571. $data['card_id'] = '';
  572. $data['mark'] = '';
  573. $data['addres'] = '';
  574. $data['user_type'] = 'h5';
  575. $data['add_time'] = time();
  576. $data['add_ip'] = app('request')->ip();
  577. $data['last_time'] = time();
  578. $data['last_ip'] = app('request')->ip();
  579. $data['nickname'] = substr(md5($account . time()), 0, 12);
  580. $data['avatar'] = $data['headimgurl'] = sys_config('h5_avatar');
  581. $data['city'] = '';
  582. $data['language'] = '';
  583. $data['province'] = '';
  584. $data['country'] = '';
  585. $data['is_promoter'] = $storeBrokerageStatu != 1 ? 1 : 0;
  586. self::beginTrans();
  587. $res2 = WechatUser::create($data);
  588. $data['uid'] = $res2->uid;
  589. $res1 = self::create($data);
  590. $res = $res1 && $res2 && UserSpread::setSpread($data['uid'], $spread);
  591. self::checkTrans($res);
  592. return $res;
  593. }
  594. /**
  595. * 密码修改
  596. * @param $account
  597. * @param $password
  598. * @return User|bool
  599. */
  600. public static function reset($account, $password)
  601. {
  602. if (!self::be(['account' => $account])) return self::setErrorInfo('用户不存在');
  603. $count = self::where('account', $account)->where('pwd', md5($password))->count();
  604. if ($count) return true;
  605. return self::where('account', $account)->update(['pwd' => md5($password)]);
  606. }
  607. /**
  608. * 获取手机号是否注册
  609. * @param $phone
  610. * @return bool
  611. */
  612. public static function checkPhone($phone)
  613. {
  614. return self::be(['account' => $phone]);
  615. }
  616. /**
  617. * 获取推广人
  618. * @param $data 查询条件
  619. * @return array
  620. * @throws DataNotFoundException
  621. * @throws ModelNotFoundException
  622. * @throws \think\exception\DbException
  623. */
  624. public static function getRankList($data)
  625. {
  626. switch ($data['type']) {
  627. case 'week':
  628. $startTime = strtotime('this week');
  629. $endTime = time();
  630. break;
  631. case 'month':
  632. $startTime = strtotime('last month');
  633. $endTime = time();
  634. break;
  635. }
  636. $list = self::alias('t0')
  637. ->field('t0.uid,t0.spread_uid,count(t1.spread_uid) AS count,t0.add_time,t0.nickname,t0.avatar')
  638. ->join('user t1', 't0.uid = t1.spread_uid', 'LEFT')
  639. ->where('t1.spread_uid', '<>', 0)
  640. ->order('count desc')
  641. ->order('t0.uid desc')
  642. ->where('t1.add_time', 'BETWEEN', [$startTime, $endTime])
  643. ->page($data['page'], $data['limit'])
  644. ->group('t0.uid')
  645. ->select();
  646. return count($list) ? $list->toArray() : [];
  647. }
  648. /**
  649. * 获取佣金排行
  650. * @param $data
  651. * @return array
  652. */
  653. public static function brokerageRank($data)
  654. {
  655. $model = self::where('status', 1);
  656. switch ($data['type']) {
  657. case 'week':
  658. $model = $model->whereIn('uid', function ($query) {
  659. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  660. ->whereWeek('add_time')->field('uid');
  661. });
  662. break;
  663. case 'month':
  664. $model = $model->whereIn('uid', function ($query) {
  665. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  666. ->whereMonth('add_time')->field('uid');
  667. });
  668. break;
  669. }
  670. $users = $model->field('uid,nickname,avatar,brokerage_price')->order('brokerage_price desc')
  671. ->page((int)$data['page'], (int)$data['limit'])->select();
  672. return count($users) ? $users->toArray() : [];
  673. }
  674. /**
  675. * 获取当前用户的佣金排行位置
  676. * @param $uid
  677. * @return int
  678. */
  679. public static function currentUserRank($type, $brokerage_price)
  680. {
  681. $model = self::where('status', 1);
  682. switch ($type) {
  683. case 'week':
  684. $model = $model->whereIn('uid', function ($query) {
  685. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  686. ->whereWeek('add_time')->field('uid');
  687. });
  688. break;
  689. case 'month':
  690. $model = $model->whereIn('uid', function ($query) {
  691. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  692. ->whereMonth('add_time')->field('uid');
  693. });
  694. break;
  695. }
  696. return $model->where('brokerage_price', '>', $brokerage_price)->count('uid');
  697. }
  698. /**
  699. * 团队奖励分红
  700. * @return void
  701. * @throws DataNotFoundException
  702. * @throws DbException
  703. * @throws ModelNotFoundException
  704. */
  705. public static function bonus()
  706. {
  707. if (!Push::where('add_time', strtotime('today'))->where('type', 0)->find()) {
  708. $user = User::select();
  709. foreach ($user as $item) {
  710. $start_time = date('Y-m-01 00:00:00', strtotime('-1 month'));
  711. $end_time = date('Y-m-d 23:59:59', strtotime(-date('d') . 'day'));
  712. $price = StoreOrder::whereBetweenTime('add_time', strtotime('yesterday'), strtotime('today'))->where('paid', 1)->where('uid', $item['uid'])->sum('pay_price');// 月结
  713. if ($price > 0) {
  714. $spread = getParents($item['uid'], $user->toArray());// 找到所有上级
  715. $v1 = 0;
  716. $v2 = 0;
  717. $v3 = 0;
  718. $v4 = 0;
  719. $v5 = 0;
  720. $one = sys_config('team1') / 100; // 等级1
  721. $tow = sys_config('team2') / 100; // 等级2
  722. $three = sys_config('team3') / 100;// 等级3
  723. $four = sys_config('team4') / 100;// 等级4
  724. $five = sys_config('team5') / 100;// 等级5
  725. foreach ($spread as $value) {
  726. $details = User::where('uid', $value)->find();
  727. if ($details['level'] > 0) {
  728. if ($details['level'] == 1) {
  729. if ($v1 == 0 and $v2 == 0 and $v3 == 0 and $v4 == 0 and $v5 == 0) {
  730. $jl = $price * $one;
  731. $v1++;
  732. }
  733. } elseif ($details['level'] == 2) {
  734. if ($v2 == 0 and $v3 == 0 and $v4 == 0 and $v5 == 0){
  735. if ($v1 == 0) {
  736. $jl = $price * $tow;
  737. $v2++;
  738. } elseif ($v1 > 0) {
  739. $jl = $price * ($tow - $one);
  740. $v2++;
  741. }
  742. }
  743. } elseif ($details['level'] == 3) {
  744. if ($v3 == 0 and $v4 == 0 and $v5 == 0){
  745. if ($v1 == 0 and $v2 == 0) {
  746. $jl = $price * $three;
  747. $v3++;
  748. } elseif ($v1 > 0 and $v2 == 0) {
  749. $jl = $price * ($three - $one);
  750. $v3++;
  751. } elseif ($v2 > 0) {
  752. $jl = ($price * ($three - $tow));
  753. $v3++;
  754. }
  755. }
  756. }elseif ($details['level'] == 4) {
  757. if ($v4 == 0 and $v5 == 0){
  758. if ($v1 == 0 and $v2 == 0 and $v3 == 0) {
  759. $jl = $price * $four;
  760. $v4++;
  761. } elseif ($v1 > 0 and $v2 == 0 and $v3 == 0) {
  762. $jl = $price * ($four - $one);
  763. $v4++;
  764. } elseif ($v2 > 0 and $v3 == 0) {
  765. $jl = ($price * ($four - $tow));
  766. $v4++;
  767. }elseif ($v3 > 0){
  768. $jl = ($price * ($four - $three));
  769. $v4++;
  770. }
  771. }
  772. }elseif ($details['level'] == 5) {
  773. if ($v5 == 0){
  774. if ($v1 == 0 and $v2 == 0 and $v3 == 0 and $v4 == 0) {
  775. $jl = $price * $five;
  776. $v5++;
  777. } elseif ($v1 > 0 and $v2 == 0 and $v3 == 0 and $v4 == 0) {
  778. $jl = $price * ($five - $one);
  779. $v5++;
  780. } elseif ($v2 > 0 and $v3 == 0 and $v4 == 0) {
  781. $jl = ($price * ($five - $tow));
  782. $v5++;
  783. }elseif ($v3 > 0 and $v4 == 0){
  784. $jl = ($price * ($five - $three));
  785. $v5++;
  786. }elseif ($v4 > 0){
  787. $jl = ($price * ($five - $four));
  788. $v5++;
  789. }
  790. }
  791. }
  792. if ($jl > 0) {
  793. $brokerage_price = $jl;// 到账佣金
  794. User::where('uid', $value)->inc('brokerage_price', $brokerage_price)->update();
  795. UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $brokerage_price, $item['uid'], $details['brokerage_price'] + $brokerage_price, '用户' . $item['uid'] . '消费' . $price . '团队奖励佣金');
  796. }
  797. $jl = 0;
  798. }
  799. }
  800. }
  801. }
  802. Push::create(['add_time' => strtotime('today'), 'type' => 0]); //存入数据库信息
  803. }
  804. }
  805. /**
  806. * 积分分红
  807. * @return void
  808. */
  809. public static function in_bonus()
  810. {
  811. if (!Push::where('add_time', strtotime('today'))->where('type', 1)->find()) {
  812. $price = StoreOrder::whereBetweenTime('add_time', strtotime('yesterday'), strtotime('today'))->where('paid', 1)->sum('pay_price');
  813. $price = $price * 0.1;// 百分之十的分红
  814. $count = UserBonus::where('status', 0)->sum('integral')/10000;//分红多少份
  815. if ($count > 0){
  816. $bonus = UserBonus::where('status', 0)->select();
  817. $copy = round($price/$count, 2);
  818. foreach ($bonus as $item)
  819. {
  820. $jl = $copy * $item['integral']/10000;
  821. $mark = '';
  822. $fa = 0;
  823. if ($item['price']+$jl > $item['integral']){
  824. $jl = $item['integral'] - $item['price'];
  825. $mark = '发放完';
  826. $fa = 1;
  827. }
  828. if ($fa == 1){
  829. UserBonus::where('id', $item['id'])->update(['status'=> 1]);
  830. UserBonus::where('id', $item['id'])->inc('price', $jl)->update();
  831. }else{
  832. UserBonus::where('id', $item['id'])->inc('price', $jl)->update();
  833. }
  834. User::where('uid', $item['uid'])->inc('brokerage_price', $jl)->update();
  835. UserBill::income('佣金', $item['uid'], 'now_money', 'brokerage', $jl, '', User::where('uid', $item['uid'])->value('brokerage_price'), '分红'.($item['integral']/10000).'份佣金'.$mark);
  836. }
  837. }
  838. Push::create(['add_time' => strtotime('today'), 'type' => 1]); //存入数据库信息
  839. }
  840. }
  841. }