Center.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace app\data\controller\api\auth;
  3. use app\data\controller\api\Auth;
  4. use app\data\service\UserService;
  5. use think\admin\Storage;
  6. use think\exception\HttpResponseException;
  7. /**
  8. * 用户资料管理
  9. * Class Center
  10. * @package app\data\controller\api\auth
  11. */
  12. class Center extends Auth
  13. {
  14. /**
  15. * 绑定数据表
  16. * @var string
  17. */
  18. private $table = 'DataUser';
  19. /**
  20. * 更新用户资料
  21. * @throws \think\db\exception\DbException
  22. */
  23. public function set()
  24. {
  25. $data = $this->_vali([
  26. 'headimg.default' => '',
  27. 'username.default' => '',
  28. 'base_age.default' => '',
  29. 'base_sex.default' => '',
  30. 'base_height.default' => '',
  31. 'base_weight.default' => '',
  32. 'base_birthday.default' => '',
  33. ]);
  34. foreach ($data as $key => $vo) {
  35. if ($vo === '') unset($data[$key]);
  36. }
  37. if (empty($data)) $this->error('没有修改的数据!');
  38. if ($this->app->db->name($this->table)->where(['id' => $this->uuid])->update($data) !== false) {
  39. $this->success('更新资料成功!', $this->getUser());
  40. } else {
  41. $this->error('更新资料失败!');
  42. }
  43. }
  44. /**
  45. * 获取用户资料
  46. */
  47. public function get()
  48. {
  49. $this->success('获取用户资料', $this->getUser());
  50. }
  51. /**
  52. * Base64 图片上传
  53. */
  54. public function image()
  55. {
  56. try {
  57. $data = $this->_vali(['base64.require' => '图片内容不为空!']);
  58. if (preg_match('|^data:image/(.*?);base64,|i', $data['base64'])) {
  59. [$ext, $img] = explode('|||', preg_replace('|^data:image/(.*?);base64,|i', '$1|||', $data['base64']));
  60. $info = Storage::instance()->set(Storage::name($img, $ext ?: 'png', 'image/'), base64_decode($img));
  61. $this->success('图片上传成功!', ['url' => $info['url']]);
  62. } else {
  63. $this->error('解析内容失败!');
  64. }
  65. } catch (HttpResponseException $exception) {
  66. throw $exception;
  67. } catch (\Exception $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. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. */
  99. public function getFrom()
  100. {
  101. $where = [];
  102. $where[] = ['deleted', '=', '0'];
  103. $where[] = ['path', 'like', "%-{$this->uuid}-%"];
  104. // 查询邀请的朋友
  105. $query = $this->_query($this->table);
  106. $query->like('nickname|username#nickname')->equal('from,id#uid');
  107. $query->field('id,from,username,nickname,headimg,amount_total,create_at');
  108. $result = $query->where($where)->order('id desc')->page(true, false, false, 15);
  109. // 统计当前用户所有下属数
  110. $userTotal = $this->app->db->name($this->table)->where($where)->count();
  111. // 统计当前用户本月下属数
  112. $where[] = ['create_at', 'like', date('Y-m-%')];
  113. $userMonth = $this->app->db->name($this->table)->where($where)->count();
  114. // 返回结果列表数据及统计
  115. $result['total'] = ['user_total' => $userTotal, 'user_month' => $userMonth];
  116. $this->success('获取我邀请的朋友', $result);
  117. }
  118. /**
  119. * 绑定用户邀请人
  120. * @throws \think\Exception
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. */
  125. public function bindFrom()
  126. {
  127. $data = $this->_vali(['from.require' => '邀请人不能为空']);
  128. if ($data['from'] == $this->uuid) {
  129. $this->error('邀请人不能是自己', UserService::instance()->total($this->uuid));
  130. }
  131. $from = $this->app->db->name($this->table)->where(['id' => $data['from']])->find();
  132. if (empty($from)) $this->error('邀请人状态异常', UserService::instance()->get($this->type, $this->uuid));
  133. if ($this->user['from'] > 0) $this->error('已绑定了邀请人', UserService::instance()->total($this->uuid));
  134. if (is_numeric(stripos($from['path'], "-{$this->uuid}-"))) $this->error('不能绑定下属');
  135. $data['path'] = rtrim($from['path'] ?: '-', '-') . '-' . $from['id'] . '-';
  136. $data['layer'] = substr_count($data['path'], '-');
  137. if ($this->app->db->name($this->table)->where(['id' => $this->uuid])->update($data) !== false) {
  138. $this->success('绑定邀请人成功', UserService::instance()->total($this->uuid));
  139. } else {
  140. $this->error('绑定邀请人失败', UserService::instance()->total($this->uuid));
  141. }
  142. }
  143. }