User.php 36 KB

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