SmsTemplateApply.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\admin\controller\sms;
  3. use app\admin\controller\AuthController;
  4. use crmeb\services\{
  5. FormBuilder, JsonService, sms\Sms, UtilService
  6. };
  7. use think\facade\Route;
  8. /**
  9. * 短信模板申请
  10. * Class SmsTemplateApply
  11. * @package app\admin\controller\sms
  12. */
  13. class SmsTemplateApply extends AuthController
  14. {
  15. /**
  16. * @var Sms
  17. */
  18. protected $smsHandle;
  19. protected function initialize()
  20. {
  21. parent::initialize(); // TODO: Change the autogenerated stub
  22. $this->smsHandle = new Sms('yunxin', [
  23. 'sms_account' => sys_config('sms_account'),
  24. 'sms_token' => sys_config('sms_token'),
  25. 'site_url' => sys_config('site_url')
  26. ]);
  27. }
  28. /**
  29. * 显示资源列表
  30. *
  31. * @return string
  32. */
  33. public function index()
  34. {
  35. if (!$this->smsHandle->isLogin()) return redirect(url('sms.smsConfig/index').'?type=4&tab_id=18');
  36. $sms_account = sys_config('sms_account');
  37. $this->assign('sms_account',$sms_account);
  38. return $this->fetch();
  39. }
  40. /**
  41. * 异步获取模板列表
  42. */
  43. public function lst()
  44. {
  45. $where = UtilService::getMore([
  46. ['status', ''],
  47. ['title', ''],
  48. ['page', 1],
  49. ['limit', 20],
  50. ]);
  51. $templateList = $this->smsHandle->template($where);
  52. if ($templateList['status'] == 400) return JsonService::fail($templateList['msg']);
  53. return JsonService::successlayui($templateList['data']);
  54. }
  55. /**
  56. * 显示创建资源表单页.
  57. *
  58. * @return string
  59. * @throws \FormBuilder\exception\FormBuilderException
  60. */
  61. public function create()
  62. {
  63. $field = [
  64. FormBuilder::input('title', '模板名称'),
  65. FormBuilder::textarea('text', '模板内容示例', '【您的短信签名】您的验证码是:{$code},有效期为{$time}分钟。如非本人操作,可不用理会。模板中的{$code}和{$time}需要替换成对应的变量,请开发者知晓。修改此项无效!'),
  66. FormBuilder::input('content', '模板内容')->type('textarea'),
  67. FormBuilder::radio('type', '模板类型', 1)->options([['label' => '验证码', 'value' => 1], ['label' => '通知', 'value' => 2], ['label' => '推广', 'value' => 3]])
  68. ];
  69. $form = FormBuilder::make_post_form('申请短信模板', $field, Route::buildUrl('save'), 2);
  70. $this->assign(compact('form'));
  71. return $this->fetch('public/form-builder');
  72. }
  73. /**
  74. * 保存新建的资源
  75. */
  76. public function save()
  77. {
  78. $data = UtilService::postMore([
  79. ['title', ''],
  80. ['content', ''],
  81. ['type', 0]
  82. ]);
  83. if (!strlen(trim($data['title']))) return JsonService::fail('请输入模板名称');
  84. if (!strlen(trim($data['content']))) return JsonService::fail('请输入模板内容');
  85. $applyStatus = $this->smsHandle->apply($data['title'], $data['content'], $data['type']);
  86. if ($applyStatus['status'] == 400) return JsonService::fail($applyStatus['msg']);
  87. return JsonService::success('申请成功');
  88. }
  89. }