User.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/21
  6. */
  7. namespace app\ebapi\model\user;
  8. use app\core\model\user\UserBill;
  9. use app\ebapi\model\store\StoreOrder;
  10. use basic\ModelBasic;
  11. use app\core\util\SystemConfigService;
  12. use think\Request;
  13. use think\Session;
  14. use traits\ModelTrait;
  15. /**
  16. * 用户
  17. * Class User
  18. * @package app\ebapi\model\user
  19. */
  20. class User extends ModelBasic
  21. {
  22. use ModelTrait;
  23. public static function updateWechatUser($wechatUser,$uid)
  24. {
  25. $userinfo=self::where('uid',$uid)->find();
  26. if($userinfo->spread_uid){
  27. return self::edit([
  28. 'nickname'=>$wechatUser['nickname']?:'',
  29. 'avatar'=>$wechatUser['headimgurl']?:'',
  30. ],$uid,'uid');
  31. }else {
  32. $data=[
  33. 'nickname' => $wechatUser['nickname'] ?: '',
  34. 'avatar' => $wechatUser['headimgurl'] ?: '',
  35. 'is_promoter' =>$userinfo->is_promoter,
  36. 'spread_uid' => 0,
  37. 'spread_time' =>0,
  38. 'last_time' => time(),
  39. 'last_ip' => Request::instance()->ip(),
  40. ];
  41. if(isset($wechatUser['code']) && !$userinfo->is_promoter && $wechatUser['code']){
  42. $data['is_promoter']=1;
  43. $data['spread_uid']=$wechatUser['code'];
  44. $data['spread_time']=time();
  45. }
  46. return self::edit($data, $uid, 'uid');
  47. }
  48. }
  49. /**
  50. * 小程序用户添加
  51. * @param $routineUser
  52. * @param int $spread_uid
  53. * @return object
  54. */
  55. public static function setRoutineUser($routineUser,$spread_uid = 0){
  56. self::beginTrans();
  57. $res1 = true;
  58. if($spread_uid) $res1 = self::where('uid',$spread_uid)->inc('spread_count',1);
  59. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ? : 1;//获取后台分销类型
  60. $res2 = self::set([
  61. 'account'=>'rt'.$routineUser['uid'].time(),
  62. 'pwd'=>md5(123456),
  63. 'nickname'=>$routineUser['nickname']?:'',
  64. 'avatar'=>$routineUser['headimgurl']?:'',
  65. 'spread_uid'=>$spread_uid,
  66. 'is_promoter'=>$spread_uid || $storeBrokerageStatu != 1 ? 1: 0,
  67. 'spread_time'=>$spread_uid ? time() : 0,
  68. 'uid'=>$routineUser['uid'],
  69. 'add_time'=>$routineUser['add_time'],
  70. 'add_ip'=>Request::instance()->ip(),
  71. 'last_time'=>time(),
  72. 'last_ip'=>Request::instance()->ip(),
  73. 'user_type'=>$routineUser['user_type']
  74. ]);
  75. $res = $res1 && $res2;
  76. self::checkTrans($res);
  77. return $res2;
  78. }
  79. /**
  80. * 获得当前登陆用户UID
  81. * @return int $uid
  82. */
  83. public static function getActiveUid()
  84. {
  85. $uid = null;
  86. $uid = Session::get('LoginUid');
  87. if($uid) return $uid;
  88. else return 0;
  89. }
  90. /**
  91. * TODO 查询当前用户信息
  92. * @param $uid $uid 用户编号
  93. * @param string $field $field 查询的字段
  94. * @return array
  95. * @throws \think\Exception
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. * @throws \think\exception\DbException
  99. */
  100. public static function getUserInfo($uid,$field = '')
  101. {
  102. if(strlen(trim($field))) $userInfo = self::where('uid',$uid)->field($field)->find();
  103. else $userInfo = self::where('uid',$uid)->find();
  104. if(!$userInfo) return false;
  105. return $userInfo->toArray();
  106. }
  107. /**
  108. * 判断当前用户是否推广员
  109. * @param int $uid
  110. * @return bool
  111. */
  112. public static function isUserSpread($uid = 0){
  113. if(!$uid) return false;
  114. $status = (int)SystemConfigService::get('store_brokerage_statu');
  115. $isPromoter = true;
  116. if($status == 1) $isPromoter = self::where('uid',$uid)->value('is_promoter');
  117. if($isPromoter) return true;
  118. else return false;
  119. }
  120. /**
  121. * 小程序用户一级分销
  122. * @param $orderInfo
  123. * @return bool
  124. */
  125. public static function backOrderBrokerage($orderInfo)
  126. {
  127. $userInfo = User::getUserInfo($orderInfo['uid']);
  128. if(!$userInfo || !$userInfo['spread_uid']) return true;
  129. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ? : 1;//获取后台分销类型
  130. if($storeBrokerageStatu == 1){
  131. if(!User::be(['uid'=>$userInfo['spread_uid'],'is_promoter'=>1])) return true;
  132. }
  133. $brokerageRatio = (SystemConfigService::get('store_brokerage_ratio') ?: 0)/100;
  134. if($brokerageRatio <= 0) return true;
  135. $cost = isset($orderInfo['cost']) ? $orderInfo['cost'] : 0;//成本价
  136. if($cost > $orderInfo['pay_price']) return true;//成本价大于支付价格时直接返回
  137. //支付金额减去邮费
  138. $orderInfo['pay_price'] = bcsub($orderInfo['pay_price'],$orderInfo['pay_postage'],2);
  139. $brokeragePrice = bcmul(bcsub($orderInfo['pay_price'],$cost,2),$brokerageRatio,2);
  140. if($brokeragePrice <= 0) return true;
  141. $mark = $userInfo['nickname'].'成功消费'.floatval($orderInfo['pay_price']).'元,奖励推广佣金'.floatval($brokeragePrice);
  142. self::beginTrans();
  143. $res1 = UserBill::income('获得推广佣金',$userInfo['spread_uid'],'now_money','brokerage',$brokeragePrice,$orderInfo['id'],0,$mark);
  144. $res2 = self::bcInc($userInfo['spread_uid'],'now_money',$brokeragePrice,'uid');
  145. $res = $res1 && $res2;
  146. self::checkTrans($res);
  147. if($res) self::backOrderBrokerageTwo($orderInfo);
  148. return $res;
  149. }
  150. /**
  151. * 小程序 二级推广
  152. * @param $orderInfo
  153. * @return bool
  154. */
  155. public static function backOrderBrokerageTwo($orderInfo){
  156. $userInfo = User::getUserInfo($orderInfo['uid']);
  157. $userInfoTwo = User::getUserInfo($userInfo['spread_uid']);
  158. if(!$userInfoTwo || !$userInfoTwo['spread_uid']) return true;
  159. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ? : 1;//获取后台分销类型
  160. if($storeBrokerageStatu == 1){
  161. if(!User::be(['uid'=>$userInfoTwo['spread_uid'],'is_promoter'=>1])) return true;
  162. }
  163. $brokerageRatio = (SystemConfigService::get('store_brokerage_two') ?: 0)/100;
  164. if($brokerageRatio <= 0) return true;
  165. $cost = isset($orderInfo['cost']) ? $orderInfo['cost'] : 0;//成本价
  166. if($cost > $orderInfo['pay_price']) return true;//成本价大于支付价格时直接返回
  167. $brokeragePrice = bcmul(bcsub($orderInfo['pay_price'],$cost,2),$brokerageRatio,2);
  168. if($brokeragePrice <= 0) return true;
  169. $mark = '二级推广人'.$userInfo['nickname'].'成功消费'.floatval($orderInfo['pay_price']).'元,奖励推广佣金'.floatval($brokeragePrice);
  170. self::beginTrans();
  171. $res1 = UserBill::income('获得推广佣金',$userInfoTwo['spread_uid'],'now_money','brokerage',$brokeragePrice,$orderInfo['id'],0,$mark);
  172. $res2 = self::bcInc($userInfoTwo['spread_uid'],'now_money',$brokeragePrice,'uid');
  173. $res = $res1 && $res2;
  174. self::checkTrans($res);
  175. return $res;
  176. }
  177. /*
  178. * 获取推荐人
  179. * @param int $two_uid
  180. * @param int $first
  181. * @param int $limit
  182. * @return array
  183. * */
  184. public static function getSpreadList($uid,$first,$limit)
  185. {
  186. $list=self::where('spread_uid',$uid)->field('uid,nickname,avatar,add_time')->limit($first,$limit)->order('add_time DESC')->select();
  187. foreach ($list as $k=>$user){
  188. $list[$k]['add_time'] = date('Y/m/d',$user['add_time']);
  189. $list[$k]['price'] = StoreOrder::getUserPrice($user['uid']);
  190. }
  191. $count = self::where('spread_uid',$uid)->field('uid,nickname,avatar,add_time')->count();
  192. $data['count'] = $count;
  193. $data['list'] = $list;
  194. return $data;
  195. }
  196. /*
  197. * 获取某个用户的下级uid
  198. * @param int $uid 用户uid
  199. * @return array
  200. * */
  201. public static function getOneSpreadUid($uid)
  202. {
  203. return self::where('spread_uid',$uid)->column('uid');
  204. }
  205. /*
  206. * 修改个人信息
  207. * */
  208. public static function editUser($avatar,$nickname,$uid)
  209. {
  210. return self::edit(['avatar'=>$avatar,'nickname'=>$nickname],$uid,'uid');
  211. }
  212. /**
  213. * TODO 获取推广人数 一级
  214. * @param int $uid
  215. * @return bool|int|string
  216. */
  217. public static function getSpreadCount($uid = 0){
  218. if(!$uid) return false;
  219. return self::where('spread_uid',$uid)->count();
  220. }
  221. public static function setUserSpreadCount($uid){
  222. self::where('uid',$uid)->update(['spread_count'=>self::getSpreadCount($uid)]);
  223. }
  224. /**
  225. * TODO 获取推广人数 二级
  226. * @param int $uid
  227. * @return bool|int|string
  228. */
  229. public static function getSpreadLevelCount($uid = 0){
  230. if(!$uid) return false;
  231. $uidSubordinate = self::where('spread_uid',$uid)->column('uid');
  232. if(!count($uidSubordinate)) return 0;
  233. return self::where('spread_uid','IN',implode(',',$uidSubordinate))->count();
  234. }
  235. /**
  236. * 获取用户下级推广人
  237. * @param int $uid 当前用户
  238. * @param int $grade 等级 0 一级 1 二级
  239. * @param string $orderBy 排序
  240. * @return array|bool|void
  241. */
  242. public static function getUserSpreadGrade($uid = 0,$grade = 0,$orderBy = '',$keyword = '',$offset = 0,$limit = 20){
  243. if(!$uid) return [];
  244. $gradeGroup = [0,1];
  245. if(!in_array($grade,$gradeGroup)) return self::setErrorInfo('等级错误');
  246. $userStair = self::where('spread_uid',$uid)->column('uid');
  247. if(!count($userStair)) return [];
  248. if($grade == 0) return self::getUserSpreadCountList(implode(',',$userStair),$orderBy,$keyword,$offset,$limit);
  249. $userSecondary = self::where('spread_uid','in',implode(',',$userStair))->column('uid');
  250. return self::getUserSpreadCountList(implode(',',$userSecondary),$orderBy,$keyword,$offset,$limit);
  251. }
  252. /**
  253. * 获取团队信息
  254. * @param string $uid
  255. * @param string $orderBy
  256. * @param string $keyword
  257. */
  258. public static function getUserSpreadCountList($uid, $orderBy = '',$keyword = '',$offset = 0,$limit = 20)
  259. {
  260. $model = new self;
  261. if($orderBy==='') $orderBy='u.add_time desc';
  262. $model = $model->alias(' u');
  263. $model = $model->join('StoreOrder o','u.uid=o.uid','LEFT');
  264. $model = $model->where('u.uid','IN',$uid);
  265. $model = $model->field("u.uid,u.nickname,u.avatar,from_unixtime(u.add_time,'%Y/%m/%d') as time,u.spread_count as childCount,COUNT(o.id) as orderCount,SUM(o.pay_price) as numberCount");
  266. if(strlen(trim($keyword))) $model = $model->where('u.nickname|u.phone','like',"%$keyword%");
  267. $model = $model->group('u.uid');
  268. $model = $model->order($orderBy);
  269. $model = $model->limit($offset,$limit);
  270. $list = $model->select();
  271. if($list) return $list->toArray();
  272. else return [];
  273. }
  274. }