123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace app\user\controller;
- use app\common\controller\Userend;
- use think\Lang;
- /**
- * Ajax异步请求接口
- * @internal
- */
- class Ajax extends Userend
- {
- protected $noNeedLogin = ['lang'];
- protected $noNeedRight = ['*'];
- protected $layout = '';
- /**
- * 加载语言包
- */
- public function lang()
- {
- header('Content-Type: application/javascript');
- $controllername = input("controllername");
- $this->loadlang($controllername);
- return jsonp(Lang::get(), 200, [], ['json_encode_param' => JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE]);
- }
- /**
- * 上传文件
- */
- public function upload()
- {
- return action('api/common/upload');
- }
- /**
- * 通用排序
- */
- public function weigh()
- {
- //排序的数组
- $ids = $this->request->post("ids");
- //拖动的记录ID
- $changeid = $this->request->post("changeid");
- //操作字段
- $field = $this->request->post("field");
- //操作的数据表
- $table = $this->request->post("table");
- //主键
- $pk = $this->request->post("pk");
- //排序的方式
- $orderway = $this->request->post("orderway", "", 'strtolower');
- $orderway = $orderway == 'asc' ? 'ASC' : 'DESC';
- $sour = $weighdata = [];
- $ids = explode(',', $ids);
- $prikey = $pk ? $pk : (Db::name($table)->getPk() ?: 'id');
- $pid = $this->request->post("pid");
- //限制更新的字段
- $field = in_array($field, ['weigh']) ? $field : 'weigh';
- // 如果设定了pid的值,此时只匹配满足条件的ID,其它忽略
- if ($pid !== '') {
- $hasids = [];
- $list = Db::name($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 = Db::name($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 = $sour[$position]; //移动到目标的ID值,取出所处改变前位置的值
- $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;
- }
- }
- $weighids[$n] = $weighdata[$offset];
- Db::name($table)->where($prikey, $n)->update([$field => $weighdata[$offset]]);
- }
- $this->success();
- }
- }
|