error = '请联系管理员配置参数'; return false; } $this->config = $config; } /** * @notes 设置手机号 * @param $mobile * @return $this * @author 段誉 * @date 2022/9/15 16:28 */ public function setMobile($mobile) { $this->mobile = $mobile; return $this; } /** * @notes 设置模板id * @param $templateId * @return $this * @author 段誉 * @date 2022/9/15 16:28 */ public function setTemplateId($templateId) { $this->templateId = $templateId; return $this; } /** * @notes 设置模板参数 * @param $templateParams * @return $this * @author 段誉 * @date 2022/9/15 16:28 */ public function setTemplateParams($templateParams) { $this->templateParams = json_encode($templateParams, JSON_UNESCAPED_UNICODE); return $this; } /** * @notes 短信内容 * @param $sceneContent * @return $this * @author 段誉 * @date 2022/9/15 16:28 */ public function setSceneContent($sceneContent) { $this->sceneContent = $sceneContent; return $this; } /** * @notes 国家区号 * @param $sceneContent * @return $this * @author 段誉 * @date 2022/9/15 16:28 */ public function setCountryCode($countryCode) { $this->countryCode = $countryCode; return $this; } /** * @notes 错误信息 * @return string|null * @author 段誉 * @date 2022/9/15 16:27 */ public function getError() { return $this->error; } /** * @notes 发送短信 * @return array|false * @author 段誉 * @date 2022/9/15 16:27 */ public function send() { try { $smsapi = $this->config['url_cn']; $user = $this->config['username']; //短信平台帐号 $pass = md5($this->config['api_key']); //短信平台密码 $content = $this->sceneContent;//要发送的短信内容 $sendPhone = urlencode($this->mobile);//要发送短信的手机号码 $countryCode = $this->countryCode;//国家区号 if('+86' != $countryCode){ $sendPhone = urlencode($this->countryCode.$this->mobile); $smsapi = $this->config['url_go']; } $sendurl = $smsapi."?u=".$user."&p=".$pass."&m=".$sendPhone."&c=".urlencode($content); $result = file_get_contents($sendurl); if($result !=0){ if($result ==51){ throw new \Exception('pwd.mobileError'); }else{ throw new \Exception('network.networkAnomaly'); } } } catch(\Exception $e) { $this->error = $e->getMessage(); return false; } } }