Goods.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. use logicmodel\WalletLogic;
  6. use think\Model;
  7. /**
  8. *
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Goods extends Backend
  13. {
  14. /**
  15. * Goods模型对象
  16. * @var \app\admin\model\Goods
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\Goods;
  23. $this->view->assign("isShowList", $this->model->getIsShowList());
  24. $this->view->assign("isHomeList", $this->model->getIsHomeList());
  25. }
  26. public function import()
  27. {
  28. parent::import();
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. /**
  36. * 查看
  37. */
  38. public function index()
  39. {
  40. //当前是否为关联查询
  41. $this->relationSearch = true;
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags', 'trim']);
  44. if ($this->request->isAjax()) {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $list = $this->model
  51. ->with(['goodscategory','author','network'])
  52. ->where(['goods.is_del'=>0])
  53. ->where($where)
  54. ->order($sort, $order)
  55. ->paginate($limit);
  56. foreach ($list as $row) {
  57. $row->visible(['id','name','image','images','price','desc','attribute','contract','meta_data','order','stock','sales','surplus','start_time','end_time','content','is_show','label','is_home']);
  58. $row->visible(['goodscategory']);
  59. $row->getRelation('goodscategory')->visible(['name']);
  60. $row->visible(['author']);
  61. $row->getRelation('author')->visible(['name']);
  62. $row->visible(['network']);
  63. $row->getRelation('network')->visible(['name']);
  64. }
  65. $result = array("total" => $list->total(), "rows" => $list->items());
  66. return json($result);
  67. }
  68. return $this->view->fetch();
  69. }
  70. public function del($ids = "")
  71. {
  72. $result = $this->model->where(['id'=>['in',$ids]])->update(['is_del'=>1]);
  73. if($result) return json(['code'=>1,'msg'=>'删除成功']);
  74. return json(['code'=>1,'msg'=>'删除成功']);
  75. }
  76. protected function selectpage()
  77. {
  78. //设置过滤方法
  79. $this->request->filter(['trim', 'strip_tags', 'htmlspecialchars']);
  80. //搜索关键词,客户端输入以空格分开,这里接收为数组
  81. $word = (array)$this->request->request("q_word/a");
  82. //当前页
  83. $page = $this->request->request("pageNumber");
  84. //分页大小
  85. $pagesize = $this->request->request("pageSize");
  86. //搜索条件
  87. $andor = $this->request->request("andOr", "and", "strtoupper");
  88. //排序方式
  89. $orderby = (array)$this->request->request("orderBy/a");
  90. //显示的字段
  91. $field = $this->request->request("showField");
  92. //主键
  93. $primarykey = $this->request->request("keyField");
  94. //主键值
  95. $primaryvalue = $this->request->request("keyValue");
  96. //搜索字段
  97. $searchfield = (array)$this->request->request("searchField/a");
  98. //自定义搜索条件
  99. $custom = (array)$this->request->request("custom/a");
  100. //是否返回树形结构
  101. $istree = $this->request->request("isTree", 0);
  102. $ishtml = $this->request->request("isHtml", 0);
  103. if ($istree) {
  104. $word = [];
  105. $pagesize = 999999;
  106. }
  107. $order = [];
  108. foreach ($orderby as $k => $v) {
  109. $order[$v[0]] = $v[1];
  110. }
  111. $field = $field ? $field : 'name';
  112. //如果有primaryvalue,说明当前是初始化传值
  113. if ($primaryvalue !== null) {
  114. $where = [$primarykey => ['in', $primaryvalue]];
  115. $pagesize = 999999;
  116. } else {
  117. $where = function ($query) use ($word, $andor, $field, $searchfield, $custom) {
  118. $logic = $andor == 'AND' ? '&' : '|';
  119. $searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
  120. $searchfield = str_replace(',', $logic, $searchfield);
  121. $word = array_filter(array_unique($word));
  122. if (count($word) == 1) {
  123. $query->where($searchfield, "like", "%" . reset($word) . "%");
  124. } else {
  125. $query->where(function ($query) use ($word, $searchfield) {
  126. foreach ($word as $index => $item) {
  127. $query->whereOr(function ($query) use ($item, $searchfield) {
  128. $query->where($searchfield, "like", "%{$item}%");
  129. });
  130. }
  131. });
  132. }
  133. if ($custom && is_array($custom)) {
  134. foreach ($custom as $k => $v) {
  135. if (is_array($v) && 2 == count($v)) {
  136. $query->where($k, trim($v[0]), $v[1]);
  137. } else {
  138. $query->where($k, '=', $v);
  139. }
  140. }
  141. }
  142. };
  143. }
  144. $adminIds = $this->getDataLimitAdminIds();
  145. if (is_array($adminIds)) {
  146. $this->model->where($this->dataLimitField, 'in', $adminIds);
  147. }
  148. $list = [];
  149. $total = $this->model->where($where)->count();
  150. if ($total > 0) {
  151. if (is_array($adminIds)) {
  152. $this->model->where($this->dataLimitField, 'in', $adminIds);
  153. }
  154. $fields = is_array($this->selectpageFields) ? $this->selectpageFields : ($this->selectpageFields && $this->selectpageFields != '*' ? explode(',', $this->selectpageFields) : []);
  155. //如果有primaryvalue,说明当前是初始化传值,按照选择顺序排序
  156. if ($primaryvalue !== null && preg_match("/^[a-z0-9_\-]+$/i", $primarykey)) {
  157. $primaryvalue = array_unique(is_array($primaryvalue) ? $primaryvalue : explode(',', $primaryvalue));
  158. //修复自定义data-primary-key为字符串内容时,给排序字段添加上引号
  159. $primaryvalue = array_map(function ($value) {
  160. return '\'' . $value . '\'';
  161. }, $primaryvalue);
  162. $primaryvalue = implode(',', $primaryvalue);
  163. $this->model->orderRaw("FIELD(`{$primarykey}`, {$primaryvalue})");
  164. } else {
  165. $this->model->order($order);
  166. }
  167. $datalist = $this->model->where($where)
  168. ->where(['is_del'=>0])
  169. ->page($page, $pagesize)
  170. ->select();
  171. foreach ($datalist as $index => $item) {
  172. unset($item['password'], $item['salt']);
  173. if ($this->selectpageFields == '*') {
  174. $result = [
  175. $primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '',
  176. $field => isset($item[$field]) ? $item[$field] : '',
  177. ];
  178. } else {
  179. $result = array_intersect_key(($item instanceof Model ? $item->toArray() : (array)$item), array_flip($fields));
  180. }
  181. $result['pid'] = isset($item['pid']) ? $item['pid'] : (isset($item['parent_id']) ? $item['parent_id'] : 0);
  182. $list[] = $result;
  183. }
  184. if ($istree && !$primaryvalue) {
  185. $tree = Tree::instance();
  186. $tree->init(collection($list)->toArray(), 'pid');
  187. $list = $tree->getTreeList($tree->getTreeArray(0), $field);
  188. if (!$ishtml) {
  189. foreach ($list as &$item) {
  190. $item = str_replace('&nbsp;', ' ', $item);
  191. }
  192. unset($item);
  193. }
  194. }
  195. }
  196. //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
  197. return json(['list' => $list, 'total' => $total]);
  198. }
  199. public function add()
  200. {
  201. if(request()->isPost()){
  202. $data = input('post.');
  203. $data = $data['row'];
  204. $data['contract'] = (new WalletLogic())->newContract();
  205. $data['meta_data'] = rand(111111,999999);
  206. $result = $this->model->insertGetId($data);
  207. if($result) $this->success('添加成功');
  208. $this->error('添加失败');
  209. }
  210. return $this->fetch();
  211. }
  212. }