Ajax.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\api\controller;
  3. use think\Exception;
  4. use think\exception\FileException;
  5. use app\common\library\Upload;
  6. use app\common\controller\Frontend;
  7. use think\facade\Db;
  8. use think\facade\Log;
  9. use app\admin\model\ReqOrder;
  10. class Ajax extends Frontend
  11. {
  12. protected $noNeedLogin = ['area', 'buildSuffixSvg','saveFirstForm'];
  13. public function initialize()
  14. {
  15. parent::initialize();
  16. }
  17. public function saveFirstForm(){
  18. $user_id = $this->auth->id;
  19. $user_id = 1;
  20. $post = $this->request->post();
  21. $reqOrder = new ReqOrder();
  22. $validate = new \app\admin\validate\ReqOrder;
  23. if (!$validate->check($post)) {
  24. $this->error($validate->getError());
  25. }
  26. $data = [
  27. //'sn' => 'as'.date('YmdH').rand(1000,9999).substr(md5($user_id),-5),
  28. 'req_user_id' => $user_id,
  29. 'req_endtime' => date('Y-m-d 00:00:00',time()*3600*24*5), //默认5天截止
  30. 'city' => $post['city'],
  31. 'act_time' => $post['act_date'],
  32. 'act_day_count' => 1,
  33. 'budget' => $post['budget'],
  34. 'participant'=> $post['participant'],
  35. 'require_item' => '',
  36. 'req_corp' => '',
  37. 'req_tel' => $post['phone'],
  38. ];
  39. $res = $reqOrder->save($data);
  40. //print log post
  41. //Log::write($post,'error');
  42. if(!$res){
  43. $this->error('保存失败');
  44. }
  45. $this->success('保存成功');
  46. }
  47. public function listFirstForm(){
  48. //get user id
  49. $user_id = $this->auth->id;
  50. $res = Db::name('req_order')->where('user_id',$user_id)->find();
  51. if(!$res){
  52. $this->error('没有数据');
  53. }
  54. $this->success('获取成功',$res);
  55. }
  56. public function upload()
  57. {
  58. $file = $this->request->file('file');
  59. try {
  60. $upload = new Upload($file);
  61. $attachment = $upload->upload(null, 0, $this->auth->id);
  62. unset($attachment['createtime'], $attachment['quote']);
  63. } catch (Exception|FileException $e) {
  64. $this->error($e->getMessage());
  65. }
  66. $this->success(__('File uploaded successfully'), [
  67. 'file' => $attachment ?? []
  68. ]);
  69. }
  70. public function area()
  71. {
  72. $this->success('', get_area());
  73. }
  74. public function buildSuffixSvg()
  75. {
  76. $suffix = $this->request->param('suffix', 'file');
  77. $background = $this->request->param('background');
  78. $content = build_suffix_svg((string)$suffix, (string)$background);
  79. return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/svg+xml');
  80. }
  81. }