Hexiao.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 核销
  7. */
  8. class Hexiao extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. //订单详情
  13. //code:order_hexiao_no|2024090466d7c9d5abb1c1
  14. //order_no:2024090466d7c9d5abb1c1
  15. public function order_info()
  16. {
  17. if($this->auth->is_hexiao != 1){$this->error('没有核销权限');}
  18. $code = input('code','');
  19. if(strpos($code,'order_hexiao_no|') !== 0){
  20. $this->error('识别不到的订单');
  21. }
  22. $order_no = substr($code,16);
  23. $order_info = Db::name('unishop_order')->where('out_trade_no',$order_no)->find();
  24. if(empty($order_info)){
  25. $this->error('不存在的订单');
  26. }
  27. if($order_info['status'] != 1){
  28. $this->error('非正常的订单');
  29. }
  30. if($order_info['have_paid'] == 0){
  31. $this->error('未支付的订单');
  32. }
  33. if($order_info['have_received'] != 0){
  34. $this->error('该订单已核销');
  35. }
  36. //下面是从unishop/order.php detail方法里拿过来的
  37. $order_id = $order_info['id'];
  38. $orderModel = new \addons\unishop\model\Order();
  39. $order = $orderModel
  40. ->with([
  41. 'products' => function ($query) {
  42. $query->field('id,order_id,image,number,price,spec,title,product_id');
  43. },
  44. ])
  45. ->where(['id' => $order_id])->find();
  46. if ($order) {
  47. $order = $order->append(['state', 'paidtime'])->toArray();
  48. foreach ($order['products'] as &$product) {
  49. $product['image'] = cdnurl($product['image']);
  50. }
  51. unset($order['pay_out_trade_no']);
  52. }
  53. $this->success(1,$order);
  54. }
  55. //完成核销动作
  56. public function hexiao(){
  57. if($this->auth->is_hexiao != 1){$this->error('没有核销权限');}
  58. $code = input('code','');
  59. if(strpos($code,'order_hexiao_no|') !== 0){
  60. $this->error('识别不到的订单');
  61. }
  62. $out_trade_no = substr($code,16);
  63. Db::startTrans();
  64. $order = Db::name('unishop_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  65. if(empty($order_info)){
  66. Db::rollback();
  67. $this->error('不存在的订单');
  68. }
  69. if($order['status'] != 1){
  70. Db::rollback();
  71. $this->error('非正常的订单');
  72. }
  73. if($order['have_paid'] == 0){
  74. Db::rollback();
  75. $this->error('未支付的订单');
  76. }
  77. if($order['have_received'] != 0){
  78. Db::rollback();
  79. $this->error('该订单已核销');
  80. }
  81. $update = [
  82. 'have_received' => time(),
  83. 'hexiao_uid' => $this->auth->id,
  84. ];
  85. $order_rs = Db::name('unishop_order')->where('id',$order['id'])->update($update);
  86. if($order_rs === false){
  87. Db::rollback();
  88. $this->error('核销失败');
  89. }
  90. //给核销人结算
  91. $bili = config('site.unishop_order_hexiaomoney_bili');
  92. $money = bcdiv(bcmul($order['total_price'],$bili,2),100,2);
  93. if($money > 0){
  94. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'hexiaomoney',$money,201,'核销订单:'.$out_trade_no,'unishop_order',$order['id']);
  95. if($rs_wallet['status']===false)
  96. {
  97. Db::rollback();
  98. $this->error($rs_wallet['msg']);
  99. }
  100. }
  101. Db::commit();
  102. $this->success('核销成功');
  103. }
  104. //核销记录
  105. public function order_list(){
  106. if($this->auth->group_id != 3){$this->error('你是不是来错地方了?');}
  107. $orderModel = new \addons\unishop\model\Order();
  108. $condition = [
  109. 'hexiao_uid' => $this->auth->id,
  110. 'status' => 1,
  111. 'have_paid' => ['gt',0],
  112. 'have_delivered' => ['gt',0],
  113. 'have_received' => ['gt',0],
  114. ];
  115. $result = $orderModel
  116. ->with([
  117. 'products' => function($query) {
  118. $query->field('id,title,image,number,price,spec,order_id,product_id');
  119. },
  120. ])
  121. ->where($condition)
  122. ->order(['have_received' => 'desc'])
  123. ->autopage()
  124. ->select();
  125. //预约日期
  126. $week_data = [
  127. 0 => '周日',
  128. 1 => '周一',
  129. 2 => '周二',
  130. 3 => '周三',
  131. 4 => '周四',
  132. 5 => '周五',
  133. 6 => '周六',
  134. 7 => '周日',
  135. ];
  136. foreach ($result as &$item) {
  137. $item->append(['order_id','state']);
  138. $item = $item->toArray();
  139. unset($item['pay_out_trade_no']);
  140. /*$evaluate = array_column($item['evaluate'], 'product_id');
  141. $refundProducts = array_column($item['refund_products'], 'order_product_id');
  142. unset($item['evaluate']);
  143. unset($item['refund_products']);*/
  144. foreach ($item['products'] as &$product) {
  145. $product['image'] = cdnurl($product['image']);
  146. // 是否已评论
  147. /*if (in_array($product['id'], $evaluate)) {
  148. $product['evaluate'] = true;
  149. } else {
  150. $product['evaluate'] = false;
  151. }*/
  152. // 是否退货
  153. /*if ($item['refund_status'] == self::REFUND_STATUS_AGREE && in_array($product['order_product_id'], $refundProducts)) {
  154. $product['refund'] = true;
  155. } else {
  156. $product['refund'] = false;
  157. }*/
  158. }
  159. $item['bookdate'] = date('Y-m-d H:i',$item['booktime']) .' '. $week_data[date('w',$item['booktime'])];
  160. }
  161. $this->success(1,$result);
  162. }
  163. }