| protected $noNeedRight = ['weigh']; |
| |
| |
| * 通用排序 |
| */ |
| public function weigh() |
| { |
| |
| $ids = $this->request->post("ids"); |
| |
| $changeid = $this->request->post("changeid"); |
| |
| $field = $this->request->post("field"); |
| |
| $table = $this->request->post("table"); |
| if (!Validate::is($table, "alphaDash")) { |
| $this->error(); |
| } |
| |
| $pk = $this->request->post("pk"); |
| |
| $orderway = strtolower($this->request->post("orderway", "")); |
| $orderway = $orderway == 'asc' ? 'ASC' : 'DESC'; |
| $sour = $weighdata = []; |
| $ids = explode(',', $ids); |
| $prikey = $pk && preg_match("/^[a-z0-9\-_]+$/i", $pk) ? $pk : (model($table)->getPk() ?: 'id'); |
| $pid = $this->request->post("pid", ""); |
| |
| $field = in_array($field, ['sort']) ? $field : 'weigh'; |
| |
| |
| if ($pid !== '') { |
| $hasids = []; |
| $list = model($table)->where($prikey, 'in', $ids)->where('pid', 'in', $pid)->field("{$prikey},pid")->select(); |
| foreach ($list as $k => $v) { |
| $hasids[] = $v[$prikey]; |
| } |
| $ids = array_values(array_intersect($ids, $hasids)); |
| } |
| |
| $list = model($table)->field("$prikey,$field")->where($prikey, 'in', $ids)->order($field, $orderway)->select(); |
| foreach ($list as $k => $v) { |
| $sour[] = $v[$prikey]; |
| $weighdata[$v[$prikey]] = $v[$field]; |
| } |
| $position = array_search($changeid, $ids); |
| $desc_id = isset($sour[$position]) ? $sour[$position] : end($sour); |
| $sour_id = $changeid; |
| $weighids = array(); |
| $temp = array_values(array_diff_assoc($ids, $sour)); |
| foreach ($temp as $m => $n) { |
| if ($n == $sour_id) { |
| $offset = $desc_id; |
| } else { |
| if ($sour_id == $temp[0]) { |
| $offset = isset($temp[$m + 1]) ? $temp[$m + 1] : $sour_id; |
| } else { |
| $offset = isset($temp[$m - 1]) ? $temp[$m - 1] : $sour_id; |
| } |
| } |
| if (!isset($weighdata[$offset])) { |
| continue; |
| } |
| $weighids[$n] = $weighdata[$offset]; |
| model($table)->where($prikey, $n)->update([$field => $weighdata[$offset]]); |
| } |
| |
| $this->success(); |
| } |