Product.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use addons\epay\library\Service;
  6. /**
  7. * 产品
  8. */
  9. class Product extends Api
  10. {
  11. protected $noNeedLogin = ['cate_product_lists','lists','info','test','xieyi_info'];
  12. protected $noNeedRight = ['*'];
  13. protected $order_status = [
  14. 0 => '待付款',
  15. 1 => '待出行',
  16. 2 => '已完成',
  17. 10 => '已取消',//未付取消
  18. 21 => '售后中',//已付取消
  19. 22 => '已退款',//已付已退
  20. ];
  21. //分类页,全部商品列表
  22. public function cate_product_lists(){
  23. $where = [
  24. 'p.is_show'=>1,
  25. 'p.jxtype'=>0,
  26. ];
  27. $keyword = input('keyword','','trim');
  28. if(!empty($keyword)){
  29. $where['p.name'] = ['LIKE','%'.$keyword.'%'];
  30. if($this->auth->isLogin()){
  31. Db::name('user_search')->insertGetId(['user_id'=>$this->auth->id,'keyword'=>$keyword]);
  32. }
  33. }
  34. //排序字段
  35. $orderby = input('orderby','view_number','trim');
  36. if($orderby != 'price' && $orderby != 'sell_number'){
  37. $orderby = 'view_number';
  38. }
  39. //排序方式
  40. $sort = input('sort',1,'intval');//1正序,2倒叙
  41. if($sort == 1){
  42. $sort = 'asc';
  43. }else{
  44. $sort = 'desc';
  45. }
  46. if($orderby == 'view_number'){$sort = 'desc';}
  47. $list = Db::name('product')->alias('p')
  48. ->join('procate','p.procate_id = procate.id','LEFT')
  49. ->field('p.id,p.name,p.images,p.title,p.info,p.image,p.address,p.price,p.view_number,p.procate_id,
  50. procate.name as procate_name
  51. ')->where($where)->order('p.'.$orderby,$sort)->select();
  52. $list = list_domain_image($list,['image','images']);
  53. foreach($list as $key => $product){
  54. $list[$key]['image'] = isset($product['images'][0]) ? $product['images'][0] : $product['image'];
  55. }
  56. $procate = Db::name('procate')->field('id,name')->order('weigh desc,id desc')->select();
  57. foreach($procate as $key => &$cate){
  58. $cate['child'] = [];
  59. foreach($list as $pk => $product){
  60. if($cate['id'] == $product['procate_id']){
  61. $cate['child'][] = $product;
  62. }
  63. }
  64. if(empty($cate['child'])){
  65. unset($procate[$key]);
  66. }
  67. }
  68. //因为有unset,所以去键名
  69. $result = [];
  70. $result[] = ['id'=>0,'name'=>'全部','child'=>$list];
  71. foreach($procate as $key => $val){
  72. $result[] = $val;
  73. }
  74. $this->success(1,$result);
  75. }
  76. //商品列表
  77. public function lists()
  78. {
  79. $where = [
  80. 'is_show'=>1,
  81. 'jxtype'=>0,
  82. ];
  83. //首页分类
  84. $tag_id = input('tag_id',0);
  85. if($tag_id){
  86. if($tag_id == -1){
  87. $where['is_recommend'] = 1;
  88. }else{
  89. $where['protag_id'] = $tag_id;
  90. }
  91. }
  92. //业务分类
  93. $cate_id = input('cate_id',0,'intval');
  94. if($cate_id){
  95. $where['procate_id'] = $cate_id;
  96. }
  97. //搜索
  98. $keyword = input('keyword','','trim');
  99. if(!empty($keyword)){
  100. $where['name'] = ['LIKE','%'.$keyword.'%'];
  101. if($this->auth->isLogin()){
  102. Db::name('user_search')->insertGetId(['user_id'=>$this->auth->id,'keyword'=>$keyword]);
  103. }
  104. }
  105. $list = Db::name('product')->field('content',true)->where($where)->order('weigh desc,id desc')->autopage()->select();
  106. $list = list_domain_image($list,['image','images']);
  107. $this->success(1,$list);
  108. }
  109. //商品详情+选路线规格
  110. public function info(){
  111. $id = input('id',0);
  112. $info = Db::name('product')->where('id',$id)->find();
  113. if(!$info){
  114. $this->error('不存在的商品');
  115. }
  116. if($info['is_show'] != 1){
  117. $this->error('商品已下架');
  118. }
  119. $info = info_domain_image($info,['image','images']);
  120. //访问量自增1
  121. Db::name('product')->where('id',$id)->setInc('view_number');
  122. //是否收藏
  123. $info['is_collect'] = 0;
  124. if($this->auth->isLogin()){
  125. $collect = Db::name('user_collect')->where('user_id',$this->auth->id)->where('product_id',$id)->find();
  126. if($collect){
  127. $info['is_collect'] = 1;
  128. }
  129. }
  130. //追加路线
  131. //单程
  132. $road = Db::name('product_road')->where('product_id',$id)->where('roadtype','IN','1,2')->select();
  133. if(!empty($road)){
  134. $roadtype = ['name'=>'单程票','type'=>1,'child'=>$road];
  135. $info['road'][] = $roadtype;
  136. }
  137. //往返
  138. $road = Db::name('product_road')->where('product_id',$id)->where('roadtype',3)->select();
  139. if(!empty($road)){
  140. $roadtype = ['name'=>'往返票','type'=>2,'child'=>$road];
  141. $info['road'][] = $roadtype;
  142. }
  143. $this->success(1,$info);
  144. }
  145. //商品详情+已确定线路,选班次
  146. public function banci(){
  147. $id = input('id',0);
  148. $info = Db::name('product')->where('id',$id)->find();
  149. if(!$info){
  150. $this->error('不存在的商品');
  151. }
  152. if($info['is_show'] != 1){
  153. $this->error('商品已下架');
  154. }
  155. $info = info_domain_image($info,['image','images']);
  156. //是否收藏
  157. $info['is_collect'] = 0;
  158. if($this->auth->isLogin()){
  159. $collect = Db::name('user_collect')->where('user_id',$this->auth->id)->where('product_id',$id)->find();
  160. if($collect){
  161. $info['is_collect'] = 1;
  162. }
  163. }
  164. //根据选择的路线显示
  165. $road_id = input('xianlu_id',0);
  166. $road_info = Db::name('product_road')->where('id',$road_id)->find();
  167. $info['xianlu_id'] = $road_id;//回传
  168. //获取票
  169. $ticket_map = [
  170. 'road_id' => $road_id,
  171. ];
  172. if($this->auth->group_id != 4){
  173. $ticket_map['is_qudao'] = 0;
  174. }
  175. $ticket = Db::name('product_ticket')->where($ticket_map)->select();
  176. $info['ticket'] = $ticket;
  177. if($road_info['roadtype'] == 3){
  178. $road_list = Db::name('product_road')->where('product_id',$id)->where('roadtype','IN','1,2')->order('roadtype','asc')->select();
  179. }else{
  180. $road_list = Db::name('product_road')->where('id',$road_id)->select();
  181. }
  182. //循环road
  183. $road_data= [];
  184. $today = time();
  185. foreach($road_list as $key => $road){
  186. //出发地
  187. $chufadi = Db::name('product_chufadi')->where('road_id',$road['id'])->select();
  188. //出发日期
  189. $riqi_data = [];
  190. //产品的所有日历
  191. $chufabanci = Db::name('product_chufabanci')
  192. ->where('road_id',$road['id'])
  193. ->where('fache_status',0)
  194. ->where('chufatime','gt',$today)
  195. ->where('ticket_remain','gt',0)
  196. ->order('chufatime','asc')->select();
  197. foreach($chufabanci as $bckey => $rili){
  198. $riqi = date('Y-m-d',$rili['chufatime']);
  199. $shijian = date('H:i',$rili['chufatime']);
  200. $chufabanci[$bckey]['riqi'] = $riqi;
  201. $chufabanci[$bckey]['shijian'] = $shijian;
  202. //
  203. $riqi_data[] = $riqi;
  204. }
  205. $riqi_data = (array_flip(array_flip($riqi_data)));//去重
  206. //双循环,入child
  207. $riqi_data_new = [];
  208. foreach($riqi_data as $riqi){
  209. $child = [];
  210. foreach($chufabanci as $bckey => $rili){
  211. if($riqi == $rili['riqi']){
  212. $child[] = $rili;
  213. }
  214. }
  215. $thisbanci = [
  216. 'name' => substr($riqi,5),
  217. 'name_ymd' => $riqi,
  218. 'child' => $child,
  219. ];
  220. $riqi_data_new[] = $thisbanci;
  221. }
  222. $thisroad = [
  223. 'road_id'=>$road['id'],
  224. 'roadtype'=>$road['roadtype'],
  225. 'roadname'=>$road['roadname'],
  226. 'chufadi' => $chufadi,
  227. 'chufabanci' => $riqi_data_new,
  228. ];
  229. $road_data[] = $thisroad;
  230. }
  231. $info['road'] = $road_data;
  232. //协议名字
  233. $info['xieyi_name'] = '';
  234. if($info['xieyi_id']){
  235. $xieyi = Db::name('product_xieyi')->where('id',$info['xieyi_id'])->where('deletetime',NULL)->find();
  236. if(!empty($xieyi)){
  237. $info['xieyi_name'] = $xieyi['name'];
  238. }else{
  239. $info['xieyi_id'] = 0;
  240. }
  241. }else{
  242. $info['xieyi_id'] = 0;
  243. }
  244. $this->success(1,$info);
  245. }
  246. //产品协议
  247. public function xieyi_info(){
  248. $id = input('id',0);
  249. $info = Db::name('product_xieyi')->where('id',$id)->find();
  250. $this->success(1,$info);
  251. }
  252. //商品详情,这一天有几个班次
  253. //给前端的辅助方法
  254. public function banci_time(){
  255. $road_id = input('road_id',0);
  256. $riqi_ymd = strtotime(input('riqi_ymd',date('Y-m-d')));
  257. $riqi_end = $riqi_ymd + 86399;
  258. //产品的所有日历
  259. $chufabanci = Db::name('product_chufabanci')
  260. ->where('road_id',$road_id)
  261. ->where('fache_status',0)
  262. ->where('chufatime','BETWEEN',[$riqi_ymd,$riqi_end])
  263. ->where('chufatime','gt',time())
  264. ->where('ticket_remain','gt',0)
  265. ->order('chufatime','asc')->select();
  266. foreach($chufabanci as $bckey => $rili){
  267. $riqi = date('m-d',$rili['chufatime']);
  268. $shijian = date('H:i',$rili['chufatime']);
  269. $chufabanci[$bckey]['riqi'] = $riqi;
  270. $chufabanci[$bckey]['riqi_ymd'] = date('Y-m-d',$rili['chufatime']);
  271. $chufabanci[$bckey]['shijian'] = $shijian;
  272. }
  273. $this->success(1,$chufabanci);
  274. }
  275. //下单
  276. public function submit(){
  277. $realname = input('realname','');
  278. $mobile = input('mobile','');
  279. if(empty($realname) || empty($mobile)){
  280. $this->error('请填写姓名和手机号');
  281. }
  282. //分享人用户id,检验
  283. $share_uid = input('share_uid',0,'intval');
  284. if(!empty($share_uid)){
  285. $share_user = Db::name('user')->field('id')->where('id',$share_uid)->find();
  286. if(!empty($share_user)){
  287. $share_uid = $share_user['id'];
  288. //保存上
  289. Db::name('user')->where('id',$this->auth->id)->update(['intro_uid'=>$share_uid]);
  290. }else{
  291. $share_uid = 0;
  292. }
  293. }else{
  294. $share_uid = !empty($this->auth->intro_uid) ? $this->auth->intro_uid : 0 ;
  295. }
  296. //商品
  297. $product_id = input('product_id',0);
  298. $product_info = Db::name('product')->where('id',$product_id)->find();
  299. if(!$product_info){
  300. $this->error('不存在的商品');
  301. }
  302. if($product_info['is_show'] != 1){
  303. $this->error('商品已下架');
  304. }
  305. //路线
  306. $road = input('road','','htmlspecialchars_decode');
  307. if(empty($road)){
  308. $this->error('请选择路线');
  309. }
  310. //子订单公共数据
  311. $nowtime = time();
  312. $order_item_public = [
  313. 'order_id' => 0,
  314. 'product_id' => $product_id,
  315. 'user_id' => $this->auth->id,
  316. 'createtime' => $nowtime,
  317. ];
  318. //初始路线,仅给票用
  319. $road_id = input('xianlu_id',0);
  320. $road_info = Db::name('product_road')->where('product_id',$product_id)->where('id',$road_id)->find();
  321. if(!$road_info){
  322. $this->error('不存在的路线');
  323. }
  324. //此路拥有的票
  325. $ticket_list = Db::name('product_ticket')->where('road_id',$road_id)->column('id,name,price');
  326. //循环票
  327. $order_ticket_all = [];
  328. $ticket_num = 0;//单个路线所需票数
  329. $total_fee = 0;//订单总价格
  330. $ticket_str = '';
  331. $ticket = input('ticket','','htmlspecialchars_decode');
  332. if(empty($ticket)){
  333. $this->error('请选择票数');
  334. }
  335. $ticket = json_decode($ticket,true);
  336. foreach($ticket as $tic){
  337. if(!isset($tic['ticket_id']) || !isset($tic['number']) ){$this->error('票数据错误1');}
  338. if(!isset($ticket_list[$tic['ticket_id']])){$this->error('票数据错误2');}
  339. $tic['number'] = intval($tic['number']);
  340. if($tic['number'] == 0){
  341. continue;
  342. }
  343. $ticket_num += $tic['number'];
  344. $ticket_info = $ticket_list[$tic['ticket_id']];
  345. $order_ticket = [
  346. 'road_id' => $road_id,
  347. 'roadname' => $road_info['roadname'],
  348. 'roadtype' => $road_info['roadtype'],
  349. 'ticket_id' => $ticket_info['id'],
  350. 'ticket_name' => $ticket_info['name'],
  351. 'ticket_price' => $ticket_info['price'],
  352. 'ticket_number' => $tic['number'],
  353. 'pay_fee' => bcmul($tic['number'],$ticket_info['price'],2),
  354. ];
  355. $total_fee = bcadd($total_fee,$order_ticket['pay_fee'],2);
  356. $order_ticket = array_merge($order_item_public,$order_ticket);
  357. $order_ticket_all[] = $order_ticket;
  358. $ticket_str .= $ticket_info['name'].'*'.$tic['number'].',';
  359. }
  360. $ticket_str = mb_substr($ticket_str,0,-1);
  361. if($ticket_num == 0){
  362. $this->error('您还没选票');
  363. }
  364. //订单入库
  365. $order_data = [
  366. 'order_no' => createUniqueNo('P',$this->auth->id),
  367. 'product_id' => $product_id,
  368. 'product_name' => $product_info['name'],
  369. 'user_id' => $this->auth->id,
  370. 'createtime' => $nowtime,
  371. 'pay_fee' => $total_fee,
  372. 'status' => 0,
  373. 'paytime' => 0,
  374. 'realname' => $realname,
  375. 'mobile' => $mobile,
  376. 'road_id' => $road_info['id'],
  377. 'roadtype' => $road_info['roadtype'],
  378. 'roadname' => $road_info['roadname'],
  379. 'ticket_str' => $ticket_str,
  380. 'share_uid' => $share_uid,
  381. 'jxtype' => $product_info['jxtype'],
  382. 'xieyi_id' => $product_info['xieyi_id'], //冗余协议id
  383. ];
  384. //入库
  385. Db::startTrans();
  386. $order_id = Db::name('order')->insertGetId($order_data);
  387. if(!$order_id){
  388. Db::rollback();
  389. $this->error('下单失败,请稍后再试');
  390. }
  391. //order_ticket入库
  392. foreach($order_ticket_all as &$order_ticket){
  393. $order_ticket['order_id'] = $order_id;
  394. }
  395. $rs_ticket = Db::name('order_ticket')->insertAll($order_ticket_all);
  396. if(!$rs_ticket){
  397. Db::rollback();
  398. $this->error('下单失败,请稍后再试');
  399. }
  400. //去程和返程
  401. $order_info_all = [];
  402. $road = json_decode($road,true);
  403. if($road_info['roadtype'] == 3 && count($road) != 2){Db::rollback();$this->error('路线数据错误'); } //往返路线,road数量只能是2
  404. if($road_info['roadtype'] != 3 && count($road) != 1){Db::rollback();$this->error('路线数据错误'); } //单程路线,road数量只能是1
  405. $order_update = [];
  406. foreach($road as $ro){
  407. //检查路线id
  408. $ro_info = Db::name('product_road')->where('product_id',$product_id)->where('id',$ro['road_id'])->find();
  409. if(empty($ro_info)){
  410. Db::rollback();
  411. $this->error('路线数据不存在');
  412. }
  413. //检查出发地
  414. $chufadi = Db::name('product_chufadi')->where('road_id',$ro['road_id'])->where('id',$ro['chufadi_id'])->find();
  415. if(empty($chufadi)){
  416. Db::rollback();
  417. $this->error('出发地数据不存在');
  418. }
  419. //检查出发班次
  420. $chufabanci = Db::name('product_chufabanci')->where('road_id',$ro['road_id'])->where('id',$ro['chufabanci_id'])->lock(true)->find();
  421. if(empty($chufabanci)){
  422. Db::rollback();
  423. $this->error('出发班次不存在');
  424. }
  425. /*if($ro_info['roadtype'] == $chufadi['chufatype'] && $ro_info['roadtype'] == $chufabanci['chufatype']){
  426. }else{
  427. $this->error('路线数据不正确');
  428. }*/
  429. //这个班次,票数是否足够
  430. if($chufabanci['ticket_remain'] < $ticket_num){
  431. Db::rollback();
  432. $this->error($ro_info['roadname'].'此班次票数不足,请换一个时间');
  433. }
  434. $banci_update = [
  435. 'ticket_remain' => $chufabanci['ticket_remain'] - $ticket_num
  436. ];
  437. $banci_rs = Db::name('product_chufabanci')->where('id',$ro['chufabanci_id'])->update($banci_update);
  438. if($banci_rs === false){
  439. Db::rollback();
  440. $this->error('购票失败');
  441. }
  442. //冗余订单数据,去返的出发时间
  443. if($chufabanci['chufatype'] == 1){
  444. $order_update['go_chufatime'] = $chufabanci['chufatime'];
  445. }
  446. if($chufabanci['chufatype'] == 2){
  447. $order_update['back_chufatime'] = $chufabanci['chufatime'];
  448. }
  449. //冗余订单数据,去返的出发地
  450. if($chufadi['chufatype'] == 1){
  451. $order_update['go_chufaname'] = $chufadi['chufaname'];
  452. }
  453. if($chufadi['chufatype'] == 2){
  454. $order_update['back_chufaname'] = $chufadi['chufaname'];
  455. }
  456. //校验完成
  457. $order_info = [
  458. 'road_id' => $ro_info['id'],
  459. 'roadname'=> $ro_info['roadname'],
  460. 'roadtype'=> $ro_info['roadtype'],
  461. 'chufadi_id'=> $chufadi['id'],
  462. 'chufaname'=> $chufadi['chufaname'],
  463. 'longitude'=> $chufadi['longitude'],
  464. 'latitude'=> $chufadi['latitude'],
  465. 'chufabanci_id'=> $chufabanci['id'],
  466. 'chufatime'=> $chufabanci['chufatime'],
  467. 'ticket_number'=> $ticket_num,
  468. 'order_id' => $order_id, //覆盖order_id
  469. 'hexiao_no' => createUniqueNo('HX'),
  470. 'hexiao_status' => 0,
  471. 'hexiao_uid' => 0,
  472. ];
  473. $order_info = array_merge($order_item_public,$order_info);
  474. $order_info_all[] = $order_info;
  475. }
  476. $order_info_rs = Db::name('order_road')->insertAll($order_info_all);
  477. if(!$order_info_rs){
  478. Db::rollback();
  479. $this->error('下单失败,请稍后再试');
  480. }
  481. //修改订单
  482. $order_up = Db::name('order')->where('id',$order_id)->update($order_update);
  483. if($order_up === false){
  484. Db::rollback();
  485. $this->error('下单失败,请稍后再试');
  486. }
  487. //准备付款
  488. Db::commit();
  489. $this->success('下单成功',$order_id);
  490. }
  491. //订单详情
  492. public function order_info(){
  493. $order_id = input('order_id',0);
  494. $order = Db::name('order')->where('id',$order_id)->find();
  495. if(empty($order)){
  496. $this->error('不存在的订单');
  497. }
  498. //订单状态文字
  499. $order['status_text'] = isset($this->order_status[$order['status']]) ? $this->order_status[$order['status']] : '';
  500. //订单支付截止时间
  501. $order_auto_cancel_minite = config('site.order_auto_cancel_minite');
  502. $order['last_paytime'] = date('Y-m-d H:i',$order['createtime'] + ($order_auto_cancel_minite * 60));
  503. //商品
  504. $product = Db::name('product')->field('id,name,title,info,image')->where('id',$order['product_id'])->find();
  505. if($product){
  506. $product = info_domain_image($product,['image']);
  507. }
  508. //路线
  509. $order_road = Db::name('order_road')->field('roadtype,chufaname,chufatime,roadname,longitude,latitude')->where('order_id',$order_id)->order('roadtype asc')->select();
  510. foreach($order_road as $key => $road){
  511. $order_road[$key]['chufatime_text'] = date('Y-m-d H:i',$road['chufatime']);
  512. }
  513. //票
  514. $order_ticket = Db::name('order_ticket')->field('ticket_name,ticket_number')->where('order_id',$order_id)->select();
  515. //结果
  516. $result = [
  517. 'order' => $order,
  518. 'product' => $product,
  519. 'order_road' => $order_road,
  520. 'order_ticket' => $order_ticket,
  521. 'ticket_str' => $order['ticket_str']
  522. ];
  523. $this->success(1,$result);
  524. }
  525. /**
  526. * json 请求
  527. * @param $url
  528. * @return mixed
  529. */
  530. private function getJson($url){
  531. $ch = curl_init();
  532. curl_setopt($ch, CURLOPT_URL, $url);
  533. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  534. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  535. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  536. $output = curl_exec($ch);
  537. curl_close($ch);
  538. return json_decode($output, true);
  539. }
  540. //支付订单
  541. public function pay_order(){
  542. //code值
  543. $code = input('code');
  544. if (!$code) {
  545. $this->error(__('Invalid parameters'));
  546. }
  547. $config = config('wxMiniProgram');
  548. $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  549. $openidInfo = $this->getJson($getopenid);
  550. if(!isset($openidInfo['openid'])) {
  551. $this->error('用户openid获取失败',$openidInfo);
  552. }
  553. //订单
  554. $pay_type = 'wechat';
  555. $platform = 'miniapp';
  556. $order_id = input('order_id',0);
  557. $map = [
  558. 'id' => $order_id,
  559. 'user_id' => $this->auth->id,
  560. 'status' => 0,
  561. ];
  562. $order = Db::name('order')->where($map)->find();
  563. if(empty($order)){
  564. $this->error('请刷新重试');
  565. }
  566. $pay_data = [
  567. 'user_id' => $order['user_id'],
  568. 'out_trade_no' => createUniqueNo('P',$order_id),
  569. 'order_amount' => $order['pay_fee'],
  570. //'order_amount' => 0.01,//测试支付强制0.01元
  571. 'createtime' => time(),
  572. 'notifytime' => 0,
  573. 'pay_type' => $pay_type,
  574. 'platform' => $platform,
  575. 'order_status' => 0,
  576. 'table_name' => 'order',
  577. 'table_id' => $order['id'],
  578. ];
  579. Db::name('pay_order')->insertGetId($pay_data);
  580. $openid = $openidInfo['openid'];
  581. //下单
  582. $params = [
  583. 'type' => $pay_type,
  584. 'orderid' => $pay_data['out_trade_no'],
  585. 'title' => '支付订单',
  586. 'amount' => $pay_data['order_amount'],
  587. 'method' => $platform,
  588. 'openid' => $openid,
  589. 'notifyurl' => config('pay_notify_url').'/api/pay/order_notify_base/paytype/'.$pay_type,
  590. 'returnurl' => '',
  591. ];
  592. $res = Service::submitOrder($params);
  593. if($pay_type == 'wechat'){
  594. $this->success('success',json_decode($res,true));
  595. }else{
  596. $this->success('success',$res);
  597. }
  598. }
  599. //提交订单的数据格式
  600. public function test(){
  601. $ticket_data = [
  602. [
  603. 'ticket_id' => 1,
  604. 'number' => 2,
  605. ],
  606. [
  607. 'ticket_id' => 4,
  608. 'number' => 3,
  609. ],
  610. ];
  611. dump(json_encode($ticket_data));
  612. $road_data = [
  613. [
  614. 'road_id' => 1,
  615. 'chufadi_id' => 1,
  616. 'chufabanci_id' => 1,
  617. ],
  618. [
  619. 'road_id' => 2,
  620. 'chufadi_id' => 4,
  621. 'chufabanci_id' => 7,
  622. ],
  623. ];
  624. dump(json_encode($road_data));
  625. }
  626. }