Index.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace addons\alisms\controller;
  3. use think\addons\Controller;
  4. /**
  5. * 阿里短信
  6. */
  7. class Index extends Controller
  8. {
  9. protected $model = null;
  10. protected $templateList = [
  11. 'register' => '注册',
  12. 'resetpwd' => '重置密码',
  13. 'changepwd' => '修改密码',
  14. 'changemobile' => '修改手机号',
  15. 'profile' => '修改个人信息',
  16. 'notice' => '通知',
  17. 'mobilelogin' => '移动端登录',
  18. 'bind' => '绑定账号',
  19. ];
  20. public function _initialize()
  21. {
  22. if (!\app\admin\library\Auth::instance()->id) {
  23. $this->error('暂无权限浏览');
  24. }
  25. parent::_initialize();
  26. }
  27. //首页
  28. public function index()
  29. {
  30. $this->view->assign('templateList', $this->templateList);
  31. return $this->view->fetch();
  32. }
  33. //发送测试短信
  34. public function send()
  35. {
  36. $config = get_addon_config('alisms');
  37. $mobile = $this->request->post('mobile');
  38. $template = $this->request->post('template');
  39. $sign = $this->request->post('sign', '');
  40. if (!$mobile) {
  41. $this->error('手机号不能为空');
  42. }
  43. $templateArr = $config['template'] ?? [];
  44. if (!isset($templateArr[$template]) || !$templateArr[$template]) {
  45. $this->error('后台未配置对应的模板CODE');
  46. }
  47. $template = $templateArr[$template];
  48. $sign = $sign ? $sign : $config['sign'];
  49. $param = (array)json_decode($this->request->post('param', '', 'trim'));
  50. $param = ['code' => mt_rand(1000, 9999)];
  51. $alisms = new \addons\alisms\library\Alisms();
  52. $ret = $alisms->mobile($mobile)
  53. ->template($template)
  54. ->sign($sign)
  55. ->param($param)
  56. ->send();
  57. if ($ret) {
  58. $this->success("发送成功");
  59. } else {
  60. $this->error("发送失败!失败原因:" . $alisms->getError());
  61. }
  62. }
  63. }