| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 | <?php defined('BASEPATH') OR exit('No direct script access allowed');class Shop extends Start_Controller {	public function __construct(){		parent::__construct();		$this->load->library('session');		$this->load->_model('Model_user','user');		$this->load->_model('Model_shop','shop');		$this->load->_model('Model_typeclass','typeclass');		$this->load->_model('Model_warehouse','warehouse');		$this->load->_model('Model_productprice','productprice');		$this->load->_model('Model_apiyy','apiyy');	}	//定义方法的调用规则 获取URI第二段值    public function _remap($arg,$arg_array)    {		if($arg == 'add')//添加        {             $this->_add();        }		else if($arg == 'edit')//修改        {             $this->_edit($arg_array);        }		else if($arg == 'del')//修改        {             $this->_del();        }		else if($arg == 'rows')//修改        {             $this->_rows();        }		else if($arg == 'pxsort')//修改        {             $this->_pxsort();        }		else if($arg == 'code')//修改        {             $this->_code($arg_array);        }		else		{			 $this->_index();		}    }	//管理	public function _index()	{		$post = $this->input->post(NULL, TRUE);		if(isset($post['page']))  		{		    $page = $this->input->post('page',true);		    $perpage = $this->input->post('perpage',true);			$shopname = $this->input->post('shopname',true);			$type = $this->input->post('type',true);			$status = $this->input->post('status',true);			$where = "1=1 ";            //数据排序            $order_str = "px asc";			if($shopname)            {                $where  .= " and shopname like '%$shopname%'";            }			if(!empty($type)){				$where  .= " and type = ".$type." ";			}			if($status == 0 || $status == 1){				$where  .= " and status = ".$status." ";			}            if(empty($page))		    {                $start = 0;		    	$perpage = 1;            }		    else		    {                $start = ($page - 1)*$perpage;            }            //取得信息列表            $info_list = $this->shop->find_all($where,'id,type,program,shopname,shortname,estimaterate,mustba,shopid,yyid,tb,status,px,remarks',$order_str,$start,$perpage);			 //格式化数据            foreach ($info_list as $key=>$value) 		    {				$type = $this->typeclass->read($value['type']);								$info_list[$key]['type'] = empty($type['title'])?"":$type['title'];				if($value['mustba'] == 1)				{					$info_list[$key]['mustba'] = '需要审核';				}				else				{					$info_list[$key]['mustba'] = '不审核';				}				if($value['tb'] == 1)				{					$info_list[$key]['tb'] = '同步';				}				else				{					$info_list[$key]['tb'] = '不同步';				}				$info_list[$key]['remarks'] = empty($value['remarks'])?"":$value['remarks'];				if($value['status'] == 0){					$info_list[$key]['status'] = 'active';				}else{					$info_list[$key]['status'] = 'inactive';				}				$info_list[$key]['px'] = "<input value=".$value['px']." onchange='edit_px(this)' data-id= '".$value['id']."' class='px_do' />";            }		    $total = $this->shop->find_count($where);		    $pagenum = ceil($total/$perpage);		    $over = $total-($start+$perpage);		    $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));		    echo json_encode($rows);exit;		}		$this->_Template('shop',$this->data);	}	//添加	public function _add()	{		$post = $this->input->post(NULL, TRUE);		if(isset($post['shopid']))		{			$post['type'] = $this->input->post('type',true);			$post['notice'] = $this->input->post('notice',true);			$post['shopid'] = $this->input->post('shopid',true);			$post['shopname'] = $this->input->post('shopname',true);			$post['shortname'] = $this->input->post('shortname',true);			$post['shopuser'] = $this->input->post('shopuser',true);			$post['shopphone'] = $this->input->post('shopphone',true);			$post['brandname'] = $this->input->post('brandname',true);			/**			$post['companyname'] = $this->input->post('companyname',true);			$post['country'] = $this->input->post('country',true);			$post['province'] = $this->input->post('province',true);			$post['city'] = $this->input->post('city',true);			$post['zip'] = $this->input->post('zip',true);			$post['adress'] = $this->input->post('adress',true);			$post['defaultpricetype'] = $this->input->post('defaultpricetype',true);			$post['updcycle'] = $this->input->post('updcycle',true);			$post['taxrate'] = $this->input->post('taxrate',true);			$post['uphone'] = $this->input->post('uphone',true);			**/			$post['estimaterate'] = $this->input->post('estimaterate',true);			$post['userexp'] = $this->input->post('userexp',true);			$post['mustba'] = $this->input->post('mustba',true);			$post['shopcost'] = $this->input->post('shopcost',true);			$post['shopcosttext'] = $this->input->post('shopcosttext',true);			$post['shopwh'] = $this->input->post('shopwh',true);			$post['shopwhtext'] = $this->input->post('shopwhtext',true);			$post['edittime'] = time();			$post['addtime'] = time();			$post['px'] = $this->input->post('px',true);			$post['remarks'] = $this->input->post('remarks',true);			$s = $post;			$s['id'] = $post['yyid'];			$s['name'] = $post['shortname'];			if($post['type'] == '1514')			{				$z = '跨境';			}			else if($post['type'] == '269')			{				$z = '独-';			}			else if($post['type'] == '1514')			{				$z = '速-';			}			$tc = $this->apiyy->get_cjkh($s);			if(isset($tc['Data'][0]))			{							}			else			{				echo json_encode(array('msg'=>$tc['Data'][0]['ErrorMsg'],'success'=>false));exit;			}			if($this->shop->insert($post))        	{         		echo json_encode(array('msg'=>'添加成功','success'=>true));exit;       	 	}       		else        	{           		echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;        	}		}		$this->_Template('shop_add',$this->data);	}	//修改	public function _edit($arg_array)	{		$post = $this->input->post(NULL, TRUE);		if(isset($post['id']))		{			$id = $this->input->post('id',true);			$ps = $this->shop->read($id);			$post['type'] = $this->input->post('type',true);			$post['notice'] = $this->input->post('notice',true);			$post['shopid'] = $this->input->post('shopid',true);			$post['shopname'] = $this->input->post('shopname',true);			$post['shortname'] = $this->input->post('shortname',true);			$post['shopuser'] = $this->input->post('shopuser',true);			$post['shopphone'] = $this->input->post('shopphone',true);			$post['brandname'] = $this->input->post('brandname',true);			/**			$post['companyname'] = $this->input->post('companyname',true);			$post['country'] = $this->input->post('country',true);			$post['province'] = $this->input->post('province',true);			$post['city'] = $this->input->post('city',true);			$post['zip'] = $this->input->post('zip',true);			$post['adress'] = $this->input->post('adress',true);			$post['defaultpricetype'] = $this->input->post('defaultpricetype',true);			$post['updcycle'] = $this->input->post('updcycle',true);			$post['taxrate'] = $this->input->post('taxrate',true);			$post['uphone'] = $this->input->post('uphone',true);			**/			$post['px'] = $this->input->post('px',true);			$post['remarks'] = $this->input->post('remarks',true);			$post['estimaterate'] = $this->input->post('estimaterate',true);			$post['userexp'] = $this->input->post('userexp',true);			$post['mustba'] = $this->input->post('mustba',true);			$post['shopcost'] = $this->input->post('shopcost',true);			$post['shopcosttext'] = $this->input->post('shopcosttext',true);			$post['shopwh'] = $this->input->post('shopwh',true);			$post['shopwhtext'] = $this->input->post('shopwhtext',true);			$post['edittime'] = time();			if($this->shop->save($post,$id))        	{         		echo json_encode(array('msg'=>'修改成功','success'=>true));exit;       	 	}       		else        	{           		echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;        	}		}		$arg_array = $arg_array[0];		$shop = $this->shop->read($arg_array);		$this->data['shop'] = $shop;		$this->_Template('shop_edit',$this->data);	}	//删除	public function _del()	{		$post = $this->input->post(NULL, TRUE);		if(isset($post['s']))  		{            $id_arr = $this->input->post('s');            $id_arr =  explode(',',$id_arr);            if(!$id_arr)            {                echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;            }            //循环删除记录            foreach ($id_arr as $v) 		    {                $this->shop->remove($v);            }            echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));		}    }	//数据	public function _rows()	{		$post = $this->input->post(NULL, TRUE);		if(isset($post['shopcost']))  		{			$productprice = $this->input->post('shopcost',true);            $data = $this->productprice->find_all('1=1');			$list = array();			foreach ($data as $key=>$value) 		    {				$list[] = array('id'=>$value['id'],'title'=>$value['name']);            }			$num = array();			if($productprice != "null")			{			    $num = $this->shop->read($productprice);//找出内容				if($num['shopcost'])				{			        $num = explode('|',trim($num['shopcost'],'|'));//数组化内容				}				else				{					$num = array();				}			}		    echo json_encode(array('msg'=>($list),'num'=>($num),'success'=>true));		}		if(isset($post['shopwh']))  		{			$warehouse = $this->input->post('shopwh',true);            $data = $this->warehouse->find_all('1=1');			$list = array();			foreach ($data as $key=>$value) 		    {				$list[] = array('id'=>$value['id'],'title'=>$value['title']);            }			$num = array();			if($warehouse != "null")			{			    $num = $this->shop->read($warehouse);//找出内容				if($num['shopwh'])				{			        $num = explode('|',trim($num['shopwh'],'|'));//数组化内容				}				else				{					$num = array();				}			}		    echo json_encode(array('msg'=>($list),'num'=>($num),'success'=>true));		}    }		public function _code($arg_array)	{				if(isset($arg_array[0])){			$shop = $this->shop->read($arg_array[0]);		}else{			$shop = [				'type'=>1			];		}		if($shop['type'] == 1514)		{			header('Location:https://services.us.tiktokshop.com/open/authorize?service_id='.$shop['codeid']);exit;		}		else		{			$setting['appkey'] = '26004389';			$setting['secret'] = 'd880d725c67b449c8a601e9b0766955d';			$setting['code'] = 'https://api-sg.aliexpress.com/oauth/authorize';			//$setting['code'] = 'https://oauth.aliexpress.com/authorize';			$setting['token'] = 'https://oauth.aliexpress.com/token';			if(!$_GET['code'])			{				header('Location:'.$setting['code'].'?response_type=code&force_auth=true&client_id='.$setting['appkey'].'&redirect_uri='.'http://'.$_SERVER['HTTP_HOST'].'/shop/code'.'&state='.$arg_array[0]);exit;				//header('Location:'.$setting['code'].'?response_type=code&client_id='.$setting['appkey'].'&redirect_uri='.'http://'.$_SERVER['HTTP_HOST'].'/shop/code'.'&state='.$arg_array[0].'&view=web&sp=ae');exit;			}			$code = $_GET['code'];			$time = (int)time()."000";			$params = [				"app_key" =>$setting['appkey'],				"code" => $code,				"sign_method" =>"sha256",				"simplify" => "true",				"timestamp" => $time			];			$sign = $this->signApiRequest($params, $setting['secret'], 'sha256','/auth/token/create');			$url = "http://api-sg.aliexpress.com/rest/auth/token/create";						$params['sign'] = $sign;			echo "<pre>";			print_r($params);						$headerA = [				"Content-Type: application/json;;charset=utf-8",			];			$ch = curl_init();			curl_setopt($ch, CURLOPT_URL, $url);			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);			curl_setopt($ch, CURLOPT_HTTPHEADER, $headerA);			curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);			curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);			//指定post数据			curl_setopt($ch, CURLOPT_POST, true);			//添加变量        	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params))	;			$output = curl_exec($ch);			$output = json_decode($output,true);			print_r($output);			// $code = $_GET['code'];			// $url = $setting['token'];			// $postfields= array('grant_type'=>'authorization_code',			// 'client_id'=>$setting['appkey'],			// 'client_secret'=>$setting['secret'],			// 'code'=>$_GET['code'],			// 'sp'=>'ae',			// 'redirect_uri'=>'http://'.$_SERVER['HTTP_HOST'].'/shop/code');			// $post_data = '';			// foreach($postfields as $key=>$value){			// $post_data .="$key=".urlencode($value)."&";}			// $ch = curl_init();			// curl_setopt($ch, CURLOPT_URL, $url);			// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);			// curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);			// curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);			// //指定post数据			// curl_setopt($ch, CURLOPT_POST, true);			// //添加变量        	// curl_setopt($ch, CURLOPT_POSTFIELDS, substr($post_data,0,-1));			// $output = curl_exec($ch);			// $output = json_decode($output,true);			// print_r($output);			// $this->shop->save(array('code'=>$output["access_token"],'codetime'=>substr($output["expire_time"],0,10)),$_GET['state']);			// header('Location:http://'.$_SERVER['HTTP_HOST'].'/user/');exit;		}	}	public  function signApiRequest($params, $appSecret, $signMethod, $apiName) {        // 对参数键名进行排序        $keys = array_keys($params);        sort($keys);                // 拼接基础字符串:先添加 API 名称        $query = $apiName;                // 遍历排序后的键值对        foreach ($keys as $key) {            $value = $params[$key];            $query .= $key . $value;        }        // 使用指定方法签名        $bytes = null;        if ($signMethod === 'sha256') {            $bytes = $this->encryptHMACSHA256($query, $appSecret);        }        // 转换为大写HEX字符串        return $this->byte2hex($bytes);    }    private  function encryptHMACSHA256($data, $secret) {		var_dump($data);        return hash_hmac('sha256', $data, $secret, true);    }    private  function byte2hex($bytes) {        $hex = bin2hex($bytes);        return strtoupper($hex);    }		public function _pxsort(){		$user = $this->user->get_api($_SESSION['api']);		if(empty($user)){			die;		}		$id = $this->input->post('id', TRUE);		$px = $this->input->post('px', TRUE);		$this->shop->save(array('px'=>$px),$id);		echo json_encode(array('code'=>0,'msg'=>'修改成功'));exit;	}}
 |