Center.php 5.8 KB

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