Order.php 8.4 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. $this->success('操作成功',$lists);
  159. }
  160. /**
  161. * 取消订单
  162. *
  163. */
  164. public function cancellationOrder(){
  165. $data = input();
  166. $where = [];
  167. $uid = $this->auth->id;
  168. $OrderModel = new OrderModel();
  169. if(!isset($data['id']) || $data['id'] == '' || $data['id'] == null){
  170. $this->error('参数错误');
  171. }
  172. $arr = [
  173. 'status' => -1
  174. ];
  175. if($OrderModel->where('user_id',$uid)->where('id',$data['id'])->update($arr)){
  176. $this->success('操作成功');
  177. }else{
  178. $this->error('操作失败');
  179. };
  180. }
  181. /**
  182. * 删除订单
  183. *
  184. */
  185. public function delOrder(){
  186. $data = input();
  187. $where = [];
  188. $uid = $this->auth->id;
  189. $OrderModel = new OrderModel();
  190. if(!isset($data['id']) || $data['id'] == '' || $data['id'] == null){
  191. $this->error('参数错误');
  192. }
  193. $arr = [
  194. 'is_deleted' => 0
  195. ];
  196. if($OrderModel->where('user_id',$uid)->where('id',$data['id'])->update($arr)){
  197. $this->success('操作成功');
  198. }else{
  199. $this->error('操作失败');
  200. };
  201. }
  202. /**
  203. * 订单支付
  204. *
  205. */
  206. public function orderPay(){
  207. $data = input();
  208. $OrderModel = new OrderModel();
  209. $order = $OrderModel->where('status',0)->where('id',$data['order_id'])->find();
  210. if(!$order){
  211. $this->error('订单不存在');
  212. }
  213. $Pay = new Pay();
  214. if($data['pay_type'] == 0){
  215. //微信
  216. $arr = [
  217. 'total_fee' => $order['total_price']*100,
  218. 'out_trade_no' => $order['order_on'],
  219. 'trade_type' => $data['trade_type'],
  220. ];
  221. return $Pay->wxpay($arr);
  222. }else if($data['pay_type'] == 1){
  223. //支付宝
  224. $arr = [
  225. 'total_amount' => $order['total_price'],
  226. 'out_trade_no' => $order['order_on'],
  227. 'subject' => '购买',
  228. 'type' => $data['trade_type'],
  229. ];
  230. return $Pay->alipay($arr);
  231. }
  232. }
  233. /**
  234. * 生成订单号
  235. *
  236. */
  237. public function get_order_sn(){
  238. $order_id_main = date('YmdHis') . rand(10000000,99999999);
  239. $order_id_len = strlen($order_id_main);
  240. $order_id_sum = 0;
  241. for($i=0; $i<$order_id_len; $i++){
  242. $order_id_sum += (int)(substr($order_id_main,$i,1));
  243. }
  244. $osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);
  245. return $osn;
  246. }
  247. }