Hexiao.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. public function __construct(){
  13. parent::__construct();
  14. if($this->auth->is_hexiao != 1){$this->error('没有核销权限');}
  15. }
  16. //首页
  17. public function index(){
  18. $wallet = Db::name('user_wallet')->where(['user_id' => $this->auth->id])->find();
  19. $rs = [
  20. 'hexiaomoney' => $wallet['hexiaomoney'],
  21. 'takecash' => $wallet['hexiaomoney'],
  22. 'wait' => 0,
  23. ];
  24. $this->success(1,$rs);
  25. }
  26. //订单详情
  27. //code:order_hexiao_no|2024090466d7c9d5abb1c1
  28. //order_no:2024090466d7c9d5abb1c1
  29. public function order_info()
  30. {
  31. $code = input('code','');
  32. if(strpos($code,'order_hexiao_no|') !== 0){
  33. $this->error('识别不到的订单');
  34. }
  35. $order_no = substr($code,16);
  36. $order_info = Db::name('unishop_order')->where('out_trade_no',$order_no)->find();
  37. if(empty($order_info)){
  38. $this->error('不存在的订单');
  39. }
  40. if($order_info['status'] != 1){
  41. $this->error('非正常的订单');
  42. }
  43. if($order_info['have_paid'] == 0){
  44. $this->error('未支付的订单');
  45. }
  46. if($order_info['have_received'] != 0){
  47. $this->error('该订单已核销');
  48. }
  49. //下面是从unishop/order.php detail方法里拿过来的
  50. $order_id = $order_info['id'];
  51. $orderModel = new \addons\unishop\model\Order();
  52. $order = $orderModel
  53. ->with([
  54. 'products' => function ($query) {
  55. $query->field('id,order_id,image,number,price,spec,title,product_id');
  56. },
  57. ])
  58. ->where(['id' => $order_id])->find();
  59. if ($order) {
  60. $order = $order->append(['state', 'paidtime'])->toArray();
  61. foreach ($order['products'] as &$product) {
  62. $product['image'] = cdnurl($product['image']);
  63. }
  64. unset($order['pay_out_trade_no']);
  65. }
  66. $this->success(1,$order);
  67. }
  68. //完成核销动作
  69. public function hexiao(){
  70. $code = input('code','');
  71. if(strpos($code,'order_hexiao_no|') !== 0){
  72. $this->error('识别不到的订单');
  73. }
  74. $out_trade_no = substr($code,16);
  75. Db::startTrans();
  76. $order = Db::name('unishop_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  77. if(empty($order)){
  78. Db::rollback();
  79. $this->error('不存在的订单');
  80. }
  81. if($order['status'] != 1){
  82. Db::rollback();
  83. $this->error('非正常的订单');
  84. }
  85. if($order['have_paid'] == 0){
  86. Db::rollback();
  87. $this->error('未支付的订单');
  88. }
  89. if($order['have_received'] != 0){
  90. Db::rollback();
  91. $this->error('该订单已核销');
  92. }
  93. $update = [
  94. 'have_received' => time(),
  95. 'hexiao_uid' => $this->auth->id,
  96. ];
  97. $order_rs = Db::name('unishop_order')->where('id',$order['id'])->update($update);
  98. if($order_rs === false){
  99. Db::rollback();
  100. $this->error('核销失败');
  101. }
  102. //给核销人结算
  103. $bili = config('site.unishop_order_hexiaomoney_bili');
  104. $money = bcdiv(bcmul($order['total_price'],$bili,2),100,2);
  105. if($money > 0){
  106. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'hexiaomoney',$money,201,'核销订单:'.$out_trade_no,'unishop_order',$order['id']);
  107. if($rs_wallet['status']===false)
  108. {
  109. Db::rollback();
  110. $this->error($rs_wallet['msg']);
  111. }
  112. }
  113. Db::commit();
  114. $this->success('核销成功');
  115. }
  116. //核销统计
  117. public function order_sum(){
  118. $startdate = input('startdate',date('Y-m-d'));
  119. $enddate = input('enddate' ,date('Y-m-d'));
  120. $starttime = strtotime($startdate);
  121. $endtime = strtotime($enddate) + 86399;
  122. $condition = [
  123. 'hexiao_uid' => $this->auth->id,
  124. 'status' => 1,
  125. 'have_paid' => ['gt',0],
  126. 'have_delivered' => ['gt',0],
  127. 'have_received' => ['gt',0],
  128. ];
  129. $count = Db::name('unishop_order')->where($condition)->where('have_received','BETWEEN',[$starttime,$endtime])->count();
  130. $total_price = Db::name('unishop_order')->where($condition)->where('have_received','BETWEEN',[$starttime,$endtime])->sum('total_price');
  131. $rs = [
  132. 'count' => $count,
  133. 'total_price' => $total_price,
  134. ];
  135. $this->success(1,$rs);
  136. }
  137. //核销记录
  138. public function order_list(){
  139. $startdate = input('startdate',date('Y-m-d'));
  140. $enddate = input('enddate' ,date('Y-m-d'));
  141. $starttime = strtotime($startdate);
  142. $endtime = strtotime($enddate) + 86399;
  143. $orderModel = new \addons\unishop\model\Order();
  144. $condition = [
  145. 'hexiao_uid' => $this->auth->id,
  146. 'status' => 1,
  147. 'have_paid' => ['gt',0],
  148. 'have_delivered' => ['gt',0],
  149. 'have_received' => ['gt',0],
  150. ];
  151. $result = $orderModel
  152. ->with([
  153. 'products' => function($query) {
  154. $query->field('id,title,image,number,price,spec,order_id,product_id');
  155. },
  156. ])
  157. ->where($condition)
  158. ->where('have_received','BETWEEN',[$starttime,$endtime])
  159. ->order(['have_received' => 'desc'])
  160. ->autopage()
  161. ->select();
  162. foreach ($result as &$item) {
  163. $item->append(['order_id','state']);
  164. $item = $item->toArray();
  165. unset($item['pay_out_trade_no']);
  166. foreach ($item['products'] as &$product) {
  167. $product['image'] = cdnurl($product['image']);
  168. }
  169. }
  170. $this->success(1,$result);
  171. }
  172. }