Admin.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // | L
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 友情链接管理
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller\links;
  15. use app\admin\model\general\Lang as LangModel;
  16. use app\admin\model\links\Links as LinksModel;
  17. use app\common\controller\Backend;
  18. use think\facade\Db;
  19. /**
  20. * 友情链接管理
  21. */
  22. class Admin extends Backend
  23. {
  24. protected $modelValidate = true;
  25. protected function syncLangData($id, array $row, $lang)
  26. {
  27. $defaultLang = LangModel::getDefaultMark();
  28. if ($id > 0) {
  29. $data = [
  30. 'did' => $id,
  31. 'lang' => $lang,
  32. 'name' => $row['name'] ?? '',
  33. 'description' => $row['description'] ?? '',
  34. 'update_time' => time(),
  35. ];
  36. $exists = Db::name('links_data')->where(['did' => $id, 'lang' => $lang])->find();
  37. if ($exists) {
  38. Db::name('links_data')->where(['did' => $id, 'lang' => $lang])->update($data);
  39. } else {
  40. $data['create_time'] = time();
  41. Db::name('links_data')->insert($data);
  42. }
  43. }
  44. if ($lang !== $defaultLang) {
  45. unset($row['name'], $row['description']);
  46. }
  47. return $row;
  48. }
  49. protected function initialize()
  50. {
  51. parent::initialize();
  52. $this->modelClass = new LinksModel;
  53. $this->assign("statusList", $this->modelClass->getStatusList());
  54. }
  55. /**
  56. * 新增友情链接
  57. */
  58. public function add()
  59. {
  60. if ($this->request->isPost()) {
  61. $row = $this->request->post('row/a', [], 'trim');
  62. if ($row) {
  63. $lang = $row['lang'] ?? LangModel::getDefaultMark();
  64. unset($row['lang']);
  65. $result = false;
  66. Db::startTrans();
  67. try {
  68. if ($this->modelValidate) {
  69. $name = str_replace("\\model\\", "\\validate\\", get_class($this->modelClass));
  70. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  71. $this->validate($row, $validate);
  72. }
  73. $saveRow = $this->syncLangData(0, $row, $lang);
  74. $result = $this->modelClass->save($saveRow);
  75. if ($result === false) {
  76. throw new \Exception('未插入任何行');
  77. }
  78. $this->syncLangData($this->modelClass->id, $row, $lang);
  79. Db::commit();
  80. } catch (\Exception $e) {
  81. Db::rollback();
  82. $this->error($e->getMessage());
  83. }
  84. if ($result !== false) {
  85. $this->success('新增成功');
  86. }
  87. $this->error('未插入任何行');
  88. }
  89. $this->error('参数不能为空');
  90. }
  91. $Terms = Db::name('Terms')->where("module", "links")->select();
  92. $this->assign("Terms", $Terms);
  93. $this->assign('langs', LangModel::getEnabledList());
  94. $this->assign('currentLang', LangModel::getDefaultMark());
  95. $this->assign('defaultLang', LangModel::getDefaultMark());
  96. return $this->fetch();
  97. }
  98. /**
  99. * 编辑友情链接
  100. */
  101. public function edit()
  102. {
  103. $id = $this->request->param('id/d', 0);
  104. $lang = $this->request->param('lang/s', LangModel::getDefaultMark());
  105. $defaultLang = LangModel::getDefaultMark();
  106. if ($this->request->isPost()) {
  107. $row = $this->request->post('row/a', [], 'trim');
  108. if ($row) {
  109. $lang = $row['lang'] ?? $defaultLang;
  110. unset($row['lang']);
  111. $result = false;
  112. Db::startTrans();
  113. try {
  114. if ($this->modelValidate) {
  115. $name = str_replace("\\model\\", "\\validate\\", get_class($this->modelClass));
  116. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  117. $this->validate($row, $validate);
  118. }
  119. $saveRow = $this->syncLangData($id, $row, $lang);
  120. $result = $this->modelClass->update($saveRow);
  121. if ($result === false) {
  122. throw new \Exception('未更新任何行');
  123. }
  124. Db::commit();
  125. } catch (\Exception $e) {
  126. Db::rollback();
  127. $this->error($e->getMessage());
  128. }
  129. if ($result !== false) {
  130. $this->success('修改成功');
  131. }
  132. $this->error('未更新任何行');
  133. }
  134. $this->error('参数不能为空');
  135. }
  136. $data = $this->modelClass->find($id);
  137. if (!$data) {
  138. $this->error('记录不存在');
  139. }
  140. $data = $data->toArray();
  141. if ($lang !== $defaultLang) {
  142. $translate = Db::name('links_data')->where(['did' => $id, 'lang' => $lang])->find();
  143. if (!$translate) {
  144. $translate = Db::name('links_data')->where(['did' => $id, 'lang' => $defaultLang])->find();
  145. }
  146. if ($translate) {
  147. $data['name'] = $translate['name'] ?: ($data['name'] ?? '');
  148. $data['description'] = $translate['description'] ?: ($data['description'] ?? '');
  149. }
  150. }
  151. $Terms = Db::name('Terms')->where("module", "links")->select();
  152. $langStatus = [];
  153. foreach (LangModel::getEnabledList() as $item) {
  154. $langStatus[$item->mark] = Db::name('links_data')->where(['did' => $id, 'lang' => $item->mark])->count() > 0 ? 1 : 0;
  155. }
  156. $this->assign("Terms", $Terms);
  157. $this->assign('data', $data);
  158. $this->assign('langs', LangModel::getEnabledList());
  159. $this->assign('currentLang', $lang);
  160. $this->assign('defaultLang', $defaultLang);
  161. $this->assign('langStatus', $langStatus);
  162. return $this->fetch();
  163. }
  164. public function del()
  165. {
  166. $ids = $this->request->param('ids', '', 'trim');
  167. if (!$ids) {
  168. $this->error('参数不能为空');
  169. }
  170. $idsArr = array_filter(explode(',', $ids));
  171. if (empty($idsArr)) {
  172. $this->error('参数不能为空');
  173. }
  174. Db::startTrans();
  175. try {
  176. $list = $this->modelClass->where('id', 'in', $idsArr)->select();
  177. foreach ($list as $row) {
  178. Db::name('links_data')->where('did', $row->id)->delete();
  179. $row->delete();
  180. }
  181. Db::commit();
  182. } catch (\Exception $e) {
  183. Db::rollback();
  184. $this->error($e->getMessage());
  185. }
  186. $this->success('删除成功');
  187. }
  188. public function index()
  189. {
  190. if ($this->request->isAjax()) {
  191. list($page, $limit, $where) = $this->buildTableParames();
  192. $list = $this->modelClass->where($where)->order('listorder desc,id desc')->page($page, $limit)->select();
  193. $total = $this->modelClass->where($where)->count();
  194. $currentLang = $this->request->param('lang/s', LangModel::getDefaultMark());
  195. $_list = [];
  196. foreach ($list as $item) {
  197. $row = $item->toArray();
  198. $statusMap = [];
  199. foreach (LangModel::getEnabledList() as $langItem) {
  200. $statusMap[$langItem->mark] = Db::name('links_data')->where(['did' => $row['id'], 'lang' => $langItem->mark])->count() > 0 ? 1 : 0;
  201. }
  202. $row['lang_status'] = $statusMap;
  203. if ($currentLang !== LangModel::getDefaultMark()) {
  204. $translate = Db::name('links_data')->where(['did' => $row['id'], 'lang' => $currentLang])->find();
  205. if (!$translate) {
  206. $translate = Db::name('links_data')->where(['did' => $row['id'], 'lang' => LangModel::getDefaultMark()])->find();
  207. }
  208. if ($translate) {
  209. $row['name'] = $translate['name'] ?: $row['name'];
  210. $row['description'] = $translate['description'] ?: $row['description'];
  211. }
  212. }
  213. $_list[] = $row;
  214. }
  215. return json(['code' => 0, 'count' => $total, 'data' => $_list]);
  216. }
  217. $langOptions = [];
  218. foreach (LangModel::getEnabledList() as $item) {
  219. $langOptions[] = [
  220. 'mark' => $item->mark,
  221. 'name' => $item->name,
  222. 'code' => $item->code,
  223. ];
  224. }
  225. $this->assignconfig('langs', $langOptions);
  226. return $this->fetch();
  227. }
  228. }