Center.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\data\controller\api\auth;
  3. use app\data\controller\api\Auth;
  4. use app\data\model\BaseUserUpgrade;
  5. use app\data\model\DataUser;
  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($preg = '|^data:image/(.*?);base64,|i', $data['base64'])) {
  54. [$ext, $img] = explode('|||', preg_replace($preg, '$1|||', $data['base64']));
  55. if (empty($ext) || !in_array(strtolower($ext), ['png', 'jpg', 'jpeg'])) {
  56. $this->error('图片格式异常!');
  57. }
  58. $name = Storage::name($img, $ext, 'image/');
  59. $info = Storage::instance()->set($name, base64_decode($img));
  60. $this->success('图片上传成功!', ['url' => $info['url']]);
  61. } else {
  62. $this->error('解析内容失败!');
  63. }
  64. } catch (HttpResponseException $exception) {
  65. throw $exception;
  66. } catch (\Exception $exception) {
  67. trace_file($exception);
  68. $this->error($exception->getMessage());
  69. }
  70. }
  71. /**
  72. * 二进制文件上传
  73. * @throws \think\admin\Exception
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. */
  78. public function upload()
  79. {
  80. $file = $this->request->file('file');
  81. if (empty($file)) $this->error('文件上传异常!');
  82. $extension = strtolower($file->getOriginalExtension());
  83. if (in_array($extension, ['php', 'sh'])) $this->error('禁止上传此类文件!');
  84. $bina = file_get_contents($file->getRealPath());
  85. $name = Storage::name($file->getPathname(), $extension, '', 'md5_file');
  86. $info = Storage::instance()->set($name, $bina, false, $file->getOriginalName());
  87. if (is_array($info) && isset($info['url'])) {
  88. $this->success('文件上传成功!', $info);
  89. } else {
  90. $this->error('文件上传失败!');
  91. }
  92. }
  93. /**
  94. * 获取用户等级
  95. */
  96. public function levels()
  97. {
  98. $levels = BaseUserUpgrade::items();
  99. $this->success('获取用户等级', array_values($levels));
  100. }
  101. /**
  102. * 获取我邀请的朋友
  103. * @throws \think\db\exception\DataNotFoundException
  104. * @throws \think\db\exception\DbException
  105. * @throws \think\db\exception\ModelNotFoundException
  106. */
  107. public function getFrom()
  108. {
  109. $map = [];
  110. $map[] = ['deleted', '=', 0];
  111. $map[] = ['path', 'like', "%-{$this->uuid}-%"];
  112. // 查询邀请的朋友
  113. $query = DataUser::mQuery()->like('nickname|username#nickname')->equal('vip_code,pids,pid1,id#uuid');
  114. $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');
  115. $result = $query->where($map)->order('id desc')->page(true, false, false, 15);
  116. // 统计当前用户所有下属数
  117. $total = DataUser::mk()->where($map)->count();
  118. // 统计当前用户本月下属数
  119. $map[] = ['create_at', 'like', date('Y-m-%')];
  120. $month = DataUser::mk()->where($map)->count();
  121. // 返回结果列表数据及统计
  122. $result['total'] = ['user_total' => $total, 'user_month' => $month];
  123. $this->success('获取我邀请的朋友', $result);
  124. }
  125. /**
  126. * 绑定用户邀请人
  127. * @throws \think\db\exception\DataNotFoundException
  128. * @throws \think\db\exception\DbException
  129. * @throws \think\db\exception\ModelNotFoundException
  130. */
  131. public function bindFrom()
  132. {
  133. $data = $this->_vali(['from.require' => '邀请人不能为空']);
  134. [$state, $message] = UserUpgradeService::bindAgent($this->uuid, $data['from'], 0);
  135. if ($state) {
  136. $this->success($message, UserAdminService::total($this->uuid));
  137. } else {
  138. $this->error($message, UserAdminService::total($this->uuid));
  139. }
  140. }
  141. }