load->database();
	$this->table = 'notice';
    $this->load_table('notice');
}
    public function get_type($shop,$type,$logisticstatus,$ordertatus)//查找符合消息发送条件的信息
	{
		if($type == 1)
		{
		    return  $this->find("shop = '$shop' and type = '$type' and ordertatus = '$ordertatus' and state = 1 and ktime < 'time()' and jtime < 'time()'");
		}
		else if($type == 2)
		{
		    return  $this->find("shop = '$shop' and type = '$type' and logisticstatus = '$logisticstatus' and state = 1 and ktime < 'time()' and jtime < 'time()'");
		}
		else if($type == 3)
		{
		    return  $this->find("shop = '$shop' and type = '$type' and state = 1 and ktime < 'time()' and jtime < 'time()'");
		}
	}
	//所属店铺、模板类型、物流状态/订单状态
	/** 通过订单状态 */
	public function get_ordertatus($ordertatus)
	{
		return  $this->find("ordertatus = '$ordertatus'");	
	}
	public function get_go($list,$shop,$genre,$option)
	{
		//aptsmt获取订单:崔付款、确认地址
		$time = time();
		$data = $this->find("shop = '".$shop['id']."' and ".$genre." = '$option' and ktime < '$time' and jtime > '$time' and state = 1");//$genre三种模式:订单状态、物流跟踪、营销。启用状态下,有效期内搜索该店铺设置
		if($data)//如果可用模板配置
		{
			$message=2;
			$list['orderinfo'] = ($message==1)?substr($list['orderinfo'],1):$list['orderinfo'];
			if($message==1)
			{
				$expresscode = (isset($express['title']))?$express['title']:'';
			}
			else
			{
				$expresscode = (isset($express['aecode']))?$express['aecode']:'';
			}
			if(!isset($list['country']))
			{
				$list['waybill'] = '';
				$list['phone'] = '';
				$list['zipcode'] = '';
				$list['country'] = '';
				$list['province'] = '';
				$list['city'] = '';
				$list['address'] = '';
				$list['product'] = '';
			}
		    $express['url'] = (isset($express['url']))?$express['url']:'';
			$list['userName'] = (isset($list['userName']))?$list['userName']:'';
			$list['orderid'] = (isset($list['orderid']))?$list['orderid']:'';
			$list['name'] = (isset($list['name']))?$list['name']:'';
			$list['bname'] = (isset($list['bname']))?$list['bname']:'';
			$list['sname'] = (isset($list['sname']))?$list['sname']:'';
			$list['saddress'] = (isset($list['saddress']))?$list['saddress']:'';
			$list['baddress'] = (isset($list['baddress']))?$list['baddress']:'';
			$list['quantity'] = (isset($list['quantity']))?(($list['quantity'] > '1')?$list['quantity'].' packages of':''):'';
		    $list['parameter'] = (isset($list['parameter']))?$list['parameter']:'';
			$ProductSKU = (isset($list['issku']))?$list['issku']:$list['sku'];
			$yjtime = (isset($express['yjtime']))?$express['yjtime']:'';
			$product = '';
		if($list['product'] != '')
		{
			$list['product'] = str_replace("'","",$list['product']);
			if($list['link'] != '' && $list['source'] != '1')
			{
				$pt = explode(',',$list['product']);
				$lk = explode(',',$list['link']);
				for($i=0;$i'.$a.'':''.$a.'';
					}
					else
					{
					 	$product .= ($product != '')?' , '.$a:$a;
					}
				
				}
			}
		}
		$ProductName = '';
		$st =  explode(';',trim($list['fpdata'],';'));
		foreach ($st as $v) 
		{
			$v =  explode('|',trim($v,'|'));
			$th = $this->producttitle->get_title($v[1]);
			if($th)
			{
				$ProductName .= $th['th'].', ';
			}
			else
			{
				$ProductName .= $v[1].', ';
			}
		}
		$ProductName =  trim($ProductName,', ');
			$firstName = explode(' ',$list['name']);
		$t= array('$userName','$firstName','$orderid','$trackingNumber','$expressCompany','$expressWebsite','$contactPerson','$Bname','$Sname','$mobileNo','$zip','$country','$province','$city','$recipientAddress','$productInfo','$ShipAddress','$BillAddress','$Quantity','$parameter','$productTitle','$ProductName','$ProductSKU','$DeliveryTime',"[Smile]","[Shy]","[Tongue]","[Titter]","[Admire]","[Risus]","[Dance]","[Blow a Kiss]","[Comfort]","[Hug]","[Put on steam]","[Victory]","[Strong]","[Kiss]","[Anthomaniac]","[Grin]","[Seek]","[Call out]","[Cast accounts]","[Miser]","[Good idea]","[Grimace]","[Angel]","[Good bye]","[Drool]","[Enjoy]","[Erotomania]","[Dumbstruck]","[Thinking]","[Puzzle]","[Query]","[No money]","[Bored]","[Suspect]","[Hush]","[Complacent]","[Shake head]","[Cold]","[Awkward]","[Smirk]","[Amazed]","[However]","[Sweat]","[Loneliness]","[Sleepy]","[Faint]","[Distressed]","[Grievance]","[Sob]","[Cry]","[Wail]","[Obey]","[Sorry]","[Bye]","[Frown]","[So tired]","[Fall ill]","[Vomit]","[Fall on evil days]","[Surprised]","[Stunned]","[Shut up]","[Due to flat]","[Despise you]","[Rage]","[Angry]","[Mammon]","[Learn from lei feng]","[Wishing you prosperity]","[Waiter]","[Boss]","[Evil]","[Challenge]","[CS]","[Invisible man]","[Bomb]","[Scream]","[Beautiful girl]","[Handsome boy]","[Fortune cat]","[Deal]","[Handclap]","[Handshake]","[Lips]","[Rose]","[Wither]","[Heart]","[Heartbreak]","[Money]","[Shopping]","[Gift]","[Email]","[Phone]","[Cheers]","[Clock]","[Waiting]","[Late]","[Plane]","[Alipay]");//需要被替换的内容
		$h= array($list['name'],$firstName,$list['orderinfo'],$list['waybill'],$expresscode,$express['url'],$list['name'],$list['bname'],$list['sname'],$list['phone'],$list['zipcode'],$list['country'],$list['province'],$list['city'],$list['address'],$list['product'],$list['saddress'],$list['baddress'],$list['quantity'],$list['parameter'],$product,$ProductName,$ProductSKU,$yjtime,"/:000","/:001","/:002","/:003","/:004","/:005","/:006","/:007","/:008","/:009","/:010","/:011","/:012","/:013","/:014","/:015","/:016","/:017","/:018","/:019","/:020","/:021","/:022","/:023","/:024","/:025","/:026","/:027","/:028","/:029","/:030","/:031","/:032","/:033","/:034","/:035","/:036","/:037","/:038","/:039","/:040","/:041","/:042","/:043","/:044","/:045","/:046","/:047","/:048","/:049","/:050","/:051","/:052","/:053","/:054","/:055","/:056","/:057","/:058","/:059","/:060","/:061","/:062","/:063","/:064","/:065","/:066","/:067","/:068","/:069","/:070","/:071","/:072","/:073","/:074","/:075","/:076","/:077","/:078","/:079","/:080","/:081","/:082","/:083","/:084","/:085","/:086","/:087","/:088","/:089","/:090","/:091","/:092","/:093","/:094","/:095","/:096","/:097","/:098");//替换的内容
			$content = str_replace($t,$h,$data['content']);
				
			if($data['message'] == 1)//如果是发送邮件
			{
				if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$list['email']))
				{
					return '邮箱地址错误,请检查';exit;
				}
				$emailbt = ($data['emailtitle'] != '')?str_replace($t,$h,$data['emailtitle']):'The product you purchased has a new progress';
				$this->load->library('email');		
				$config['protocol'] = 'smtp';
				$config['smtp_host'] = 'smtpdm-ap-southeast-1.aliyun.com';
				$config['smtp_port'] = 465; 
				$config['smtp_user'] = 'service@email.supernovahair.com';
				$config['smtp_pass'] = 'LONGyihair374';
				$config['smtp_crypto'] = 'ssl';
				$config['crlf'] = "\r\n"; 
				$config['newline'] = "\r\n";
				$this->email->initialize($config);
				$this->email->set_newline("\r\n");
				$this->email->from('service@email.supernovahair.com',$shopname);
				$this->email->to($list['email']);//收件
				$this->email->subject($emailbt);//标题
				$this->email->message($content);//内容
				if ($this->email->send())
        		{
					return 1;//成功
        		}
				else
				{
					return '发送失败';
				}
			}
			else if($data['message'] == 2 && $list['userid'] != '')//如果是速卖通站内信,用户ID不为空
			{
				$ch = curl_init();
				$url = 'http://39.100.97.180/smtold.php';
				curl_setopt($ch,CURLOPT_URL,$url);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
				curl_setopt($ch,CURLOPT_HEADER,0);
				curl_setopt($ch,CURLOPT_POST, 1);
				curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,600);
        		//设置post数据
				$post = array();
				$post['shopid'] = $shop['brandname'];//速卖通店铺ID
				$post['userid'] = $list['userid'];//速卖通买家ID
				$post['message_type'] = 'order';//消息类型,product(商品)、member(会员,包含店铺)、order(订单)
				$post['content'] = $content;//发送内容
				$post['code'] = $shop['code'];
				$post['order'] = $list['orderinfo'];
        		curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));
				$res = curl_exec($ch);
				curl_close($ch);
				$res = json_decode($res,true);
   	   		 	if(isset($res['is_success']))
				{
					return 1;//成功
				}
				else
				{
					return isset($res['error_msg'])?$res['error_msg']:(isset($res['sub_msg'])?$res['sub_msg']:'发送失败');
				}
			}
			else
			{
				return '邮箱错误或者没有UserID';
			}
		}
		else
		{
			return '无可用站内信模板';
		}
	}
	
	public function get_god($list,$shop,$express,$notice)//单个订单整体模式
	{
		$list['orderinfo'] = ($notice['message']==1)?substr($list['orderinfo'],1):$list['orderinfo'];
		if($notice['message']==1)
		{
			$expresscode = (isset($express['title']))?$express['title']:'';
		}
		else
		{
			$expresscode = (isset($express['aecode']))?$express['aecode']:'';
		}
		if(!isset($list['country']))
		{
			$list['waybill'] = '';
			$list['phone'] = '';
			$list['zipcode'] = '';
			$list['country'] = '';
			$list['province'] = '';
			$list['city'] = '';
			$list['address'] = '';
			$list['product'] = '';
		}
		$express['url'] = (isset($express['url']))?$express['url']:'';
		$list['userName'] = (isset($list['userName']))?$list['userName']:'';
		$list['orderid'] = (isset($list['orderid']))?$list['orderid']:'';
		$list['name'] = (isset($list['name']))?$list['name']:'';
		$list['bname'] = (isset($list['bname']))?$list['bname']:'';
		$list['sname'] = (isset($list['sname']))?$list['sname']:'';
		$list['saddress'] = (isset($list['saddress']))?$list['saddress']:'';
		$list['baddress'] = (isset($list['baddress']))?$list['baddress']:'';
		$list['quantity'] = (isset($list['quantity']))?(($list['quantity'] > '1')?$list['quantity'].' packages of':''):'';
		$list['parameter'] = (isset($list['parameter']))?$list['parameter']:'';
		$list['waybill'] = (isset($list['waybill']))?$list['waybill']:'';
		$ProductSKU = (isset($list['issku']))?$list['issku']:$list['sku'];
		$yjtime = (isset($express['yjtime']))?$express['yjtime']:'';
		$product = '';
		if($list['product'] != '')
		{
			$list['product'] = str_replace("'","",$list['product']);
			if($list['link'] != '' && $list['source'] != '1')
			{
				$pt = explode(',',$list['product']);
				$lk = explode(',',$list['link']);
				for($i=0;$i
'.$a.'':''.$a.'';
					}
					else
					{
					 	$product .= ($product != '')?' , '.$a:$a;
					}
				
				}
			}
		}
		$ProductName = '';
		$st =  explode(';',trim($list['fpdata'],';'));
		foreach ($st as $v) 
		{
			$v =  explode('|',trim($v,'|'));
			if(isset($v[1]))
			{
			    $th = $this->producttitle->get_title($v[1]);
			    if($th)
			    {
				$ProductName .= $th['th'].', ';
				}
				else
				{
					$ProductName .= $v[1].', ';
				}
			}
		}
		$ProductName =  trim($ProductName,', ');
		$firstName = explode(' ',$list['name']);
		$t= array('$userName','$firstName','$orderid','$trackingNumber','$expressCompany','$expressWebsite','$contactPerson','$Bname','$Sname','$mobileNo','$zip','$country','$province','$city','$recipientAddress','$productInfo','$ShipAddress','$BillAddress','$Quantity','$parameter','$productTitle','$ProductName','$ProductSKU','$DeliveryTime',"[Smile]","[Shy]","[Tongue]","[Titter]","[Admire]","[Risus]","[Dance]","[Blow a Kiss]","[Comfort]","[Hug]","[Put on steam]","[Victory]","[Strong]","[Kiss]","[Anthomaniac]","[Grin]","[Seek]","[Call out]","[Cast accounts]","[Miser]","[Good idea]","[Grimace]","[Angel]","[Good bye]","[Drool]","[Enjoy]","[Erotomania]","[Dumbstruck]","[Thinking]","[Puzzle]","[Query]","[No money]","[Bored]","[Suspect]","[Hush]","[Complacent]","[Shake head]","[Cold]","[Awkward]","[Smirk]","[Amazed]","[However]","[Sweat]","[Loneliness]","[Sleepy]","[Faint]","[Distressed]","[Grievance]","[Sob]","[Cry]","[Wail]","[Obey]","[Sorry]","[Bye]","[Frown]","[So tired]","[Fall ill]","[Vomit]","[Fall on evil days]","[Surprised]","[Stunned]","[Shut up]","[Due to flat]","[Despise you]","[Rage]","[Angry]","[Mammon]","[Learn from lei feng]","[Wishing you prosperity]","[Waiter]","[Boss]","[Evil]","[Challenge]","[CS]","[Invisible man]","[Bomb]","[Scream]","[Beautiful girl]","[Handsome boy]","[Fortune cat]","[Deal]","[Handclap]","[Handshake]","[Lips]","[Rose]","[Wither]","[Heart]","[Heartbreak]","[Money]","[Shopping]","[Gift]","[Email]","[Phone]","[Cheers]","[Clock]","[Waiting]","[Late]","[Plane]","[Alipay]");//需要被替换的内容
		$h= array($list['name'],$firstName[0],$list['orderinfo'],$list['waybill'],$expresscode,$express['url'],$list['name'],$list['bname'],$list['sname'],$list['phone'],$list['zipcode'],$list['country'],$list['province'],$list['city'],$list['address'],$list['product'],$list['saddress'],$list['baddress'],$list['quantity'],$list['parameter'],$product,$ProductName,$ProductSKU,$yjtime,"/:000","/:001","/:002","/:003","/:004","/:005","/:006","/:007","/:008","/:009","/:010","/:011","/:012","/:013","/:014","/:015","/:016","/:017","/:018","/:019","/:020","/:021","/:022","/:023","/:024","/:025","/:026","/:027","/:028","/:029","/:030","/:031","/:032","/:033","/:034","/:035","/:036","/:037","/:038","/:039","/:040","/:041","/:042","/:043","/:044","/:045","/:046","/:047","/:048","/:049","/:050","/:051","/:052","/:053","/:054","/:055","/:056","/:057","/:058","/:059","/:060","/:061","/:062","/:063","/:064","/:065","/:066","/:067","/:068","/:069","/:070","/:071","/:072","/:073","/:074","/:075","/:076","/:077","/:078","/:079","/:080","/:081","/:082","/:083","/:084","/:085","/:086","/:087","/:088","/:089","/:090","/:091","/:092","/:093","/:094","/:095","/:096","/:097","/:098");//替换的内容
		$notice['content'] = str_replace($t,$h,$notice['content']);
		if($notice['emailtitle'] != '')
		{
			$notice['emailtitle'] = str_replace($t,$h,$notice['emailtitle']);
			$emailbt = $notice['emailtitle'];
		}
		else
		{
			$emailbt = $shop['brandname'].' Hair Service- Order Confirmation #'.$list['orderinfo'];
		}
		if($notice['message'] == 1)//如果是发送邮件
		{
			if(!$notice['email']['smtp_host'] || !$notice['email']['smtp_user'] || !$notice['email']['smtp_pass'])
			{
				return '此店铺没有配置邮件服务器信息!';exit;
			}
			if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$list['email']))
			{
				return '邮箱地址错误,请检查';exit;
			}
			if($notice['email']['smtp_host'] == 'smtp.gmail.com')
			{
				$ch = curl_init();
				$url = 'http://e.wepolicy.cn/';
				curl_setopt($ch,CURLOPT_URL,$url);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
				curl_setopt($ch,CURLOPT_HEADER,0);
				curl_setopt($ch,CURLOPT_POST, 1);
				curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,600);
        		//设置post数据
				$post = array();
				$post['notice'] = $notice;
				$post['list'] = $list;
				$post['shop'] = $order;
				$post['emailbt'] = $emailbt;
        		curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));
				$res = curl_exec($ch);
				curl_close($ch);
				$res = json_decode($res,true);
   	   		 	if($res)
				{
					return $res;//成功
				}
				else
				{
					return '发送失败';
				}
			}
			else
			{
			    $this->load->library('email');		
			    $config['protocol'] = $notice['email']['protocol'];
			    $config['smtp_host'] = $notice['email']['smtp_host'];
			    $config['smtp_port'] = $notice['email']['smtp_port'];
			    $config['smtp_timeout']='30';
			    $config['smtp_user'] = $notice['email']['smtp_user'];
			    $config['smtp_pass'] = $notice['email']['smtp_pass'];
			    $config['smtp_crypto'] = $notice['email']['smtp_crypto'];
			    $config['crlf'] = "\r\n"; 
			    $config['newline'] = "\r\n";
			    if(stripos($notice['content'],'
','
','
email->initialize($config); $this->email->set_newline("\r\n"); $this->email->from($notice['email']['smtp_user'],$shop['shopname']);//发送方 $this->email->to($list['email']);//收件 $this->email->subject($emailbt);//标题 $this->email->message($notice['content']);//内容 if ($this->email->send()) { return 1; } else { return $this->email->print_debugger(array('headers')); } } } else if($notice['message'] == 2 && $list['userid'] != '')//如果是速卖通站内信,用户ID不为空 { /** $ch = curl_init(); $url = 'http://39.100.97.180/smtold.php'; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_HEADER,0); curl_setopt($ch,CURLOPT_POST, 1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,600); //设置post数据 $post = array(); $post['shopid'] = $shop['brandname'];//速卖通店铺ID $post['userid'] = $list['userid'];//速卖通买家ID $post['message_type'] = 'order';//消息类型,product(商品)、member(会员,包含店铺)、order(订单) $post['content'] = $notice['content'];//发送内容 $post['code'] = $shop['code']; $post['order'] = $list['orderinfo']; curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post)); $res = curl_exec($ch); curl_close($ch); $res = json_decode($res,true); if(isset($res['is_success'])) { return 1;//成功 } else { return $res; } **/ } else if($notice['message'] == 3 && $list['user_id'] != '')//如果是TT会话,用户ID不为空 { $url = 'https://open-api.tiktokglobalshop.com'; $link = '/customer_service/202309/conversations'; $time = time(); $post['buyer_user_id'] = $list['user_id']; $queryParams['shop_cipher'] =$shop['shop_cipher']; //$queryParams['shop_id'] =$shop['codeid']; $queryParams['app_key'] =$shop['app_key']; $queryParams['timestamp'] =$time; $link .= '?'.http_build_query($queryParams); $sign = $this->apitt->sign($link,$shop['app_secret'],$post); $url .= $link.'&sign='.$sign; $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']); $res = $this->apitt->su_curl($post,$url,$headers); $res = json_decode($res,true); if(isset($res['data']['conversation_id'])) { $f = $this->_ttfs($shop,$notice['content'],$res['data']['conversation_id']); if(isset($f['data']['message_id'])) { return 1;//成功 } else { return isset($f['message'])?'B:'.$f['message']:'发送失败'; } } else { return isset($res['message'])?'A:'.$res['message']:'发送失败'; } } else { return '无可用发送平台!'; } } public function _ttfs($shop,$notice,$conversation_id)//TT发送信息 { $url = 'https://open-api.tiktokglobalshop.com'; $link = '/customer_service/202309/conversations/'.$conversation_id.'/messages'; $time = time(); $notice = str_replace('
','\n',$notice); $post['type'] = 'TEXT'; $post['content'] = '{"content": "'.strip_tags($notice).'"}'; //$queryParams['access_token'] = $shop['token']; $queryParams['shop_cipher'] = $shop['shop_cipher']; $queryParams['shop_id'] = $shop['codeid']; $queryParams['app_key'] = $shop['app_key']; $queryParams['version'] = '202309'; $queryParams['timestamp'] =$time; $link .= '?'.http_build_query($queryParams); $sign = $this->apitt->sign($link,$shop['app_secret'],$post); $url .= $link.'&sign='.$sign; $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']); $res = $this->apitt->su_curl($post,$url,$headers); $res = json_decode($res,true); return $res; } public function get_smtone($list,$shop,$content)//单个订单非快递信息模式模式 { if(!isset($list['country'])) { $list['waybill'] = ''; $list['phone'] = ''; $list['zipcode'] = ''; $list['country'] = ''; $list['province'] = ''; $list['city'] = ''; $list['address'] = ''; $list['product'] = ''; } $expresscode = (isset($express['aecode']))?$express['aecode']:''; $express['url'] = (isset($express['url']))?$express['url']:''; $list['userName'] = (isset($list['userName']))?$list['userName']:''; $list['orderid'] = (isset($list['orderid']))?$list['orderid']:''; $list['name'] = (isset($list['name']))?$list['name']:''; $list['bname'] = (isset($list['bname']))?$list['bname']:''; $list['sname'] = (isset($list['sname']))?$list['sname']:''; $product = ''; if($list['product'] != '') { $list['product'] = str_replace("'","",$list['product']); if($list['link'] != '' && $list['source'] != '1') { $pt = explode(',',$list['product']); $lk = explode(',',$list['link']); for($i=0;$i