Center.php 5.7 KB

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