123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Gold
- * Date: 2024/1/16
- * Time: 9:25
- */
- namespace app\api\controller;
- use app\admin\model\order\Order as OrderModel; //订单
- use app\admin\model\order\OrderGoods; //订单商品
- use app\admin\model\books\Books; //教材
- use app\admin\model\video\Video; //教材
- use app\api\controller\Pay; //教材
- use app\common\controller\Api;
- use think\Db;
- class Order extends Api
- {
- protected $noNeedRight = ['*'];
- /**
- * 创建订单
- *
- */
- public function createOrder(){
- $data = input();
- $uid = $this->auth->id;
- $OrderModel = new OrderModel();
- $OrderGoods = new OrderGoods();
- $Books = new Books();
- $Video = new Video();
- if(!isset($data['type']) || !$data['goods_id']) $this->error('参数错误');
- if($data['type'] == '0'){
- $total_price = $Books->where('id','in',$data['goods_id'])->sum('price');
- }else if($data['type'] == '1'){
- $total_price = $Video->where('id','in',$data['goods_id'])->sum('price');
- }
- // return $total_price;
- $order_arr = [
- 'user_id' => $uid,
- 'order_on' => $this->get_order_sn(),
- 'type' => $data['type'],
- 'total_price' => $total_price
- ];
- Db::startTrans();
- try {
- $save = $OrderModel->save($order_arr);
- $goods_id = explode(',',$data['goods_id']);
- if(count($goods_id) >= 1){
- foreach ($goods_id as $k => $v){
- if($data['type'] == 0){
- $price = $Books->where('id',$v)->column('price');
- $arr = [
- 'order_id' => $OrderModel->id,
- 'goods_type' => $data['type'],
- 'goods_id' => $v,
- 'price' => (int)$price[0],
- ];
- $OrderGoods = new OrderGoods();
- $OrderGoods->save($arr);
- }else if($data['type'] == 1){
- $price = $Video->where('id',$v)->column('price');
- $arr = [
- 'order_id' => $OrderModel->id,
- 'goods_type' => $data['type'],
- 'goods_id' => $v,
- 'price' => (int)$price[0],
- ];
- $OrderGoods = new OrderGoods();
- $OrderGoods->save($arr);
- }
- }
- }
- Db::commit();
- } catch (Exception $e) {
- $this->setError($e->getMessage());
- Db::rollback();
- return false;
- }
- $this->success('操作成功',$OrderModel);
- }
- /**
- * 订单列表
- *
- */
- public function getOrderList(){
- $data = input();
- $where = [];
- $uid = $this->auth->id;
- $OrderModel = new OrderModel();
- if(!isset($data['page']) || $data['page'] == '' || $data['page'] == null){
- $page = 1;
- }else{
- $page = $data['page'];
- }
- if(!isset($data['limit']) || $data['limit'] == '' || $data['limit'] == null){
- $limit = 20;
- }else{
- $limit = $data['limit'];
- }
- if(isset($data['status']) && $data['status'] != '' && $data['status'] != null){
- $where['status'] = $data['status'];
- }
- $where['user_id'] = $uid;
- $where['is_deleted'] = 1;
- $list = $OrderModel->where($where)->with(['goods'])->paginate([
- 'list_rows'=> $limit,
- 'page' => $page,
- ]);
- $lists = $list->toArray();
- foreach ($lists['data'] as $key => $item){
- foreach ($item['goods'] as $k => $v){
- if($v['goods_type'] == 0){
- $Books = new Books();
- $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();
- $good['is_free'] = 1;
- if($good['price'] <= 0){
- $good['is_free'] = 0;
- }
- $lists['data'][$key]['goods'][$k]['good_item'] = $good;
- }else if($v['goods_type'] == 1){
- $Video = new Video();
- $lists['data'][$key]['goods'][$k]['good_item'] = $Video->where('id',$v['goods_id'])->find();
- }
- }
- }
- $this->success('操作成功',$lists);
- }
- /**
- * 订单详情
- *
- */
- public function getOrderInfo(){
- $data = input();
- $where = [];
- $uid = $this->auth->id;
- $OrderModel = new OrderModel();
- if(!isset($data['id']) || $data['id'] == '' || $data['id'] == null){
- $this->error('参数错误');
- }
- $list = $OrderModel->where('id',$data['id'])->where('user_id',$uid)->where('is_deleted',1)->with(['goods'])->find();
- if(!$list){
- $this->error('订单不存在');
- }
- $lists = $list->toArray();
- foreach ($lists['goods'] as $k => $v){
- if($v['goods_type'] == 0){
- $Books = new Books();
- $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();
- $good['is_free'] = 1;
- if($good['price'] <= 0){
- $good['is_free'] = 0;
- }
- $lists['goods'][$k]['good_item'] = $good;
- }else if($v['goods_type'] == 1){
- $Video = new Video();
- $lists['goods'][$k]['good_item'] = $Video->where('id',$v['goods_id'])->find();
- }
- }
- $lists['createtime'] = date('Y-m-d H:i:s', $lists['createtime']);
- $this->success('操作成功',$lists);
- }
- /**
- * 取消订单
- *
- */
- public function cancellationOrder(){
- $data = input();
- $where = [];
- $uid = $this->auth->id;
- $OrderModel = new OrderModel();
- if(!isset($data['id']) || $data['id'] == '' || $data['id'] == null){
- $this->error('参数错误');
- }
- $arr = [
- 'status' => -1
- ];
- if($OrderModel->where('user_id',$uid)->where('id',$data['id'])->update($arr)){
- $this->success('操作成功');
- }else{
- $this->error('操作失败');
- };
- }
- /**
- * 删除订单
- *
- */
- public function delOrder(){
- $data = input();
- $where = [];
- $uid = $this->auth->id;
- $OrderModel = new OrderModel();
- if(!isset($data['id']) || $data['id'] == '' || $data['id'] == null){
- $this->error('参数错误');
- }
- $arr = [
- 'is_deleted' => 0
- ];
- if($OrderModel->where('user_id',$uid)->where('id',$data['id'])->update($arr)){
- $this->success('操作成功');
- }else{
- $this->error('操作失败');
- };
- }
- /**
- * 订单支付
- *
- */
- public function orderPay(){
- $data = input();
- $OrderModel = new OrderModel();
- $order = $OrderModel->where('status',0)->where('id',$data['order_id'])->find();
- if(!$order){
- $this->error('订单不存在');
- }
- $Pay = new Pay();
- if($data['pay_type'] == 0){
- //微信
- $arr = [
- 'total_fee' => $order['total_price']*100,
- 'out_trade_no' => $order['order_on'],
- 'trade_type' => $data['trade_type'],
- ];
- return $Pay->wxpay($arr);
- }else if($data['pay_type'] == 1){
- //支付宝
- $arr = [
- 'total_amount' => $order['total_price'],
- 'out_trade_no' => $order['order_on'],
- 'subject' => '购买',
- 'type' => $data['trade_type'],
- ];
- return $Pay->alipay($arr);
- }
- }
- /**
- * 生成订单号
- *
- */
- public function get_order_sn(){
- $order_id_main = date('YmdHis') . rand(10000000,99999999);
- $order_id_len = strlen($order_id_main);
- $order_id_sum = 0;
- for($i=0; $i<$order_id_len; $i++){
- $order_id_sum += (int)(substr($order_id_main,$i,1));
- }
- $osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);
- return $osn;
- }
- }
|