Shop.php 15 KB

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