Shop.php 14 KB

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