BodyProfile.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\model\BodyProfile as BodyProfileModel;
  5. use app\common\model\BodyMeasurements;
  6. use app\common\model\BodyTypeConfig;
  7. use app\common\model\BodyTypeSelection;
  8. use app\common\model\BodyAiReport;
  9. use think\Db;
  10. use think\exception\ValidateException;
  11. use think\exception\PDOException;
  12. use think\exception\DbException;
  13. use Exception;
  14. use app\common\Service\BodyProfileService;
  15. /**
  16. * 身体档案管理
  17. *
  18. * @icon fa fa-user
  19. * @remark 管理用户身体档案信息,包括基础数据、测量记录、体型选择等
  20. */
  21. class BodyProfile extends Backend
  22. {
  23. /**
  24. * BodyProfile模型对象
  25. * @var \app\common\model\BodyProfile
  26. */
  27. protected $model = null;
  28. /**
  29. * 无需登录的方法,同时也就无需鉴权了
  30. * @var array
  31. */
  32. protected $noNeedLogin = [];
  33. /**
  34. * 无需鉴权的方法,但需要登录
  35. * @var array
  36. */
  37. protected $noNeedRight = [];
  38. /**
  39. * 快速搜索时执行查找的字段
  40. * @var string
  41. */
  42. protected $searchFields = 'profile_name,relation';
  43. /**
  44. * 关联查询
  45. * @var array
  46. */
  47. protected $relationSearch = true;
  48. public function _initialize()
  49. {
  50. parent::_initialize();
  51. $this->model = new BodyProfileModel;
  52. }
  53. /**
  54. * 默认生成的控制器所继承的父类中有index方法,在这里重写下
  55. */
  56. public function index()
  57. {
  58. //当前是否为关联查询
  59. $this->relationSearch = true;
  60. //设置过滤方法
  61. $this->request->filter(['strip_tags', 'trim']);
  62. if ($this->request->isAjax()) {
  63. //如果发送的来源是Selectpage,则转发到Selectpage
  64. if ($this->request->request('keyField')) {
  65. return $this->selectpage();
  66. }
  67. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  68. $list = $this->model
  69. ->with(['user'])
  70. ->where($where)
  71. ->order($sort, $order)
  72. ->paginate($limit);
  73. foreach ($list as $row) {
  74. $row->visible(['id','profile_name','user_id','gender','is_own','relation','age','height','weight','createtime','updatetime']);
  75. $row->visible(['user']);
  76. $row->getRelation('user')->visible(['username','nickname']);
  77. // 计算BMI
  78. $row['bmi'] = $row->calculateBMI();
  79. $row['bmi_level'] = $row->getBMILevel();
  80. }
  81. $result = array("total" => $list->total(), "rows" => $list->items());
  82. return json($result);
  83. }
  84. return $this->view->fetch();
  85. }
  86. /**
  87. * 查看详情
  88. */
  89. public function detail($ids = null)
  90. {
  91. $row = $this->model->get($ids);
  92. if (!$row) {
  93. $this->error(__("No Results were found"));
  94. }
  95. $adminIds = $this->getDataLimitAdminIds();
  96. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  97. $this->error(__("You have no permission"));
  98. }
  99. // 使用方法查询
  100. $profileData = BodyProfileService::getProfileDetail($row->id, $row->user_id);
  101. $this->view->assign("profileData", $profileData);
  102. $this->view->assign("row", $row);
  103. return $this->view->fetch();
  104. }
  105. }