User.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use think\db;
  5. use Jst\Jst;
  6. class User extends Controller{
  7. //添加发票
  8. public function addInvoice(){
  9. $data=$this->request->param();
  10. $data['time']=time();
  11. if(db::name('invoice')->insert($data)){
  12. $rdata['code']=200;
  13. $rdata['mess']='添加成功';
  14. }else{
  15. $rdata['code']=400;
  16. $rdata['mess']='添加失败';
  17. }
  18. return json($rdata);
  19. }
  20. //显示全部发票
  21. public function showInvoice(){
  22. $openid=$this->request->param('openid');
  23. if(!$openid){
  24. return json(['code'=>401,'mess'=>'请先登录']);
  25. }
  26. $data=db::name('invoice')->where('openid',$openid)->select();
  27. if($data){
  28. $rdata['code']=200;
  29. $rdata['mess']=$data;
  30. }else{
  31. $rdata['code']=400;
  32. $rdata['mess']='查看错误';
  33. }
  34. return json($rdata);
  35. }
  36. //查看发票
  37. public function catInvoice(){
  38. $id=$this->request->param('id');
  39. if(!$id){
  40. return json(['code'=>401,'mess'=>'发票id为空']);
  41. }
  42. $data=db::name('invoice')->find($id);
  43. if($data){
  44. $rdata['code']=200;
  45. $rdata['mess']=$data;
  46. }else{
  47. $rdata['code']=400;
  48. $rdata['mess']='查看错误';
  49. }
  50. return json($rdata);
  51. }
  52. //修改发票
  53. public function editInvoice(){
  54. $data=$this->request->param();
  55. if(!$data['id']){
  56. return json(['code'=>401,'mess'=>'发票id为空']);
  57. }
  58. if(db::name('invoice')->update($data)){
  59. $rdata['code']=200;
  60. $rdata['mess']='修改成功';
  61. }else{
  62. $rdata['code']=400;
  63. $rdata['mess']='修改失败';
  64. }
  65. return json($rdata);
  66. }
  67. //删除发票
  68. public function delInvoice(){
  69. $res=$this->request->param();
  70. $data=db::name('invoice')->where($res)->delete();
  71. if($data){
  72. $rdata['code']=200;
  73. $rdata['mess']='删除成功';
  74. }else{
  75. $rdata['code']=400;
  76. $rdata['mess']='删除失败';
  77. }
  78. return json($rdata);
  79. }
  80. //支付订单列表
  81. public function my_order(){
  82. $res=$this->request->param();
  83. if(!$res['openid']){
  84. return json(['code'=>401,'mess'=>'请先登录']);
  85. }
  86. $zhekou=GetPrice($res['openid']); //计算用户折扣
  87. $openid=$res['openid'];
  88. //退款的订单id
  89. $order_info_refund=db::name('order_info_refund')->field('id,order_info_true_id')->where('state','<>','1')->where('openid',$openid)->select();
  90. $ids=array_column($order_info_refund,'order_info_true_id');
  91. if($res['state']==0){
  92. $data=db::name('order_info_true')
  93. ->field('id,kuaidi,code,state,totprice')
  94. ->where('id','NOT IN',$ids)
  95. ->where('openid',$openid)
  96. ->where('isdel','<>',1)
  97. ->order("id",'desc')
  98. ->select();
  99. }elseif($res['state']==5){
  100. //退款的订单
  101. $data=db::name('order_info_refund')->alias('r')
  102. ->join('order_info_true t',"r.order_info_true_id=t.id")
  103. ->where('r.openid',$openid)
  104. ->where('r.state','<>','1')
  105. ->where('r.isdel','<>','1')
  106. ->field('r.state,r.id,t.kuaidi,t.code,t.totprice')
  107. ->order('r.id','desc')
  108. ->select();
  109. }else{
  110. $data=db::name('order_info_true')
  111. ->field('id,kuaidi,code,state,totprice')
  112. ->where('id','NOT IN',$ids)
  113. ->where($res)
  114. ->where('isdel','<>',1)
  115. ->order("id",'desc')
  116. ->select();
  117. }
  118. if(!isset($data)){
  119. return json(['code'=>400,'mess'=>'没有订单']);
  120. }
  121. foreach($data as $key=>$val){
  122. $code=$val['code'];
  123. $tmp=db::name("order")->alias('o')
  124. ->join('goods_inventory i',"o.inventory_id=i.id")
  125. ->join('goods g','o.goods_id=g.id')
  126. ->field('g.id,g.img1,g.name,o.nums,o.price,o.cangku,g.xinghao,i.fenlei')
  127. ->where('o.code',$val['code'])
  128. ->select();
  129. $tmpnums=0;
  130. $tmpprice=0;
  131. foreach($tmp as $key1=>$val1){
  132. $tmp[$key1]['price']=round($val1['price']*$zhekou,1);
  133. $cangku=$val1['cangku'];
  134. $tmpnums=$tmpnums+$val1['nums'];
  135. $tmpprice=$tmpprice+round($val1['nums']*$val1['price']*$zhekou,1);
  136. if(db::name('evaluate')->where(['goods_id'=>$val1['id'],'code'=>$code])->find()){
  137. $tmp[$key1]['evaluate']=1;
  138. }else{
  139. $tmp[$key1]['evaluate']=0;
  140. }
  141. }
  142. $data[$key]['goods']=$tmp;
  143. $data[$key]['cangku']=$cangku;
  144. $data[$key]['nums']=$tmpnums;
  145. // $data[$key]['tot_money']=$tmpprice;
  146. $data[$key]['tot_money']=$val['totprice'];
  147. }
  148. return json($data);
  149. }
  150. //删除订单
  151. public function delOrder(){
  152. $res=$this->request->param();
  153. if(!isset($res['openid']) || !isset($res['id'])){
  154. return json(['code'=>401,'mess'=>'参数错误']);
  155. }
  156. if(db::name("order_info_refund")->where($res)->update(['isdel'=>'1'])){
  157. $response['code']=200;
  158. $response['mess']='删除成功';
  159. }else{
  160. $response['code']=400;
  161. $response['mess']='删除失败';
  162. }
  163. return json($response);
  164. }
  165. //删除订单
  166. public function delOrderReald(){
  167. $res=$this->request->param();
  168. if(!isset($res['openid']) || !isset($res['id'])){
  169. return json(['code'=>401,'mess'=>'参数错误']);
  170. }
  171. if(db::name("order_info_true")->where($res)->update(['isdel'=>'1'])){
  172. $response['code']=200;
  173. $response['mess']='删除成功';
  174. }else{
  175. $response['code']=400;
  176. $response['mess']='删除失败';
  177. }
  178. return json($response);
  179. }
  180. //确认收货
  181. public function trueOrder(){
  182. $res=$this->request->param();
  183. if(!isset($res['openid']) || !isset($res['id'])){
  184. return json(['code'=>401,'mess'=>'参数错误']);
  185. }
  186. if(db::name("order_info_true")->where($res)->update(['state'=>4])){
  187. //收益的金额
  188. $order_info_true=db::name("order_info_true")->where($res)->field('code,time,openid,totprice,kuaidifei,crsNo')->find();
  189. $totpricezhe=round($order_info_true['totprice']*0.05,2);
  190. //得到收益人的openid 计算收益
  191. $generalize=db::name('generalize')->field('popenid,rank')->where('openid',$res['openid'])->find();
  192. //存入临时数据库7天进入账号
  193. if($generalize){
  194. $generalize_price_tmp['openid']=$generalize['popenid'];
  195. $generalize_price_tmp['price']=$totpricezhe;
  196. $generalize_price_tmp['time']=time();
  197. $generalize_price_tmp['code']=$order_info_true['code'];
  198. // $zeng=json_encode($generalize_price_tmp);
  199. db::name('generalize_price_tmp')->insert($generalize_price_tmp);
  200. }
  201. //给总收益累计
  202. db::name('user')->where('openid',$res['openid'])->setInc('cumulative',$order_info_true['totprice']);
  203. //修改erp订单状态
  204. $jst=new Jst();
  205. $orderInfo=db::name("order")->alias('o')
  206. ->join('goods_inventory g',"o.inventory_id=g.id")
  207. ->join('goods s','o.goods_id=s.id')
  208. ->field('g.shop_i_id,o.province,o.city,o.district,o.address,o.price,o.name,o.phone,o.inventory_id,o.nums,g.fenlei,s.name sname')
  209. ->where('o.code',$order_info_true['code'])
  210. ->select();
  211. // $this->intext(json_encode($orderInfo));
  212. foreach($orderInfo as $infokey=>$infoval){
  213. $items[$infokey]=[
  214. 'sku_id'=>$infoval['shop_i_id'],
  215. 'properties_value'=>$infoval['fenlei'],
  216. 'amount'=>$infoval['price'],
  217. 'base_price'=>$infoval['price'],
  218. 'qty'=>$infoval['nums'],
  219. 'name'=>$infoval['sname'],
  220. 'outer_oi_id'=>'zidingdanhao1'
  221. ];
  222. }
  223. // $this->intext(json_encode($items));
  224. $jstData=[
  225. [
  226. 'shop_id'=>'10223774', //店铺编号
  227. 'so_id'=>$order_info_true['code'], //订单id
  228. 'order_date'=>date("Y-m-d H:i:s",$order_info_true['time']), //下单时间
  229. 'shop_status'=>'TRADE_FINISHED', //0:不启用,1:启用
  230. 'shop_buyer_id'=>$order_info_true['openid'], //卖家昵称
  231. 'receiver_state'=>$orderInfo[0]['province'], //收货省份
  232. 'receiver_city'=>$orderInfo[0]['city'], //收货城市
  233. 'receiver_district'=>$orderInfo[0]['district'], //收货区域
  234. 'receiver_address'=>$orderInfo[0]['address'], //详细地址
  235. 'receiver_name'=>$orderInfo[0]['name'], //收货人姓名
  236. 'receiver_mobile'=>$orderInfo[0]['phone'], //收货手机号
  237. 'pay_amount'=>$order_info_true['totprice'], //卖家昵称
  238. 'freight'=>$order_info_true['kuaidifei'], //卖家昵称
  239. 'items'=>$items,
  240. 'pay'=>[
  241. 'outer_pay_id'=>$order_info_true['crsNo'],
  242. 'pay_date'=>date("Y-m-d H:i:s",$order_info_true['time']),
  243. 'payment'=>'微信支付',
  244. 'seller_account'=>'援邦消防',
  245. 'buyer_account'=>$order_info_true['openid'],
  246. 'amount'=>$order_info_true['totprice']
  247. ]
  248. ]
  249. ];
  250. $jstData=json_encode($jstData);
  251. $res=$jst->queryData('orders.upload',$jstData);
  252. // $this->intext($res);
  253. $response['code']=200;
  254. $response['mess']='操作成功';
  255. }else{
  256. $response['code']=400;
  257. $response['mess']='操作失败';
  258. }
  259. return json($response);
  260. }
  261. //写入文件操作
  262. public function intext($data){
  263. $myfile = fopen("a.txt", "a+");
  264. fwrite($myfile, $data);
  265. fclose($myfile);
  266. }
  267. //申请退款
  268. public function refund(){
  269. $openid=$this->request->param('openid');
  270. $id=$this->request->param('id');
  271. $reason=$this->request->param('reason');
  272. if(!isset($id) || !isset($openid) || !isset($reason)){
  273. return json(['code'=>400,'mess'=>'缺少参数']);
  274. }
  275. //退款订单信息
  276. $order_info_true=db::name('order_info_true')->field('id,code,totprice,crsNo,state,card,openid,time,kuaidifei')->where('id',$id)->find();
  277. //退款订单的总金额
  278. $totprice=db::name('order_info_true')->field("sum(totprice) stotprice")->where("crsNo",$order_info_true['crsNo'])->find();
  279. // dump($totprice['stotprice']);
  280. //判断是否已经申请过退款
  281. $isrefund=db::name('order_info_refund')->where('code',$order_info_true['code'])->find();
  282. if(isset($isrefund)){
  283. return json(['code'=>401,'mess'=>'退款审核失败']);
  284. }
  285. $refundDdata['openid']=$openid;
  286. $refundDdata['reason']=$reason;
  287. $refundDdata['time']=time();
  288. $refundDdata['order_info_true_id']=$order_info_true['id'];
  289. $refundDdata['code']=$order_info_true['code'];
  290. $refundDdata['out_trade_no']=$order_info_true['crsNo'];
  291. $refundDdata['total_fee']=$totprice['stotprice'];
  292. $refundDdata['state']='6';
  293. $refundDdata['card']=$order_info_true['card'];
  294. $refundDdata['refund_fee']=$order_info_true['totprice']; //退款的金额
  295. if(db::name('order_info_refund')->insert($refundDdata)){
  296. //修改erp订单状态
  297. $jst=new Jst();
  298. $orderInfo=db::name("order")->alias('o')
  299. ->join('goods_inventory g',"o.inventory_id=g.id")
  300. ->join('goods s','o.goods_id=s.id')
  301. ->field('g.shop_i_id,o.province,o.city,o.district,o.address,o.price,o.name,o.phone,o.inventory_id,o.nums,g.fenlei,s.name sname')
  302. ->where('o.code',$order_info_true['code'])
  303. ->select();
  304. // $this->intext(json_encode($orderInfo));
  305. foreach($orderInfo as $infokey=>$infoval){
  306. $items[$infokey]=[
  307. 'sku_id'=>$infoval['shop_i_id'],
  308. 'properties_value'=>$infoval['fenlei'],
  309. 'amount'=>$infoval['price'],
  310. 'base_price'=>$infoval['price'],
  311. 'qty'=>$infoval['nums'],
  312. 'name'=>$infoval['sname'],
  313. 'refund_status'=>'waiting',
  314. 'refund_qty'=>$infoval['nums'],
  315. 'outer_oi_id'=>'zidingdanhao1'
  316. ];
  317. }
  318. // $this->intext(json_encode($items));
  319. //判断订单原始装填
  320. switch($order_info_true['state']){
  321. case 1:
  322. $shop_status='WAIT_BUYER_PAY';
  323. break;
  324. case 2:
  325. $shop_status='WAIT_SELLER_SEND_GOODS';
  326. break;
  327. case 3:
  328. $shop_status='WAIT_BUYER_CONFIRM_GOODS';
  329. break;
  330. case 4:
  331. $shop_status='TRADE_FINISHED';
  332. break;
  333. }
  334. $jstData=[
  335. [
  336. 'shop_id'=>'10223774', //店铺编号
  337. 'so_id'=>$order_info_true['code'], //订单id
  338. 'order_date'=>date("Y-m-d H:i:s",$order_info_true['time']), //下单时间
  339. 'shop_status'=>$shop_status, //订单状态
  340. 'shop_buyer_id'=>$order_info_true['openid'], //卖家昵称
  341. 'receiver_state'=>$orderInfo[0]['province'], //收货省份
  342. 'receiver_city'=>$orderInfo[0]['city'], //收货城市
  343. 'receiver_district'=>$orderInfo[0]['district'], //收货区域
  344. 'receiver_address'=>$orderInfo[0]['address'], //详细地址
  345. 'receiver_name'=>$orderInfo[0]['name'], //收货人姓名
  346. 'receiver_mobile'=>$orderInfo[0]['phone'], //收货手机号
  347. 'pay_amount'=>$order_info_true['totprice'], //卖家昵称
  348. 'freight'=>$order_info_true['kuaidifei'], //卖家昵称
  349. 'items'=>$items,
  350. 'pay'=>[
  351. 'outer_pay_id'=>$order_info_true['crsNo'],
  352. 'pay_date'=>date("Y-m-d H:i:s",$order_info_true['time']),
  353. 'payment'=>'微信支付',
  354. 'seller_account'=>'援邦消防',
  355. 'buyer_account'=>$order_info_true['openid'],
  356. 'amount'=>$order_info_true['totprice']
  357. ]
  358. ]
  359. ];
  360. $jstData=json_encode($jstData);
  361. $res=$jst->queryData('orders.upload',$jstData);
  362. // $this->intext($res);
  363. return json(['code'=>200,'mess'=>'OK']);
  364. }
  365. // if(db::name('order_info_true')->where(['id'=>$id,'openid'=>$openid])->update(['state'=>5,'reason'=>$reason])){
  366. // }
  367. }
  368. //我的推广码
  369. public function myCode(){
  370. $openid=$this->request->param('openid');
  371. if(!$openid){
  372. return json(['code'=>401,'mess'=>'请先登录']);
  373. }
  374. $data=db::name('generalize')->where('openid',$openid)->field('selfcode')->find()['selfcode'];
  375. if(!isset($data)){
  376. return json(['code'=>400,'mess'=>'缺少参数']);
  377. }else{
  378. return json(['code'=>200,'mess'=>$data]);
  379. }
  380. }
  381. //订单详情
  382. public function orderInfo(){
  383. $openid=$this->request->param('openid');
  384. $code=$this->request->param('code'); //订单号
  385. $data=db::name('order_info_true')->alias('t')
  386. ->join('order o',"o.code=t.code")
  387. ->join("goods g","g.id=o.goods_id")
  388. ->join('goods_inventory i',"i.id=o.inventory_id")
  389. ->field('t.id,t.code,t.kuaidi,t.state,t.totprice,t.kuaidifei,t.vipprice,o.province,o.city,o.district,o.address,o.phone,o.name,o.nums,o.price,i.fenlei,g.name gname,g.xinghao,i.img')
  390. ->where("t.code",$code)
  391. ->select();
  392. foreach($data as $key=>$val){
  393. $rdata['name']=$val['name'];
  394. $rdata['state']=$val['state'];
  395. $rdata['province']=$val['province'];
  396. $rdata['city']=$val['city'];
  397. $rdata['district']=$val['district'];
  398. $rdata['address']=$val['address'];
  399. $rdata['phone']=$val['phone'];
  400. $rdata['id']=$val['id'];
  401. $rdata['code']=$val['code'];
  402. $rdata['kuaidi']=$val['kuaidi'];
  403. $rdata['totprice']=$val['totprice'];
  404. $rdata['kuaidifei']=$val['kuaidifei'];
  405. $rdata['vipprice']=$val['vipprice'];
  406. unset($data[$key]['name']);
  407. unset($data[$key]['state']);
  408. unset($data[$key]['province']);
  409. unset($data[$key]['city']);
  410. unset($data[$key]['district']);
  411. unset($data[$key]['address']);
  412. unset($data[$key]['phone']);
  413. unset($data[$key]['id']);
  414. unset($data[$key]['code']);
  415. unset($data[$key]['kuaidi']);
  416. unset($data[$key]['totprice']);
  417. unset($data[$key]['kuaidifei']);
  418. unset($data[$key]['vipprice']);
  419. }
  420. if($data){
  421. $rdata['goods']=$data;
  422. $rdata['return_code']=200;
  423. }else{
  424. $rdata['return_code']=400;
  425. $rdata['mess']='信息有误';
  426. }
  427. return json($rdata);
  428. }
  429. //查询快递
  430. public function express(){
  431. $code=$this->request->param('code');
  432. $no=db::name('order_info_true')->where('code',$code)->field('no')->find()['no'];
  433. // http_request
  434. if(!isset($no)){
  435. return json(['status'=>501,'msg'=>'订单错误']);
  436. }else{
  437. $url="https://kdwlcxf.market.alicloudapi.com/kdwlcx?no=".$no;
  438. $appcode='ef02a75b81334e119245ed999275047b';
  439. $headers = array();
  440. array_push($headers, "Authorization:APPCODE " . $appcode);
  441. $data=http_request($url,'',$headers);
  442. return $data;
  443. }
  444. }
  445. //意见反馈
  446. public function msg(){
  447. $data=$this->request->param();
  448. $data['time']=time();
  449. if(db::name('msg')->insert($data)){
  450. $req['code']=200;
  451. }else{
  452. $req['code']=400;
  453. }
  454. return json($req);
  455. }
  456. }