Ajax_阿里云.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\exception\UploadException;
  5. use app\common\library\Upload;
  6. use fast\Random;
  7. use think\addons\Service;
  8. use think\Cache;
  9. use think\Config;
  10. use think\Db;
  11. use think\Lang;
  12. use think\Validate;
  13. use AliCloud\OSS\OssClient;
  14. use AliCloud\OSS\Core\OssException;
  15. /**
  16. * Ajax异步请求接口
  17. * @internal
  18. */
  19. class Ajax extends Backend
  20. {
  21. protected $noNeedLogin = ['lang'];
  22. protected $noNeedRight = ['*'];
  23. protected $layout = '';
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. //设置过滤方法
  28. $this->request->filter(['trim', 'strip_tags', 'htmlspecialchars']);
  29. }
  30. /**
  31. * 加载语言包
  32. */
  33. public function lang()
  34. {
  35. header('Content-Type: application/javascript');
  36. header("Cache-Control: public");
  37. header("Pragma: cache");
  38. $offset = 30 * 60 * 60 * 24; // 缓存一个月
  39. header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
  40. $controllername = input("controllername");
  41. //默认只加载了控制器对应的语言名,你还根据控制器名来加载额外的语言包
  42. $this->loadlang($controllername);
  43. return jsonp(Lang::get(), 200, [], ['json_encode_param' => JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE]);
  44. }
  45. /**
  46. * 上传文件
  47. */
  48. public function upload()
  49. {
  50. Config::set('default_return_type', 'json');
  51. //必须设定cdnurl为空,否则cdnurl函数计算错误
  52. Config::set('upload.cdnurl', '');
  53. $chunkid = $this->request->post("chunkid");
  54. if ($chunkid) {
  55. if (!Config::get('upload.chunking')) {
  56. $this->error(__('Chunk file disabled'));
  57. }
  58. $action = $this->request->post("action");
  59. $chunkindex = $this->request->post("chunkindex/d");
  60. $chunkcount = $this->request->post("chunkcount/d");
  61. $filename = $this->request->post("filename");
  62. $method = $this->request->method(true);
  63. if ($action == 'merge') {
  64. $attachment = null;
  65. //合并分片文件
  66. try {
  67. $upload = new Upload();
  68. $attachment = $upload->merge($chunkid, $chunkcount, $filename);
  69. } catch (UploadException $e) {
  70. $this->error($e->getMessage());
  71. }
  72. $this->success(__('Uploaded successful'), '', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  73. } elseif ($method == 'clean') {
  74. //删除冗余的分片文件
  75. try {
  76. $upload = new Upload();
  77. $upload->clean($chunkid);
  78. } catch (UploadException $e) {
  79. $this->error($e->getMessage());
  80. }
  81. $this->success();
  82. } else {
  83. //上传分片文件
  84. //默认普通上传文件
  85. $file = $this->request->file('file');
  86. try {
  87. $upload = new Upload($file);
  88. $upload->chunk($chunkid, $chunkindex, $chunkcount);
  89. } catch (UploadException $e) {
  90. $this->error($e->getMessage());
  91. }
  92. $this->success();
  93. }
  94. } else {
  95. $attachment = null;
  96. //默认普通上传文件
  97. $file = $this->request->file('file');
  98. $c = $file->getInfo();
  99. $name = $c['tmp_name'];
  100. $res = $this->storage_save($name );
  101. if($res['info']['http_code'] == 200){
  102. $attachment = $res['info']['url'];
  103. $this->success(__('Uploaded successful'), '', ['url' => $attachment , 'fullurl' => $attachment ]);
  104. }else{
  105. $this->error("上传失败");
  106. }
  107. // try {
  108. // $upload = new Upload($file);
  109. // $attachment = $upload->upload();
  110. // } catch (UploadException $e) {
  111. // $this->error($e->getMessage());
  112. // }
  113. // $this->success(__('Uploaded successful'), '', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
  114. }
  115. }
  116. /**
  117. *$srcPath就是你要上传文件的地址
  118. *$newurl就是你上传到OSS的文件名,或路径+文件名
  119. **/
  120. function storage_save($srcPath,$newurl = "")
  121. {
  122. $newurl = md5(date("Y-m-d H:i:s")).".jpg";
  123. //配置
  124. $accessKeyId = 'LTAI5t8sFjgLJP5PF8cbo9JE';
  125. //你的阿里的accessid
  126. $accessKeySecret = '0VZtA83GMNtXSyvCxToaToXICILtF4';
  127. //你的阿里的accesskey
  128. $endpoint = 'oss-cn-shanghai.aliyuncs.com';
  129. // Endpoint以北京为例,其它Region请按实际情况填写。
  130. $bucket = 'bab668';
  131. // 存储空间名称
  132. //
  133. $object = $newurl;
  134. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);//引用
  135. $res = $ossClient->uploadFile($bucket, $object, $srcPath);//上传
  136. return $res;
  137. }
  138. /**
  139. * 通用排序
  140. */
  141. public function weigh()
  142. {
  143. //排序的数组
  144. $ids = $this->request->post("ids");
  145. //拖动的记录ID
  146. $changeid = $this->request->post("changeid");
  147. //操作字段
  148. $field = $this->request->post("field");
  149. //操作的数据表
  150. $table = $this->request->post("table");
  151. if (!Validate::is($table, "alphaDash")) {
  152. $this->error();
  153. }
  154. //主键
  155. $pk = $this->request->post("pk");
  156. //排序的方式
  157. $orderway = strtolower($this->request->post("orderway", ""));
  158. $orderway = $orderway == 'asc' ? 'ASC' : 'DESC';
  159. $sour = $weighdata = [];
  160. $ids = explode(',', $ids);
  161. $prikey = $pk && preg_match("/^[a-z0-9\-_]+$/i", $pk) ? $pk : (Db::name($table)->getPk() ?: 'id');
  162. $pid = $this->request->post("pid", "");
  163. //限制更新的字段
  164. $field = in_array($field, ['weigh']) ? $field : 'weigh';
  165. // 如果设定了pid的值,此时只匹配满足条件的ID,其它忽略
  166. if ($pid !== '') {
  167. $hasids = [];
  168. $list = Db::name($table)->where($prikey, 'in', $ids)->where('pid', 'in', $pid)->field("{$prikey},pid")->select();
  169. foreach ($list as $k => $v) {
  170. $hasids[] = $v[$prikey];
  171. }
  172. $ids = array_values(array_intersect($ids, $hasids));
  173. }
  174. $list = Db::name($table)->field("$prikey,$field")->where($prikey, 'in', $ids)->order($field, $orderway)->select();
  175. foreach ($list as $k => $v) {
  176. $sour[] = $v[$prikey];
  177. $weighdata[$v[$prikey]] = $v[$field];
  178. }
  179. $position = array_search($changeid, $ids);
  180. $desc_id = $sour[$position]; //移动到目标的ID值,取出所处改变前位置的值
  181. $sour_id = $changeid;
  182. $weighids = array();
  183. $temp = array_values(array_diff_assoc($ids, $sour));
  184. foreach ($temp as $m => $n) {
  185. if ($n == $sour_id) {
  186. $offset = $desc_id;
  187. } else {
  188. if ($sour_id == $temp[0]) {
  189. $offset = isset($temp[$m + 1]) ? $temp[$m + 1] : $sour_id;
  190. } else {
  191. $offset = isset($temp[$m - 1]) ? $temp[$m - 1] : $sour_id;
  192. }
  193. }
  194. if (!isset($weighdata[$offset])) {
  195. continue;
  196. }
  197. $weighids[$n] = $weighdata[$offset];
  198. Db::name($table)->where($prikey, $n)->update([$field => $weighdata[$offset]]);
  199. }
  200. $this->success();
  201. }
  202. /**
  203. * 清空系统缓存
  204. */
  205. public function wipecache()
  206. {
  207. try {
  208. $type = $this->request->request("type");
  209. switch ($type) {
  210. case 'all':
  211. // no break
  212. case 'content':
  213. //内容缓存
  214. rmdirs(CACHE_PATH, false);
  215. Cache::clear();
  216. if ($type == 'content') {
  217. break;
  218. }
  219. case 'template':
  220. // 模板缓存
  221. rmdirs(TEMP_PATH, false);
  222. if ($type == 'template') {
  223. break;
  224. }
  225. case 'addons':
  226. // 插件缓存
  227. Service::refresh();
  228. if ($type == 'addons') {
  229. break;
  230. }
  231. case 'browser':
  232. // 浏览器缓存
  233. // 只有生产环境下才修改
  234. if (!config('app_debug')) {
  235. $version = config('site.version');
  236. $newversion = preg_replace_callback("/(.*)\.([0-9]+)\$/", function ($match) {
  237. return $match[1] . '.' . ($match[2] + 1);
  238. }, $version);
  239. if ($newversion && $newversion != $version) {
  240. Db::startTrans();
  241. try {
  242. \app\common\model\Config::where('name', 'version')->update(['value' => $newversion]);
  243. \app\common\model\Config::refreshFile();
  244. Db::commit();
  245. } catch (\Exception $e) {
  246. Db::rollback();
  247. exception($e->getMessage());
  248. }
  249. }
  250. }
  251. if ($type == 'browser') {
  252. break;
  253. }
  254. }
  255. } catch (\Exception $e) {
  256. $this->error($e->getMessage());
  257. }
  258. \think\Hook::listen("wipecache_after");
  259. $this->success();
  260. }
  261. /**
  262. * 读取分类数据,联动列表
  263. */
  264. public function category()
  265. {
  266. $type = $this->request->get('type', '');
  267. $pid = $this->request->get('pid', '');
  268. $where = ['status' => 'normal'];
  269. $categorylist = null;
  270. if ($pid || $pid === '0') {
  271. $where['pid'] = $pid;
  272. }
  273. if ($type) {
  274. $where['type'] = $type;
  275. }
  276. $categorylist = Db::name('category')->where($where)->field('id as value,name')->order('weigh desc,id desc')->select();
  277. $this->success('', '', $categorylist);
  278. }
  279. /**
  280. * 读取省市区数据,联动列表
  281. */
  282. public function area()
  283. {
  284. $params = $this->request->get("row/a");
  285. if (!empty($params)) {
  286. $province = isset($params['province']) ? $params['province'] : '';
  287. $city = isset($params['city']) ? $params['city'] : '';
  288. } else {
  289. $province = $this->request->get('province', '');
  290. $city = $this->request->get('city', '');
  291. }
  292. $where = ['pid' => 0, 'level' => 1];
  293. $provincelist = null;
  294. if ($province !== '') {
  295. $where['pid'] = $province;
  296. $where['level'] = 2;
  297. if ($city !== '') {
  298. $where['pid'] = $city;
  299. $where['level'] = 3;
  300. }
  301. }
  302. $provincelist = Db::name('area')->where($where)->field('id as value,name')->select();
  303. $this->success('', '', $provincelist);
  304. }
  305. /**
  306. * 生成后缀图标
  307. */
  308. public function icon()
  309. {
  310. $suffix = $this->request->request("suffix");
  311. header('Content-type: image/svg+xml');
  312. $suffix = $suffix ? $suffix : "FILE";
  313. echo build_suffix_image($suffix);
  314. exit;
  315. }
  316. }