User.php 33 KB

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