Some days ago, I wrote the Go design mode (4)- code writing, but I was in a hurry to write at that time. The code writing in the article was not very good, so I took some time to optimize it again. It’s still not perfect, but it’s better than last time.
This time there are some optimization points, you can look for, see where can continue to improve.
/ / tools
class Utils {
// Returns the current millisecond timestamp
public function msectime() {
list($msec.$sec) = explode(' ', microtime());
$msectime = (float) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return $msectime;
}
/ / stitching URL
public function buildQuery($url.$arr) {
return $url."?".http_build_query($arr); }}/ / Http client
class HttpClient {
/ / HTTP Get requests
function sendGetRequest($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_URL, $url);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
/ / HTTP Post request
function sendPostRequest($url.$postData) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output = curl_exec($ch);
curl_close($ch);
return $output; }}// Interact with the hospital system
class HospitalOperation {
public $httpClient;
public $hospital;
public $utils;
function __construct($hospital) {
$this->httpClient = new HttpClient();
$this->hospital = $hospital;
$this->utils = new Utils();
}
//1. Obtain the personal ID
public function getUserId() {
var_dump("Start executing getUserId");
$url = $this->utils->buildQuery($this->hospital->getUinfoUrl(),$this->hospital->getUinfoParams());
$uInfo = $this->httpClient->sendGetRequest($url);
if(empty($uInfo)) {
var_dump("Failed to execute getUserId");
return -1;
}
$res = $this->hospital->getUid($uInfo);
if(empty($res)) {
var_dump("Failed to get getUserId");
return -1;
}
var_dump("GetUserId succeeded, result is".$res);
$this->hospital->setRecordData($res);
return 0;
}
//2. Obtain the patient id
public function getPatientId() {
var_dump("Start executing getPatientId");
$url = $this->utils->buildQuery($this->hospital->getPatientUrl(),$this->hospital->getPatientParams());
$listInfo = $this->httpClient->sendGetRequest($url);
if(empty($listInfo)) {
var_dump("Failed to execute getPatientId");
return -1;
}
$res = $this->hospital->getPatientId($listInfo);
if(empty($res)) {
var_dump("Failed to get getPatientId");
return -1;
}
var_dump("GetPatientId successful, patientId is".$res);
$this->hospital->setRecordData($res);
return 0;
}
//3. Get the id of the doctor's available period
public function getBresPeakId() {
var_dump("Start executing getBresPeakId");
$url = $this->utils->buildQuery($this->hospital->getBresPeakUrl(),$this->hospital->getBresPeakParams());
$bresInfo = $this->httpClient->sendGetRequest($url);
if(empty($bresInfo)) {
var_dump("Failed to execute getBresPeakId");
return -1;
}
$res = $this->hospital->getBresPeakId($bresInfo);
if(empty($res)) {
var_dump("Failed to get getBresPeakId");
return -1;
}
var_dump("Obtaining getBresPeakId succeeded".$res);
$this->hospital->setRecordData($res);
return 0;
}
4 / / registration
public function registe() {
var_dump("Start executing registe");
$url = $this->utils->buildQuery($this->hospital->getRegistUrl(),$this->hospital->getRegistParams());
$postData = $this->hospital->getRegistPostData();
$regInfo = $this->httpClient->sendPostRequest($url.$postData);
$res = $this->hospital->getRegistRes($regInfo);
if(empty($res)) {
var_dump('registe failed'.$postData);
} else {
var_dump('registe success'.$postData.$res);
}
return $res;
}
//5. Execute the entire process
function run() {
$res = $this->getUserId();
if($res= = -1) {
exit;
}
$res = $this->getPatientId();
if($res= = -1) {
exit;
}
$res = $this->getBresPeakId();
if($res= = -1) {
exit;
}
$this->registe(); }}// Return data decode
class Decode {
public function decodeFunc($data) {
$data = json_decode($data.true);
return $data; }}/ / class hospital
class Hospital {
protected $wxId;
/ / WeChat ID
protected $regDepId;
// Make an appointment
protected $docName;
// Doctor's name
protected $patientName;
// Patient name
protected $regDay;
// Date of appointment
protected $uInfoUrl;
// Get personal information URL from wechat
protected $patientListUrl;
// Get the patient list URL
protected $bresPeakUrl;
// Doctor visit time URL
protected $regUrl;
/ / registered URL
protected $uId;
// Wechat ID corresponds to the user ID
protected $recordData = array(a);// Register the data to be submitted
public $utils;
/ / tools
public $decode;
// The interface returns data parsing
function __construct($config.$regDepId.$docName.$patientName.$regDay.$decode) {
$this->wxId = $config['wxId'];
$this->uInfoUrl = $config['uInfoUrl'];
$this->patientListUrl = $config['patientListUrl'];
$this->bresPeakUrl = $config['bresPeakUrl'];
$this->regUrl = $config['regUrl'];
$this->regDepId = $regDepId;
$this->docName = $docName;
$this->patientName = $patientName;
$this->regDay = $regDay;
$this->decode = $decode;
$this->utils = new Utils();
}
/** * intermediate result record */
final public function setRecordData($data) {
foreach ($data as $key= >$value) {
$this->recordData[$key] = $value; }}final public function getRecordData() :array {
return $this->recordData;
}
/** * url related */
final public function getUinfoUrl() :string {
return $this->uInfoUrl;
}
final public function getPatientUrl() :string {
return $this->patientListUrl;
}
final public function getBresPeakUrl() :string {
return $this->bresPeakUrl;
}
final public function getRegistUrl() :string {
return $this->regUrl;
}
public function getUinfoParams() :array {}// Subclasses need to be overridden
public function getPatientParams() :array {}// Subclasses need to be overridden
public function getBresPeakParams() :array {}// Subclasses need to be overridden
public function getRegistParams() :array {}// Subclasses need to be overridden
public function getRegistPostData() :array {}// Subclasses need to be overridden
/** * obtain the corresponding user ID of wechat, null means failed to obtain * subclass need to rewrite */
public function getUid($res) :array {}/** * get patient Id, null means failed to get * subclass needs to be overridden */
public function getPatientId($res) :array {}/** * get the doctor's interview time Id, null means failed to get * subclass needs to be overridden */
public function getBresPeakId($res) :array {}/** * get the result of registration, null means registration failed * subclass should be overridden */
public function getRegistRes($res) :array {}}/** * Class HospitalA * inherits from Hospital Class, mainly used in * 1. Generate the URL of each interface * 2. Parse the returned data to obtain the desired result * 3. Finally generate the registered data postData */
class HospitalA extends Hospital {
/** * Url request parameters related */
public function getUinfoParams() :array {
return array(
'act'= >'userinfo_oid'.'uid'= >$this->wxId,
'tm'= >$this->utils->msectime(),
'oid'= >$this->wxId,
);
}
public function getPatientParams() :array {
$recordData = $this->getRecordData();
return array(
'act'= >'member'.'uid'= >$recordData['uid'].'oid'= >$this->wxId,
);
}
public function getBresPeakParams() :array {
return array(
'act'= >'bespeak_v1'.'deptid'= >$this->regDepId,
'clsid'= >2./ / visit
'tm'= >$this->utils->msectime(),
);
}
public function getRegistParams() :array {
return array(
'act'= >'bespeak',); }public function getRegistPostData() :array {
$recordData = $this->getRecordData();
return array(
'oid'= >$this->wxId,
'uid'= >$recordData['uid'].'sickid'= >$recordData['sickid'].'bespeakid'= >$recordData['bespeakid'].'aorp'= >$recordData['aorp']); }/ * * *@param $res
* @returnArray * gets the uid */ of wechat based on the returned result
public function getUid($res) :array {
$res = $this->decode->decodeFunc($res);
if($res['result'] != 'ok') {
return array(a); }$uInfo = $this->decode->decodeFunc($res['data']);
return array(
'uid'= >$uInfo['id']); }/ * * *@param $res
* @returnArray * Gets the patient ID */ based on the returned result
public function getPatientId($res) :array {
$res = $this->decode->decodeFunc($res);
if($res['result'] != 'ok') {
return array(a); }$list = $this->decode->decodeFunc($res['data']);
foreach ($list as $item) {
if ($item['name'] = =$this->patientName) {
return array(
'sickid'= >$item['id']); }}return array(a); }/ * * *@param $res
* @returnArray * Retrieves available consultation periods based on the returned results */
public function getBresPeakId($res) :array {
$res = $this->decode->decodeFunc($res);
if($res['result'] != 'ok') {
return array(a); }$docList = $this->decode->decodeFunc($res['data']);
$bespeakid = -1;
$aorp = 0;
//0 am 1 PM
$flag = 0;
//var_dump($docList);
foreach ($docList as $item) {
if ($item['name'] = =$this->docName && $item['bdate'] = =$this->regDay) {
if ($item['pm'] != 0 && $item['pm'] != 'about the full') {
// There will be an appointment in the afternoon
$aorp = 1;
$flag = 1;
} else if ($item['am'] != 0 && $item['am'] != 'about the full') {
// There's a number in the morning
$aorp = 0;
$flag = 1;
}
if($flag= =1) {
$bespeakid = (int) ($item['id']);
var_dump('Choose the doctor as'.$item);
break; }}}if($bespeakid! = -1) {
return array(
'bespeakid'= >$bespeakid.'aorp'= >$aorp,); }return array(a); }/ * * *@param $res
* @returnArray * Determines whether the registration is successful based on the returned result */
public function getRegistRes($res) :array {
$res = $this->decode->decodeFunc($res);
var_dump($res);
if($res['result'] != 'ok') {
return array(a); }return array(
'res'= >'success',); }}class HosiptalFactory {
public function createHospital($hospitalName.$config.$regDepId.$docName.$patientName.$regDay.$decode) {
$hospital = null;
switch ($hospitalName) {
case 'A': $hospital = new HospitalA($config['A'].$regDepId.$docName.$patientName.$regDay.$decode);
}
return $hospital; }}function main() {
$config = array(
'A'= >array(/ / 1 configuration
'wxId'= >The '*'.'uInfoUrl'= >The '*'.'patientListUrl'= >The '*'.'bresPeakUrl'= >The '*'.'regUrl'= >The '*',),);$decode = new Decode();
$docName = The '*';
$patientName = The '*';
$regDepId= (.$regDay = '2021-03-26';
$hospitalName = 'A';
// Use factory mode to create the corresponding hospital
$factory = new HosiptalFactory();
$hospital = $factory->createHospital($hospitalName.$config.$regDepId.$docName.$patientName.$regDay.$decode);
$oper = new HospitalOperation($hospital);
$oper->run();
}
main();
Copy the code