Order.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Gold
  5. * Date: 2024/1/16
  6. * Time: 9:25
  7. */
  8. namespace app\api\controller;
  9. use app\admin\model\order\Order as OrderModel; //订单
  10. use app\admin\model\order\OrderGoods; //订单商品
  11. use app\admin\model\books\Books; //教材
  12. use app\admin\model\video\Video; //教材
  13. use app\api\controller\Pay; //教材
  14. use app\common\controller\Api;
  15. use think\Db;
  16. class Order extends Api
  17. {
  18. protected $noNeedRight = ['*'];
  19. /**
  20. * 创建订单
  21. *
  22. */
  23. public function createOrder(){
  24. $data = input();
  25. $uid = $this->auth->id;
  26. $OrderModel = new OrderModel();
  27. $OrderGoods = new OrderGoods();
  28. $Books = new Books();
  29. $Video = new Video();
  30. if(!isset($data['type']) || !$data['goods_id']) $this->error('参数错误');
  31. if($data['type'] == '0'){
  32. $total_price = $Books->where('id','in',$data['goods_id'])->sum('price');
  33. }else if($data['type'] == '1'){
  34. $total_price = $Video->where('id','in',$data['goods_id'])->sum('price');
  35. }
  36. // return $total_price;
  37. $order_arr = [
  38. 'user_id' => $uid,
  39. 'order_on' => $this->get_order_sn(),
  40. 'type' => $data['type'],
  41. 'total_price' => $total_price
  42. ];
  43. Db::startTrans();
  44. try {
  45. $save = $OrderModel->save($order_arr);
  46. $goods_id = explode(',',$data['goods_id']);
  47. if(count($goods_id) >= 1){
  48. foreach ($goods_id as $k => $v){
  49. if($data['type'] == 0){
  50. $price = $Books->where('id',$v)->column('price');
  51. $arr = [
  52. 'order_id' => $OrderModel->id,
  53. 'goods_type' => $data['type'],
  54. 'goods_id' => $v,
  55. 'price' => (int)$price[0],
  56. ];
  57. $OrderGoods = new OrderGoods();
  58. $OrderGoods->save($arr);
  59. }else if($data['type'] == 1){
  60. $price = $Video->where('id',$v)->column('price');
  61. $arr = [
  62. 'order_id' => $OrderModel->id,
  63. 'goods_type' => $data['type'],
  64. 'goods_id' => $v,
  65. 'price' => (int)$price[0],
  66. ];
  67. $OrderGoods = new OrderGoods();
  68. $OrderGoods->save($arr);
  69. }
  70. }
  71. }
  72. Db::commit();
  73. } catch (Exception $e) {
  74. $this->setError($e->getMessage());
  75. Db::rollback();
  76. return false;
  77. }
  78. $this->success('操作成功',$OrderModel);
  79. }
  80. /**
  81. * 订单列表
  82. *
  83. */
  84. public function getOrderList(){
  85. $data = input();
  86. $where = [];
  87. $uid = $this->auth->id;
  88. $OrderModel = new OrderModel();
  89. if(!isset($data['page']) || $data['page'] == '' || $data['page'] == null){
  90. $page = 1;
  91. }else{
  92. $page = $data['page'];
  93. }
  94. if(!isset($data['limit']) || $data['limit'] == '' || $data['limit'] == null){
  95. $limit = 20;
  96. }else{
  97. $limit = $data['limit'];
  98. }
  99. if(isset($data['status']) && $data['status'] != '' && $data['status'] != null){
  100. $where['status'] = $data['status'];
  101. }
  102. $where['user_id'] = $uid;
  103. $where['is_deleted'] = 1;
  104. $list = $OrderModel->where($where)->with(['goods'])->paginate([
  105. 'list_rows'=> $limit,
  106. 'page' => $page,
  107. ]);
  108. $lists = $list->toArray();
  109. foreach ($lists['data'] as $key => $item){
  110. foreach ($item['goods'] as $k => $v){
  111. if($v['goods_type'] == 0){
  112. $Books = new Books();
  113. $good = $Books->where('id',$v['goods_id'])->field('id,title,image,book_type,is_new_recommend,author,is_awards,price,entity_price,createtime,updatetime,level_id,arrangement_id,speciality_id,series_id,is_series')->find();
  114. $good['is_free'] = 1;
  115. if($good['price'] <= 0){
  116. $good['is_free'] = 0;
  117. }
  118. $lists['data'][$key]['goods'][$k]['good_item'] = $good;
  119. }else if($v['goods_type'] == 1){
  120. $Video = new Video();
  121. $lists['data'][$key]['goods'][$k]['good_item'] = $Video->where('id',$v['goods_id'])->find();
  122. }
  123. }
  124. }
  125. $this->success('操作成功',$lists);
  126. }
  127. /**
  128. * 订单详情
  129. *
  130. */
  131. public function getOrderInfo(){
  132. $data = input();
  133. $where = [];
  134. $uid = $this->auth->id;
  135. $OrderModel = new OrderModel();
  136. if(!isset($data['id']) || $data['id'] == '' || $data['id'] == null){
  137. $this->error('参数错误');
  138. }
  139. $list = $OrderModel->where('id',$data['id'])->where('user_id',$uid)->where('is_deleted',1)->with(['goods'])->find();
  140. if(!$list){
  141. $this->error('订单不存在');
  142. }
  143. $lists = $list->toArray();
  144. foreach ($lists['goods'] as $k => $v){
  145. if($v['goods_type'] == 0){
  146. $Books = new Books();
  147. $good = $Books->where('id',$v['goods_id'])->field('id,title,image,book_type,is_new_recommend,author,is_awards,price,entity_price,createtime,updatetime,level_id,arrangement_id,speciality_id,series_id,is_series')->find();
  148. $good['is_free'] = 1;
  149. if($good['price'] <= 0){
  150. $good['is_free'] = 0;
  151. }
  152. $lists['goods'][$k]['good_item'] = $good;
  153. }else if($v['goods_type'] == 1){
  154. $Video = new Video();
  155. $lists['goods'][$k]['good_item'] = $Video->where('id',$v['goods_id'])->find();
  156. }
  157. }
  158. $lists['createtime'] = date('Y-m-d H:i:s', $lists['createtime']);
  159. $this->success('操作成功',$lists);
  160. }
  161. /**
  162. * 取消订单
  163. *
  164. */
  165. public function cancellationOrder(){
  166. $data = input();
  167. $where = [];
  168. $uid = $this->auth->id;
  169. $OrderModel = new OrderModel();
  170. if(!isset($data['id']) || $data['id'] == '' || $data['id'] == null){
  171. $this->error('参数错误');
  172. }
  173. $arr = [
  174. 'status' => -1
  175. ];
  176. if($OrderModel->where('user_id',$uid)->where('id',$data['id'])->update($arr)){
  177. $this->success('操作成功');
  178. }else{
  179. $this->error('操作失败');
  180. };
  181. }
  182. /**
  183. * 删除订单
  184. *
  185. */
  186. public function delOrder(){
  187. $data = input();
  188. $where = [];
  189. $uid = $this->auth->id;
  190. $OrderModel = new OrderModel();
  191. if(!isset($data['id']) || $data['id'] == '' || $data['id'] == null){
  192. $this->error('参数错误');
  193. }
  194. $arr = [
  195. 'is_deleted' => 0
  196. ];
  197. if($OrderModel->where('user_id',$uid)->where('id',$data['id'])->update($arr)){
  198. $this->success('操作成功');
  199. }else{
  200. $this->error('操作失败');
  201. };
  202. }
  203. /**
  204. * 订单支付
  205. *
  206. */
  207. public function orderPay(){
  208. $data = input();
  209. $OrderModel = new OrderModel();
  210. $order = $OrderModel->where('status',0)->where('id',$data['order_id'])->find();
  211. if(!$order){
  212. $this->error('订单不存在');
  213. }
  214. $Pay = new Pay();
  215. if($data['pay_type'] == 0){
  216. //微信
  217. $arr = [
  218. 'total_fee' => $order['total_price']*100,
  219. 'out_trade_no' => $order['order_on'],
  220. 'trade_type' => $data['trade_type'],
  221. ];
  222. return $Pay->wxpay($arr);
  223. }else if($data['pay_type'] == 1){
  224. //支付宝
  225. $arr = [
  226. 'total_amount' => $order['total_price'],
  227. 'out_trade_no' => $order['order_on'],
  228. 'subject' => '购买',
  229. 'type' => $data['trade_type'],
  230. ];
  231. return $Pay->alipay($arr);
  232. }
  233. }
  234. /**
  235. * 生成订单号
  236. *
  237. */
  238. public function get_order_sn(){
  239. $order_id_main = date('YmdHis') . rand(10000000,99999999);
  240. $order_id_len = strlen($order_id_main);
  241. $order_id_sum = 0;
  242. for($i=0; $i<$order_id_len; $i++){
  243. $order_id_sum += (int)(substr($order_id_main,$i,1));
  244. }
  245. $osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);
  246. return $osn;
  247. }
  248. }