Shop.php 14 KB

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