User.php 32 KB

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