Shop.php 11 KB

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