Order.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace app\data\controller\shop;
  3. use app\data\model\BaseUserPayment;
  4. use app\data\model\DataUser;
  5. use app\data\model\ShopOrder;
  6. use app\data\model\ShopOrderSend;
  7. use app\data\service\OrderService;
  8. use app\data\service\PaymentService;
  9. use app\data\service\UserAdminService;
  10. use think\admin\Controller;
  11. use think\admin\extend\CodeExtend;
  12. use think\admin\service\AdminService;
  13. use think\exception\HttpResponseException;
  14. /**
  15. * 订单数据管理
  16. * Class Order
  17. * @package app\data\controller\shop
  18. */
  19. class Order extends Controller
  20. {
  21. /**
  22. * 支付方式
  23. * @var array
  24. */
  25. protected $payments = [];
  26. /**
  27. * 控制器初始化
  28. */
  29. protected function initialize()
  30. {
  31. parent::initialize();
  32. //$this->payments = PaymentService::getTypeAll();
  33. $this->payments = [
  34. [
  35. 'name'=>'支付宝支付'
  36. ],
  37. [
  38. 'name'=>'微信支付'
  39. ]
  40. ];
  41. // // 读取支付通道配置
  42. // $query = BaseUserPayment::mk()->where(['status' => 1, 'deleted' => 0]);
  43. // //$query->whereIn('code', str2arr($payments))->whereIn('type', PaymentService::getTypeApi($this->type));
  44. // $result = $query->order('sort desc,id desc')->column('type,code,name,cover,content,remark', 'code');
  45. // foreach ($result as &$vo) $vo['content'] = ['voucher_qrcode' => json_decode($vo['content'])->voucher_qrcode ?? ''];
  46. // $this->payments2 = array_values($result);
  47. // dump($this->payments2);
  48. }
  49. /**
  50. * 订单数据管理
  51. * @auth true
  52. * @menu true
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function index()
  58. {
  59. $this->title = '订单数据管理';
  60. if(AdminService::getUserId()==10000){
  61. $admin_id = [];
  62. }
  63. else{
  64. $admin_id['admin_id']=AdminService::getUserId();
  65. }
  66. // 状态数据统计
  67. $this->total = ['t0' => 0, 't2' => 0, 't3' => 0, 't4' => 0, 't5' => 0, 't6' => 0, 't7' => 0, 't8' => 0, 'ta' => 0];
  68. foreach (ShopOrder::mk()->where($admin_id)->field('status,count(1) total')
  69. ->group('status')->cursor() as $vo) {
  70. [$this->total["t{$vo['status']}"] = $vo['total'], $this->total["ta"] += $vo['total']];
  71. }
  72. // 订单列表查询
  73. $query = ShopOrder::mQuery()->where($admin_id);
  74. $query->like('order_no,truck_name,truck_phone,truck_province|truck_area|truck_address#address,truck_send_no,truck_send_name');
  75. $query->equal('status,payment_type,payment_status')->dateBetween('create_at,payment_datetime,cancel_datetime,truck_datetime,truck_send_datetime');
  76. // 发货信息搜索
  77. $db = ShopOrderSend::mQuery()->like('address_name#truck_address_name,address_phone#truck_address_phone,address_province|address_city|address_area|address_content#truck_address_content')->db();
  78. if ($db->getOptions('where')) $query->whereRaw("order_no in {$db->field('order_no')->buildSql()}");
  79. // 用户搜索查询
  80. $db = DataUser::mQuery()->like('phone|nickname#user_keys')->db();
  81. if ($db->getOptions('where')) $query->whereRaw("uuid in {$db->field('id')->buildSql()}");
  82. // 代理搜索查询
  83. $db = DataUser::mQuery()->like('phone|nickname#from_keys')->db();
  84. if ($db->getOptions('where')) $query->whereRaw("puid1 in {$db->field('id')->buildSql()}");
  85. // 列表选项卡
  86. if (is_numeric($this->type = trim(input('type', 'ta'), 't'))) {
  87. $query->where(['status' => $this->type]);
  88. }
  89. // 分页排序处理
  90. $query->where(['deleted_status' => 0])->order('id desc')->layTable();
  91. }
  92. /**
  93. * 订单列表处理
  94. * @param array $data
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. */
  99. protected function _index_page_filter(array &$data)
  100. {
  101. UserAdminService::buildByUid($data);
  102. UserAdminService::buildByUid($data, 'puid1', 'from');
  103. OrderService::buildData($data);
  104. foreach ($data as &$vo) {
  105. if (!is_null($vo['payment_type']) and '' != $vo['payment_type']) {
  106. $vo['payment_name'] = PaymentService::name($vo['payment_type']);
  107. }
  108. }
  109. }
  110. /**
  111. * 单据凭证支付审核
  112. * @auth true
  113. * @throws \think\db\exception\DataNotFoundException
  114. * @throws \think\db\exception\DbException
  115. * @throws \think\db\exception\ModelNotFoundException
  116. */
  117. public function audit()
  118. {
  119. if ($this->request->isGet()) {
  120. ShopOrder::mForm('', 'order_no');
  121. } else {
  122. $data = $this->_vali([
  123. 'order_no.require' => '订单单号不能为空!',
  124. 'status.in:0,1' => '审核状态数值异常!',
  125. 'status.require' => '审核状态不能为空!',
  126. 'remark.default' => '',
  127. ]);
  128. if (empty($data['status'])) {
  129. $data['status'] = 0;
  130. $data['cancel_status'] = 1;
  131. $data['cancel_remark'] = $data['remark'] ?: '后台审核驳回并取消订单';
  132. $data['cancel_datetime'] = date('Y-m-d H:i:s');
  133. } else {
  134. $data['status'] = 4;
  135. $data['payment_code'] = CodeExtend::uniqidDate(20, 'T');
  136. $data['payment_status'] = 1;
  137. $data['payment_remark'] = $data['remark'] ?: '后台审核支付凭证通过';
  138. $data['payment_datetime'] = date('Y-m-d H:i:s');
  139. }
  140. $order = ShopOrder::mk()->where(['order_no' => $data['order_no']])->find();
  141. if (empty($order) || $order['status'] !== 3) $this->error('不允许操作审核!');
  142. // 无需发货时的处理
  143. if ($data['status'] === 4 && empty($order['truck_type'])) $data['status'] = 6;
  144. // 更新订单支付状态
  145. $map = ['status' => 3, 'order_no' => $data['order_no']];
  146. if (ShopOrder::mk()->strict(false)->where($map)->update($data) !== false) {
  147. if (in_array($data['status'], [4, 5, 6])) {
  148. $this->app->event->trigger('ShopOrderPayment', $data['order_no']);
  149. $this->success('订单审核通过成功!');
  150. } else {
  151. $this->app->event->trigger('ShopOrderCancel');
  152. OrderService::stock($data['order_no']);
  153. $this->success('审核驳回并取消成功!');
  154. }
  155. } else {
  156. $this->error('订单审核失败!');
  157. }
  158. }
  159. }
  160. /**
  161. * 清理订单数据
  162. * @auth true
  163. */
  164. public function clean()
  165. {
  166. $this->_queue('定时清理无效订单数据', "xdata:OrderClean", 0, [], 0, 60);
  167. }
  168. /**
  169. * 取消未支付的订单
  170. * @auth true
  171. * @throws \think\db\exception\DataNotFoundException
  172. * @throws \think\db\exception\DbException
  173. * @throws \think\db\exception\ModelNotFoundException
  174. */
  175. public function cancel()
  176. {
  177. $map = $this->_vali(['order_no.require' => '订单号不能为空!',]);
  178. $order = ShopOrder::mk()->where($map)->find();
  179. if (empty($order)) $this->error('订单查询异常!');
  180. if (!in_array($order['status'], [1, 2, 3])) $this->error('订单不能取消!');
  181. try {
  182. $result = $order->save([
  183. 'status' => 0,
  184. 'cancel_status' => 1,
  185. 'cancel_remark' => '后台取消未支付的订单',
  186. 'cancel_datetime' => date('Y-m-d H:i:s'),
  187. ]);
  188. if ($result !== false) {
  189. OrderService::stock($order['order_no']);
  190. $this->app->event->trigger('ShopOrderCancel', $order['order_no']);
  191. $this->success('取消未支付的订单成功!');
  192. } else {
  193. $this->error('取消支付的订单失败!');
  194. }
  195. } catch (HttpResponseException $exception) {
  196. throw $exception;
  197. } catch (\Exception $exception) {
  198. $this->error($exception->getMessage());
  199. }
  200. }
  201. }