Usercenter.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 会员中心
  7. */
  8. class Usercenter extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = '*';
  12. //我的下级
  13. public function myintro(){
  14. $list = Db::name('user')->field('id,nickname,avatar,jointime')->where('intro_uid',$this->auth->id)->autopage()->order('id desc')->select();
  15. $list = list_domain_image($list,['avatar']);
  16. $this->success(1,$list);
  17. }
  18. //用户的业绩总额等
  19. public function my_intro_data(){
  20. //从lib/auth ->getUserinfo 拿过来
  21. $userinfo['score'] = model('wallet')->getWallet($this->auth->id,'score');//积分
  22. $userinfo['withdraw_score'] = Db::name('user_withdraw')->where('user_id',$this->auth->id)->where('status',1)->sum('score');//已兑换积分
  23. $userinfo['share_score'] = Db::name('user_score_log')->where('user_id',$this->auth->id)->where('log_type',3)->sum('change_value');//分享奖励
  24. $userinfo['intro_number'] = Db::name('user')->where('intro_uid',$this->auth->id)->count('id');//我的粉丝
  25. //我的销售总额
  26. $yeji = $this->jiesuan_yeji($this->auth->id);
  27. $userinfo['yeji'] = $yeji;
  28. //我自己的提成
  29. $rule = $this->jiesuan_daili_level($yeji);
  30. $my_agent_score = bcdiv(bcmul($yeji,$rule['bili'],0),100,0);
  31. //我的下级贡献上来的极差提成
  32. $my_down_all_jicha_score = 0; //所有下级极差总额
  33. $my_down_uids = Db::name('user')->where('intro_uid',$this->auth->id)->column('id'); //下级用户ids
  34. if(!empty($my_down_uids)){
  35. foreach($my_down_uids as $down_uid){
  36. $down_yeji = $this->jiesuan_yeji($down_uid);
  37. $down_rule = $this->jiesuan_daili_level($down_yeji);
  38. $jicha_bili = $rule['bili'] - $down_rule['bili'];
  39. $my_down_all_jicha_score = bcadd($my_down_all_jicha_score,bcmul($jicha_bili,$down_yeji));
  40. }
  41. }
  42. //我的提成+下级极差提成
  43. $userinfo['agent_score'] = bcadd($my_agent_score,$my_down_all_jicha_score);
  44. $this->success(1,$userinfo);
  45. }
  46. //生成我的视频海报
  47. //生成邀请码二维码图片
  48. public function inviteimage($introcode) {
  49. $params['text'] = config('h5_url') . '/#/pages/login/register?introcode=' . $introcode;
  50. $qrcode_service = \addons\qrcode\library\Service::qrcode($params);
  51. // $mimetype = 'image/png';
  52. // $response = Response::create()->header("Content-Type", $mimetype);
  53. // 直接显示二维码
  54. // header('Content-Type: ' . $qrcode_service->getContentType());
  55. // $response->content($qrcode_service->writeString());
  56. $qrcodePath = ROOT_PATH . 'public/uploads/qrcode/';
  57. if (!is_dir($qrcodePath)) {
  58. @mkdir($qrcodePath);
  59. }
  60. if (is_really_writable($qrcodePath)) {
  61. $filename = md5(implode('', $params)) . '.png';
  62. $filePath = $qrcodePath . $filename;
  63. $qrcode_service->writeFile($filePath);
  64. }
  65. return '/uploads/qrcode/' . $filename;
  66. }
  67. //生成视频分享海报
  68. public function shareposter() {
  69. $inviteimage = $this->inviteimage($this->auth->introcode);
  70. $data = [
  71. [
  72. "left"=> "100px",
  73. "top"=> "424px",
  74. "type"=> "nickname",
  75. "width"=> "166px",
  76. "height"=> "38px",
  77. "size"=> "11px",
  78. "color"=> "#333333",
  79. "content" => '邀请码:'.$this->auth->introcode,
  80. ],
  81. [
  82. "left"=> "90px",
  83. "top"=> "260px",
  84. "type"=> "img",
  85. "width"=> "140px",
  86. "height"=> "140px",
  87. "src"=> httpurllocal($inviteimage)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  88. ],
  89. ];
  90. $data = json_encode($data, 320);
  91. $poster = [
  92. 'id' => 1,
  93. 'title' => '测试',
  94. 'waittext' => '您的专属海报正在拼命生成中,请等待片刻...',
  95. 'bg_image' => '/assets/img/posteruserbg.png',
  96. 'data' => $data,
  97. 'status' => 'normal',
  98. 'weigh' => 0,
  99. 'createtime' => 1653993709,
  100. 'updatetime' => 1653994259,
  101. ];
  102. $image = new \addons\poster\library\Image();
  103. $imgurl = $image->createPosterImage_user($poster, $this->auth->getUser());
  104. if (!$imgurl) {
  105. $this->error('生成海报出错');
  106. }
  107. $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl;
  108. //echo '<html><body><img src="'.$imgurl.'"></body></html>';
  109. $this->success('', $imgurl);
  110. }
  111. /////////////////////////////
  112. //获取业绩
  113. private function jiesuan_yeji($user_id){
  114. //找到所有下级
  115. $commonuser = new \app\common\model\User();
  116. $user_ids = $commonuser->my_down_all($user_id,[$user_id]);
  117. if(empty($user_ids)){
  118. return 0;
  119. }
  120. $map = [
  121. 'status' => 1,
  122. 'have_paid' => ['gt',0],
  123. 'user_id' => ['IN',$user_ids],
  124. ];
  125. $yeji = Db::name('unishop_order')->where($map)->sum('order_price');
  126. //减去提现的
  127. $map = [
  128. 'status' => 1,
  129. 'user_id' => ['IN',$user_ids],
  130. ];
  131. $take_cash = Db::name('user_withdraw')->where($map)->sum('score');
  132. $yeji = $yeji - $take_cash;
  133. return $yeji;
  134. }
  135. //确认代理等级及规则
  136. private function jiesuan_daili_level($yeji){
  137. $data = Db::name('zongdai')->order('id asc')->select();
  138. $return = $data[0]; //默认第0个
  139. foreach($data as $key => $rule){
  140. if($yeji >= $rule['yeji']){
  141. $return = $rule;
  142. }
  143. }
  144. return $return;
  145. }
  146. }