Hexiao.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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->group_id != 3){$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. /*'extend' => function ($query) {
  45. $query->field('id,order_id,address_id,address_json,express_number,express_company');
  46. },
  47. 'evaluate' => function ($query) {
  48. $query->field('id,order_id,product_id');
  49. }*/
  50. ])
  51. ->where(['id' => $order_id])->find();
  52. if ($order) {
  53. $order = $order->append(['state', 'paidtime'])->toArray();
  54. // 快递单号
  55. /*$order['express_number'] = $order['extend']['express_number'];
  56. $order['express_company'] = '快递单号';
  57. $order['express'] = '';
  58. if (class_exists(\addons\expressquery\library\Expressquery::class)) {
  59. $expressInfo = Db::name('expressquery')->where(['express' => $order['extend']['express_company']])->find();
  60. $order['express_company'] = $expressInfo['name'] ?? '快递单号';
  61. $order['express'] = $expressInfo['express'] ?? '';
  62. }*/
  63. // 送货地址
  64. /*$address = json_decode($order['extend']['address_json'], true);
  65. $area = (new \addons\unishop\model\Area())
  66. ->whereIn('id', [$address['province_id'], $address['city_id'], $address['area_id']])
  67. ->column('name', 'id');
  68. $delivery['username'] = $address['name'];
  69. $delivery['mobile'] = $address['mobile'];
  70. $delivery['address'] = $area[$address['province_id']] . ' ' . $area[$address['city_id']] . ' ' . $area[$address['area_id']] . ' ' . $address['address'];
  71. $order['delivery'] = $delivery;*/
  72. // 是否已评论
  73. // $evaluate = array_column($order['evaluate'], 'product_id');
  74. foreach ($order['products'] as &$product) {
  75. $product['image'] = cdnurl($product['image']);
  76. /*if (in_array($product['id'], $evaluate)) {
  77. $product['evaluate'] = true;
  78. } else {
  79. $product['evaluate'] = false;
  80. }*/
  81. //限定核销产品
  82. if(!in_array($product['product_id'],explode(',',$this->auth->product_ids))){
  83. $this->error('您无法核销当前订单,请移至正确核销点,或联系客服','',2);
  84. }
  85. }
  86. // unset($order['evaluate']);
  87. unset($order['extend']);
  88. unset($order['pay_out_trade_no']);
  89. //预约日期
  90. $week_data = [
  91. 0 => '周日',
  92. 1 => '周一',
  93. 2 => '周二',
  94. 3 => '周三',
  95. 4 => '周四',
  96. 5 => '周五',
  97. 6 => '周六',
  98. 7 => '周日',
  99. ];
  100. $order['bookdate'] = date('Y-m-d H:i',$order['booktime']) .' '. $week_data[date('w',$order['booktime'])];
  101. //追加退改规则
  102. $order['order_refund_rule'] = config('site.order_refund_rule');
  103. //电子凭证,核销码
  104. /*$qrcode_string = 'order_hexiao_no|' . $order['out_trade_no'];
  105. $order['order_hexiao_qrcode'] = httpurllocal($this->inviteimage($qrcode_string));*/
  106. }
  107. //追加退改规则
  108. $this->success(1,$order);
  109. }
  110. //完成核销动作
  111. public function hexiao(){
  112. if($this->auth->group_id != 3){$this->error('你是不是来错地方了?');}
  113. // $order_id = input('order_id', 0);
  114. // $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  115. $code = input('code','');
  116. if(strpos($code,'order_hexiao_no|') !== 0){
  117. $this->error('识别不到的订单');
  118. }
  119. $out_trade_no = substr($code,16);
  120. Db::startTrans();
  121. $order = Db::name('unishop_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  122. if (!$order) {
  123. Db::rollback();
  124. $this->error('不存在的订单');
  125. }
  126. if($order['status'] != 1){
  127. Db::rollback();
  128. $this->error('非正常的订单');
  129. }
  130. if($order['have_paid'] == 0){
  131. Db::rollback();
  132. $this->error('未支付的订单');
  133. }
  134. if($order['have_received'] != 0){
  135. Db::rollback();
  136. $this->error('该订单已核销');
  137. }
  138. //限定核销产品
  139. $products = Db::name('unishop_order_product')->where('order_id',$order['id'])->select();
  140. foreach($products as $key => $product){
  141. if(!in_array($product['product_id'],explode(',',$this->auth->product_ids))){
  142. $this->success('您无法核销当前订单,请移至正确核销点,或联系客服','',2);
  143. }
  144. }
  145. $update = [
  146. 'have_received' => time(),
  147. 'hexiao_uid' => $this->auth->id,
  148. ];
  149. $order_rs = Db::name('unishop_order')->where('id',$order['id'])->update($update);
  150. if($order_rs === false){
  151. Db::rollback();
  152. $this->error('核销失败');
  153. }
  154. //给推广人结算
  155. if($order['intro_uid']){
  156. $bili = config('site.unishop_order_intromoney_bili');
  157. $money = bcdiv(bcmul($order['total_price'],$bili,2),100,2);
  158. if($money > 0){
  159. $rs_wallet = model('Wallet')->lockChangeAccountRemain($order['intro_uid'],'money',$money,10,'推广订单:'.$out_trade_no,'unishop_order',$order['id']);
  160. if($rs_wallet['status']===false)
  161. {
  162. Db::rollback();
  163. $this->error($rs_wallet['msg']);
  164. }
  165. }
  166. }
  167. Db::commit();
  168. $this->success('核销成功');
  169. }
  170. //核销记录
  171. public function order_list(){
  172. if($this->auth->group_id != 3){$this->error('你是不是来错地方了?');}
  173. $orderModel = new \addons\unishop\model\Order();
  174. $condition = [
  175. 'hexiao_uid' => $this->auth->id,
  176. 'status' => 1,
  177. 'have_paid' => ['gt',0],
  178. 'have_delivered' => ['gt',0],
  179. 'have_received' => ['gt',0],
  180. ];
  181. $result = $orderModel
  182. ->with([
  183. 'products' => function($query) {
  184. $query->field('id,title,image,number,price,spec,order_id,product_id');
  185. },
  186. ])
  187. ->where($condition)
  188. ->order(['have_received' => 'desc'])
  189. ->autopage()
  190. ->select();
  191. //预约日期
  192. $week_data = [
  193. 0 => '周日',
  194. 1 => '周一',
  195. 2 => '周二',
  196. 3 => '周三',
  197. 4 => '周四',
  198. 5 => '周五',
  199. 6 => '周六',
  200. 7 => '周日',
  201. ];
  202. foreach ($result as &$item) {
  203. $item->append(['order_id','state']);
  204. $item = $item->toArray();
  205. unset($item['pay_out_trade_no']);
  206. /*$evaluate = array_column($item['evaluate'], 'product_id');
  207. $refundProducts = array_column($item['refund_products'], 'order_product_id');
  208. unset($item['evaluate']);
  209. unset($item['refund_products']);*/
  210. foreach ($item['products'] as &$product) {
  211. $product['image'] = cdnurl($product['image']);
  212. // 是否已评论
  213. /*if (in_array($product['id'], $evaluate)) {
  214. $product['evaluate'] = true;
  215. } else {
  216. $product['evaluate'] = false;
  217. }*/
  218. // 是否退货
  219. /*if ($item['refund_status'] == self::REFUND_STATUS_AGREE && in_array($product['order_product_id'], $refundProducts)) {
  220. $product['refund'] = true;
  221. } else {
  222. $product['refund'] = false;
  223. }*/
  224. }
  225. $item['bookdate'] = date('Y-m-d H:i',$item['booktime']) .' '. $week_data[date('w',$item['booktime'])];
  226. }
  227. $this->success(1,$result);
  228. }
  229. }