Shop.php 14 KB

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