Ajax.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------------
  4. * 行到水穷处,坐看云起时
  5. * 开发软件,找贵阳云起信息科技,官网地址:https://www.56q7.com/
  6. * ----------------------------------------------------------------------------
  7. * Author: 老成
  8. * email:85556713@qq.com
  9. */
  10. declare (strict_types = 1);
  11. namespace app\admin\controller;
  12. use app\admin\service\AdminUploadService;
  13. use app\common\controller\Backend;
  14. use app\common\library\Tree;
  15. use app\common\model\Attachment;
  16. use app\common\model\QrcodeScan;
  17. use app\common\model\Third;
  18. use app\common\model\Qrcode;
  19. use app\common\model\Category;
  20. use app\common\service\msg\BackendMsg;
  21. use think\annotation\route\Group;
  22. use think\annotation\route\Route;
  23. use think\facade\Cache;
  24. use think\facade\Config;
  25. use think\Response;
  26. #[Group("ajax")]
  27. class Ajax extends Backend{
  28. protected $noNeedRight = ['*'];
  29. protected $noNeedLogin = ['js'];
  30. /**
  31. *上传文件
  32. */
  33. #[Route('POST','upload')]
  34. public function upload()
  35. {
  36. $disks=$this->request->post('disks');
  37. if(!$disks){
  38. $disks=$this->config['upload']['disks'];
  39. }
  40. $category=$this->request->post('category');
  41. $catelist=site_config('dictionary.filegroup');
  42. if(!key_exists($category,$catelist)){
  43. $category='';
  44. }
  45. $file = $this->request->file('file');
  46. $classname=config('filesystem.disks')[$disks]['class'];
  47. $name=config('filesystem.disks')[$disks]['name'];
  48. if(!class_exists($classname)){
  49. $this->error($name.'扩展未安装,请先下载');
  50. }
  51. try{
  52. $savename=$classname::newInstance([
  53. 'config'=>config('yunqi.upload'),
  54. 'admin_id'=>$this->auth->id,
  55. 'category'=>$category,
  56. 'file'=>$file
  57. ])->save();
  58. }catch (\Exception $e){
  59. $this->error(__('上传文件出错'),[
  60. 'file'=>$e->getFile(),
  61. 'line'=>$e->getLine(),
  62. 'msg'=>$e->getMessage()
  63. ]);
  64. }
  65. $this->success('',$savename);
  66. }
  67. /**
  68. *读取私有文件
  69. */
  70. #[Route('GET','readfile')]
  71. public function readfile(string $sha1='')
  72. {
  73. $attachment=Attachment::where("sha1",$sha1)->find();
  74. $filepath=root_path().$attachment['url'];
  75. $myfile = fopen($filepath, "r");
  76. $filecontent=fread($myfile,filesize($filepath));
  77. fclose($myfile);
  78. $ext = pathinfo($filepath, PATHINFO_EXTENSION);
  79. if(
  80. $ext=='jpg' ||
  81. $ext=='JPG' ||
  82. $ext=='png' ||
  83. $ext=='PNG' ||
  84. $ext=='jpeg' ||
  85. $ext=='JPEG' ||
  86. $ext=='gif' ||
  87. $ext=='GIF' ||
  88. $ext=='bmp' ||
  89. $ext=='BMP'
  90. ){
  91. header('Content-type:image/'.$ext);
  92. echo $filecontent;
  93. exit();
  94. }
  95. $filename = pathinfo($filepath, PATHINFO_FILENAME);
  96. Header ( "Content-type: application/octet-stream");
  97. Header ( "Accept-Ranges: bytes" );
  98. Header ( "Accept-Length: ".filesize ($filepath));
  99. Header ( "Content-Disposition: attachment; filename=".$filename.".".$ext);
  100. echo $filecontent;
  101. exit();
  102. }
  103. /**
  104. * 模拟读取通知消息,需要开发者完善动态效果
  105. */
  106. #[Route('GET,POST','message')]
  107. public function message()
  108. {
  109. $msgService=BackendMsg::newInstance(['msg_type'=>'backend']);
  110. //阅读消息
  111. if($this->request->isPost()){
  112. $ids=$this->request->post('ids/a');
  113. $msgService->read($ids);
  114. $this->success();
  115. }
  116. //获取消息
  117. if($this->request->isGet()){
  118. $message=$msgService->list($this->auth->id,1,100,false);
  119. if(empty($message)){
  120. $this->success('',array([
  121. 'title'=>__('通知消息'),
  122. 'list'=>[]
  123. ]));
  124. }
  125. $r=[];
  126. foreach ($message as $value){
  127. $hastitle=false;
  128. foreach ($r as $xs){
  129. if($xs['title']==$value['title']){
  130. $hastitle=true;
  131. }
  132. }
  133. if(!$hastitle){
  134. $r[]=[
  135. 'title'=>$value['title'],
  136. 'list'=>[]
  137. ];
  138. }
  139. }
  140. foreach ($message as $value){
  141. foreach ($r as $key=>$xs){
  142. if($xs['title']==$value['title']){
  143. $r[$key]['list'][]=$value;
  144. }
  145. }
  146. }
  147. $this->success('',$r);
  148. }
  149. }
  150. /**
  151. * 清空系统缓存
  152. */
  153. #[Route('GET','wipecache')]
  154. public function wipecache()
  155. {
  156. try {
  157. $type = $this->request->request("type");
  158. switch ($type) {
  159. case 'all':
  160. // no break
  161. case 'content':
  162. //内容缓存
  163. Cache::clear();
  164. if ($type == 'content') {
  165. break;
  166. }
  167. case 'template':
  168. // 模板缓存
  169. $list=scandir(root_path().'app'.DS);
  170. foreach ($list as $file){
  171. if(!is_dir($file)){
  172. continue;
  173. }
  174. if($file=='common'){
  175. continue;
  176. }
  177. $temp=root_path().'runtime'.DS.$file.DS.'temp';
  178. if(is_dir($temp)){
  179. rmdirs($temp, false);
  180. }
  181. }
  182. if ($type == 'template') {
  183. break;
  184. }
  185. case 'browser':
  186. // 浏览器缓存
  187. if ($type == 'browser') {
  188. break;
  189. }
  190. }
  191. } catch (\Exception $e) {
  192. $this->error($e->getMessage());
  193. }
  194. $this->success();
  195. }
  196. /**
  197. * 获取分类
  198. */
  199. #[Route('GET','category')]
  200. public function category()
  201. {
  202. $type = $this->request->get('type', '');
  203. $pid = $this->request->get('pid', 0);
  204. $where = ['status' => 'normal'];
  205. if ($type) {
  206. $where['type'] = $type;
  207. }
  208. $categorylist = Category::where($where)->field('id,pid,name')->order('weigh desc,id desc')->select()->toArray();
  209. $r=Tree::instance()->init($categorylist)->getTreeArray($pid);
  210. $this->success('',$r);
  211. }
  212. /**
  213. * 获取地区
  214. */
  215. #[Route('GET','area')]
  216. public function area()
  217. {
  218. if(!addons_installed('area')){
  219. $this->error('请先安装插件area');
  220. }
  221. $pid = $this->request->get("pid");
  222. $provincelist = \app\common\model\Area::where('pid',$pid)->field('id,name')->select();
  223. $this->success('', $provincelist);
  224. }
  225. /**
  226. * 获取js文件
  227. */
  228. #[Route('GET','js/:name')]
  229. public function js($name)
  230. {
  231. $header = ['Content-Type' => 'application/javascript'];
  232. if (!Config::get('app.app_debug')) {
  233. $offset = 30 * 60 * 60 * 24; // 缓存一个月
  234. $header['Cache-Control'] = 'public';
  235. $header['Pragma'] = 'cache';
  236. $header['Expires'] = gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
  237. }
  238. // 页面缓存
  239. ob_start();
  240. ob_implicit_flush(false);
  241. require(root_path().'runtime'.DS.'admin'.DS.'temp'.DS.$name.'-js.php');
  242. // 获取并清空缓存
  243. $content = ob_get_clean();
  244. $response = Response::create(trim($content))->header($header);
  245. $response->send();
  246. exit;
  247. }
  248. //绑定第三方账号
  249. #[Route('GET,JSON','third/:action')]
  250. public function third($action)
  251. {
  252. if($action=='qrcode'){
  253. $platform=$this->request->get('platform');
  254. $foreign_key=$this->request->get('foreign_key');
  255. if($platform=='mpapp'){
  256. $config=[
  257. 'appid'=>site_config("uniapp.mpapp_id"),
  258. 'appsecret'=>site_config("uniapp.mpapp_secret"),
  259. ];
  260. $qrcode=Qrcode::createQrcode(Qrcode::TYPE('绑定第三方账号'),$foreign_key,5*60);
  261. $wechat=new \WeChat\Qrcode($config);
  262. $ticket = $wechat->create($qrcode->id)['ticket'];
  263. $url=$wechat->url($ticket);
  264. $content=file_get_contents($url);
  265. header('Content-Type: image/png');
  266. echo $content;
  267. exit;
  268. }
  269. }
  270. if($action=='check'){
  271. $platform=$this->request->get('platform');
  272. $foreign_key=$this->request->get('foreign_key');
  273. $scan=QrcodeScan::where('foreign_key',$foreign_key)->find();
  274. if($scan){
  275. $third=Third::where(['openid'=>$scan->openid,'platform'=>$platform])->find();
  276. if($third){
  277. $this->success('',$third);
  278. }
  279. }
  280. $this->error();
  281. }
  282. if($action=='selectpage'){
  283. $platform=$this->request->get('platform');
  284. $this->model=new Third();
  285. $where=[];
  286. $where[]=['platform','=',$platform];
  287. return $this->selectpage($where);
  288. }
  289. }
  290. }