Shop.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Shop extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_user','user');
  7. $this->load->_model('Model_shop','shop');
  8. $this->load->_model('Model_typeclass','typeclass');
  9. $this->load->_model('Model_warehouse','warehouse');
  10. $this->load->_model('Model_productprice','productprice');
  11. $this->load->_model('Model_apiyy','apiyy');
  12. }
  13. //定义方法的调用规则 获取URI第二段值
  14. public function _remap($arg,$arg_array)
  15. {
  16. if($arg == 'add')//添加
  17. {
  18. $this->_add();
  19. }
  20. else if($arg == 'edit')//修改
  21. {
  22. $this->_edit($arg_array);
  23. }
  24. else if($arg == 'del')//修改
  25. {
  26. $this->_del();
  27. }
  28. else if($arg == 'rows')//修改
  29. {
  30. $this->_rows();
  31. }
  32. else if($arg == 'code')//修改
  33. {
  34. $this->_code($arg_array);
  35. }
  36. else
  37. {
  38. $this->_index();
  39. }
  40. }
  41. //管理
  42. public function _index()
  43. {
  44. $post = $this->input->post(NULL, TRUE);
  45. if(isset($post['page']))
  46. {
  47. $page = $this->input->post('page',true);
  48. $perpage = $this->input->post('perpage',true);
  49. $shopname = $this->input->post('shopname',true);
  50. $type = $this->input->post('type',true);
  51. $status = $this->input->post('status',true);
  52. $where = "1=1 ";
  53. //数据排序
  54. $order_str = "id asc";
  55. if($shopname)
  56. {
  57. $where .= " and shopname like '%$shopname%'";
  58. }
  59. if(!empty($type)){
  60. $where .= " and type = ".$type." ";
  61. }
  62. if($status == 0 || $status == 1){
  63. $where .= " and status = ".$status." ";
  64. }
  65. if(empty($page))
  66. {
  67. $start = 0;
  68. $perpage = 1;
  69. }
  70. else
  71. {
  72. $start = ($page - 1)*$perpage;
  73. }
  74. //取得信息列表
  75. $info_list = $this->shop->find_all($where,'id,type,shopname,shortname,estimaterate,mustba,shopid,yyid,tb,status',$order_str,$start,$perpage);
  76. //格式化数据
  77. foreach ($info_list as $key=>$value)
  78. {
  79. $type = $this->typeclass->read($value['type']);
  80. $info_list[$key]['type'] = empty($type['title'])?"":$type['title'];
  81. if($value['mustba'] == 1)
  82. {
  83. $info_list[$key]['mustba'] = '需要审核';
  84. }
  85. else
  86. {
  87. $info_list[$key]['mustba'] = '不审核';
  88. }
  89. if($value['tb'] == 1)
  90. {
  91. $info_list[$key]['tb'] = '同步';
  92. }
  93. else
  94. {
  95. $info_list[$key]['tb'] = '不同步';
  96. }
  97. if($value['status'] == 0){
  98. $info_list[$key]['status'] = 'active';
  99. }else{
  100. $info_list[$key]['status'] = 'inactive';
  101. }
  102. }
  103. $total = $this->shop->find_count($where);
  104. $pagenum = ceil($total/$perpage);
  105. $over = $total-($start+$perpage);
  106. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  107. echo json_encode($rows);exit;
  108. }
  109. $this->_Template('shop',$this->data);
  110. }
  111. //添加
  112. public function _add()
  113. {
  114. $post = $this->input->post(NULL, TRUE);
  115. if(isset($post['shopid']))
  116. {
  117. $post['type'] = $this->input->post('type',true);
  118. $post['notice'] = $this->input->post('notice',true);
  119. $post['shopid'] = $this->input->post('shopid',true);
  120. $post['shopname'] = $this->input->post('shopname',true);
  121. $post['shortname'] = $this->input->post('shortname',true);
  122. $post['shopuser'] = $this->input->post('shopuser',true);
  123. $post['shopphone'] = $this->input->post('shopphone',true);
  124. $post['brandname'] = $this->input->post('brandname',true);
  125. /**
  126. $post['companyname'] = $this->input->post('companyname',true);
  127. $post['country'] = $this->input->post('country',true);
  128. $post['province'] = $this->input->post('province',true);
  129. $post['city'] = $this->input->post('city',true);
  130. $post['zip'] = $this->input->post('zip',true);
  131. $post['adress'] = $this->input->post('adress',true);
  132. $post['defaultpricetype'] = $this->input->post('defaultpricetype',true);
  133. $post['updcycle'] = $this->input->post('updcycle',true);
  134. $post['taxrate'] = $this->input->post('taxrate',true);
  135. $post['uphone'] = $this->input->post('uphone',true);
  136. **/
  137. $post['estimaterate'] = $this->input->post('estimaterate',true);
  138. $post['userexp'] = $this->input->post('userexp',true);
  139. $post['mustba'] = $this->input->post('mustba',true);
  140. $post['shopcost'] = $this->input->post('shopcost',true);
  141. $post['shopcosttext'] = $this->input->post('shopcosttext',true);
  142. $post['shopwh'] = $this->input->post('shopwh',true);
  143. $post['shopwhtext'] = $this->input->post('shopwhtext',true);
  144. $post['edittime'] = time();
  145. $post['addtime'] = time();
  146. $px = $this->shop->find_all("1=1",'*','px desc');
  147. $post['px'] = $px[0]['px']+10;
  148. $s = $post;
  149. $s['id'] = $post['yyid'];
  150. $s['name'] = $post['shortname'];
  151. if($post['type'] == '1514')
  152. {
  153. $z = '跨境';
  154. }
  155. else if($post['type'] == '269')
  156. {
  157. $z = '独-';
  158. }
  159. else if($post['type'] == '1514')
  160. {
  161. $z = '速-';
  162. }
  163. $tc = $this->apiyy->get_cjkh($s);
  164. if(isset($tc['Data'][0]))
  165. {
  166. }
  167. else
  168. {
  169. echo json_encode(array('msg'=>$tc['Data'][0]['ErrorMsg'],'success'=>false));exit;
  170. }
  171. if($this->shop->insert($post))
  172. {
  173. echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
  174. }
  175. else
  176. {
  177. echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
  178. }
  179. }
  180. $this->_Template('shop_add',$this->data);
  181. }
  182. //修改
  183. public function _edit($arg_array)
  184. {
  185. $post = $this->input->post(NULL, TRUE);
  186. if(isset($post['id']))
  187. {
  188. $id = $this->input->post('id',true);
  189. $ps = $this->shop->read($id);
  190. $post['type'] = $this->input->post('type',true);
  191. $post['notice'] = $this->input->post('notice',true);
  192. $post['shopid'] = $this->input->post('shopid',true);
  193. $post['shopname'] = $this->input->post('shopname',true);
  194. $post['shortname'] = $this->input->post('shortname',true);
  195. $post['shopuser'] = $this->input->post('shopuser',true);
  196. $post['shopphone'] = $this->input->post('shopphone',true);
  197. $post['brandname'] = $this->input->post('brandname',true);
  198. /**
  199. $post['companyname'] = $this->input->post('companyname',true);
  200. $post['country'] = $this->input->post('country',true);
  201. $post['province'] = $this->input->post('province',true);
  202. $post['city'] = $this->input->post('city',true);
  203. $post['zip'] = $this->input->post('zip',true);
  204. $post['adress'] = $this->input->post('adress',true);
  205. $post['defaultpricetype'] = $this->input->post('defaultpricetype',true);
  206. $post['updcycle'] = $this->input->post('updcycle',true);
  207. $post['taxrate'] = $this->input->post('taxrate',true);
  208. $post['uphone'] = $this->input->post('uphone',true);
  209. **/
  210. $post['estimaterate'] = $this->input->post('estimaterate',true);
  211. $post['userexp'] = $this->input->post('userexp',true);
  212. $post['mustba'] = $this->input->post('mustba',true);
  213. $post['shopcost'] = $this->input->post('shopcost',true);
  214. $post['shopcosttext'] = $this->input->post('shopcosttext',true);
  215. $post['shopwh'] = $this->input->post('shopwh',true);
  216. $post['shopwhtext'] = $this->input->post('shopwhtext',true);
  217. $post['edittime'] = time();
  218. if($this->shop->save($post,$id))
  219. {
  220. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  221. }
  222. else
  223. {
  224. echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
  225. }
  226. }
  227. $arg_array = $arg_array[0];
  228. $shop = $this->shop->read($arg_array);
  229. $this->data['shop'] = $shop;
  230. $this->_Template('shop_edit',$this->data);
  231. }
  232. //删除
  233. public function _del()
  234. {
  235. $post = $this->input->post(NULL, TRUE);
  236. if(isset($post['s']))
  237. {
  238. $id_arr = $this->input->post('s');
  239. $id_arr = explode(',',$id_arr);
  240. if(!$id_arr)
  241. {
  242. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  243. }
  244. //循环删除记录
  245. foreach ($id_arr as $v)
  246. {
  247. $this->shop->remove($v);
  248. }
  249. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  250. }
  251. }
  252. //数据
  253. public function _rows()
  254. {
  255. $post = $this->input->post(NULL, TRUE);
  256. if(isset($post['shopcost']))
  257. {
  258. $productprice = $this->input->post('shopcost',true);
  259. $data = $this->productprice->find_all('1=1');
  260. $list = array();
  261. foreach ($data as $key=>$value)
  262. {
  263. $list[] = array('id'=>$value['id'],'title'=>$value['name']);
  264. }
  265. $num = array();
  266. if($productprice != "null")
  267. {
  268. $num = $this->shop->read($productprice);//找出内容
  269. if($num['shopcost'])
  270. {
  271. $num = explode('|',trim($num['shopcost'],'|'));//数组化内容
  272. }
  273. else
  274. {
  275. $num = array();
  276. }
  277. }
  278. echo json_encode(array('msg'=>($list),'num'=>($num),'success'=>true));
  279. }
  280. if(isset($post['shopwh']))
  281. {
  282. $warehouse = $this->input->post('shopwh',true);
  283. $data = $this->warehouse->find_all('1=1');
  284. $list = array();
  285. foreach ($data as $key=>$value)
  286. {
  287. $list[] = array('id'=>$value['id'],'title'=>$value['title']);
  288. }
  289. $num = array();
  290. if($warehouse != "null")
  291. {
  292. $num = $this->shop->read($warehouse);//找出内容
  293. if($num['shopwh'])
  294. {
  295. $num = explode('|',trim($num['shopwh'],'|'));//数组化内容
  296. }
  297. else
  298. {
  299. $num = array();
  300. }
  301. }
  302. echo json_encode(array('msg'=>($list),'num'=>($num),'success'=>true));
  303. }
  304. }
  305. public function _code($arg_array)
  306. {
  307. $shop = $this->shop->read($arg_array[0]);
  308. if($shop['type'] == 1514)
  309. {
  310. header('Location:https://services.us.tiktokshop.com/open/authorize?service_id='.$shop['codeid']);exit;
  311. }
  312. else
  313. {
  314. $setting['appkey'] = '26004389';
  315. $setting['secret'] = 'd880d725c67b449c8a601e9b0766955d';
  316. $setting['code'] = 'https://oauth.aliexpress.com/authorize';
  317. $setting['token'] = 'https://oauth.aliexpress.com/token';
  318. if(!$_GET['code'])
  319. {
  320. 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;
  321. }
  322. $code = $_GET['code'];
  323. $time = (int)time()."000";
  324. $params = [
  325. "app_key" =>$setting['appkey'],
  326. "code" => $code,
  327. "sign_method" =>"sha256",
  328. "simplify" => "true",
  329. "timestamp" => $time
  330. ];
  331. $sign = $this->signApiRequest($params, $setting['secret'], 'sha256','/auth/token/create');
  332. $url = "http://api-sg.aliexpress.com/rest/auth/token/create";
  333. $params['sign'] = $sign;
  334. echo "<pre>";
  335. print_r($params);
  336. $headerA = [
  337. "Content-Type: application/json;;charset=utf-8",
  338. ];
  339. $ch = curl_init();
  340. curl_setopt($ch, CURLOPT_URL, $url);
  341. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  342. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerA);
  343. curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
  344. curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
  345. //指定post数据
  346. curl_setopt($ch, CURLOPT_POST, true);
  347. //添加变量
  348. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)) ;
  349. $output = curl_exec($ch);
  350. $output = json_decode($output,true);
  351. print_r($output);
  352. // $code = $_GET['code'];
  353. // $url = $setting['token'];
  354. // $postfields= array('grant_type'=>'authorization_code',
  355. // 'client_id'=>$setting['appkey'],
  356. // 'client_secret'=>$setting['secret'],
  357. // 'code'=>$_GET['code'],
  358. // 'sp'=>'ae',
  359. // 'redirect_uri'=>'http://'.$_SERVER['HTTP_HOST'].'/shop/code');
  360. // $post_data = '';
  361. // foreach($postfields as $key=>$value){
  362. // $post_data .="$key=".urlencode($value)."&";}
  363. // $ch = curl_init();
  364. // curl_setopt($ch, CURLOPT_URL, $url);
  365. // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  366. // curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
  367. // curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
  368. // //指定post数据
  369. // curl_setopt($ch, CURLOPT_POST, true);
  370. // //添加变量
  371. // curl_setopt($ch, CURLOPT_POSTFIELDS, substr($post_data,0,-1));
  372. // $output = curl_exec($ch);
  373. // $output = json_decode($output,true);
  374. // print_r($output);
  375. // $this->shop->save(array('code'=>$output["access_token"],'codetime'=>substr($output["expire_time"],0,10)),$_GET['state']);
  376. // header('Location:http://'.$_SERVER['HTTP_HOST'].'/user/');exit;
  377. }
  378. }
  379. public function signApiRequest($params, $appSecret, $signMethod, $apiName) {
  380. // 对参数键名进行排序
  381. $keys = array_keys($params);
  382. sort($keys);
  383. // 拼接基础字符串:先添加 API 名称
  384. $query = $apiName;
  385. // 遍历排序后的键值对
  386. foreach ($keys as $key) {
  387. $value = $params[$key];
  388. $query .= $key . $value;
  389. }
  390. // 使用指定方法签名
  391. $bytes = null;
  392. if ($signMethod === 'sha256') {
  393. $bytes = $this->encryptHMACSHA256($query, $appSecret);
  394. }
  395. // 转换为大写HEX字符串
  396. return $this->byte2hex($bytes);
  397. }
  398. private function encryptHMACSHA256($data, $secret) {
  399. var_dump($data);
  400. return hash_hmac('sha256', $data, $secret, true);
  401. }
  402. private function byte2hex($bytes) {
  403. $hex = bin2hex($bytes);
  404. return strtoupper($hex);
  405. }
  406. }