Shoporder.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 订单管理
  7. */
  8. namespace app\ais\controller\api\v1;
  9. use app\ais\controller\api\Base;
  10. use app\ais\model\AisShopOrder;
  11. use app\ais\model\AisBank;
  12. use app\ais\model\AisShop;
  13. use app\ais\model\AisConfig;
  14. use app\ais\model\AisCouponUser;
  15. use app\ais\model\AisVipUser;
  16. use app\common\model\SystemUser;
  17. use app\common\facade\WechatPay;
  18. class Shoporder extends Base{
  19. /**
  20. * 初始化当前应用是否登录
  21. * @return void
  22. */
  23. public function initialize() {
  24. parent::initialize();
  25. $this->isUserAuth();
  26. }
  27. /**
  28. * @param int $store_id
  29. * 店铺订单
  30. */
  31. public function index(){
  32. $param['active'] = $this->request->param('active/d',0);
  33. $param['page'] = $this->request->param('page/d',1);
  34. $this->apiSign($param);
  35. $condition['uid'] = $this->user->id;
  36. $condition['is_del'] = 0;
  37. switch ($param['active']) {
  38. case 1:
  39. $condition['paid_at'] = 0;
  40. break;
  41. case 2:
  42. $condition['paid_at'] = 1;
  43. $condition['status'] = 0;
  44. break;
  45. case 3:
  46. $condition['paid_at'] = 1;
  47. $condition['status'] = 1;
  48. break;
  49. }
  50. $order = AisShopOrder::with(['store' => function($query) {
  51. $query->field('name,address,id');
  52. }])->field('store_id,id,order_no,paid_at,paid_time,phone,points,message,shop_id,status,shop_cache,amount,create_time,is_del')->where($condition)->page($param['page'],10)->select();
  53. if($order->isEmpty()){
  54. return enjson(204);
  55. }
  56. $data = [];
  57. $ids = [];
  58. foreach ($order as $key => $value) {
  59. $data[$key] = $value;
  60. $data[$key]['status_text'] = $value['statu'];
  61. $data[$key]['end_paytime'] = 0;
  62. if($value['paid_at'] == 0){
  63. $data[$key]['end_paytime'] = (($value['create_time'] + 60 * 10) - time()) * 1000;
  64. }
  65. if($value['paid_at'] == 0 && $data[$key]['end_paytime'] < 0){
  66. $ids[] = $value['id'];
  67. unset($data[$key]);
  68. }
  69. }
  70. if(!empty($ids)){ //过期了更改了状态
  71. AisShopOrder::where(['id' => $ids])->update(['is_del' => 1]);
  72. }
  73. if(empty($data)){
  74. return enjson(204);
  75. }
  76. return enjson(200,array_values($data));
  77. }
  78. /**
  79. * @param int $store_id
  80. */
  81. public function views(){
  82. $param['id'] = $this->request->param('id/d',0);
  83. $this->apiSign($param);
  84. $condition['uid'] = $this->user->id;
  85. $condition['id'] = $param['id'];
  86. $order = AisShopOrder::with(['store' => function($query) {
  87. $query->field('name,address,id,longitude,latitude');
  88. }])->field('store_id,id,order_no,paid_at,paid_time,phone,points,message,shop_id,status,shop_cache,amount,create_time,is_del,thrifty')->where($condition)->find();
  89. if(empty($order)){
  90. return enjson(204);
  91. }
  92. $order->end_paytime = 0;
  93. if(!$order->paid_at){
  94. $order->end_paytime = (($order->create_time + 60 * 10) - time()) * 1000;
  95. }
  96. if($order->paid_at == 0 && $order->end_paytime < 0){
  97. $order->is_del = 1;
  98. $order->save();
  99. }
  100. $shop_cache = $order->shop_cache;
  101. $shop_cache['notice'] = str_replace('<img', '<img class="img" style="max-width:100%;height:auto"',dehtml($shop_cache['notice']));
  102. $order->shop_cache = $shop_cache;
  103. $order->paid_time = date('Y-m-d H:i:s',$order->paid_time);
  104. $order->create_time = date('Y-m-d H:i:s',$order->create_time);
  105. return enjson(200,$order);
  106. }
  107. /**
  108. * @param int $order_id
  109. * @return \think\response\Json
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. * @throws \think\exception\DbException
  113. * 获取核心订单
  114. */
  115. public function orderVer(){
  116. $param['id'] = $this->request->param('id/d',0);
  117. $this->apiSign($param);
  118. $condition['id'] = $param['id'];
  119. $condition['paid_at'] = 1;
  120. $condition['status'] = 0;
  121. $condition['is_del'] = 0;
  122. $info = AisShopOrder::with(['store' => function($query) {
  123. $query->field('name,address,id,longitude,latitude');
  124. }])->where($condition)->field('store_id,uid,id,order_no,paid_at,paid_time,phone,points,message,shop_id,status,shop_cache,amount,create_time,is_del')->find();
  125. if(empty($info)){
  126. return enjson(303,'订单不存在',['url' => '/pages/index']);
  127. }
  128. $uid = array_flip(array_column($info->storeWorker->toArray(),'uid'));
  129. if(empty($uid)){
  130. return enjson(303,'商家没有设置核销员',['url' => '/pages/index']);
  131. }
  132. if (!isset($uid[$this->user->id])){
  133. return enjson(303,'你无权限核销订单',['url' => '/pages/index']);
  134. }
  135. if(request()->isPost()){
  136. $info->status = 1;
  137. $info->save();
  138. return enjson(303,'订单成功核销',['url' => '/pages/user/index']);
  139. }else{
  140. $shop_cache = $info->shop_cache;
  141. $shop_cache['notice'] = str_replace('<img', '<img class="img" style="max-width:100%;height:auto"',dehtml($shop_cache['notice']));
  142. $info->shop_cache = $shop_cache;
  143. $info->paid_time = date('Y-m-d H:i:s',$info->paid_time);
  144. $info->create_time = date('Y-m-d H:i:s',$info->create_time);
  145. return enjson(200,'成功',$info);
  146. }
  147. }
  148. }