User.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php
  2. namespace App\Modes;
  3. use Carbon\Carbon;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. use Illuminate\Notifications\Notifiable;
  6. use Illuminate\Foundation\Auth\User as Authenticatable;
  7. use SimpleSoftwareIO\QrCode\Facades\QrCode;
  8. use Spatie\Permission\Traits\HasRoles;
  9. use Tymon\JWTAuth\Contracts\JWTSubject;
  10. /**
  11. * App\Modes\User
  12. *
  13. * @property int $id
  14. * @property int|null $mobile 手机号码
  15. * @property string $nick_name 昵称
  16. * @property string $avatar 头像
  17. * @property string $password 密码
  18. * @property string $real_name 真实姓名
  19. * @property string $id_card 真实姓名
  20. * @property float $balance 余额
  21. * @property float $coin 点币
  22. * @property string $ssid session_id
  23. * @property int $province 省份(area.id)
  24. * @property int $city 市(area.id)
  25. * @property int $district 区(area.id)
  26. * @property int $invitor 邀请人id
  27. * @property string $invite_code 邀请码
  28. * @property int $level 等级:1-A 2-B 3-C 4-D 5-E 6-F 7-G 8-H
  29. * @property int $is_frontend 是否前端用户 1-是
  30. * @property int $status 状态 1-启用 2-禁用
  31. * @property int $full_status 是否完成资料填写 0-否 1-完成
  32. * @property int $is_super 是否超级管理员 0-否 1-是 2-普通管理员 3-代理管理员
  33. * @property int $shadow 影子用户 1-是
  34. * @property int $commission_status 是否修改分佣比例状态 1-未修改 2-修改
  35. * @property int $sadmin_uid 地区代理管理员id
  36. * @property \Illuminate\Support\Carbon $created_at
  37. * @property \Illuminate\Support\Carbon $updated_at
  38. * @property string|null $deleted_at 是否删除
  39. * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
  40. * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Permission[] $permissions
  41. * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Role[] $roles
  42. * @method static bool|null forceDelete()
  43. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User newModelQuery()
  44. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User newQuery()
  45. * @method static \Illuminate\Database\Query\Builder|\App\Modes\User onlyTrashed()
  46. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User permission($permissions)
  47. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User query()
  48. * @method static bool|null restore()
  49. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User role($roles, $guard = null)
  50. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereAvatar($value)
  51. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereSsid($value)
  52. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereBalance($value)
  53. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereCity($value)
  54. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereCoin($value)
  55. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereCommissionStatus($value)
  56. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereCreatedAt($value)
  57. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereDeletedAt($value)
  58. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereDistrict($value)
  59. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereFullStatus($value)
  60. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereId($value)
  61. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereIdCard($value)
  62. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereInviteCode($value)
  63. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereInvitor($value)
  64. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereIsFrontend($value)
  65. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereIsSuper($value)
  66. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereLevel($value)
  67. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereMobile($value)
  68. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereNickName($value)
  69. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User wherePassword($value)
  70. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereProvince($value)
  71. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereRealName($value)
  72. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereSadminUid($value)
  73. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereShadow($value)
  74. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereStatus($value)
  75. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereUpdatedAt($value)
  76. * @method static \Illuminate\Database\Query\Builder|\App\Modes\User withTrashed()
  77. * @method static \Illuminate\Database\Query\Builder|\App\Modes\User withoutTrashed()
  78. * @mixin \Eloquent
  79. * @property int $is_apply 是否可以申请成为区域代理 0-否 n-是(1-县区级 2-市级)
  80. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User whereIsApply($value)
  81. * @property string $pay_password 支付密码
  82. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\User wherePayPassword($value)
  83. */
  84. class User extends Authenticatable implements JWTSubject
  85. {
  86. use HasRoles, SoftDeletes, Notifiable;
  87. protected $table = 'user';
  88. protected $guarded = [
  89. ];
  90. protected $guard_name = 'backendGuard';
  91. protected $dates = ['created_at'];
  92. protected static $ids = [];
  93. protected $hidden = [
  94. // 'password'
  95. ];
  96. public function getJWTIdentifier()
  97. {
  98. return $this->getKey();
  99. }
  100. /**
  101. * 添加到JWT的任何自定义声明
  102. */
  103. public function getJWTCustomClaims()
  104. {
  105. return [];
  106. }
  107. /*生成支付二维码*/
  108. public static function getPayCode(string $code_url)
  109. {
  110. if(substr($code_url, 0, 6) == "weixin"){
  111. $im= QRcode::format('png')->generate($code_url);
  112. return $im;
  113. }
  114. return '';
  115. }
  116. // /**
  117. // * 获得此用户的角色。
  118. // */
  119. // public function Roles()
  120. // {
  121. // return $this->belongsToMany('App\Modes\SystemRoles', null, 'model_id', 'role_id');
  122. // }
  123. /**************************************静态方法******************************************/
  124. /**
  125. * 获取邀请码
  126. * @author lyh
  127. * @date 2019/3/19
  128. * @param $mobile
  129. * @return string
  130. * @description
  131. */
  132. private static function getInviteCode($mobile)
  133. {
  134. return md5($mobile);
  135. }
  136. /*
  137. * 代理费折合点币
  138. * author wsl
  139. * 20190709
  140. * $uid 用户id
  141. * $money 缴费金额
  142. * $type 升级1 升级区域代理4
  143. * */
  144. public static function conversionCoin($uid,$money,$type=1){
  145. $userinfo=User::whereId($uid)->first();
  146. $setting=Proxy::getSettingByAreaAndLevel($userinfo->level,$userinfo->province,$userinfo->city,$userinfo->district);
  147. if(!empty($setting)){
  148. \DB::beginTransaction();
  149. try {
  150. //添加点币到账户
  151. $coin=$money*$setting['coin']/100;
  152. User::whereId($uid)->increment('coin', $coin);
  153. //添加记录
  154. if($type==4){
  155. $remark='【id:'.$uid.'】花费'.$money.'元升级区域代理审核通过获得点币'.$coin.'个';
  156. }else{
  157. $remark='【id:'.$uid.'】花费'.$money.'升级审核通过获得点币'.$coin.'个';
  158. }
  159. $remark.=',折合率:'.$setting['coin'].'%';
  160. AccountLog::saveDataByPost($uid,45,$coin,$userinfo->coin,1,1,$remark);
  161. //消息通知
  162. $msgarr=['title'=>'点币到账通知','uid'=>$uid,'type'=>13,'content'=>'您的点币到账了哦'];
  163. UserMsg::insert($msgarr);
  164. \DB::commit();
  165. } catch (\Exception $exception) {
  166. \DB::rollBack();
  167. }
  168. }
  169. }
  170. /**
  171. * 获取二维码
  172. * @author lyh
  173. * @date 2019/3/19
  174. * @param string $invite_code
  175. * @return string
  176. * @description
  177. */
  178. public static function getInviteImage(string $invite_code)
  179. {
  180. $name = '/image/invite/' . $invite_code . '.png';
  181. if (is_numeric(QrCode::format('png')->size(400)->generate(env('APP_URL') . '/#/register?inviteCode=' . $invite_code, public_path($name)))) {
  182. return $name;
  183. }
  184. return '';
  185. }
  186. /**
  187. * 获取用户列表
  188. * @author lyh
  189. * @date 2019/3/19
  190. * @param $ids
  191. * @param array $array
  192. * @description
  193. */
  194. public static function getList($ids, array $array = ['*'])
  195. {
  196. $user = User::whereIn('id', $ids)->select($array)->paginate(perPage());
  197. return $user;
  198. }
  199. /**
  200. * 获取今天的用户量
  201. * @author lyh
  202. * @date 2019/4/3
  203. * @description
  204. */
  205. public static function getRegByInToday()
  206. {
  207. $start = Carbon::today();
  208. $end = Carbon::today()->subDay(-1)->subSecond(1);
  209. $user = User::whereBetween('created_at', [$start, $end])->whereNotNull('level');
  210. if (\Auth::user()->is_super == 3) {
  211. $user = $user->whereIn('id', getIds());
  212. }
  213. return $user->count('id') ?? 0;
  214. }
  215. public static function getIsApply(int $apply_level)
  216. {
  217. switch ($apply_level) {
  218. case 6:
  219. $isApply = 1;
  220. break;
  221. case 7:
  222. $isApply = 2;
  223. break;
  224. default;
  225. $isApply = 0;
  226. }
  227. return $isApply;
  228. }
  229. /**
  230. * 查找用户
  231. * @author fatty
  232. * @date 2018/12/13
  233. * @param $param
  234. * @return User|\Illuminate\Database\Eloquent\Model|int|null|object
  235. * @description
  236. */
  237. public static function findUser($param)
  238. {
  239. $user = User::where([
  240. 'account_no' => $param['account_no'],
  241. 'password' => bcrypt($param['password'])
  242. ])
  243. ->first();
  244. if (!$user) {
  245. return 10003;
  246. }
  247. return $user;
  248. }
  249. /**
  250. * 添加用户
  251. * @author lyh
  252. * @date 2019/3/19
  253. * @description
  254. */
  255. public static function addGetId($param)
  256. {
  257. $id = User::insertGetId([
  258. 'mobile' => $param['mobile'],
  259. 'invitor' => $param['invitor'],
  260. 'password' => self::encodePassword($param['password']),
  261. 'invite_code' => User::getInviteCode($param['mobile']),
  262. 'is_frontend' => 1
  263. ]);
  264. // todo 待定 分销佣金分润,升级 条件在升级条件数据返回显示
  265. //User::upgrade($param['invitor']);
  266. return $id;
  267. }
  268. /**
  269. * 加密密码
  270. * @author lyh
  271. * @date 2019/3/19
  272. * @param $password
  273. * @return string
  274. * @description
  275. */
  276. public static function encodePassword($password)
  277. {
  278. return bcrypt($password);
  279. }
  280. /**
  281. * 获取所有直推用户id
  282. * @author lyh
  283. * @date 2019/3/19
  284. * @description
  285. */
  286. public static function getInvitee($uid, $start_time = null, $end_time = null)
  287. {
  288. $user = User::whereInvitor($uid);
  289. if ($start_time != null && $end_time != null) {
  290. $user->whereBetween('created_at', [$start_time, $end_time]);
  291. } else if ($start_time != null && $end_time = null) {
  292. $user->where('created_at', '>', $start_time);
  293. } else if ($start_time == null && $end_time != null) {
  294. $user->where('created_at', '<', $end_time);
  295. }
  296. $ids = $user->pluck('id');
  297. if (empty($ids)) {
  298. return [];
  299. }
  300. return $ids;
  301. }
  302. /**
  303. * 获取所有间推一用户id
  304. * @author lyh
  305. * @date 2019/3/19
  306. * @description
  307. */
  308. public static function getInviteeOne($uid, $start_time = null, $end_time = null)
  309. {
  310. // 直推
  311. $invitee = self::getInvitee($uid, $start_time, $end_time);
  312. if (empty($invitee)) {
  313. return [];
  314. }
  315. $user = User::whereIn('invitor', $invitee);
  316. if ($start_time != null && $end_time != null) {
  317. $user->whereBetween('created_at', [$start_time, $end_time]);
  318. }
  319. $inviteeOne = $user->pluck('id');
  320. if (empty($inviteeOne)) {
  321. return [];
  322. }
  323. return $inviteeOne;
  324. }
  325. /**
  326. * 获取所有间推二用户id
  327. * @author lyh
  328. * @date 2019/3/19
  329. * @description
  330. */
  331. public static function getInviteeTwo($uid, $start_time = null, $end_time = null)
  332. {
  333. // 间推一
  334. $inviteeOne = self::getInviteeOne($uid, $start_time, $end_time);
  335. if (empty($inviteeOne)) {
  336. return [];
  337. }
  338. $user = User::whereIn('invitor', $inviteeOne);
  339. if ($start_time != null && $end_time != null) {
  340. $user->whereBetween('created_at', [$start_time, $end_time]);
  341. }
  342. $inviteeTwo = $user->pluck('id');
  343. if (empty($inviteeTwo)) {
  344. return [];
  345. }
  346. return $inviteeTwo;
  347. }
  348. /**
  349. * 获取三级以内所有的被邀请人id(包括自身)
  350. * @author lyh
  351. * @date 2019/3/25
  352. * @description
  353. */
  354. public static function getAllInvite($uid, $start_time = null, $end_time = null, $isMy = true)
  355. {
  356. $ids = [];
  357. if ($isMy == true) {
  358. $ids[] = $uid;
  359. }
  360. $invitee = self::getInvitee($uid, $start_time, $end_time);
  361. if (!empty($invitee)) {
  362. $ids = array_merge($ids, $invitee->toArray());
  363. $inviteOne = self::getInviteeOne($uid, $start_time, $end_time);
  364. if (!empty($inviteOne)) {
  365. $ids = array_merge($ids, $inviteOne->toArray());
  366. $inviteTwo = self::getInviteeTwo($uid, $start_time, $end_time);
  367. if (!empty($inviteTwo)) {
  368. $ids = array_merge($ids, $inviteTwo->toArray());
  369. }
  370. }
  371. }
  372. return $ids;
  373. }
  374. /**
  375. *根据条件获取下三级代理的id
  376. * author wsl
  377. * 20190628
  378. *
  379. *
  380. *
  381. */
  382. public static function getAllChilds($uid,$endtime=null){
  383. $user=User::whereInvitor($uid)->select('id','mobile','nick_name');
  384. if ($endtime != null) {
  385. $user->where('created_at', '<',$endtime);
  386. }
  387. $childsarr=$user->get();//直推
  388. $i=0;
  389. $arr=[];
  390. if(!empty($childsarr)){
  391. foreach($childsarr as $key=>$value){
  392. $arr[]=$value->id;
  393. $childsarr1=User::whereInvitor($value->id)->select('id','mobile','nick_name')->get();//间1
  394. if(!empty($childsarr1)){
  395. foreach($childsarr1 as $k1=>$v1){
  396. $arr[]=$v1->id;
  397. $childsarr2=User::whereInvitor($v1->id)->select('id','mobile','nick_name')->get();//间2
  398. if(!empty($childsarr2)){
  399. foreach($childsarr2 as $k2=>$v2){
  400. $arr[]=$v2->id;
  401. }
  402. }
  403. }
  404. }
  405. }
  406. }
  407. return $arr;
  408. }
  409. /**
  410. * 获取用户信息
  411. * @author lyh
  412. * @date 2019/3/19
  413. * @param int $invitor
  414. * @param array $array
  415. * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|null|object
  416. * @description
  417. */
  418. public static function getInfo(int $invitor, array $array = ['*'])
  419. {
  420. return User::whereId($invitor)->select($array)->first();
  421. }
  422. /**************************************模型关联******************************************/
  423. /**
  424. * [get_business_goal 统计用户的业务量:6直推投放广告交费+6自己投放广告交费+7自己所交代理费+32自己点币充值费]
  425. * @author lgs
  426. * @DateTime 2019-05-23T15:05:22+0800
  427. * @param [type] $uid [用户id]
  428. * @param string $start_date [开始日期]
  429. * @param string $end_date [结束日期]
  430. * @return [type] [description]
  431. */
  432. public static function get_business_goal($uid,$start_date='',$end_date=''){
  433. $rec_uids = self::getInvitee($uid);//所有直推用户id
  434. $rec_ad = AccountLog::whereIn('type', [6])->whereBetween('created_at', [$start_date, $end_date])->whereIn('uid', $rec_uids)->whereStatus(1)->sum('money');//直推的广告费业绩
  435. $self = AccountLog::whereIn('type', [6,7,32])->whereBetween('created_at', [$start_date, $end_date])->whereUid($uid)->whereStatus(1)->sum('money');//自己的业绩
  436. $total = abs($rec_ad + $self);//都是负数
  437. return $total;
  438. }
  439. }