Shop.php 11 KB

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