Icon.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\controller;
  10. use app\common\builder\ZBuilder;
  11. use app\admin\model\Icon as IconModel;
  12. use app\admin\model\IconList as IconListModel;
  13. /**
  14. * 图标控制器
  15. * @package app\admin\controller
  16. */
  17. class Icon extends Admin
  18. {
  19. /**
  20. * 图标列表
  21. * @author 蔡伟明 <314013107@qq.com>
  22. * @return mixed
  23. * @throws \think\Exception
  24. * @throws \think\exception\DbException
  25. */
  26. public function index()
  27. {
  28. $data_list = IconModel::where($this->getMap())
  29. ->order($this->getOrder('id DESC'))
  30. ->paginate();
  31. return ZBuilder::make('table')
  32. ->addTopButtons('add,enable,disable,delete')
  33. ->addRightButton('list', [
  34. 'title' => '图标列表',
  35. 'icon' => 'fa fa-list',
  36. 'href' => url('items', ['id' => '__id__'])
  37. ])
  38. ->addRightButton('reload', [
  39. 'title' => '更新图标',
  40. 'icon' => 'fa fa-refresh',
  41. 'class' => 'btn btn-xs btn-default ajax-get confirm',
  42. 'href' => url('reload', ['id' => '__id__'])
  43. ])
  44. ->addRightButton('delete')
  45. ->setSearch('name')
  46. ->addColumns([
  47. ['id', 'ID'],
  48. ['name', '名称', 'text.edit'],
  49. ['url', '链接', 'text.edit'],
  50. ['status', '状态', 'switch'],
  51. ['create_time', '创建时间', 'datetime'],
  52. ['right_button', '操作', 'btn'],
  53. ])
  54. ->setRowList($data_list)
  55. ->fetch();
  56. }
  57. /**
  58. * 新增
  59. * @author 蔡伟明 <314013107@qq.com>
  60. * @return mixed
  61. * @throws \think\Exception
  62. * @throws \think\exception\PDOException
  63. */
  64. public function add()
  65. {
  66. if ($this->request->isPost()) {
  67. $data = $this->request->post('', null, 'trim');
  68. $data['name'] == '' && $this->error('请填写名称');
  69. $data['url'] == '' && $this->error('请填写链接');
  70. $data['create_time'] = $this->request->time();
  71. $data['update_time'] = $this->request->time();
  72. // 获取图标信息
  73. $url = substr($data['url'], 0, 4) == 'http' ? $data['url'] : 'http:'.$data['url'];
  74. $content = file_get_contents($url);
  75. // 获取字体名
  76. $font_family = '';
  77. $pattern = '/font-family: "(.*)";/';
  78. if (preg_match($pattern, $content, $match)) {
  79. $font_family = $match[1];
  80. } else {
  81. $this->error('无法获取字体名');
  82. }
  83. $IconModel = new IconModel();
  84. if ($id = $IconModel->insertGetId($data)) {
  85. // 拉取图标列表
  86. $pattern = '/\.(.*):before/';
  87. if (preg_match_all($pattern, $content, $matches)) {
  88. $icon_list = [];
  89. foreach ($matches[1] as $match) {
  90. $icon_list[] = [
  91. 'icon_id' => $id,
  92. 'title' => $match,
  93. 'class' => $font_family . ' ' . $match,
  94. 'code' => $match,
  95. ];
  96. }
  97. $IconListModel = new IconListModel();
  98. if ($IconListModel->saveAll($icon_list)) {
  99. $this->success('新增成功', 'index');
  100. } else {
  101. $IconModel->where('id', $id)->delete();
  102. $this->error('图标添加失败');
  103. }
  104. }
  105. $this->success('新增成功', 'index');
  106. } else {
  107. $this->error('新增失败');
  108. }
  109. }
  110. return ZBuilder::make('form')
  111. ->addFormItems([
  112. ['text', 'name', '名称', '可填写中文'],
  113. ['text', 'url', '链接', '如://at.alicdn.com/t/font_588968_z5hsg7xluoh41jor.css'],
  114. ])
  115. ->fetch();
  116. }
  117. /**
  118. * 图标列表
  119. * @param string $id
  120. * @author 蔡伟明 <314013107@qq.com>
  121. * @return mixed
  122. * @throws \think\Exception
  123. * @throws \think\exception\DbException
  124. */
  125. public function items($id = '')
  126. {
  127. $data_list = IconListModel::where($this->getMap())
  128. ->order($this->getOrder('id DESC'))
  129. ->where('icon_id', $id)
  130. ->paginate();
  131. return ZBuilder::make('table')
  132. ->setTableName('admin_icon_list')
  133. ->addTopButtons('back')
  134. ->addTopButton('add', [
  135. 'title' => '更新图标',
  136. 'icon' => 'fa fa-refresh',
  137. 'class' => 'btn btn-primary ajax-get confirm',
  138. 'href' => url('reload', ['id' => $id])
  139. ])
  140. ->setSearch('title,code')
  141. ->addColumns([
  142. ['icon', '图标', 'callback', function($data){
  143. return '<i class="'.$data['class'].'"></i>';
  144. }, '__data__'],
  145. ['title', '图标标题', 'text.edit'],
  146. ['code', '图标关键词', 'text.edit'],
  147. ['class', '图标类名'],
  148. ])
  149. ->setRowList($data_list)
  150. ->fetch();
  151. }
  152. /**
  153. * 更新图标
  154. * @param string $id
  155. * @author 蔡伟明 <314013107@qq.com>
  156. * @throws \think\Exception
  157. * @throws \think\exception\PDOException
  158. */
  159. public function reload($id = '')
  160. {
  161. $icon = IconModel::get($id);
  162. // 获取图标信息
  163. $url = substr($icon['url'], 0, 4) == 'http' ? $icon['url'] : 'http:'.$icon['url'];
  164. $content = file_get_contents($url);
  165. // 获取字体名
  166. $font_family = '';
  167. $pattern = '/font-family: "(.*)";/';
  168. if (preg_match($pattern, $content, $match)) {
  169. $font_family = $match[1];
  170. } else {
  171. $this->error('无法获取字体名');
  172. }
  173. // 拉取图标列表
  174. $pattern = '/\.(.*):before/';
  175. if (preg_match_all($pattern, $content, $matches)) {
  176. $icon_list = [];
  177. foreach ($matches[1] as $match) {
  178. $icon_list[] = [
  179. 'icon_id' => $id,
  180. 'title' => $match,
  181. 'class' => $font_family . ' ' . $match,
  182. 'code' => $match,
  183. ];
  184. }
  185. $IconListModel = new IconListModel();
  186. $IconListModel->where('icon_id', $id)->delete();
  187. if ($IconListModel->saveAll($icon_list)) {
  188. $this->success('更新成功');
  189. } else {
  190. $this->error('图标添加失败');
  191. }
  192. }
  193. $this->success('更新成功');
  194. }
  195. /**
  196. * 删除图标库
  197. * @param string $ids
  198. * @throws \think\Exception
  199. * @throws \think\exception\PDOException
  200. * @author 蔡伟明 <314013107@qq.com>
  201. */
  202. public function delete($ids = '')
  203. {
  204. $ids == '' && $this->error('请选择要删除的数据');
  205. $ids = (array)$ids;
  206. // 删除图标列表
  207. if (false !== IconListModel::where('icon_id', 'in', $ids)->delete()) {
  208. // 删除图标库
  209. if (false !== IconModel::where('id', 'in', $ids)->delete()) {
  210. $this->success('删除成功');
  211. }
  212. }
  213. $this->error('删除失败');
  214. }
  215. }