error = '请联系管理员配置参数'; return false; } $this->config = $config; } /** * Notes: 设置手机号 * @param string $mobile * @author 段誉(2021/5/18 17:41) * @return $this */ public function setMobile($mobile = '') { $this->mobile = $mobile; return $this; } /** * Notes: 设置模板id * @param null $template_id * @author 段誉(2021/5/18 17:41) * @return $this */ public function setTemplateId($template_id = null) { $this->template_id = $template_id; return $this; } /** * Notes: 设置模板参数 * @param null $template_param * @author 段誉(2021/5/18 17:42) * @return $this */ public function setTemplateParam($template_param = null) { $this->template_param = $template_param; return $this; } /** * @notes 发送 (此处$config[app_key]为系统统一命名,原腾讯云命名为secret_id) * @return false|mixed * @author 段誉 * @date 2021/8/4 10:40 */ public function send() { try { $cred = new Credential($this->config['app_key'], $this->config['secret_key']); $httpProfile = new HttpProfile(); $httpProfile->setEndpoint("sms.tencentcloudapi.com"); $clientProfile = new ClientProfile(); $clientProfile->setHttpProfile($httpProfile); $client = new SmsClient($cred, "", $clientProfile); $req = new SendSmsRequest(); $params = [ 'PhoneNumberSet' => ['+86'.$this->mobile], 'TemplateID' => $this->template_id, 'Sign' => $this->config['sign'], 'TemplateParamSet' => $this->template_param, 'SmsSdkAppid' => $this->config['app_id'], ]; $req->fromJsonString(json_encode($params)); $resp = json_decode($client->SendSms($req)->toJsonString(), true); if (isset($resp['SendStatusSet']) && $resp['SendStatusSet'][0]['Code'] == 'Ok') { return $resp; } else { $message = $res['SendStatusSet'][0]['Message'] ?? json_encode($resp); throw new \Exception('短信错误:' . $message); } } catch (TencentCloudSDKException $e) { $this->error = $e->getMessage(); return false; } catch (\Exception $e) { $this->error = $e->getMessage(); return false; } } }