123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 核销
- */
- class Hexiao extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- public function __construct(){
- parent::__construct();
- if($this->auth->is_hexiao != 1){$this->error('没有核销权限');}
- }
- //首页
- public function index(){
- $wallet = Db::name('user_wallet')->where(['user_id' => $this->auth->id])->find();
- $rs = [
- 'hexiaomoney' => $wallet['hexiaomoney'],
- 'takecash' => $wallet['hexiaomoney'],
- 'wait' => 0,
- ];
- $this->success(1,$rs);
- }
- //订单详情
- //code:order_hexiao_no|2024090466d7c9d5abb1c1
- //order_no:2024090466d7c9d5abb1c1
- public function order_info()
- {
- $code = input('code','');
- if(strpos($code,'order_hexiao_no|') !== 0){
- $this->error('识别不到的订单');
- }
- $order_no = substr($code,16);
- $order_info = Db::name('unishop_order')->where('out_trade_no',$order_no)->find();
- if(empty($order_info)){
- $this->error('不存在的订单');
- }
- if($order_info['status'] != 1){
- $this->error('非正常的订单');
- }
- if($order_info['have_paid'] == 0){
- $this->error('未支付的订单');
- }
- if($order_info['have_received'] != 0){
- $this->error('该订单已核销');
- }
- //下面是从unishop/order.php detail方法里拿过来的
- $order_id = $order_info['id'];
- $orderModel = new \addons\unishop\model\Order();
- $order = $orderModel
- ->with([
- 'products' => function ($query) {
- $query->field('id,order_id,image,number,price,spec,title,product_id');
- },
- ])
- ->where(['id' => $order_id])->find();
- if ($order) {
- $order = $order->append(['state', 'paidtime'])->toArray();
- foreach ($order['products'] as &$product) {
- $product['image'] = cdnurl($product['image']);
- }
- unset($order['pay_out_trade_no']);
- }
- $this->success(1,$order);
- }
- //完成核销动作
- public function hexiao(){
- $code = input('code','');
- if(strpos($code,'order_hexiao_no|') !== 0){
- $this->error('识别不到的订单');
- }
- $out_trade_no = substr($code,16);
- Db::startTrans();
- $order = Db::name('unishop_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
- if(empty($order)){
- Db::rollback();
- $this->error('不存在的订单');
- }
- if($order['status'] != 1){
- Db::rollback();
- $this->error('非正常的订单');
- }
- if($order['have_paid'] == 0){
- Db::rollback();
- $this->error('未支付的订单');
- }
- if($order['have_received'] != 0){
- Db::rollback();
- $this->error('该订单已核销');
- }
- $update = [
- 'have_received' => time(),
- 'hexiao_uid' => $this->auth->id,
- ];
- $order_rs = Db::name('unishop_order')->where('id',$order['id'])->update($update);
- if($order_rs === false){
- Db::rollback();
- $this->error('核销失败');
- }
- //给核销人结算
- $bili = config('site.unishop_order_hexiaomoney_bili');
- $money = bcdiv(bcmul($order['total_price'],$bili,2),100,2);
- if($money > 0){
- $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'hexiaomoney',$money,201,'核销订单:'.$out_trade_no,'unishop_order',$order['id']);
- if($rs_wallet['status']===false)
- {
- Db::rollback();
- $this->error($rs_wallet['msg']);
- }
- }
- Db::commit();
- $this->success('核销成功');
- }
- //核销统计
- public function order_sum(){
- $startdate = input('startdate',date('Y-m-d'));
- $enddate = input('enddate' ,date('Y-m-d'));
- $starttime = strtotime($startdate);
- $endtime = strtotime($enddate) + 86399;
- $condition = [
- 'hexiao_uid' => $this->auth->id,
- 'status' => 1,
- 'have_paid' => ['gt',0],
- 'have_delivered' => ['gt',0],
- 'have_received' => ['gt',0],
- ];
- $count = Db::name('unishop_order')->where($condition)->where('have_received','BETWEEN',[$starttime,$endtime])->count();
- $total_price = Db::name('unishop_order')->where($condition)->where('have_received','BETWEEN',[$starttime,$endtime])->sum('total_price');
- $rs = [
- 'count' => $count,
- 'total_price' => $total_price,
- ];
- $this->success(1,$rs);
- }
- //核销记录
- public function order_list(){
- $startdate = input('startdate',date('Y-m-d'));
- $enddate = input('enddate' ,date('Y-m-d'));
- $starttime = strtotime($startdate);
- $endtime = strtotime($enddate) + 86399;
- $orderModel = new \addons\unishop\model\Order();
- $condition = [
- 'hexiao_uid' => $this->auth->id,
- 'status' => 1,
- 'have_paid' => ['gt',0],
- 'have_delivered' => ['gt',0],
- 'have_received' => ['gt',0],
- ];
- $result = $orderModel
- ->with([
- 'products' => function($query) {
- $query->field('id,title,image,number,price,spec,order_id,product_id');
- },
- ])
- ->where($condition)
- ->where('have_received','BETWEEN',[$starttime,$endtime])
- ->order(['have_received' => 'desc'])
- ->autopage()
- ->select();
- foreach ($result as &$item) {
- $item->append(['order_id','state']);
- $item = $item->toArray();
- unset($item['pay_out_trade_no']);
- foreach ($item['products'] as &$product) {
- $product['image'] = cdnurl($product['image']);
- }
- }
- $this->success(1,$result);
- }
- }
|