User.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. <?php
  2. namespace app\models\user;
  3. use app\models\store\StoreOrder;
  4. use app\models\store\StoreOrderCartInfo;
  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\Db;
  12. use think\facade\Session;
  13. use crmeb\traits\ModelTrait;
  14. use crmeb\basic\BaseModel;
  15. use crmeb\traits\JwtAuthModelTrait;
  16. /**
  17. * TODO 用户Model
  18. * Class User
  19. * @package app\models\user
  20. */
  21. class User extends BaseModel
  22. {
  23. use JwtAuthModelTrait;
  24. use ModelTrait;
  25. protected $pk = 'uid';
  26. protected $name = 'user';
  27. protected $insert = ['add_time', 'add_ip', 'last_time', 'last_ip'];
  28. protected $hidden = [
  29. 'add_ip', 'add_time', 'account', 'clean_time', 'last_ip', 'last_time', 'pwd', 'status', 'mark', 'pwd'
  30. ];
  31. protected function setAddTimeAttr($value)
  32. {
  33. return time();
  34. }
  35. protected function setAddIpAttr($value)
  36. {
  37. return app('request')->ip();
  38. }
  39. protected function setLastTimeAttr($value)
  40. {
  41. return time();
  42. }
  43. protected function setLastIpAttr($value)
  44. {
  45. return app('request')->ip();
  46. }
  47. /**
  48. * @param $wechatUser
  49. * @param int $spread_uid
  50. * @return bool
  51. * @throws DataNotFoundException
  52. * @throws DbException
  53. * @throws ModelNotFoundException
  54. */
  55. public static function setWechatUser($wechatUser, $spread_uid = 0)
  56. {
  57. $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
  58. $res = self::create([
  59. 'account' => 'wx' . $wechatUser['uid'] . time(),
  60. 'pwd' => md5(123456),
  61. 'nickname' => $wechatUser['nickname'] ?: '',
  62. 'avatar' => $wechatUser['headimgurl'] ?: '',
  63. 'add_time' => time(),
  64. 'add_ip' => app('request')->ip(),
  65. 'last_time' => time(),
  66. 'last_ip' => app('request')->ip(),
  67. 'uid' => $wechatUser['uid'],
  68. 'is_promoter' => $storeBrokerageStatu != 1 ? 1 : 0,
  69. 'user_type' => 'wechat'
  70. ]);
  71. return $res && UserSpread::setSpread($wechatUser['uid'], $spread_uid);
  72. }
  73. /**
  74. * TODO 获取会员是否被清除过的时间
  75. * @param $uid
  76. * @return int|mixed
  77. * @throws DataNotFoundException
  78. * @throws ModelNotFoundException
  79. * @throws DbException
  80. */
  81. public static function getCleanTime($uid)
  82. {
  83. $user = self::where('uid', $uid)->field(['add_time', 'clean_time'])->find();
  84. if (!$user) return 0;
  85. return $user['clean_time'] ? $user['clean_time'] : $user['add_time'];
  86. }
  87. /**
  88. * 更新用户信息
  89. * @param array $wechatUser 用户信息
  90. * @param int $uid 用户uid
  91. * @return bool|void
  92. * @throws DataNotFoundException
  93. * @throws ModelNotFoundException
  94. * @throws DbException
  95. */
  96. public static function updateWechatUser($wechatUser, $uid)
  97. {
  98. $userInfo = self::where('uid', $uid)->find();
  99. if (!$userInfo) return;
  100. //增加成为分销权限
  101. if (!$userInfo->is_promoter) {
  102. $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $uid])->sum('pay_price');
  103. $status = is_brokerage_statu($price);
  104. } else {
  105. $status = false;
  106. }
  107. if ($userInfo->spread_uid) {
  108. return self::edit([
  109. 'nickname' => $wechatUser['nickname'] ?: '',
  110. 'avatar' => $wechatUser['headimgurl'] ?: '',
  111. 'is_promoter' => $status ? 1 : $userInfo->is_promoter,
  112. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  113. ], $uid, 'uid');
  114. } else {
  115. $data = [
  116. 'nickname' => $wechatUser['nickname'] ?: '',
  117. 'avatar' => $wechatUser['headimgurl'] ?: '',
  118. 'is_promoter' => $status ? 1 : $userInfo->is_promoter,
  119. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  120. // 'spread_uid' => 0,
  121. // 'spread_time' => 0,
  122. 'last_time' => time(),
  123. 'last_ip' => request()->ip(),
  124. ];
  125. //TODO 获取后台分销类型
  126. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  127. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  128. // if (isset($wechatUser['code']) && $wechatUser['code'] && $wechatUser['code'] != $uid && $uid != self::where('uid', $wechatUser['code'])->value('spread_uid')) {
  129. // if ($storeBrokerageStatus == 1) {
  130. // $spreadCount = self::where('uid', $wechatUser['code'])->count();
  131. // if ($spreadCount) {
  132. // $spreadInfo = self::where('uid', $wechatUser['code'])->find();
  133. // if ($spreadInfo->is_promoter) {
  134. // //TODO 只有扫码才可以获得推广权限
  135. //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
  136. // }
  137. // }
  138. // }
  139. // $data['spread_uid'] = $wechatUser['code'];
  140. // $data['spread_time'] = time();
  141. // }
  142. return self::edit($data, $uid, 'uid') && UserSpread::setSpread($uid, $wechatUser['code'] ?? 0);
  143. }
  144. }
  145. /**
  146. * 设置推广关系
  147. * @param $spread
  148. * @param $uid
  149. * @return bool
  150. * @throws DataNotFoundException
  151. * @throws ModelNotFoundException
  152. * @throws DbException
  153. */
  154. public static function setSpread($spread, $uid)
  155. {
  156. return UserSpread::setSpread($uid, $spread);
  157. //当前用户信息
  158. // $userInfo = self::where('uid', $uid)->find();
  159. // if (!$userInfo) return true;
  160. // //当前用户有上级直接返回
  161. // if ($userInfo->spread_uid) return true;
  162. // //没有推广编号直接返回
  163. // if (!$spread) return true;
  164. // if ($spread == $uid) return true;
  165. // if ($uid == self::where('uid', $spread)->value('spread_uid')) return true;
  166. // //TODO 获取后台分销类型
  167. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  168. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  169. // if ($storeBrokerageStatus == 1) {
  170. // $spreadCount = self::where('uid', $spread)->count();
  171. // if ($spreadCount) {
  172. // $spreadInfo = self::where('uid', $spread)->find();
  173. // if ($spreadInfo->is_promoter) {
  174. // //TODO 只有扫码才可以获得推广权限
  175. //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
  176. // }
  177. // }
  178. // }
  179. // $data['spread_uid'] = $spread;
  180. // $data['spread_time'] = time();
  181. // return self::edit($data, $uid, 'uid');
  182. }
  183. /**
  184. * 小程序用户添加
  185. * @param $routineUser
  186. * @param int $spread_uid
  187. * @return object
  188. * @throws DataNotFoundException
  189. * @throws DbException
  190. * @throws ModelNotFoundException
  191. */
  192. public static function setRoutineUser($routineUser, $spread_uid = 0)
  193. {
  194. self::beginTrans();
  195. $res1 = true;
  196. if ($spread_uid) $res1 = self::where('uid', $spread_uid)->inc('spread_count', 1)->update();
  197. $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
  198. $res2 = self::create([
  199. 'account' => 'rt' . $routineUser['uid'] . time(),
  200. 'pwd' => md5(123456),
  201. 'nickname' => $routineUser['nickname'] ?: '',
  202. 'avatar' => $routineUser['headimgurl'] ?: '',
  203. // 'spread_uid' => $spread_uid,
  204. 'is_promoter' => $storeBrokerageStatu != 1 ? 1 : 0,
  205. // 'spread_time' => $spread_uid ? time() : 0,
  206. 'uid' => $routineUser['uid'],
  207. 'add_time' => $routineUser['add_time'],
  208. 'add_ip' => request()->ip(),
  209. 'last_time' => time(),
  210. 'last_ip' => request()->ip(),
  211. 'user_type' => $routineUser['user_type']
  212. ]);
  213. $res = $res1 && $res2 && UserSpread::setSpread($routineUser['uid'], $spread_uid);
  214. self::checkTrans($res);
  215. return $res2;
  216. }
  217. /**
  218. * 获得当前登陆用户UID
  219. * @return int $uid
  220. */
  221. public static function getActiveUid()
  222. {
  223. $uid = null;
  224. $uid = Session::get('LoginUid');
  225. if ($uid) return $uid;
  226. else return 0;
  227. }
  228. /**
  229. * TODO 查询当前用户信息
  230. * @param $uid $uid 用户编号
  231. * @param string $field $field 查询的字段
  232. * @return array
  233. * @throws DataNotFoundException
  234. * @throws ModelNotFoundException
  235. * @throws DbException
  236. */
  237. public static function getUserInfo($uid, $field = '')
  238. {
  239. if (strlen(trim($field))) $userInfo = self::where('uid', $uid)->field($field)->find();
  240. else $userInfo = self::where('uid', $uid)->find();
  241. if (!$userInfo) return [];
  242. return $userInfo->toArray();
  243. }
  244. /**
  245. * 判断当前用户是否推广员
  246. * @param int $uid
  247. * @return bool
  248. */
  249. public static function isUserSpread($uid = 0)
  250. {
  251. if (!$uid) return false;
  252. $status = (int)sys_config('store_brokerage_statu');
  253. $isPromoter = true;
  254. if ($status == 1) $isPromoter = self::where('uid', $uid)->value('is_promoter');
  255. if ($isPromoter) return true;
  256. else return false;
  257. }
  258. /**
  259. * TODO 一级返佣
  260. * @param $orderInfo
  261. * @return bool
  262. * @throws DataNotFoundException
  263. * @throws ModelNotFoundException
  264. * @throws DbException
  265. */
  266. public static function backOrderBrokerage($orderInfo, bool $open = true)
  267. {
  268. //TODO 营销产品不返佣金
  269. if (isset($orderInfo['combination_id']) && $orderInfo['combination_id']) return true;
  270. if (isset($orderInfo['seckill_id']) && $orderInfo['seckill_id']) return true;
  271. if (isset($orderInfo['bargain_id']) && $orderInfo['bargain_id']) return true;
  272. $userInfo = User::getUserInfo($orderInfo['uid']);
  273. //TODO 当前用户不存在 没有上级 或者 当用用户上级时自己 直接返回
  274. if (!$userInfo || !$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid']) return true;
  275. if (!User::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return self::backOrderBrokerageTwo($orderInfo, $open);
  276. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  277. list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId);
  278. //TODO 返佣金额小于等于0 直接返回不返佣金
  279. if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
  280. //TODO 获取上级推广员信息
  281. // $open && self::beginTrans();
  282. $res = true;
  283. if ($virtualBrokeragePrice > 0) {
  284. $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
  285. //TODO 上级推广员返佣之后的金额
  286. $balance = bcadd($spreadUserInfo['brokerage_price'], $virtualBrokeragePrice, 2);
  287. $mark = $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
  288. //TODO 添加推广记录
  289. $res1 = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
  290. //TODO 添加用户余额
  291. $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
  292. //TODO 一级返佣成功 跳转二级返佣
  293. $res = $res1 && $res2;
  294. }
  295. if ($realBrokeragePrice > 0) {
  296. $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
  297. //TODO 上级推广员返佣之后的金额
  298. $mark = $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
  299. //TODO 添加推广记录
  300. $res = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfo['brokerage_price'], $mark, 0);
  301. }
  302. $res = $res && self::backOrderBrokerageTwo($orderInfo);
  303. // $open && self::checkTrans($res);
  304. return $res;
  305. }
  306. /**
  307. * TODO 二级推广
  308. * @param $orderInfo
  309. * @return bool
  310. * @throws DataNotFoundException
  311. * @throws ModelNotFoundException
  312. * @throws DbException
  313. */
  314. public static function backOrderBrokerageTwo($orderInfo, bool $open = true)
  315. {
  316. //TODO 获取购买商品的用户
  317. $userInfo = User::getUserInfo($orderInfo['uid']);
  318. //TODO 获取上推广人
  319. $userInfoTwo = User::getUserInfo($userInfo['spread_uid']);
  320. //TODO 上推广人不存在 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
  321. if (!$userInfoTwo || !$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid']) return true;
  322. //TODO 获取后台分销类型 1 指定分销 2 人人分销
  323. if (!User::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
  324. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  325. list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId, false);
  326. //TODO 返佣金额小于等于0 直接返回不返佣金
  327. if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
  328. // $open && self::beginTrans();
  329. $res = true;
  330. if ($virtualBrokeragePrice > 0) {
  331. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  332. //TODO 上级推广员返佣之后的金额
  333. $balance = bcadd($spreadUserInfoTwo['brokerage_price'], $virtualBrokeragePrice, 2);
  334. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
  335. //TODO 添加推广记录
  336. $res1 = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
  337. //TODO 添加用户余额
  338. $res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
  339. //TODO 一级返佣成功 跳转二级返佣
  340. $res = $res1 && $res2;
  341. }
  342. if ($realBrokeragePrice > 0) {
  343. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  344. //TODO 上级推广员返佣之后的金额
  345. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
  346. //TODO 添加推广记录
  347. $res = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfoTwo['brokerage_price'], $mark, 0);
  348. }
  349. // $res = $res && self::backOrderBrokerageTwo($orderInfo);
  350. // $open && self::checkTrans($res);
  351. return $res;
  352. }
  353. /**
  354. * 发放实际商品佣金
  355. * @param $orderInfo
  356. * @return bool
  357. * @throws DataNotFoundException
  358. * @throws DbException
  359. * @throws ModelNotFoundException
  360. */
  361. public static function sendBackOrderBrokerage($orderInfo)
  362. {
  363. $list = UserBill::where(['link_id' => $orderInfo['id'], 'type' => 'brokerage', 'category' => 'now_money', 'pm' => 1, 'status' => 0])->select();
  364. if ($list) {
  365. self::beginTrans();
  366. try {
  367. foreach ($list as $v) {
  368. self::bcInc($v['uid'], 'brokerage_price', $v['number'], 'uid');
  369. $balance = self::getUserInfo($v['uid'])['brokerage_price'];
  370. UserBill::where('id', $v['id'])->update(['add_time' => time(), 'status' => 1, 'balance' => $balance]);
  371. }
  372. self::commitTrans();
  373. return true;
  374. } catch (Exception $e) {
  375. self::rollbackTrans();
  376. return false;
  377. }
  378. }
  379. return true;
  380. }
  381. /**
  382. * 获取推荐人 暂无使用
  383. * @param $uid
  384. * @param $page
  385. * @param $limit
  386. * @return mixed
  387. */
  388. public static function getSpreadList($uid, $page, $limit)
  389. {
  390. $list = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->page($page, $limit)->order('add_time DESC')->select();
  391. foreach ($list as $k => $user) {
  392. $list[$k]['add_time'] = date('Y/m/d', $user['add_time']);
  393. $list[$k]['price'] = StoreOrder::getUserPrice($user['uid']);
  394. }
  395. $count = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->count();
  396. $data['count'] = $count;
  397. $data['list'] = $list;
  398. return $data;
  399. }
  400. /**
  401. * 获取某个用户的下级uid
  402. * @param $uid
  403. * @return array
  404. */
  405. public static function getOneSpreadUid($uid)
  406. {
  407. return self::where('spread_uid', $uid)->column('uid');
  408. }
  409. /**
  410. * 修改个人信息
  411. * @param $avatar 头像
  412. * @param $nickname 昵称
  413. * @param $uid 用户uid
  414. * @return bool
  415. */
  416. public static function editUser($avatar, $nickname, $uid)
  417. {
  418. return self::edit(['avatar' => $avatar, 'nickname' => $nickname], $uid, 'uid');
  419. }
  420. /**
  421. * TODO 获取推广人数 一级
  422. * @param int $uid
  423. * @return bool|int|string
  424. */
  425. public static function getSpreadCount($uid = 0)
  426. {
  427. if (!$uid) return false;
  428. return self::where('spread_uid', $uid)->count();
  429. }
  430. /**
  431. * 修改当前用户的推广人数
  432. * @param $uid
  433. */
  434. public static function setUserSpreadCount($uid)
  435. {
  436. if (!$uid) return true;
  437. if (self::getSpreadCount($uid) > 0) {
  438. self::where('uid', $uid)->update(['spread_count' => 0]);
  439. } else {
  440. self::where('uid', $uid)->update(['spread_count' => 0]);
  441. return true;
  442. }
  443. return self::where('uid', $uid)->update(['spread_count' => self::getSpreadCount($uid)]);
  444. }
  445. /**
  446. * TODO 获取推广人数 二级
  447. * @param int $uid
  448. * @return bool|int|string
  449. */
  450. public static function getSpreadLevelCount($uid = 0)
  451. {
  452. if (!$uid) return false;
  453. $uidSubordinate = self::where('spread_uid', $uid)->column('uid');
  454. if (!count($uidSubordinate)) return 0;
  455. return self::where('spread_uid', 'IN', implode(',', $uidSubordinate))->count();
  456. }
  457. /**
  458. * 获取用户下级推广人
  459. * @param int $uid 当前用户
  460. * @param int $grade 等级 0 一级 1 二级
  461. * @param string $orderBy 排序
  462. * @param string $keyword
  463. * @param int $page
  464. * @param int $limit
  465. * @return array|bool
  466. */
  467. public static function getUserSpreadGrade($uid = 0, $grade = 0, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  468. {
  469. if (!$uid) return [];
  470. $gradeGroup = [0, 1];
  471. if (!in_array($grade, $gradeGroup)) return self::setErrorInfo('等级错误');
  472. $userStair = self::where('spread_uid', $uid)->column('uid');
  473. if (!count($userStair)) return [];
  474. if ($grade == 0) return self::getUserSpreadCountList(implode(',', $userStair), $orderBy, $keyword, $page, $limit);
  475. $userSecondary = self::where('spread_uid', 'in', implode(',', $userStair))->column('uid');
  476. return self::getUserSpreadCountList(implode(',', $userSecondary), $orderBy, $keyword, $page, $limit);
  477. }
  478. /**
  479. * 获取团队信息
  480. * @param $uid
  481. * @param string $orderBy
  482. * @param string $keyword
  483. * @param int $page
  484. * @param int $limit
  485. * @return array
  486. */
  487. public static function getUserSpreadCountList($uid, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  488. {
  489. $model = new self;
  490. if ($orderBy === '') $orderBy = 'u.add_time desc';
  491. $model = $model->alias(' u');
  492. $sql = StoreOrder::where('o.paid', 1)->group('o.uid')->field(['SUM(o.pay_price) as numberCount', 'o.uid', 'o.order_id'])
  493. ->where('o.is_del', 0)->where('o.is_system_del', 0)->alias('o')->fetchSql(true)->select();
  494. $model = $model->join("(" . $sql . ") p", 'u.uid = p.uid', 'LEFT');
  495. $model = $model->where('u.uid', 'IN', $uid);
  496. $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");
  497. if (strlen(trim($keyword))) $model = $model->where('u.nickname|u.phone', 'like', "%$keyword%");
  498. $model = $model->group('u.uid');
  499. $model = $model->order($orderBy);
  500. $model = $model->page($page, $limit);
  501. $list = $model->select();
  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. public static function user_count()
  697. {
  698. $model = new self();
  699. $silver_user = $model->where([['level', '=', 1], ['brokerage_price', '<', 200]])->select();// 白银会员
  700. $data['silver'] = count($silver_user);// 白银会员总数
  701. $data['silver_user'] = $silver_user ? $silver_user->toArray() : [];
  702. $gold_user = $model->where([['level', '=', 2], ['brokerage_price', '<', 6000]])->select();// 黄金会员
  703. $data['gold'] = count($gold_user);// 黄金会员总数
  704. $data['gold_user'] = $gold_user ? $gold_user->toArray() : [];
  705. $diamonds_user = $model->where([['level', '=', 3]])->select();// 钻石会员总数
  706. $data['diamonds'] = count($diamonds_user);// 钻石会员总数
  707. $data['diamonds_user'] = $diamonds_user ? $diamonds_user->toArray() : [];
  708. return $data;
  709. }
  710. /**
  711. * 商家转账积分分红
  712. * @param $money
  713. * @return void
  714. * @throws \Exception
  715. */
  716. public static function bring_forward($money)
  717. {
  718. $data = self::user_count();
  719. $model = new self();
  720. $silver_user = $data['silver_user'];
  721. $silver = $data['silver'];
  722. $gold_user = $data['gold_user'];
  723. $gold = $data['gold'];
  724. $diamonds_user = $data['diamonds_user'];
  725. $diamonds = $data['diamonds'];
  726. //商家转账积分后
  727. $divide = ($money * 0.1)/($silver+$gold);//白银会员和黄金会员分百分之十的金额
  728. $diamonds_divide = ($money * 0.01)/$diamonds;//钻石会员销售的百分之1
  729. $user = array_merge($silver_user, $gold_user);
  730. if ($user){
  731. // 黄金会员和白银会员
  732. foreach ($user as &$item){
  733. if ($item['level'] == 1){
  734. if ($item['brokerage_price']+$divide > 200){
  735. $item['brokerage_price'] += 200 - $item['brokerage_price'];
  736. UserBill::income('佣金', $item['uid'], 'now_money', 'brokerage', 200 - $item['brokerage_price'], $item['spread_uid'], $item['brokerage_price'], '商家销售额分红,已达到上限');
  737. }else{
  738. $item['brokerage_price'] += $divide;
  739. UserBill::income('佣金', $item['uid'], 'now_money', 'brokerage', $divide, $item['spread_uid'], $item['brokerage_price'], '商家销售额分红');
  740. }
  741. }elseif ($item['level'] == 2){
  742. if ($item['brokerage_price']+$divide > 6000){
  743. $item['brokerage_price'] += 6000 - $item['brokerage_price'];
  744. UserBill::income('佣金', $item['uid'], 'now_money', 'brokerage', 6000 - $item['brokerage_price'], $item['spread_uid'], $item['brokerage_price'], '商家销售额分红,已达到上限');
  745. }else{
  746. $item['brokerage_price'] += $divide;
  747. UserBill::income('佣金', $item['uid'], 'now_money', 'brokerage', $divide, $item['spread_uid'], $item['brokerage_price'], '商家销售额分红');
  748. }
  749. }
  750. }
  751. $model->saveAll($user);
  752. }
  753. if ($diamonds_user){
  754. // 钻石会员
  755. foreach ($diamonds_user as &$item){
  756. $item['brokerage_price'] += $diamonds_divide;
  757. UserBill::income('佣金', $item['uid'], 'now_money', 'brokerage', $diamonds_divide, $item['spread_uid'], $item['brokerage_price'], '商家销售额分红');
  758. }
  759. $model->saveAll($diamonds_user);
  760. }
  761. }
  762. public static function gift_bag($order_id)
  763. {
  764. $data = self::user_count();
  765. $model = new self();
  766. $silver_user = $data['silver_user'];
  767. $silver = $data['silver'];
  768. $gold_user = $data['gold_user'];
  769. $gold = $data['gold'];
  770. $diamonds_user = $data['diamonds_user'];
  771. $diamonds = $data['diamonds'];
  772. // 礼包分红
  773. $order = StoreOrder::where('order_id', $order_id)->find();
  774. $user = User::where('uid', $order['uid'])->find();
  775. $info = StoreOrderCartInfo::where('oid', $order['id'])->where('product_id', '<=', 2)->select();
  776. foreach ($info as $item){
  777. $product = StoreProduct::where('id', $item['product_id'])->find();
  778. if ($user['spread_uid']){
  779. // 当前用户是否有上级
  780. $spread = User::where('uid', $user['spread_uid'])->find();
  781. if ($spread['level'] == 2){
  782. // 如果用户是黄金会员
  783. $push = $product['price'] * 0.2; //获得价格百分之20的佣金
  784. if ($spread['brokerage_price']+$push > 6000){
  785. $max = 6000 - $spread['brokerage_price'];
  786. $spread['brokerage_price'] += 6000 - $spread['brokerage_price'];
  787. UserBill::income('佣金', $spread['uid'], 'now_money', 'brokerage', $max, $spread['spread_uid'], $spread['brokerage_price'], '直推礼包销售额20%分红,已达到上限');
  788. }else{
  789. $spread['brokerage_price'] += $push;
  790. UserBill::income('佣金', $spread['uid'], 'now_money', 'brokerage', $push, $spread['spread_uid'], $spread['brokerage_price'], '直推礼包销售额20%分红');
  791. }
  792. $spread->save();
  793. }
  794. }
  795. if ($product['id'] == 1){
  796. $divide = ($product['price'] * 0.1)/$silver; // 200礼包的百分之十白银会员平分
  797. $diamonds_divide = ($product['price'] * 0.03)/$diamonds;//钻石会员200礼包的百分之3
  798. if ($silver_user){
  799. foreach ($silver_user as &$value){
  800. if ($value['brokerage_price']+$divide > 200){
  801. $max = 200 - $value['brokerage_price'];
  802. $value['brokerage_price'] += 200 - $value['brokerage_price'];
  803. UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $max, $value['spread_uid'], $value['brokerage_price'], '白银会员200礼包销售额分红,已达到上限');
  804. }else{
  805. $value['brokerage_price'] += $divide;
  806. UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $divide, $value['spread_uid'], $value['brokerage_price'], '白银会员200礼包销售额分红');
  807. }
  808. }
  809. $model->saveAll($silver_user);
  810. }
  811. if ($diamonds_user){
  812. // 钻石会员
  813. foreach ($diamonds_user as &$value){
  814. $value['brokerage_price'] += $diamonds_divide;
  815. UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $diamonds_divide, $value['spread_uid'], $value['brokerage_price'], '钻石会员200礼包提成');
  816. }
  817. $model->saveAll($diamonds_user);
  818. }
  819. }elseif ($product['id'] == 2){
  820. //1991礼包 白银会员和黄金会员分百分之十的金额
  821. $divide = ($product['price'] * 0.1)/($silver+$gold);
  822. $user = array_merge($silver_user, $gold_user);
  823. $top_user = getParent($order['uid']); // 获取到上级所有钻石用户
  824. $count = count($top_user);
  825. if ($count > 0){
  826. $top_divide = ($product['price'] * 0.2)/$count;//1991礼包百分之二十上级所有钻石会员平分
  827. foreach ($top_user as $v){
  828. $details = self::where('uid', $v)->find();
  829. $details['brokerage_price'] += $top_divide;
  830. $details->save();
  831. UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $top_divide, $details['spread_uid'], $details['brokerage_price'], '钻石会员1991礼包销售额20%分红');
  832. }
  833. }
  834. if ($user){
  835. // 黄金会员和白银会员
  836. foreach ($user as &$value){
  837. if ($value['level'] == 1){
  838. if ($value['brokerage_price']+$divide > 200){
  839. $max = 200 - $value['brokerage_price'];
  840. $value['brokerage_price'] += 200 - $value['brokerage_price'];
  841. UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $max, $value['spread_uid'], $value['brokerage_price'], '白银会员1991礼包销售额分红,已达到上限');
  842. }else{
  843. $value['brokerage_price'] += $divide;
  844. UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $divide, $value['spread_uid'], $value['brokerage_price'], '白银会员1991礼包销售额分红');
  845. }
  846. }elseif ($value['level'] == 2){
  847. if ($value['brokerage_price']+$divide > 6000){
  848. $max = 6000 - $value['brokerage_price']; // 到达上限
  849. $value['brokerage_price'] += 6000 - $value['brokerage_price'];
  850. UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $max, $value['spread_uid'], $value['brokerage_price'], '黄金会员1991礼包销售额分红,已达到上限');
  851. }else{
  852. $value['brokerage_price'] += $divide;
  853. UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $divide, $value['spread_uid'], $value['brokerage_price'], '黄金会员1991礼包销售额分红');
  854. }
  855. }
  856. }
  857. $model->saveAll($user);
  858. }
  859. }
  860. }
  861. }
  862. /**
  863. * 普通商品确认收货分红
  864. * @param $uid
  865. * @param $order_id
  866. * @return void
  867. * @throws DataNotFoundException
  868. * @throws DbException
  869. * @throws ModelNotFoundException
  870. */
  871. public static function receiving($uid, $order_id)
  872. {
  873. $order = StoreOrder::where('order_id', $order_id)->find();
  874. $user = User::where('uid', $order['uid'])->find();
  875. $info = StoreOrderCartInfo::where('oid', $order['id'])->where('product_id', '>', 2)->select(); // 普通商品
  876. if ($user['spread_uid']){
  877. $top_user = getParent($uid); // 获取到上级所有钻石用户
  878. $count = count($top_user);
  879. $s_user = getParents($uid);// 获取到上级所有黄金白银用户
  880. $s_count = count($s_user);
  881. foreach ($info as $item){
  882. $product = StoreProduct::where('id', $item['product_id'])->find();
  883. if ($s_count > 0){
  884. // 黄金白银会员分红
  885. $divide = ($product['price'] * 0.1 )/ $s_count; // 普通商品百分之十的价格黄金和白银会员平分
  886. foreach ($s_user as $k => $v){
  887. $details = User::where('uid', $v)->find();
  888. if ($details['level'] == 1){
  889. if ($details['brokerage_price'] < 200){
  890. if ($details['brokerage_price']+$divide > 200){
  891. $max = 200 - $details['brokerage_price'];
  892. $details['brokerage_price'] += $max;
  893. $details->save();
  894. UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $max, $details['spread_uid'], $details['brokerage_price'], '白银会员普通商品分红,已达到上限');
  895. }else{
  896. $details['brokerage_price'] += $divide;
  897. $details->save();
  898. UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $divide, $details['spread_uid'], $details['brokerage_price'], '白银会员普通商品分红');
  899. }
  900. }
  901. }elseif ($details['level'] == 2){
  902. if ($details['brokerage_price']+$divide > 6000){
  903. $max = 6000 - $details['brokerage_price'];
  904. $details['brokerage_price'] += $max;
  905. $details->save();
  906. UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $max, $details['spread_uid'], $details['brokerage_price'], '黄金会员普通商品分红,已达到上限');
  907. }else{
  908. $details['brokerage_price'] += $divide;
  909. $details->save();
  910. UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $divide, $details['spread_uid'], $details['brokerage_price'], '黄金会员普通商品分红');
  911. }
  912. }
  913. }
  914. }
  915. if ($count > 0){
  916. // 钻石会员分红
  917. $top_divide = ($product['price'] * 0.05 )/ $count; // 钻石会员平分普通商品百分之五
  918. foreach ($top_user as $v){
  919. $details = self::where('uid', $v)->find();
  920. $details['brokerage_price'] += $top_divide;
  921. $details->save();
  922. UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $top_divide, $details['spread_uid'], $details['brokerage_price'], '钻石会员普通商品分成');
  923. }
  924. }
  925. }
  926. }
  927. }
  928. /**
  929. * 分红
  930. * @param $type
  931. * @param $money
  932. * @param $uid
  933. * @param $order_id
  934. * @return array
  935. * @throws \Exception
  936. */
  937. public static function bonus($type, $money, $uid = 0, $order_id = '')
  938. {
  939. switch ($type){
  940. case 1:
  941. self::bring_forward($money);
  942. break;
  943. case 2:
  944. self::gift_bag($order_id);
  945. break;
  946. case 3:
  947. self::receiving($uid, $order_id);
  948. break;
  949. }
  950. return [];
  951. }
  952. public static function release()
  953. {
  954. $model = Db::name('release');
  955. $time = strtotime('today');
  956. if (!$model->where('time', $time)->find()){
  957. $user = self::where('integral', '>', 0)->select();
  958. foreach ($user as &$item){
  959. $release = round($item['integral']) * 0.03;// 释放积分额度
  960. $item['integral'] -= $release;
  961. $item['voucher'] += $release;
  962. UserBill::expend('释放积分', $item['uid'], 'integral', 'release_integral', $release, 0, $item['integral'], '每天释放积分额度');
  963. UserBill::income('增加抵用券',$item['uid'], 'voucher', 'release_ivoucher', $release, 0, $item['voucher'], '每天释放积分,变换抵用券');
  964. $item->save();
  965. }
  966. $model->insert([
  967. 'time' => $time
  968. ]);
  969. }
  970. }
  971. }