User.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. <?php
  2. namespace app\models\user;
  3. use app\admin\model\diagnosis\DiagnosisOrder;
  4. use app\admin\model\user\UserCard;
  5. use app\admin\model\user\UserEquity;
  6. use app\models\store\StoreOrder;
  7. use app\models\store\StoreProduct;
  8. use app\Request;
  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', 'account', '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)
  58. {
  59. $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
  60. $res = self::create([
  61. 'account' => 'wx' . $wechatUser['uid'] . time(),
  62. 'pwd' => md5(123456),
  63. 'nickname' => $wechatUser['nickname'] ?: '',
  64. 'avatar' => $wechatUser['headimgurl'] ?: '',
  65. 'add_time' => time(),
  66. 'add_ip' => app('request')->ip(),
  67. 'last_time' => time(),
  68. 'last_ip' => app('request')->ip(),
  69. 'uid' => $wechatUser['uid'],
  70. 'is_promoter' => $storeBrokerageStatu != 1 ? 1 : 0,
  71. 'user_type' => 'wechat'
  72. ]);
  73. return $res && UserSpread::setSpread($wechatUser['uid'], $spread_uid);
  74. }
  75. /**
  76. * TODO 获取会员是否被清除过的时间
  77. * @param $uid
  78. * @return int|mixed
  79. * @throws DataNotFoundException
  80. * @throws ModelNotFoundException
  81. * @throws DbException
  82. */
  83. public static function getCleanTime($uid)
  84. {
  85. $user = self::where('uid', $uid)->field(['add_time', 'clean_time'])->find();
  86. if (!$user) return 0;
  87. return $user['clean_time'] ? $user['clean_time'] : $user['add_time'];
  88. }
  89. /**
  90. * 更新用户信息
  91. * @param array $wechatUser 用户信息
  92. * @param int $uid 用户uid
  93. * @return bool|void
  94. * @throws DataNotFoundException
  95. * @throws ModelNotFoundException
  96. * @throws DbException
  97. */
  98. public static function updateWechatUser($wechatUser, $uid)
  99. {
  100. $userInfo = self::where('uid', $uid)->find();
  101. if (!$userInfo) return;
  102. //增加成为分销权限
  103. if (!$userInfo->is_promoter) {
  104. $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $uid])->sum('pay_price');
  105. $status = is_brokerage_statu($price);
  106. } else {
  107. $status = false;
  108. }
  109. if ($userInfo->spread_uid) {
  110. return self::edit([
  111. 'nickname' => $wechatUser['nickname'] ?: '',
  112. 'avatar' => $wechatUser['headimgurl'] ?: '',
  113. 'phone' => $wechatUser['phone'],
  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. 'phone' => $wechatUser['phone'],
  123. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  124. // 'spread_uid' => 0,
  125. // 'spread_time' => 0,
  126. 'last_time' => time(),
  127. 'last_ip' => request()->ip(),
  128. ];
  129. //TODO 获取后台分销类型
  130. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  131. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  132. // if (isset($wechatUser['code']) && $wechatUser['code'] && $wechatUser['code'] != $uid && $uid != self::where('uid', $wechatUser['code'])->value('spread_uid')) {
  133. // if ($storeBrokerageStatus == 1) {
  134. // $spreadCount = self::where('uid', $wechatUser['code'])->count();
  135. // if ($spreadCount) {
  136. // $spreadInfo = self::where('uid', $wechatUser['code'])->find();
  137. // if ($spreadInfo->is_promoter) {
  138. // //TODO 只有扫码才可以获得推广权限
  139. //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
  140. // }
  141. // }
  142. // }
  143. // $data['spread_uid'] = $wechatUser['code'];
  144. // $data['spread_time'] = time();
  145. // }
  146. return self::edit($data, $uid, 'uid') && UserSpread::setSpread($uid, $wechatUser['code'] ?? 0);
  147. }
  148. }
  149. /**
  150. * 设置推广关系
  151. * @param $spread
  152. * @param $uid
  153. * @return bool
  154. * @throws DataNotFoundException
  155. * @throws ModelNotFoundException
  156. * @throws DbException
  157. */
  158. public static function setSpread($spread, $uid)
  159. {
  160. return UserSpread::setSpread($uid, $spread);
  161. //当前用户信息
  162. // $userInfo = self::where('uid', $uid)->find();
  163. // if (!$userInfo) return true;
  164. // //当前用户有上级直接返回
  165. // if ($userInfo->spread_uid) return true;
  166. // //没有推广编号直接返回
  167. // if (!$spread) return true;
  168. // if ($spread == $uid) return true;
  169. // if ($uid == self::where('uid', $spread)->value('spread_uid')) return true;
  170. // //TODO 获取后台分销类型
  171. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  172. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  173. // if ($storeBrokerageStatus == 1) {
  174. // $spreadCount = self::where('uid', $spread)->count();
  175. // if ($spreadCount) {
  176. // $spreadInfo = self::where('uid', $spread)->find();
  177. // if ($spreadInfo->is_promoter) {
  178. // //TODO 只有扫码才可以获得推广权限
  179. //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
  180. // }
  181. // }
  182. // }
  183. // $data['spread_uid'] = $spread;
  184. // $data['spread_time'] = time();
  185. // return self::edit($data, $uid, 'uid');
  186. }
  187. /**
  188. * 小程序用户添加
  189. * @param $routineUser
  190. * @param int $spread_uid
  191. * @return object
  192. * @throws DataNotFoundException
  193. * @throws DbException
  194. * @throws ModelNotFoundException
  195. */
  196. public static function setRoutineUser($routineUser, $spread_uid = 0)
  197. {
  198. self::beginTrans();
  199. $res1 = true;
  200. if ($spread_uid) $res1 = self::where('uid', $spread_uid)->inc('spread_count', 1)->update();
  201. $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
  202. $res2 = self::create([
  203. 'account' => 'rt' . $routineUser['uid'] . time(),
  204. 'pwd' => md5(123456),
  205. 'nickname' => $routineUser['nickname'] ?: '',
  206. 'avatar' => $routineUser['headimgurl'] ?: '',
  207. // 'spread_uid' => $spread_uid,
  208. 'is_promoter' => $storeBrokerageStatu != 1 ? 1 : 0,
  209. // 'spread_time' => $spread_uid ? time() : 0,
  210. 'uid' => $routineUser['uid'],
  211. 'add_time' => $routineUser['add_time'],
  212. 'add_ip' => request()->ip(),
  213. 'last_time' => time(),
  214. 'last_ip' => request()->ip(),
  215. 'user_type' => $routineUser['user_type'],
  216. 'phone' => $routineUser['phone'],
  217. ]);
  218. $res = $res1 && $res2 && UserSpread::setSpread($routineUser['uid'], $spread_uid);
  219. self::checkTrans($res);
  220. return $res2;
  221. }
  222. /**
  223. * 获得当前登陆用户UID
  224. * @return int $uid
  225. */
  226. public static function getActiveUid()
  227. {
  228. $uid = null;
  229. $uid = Session::get('LoginUid');
  230. if ($uid) return $uid;
  231. else return 0;
  232. }
  233. /**
  234. * TODO 查询当前用户信息
  235. * @param $uid $uid 用户编号
  236. * @param string $field $field 查询的字段
  237. * @return array
  238. * @throws DataNotFoundException
  239. * @throws ModelNotFoundException
  240. * @throws DbException
  241. */
  242. public static function getUserInfo($uid, $field = '')
  243. {
  244. if (strlen(trim($field))) $userInfo = self::where('uid', $uid)->field($field)->find();
  245. else $userInfo = self::where('uid', $uid)->find();
  246. if (!$userInfo) return [];
  247. return $userInfo->toArray();
  248. }
  249. /**
  250. * 判断当前用户是否推广员
  251. * @param int $uid
  252. * @return bool
  253. */
  254. public static function isUserSpread($uid = 0)
  255. {
  256. if (!$uid) return false;
  257. $status = (int)sys_config('store_brokerage_statu');
  258. $isPromoter = true;
  259. if ($status == 1) $isPromoter = self::where('uid', $uid)->value('is_promoter');
  260. if ($isPromoter) return true;
  261. else return false;
  262. }
  263. /**
  264. * TODO 一级返佣
  265. * @param $orderInfo
  266. * @return bool
  267. * @throws DataNotFoundException
  268. * @throws ModelNotFoundException
  269. * @throws DbException
  270. */
  271. public static function backOrderBrokerage($orderInfo, bool $open = true)
  272. {
  273. //TODO 营销产品不返佣金
  274. if (isset($orderInfo['combination_id']) && $orderInfo['combination_id']) return true;
  275. if (isset($orderInfo['seckill_id']) && $orderInfo['seckill_id']) return true;
  276. if (isset($orderInfo['bargain_id']) && $orderInfo['bargain_id']) return true;
  277. $userInfo = User::getUserInfo($orderInfo['uid']);
  278. //TODO 当前用户不存在 没有上级 或者 当用用户上级时自己 直接返回
  279. if (!$userInfo || !$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid']) return true;
  280. if (!User::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return self::backOrderBrokerageTwo($orderInfo, $open);
  281. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  282. list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId);
  283. //TODO 返佣金额小于等于0 直接返回不返佣金
  284. if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
  285. //TODO 获取上级推广员信息
  286. // $open && self::beginTrans();
  287. $res = true;
  288. if ($virtualBrokeragePrice > 0) {
  289. $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
  290. //TODO 上级推广员返佣之后的金额
  291. $balance = bcadd($spreadUserInfo['brokerage_price'], $virtualBrokeragePrice, 2);
  292. $mark = $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
  293. //TODO 添加推广记录
  294. $res1 = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
  295. //TODO 添加用户余额
  296. $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
  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 backOrderBrokerageTwo($orderInfo, bool $open = true)
  320. {
  321. //TODO 获取购买商品的用户
  322. $userInfo = User::getUserInfo($orderInfo['uid']);
  323. //TODO 获取上推广人
  324. $userInfoTwo = User::getUserInfo($userInfo['spread_uid']);
  325. //TODO 上推广人不存在 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
  326. if (!$userInfoTwo || !$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid']) return true;
  327. //TODO 获取后台分销类型 1 指定分销 2 人人分销
  328. if (!User::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
  329. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  330. list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId, false);
  331. //TODO 返佣金额小于等于0 直接返回不返佣金
  332. if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
  333. // $open && self::beginTrans();
  334. $res = true;
  335. if ($virtualBrokeragePrice > 0) {
  336. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  337. //TODO 上级推广员返佣之后的金额
  338. $balance = bcadd($spreadUserInfoTwo['brokerage_price'], $virtualBrokeragePrice, 2);
  339. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
  340. //TODO 添加推广记录
  341. $res1 = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
  342. //TODO 添加用户余额
  343. $res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
  344. //TODO 一级返佣成功 跳转二级返佣
  345. $res = $res1 && $res2;
  346. }
  347. if ($realBrokeragePrice > 0) {
  348. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  349. //TODO 上级推广员返佣之后的金额
  350. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
  351. //TODO 添加推广记录
  352. $res = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfoTwo['brokerage_price'], $mark, 0);
  353. }
  354. // $res = $res && self::backOrderBrokerageTwo($orderInfo);
  355. // $open && self::checkTrans($res);
  356. return $res;
  357. }
  358. /**
  359. * 发放实际商品佣金
  360. * @param $orderInfo
  361. * @return bool
  362. * @throws DataNotFoundException
  363. * @throws DbException
  364. * @throws ModelNotFoundException
  365. */
  366. public static function sendBackOrderBrokerage($orderInfo)
  367. {
  368. $list = UserBill::where(['link_id' => $orderInfo['id'], 'type' => 'brokerage', 'category' => 'now_money', 'pm' => 1, 'status' => 0])->select();
  369. if ($list) {
  370. self::beginTrans();
  371. try {
  372. foreach ($list as $v) {
  373. self::bcInc($v['uid'], 'brokerage_price', $v['number'], 'uid');
  374. $balance = self::getUserInfo($v['uid'])['brokerage_price'];
  375. UserBill::where('id', $v['id'])->update(['add_time' => time(), 'status' => 1, 'balance' => $balance]);
  376. }
  377. self::commitTrans();
  378. return true;
  379. } catch (Exception $e) {
  380. self::rollbackTrans();
  381. return false;
  382. }
  383. }
  384. return true;
  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 $uid 用户uid
  419. * @return bool
  420. */
  421. public static function editUser($avatar, $nickname, $uid)
  422. {
  423. return self::edit(['avatar' => $avatar, 'nickname' => $nickname], $uid, 'uid');
  424. }
  425. /**
  426. * TODO 获取推广人数 一级
  427. * @param int $uid
  428. * @return bool|int|string
  429. */
  430. public static function getSpreadCount($uid = 0)
  431. {
  432. if (!$uid) return false;
  433. return self::where('spread_uid', $uid)->count();
  434. }
  435. /**
  436. * 修改当前用户的推广人数
  437. * @param $uid
  438. */
  439. public static function setUserSpreadCount($uid)
  440. {
  441. if (!$uid) return true;
  442. if (self::getSpreadCount($uid) > 0) {
  443. self::where('uid', $uid)->update(['spread_count' => 0]);
  444. } else {
  445. self::where('uid', $uid)->update(['spread_count' => 0]);
  446. return true;
  447. }
  448. return self::where('uid', $uid)->update(['spread_count' => self::getSpreadCount($uid)]);
  449. }
  450. /**
  451. * TODO 获取推广人数 二级
  452. * @param int $uid
  453. * @return bool|int|string
  454. */
  455. public static function getSpreadLevelCount($uid = 0)
  456. {
  457. if (!$uid) return false;
  458. $uidSubordinate = self::where('spread_uid', $uid)->column('uid');
  459. if (!count($uidSubordinate)) return 0;
  460. return self::where('spread_uid', 'IN', implode(',', $uidSubordinate))->count();
  461. }
  462. /**
  463. * 获取用户下级推广人
  464. * @param int $uid 当前用户
  465. * @param int $grade 等级 0 一级 1 二级
  466. * @param string $orderBy 排序
  467. * @param string $keyword
  468. * @param int $page
  469. * @param int $limit
  470. * @return array|bool
  471. */
  472. public static function getUserSpreadGrade($uid = 0, $grade = 0, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  473. {
  474. if (!$uid) return [];
  475. $gradeGroup = [0, 1];
  476. if (!in_array($grade, $gradeGroup)) return self::setErrorInfo('等级错误');
  477. $userStair = self::where('spread_uid', $uid)->column('uid');
  478. if (!count($userStair)) return [];
  479. if ($grade == 0) return self::getUserSpreadCountList(implode(',', $userStair), $orderBy, $keyword, $page, $limit);
  480. $userSecondary = self::where('spread_uid', 'in', implode(',', $userStair))->column('uid');
  481. return self::getUserSpreadCountList(implode(',', $userSecondary), $orderBy, $keyword, $page, $limit);
  482. }
  483. /**
  484. * 获取团队信息
  485. * @param $uid
  486. * @param string $orderBy
  487. * @param string $keyword
  488. * @param int $page
  489. * @param int $limit
  490. * @return array
  491. */
  492. public static function getUserSpreadCountList($uid, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  493. {
  494. $model = new self;
  495. if ($orderBy === '') $orderBy = 'u.add_time desc';
  496. $model = $model->alias(' u');
  497. $sql = StoreOrder::where('o.paid', 1)->group('o.uid')->field(['SUM(o.pay_price) as numberCount', 'o.uid', 'o.order_id'])
  498. ->where('o.is_del', 0)->where('o.is_system_del', 0)->alias('o')->fetchSql(true)->select();
  499. $model = $model->join("(" . $sql . ") p", 'u.uid = p.uid', 'LEFT');
  500. $model = $model->where('u.uid', 'IN', $uid);
  501. $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");
  502. if (strlen(trim($keyword))) $model = $model->where('u.nickname|u.phone', 'like', "%$keyword%");
  503. $model = $model->group('u.uid');
  504. $model = $model->order($orderBy);
  505. $model = $model->page($page, $limit);
  506. $list = $model->select();
  507. if ($list) return $list->toArray();
  508. else return [];
  509. }
  510. /**
  511. * 设置用户的上级关系
  512. * @param $uid 用户uid
  513. * @param $spreadUid 上级用户uid
  514. * @return User|bool
  515. * @throws DataNotFoundException
  516. * @throws ModelNotFoundException
  517. * @throws DbException
  518. */
  519. public static function setSpreadUid($uid, $spreadUid)
  520. {
  521. return UserSpread::setSpread($uid, $spreadUid);
  522. // 自己不能绑定自己为上级
  523. // if ($uid == $spreadUid) return false;
  524. // //TODO 获取后台分销类型
  525. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  526. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  527. // if ($storeBrokerageStatus == 1) {
  528. // $spreadCount = self::where('uid', $spreadUid)->count();
  529. // if ($spreadCount) {
  530. // $spreadInfo = self::where('uid', $spreadUid)->find();
  531. // if ($spreadInfo->is_promoter) {
  532. // //TODO 只有扫码才可以获得推广权限
  533. // if (isset($wechatUser['isPromoter'])) $data['is_promoter'] = 1;
  534. // }
  535. // }
  536. // }
  537. // return self::where('uid', $uid)->update(['spread_uid' => $spreadUid, 'spread_time' => time()]);
  538. }
  539. /**
  540. * 判断上下级关系是否存在
  541. * @param $uid
  542. * @param $spreadUid
  543. * @return bool|int
  544. */
  545. public static function validSpread($uid, $spreadUid)
  546. {
  547. if (!$uid || !$spreadUid) return false;
  548. return self::where('uid', $uid)->where('spread_uid', $spreadUid)->count();
  549. }
  550. /**
  551. * H5用户注册
  552. * @param $account
  553. * @param $password
  554. * @param $spread
  555. * @return bool
  556. * @throws DataNotFoundException
  557. * @throws DbException
  558. * @throws ModelNotFoundException
  559. */
  560. public static function register($account, $password, $spread)
  561. {
  562. $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
  563. if (self::be(['account' => $account])) return self::setErrorInfo('用户已存在');
  564. $phone = $account;
  565. $data['account'] = $account;
  566. $data['pwd'] = md5($password);
  567. $data['phone'] = $phone;
  568. // if ($spread) {
  569. // $data['spread_uid'] = $spread;
  570. // $data['spread_time'] = time();
  571. // }
  572. $data['real_name'] = '';
  573. $data['birthday'] = 0;
  574. $data['card_id'] = '';
  575. $data['mark'] = '';
  576. $data['addres'] = '';
  577. $data['user_type'] = 'h5';
  578. $data['add_time'] = time();
  579. $data['add_ip'] = app('request')->ip();
  580. $data['last_time'] = time();
  581. $data['last_ip'] = app('request')->ip();
  582. $data['nickname'] = substr(md5($account . time()), 0, 12);
  583. $data['avatar'] = $data['headimgurl'] = sys_config('h5_avatar');
  584. $data['city'] = '';
  585. $data['language'] = '';
  586. $data['province'] = '';
  587. $data['country'] = '';
  588. $data['is_promoter'] = $storeBrokerageStatu != 1 ? 1 : 0;
  589. self::beginTrans();
  590. $res2 = WechatUser::create($data);
  591. $data['uid'] = $res2->uid;
  592. $res1 = self::create($data);
  593. $res = $res1 && $res2 && UserSpread::setSpread($data['uid'], $spread);
  594. self::checkTrans($res);
  595. return $res;
  596. }
  597. /**
  598. * 密码修改
  599. * @param $account
  600. * @param $password
  601. * @return User|bool
  602. */
  603. public static function reset($account, $password)
  604. {
  605. if (!self::be(['account' => $account])) return self::setErrorInfo('用户不存在');
  606. $count = self::where('account', $account)->where('pwd', md5($password))->count();
  607. if ($count) return true;
  608. return self::where('account', $account)->update(['pwd' => md5($password)]);
  609. }
  610. /**
  611. * 获取手机号是否注册
  612. * @param $phone
  613. * @return bool
  614. */
  615. public static function checkPhone($phone)
  616. {
  617. return self::be(['account' => $phone]);
  618. }
  619. /**
  620. * 获取推广人
  621. * @param $data 查询条件
  622. * @return array
  623. * @throws DataNotFoundException
  624. * @throws ModelNotFoundException
  625. * @throws \think\exception\DbException
  626. */
  627. public static function getRankList($data)
  628. {
  629. switch ($data['type']) {
  630. case 'week':
  631. $startTime = strtotime('this week');
  632. $endTime = time();
  633. break;
  634. case 'month':
  635. $startTime = strtotime('last month');
  636. $endTime = time();
  637. break;
  638. }
  639. $list = self::alias('t0')
  640. ->field('t0.uid,t0.spread_uid,count(t1.spread_uid) AS count,t0.add_time,t0.nickname,t0.avatar')
  641. ->join('user t1', 't0.uid = t1.spread_uid', 'LEFT')
  642. ->where('t1.spread_uid', '<>', 0)
  643. ->order('count desc')
  644. ->order('t0.uid desc')
  645. ->where('t1.add_time', 'BETWEEN', [$startTime, $endTime])
  646. ->page($data['page'], $data['limit'])
  647. ->group('t0.uid')
  648. ->select();
  649. return count($list) ? $list->toArray() : [];
  650. }
  651. /**
  652. * 获取佣金排行
  653. * @param $data
  654. * @return array
  655. */
  656. public static function brokerageRank($data)
  657. {
  658. $model = self::where('status', 1);
  659. switch ($data['type']) {
  660. case 'week':
  661. $model = $model->whereIn('uid', function ($query) {
  662. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  663. ->whereWeek('add_time')->field('uid');
  664. });
  665. break;
  666. case 'month':
  667. $model = $model->whereIn('uid', function ($query) {
  668. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  669. ->whereMonth('add_time')->field('uid');
  670. });
  671. break;
  672. }
  673. $users = $model->field('uid,nickname,avatar,brokerage_price')->order('brokerage_price desc')
  674. ->page((int)$data['page'], (int)$data['limit'])->select();
  675. return count($users) ? $users->toArray() : [];
  676. }
  677. /**
  678. * 获取当前用户的佣金排行位置
  679. * @param $uid
  680. * @return int
  681. */
  682. public static function currentUserRank($type, $brokerage_price)
  683. {
  684. $model = self::where('status', 1);
  685. switch ($type) {
  686. case 'week':
  687. $model = $model->whereIn('uid', function ($query) {
  688. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  689. ->whereWeek('add_time')->field('uid');
  690. });
  691. break;
  692. case 'month':
  693. $model = $model->whereIn('uid', function ($query) {
  694. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  695. ->whereMonth('add_time')->field('uid');
  696. });
  697. break;
  698. }
  699. return $model->where('brokerage_price', '>', $brokerage_price)->count('uid');
  700. }
  701. /**
  702. * 优惠卡失效
  703. * @return void
  704. * @throws DataNotFoundException
  705. * @throws DbException
  706. * @throws ModelNotFoundException
  707. */
  708. public static function userCard()
  709. {
  710. $user_card = UserCard::where('status', 1)->where('use', 1)->select();
  711. if (count($user_card) > 0){
  712. foreach ($user_card as $item)
  713. {
  714. if ($item['end_time'] < time()){
  715. UserCard::where('id',$item['id'])->update(['use' => 0]);
  716. UserEquity::where('user_card_id', $item['id'])->update(['use' => 0]);
  717. }else{
  718. $equity = UserEquity::where('user_card_id', $item['id'])->where('use', 1)->select();
  719. if (count($equity) == 0){
  720. UserCard::where('id',$item['id'])->update(['use' => 0]);
  721. }
  722. }
  723. }
  724. }
  725. }
  726. /**
  727. * 订单过期
  728. * @param Request $request
  729. * @return mixed
  730. * @throws DataNotFoundException
  731. * @throws DbException
  732. * @throws ModelNotFoundException
  733. */
  734. public static function order_overdue()
  735. {
  736. $list = DiagnosisOrder::alias('a')
  737. ->field('a.*')
  738. ->leftJoin('diagnosis_order_attr b', 'a.id = b.oid')
  739. ->where('a.status', 0)
  740. ->where('a.paid', 1)
  741. ->where('b.time', '<', time())
  742. ->select();
  743. if ($list){
  744. foreach ($list as $item)
  745. {
  746. DiagnosisOrder::where('id', $item['id'])->update(['status' => -2]);
  747. }
  748. }
  749. return app('json')->successful($list);
  750. }
  751. }