User.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. if ($orderInfo['over'] > 0) $sum_sp = ['real' => $orderInfo['over'], 'virtual' => 0];
  348. $brokeragePrice = bcmul($sum_sp['real'], $level['same_level_award_ratio'], 2);
  349. if ($brokeragePrice > 0) {
  350. $mark = '用户' . $userInfo['nickname'] . '(' . $orderInfo['uid'] . ')' . '购买实体升级商品,获得平级奖励' . $brokeragePrice;
  351. $res = $res && UserBill::income('获得商品平级奖', $spread_user['uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], $spread_user['brokerage_price'], $mark, 0);
  352. }
  353. $brokeragePrice = bcmul($sum_sp['virtual'], $level['same_level_award_ratio'], 2);
  354. if ($brokeragePrice > 0) {
  355. $mark = '用户' . $userInfo['nickname'] . '(' . $orderInfo['uid'] . ')' . '购买虚拟升级商品,获得平级奖励' . $brokeragePrice;
  356. $res = $res && UserBill::income('获得商品平级奖', $spread_user['uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], bcadd($spread_user['brokerage_price'], $brokeragePrice, 2), $mark);
  357. Message::addMessage($orderInfo['uid'], $mark, 200);
  358. $res = $res && User::where('uid', $spread_user['uid'])->inc('brokerage_price', $brokeragePrice)->update();
  359. }
  360. return $res;
  361. }
  362. /**
  363. * TODO 二级推广
  364. * @param $orderInfo
  365. * @return bool
  366. * @throws DataNotFoundException
  367. * @throws ModelNotFoundException
  368. * @throws DbException
  369. */
  370. public static function backOrderBrokerageTwo($orderInfo, bool $open = true)
  371. {
  372. //TODO 获取购买商品的用户
  373. $userInfo = User::getUserInfo($orderInfo['uid']);
  374. //TODO 获取上推广人
  375. $userInfoTwo = User::getUserInfo($userInfo['spread_uid']);
  376. //TODO 上推广人不存在 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
  377. if (!$userInfoTwo || !$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid']) return true;
  378. //TODO 获取后台分销类型 1 指定分销 2 人人分销
  379. if (!User::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
  380. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  381. list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($orderInfo['uid'], $cartId, false);
  382. //TODO 返佣金额小于等于0 直接返回不返佣金
  383. if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
  384. // $open && self::beginTrans();
  385. $res = true;
  386. if ($virtualBrokeragePrice > 0) {
  387. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  388. //TODO 上级推广员返佣之后的金额
  389. $balance = bcadd($spreadUserInfoTwo['brokerage_price'], $virtualBrokeragePrice, 2);
  390. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
  391. //TODO 添加推广记录
  392. $res1 = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
  393. //TODO 添加用户余额
  394. $res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
  395. Message::addMessage($userInfoTwo['spread_uid'], $mark, 200);
  396. //TODO 一级返佣成功 跳转二级返佣
  397. $res = $res1 && $res2;
  398. }
  399. if ($realBrokeragePrice > 0) {
  400. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  401. //TODO 上级推广员返佣之后的金额
  402. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
  403. //TODO 添加推广记录
  404. $res = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfoTwo['brokerage_price'], $mark, 0);
  405. }
  406. // $res = $res && self::backOrderBrokerageTwo($orderInfo);
  407. // $open && self::checkTrans($res);
  408. return $res;
  409. }
  410. /**
  411. * 发放实际商品佣金
  412. * @param $orderInfo
  413. * @return bool
  414. * @throws DataNotFoundException
  415. * @throws DbException
  416. * @throws ModelNotFoundException
  417. */
  418. public static function sendBackOrderBrokerage($orderInfo)
  419. {
  420. $list = UserBill::where(['link_id' => $orderInfo['id'], 'type' => 'brokerage', 'category' => 'now_money', 'pm' => 1, 'status' => 0])->select();
  421. if ($list) {
  422. self::beginTrans();
  423. try {
  424. foreach ($list as $v) {
  425. self::bcInc($v['uid'], 'brokerage_price', $v['number'], 'uid');
  426. $balance = self::getUserInfo($v['uid'])['brokerage_price'];
  427. UserBill::where('id', $v['id'])->update(['add_time' => time(), 'status' => 1, 'balance' => $balance]);
  428. Message::addMessage($v['uid'], $v['mark'], 200);
  429. }
  430. self::commitTrans();
  431. return true;
  432. } catch (Exception $e) {
  433. self::rollbackTrans();
  434. return false;
  435. }
  436. }
  437. return true;
  438. }
  439. /**
  440. * 获取推荐人 暂无使用
  441. * @param $uid
  442. * @param $page
  443. * @param $limit
  444. * @return mixed
  445. */
  446. public static function getSpreadList($uid, $page, $limit)
  447. {
  448. $list = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->page($page, $limit)->order('add_time DESC')->select();
  449. foreach ($list as $k => $user) {
  450. $list[$k]['add_time'] = date('Y/m/d', $user['add_time']);
  451. $list[$k]['price'] = StoreOrder::getUserPrice($user['uid']);
  452. }
  453. $count = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->count();
  454. $data['count'] = $count;
  455. $data['list'] = $list;
  456. return $data;
  457. }
  458. /**
  459. * 获取某个用户的下级uid
  460. * @param $uid
  461. * @return array
  462. */
  463. public static function getOneSpreadUid($uid)
  464. {
  465. return self::where('spread_uid', $uid)->column('uid');
  466. }
  467. /**
  468. * 修改个人信息
  469. * @param $avatar 头像
  470. * @param $nickname 昵称
  471. * @param $uid 用户uid
  472. * @return bool
  473. */
  474. public static function editUser($avatar, $nickname, $uid)
  475. {
  476. return self::edit(['avatar' => $avatar, 'nickname' => $nickname], $uid, 'uid');
  477. }
  478. /**
  479. * TODO 获取推广人数 一级
  480. * @param int $uid
  481. * @return bool|int|string
  482. */
  483. public static function getSpreadCount($uid = 0)
  484. {
  485. if (!$uid) return false;
  486. return self::where('spread_uid', $uid)->count();
  487. }
  488. /**
  489. * 修改当前用户的推广人数
  490. * @param $uid
  491. */
  492. public static function setUserSpreadCount($uid)
  493. {
  494. if (!$uid) return true;
  495. if (self::getSpreadCount($uid) > 0) {
  496. self::where('uid', $uid)->update(['spread_count' => 0]);
  497. } else {
  498. self::where('uid', $uid)->update(['spread_count' => 0]);
  499. return true;
  500. }
  501. return self::where('uid', $uid)->update(['spread_count' => self::getSpreadCount($uid)]);
  502. }
  503. /**
  504. * TODO 获取推广人数 二级
  505. * @param int $uid
  506. * @return bool|int|string
  507. */
  508. public static function getSpreadLevelCount($uid = 0)
  509. {
  510. if (!$uid) return false;
  511. $uidSubordinate = self::where('spread_uid', $uid)->column('uid');
  512. if (!count($uidSubordinate)) return 0;
  513. return self::where('spread_uid', 'IN', implode(',', $uidSubordinate))->count();
  514. }
  515. /**
  516. * 获取用户下级推广人
  517. * @param int $uid 当前用户
  518. * @param int $grade 等级 0 一级 1 二级
  519. * @param string $orderBy 排序
  520. * @param string $keyword
  521. * @param int $page
  522. * @param int $limit
  523. * @return array|bool
  524. */
  525. public static function getUserSpreadGrade($uid = 0, $grade = 0, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  526. {
  527. if (!$uid) return [];
  528. $gradeGroup = [0, 1];
  529. if (!in_array($grade, $gradeGroup)) return self::setErrorInfo('等级错误');
  530. $userStair = self::where('spread_uid', $uid)->column('uid');
  531. if (!count($userStair)) return [];
  532. if ($grade == 0) return self::getUserSpreadCountList(implode(',', $userStair), $orderBy, $keyword, $page, $limit);
  533. $userSecondary = self::where('spread_uid', 'in', implode(',', $userStair))->column('uid');
  534. return self::getUserSpreadCountList(implode(',', $userSecondary), $orderBy, $keyword, $page, $limit);
  535. }
  536. /**
  537. * 获取团队信息
  538. * @param $uid
  539. * @param string $orderBy
  540. * @param string $keyword
  541. * @param int $page
  542. * @param int $limit
  543. * @return array
  544. */
  545. public static function getUserSpreadCountList($uid, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  546. {
  547. $model = new self;
  548. if ($orderBy === '') $orderBy = 'u.add_time desc';
  549. $model = $model->alias(' u');
  550. $sql = StoreOrder::where('o.paid', 1)->group('o.uid')->field(['SUM(o.pay_price) as numberCount', 'o.uid', 'o.order_id'])
  551. ->where('o.is_del', 0)->where('o.is_system_del', 0)->alias('o')->fetchSql(true)->select();
  552. $model = $model->join("(" . $sql . ") p", 'u.uid = p.uid', 'LEFT');
  553. $model = $model->where('u.uid', 'IN', $uid);
  554. $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");
  555. if (strlen(trim($keyword))) $model = $model->where('u.nickname|u.phone', 'like', "%$keyword%");
  556. $model = $model->group('u.uid');
  557. $model = $model->order($orderBy);
  558. $model = $model->page($page, $limit);
  559. $list = $model->select();
  560. if ($list) {
  561. $list = $list->toArray();
  562. foreach ($list as &$v) {
  563. // $orders = StoreOrder::where('paid', 1)
  564. // ->where('refund_status', 0)
  565. // ->where('is_del', 0)
  566. // ->where('uid', $v['uid'])
  567. // ->select();
  568. // $count = 0;
  569. // foreach ($orders as $vv) {
  570. // $product_ids = StoreCart::where('id', 'in', $vv['cart_id'])->select();
  571. // foreach ($product_ids as $vvv) {
  572. // $cates = StoreProductCate::where('product_id', $vvv['product_id'])->column('cate_id');
  573. // if (in_array(49, $cates)) {
  574. // $count += $vvv['cart_num'];
  575. // }
  576. // }
  577. // }
  578. $v['level_shop_num'] = Achievement::where('uid', $v['uid'])->where('from_uid', $v['uid'])->sum('achievement');
  579. }
  580. return $list;
  581. } else
  582. return [];
  583. }
  584. /**
  585. * 设置用户的上级关系
  586. * @param $uid 用户uid
  587. * @param $spreadUid 上级用户uid
  588. * @return User|bool
  589. * @throws DataNotFoundException
  590. * @throws ModelNotFoundException
  591. * @throws DbException
  592. */
  593. public static function setSpreadUid($uid, $spreadUid)
  594. {
  595. return UserSpread::setSpread($uid, $spreadUid);
  596. // 自己不能绑定自己为上级
  597. // if ($uid == $spreadUid) return false;
  598. // //TODO 获取后台分销类型
  599. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  600. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  601. // if ($storeBrokerageStatus == 1) {
  602. // $spreadCount = self::where('uid', $spreadUid)->count();
  603. // if ($spreadCount) {
  604. // $spreadInfo = self::where('uid', $spreadUid)->find();
  605. // if ($spreadInfo->is_promoter) {
  606. // //TODO 只有扫码才可以获得推广权限
  607. // if (isset($wechatUser['isPromoter'])) $data['is_promoter'] = 1;
  608. // }
  609. // }
  610. // }
  611. // return self::where('uid', $uid)->update(['spread_uid' => $spreadUid, 'spread_time' => time()]);
  612. }
  613. /**
  614. * 判断上下级关系是否存在
  615. * @param $uid
  616. * @param $spreadUid
  617. * @return bool|int
  618. */
  619. public static function validSpread($uid, $spreadUid)
  620. {
  621. if (!$uid || !$spreadUid) return false;
  622. return self::where('uid', $uid)->where('spread_uid', $spreadUid)->count();
  623. }
  624. /**
  625. * H5用户注册
  626. * @param $account
  627. * @param $password
  628. * @param $spread
  629. * @return bool
  630. * @throws DataNotFoundException
  631. * @throws DbException
  632. * @throws ModelNotFoundException
  633. */
  634. public static function register($account, $password, $trade_password, $spread, $site_id)
  635. {
  636. if (self::be(['account' => $account, 'site_id' => $site_id])) return self::setErrorInfo('用户已存在');
  637. $phone = mobile_check($account) ? $account : '';
  638. $email = mobile_check($account) ? '' : $account;
  639. $data['account'] = $account;
  640. $data['pwd'] = md5($password);
  641. $data['trade_pwd'] = md5(md5($trade_password));
  642. $data['phone'] = $phone;
  643. $data['email'] = $email;
  644. // if ($spread) {
  645. // $data['spread_uid'] = $spread;
  646. // $data['spread_time'] = time();
  647. // }
  648. $data['real_name'] = '';
  649. $data['birthday'] = 0;
  650. $data['card_id'] = '';
  651. $data['mark'] = '';
  652. $data['addres'] = '';
  653. $data['user_type'] = 'h5';
  654. $data['add_time'] = time();
  655. $data['add_ip'] = app('request')->ip();
  656. $data['last_time'] = time();
  657. $data['last_ip'] = app('request')->ip();
  658. $data['nickname'] = substr(md5($account . time()), 0, 12);
  659. $data['avatar'] = $data['headimgurl'] = sys_config('h5_avatar');
  660. $data['city'] = '';
  661. $data['language'] = '';
  662. $data['province'] = '';
  663. $data['country'] = '';
  664. $data['is_promoter'] = 1;
  665. $data['site_id'] = $site_id;
  666. self::beginTrans();
  667. $res2 = WechatUser::create($data);
  668. $data['uid'] = $res2->uid;
  669. $res1 = self::create($data);
  670. $res1 = $res1 && self::sendRegisterGift($res2->uid);
  671. $res = $res1 && $res2 && UserSpread::setSpread($data['uid'], $spread);
  672. self::checkTrans($res);
  673. return $res;
  674. }
  675. public static function sendRegisterGift($uid)
  676. {
  677. $gift = sys_config('register_gift', 0);
  678. if ($gift <= 0) return true;
  679. $res = UserMoney::incomeMoney($uid, 'LALA', $gift, 'register_gift', '注册赠送', '注册赠送LALA', 0, 1, false);
  680. return $res ?: self::setErrorInfo('发放注册奖失败');
  681. }
  682. /**
  683. * 密码修改
  684. * @param $account
  685. * @param $password
  686. * @return User|bool
  687. */
  688. public
  689. static function reset($account, $password, $site_id, $type = 1)
  690. {
  691. if (!self::be(['account' => $account])) return self::setErrorInfo('用户不存在');
  692. $count = self::where('account', $account)->where('site_id', $site_id)->where($type == 1 ? 'pwd' : 'trade_pwd', $type == 1 ? md5($password) : md5(md5($password)))->count();
  693. if ($count) return true;
  694. return self::where('account', $account)->where('site_id', $site_id)->update([$type == 1 ? 'pwd' : 'trade_pwd' => $type == 1 ? md5($password) : md5(md5($password))]);
  695. }
  696. /**
  697. * 获取手机号是否注册
  698. * @param $phone
  699. * @return bool
  700. */
  701. public
  702. static function checkPhone($phone, $site_id)
  703. {
  704. return self::be(['account' => $phone, 'site_id' => $site_id]);
  705. }
  706. /**
  707. * 获取推广人
  708. * @param $data 查询条件
  709. * @return array
  710. * @throws DataNotFoundException
  711. * @throws ModelNotFoundException
  712. * @throws \think\exception\DbException
  713. */
  714. public
  715. static function getRankList($data)
  716. {
  717. switch ($data['type']) {
  718. case 'week':
  719. $startTime = strtotime('this week');
  720. $endTime = time();
  721. break;
  722. case 'month':
  723. $startTime = strtotime('last month');
  724. $endTime = time();
  725. break;
  726. }
  727. $list = self::alias('t0')
  728. ->field('t0 . uid,t0 . spread_uid,count(t1 . spread_uid) as count,t0 . add_time,t0 . nickname,t0 . avatar')
  729. ->join('user t1', 't0 . uid = t1 . spread_uid', 'LEFT')
  730. ->where('t1 . spread_uid', ' <> ', 0)
  731. ->order('count desc')
  732. ->order('t0 . uid desc')
  733. ->where('t1 . add_time', 'BETWEEN', [$startTime, $endTime])
  734. ->page($data['page'], $data['limit'])
  735. ->group('t0 . uid')
  736. ->select();
  737. return count($list) ? $list->toArray() : [];
  738. }
  739. /**
  740. * 获取佣金排行
  741. * @param $data
  742. * @return array
  743. */
  744. public
  745. static function brokerageRank($data)
  746. {
  747. $model = self::where('status', 1);
  748. switch ($data['type']) {
  749. case 'week':
  750. $model = $model->whereIn('uid', function ($query) {
  751. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  752. ->whereWeek('add_time')->field('uid');
  753. });
  754. break;
  755. case 'month':
  756. $model = $model->whereIn('uid', function ($query) {
  757. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  758. ->whereMonth('add_time')->field('uid');
  759. });
  760. break;
  761. }
  762. $users = $model->field('uid,nickname,avatar,brokerage_price')->order('brokerage_price desc')
  763. ->page((int)$data['page'], (int)$data['limit'])->select();
  764. return count($users) ? $users->toArray() : [];
  765. }
  766. /**
  767. * 获取当前用户的佣金排行位置
  768. * @param $uid
  769. * @return int
  770. */
  771. public
  772. static function currentUserRank($type, $brokerage_price)
  773. {
  774. $model = self::where('status', 1);
  775. switch ($type) {
  776. case 'week':
  777. $model = $model->whereIn('uid', function ($query) {
  778. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  779. ->whereWeek('add_time')->field('uid');
  780. });
  781. break;
  782. case 'month':
  783. $model = $model->whereIn('uid', function ($query) {
  784. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  785. ->whereMonth('add_time')->field('uid');
  786. });
  787. break;
  788. }
  789. return $model->where('brokerage_price', ' > ', $brokerage_price)->count('uid');
  790. }
  791. }