Ajax.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Backend;
  4. use think\Exception;
  5. use think\exception\FileException;
  6. use app\common\library\Upload;
  7. use think\exception\ValidateException;
  8. use think\facade\Db;
  9. use think\facade\Event;
  10. use app\admin\model\ReqOrder;
  11. use app\admin\validate\ReqOrder as ReqOrderValidate;use think\facade\Validate;
  12. class Ajax extends Backend
  13. {
  14. protected $noNeedLogin = ['area', 'buildSuffixSvg','saveFirstForm','listFirstForm','FirstFormInfo'];
  15. public function initialize()
  16. {
  17. parent::initialize();
  18. }
  19. public function saveFirstForm(){
  20. $user_id = $this->auth->id;
  21. // $user_id = 1;
  22. $post = $this->request->post();
  23. $reqOrder = new ReqOrder();
  24. $validate = new ReqOrderValidate();
  25. try{
  26. $validate->scene("add")->check($post);
  27. }catch (ValidateException $e){
  28. $this->error($e->getMessage());
  29. }
  30. $data = [
  31. 'reqCode' => 'REQ'.date('YmdH').rand(1000,9999).substr(md5($user_id),-5),
  32. 'req_user_id' => $user_id,
  33. 'req_endtime' => date('Y-m-d 00:00:00',time()+3600*24*5), //默认5天截止
  34. 'city' => $post['city'],
  35. 'name' => $post['name'],
  36. 'act_time' => $post['act_time'],
  37. 'act_day_count' => $post['act_day_count'],
  38. 'budget' => $post['budget'],
  39. 'participant'=> $post['participant'],
  40. 'require_item' =>$post['require_item'],
  41. 'req_corp' =>$post['req_corp'],
  42. 'req_tel' => $post['phone'],
  43. ];
  44. $res = $reqOrder->save($data);
  45. if(!$res){
  46. $this->error('保存失败');
  47. }
  48. Event::trigger("ReOrderAdd",$data);
  49. $this->success('保存成功',["reqCode"=>$data['reqCode']]);
  50. }
  51. public function listFirstForm(){
  52. $post = $this->request->post();
  53. $user_id = $this->auth->id;
  54. $where =[['req_user_id',"=",$user_id]];
  55. if ($post['status']!='')$where[]=["status","=",$post['status']];
  56. if ($post['flow_stage']!='')$where[]=["flow_stage","=",$post['flow_stage']];
  57. $res = Db::name('req_order')
  58. ->where($where)
  59. ->paginate($post['limit'])
  60. ->order("id desc");
  61. $this->success('', [
  62. 'list' => $res->items(),
  63. 'total' => $res->total(),
  64. 'remark' => get_route_remark(),
  65. ]);
  66. }
  67. public function FirstFormInfo(){
  68. $post = $this->request->post();
  69. $valide =Validate::rule(["reqCode|订单编号"=>"require|max:255"]);
  70. if($valide->check($post)==false)$this->error($valide->getError());
  71. $user_id = $this->auth->id;
  72. $res = Db::name('req_order')->where('req_user_id',$user_id)->where("reqCode",$post['reqCode'])->findOrEmpty();
  73. if(!$res){
  74. $this->error('信息获取失败');
  75. }
  76. $res['supplierName'] = Db::name("supplier")->where(["id"=>$res['supplier_id']])->value("name","");
  77. $res['city_area'] = Db::name("area")->where(["id"=>$res['city']])->value("mergename","");
  78. $this->success('',$res);
  79. }
  80. public function upload()
  81. {
  82. $file = $this->request->file('file');
  83. try {
  84. $upload = new Upload($file);
  85. $attachment = $upload->upload(null, 0, $this->auth->id);
  86. unset($attachment['createtime'], $attachment['quote']);
  87. } catch (Exception|FileException $e) {
  88. $this->error($e->getMessage());
  89. }
  90. $this->success(__('File uploaded successfully'), [
  91. 'file' => $attachment ?? []
  92. ]);
  93. }
  94. public function area()
  95. {
  96. $this->success('', get_area());
  97. }
  98. public function buildSuffixSvg()
  99. {
  100. $suffix = $this->request->param('suffix', 'file');
  101. $background = $this->request->param('background');
  102. $content = build_suffix_svg((string)$suffix, (string)$background);
  103. return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/svg+xml');
  104. }
  105. }