User.php 29 KB

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