User.php 31 KB

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