WebAjax.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Contacts;
  4. use app\common\exception\UploadException;
  5. use app\common\library\Sms as Smslib;
  6. use app\common\library\Upload;
  7. use think\Hook;
  8. /**
  9. * Ajax异步请求接口
  10. * @internal
  11. */
  12. class WebAjax extends WebIndexApi
  13. {
  14. protected $noNeedLogin = ['sendsms'];
  15. protected $noNeedRight = [];
  16. protected $layout = '';
  17. /**
  18. * 上传文件
  19. */
  20. public function upload() {
  21. $attachment = null;
  22. //默认普通上传文件
  23. $file = $this->request->file('file');
  24. $name=input('name');
  25. try {
  26. $upload = new Upload($file);
  27. $attachment = $upload->upload();
  28. $info = $attachment->toArray();
  29. $file = new \addons\qingdongams\model\File();
  30. $params = [
  31. 'name' => $name??$info['filename'],
  32. 'save_name' => $info['url'],
  33. 'size' => $info['filesize'],
  34. 'types' => $info['mimetype'],
  35. 'file_path' => $info['url'],
  36. 'create_staff_id' => empty($staff)?0:$staff->id,
  37. ];
  38. $file->data(array_filter($params));
  39. $file->save();
  40. $fileId = $file->id;
  41. } catch (UploadException $e) {
  42. return json_encode(['code' => 0, 'msg' => $e->getMessage()]);
  43. }
  44. $this->success(__('Uploaded successful'), [
  45. 'id' => $fileId,
  46. 'url' => cdnurl($params['file_path'], true)
  47. ]);
  48. }
  49. /**
  50. * 发送验证码
  51. *
  52. * @ApiMethod (POST)
  53. * @param string $mobile 手机号
  54. * @param string $event 事件名称
  55. */
  56. public function sendSms()
  57. {
  58. $mobile = $this->request->post("mobile");
  59. $event = 'mobilelogin';
  60. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  61. $this->error(__('手机号不正确'));
  62. }
  63. $last = Smslib::get($mobile, $event);
  64. if ($last && time() - $last['createtime'] < 60) {
  65. // $this->error(__('发送频繁'));
  66. }
  67. $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  68. if ($ipSendTotal >= 5) {
  69. // $this->error(__('发送频繁'));
  70. }
  71. $customerId = Contacts::where(['mobile' => $mobile])->value('customer_id');
  72. if (!$customerId) {
  73. $this->error('手机号不存在');
  74. }
  75. if (!Hook::get('sms_send')) {
  76. $this->error(__('请在后台插件管理安装短信验证插件'));
  77. }
  78. $ret = Smslib::send($mobile, null, $event);
  79. if ($ret) {
  80. $this->success(__('发送成功'));
  81. } else {
  82. $this->error(__('发送失败,请检查短信配置是否正确'));
  83. }
  84. }
  85. }