Ajax.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\user\controller;
  3. use app\common\controller\Userend;
  4. use think\Lang;
  5. /**
  6. * Ajax异步请求接口
  7. * @internal
  8. */
  9. class Ajax extends Userend
  10. {
  11. protected $noNeedLogin = ['lang'];
  12. protected $noNeedRight = ['*'];
  13. protected $layout = '';
  14. /**
  15. * 加载语言包
  16. */
  17. public function lang()
  18. {
  19. header('Content-Type: application/javascript');
  20. $controllername = input("controllername");
  21. $this->loadlang($controllername);
  22. return jsonp(Lang::get(), 200, [], ['json_encode_param' => JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE]);
  23. }
  24. /**
  25. * 上传文件
  26. */
  27. public function upload()
  28. {
  29. return action('api/common/upload');
  30. }
  31. /**
  32. * 通用排序
  33. */
  34. public function weigh()
  35. {
  36. //排序的数组
  37. $ids = $this->request->post("ids");
  38. //拖动的记录ID
  39. $changeid = $this->request->post("changeid");
  40. //操作字段
  41. $field = $this->request->post("field");
  42. //操作的数据表
  43. $table = $this->request->post("table");
  44. //主键
  45. $pk = $this->request->post("pk");
  46. //排序的方式
  47. $orderway = $this->request->post("orderway", "", 'strtolower');
  48. $orderway = $orderway == 'asc' ? 'ASC' : 'DESC';
  49. $sour = $weighdata = [];
  50. $ids = explode(',', $ids);
  51. $prikey = $pk ? $pk : (Db::name($table)->getPk() ?: 'id');
  52. $pid = $this->request->post("pid");
  53. //限制更新的字段
  54. $field = in_array($field, ['weigh']) ? $field : 'weigh';
  55. // 如果设定了pid的值,此时只匹配满足条件的ID,其它忽略
  56. if ($pid !== '') {
  57. $hasids = [];
  58. $list = Db::name($table)->where($prikey, 'in', $ids)->where('pid', 'in', $pid)->field("{$prikey},pid")->select();
  59. foreach ($list as $k => $v) {
  60. $hasids[] = $v[$prikey];
  61. }
  62. $ids = array_values(array_intersect($ids, $hasids));
  63. }
  64. $list = Db::name($table)->field("$prikey,$field")->where($prikey, 'in', $ids)->order($field, $orderway)->select();
  65. foreach ($list as $k => $v) {
  66. $sour[] = $v[$prikey];
  67. $weighdata[$v[$prikey]] = $v[$field];
  68. }
  69. $position = array_search($changeid, $ids);
  70. $desc_id = $sour[$position]; //移动到目标的ID值,取出所处改变前位置的值
  71. $sour_id = $changeid;
  72. $weighids = array();
  73. $temp = array_values(array_diff_assoc($ids, $sour));
  74. foreach ($temp as $m => $n) {
  75. if ($n == $sour_id) {
  76. $offset = $desc_id;
  77. } else {
  78. if ($sour_id == $temp[0]) {
  79. $offset = isset($temp[$m + 1]) ? $temp[$m + 1] : $sour_id;
  80. } else {
  81. $offset = isset($temp[$m - 1]) ? $temp[$m - 1] : $sour_id;
  82. }
  83. }
  84. $weighids[$n] = $weighdata[$offset];
  85. Db::name($table)->where($prikey, $n)->update([$field => $weighdata[$offset]]);
  86. }
  87. $this->success();
  88. }
  89. }