Center.php 6.1 KB

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