initialize($config); } public function initialize($config) { try { $classSpace = __NAMESPACE__ . '\\driver\\' . ucfirst(strtolower($config['sms_type'])) . 'Sms'; if (!class_exists($classSpace)) { throw new CommonException(204003); } $this->driver = new $classSpace($config); if(!is_null($this->driver->getError())) { throw new CommonException($this->driver->getError()); } return true; } catch ( Exception $e) { $this->error = $e->getMessage(); return false; } } /** * 获取短信错误 * @return null */ public function getError() { return $this->error; } /** * 发送短信 * @param $mobile * @param $data * @return bool */ public function send($mobile, $data) { try { // 开始发送 $result = $this->driver ->setMobile($mobile) ->setTemplateId($data['template_id']) ->setTemplateParams($data['params']) ->send(); if(false === $result) { throw new MessageException($this->driver->getError()); } return $result; } catch( Exception $e) { $this->error = $e->getMessage(); return false; } } }