GoodsOrder.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. namespace app\api\controller;
  3. use think\cache\driver\Redis;
  4. use think\Db;
  5. use think\Exception;
  6. use EasyWeChat\Factory;
  7. use think\Session;
  8. /**
  9. * @title 商品订单
  10. * @controller GoodsOrder
  11. * @group base
  12. */
  13. class GoodsOrder extends Base
  14. {
  15. public function initialize()
  16. {
  17. parent::initialize();
  18. parent::check_login();
  19. cancel_goods_order($this->uid);
  20. }
  21. /**
  22. * @title 生成订单(立即购买 )
  23. * @desc 生成订单(立即购买 )
  24. * @author qc
  25. * @url /api/Goods_order/createOrder
  26. * @method POST
  27. * @header name:Authorization require:1 desc:Token
  28. * @param name:goods_id type:int require:1 default:-- desc:商品id
  29. * @return name:order_id type:int require:0 default:0 desc:成功时返回订单id
  30. */
  31. public function createOrder()
  32. {
  33. $goods_id = input("post.goods_id");
  34. $num = input("post.num",1);
  35. $msg= ''; $com=true; $order_id = 0;
  36. Db::startTrans();
  37. try {
  38. $user_info = Db::name('store_member')->where('id',$this->uid)->find();
  39. if(!$user_info['is_auth']) throw new Exception('请实名认证后购买!');
  40. $goods_info = Db::table('store_goods')->where(['id'=>$goods_id,'type'=>1,'status'=>1,'is_deleted'=>0])->find();
  41. if(empty($goods_info)) throw new Exception('商品已下架,嘤嘤嘤~~~');
  42. if(strtotime($goods_info['sell_time']) > time()) throw new Exception('商品还未开始售卖!请耐心等待');
  43. if($goods_info['stock'] <= 0) throw new Exception('商品已售罄~~');
  44. if($goods_info['goods_price'] <= 0) throw new Exception('商品价格有误');
  45. $pro_info = [
  46. 'type'=>$goods_info['type'],
  47. 'is_gift'=>$goods_info['is_gift'],
  48. 'crystal'=>$goods_info['crystal'],
  49. 'gift_day'=>$goods_info['gift_day'],
  50. 'goods_price'=>$goods_info['goods_price'],
  51. ];
  52. $order_int =[
  53. 'uid' => $this->uid,
  54. 'order_no' => get_order_sn(),
  55. 'price_total' => $goods_info['goods_price'] * $num,
  56. 'goods_num' => $num,
  57. 'goods_id' => $goods_id,
  58. 'goods_cover' => $goods_info['cover'],
  59. 'goods_name' => $goods_info['name'],
  60. 'create_at' => date('Y-m-d H:i:s'),
  61. 'pro_info' => json_encode($pro_info),
  62. ];
  63. //扣减库存
  64. Db::table('store_goods')->where('id',$goods_id)->setDec('stock',$num);
  65. // 生成订单
  66. Db::table('goods_order')->insert($order_int);
  67. $order_id = Db::getLastInsID();
  68. Db::commit();
  69. }catch (\Exception $e){
  70. Db::rollback();
  71. $com=false;
  72. $msg = $e->getMessage();
  73. }
  74. if($com){
  75. $this->success('ok',['order_id'=>$order_id]);
  76. }else{
  77. $this->error($msg);
  78. }
  79. }
  80. /**
  81. * @title 获取订单详情
  82. * @desc 获取订单详情
  83. * @author qc
  84. * @url /api/Goods_order/getOrderDetail
  85. * @method GET
  86. * @header name:Authorization require:1 desc:Token
  87. * @param name:id type:int require:1 default:-- desc:订单id
  88. * @return name:order_no type:string default:-- desc:订单号
  89. * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),9取消
  90. * @return name:price_total type:float default:-- desc:订单金额
  91. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  92. * @return name:pay_no type:string default:-- desc:支付单号
  93. * @return name:create_at type:string default:-- desc:订单创建时间
  94. * @return name:pay_type type:string default:-- desc:支付方式(0未支付,1微信)
  95. * @return name:pay_time type:string default:-- desc:支付时间
  96. * @return name:cancel_state type:int default:-- desc:取消状态
  97. * @return name:goods_id type:int default:-- desc:商品id
  98. * @return name:goods_num type:int default:-- desc:商品数量
  99. * @return name:goods_cover type:string default:-- desc:商品封面
  100. * @return name:goods_auth type:string default:-- desc:创作者
  101. * @return name:auth_img type:string default:-- desc:创作者头像
  102. * @return name:issuer type:string default:-- desc:发行方
  103. * @return name:detail type:string default:-- desc:详情
  104. * @return name:buy_notice type:string default:-- desc:购买须知
  105. * @return name:pay_content type:string default:-- desc:支付弹窗内容
  106. * @return name:pay_pic type:string default:-- desc:支付弹窗图片
  107. * @return name:adv_type type:int default:-- desc:广告类型(1图片,2mp3,3视频)
  108. * @return name:adv_cover type:string default:-- desc:广告图片
  109. * @return name:mp3_url type:string default:-- desc:广告mp3
  110. * @return name:audio_url type:string default:-- desc:广告视频
  111. */
  112. public function getOrderDetail()
  113. {
  114. $order_id = input('id',0);
  115. $detail = Db::table('goods_order')->alias('o')
  116. ->field('o.*,g.goods_auth,g.auth_img,g.goods_auth,g.issuer,g.detail,g.buy_notice,g.pay_content,g.pay_pic,g.type as goods_type,g.adv_type,g.adv_cover,g.mp3_url,g.audio_line,g.audio_url,g.vir_num')
  117. ->join('store_goods g','g.id = o.goods_id','LEFT')
  118. ->find($order_id);
  119. $this->success('获取成功',['detail'=>$detail]);
  120. }
  121. /**
  122. * @title 订单支付
  123. * @desc 订单支付
  124. * @author qc
  125. * @url /api/Goods_order/payOrder
  126. * @method POST
  127. * @header name:Authorization require:1 desc:Token
  128. * @param name:id type:int require:0 default:0 desc:订单id
  129. * @return name:config type:array default:0 desc:支付配置
  130. * @return name:config.appId type:string default:-- desc:公众账号ID
  131. * @return name:config.nonceStr type:string default:-- desc:随机字符串
  132. * @return name:config.signType type:string default:-- desc:签名类型
  133. * @return name:config.paySign type:string default:-- desc:签名
  134. * @return name:config.timestamp type:string default:-- desc:时间戳
  135. * @return name:goods_info type:array default:-- desc:商品
  136. * @return name:goods_info.pay_pic type:string default:-- desc:支付弹窗图片
  137. * @return name:goods_info.pay_content type:string default:-- desc:支付弹窗内容
  138. */
  139. public function payOrder(){
  140. Db::startTrans();
  141. $msg= 'ok';$config=false;
  142. try{
  143. $order_id = input('post.id',0);
  144. $order_info = Db::table('goods_order')->find($order_id);
  145. if(empty($order_info)) throw new Exception('订单不存在');
  146. if($order_info['status'] != 0 ) throw new Exception('订单状态有误!');
  147. $user_info = Db::table('store_member')->find($this->uid);
  148. if(!$user_info['openid']) throw new Exception('用户openid为空!');
  149. $goods_info = Db::table('store_goods')->field('pay_pic,pay_content')->find($order_info['goods_id']);
  150. $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
  151. if(!$order_info['pay_no'])Db::table('goods_order')->where(['id'=>$order_id])->update(['pay_no'=>$pay_no]);
  152. $notify_url = $this->request->root(true) . '/api/Pay/goodsOrderNotify';
  153. $config = Pay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
  154. $config ? Db::commit() :Db::rollback();
  155. }catch (\Exception $e){
  156. Db::rollback();
  157. $msg = $e->getMessage();
  158. }
  159. $config ? $this->success($msg,['config'=>$config,'goods_info'=>$goods_info]) : $this->error($msg);
  160. }
  161. /**
  162. * @title 取消订单
  163. * @desc 取消订单
  164. * @author qc
  165. * @url /api/Goods_order/cancelOrder
  166. * @method POST
  167. * @header name:Authorization require:1 desc:Token
  168. * @param name:id type:int require:1 default:-- desc:订单id
  169. */
  170. public function cancelOrder()
  171. {
  172. $is_commit = true;
  173. Db::startTrans();
  174. try {
  175. $order_id = input('post.id');
  176. $order_detail = Db::table('goods_order')->where(['id'=>$order_id,'uid'=>$this->uid])->find();
  177. if(empty($order_detail)) throw new Exception('订单不存在');
  178. if($order_detail['pay_state'] != 0) throw new Exception('订单已支付');
  179. if($order_detail['cancel_state'] != 0) throw new Exception('订单已取消,不能重复取消');
  180. //恢复库存
  181. Db::table('store_goods')->where('id',$order_detail['goods_id'])->setInc('stock',$order_detail['goods_num']);
  182. // 取消订单
  183. Db::table('goods_order')
  184. ->where(['id'=>$order_id])
  185. ->update(['cancel_state'=>1,'status'=>9]);
  186. Db::commit();
  187. }catch (\Exception $e){
  188. Db::rollback();
  189. $is_commit = false;
  190. $msg = $e->getMessage();
  191. }
  192. $is_commit ? $this->success('取消成功') : $this->error($msg);
  193. }
  194. /**
  195. * @title 获取订单列表
  196. * @desc 获取订单列表
  197. * @author qc
  198. * @url /api/Goods_order/getMyOrderList
  199. * @method GET
  200. * @header name:Authorization require:1 desc:Token
  201. * @param name:page type:int : default:1 desc:页数
  202. * @param name:page_num type:int : default:20 desc:每页数
  203. * @param name:status type:int : default:-1 desc:订单状态(-1全部,0待支付,1已支付(待发货),9取消)
  204. * @return name:order_no type:string default:-- desc:订单号
  205. * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),9取消
  206. * @return name:price_total type:float default:-- desc:订单金额
  207. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  208. * @return name:pay_no type:string default:-- desc:支付单号
  209. * @return name:create_at type:string default:-- desc:订单创建时间
  210. * @return name:pay_type type:string default:-- desc:支付方式(0未支付,1微信)
  211. * @return name:pay_time type:string default:-- desc:支付时间
  212. * @return name:cancel_state type:int default:-- desc:取消状态
  213. * @return name:goods_id type:int default:-- desc:商品id
  214. * @return name:goods_num type:int default:-- desc:商品数量
  215. * @return name:goods_cover type:int default:-- desc:商品封面
  216. * @return name:past_int type:int default:-- desc:订单多少秒后过期
  217. * @return name:goods_name type:int default:-- desc:商品名称
  218. * @return name:goods_auth type:int default:-- desc:作者
  219. * @return name:pay_content type:string default:-- desc:支付弹窗内容
  220. * @return name:pay_pic type:string default:-- desc:支付弹窗图片
  221. */
  222. public function getMyOrderList()
  223. {
  224. $status = input('status',-1);
  225. $where=[];
  226. if($status > -1) $where['o.status'] = $status;
  227. $where['o.uid'] = $this->uid;
  228. $where['o.is_deleted'] = 0;
  229. $list = Db::table('goods_order')->alias('o')
  230. ->field('o.*,g.goods_auth,g.pay_content,g.pay_pic,g.vir_num')
  231. ->join('store_goods g','g.id = o.goods_id','LEFT')
  232. ->where($where)
  233. ->order('id desc')
  234. ->limit($this->off_set,$this->page_num)
  235. ->select();
  236. $set_time = intval(sysconf('cancel_time')) ? : 10;// 没设置默认5分钟未支付的自动取消
  237. array_walk($list,function (&$val) use ($set_time){
  238. $val['past_int'] = 0;
  239. if($val['status'] == 0 && strtotime($val['create_at']) + $set_time * 60 > time()){
  240. $val['past_int'] = strtotime($val['create_at']) + $set_time * 60 - time();
  241. }
  242. });
  243. $this->success('获取成功',['list'=>$list]);
  244. }
  245. /**
  246. * @title 删除订单
  247. * @desc 删除订单
  248. * @author qc
  249. * @url /api/Goods_order/delOrder
  250. * @method POST
  251. * @header name:Authorization require:1 desc:Token
  252. * @param name:id type:int require:1 default:1 desc:订单id
  253. */
  254. public function delOrder()
  255. {
  256. $order_id = input('post.id');
  257. $order_info = Db::table('goods_order')
  258. ->where(['uid'=>$this->uid,'is_deleted'=>0,'id'=>$order_id])
  259. ->find();
  260. if(empty($order_info)) $this->error('订单不存在');
  261. if($order_info['status'] == 0) $this->error('未取消订单不能删除');
  262. Db::table('goods_order')->where(['id'=>$order_id])->update(['is_deleted'=>1]);
  263. $this->success('删除成功');
  264. }
  265. }