User.php 34 KB

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