Order.php 9.4 KB

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