VerificationCodeController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020-06-11
  6. * Time: 13:36
  7. */
  8. namespace api\js\controller;
  9. use Aliyun\DySDKLite\SignatureHelper;
  10. use api\js\model\UserModel;
  11. use cmf\controller\RestBaseController;
  12. use think\Db;
  13. //use Aliyun\DySDKLite\SignatureHelper;
  14. class VerificationCodeController extends RestBaseController
  15. {
  16. /**
  17. * 发送验证码
  18. */
  19. public function getVerificationCode()
  20. {
  21. $mobile = $this->request->param('mobile');
  22. $type = $this->request->param('type',1);
  23. $status = $this->request->param('status',1);
  24. if(!cmf_check_mobile($mobile)){
  25. $this->error('手机号不正确!');
  26. }
  27. $params = array ();
  28. $alisms = cmf_get_option('alisms');
  29. if($type == 1){
  30. if($status == 1){
  31. $where['user_type'] = 2;
  32. }
  33. if($status == 2){
  34. $where['user_type'] = 3;
  35. }
  36. $where['mobile'] = $mobile;
  37. $user_id = UserModel::name('user')->where($where)->value('id');
  38. if($user_id){
  39. $this->error('手机号已注册!');
  40. }
  41. $params["TemplateCode"] = $alisms['mb_id'];
  42. }else if($type == 2){
  43. $user_id = UserModel::name('user')->where('mobile',$mobile)->value('id');
  44. if(!$user_id){
  45. $this->error('手机号未注册!');
  46. }
  47. $params["TemplateCode"] = $alisms['edit_mb_id'];
  48. }else{
  49. $params["TemplateCode"] = $alisms['mb_id'];
  50. }
  51. $code = cmf_get_verification_code($mobile);
  52. if (empty($code)) {
  53. $this->error('验证码发送过多,明天再试');
  54. }
  55. cmf_verification_code_log($mobile, $code, time()+5*60);
  56. $param = ['mobile' => $mobile, 'code' => $code];
  57. $result = hook_one("send_mobile_verification_code", $param);
  58. if ($result !== false && !empty($result['error'])) {
  59. $this->error($result['message']);
  60. }
  61. $security = false;
  62. $accessKeyId = $alisms['access_key_id'];
  63. $accessKeySecret = $alisms['access_key_secret'];;
  64. $params["PhoneNumbers"] = $mobile;
  65. $params["SignName"] = $alisms['sms_qm'];
  66. $params['TemplateParam'] = Array (
  67. "code" => $code,
  68. );
  69. // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
  70. if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
  71. $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
  72. }
  73. // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
  74. $helper = new SignatureHelper();
  75. // 此处可能会抛出异常,注意catch
  76. $content = $helper->request(
  77. $accessKeyId,
  78. $accessKeySecret,
  79. "dysmsapi.aliyuncs.com",
  80. array_merge($params, array(
  81. "RegionId" => "cn-hangzhou",
  82. "Action" => "SendSms",
  83. "Version" => "2017-05-25",
  84. )),
  85. $security
  86. );
  87. if($content->Message == 'OK' && $content->Code == 'OK'){
  88. $this->success('验证已发送'.$mobile.'注意查收');
  89. }else{
  90. $this->error($content->Message);
  91. }
  92. }
  93. }