Shop.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. $where = "1=1 ";
  51. //数据排序
  52. $order_str = "id asc";
  53. if($shopname)
  54. {
  55. $where .= " and shopname like '%$shopname%'";
  56. }
  57. if(empty($page))
  58. {
  59. $start = 0;
  60. $perpage = 1;
  61. }
  62. else
  63. {
  64. $start = ($page - 1)*$perpage;
  65. }
  66. //取得信息列表
  67. $info_list = $this->shop->find_all($where,'id,type,shopname,shortname,estimaterate,mustba,shopid,yyid,tb,status',$order_str,$start,$perpage);
  68. //格式化数据
  69. foreach ($info_list as $key=>$value)
  70. {
  71. $type = $this->typeclass->read($value['type']);
  72. $info_list[$key]['type'] = empty($type['title'])?"":$type['title'];
  73. if($value['mustba'] == 1)
  74. {
  75. $info_list[$key]['mustba'] = '需要审核';
  76. }
  77. else
  78. {
  79. $info_list[$key]['mustba'] = '不审核';
  80. }
  81. if($value['tb'] == 1)
  82. {
  83. $info_list[$key]['tb'] = '同步';
  84. }
  85. else
  86. {
  87. $info_list[$key]['tb'] = '不同步';
  88. }
  89. if($value['status'] == 0){
  90. $info_list[$key]['status'] = 'active';
  91. }else{
  92. $info_list[$key]['status'] = 'inactive';
  93. }
  94. }
  95. $total = $this->shop->find_count($where);
  96. $pagenum = ceil($total/$perpage);
  97. $over = $total-($start+$perpage);
  98. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  99. echo json_encode($rows);exit;
  100. }
  101. $this->_Template('shop',$this->data);
  102. }
  103. //添加
  104. public function _add()
  105. {
  106. $post = $this->input->post(NULL, TRUE);
  107. if(isset($post['shopid']))
  108. {
  109. $post['type'] = $this->input->post('type',true);
  110. $post['notice'] = $this->input->post('notice',true);
  111. $post['shopid'] = $this->input->post('shopid',true);
  112. $post['shopname'] = $this->input->post('shopname',true);
  113. $post['shortname'] = $this->input->post('shortname',true);
  114. $post['shopuser'] = $this->input->post('shopuser',true);
  115. $post['shopphone'] = $this->input->post('shopphone',true);
  116. $post['brandname'] = $this->input->post('brandname',true);
  117. /**
  118. $post['companyname'] = $this->input->post('companyname',true);
  119. $post['country'] = $this->input->post('country',true);
  120. $post['province'] = $this->input->post('province',true);
  121. $post['city'] = $this->input->post('city',true);
  122. $post['zip'] = $this->input->post('zip',true);
  123. $post['adress'] = $this->input->post('adress',true);
  124. $post['defaultpricetype'] = $this->input->post('defaultpricetype',true);
  125. $post['updcycle'] = $this->input->post('updcycle',true);
  126. $post['taxrate'] = $this->input->post('taxrate',true);
  127. $post['uphone'] = $this->input->post('uphone',true);
  128. **/
  129. $post['estimaterate'] = $this->input->post('estimaterate',true);
  130. $post['userexp'] = $this->input->post('userexp',true);
  131. $post['mustba'] = $this->input->post('mustba',true);
  132. $post['shopcost'] = $this->input->post('shopcost',true);
  133. $post['shopcosttext'] = $this->input->post('shopcosttext',true);
  134. $post['shopwh'] = $this->input->post('shopwh',true);
  135. $post['shopwhtext'] = $this->input->post('shopwhtext',true);
  136. $post['edittime'] = time();
  137. $post['addtime'] = time();
  138. $px = $this->shop->find_all("1=1",'*','px desc');
  139. $post['px'] = $px[0]['px']+10;
  140. $s = $post;
  141. $s['id'] = $post['yyid'];
  142. $s['name'] = $post['shortname'];
  143. if($post['type'] == '1514')
  144. {
  145. $z = '跨境';
  146. }
  147. else if($post['type'] == '269')
  148. {
  149. $z = '独-';
  150. }
  151. else if($post['type'] == '1514')
  152. {
  153. $z = '速-';
  154. }
  155. $tc = $this->apiyy->get_cjkh($s);
  156. if(isset($tc['Data'][0]))
  157. {
  158. }
  159. else
  160. {
  161. echo json_encode(array('msg'=>$tc['Data'][0]['ErrorMsg'],'success'=>false));exit;
  162. }
  163. if($this->shop->insert($post))
  164. {
  165. echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
  166. }
  167. else
  168. {
  169. echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
  170. }
  171. }
  172. $this->_Template('shop_add',$this->data);
  173. }
  174. //修改
  175. public function _edit($arg_array)
  176. {
  177. $post = $this->input->post(NULL, TRUE);
  178. if(isset($post['id']))
  179. {
  180. $id = $this->input->post('id',true);
  181. $ps = $this->shop->read($id);
  182. $post['type'] = $this->input->post('type',true);
  183. $post['notice'] = $this->input->post('notice',true);
  184. $post['shopid'] = $this->input->post('shopid',true);
  185. $post['shopname'] = $this->input->post('shopname',true);
  186. $post['shortname'] = $this->input->post('shortname',true);
  187. $post['shopuser'] = $this->input->post('shopuser',true);
  188. $post['shopphone'] = $this->input->post('shopphone',true);
  189. $post['brandname'] = $this->input->post('brandname',true);
  190. /**
  191. $post['companyname'] = $this->input->post('companyname',true);
  192. $post['country'] = $this->input->post('country',true);
  193. $post['province'] = $this->input->post('province',true);
  194. $post['city'] = $this->input->post('city',true);
  195. $post['zip'] = $this->input->post('zip',true);
  196. $post['adress'] = $this->input->post('adress',true);
  197. $post['defaultpricetype'] = $this->input->post('defaultpricetype',true);
  198. $post['updcycle'] = $this->input->post('updcycle',true);
  199. $post['taxrate'] = $this->input->post('taxrate',true);
  200. $post['uphone'] = $this->input->post('uphone',true);
  201. **/
  202. $post['estimaterate'] = $this->input->post('estimaterate',true);
  203. $post['userexp'] = $this->input->post('userexp',true);
  204. $post['mustba'] = $this->input->post('mustba',true);
  205. $post['shopcost'] = $this->input->post('shopcost',true);
  206. $post['shopcosttext'] = $this->input->post('shopcosttext',true);
  207. $post['shopwh'] = $this->input->post('shopwh',true);
  208. $post['shopwhtext'] = $this->input->post('shopwhtext',true);
  209. $post['edittime'] = time();
  210. if($this->shop->save($post,$id))
  211. {
  212. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  213. }
  214. else
  215. {
  216. echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
  217. }
  218. }
  219. $arg_array = $arg_array[0];
  220. $shop = $this->shop->read($arg_array);
  221. $this->data['shop'] = $shop;
  222. $this->_Template('shop_edit',$this->data);
  223. }
  224. //删除
  225. public function _del()
  226. {
  227. $post = $this->input->post(NULL, TRUE);
  228. if(isset($post['s']))
  229. {
  230. $id_arr = $this->input->post('s');
  231. $id_arr = explode(',',$id_arr);
  232. if(!$id_arr)
  233. {
  234. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  235. }
  236. //循环删除记录
  237. foreach ($id_arr as $v)
  238. {
  239. $this->shop->remove($v);
  240. }
  241. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  242. }
  243. }
  244. //数据
  245. public function _rows()
  246. {
  247. $post = $this->input->post(NULL, TRUE);
  248. if(isset($post['shopcost']))
  249. {
  250. $productprice = $this->input->post('shopcost',true);
  251. $data = $this->productprice->find_all('1=1');
  252. $list = array();
  253. foreach ($data as $key=>$value)
  254. {
  255. $list[] = array('id'=>$value['id'],'title'=>$value['name']);
  256. }
  257. $num = array();
  258. if($productprice != "null")
  259. {
  260. $num = $this->shop->read($productprice);//找出内容
  261. if($num['shopcost'])
  262. {
  263. $num = explode('|',trim($num['shopcost'],'|'));//数组化内容
  264. }
  265. else
  266. {
  267. $num = array();
  268. }
  269. }
  270. echo json_encode(array('msg'=>($list),'num'=>($num),'success'=>true));
  271. }
  272. if(isset($post['shopwh']))
  273. {
  274. $warehouse = $this->input->post('shopwh',true);
  275. $data = $this->warehouse->find_all('1=1');
  276. $list = array();
  277. foreach ($data as $key=>$value)
  278. {
  279. $list[] = array('id'=>$value['id'],'title'=>$value['title']);
  280. }
  281. $num = array();
  282. if($warehouse != "null")
  283. {
  284. $num = $this->shop->read($warehouse);//找出内容
  285. if($num['shopwh'])
  286. {
  287. $num = explode('|',trim($num['shopwh'],'|'));//数组化内容
  288. }
  289. else
  290. {
  291. $num = array();
  292. }
  293. }
  294. echo json_encode(array('msg'=>($list),'num'=>($num),'success'=>true));
  295. }
  296. }
  297. public function _code($arg_array)
  298. {
  299. $shop = $this->shop->read($arg_array[0]);
  300. if($shop['type'] == 1514)
  301. {
  302. header('Location:https://services.us.tiktokshop.com/open/authorize?service_id='.$shop['codeid']);exit;
  303. }
  304. else
  305. {
  306. $setting['appkey'] = '26004389';
  307. $setting['secret'] = 'd880d725c67b449c8a601e9b0766955d';
  308. $setting['code'] = 'https://oauth.aliexpress.com/authorize';
  309. $setting['token'] = 'https://oauth.aliexpress.com/token';
  310. if(!$_GET['code'])
  311. {
  312. 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;
  313. }
  314. $code = $_GET['code'];
  315. $url = $setting['token'];
  316. $postfields= array('grant_type'=>'authorization_code',
  317. 'client_id'=>$setting['appkey'],
  318. 'client_secret'=>$setting['secret'],
  319. 'code'=>$_GET['code'],
  320. 'sp'=>'ae',
  321. 'redirect_uri'=>'http://'.$_SERVER['HTTP_HOST'].'/shop/code');
  322. $post_data = '';
  323. foreach($postfields as $key=>$value){
  324. $post_data .="$key=".urlencode($value)."&";}
  325. $ch = curl_init();
  326. curl_setopt($ch, CURLOPT_URL, $url);
  327. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  328. curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
  329. curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
  330. //指定post数据
  331. curl_setopt($ch, CURLOPT_POST, true);
  332. //添加变量
  333. curl_setopt($ch, CURLOPT_POSTFIELDS, substr($post_data,0,-1));
  334. $output = curl_exec($ch);
  335. $output = json_decode($output,true);
  336. print_r($output);
  337. $this->shop->save(array('code'=>$output["access_token"],'codetime'=>substr($output["expire_time"],0,10)),$_GET['state']);
  338. header('Location:http://'.$_SERVER['HTTP_HOST'].'/user/');exit;
  339. }
  340. }
  341. }