123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- namespace app\data\controller\api\auth;
- use app\data\controller\api\Auth;
- use app\data\service\UserService;
- use think\admin\Storage;
- use think\exception\HttpResponseException;
- /**
- * 用户资料管理
- * Class Center
- * @package app\data\controller\api\auth
- */
- class Center extends Auth
- {
- /**
- * 绑定数据表
- * @var string
- */
- private $table = 'DataUser';
- /**
- * 更新用户资料
- * @throws \think\db\exception\DbException
- */
- public function set()
- {
- $data = $this->_vali([
- 'headimg.default' => '',
- 'username.default' => '',
- 'base_age.default' => '',
- 'base_sex.default' => '',
- 'base_height.default' => '',
- 'base_weight.default' => '',
- 'base_birthday.default' => '',
- ]);
- foreach ($data as $key => $vo) {
- if ($vo === '') unset($data[$key]);
- }
- if (empty($data)) $this->error('没有修改的数据!');
- if ($this->app->db->name($this->table)->where(['id' => $this->uuid])->update($data) !== false) {
- $this->success('更新资料成功!', $this->getUser());
- } else {
- $this->error('更新资料失败!');
- }
- }
- /**
- * 获取用户资料
- */
- public function get()
- {
- $this->success('获取用户资料', $this->getUser());
- }
- /**
- * Base64 图片上传
- */
- public function image()
- {
- try {
- $data = $this->_vali(['base64.require' => '图片内容不为空!']);
- if (preg_match('|^data:image/(.*?);base64,|i', $data['base64'])) {
- [$ext, $img] = explode('|||', preg_replace('|^data:image/(.*?);base64,|i', '$1|||', $data['base64']));
- $info = Storage::instance()->set(Storage::name($img, $ext ?: 'png', 'image/'), base64_decode($img));
- $this->success('图片上传成功!', ['url' => $info['url']]);
- } else {
- $this->error('解析内容失败!');
- }
- } catch (HttpResponseException $exception) {
- throw $exception;
- } catch (\Exception $exception) {
- $this->error($exception->getMessage());
- }
- }
- /**
- * 二进制文件上传
- * @throws \think\admin\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function upload()
- {
- $file = $this->request->file('file');
- if (empty($file)) $this->error('文件上传异常!');
- $extension = strtolower($file->getOriginalExtension());
- if (in_array($extension, ['php', 'sh'])) $this->error('禁止上传此类文件!');
- $bina = file_get_contents($file->getRealPath());
- $name = Storage::name($file->getPathname(), $extension, '', 'md5_file');
- $info = Storage::instance()->set($name, $bina, false, $file->getOriginalName());
- if (is_array($info) && isset($info['url'])) {
- $this->success('文件上传成功!', $info);
- } else {
- $this->error('文件上传失败!');
- }
- }
- /**
- * 获取我邀请的朋友
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getFrom()
- {
- $where = [];
- $where[] = ['deleted', '=', '0'];
- $where[] = ['path', 'like', "%-{$this->uuid}-%"];
- // 查询邀请的朋友
- $query = $this->_query($this->table);
- $query->like('nickname|username#nickname')->equal('from,id#uid');
- $query->field('id,from,username,nickname,headimg,amount_total,create_at');
- $result = $query->where($where)->order('id desc')->page(true, false, false, 15);
- // 统计当前用户所有下属数
- $userTotal = $this->app->db->name($this->table)->where($where)->count();
- // 统计当前用户本月下属数
- $where[] = ['create_at', 'like', date('Y-m-%')];
- $userMonth = $this->app->db->name($this->table)->where($where)->count();
- // 返回结果列表数据及统计
- $result['total'] = ['user_total' => $userTotal, 'user_month' => $userMonth];
- $this->success('获取我邀请的朋友', $result);
- }
- /**
- * 绑定用户邀请人
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function bindFrom()
- {
- $data = $this->_vali(['from.require' => '邀请人不能为空']);
- if ($data['from'] == $this->uuid) {
- $this->error('邀请人不能是自己', UserService::instance()->total($this->uuid));
- }
- $from = $this->app->db->name($this->table)->where(['id' => $data['from']])->find();
- if (empty($from)) $this->error('邀请人状态异常', UserService::instance()->get($this->type, $this->uuid));
- if ($this->user['from'] > 0) $this->error('已绑定了邀请人', UserService::instance()->total($this->uuid));
- if (is_numeric(stripos($from['path'], "-{$this->uuid}-"))) $this->error('不能绑定下属');
- $data['path'] = rtrim($from['path'] ?: '-', '-') . '-' . $from['id'] . '-';
- $data['layer'] = substr_count($data['path'], '-');
- if ($this->app->db->name($this->table)->where(['id' => $this->uuid])->update($data) !== false) {
- $this->success('绑定邀请人成功', UserService::instance()->total($this->uuid));
- } else {
- $this->error('绑定邀请人失败', UserService::instance()->total($this->uuid));
- }
- }
- }
|