User.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. $cart_info = $item['cart_info'];
  778. for ($i = 0;$i < $cart_info['cart_num']; $i++){
  779. $product = StoreProduct::where('id', $item['product_id'])->find();
  780. if ($user['spread_uid']){
  781. // 当前用户是否有上级
  782. $spread = User::where('uid', $user['spread_uid'])->find();
  783. if ($spread['level'] == 2){
  784. // 如果用户是黄金会员
  785. $push = $product['price'] * 0.2; //获得价格百分之20的佣金
  786. if ($spread['brokerage_price']+$push > 6000){
  787. $max = 6000 - $spread['brokerage_price'];
  788. $spread['brokerage_price'] += 6000 - $spread['brokerage_price'];
  789. UserBill::income('佣金', $spread['uid'], 'now_money', 'brokerage', $max, $spread['spread_uid'], $spread['brokerage_price'], '直推礼包销售额20%分红,已达到上限');
  790. }else{
  791. $spread['brokerage_price'] += $push;
  792. UserBill::income('佣金', $spread['uid'], 'now_money', 'brokerage', $push, $spread['spread_uid'], $spread['brokerage_price'], '直推礼包销售额20%分红');
  793. }
  794. $spread->save();
  795. }
  796. }
  797. if ($product['id'] == 1){
  798. $divide = $silver == 0 ? 0 :($product['price'] * 0.1)/$silver; // 200礼包的百分之十白银会员平分
  799. $diamonds_divide = $silver == 0 ? 0:($product['price'] * 0.03)/$diamonds;//钻石会员200礼包的百分之3
  800. if ($silver_user){
  801. foreach ($silver_user as &$value){
  802. if ($value['brokerage_price']+$divide > 200){
  803. $max = 200 - $value['brokerage_price'];
  804. $value['brokerage_price'] += 200 - $value['brokerage_price'];
  805. UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $max, $value['spread_uid'], $value['brokerage_price'], '白银会员200礼包销售额分红,已达到上限');
  806. }else{
  807. $value['brokerage_price'] += $divide;
  808. UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $divide, $value['spread_uid'], $value['brokerage_price'], '白银会员200礼包销售额分红');
  809. }
  810. }
  811. $model->saveAll($silver_user);
  812. }
  813. if ($diamonds_user){
  814. // 钻石会员
  815. foreach ($diamonds_user as &$value){
  816. $value['brokerage_price'] += $diamonds_divide;
  817. UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $diamonds_divide, $value['spread_uid'], $value['brokerage_price'], '钻石会员200礼包提成');
  818. }
  819. $model->saveAll($diamonds_user);
  820. }
  821. }elseif ($product['id'] == 2){
  822. //1991礼包 白银会员和黄金会员分百分之十的金额
  823. $divide = $silver+$gold == 0? 0 : ($product['price'] * 0.1)/($silver+$gold);
  824. $user = array_merge($silver_user, $gold_user);
  825. $top_user = getParent($order['uid']); // 获取到上级所有钻石用户
  826. $count = count($top_user);
  827. if ($count > 0){
  828. $top_divide = ($product['price'] * 0.2)/$count;//1991礼包百分之二十上级所有钻石会员平分
  829. foreach ($top_user as $v){
  830. $details = self::where('uid', $v)->find();
  831. $details['brokerage_price'] += $top_divide;
  832. $details->save();
  833. UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $top_divide, $details['spread_uid'], $details['brokerage_price'], '钻石会员1991礼包销售额20%分红');
  834. }
  835. }
  836. if ($user){
  837. // 黄金会员和白银会员
  838. foreach ($user as &$value){
  839. if ($value['level'] == 1){
  840. if ($value['brokerage_price']+$divide > 200){
  841. $max = 200 - $value['brokerage_price'];
  842. $value['brokerage_price'] += 200 - $value['brokerage_price'];
  843. UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $max, $value['spread_uid'], $value['brokerage_price'], '白银会员1991礼包销售额分红,已达到上限');
  844. }else{
  845. $value['brokerage_price'] += $divide;
  846. UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $divide, $value['spread_uid'], $value['brokerage_price'], '白银会员1991礼包销售额分红');
  847. }
  848. }elseif ($value['level'] == 2){
  849. if ($value['brokerage_price']+$divide > 6000){
  850. $max = 6000 - $value['brokerage_price']; // 到达上限
  851. $value['brokerage_price'] += 6000 - $value['brokerage_price'];
  852. UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $max, $value['spread_uid'], $value['brokerage_price'], '黄金会员1991礼包销售额分红,已达到上限');
  853. }else{
  854. $value['brokerage_price'] += $divide;
  855. UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $divide, $value['spread_uid'], $value['brokerage_price'], '黄金会员1991礼包销售额分红');
  856. }
  857. }
  858. }
  859. $model->saveAll($user);
  860. }
  861. }
  862. }
  863. }
  864. }
  865. /**
  866. * 普通商品确认收货分红
  867. * @param $uid
  868. * @param $order_id
  869. * @return void
  870. * @throws DataNotFoundException
  871. * @throws DbException
  872. * @throws ModelNotFoundException
  873. */
  874. public static function receiving($uid, $order_id)
  875. {
  876. $order = StoreOrder::where('order_id', $order_id)->find();
  877. $user = User::where('uid', $order['uid'])->find();
  878. $info = StoreOrderCartInfo::where('oid', $order['id'])->where('product_id', '>', 2)->select(); // 普通商品
  879. if ($user['spread_uid']){
  880. $top_user = getParent($uid); // 获取到上级所有钻石用户
  881. $count = count($top_user);
  882. $s_user = getParents($uid);// 获取到上级所有黄金白银用户
  883. $s_count = count($s_user);
  884. foreach ($info as $item){
  885. $cart_info = $item['cart_info'];
  886. $product = StoreProduct::where('id', $item['product_id'])->find();
  887. $product['price'] = $product['price'] * $cart_info['cart_num'];
  888. if ($s_count > 0){
  889. // 黄金白银会员分红
  890. $divide = ($product['price'] * 0.1 )/ $s_count; // 普通商品百分之十的价格黄金和白银会员平分
  891. foreach ($s_user as $k => $v){
  892. $details = User::where('uid', $v)->find();
  893. if ($details['level'] == 1){
  894. if ($details['brokerage_price'] < 200){
  895. if ($details['brokerage_price']+$divide > 200){
  896. $max = 200 - $details['brokerage_price'];
  897. $details['brokerage_price'] += $max;
  898. $details->save();
  899. UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $max, $details['spread_uid'], $details['brokerage_price'], '白银会员普通商品分红,已达到上限');
  900. }else{
  901. $details['brokerage_price'] += $divide;
  902. $details->save();
  903. UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $divide, $details['spread_uid'], $details['brokerage_price'], '白银会员普通商品分红');
  904. }
  905. }
  906. }elseif ($details['level'] == 2){
  907. if ($details['brokerage_price']+$divide > 6000){
  908. $max = 6000 - $details['brokerage_price'];
  909. $details['brokerage_price'] += $max;
  910. $details->save();
  911. UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $max, $details['spread_uid'], $details['brokerage_price'], '黄金会员普通商品分红,已达到上限');
  912. }else{
  913. $details['brokerage_price'] += $divide;
  914. $details->save();
  915. UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $divide, $details['spread_uid'], $details['brokerage_price'], '黄金会员普通商品分红');
  916. }
  917. }
  918. }
  919. }
  920. if ($count > 0){
  921. // 钻石会员分红
  922. $top_divide = ($product['price'] * 0.05 )/ $count; // 钻石会员平分普通商品百分之五
  923. foreach ($top_user as $v){
  924. $details = self::where('uid', $v)->find();
  925. $details['brokerage_price'] += $top_divide;
  926. $details->save();
  927. UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $top_divide, $details['spread_uid'], $details['brokerage_price'], '钻石会员普通商品分成');
  928. }
  929. }
  930. }
  931. }
  932. }
  933. /**
  934. * 分红
  935. * @param $type
  936. * @param $money
  937. * @param $uid
  938. * @param $order_id
  939. * @return array
  940. * @throws \Exception
  941. */
  942. public static function bonus($type, $money, $uid = 0, $order_id = '')
  943. {
  944. switch ($type){
  945. case 1:
  946. self::bring_forward($money);
  947. break;
  948. case 2:
  949. self::gift_bag($order_id);
  950. break;
  951. case 3:
  952. self::receiving($uid, $order_id);
  953. break;
  954. }
  955. return [];
  956. }
  957. public static function release()
  958. {
  959. $model = Db::name('release');
  960. $time = strtotime('today');
  961. if (!$model->where('time', $time)->find()){
  962. $user = self::where('integral', '>', 0)->select();
  963. foreach ($user as &$item){
  964. $release = round($item['integral']) * 0.03;// 释放积分额度
  965. $item['integral'] -= $release;
  966. $item['voucher'] += $release;
  967. UserBill::expend('释放积分', $item['uid'], 'integral', 'release_integral', $release, 0, $item['integral'], '每天释放积分额度');
  968. UserBill::income('增加抵用券',$item['uid'], 'voucher', 'release_ivoucher', $release, 0, $item['voucher'], '每天释放积分,变换抵用券');
  969. $item->save();
  970. }
  971. $model->insert([
  972. 'time' => $time
  973. ]);
  974. }
  975. }
  976. /**
  977. * 获取ID
  978. * @param string $key 键名
  979. * @return mixed
  980. */
  981. public static function getkeytoid(string $key)
  982. {
  983. $rs = self::order('uid DESC')->find();
  984. return $rs['uid']+1;
  985. }
  986. }