Center.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace app\data\controller\api\auth;
  3. use app\data\controller\api\Auth;
  4. use app\data\model\DataUser;
  5. use app\data\service\RebateService;
  6. use app\data\service\UserAdminService;
  7. use app\data\service\UserUpgradeService;
  8. use think\admin\Storage;
  9. use think\exception\HttpResponseException;
  10. /**
  11. * 用户资料管理
  12. * Class Center
  13. * @package app\data\controller\api\auth
  14. */
  15. class Center extends Auth
  16. {
  17. /**
  18. * 更新用户资料
  19. */
  20. public function set()
  21. {
  22. $data = $this->_vali([
  23. 'headimg.default' => '',
  24. 'username.default' => '',
  25. 'base_age.default' => '',
  26. 'base_sex.default' => '',
  27. 'base_height.default' => '',
  28. 'base_weight.default' => '',
  29. 'base_birthday.default' => '',
  30. ]);
  31. foreach ($data as $key => $vo) if ($vo === '') unset($data[$key]);
  32. if (empty($data)) $this->error('没有修改的数据!');
  33. if (DataUser::mk()->where(['id' => $this->uuid])->update($data) !== false) {
  34. $this->success('更新资料成功!', $this->getUser());
  35. } else {
  36. $this->error('更新资料失败!');
  37. }
  38. }
  39. /**
  40. * 获取用户资料
  41. */
  42. public function get()
  43. {
  44. $this->success('获取用户资料', $this->getUser());
  45. }
  46. /**
  47. * Base64 图片上传
  48. */
  49. public function image()
  50. {
  51. try {
  52. $data = $this->_vali(['base64.require' => '图片内容不为空!']);
  53. if (preg_match('|^data:image/(.*?);base64,|i', $data['base64'])) {
  54. [$ext, $img] = explode('|||', preg_replace('|^data:image/(.*?);base64,|i', '$1|||', $data['base64']));
  55. $info = Storage::instance()->set(Storage::name($img, $ext ?: 'png', 'image/'), base64_decode($img));
  56. $this->success('图片上传成功!', ['url' => $info['url']]);
  57. } else {
  58. $this->error('解析内容失败!');
  59. }
  60. } catch (HttpResponseException $exception) {
  61. throw $exception;
  62. } catch (\Exception $exception) {
  63. $this->error($exception->getMessage());
  64. }
  65. }
  66. /**
  67. * 二进制文件上传
  68. * @throws \think\admin\Exception
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function upload()
  74. {
  75. $file = $this->request->file('file');
  76. if (empty($file)) $this->error('文件上传异常!');
  77. $extension = strtolower($file->getOriginalExtension());
  78. if (in_array($extension, ['php', 'sh'])) $this->error('禁止上传此类文件!');
  79. $bina = file_get_contents($file->getRealPath());
  80. $name = Storage::name($file->getPathname(), $extension, '', 'md5_file');
  81. $info = Storage::instance()->set($name, $bina, false, $file->getOriginalName());
  82. if (is_array($info) && isset($info['url'])) {
  83. $this->success('文件上传成功!', $info);
  84. } else {
  85. $this->error('文件上传失败!');
  86. }
  87. }
  88. /**
  89. * 获取用户等级
  90. */
  91. public function levels()
  92. {
  93. $levels = UserUpgradeService::instance()->levels();
  94. foreach ($levels as &$level) {
  95. $level['prizes'] = [];
  96. foreach (str2arr($level['rebate_rule']) as $code) {
  97. $level['prizes'][$code] = RebateService::instance()->name($code);
  98. }
  99. }
  100. $this->success('获取用户等级', array_values($levels));
  101. }
  102. /**
  103. * 获取我邀请的朋友
  104. * @throws \think\db\exception\DataNotFoundException
  105. * @throws \think\db\exception\DbException
  106. * @throws \think\db\exception\ModelNotFoundException
  107. */
  108. public function getFrom()
  109. {
  110. $map = [];
  111. $map[] = ['deleted', '=', 0];
  112. $map[] = ['path', 'like', "%-{$this->uuid}-%"];
  113. // 查询邀请的朋友
  114. $query = DataUser::mQuery();
  115. $query->like('nickname|username#nickname')->equal('vip_code,pids,pid1,id#uuid');
  116. $query->field('id,pid0,pid1,pid2,pids,username,nickname,headimg,order_amount_total,teams_amount_total,teams_amount_direct,teams_amount_indirect,teams_users_total,teams_users_direct,teams_users_indirect,rebate_total,rebate_used,rebate_lock,create_at');
  117. $result = $query->where($map)->order('id desc')->page(true, false, false, 15);
  118. // 统计当前用户所有下属数
  119. $total = DataUser::mk()->where($map)->count();
  120. // 统计当前用户本月下属数
  121. $map[] = ['create_at', 'like', date('Y-m-%')];
  122. $month = DataUser::mk()->where($map)->count();
  123. // 返回结果列表数据及统计
  124. $result['total'] = ['user_total' => $total, 'user_month' => $month];
  125. $this->success('获取我邀请的朋友', $result);
  126. }
  127. /**
  128. * 绑定用户邀请人
  129. * @throws \think\db\exception\DataNotFoundException
  130. * @throws \think\db\exception\DbException
  131. * @throws \think\db\exception\ModelNotFoundException
  132. */
  133. public function bindFrom()
  134. {
  135. $data = $this->_vali(['from.require' => '邀请人不能为空']);
  136. [$state, $message] = UserUpgradeService::instance()->bindAgent($this->uuid, $data['from'], 0);
  137. if ($state) {
  138. $this->success($message, UserAdminService::instance()->total($this->uuid));
  139. } else {
  140. $this->error($message, UserAdminService::instance()->total($this->uuid));
  141. }
  142. }
  143. }