AiMeasureConfig.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace app\admin\controller\general;
  3. use app\common\controller\Backend;
  4. use app\common\model\Config as ConfigModel;
  5. use think\Cache;
  6. use think\Db;
  7. use think\Exception;
  8. use think\Validate;
  9. /**
  10. * AI测量素材配置
  11. *
  12. * @icon fa fa-camera
  13. * @remark AI测量功能的素材配置管理,包含自拍模式和帮拍模式的配置项
  14. */
  15. class AiMeasureConfig extends Backend
  16. {
  17. /**
  18. * @var \app\common\model\Config
  19. */
  20. protected $model = null;
  21. protected $noNeedRight = ['check', 'selectpage'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new ConfigModel;
  26. }
  27. /**
  28. * 查看
  29. */
  30. public function index()
  31. {
  32. // AI测量素材配置分组
  33. $aiMeasureGroups = [
  34. 'ai_measure_selfie' => 'AI测量自拍模式',
  35. 'ai_measure_helper' => 'AI测量帮拍模式',
  36. // 'ai_measure_common' => 'AI测量通用配置'
  37. ];
  38. $siteList = [];
  39. foreach ($aiMeasureGroups as $k => $v) {
  40. $siteList[$k]['name'] = $k;
  41. $siteList[$k]['title'] = $v;
  42. $siteList[$k]['list'] = [];
  43. }
  44. // 获取AI测量相关的配置项
  45. $configList = $this->model->where('group', 'in', array_keys($aiMeasureGroups))->select();
  46. foreach ($configList as $k => $v) {
  47. if (!isset($siteList[$v['group']])) {
  48. continue;
  49. }
  50. $value = $v->toArray();
  51. $value['title'] = __($value['title']);
  52. if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) {
  53. $value['value'] = explode(',', $value['value']);
  54. }
  55. $value['content'] = json_decode($value['content'], true);
  56. $value['tip'] = htmlspecialchars($value['tip']);
  57. $siteList[$v['group']]['list'][] = $value;
  58. }
  59. $index = 0;
  60. foreach ($siteList as $k => &$v) {
  61. $v['active'] = !$index ? true : false;
  62. $index++;
  63. }
  64. $this->view->assign('siteList', $siteList);
  65. $this->view->assign('typeList', ConfigModel::getTypeList());
  66. $this->view->assign('groupList', $aiMeasureGroups);
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 添加
  71. */
  72. public function add()
  73. {
  74. if (!config('app_debug')) {
  75. $this->error(__('Only work at development environment'));
  76. }
  77. if ($this->request->isPost()) {
  78. $this->token();
  79. $params = $this->request->post("row/a", [], 'trim');
  80. if ($params) {
  81. // 限制只能添加AI测量相关的配置组
  82. if (!in_array($params['group'], ['ai_measure_selfie', 'ai_measure_helper', 'ai_measure_common'])) {
  83. $this->error(__('Invalid group parameter'));
  84. }
  85. foreach ($params as $k => &$v) {
  86. $v = is_array($v) && $k !== 'setting' ? implode(',', $v) : $v;
  87. }
  88. if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {
  89. $params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
  90. } else {
  91. $params['content'] = '';
  92. }
  93. try {
  94. $result = $this->model->create($params);
  95. } catch (Exception $e) {
  96. $this->error($e->getMessage());
  97. }
  98. if ($result !== false) {
  99. try {
  100. ConfigModel::refreshFile();
  101. } catch (Exception $e) {
  102. $this->error($e->getMessage());
  103. }
  104. $this->success();
  105. } else {
  106. $this->error($this->model->getError());
  107. }
  108. }
  109. $this->error(__('Parameter %s can not be empty', ''));
  110. }
  111. return $this->view->fetch();
  112. }
  113. /**
  114. * 编辑
  115. * @param null $ids
  116. */
  117. public function edit($ids = null)
  118. {
  119. if ($this->request->isPost()) {
  120. $this->token();
  121. $row = $this->request->post("row/a", [], 'trim');
  122. if ($row) {
  123. $configList = [];
  124. // 只处理AI测量相关的配置项
  125. $aiMeasureConfigs = $this->model->where('group', 'in', ['ai_measure_selfie', 'ai_measure_helper', 'ai_measure_common'])->select();
  126. foreach ($aiMeasureConfigs as $v) {
  127. if (isset($row[$v['name']])) {
  128. $value = $row[$v['name']];
  129. if (is_array($value) && isset($value['field'])) {
  130. $value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
  131. } else {
  132. $value = is_array($value) ? implode(',', $value) : $value;
  133. }
  134. $v['value'] = $value;
  135. $configList[] = $v->toArray();
  136. }
  137. }
  138. try {
  139. $this->model->allowField(true)->saveAll($configList);
  140. } catch (Exception $e) {
  141. $this->error($e->getMessage());
  142. }
  143. try {
  144. ConfigModel::refreshFile();
  145. } catch (Exception $e) {
  146. $this->error($e->getMessage());
  147. }
  148. $this->success();
  149. }
  150. $this->error(__('Parameter %s can not be empty', ''));
  151. }
  152. }
  153. /**
  154. * 删除
  155. * @param string $ids
  156. */
  157. public function del($ids = "")
  158. {
  159. if (!config('app_debug')) {
  160. $this->error(__('Only work at development environment'));
  161. }
  162. $name = $this->request->post('name');
  163. $config = ConfigModel::getByName($name);
  164. if ($name && $config) {
  165. // 检查是否为AI测量配置项
  166. if (!in_array($config['group'], ['ai_measure_selfie', 'ai_measure_helper', 'ai_measure_common'])) {
  167. $this->error(__('Can only delete AI measurement configuration items'));
  168. }
  169. try {
  170. $config->delete();
  171. ConfigModel::refreshFile();
  172. } catch (Exception $e) {
  173. $this->error($e->getMessage());
  174. }
  175. $this->success();
  176. } else {
  177. $this->error(__('Invalid parameters'));
  178. }
  179. }
  180. /**
  181. * 检测配置项是否存在
  182. * @internal
  183. */
  184. public function check()
  185. {
  186. $params = $this->request->post("row/a");
  187. if ($params) {
  188. $config = $this->model->get($params);
  189. if (!$config) {
  190. $this->success();
  191. } else {
  192. $this->error(__('Name already exist'));
  193. }
  194. } else {
  195. $this->error(__('Invalid parameters'));
  196. }
  197. }
  198. public function selectpage()
  199. {
  200. $id = $this->request->get("id/d");
  201. $config = \app\common\model\Config::get($id);
  202. if (!$config) {
  203. $this->error(__('Invalid parameters'));
  204. }
  205. $setting = $config['setting'];
  206. //自定义条件
  207. $custom = isset($setting['conditions']) ? (array)json_decode($setting['conditions'], true) : [];
  208. $custom = array_filter($custom);
  209. $this->request->request(['showField' => $setting['field'], 'keyField' => $setting['primarykey'], 'custom' => $custom, 'searchField' => [$setting['field'], $setting['primarykey']]]);
  210. $this->model = \think\Db::connect()->setTable($setting['table']);
  211. return parent::selectpage();
  212. }
  213. }