Admin.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\user;
  12. use app\common\repositories\store\order\StoreOrderRepository;
  13. use app\common\repositories\store\service\StoreServiceRepository;
  14. use app\controller\merchant\Common;
  15. use crmeb\basic\BaseController;
  16. use think\App;
  17. use think\exception\HttpResponseException;
  18. use think\exception\ValidateException;
  19. use think\facade\Db;
  20. use think\response\Json;
  21. class Admin extends BaseController
  22. {
  23. public function __construct(App $app)
  24. {
  25. parent::__construct($app);
  26. }
  27. public function orderStatistics($merId, StoreOrderRepository $repository)
  28. {
  29. $order = $repository->OrderTitleNumber($merId, null);
  30. $common = app()->make(Common::class);
  31. $data = [];
  32. $data['today'] = $common->mainGroup('today', $merId);
  33. $data['yesterday'] = $common->mainGroup('yesterday', $merId);
  34. $data['month'] = $common->mainGroup('month', $merId);
  35. return app('json')->success(compact('order', 'data'));
  36. }
  37. public function orderDetail($merId, StoreOrderRepository $repository)
  38. {
  39. [$page, $limit] = $this->getPage();
  40. list($start, $stop) = $this->request->params([
  41. ['start', strtotime(date('Y-m'))],
  42. ['stop', time()],
  43. ], true);
  44. if ($start == $stop) return app('json')->fail('参数有误');
  45. if ($start > $stop) {
  46. $middle = $stop;
  47. $stop = $start;
  48. $start = $middle;
  49. }
  50. $where = $this->request->has('start') ? ['dateRange' => compact('start', 'stop')] : [];
  51. $list = $repository->orderGroupNumPage($where, $page, $limit, $merId);
  52. return app('json')->success($list);
  53. }
  54. public function orderList($merId, StoreOrderRepository $repository)
  55. {
  56. [$page, $limit] = $this->getPage();
  57. $where = $this->request->params(['status']);
  58. $where['mer_id'] = $merId;
  59. $where['is_del'] = 0;
  60. return app('json')->success($repository->merchantGetList($where, $page, $limit));
  61. }
  62. public function order($merId, $id, StoreOrderRepository $repository)
  63. {
  64. $detail = $repository->getDetail($id);
  65. if (!$detail)
  66. return app('json')->fail('订单不存在');
  67. if ($detail['mer_id'] != $merId)
  68. return app('json')->fail('没有权限');
  69. return app('json')->success($detail->toArray());
  70. }
  71. protected function checkOrderAuth($merId, $id)
  72. {
  73. if (!app()->make(StoreOrderRepository::class)->existsWhere(['mer_id' => $merId, 'order_id' => $id]))
  74. throw new ValidateException('没有权限');
  75. }
  76. public function mark($merId, $id, StoreOrderRepository $repository)
  77. {
  78. $this->checkOrderAuth($merId, $id);
  79. $data = $this->request->params(['remark']);
  80. $repository->update($id, $data);
  81. return app('json')->success('备注成功');
  82. }
  83. public function price($merId, $id, StoreOrderRepository $repository)
  84. {
  85. $this->checkOrderAuth($merId, $id);
  86. $data = $this->request->params(['total_price', 'pay_postage']);
  87. if ($data['total_price'] < 0 || $data['pay_postage'] < 0)
  88. return app('json')->fail('金额不可未负数');
  89. if (!$repository->merStatusExists((int)$id, $merId))
  90. return app('json')->fail('订单信息或状态错误');
  91. $repository->eidt($id, $data);
  92. return app('json')->success('修改成功');
  93. }
  94. public function delivery($merId, $id, StoreOrderRepository $repository)
  95. {
  96. $this->checkOrderAuth($merId, $id);
  97. if (!$repository->merDeliveryExists((int)$id, $merId,1))
  98. return app('json')->fail('订单信息或状态错误');
  99. $type = $this->request->param('delivery_type');
  100. if($type == 4){
  101. if(!systemConfig('crmeb_serve_dump')) return app('json')->fail('电子面单功能未开启');
  102. $params = $this->request->params([
  103. 'delivery_name',
  104. 'from_name',
  105. 'from_tel',
  106. 'from_addr',
  107. 'temp_id',
  108. ]);
  109. $repository->dump($id,$merId,$params);
  110. } else {
  111. $data = $this->request->params([
  112. 'delivery_type',
  113. 'delivery_name',
  114. 'delivery_id',
  115. ]);
  116. if(preg_match('/([\x81-\xfe][\x40-\xfe])/',$data['delivery_id']))
  117. return app('json')->fail('请输入正确的单号/电话');
  118. $repository->delivery($id, $merId, $data);
  119. }
  120. return app('json')->success('发货成功');
  121. }
  122. public function payPrice($merId, StoreOrderRepository $repository)
  123. {
  124. list($start, $stop, $month) = $this->request->params([
  125. ['start', strtotime(date('Y-m'))],
  126. ['stop', time()],
  127. 'month'
  128. ], true);
  129. if ($month) {
  130. $start = date('Y/m/d', strtotime(getStartModelTime('month')));
  131. $stop = date('Y/m/d H:i:s', strtotime('+ 1day'));
  132. $front = date('Y/m/d', strtotime('first Day of this month', strtotime('-1 day', strtotime('first Day of this month'))));
  133. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  134. } else {
  135. if ($start == $stop) return app('json')->fail('参数有误');
  136. if ($start > $stop) {
  137. $middle = $stop;
  138. $stop = $start;
  139. $start = $middle;
  140. }
  141. $space = bcsub($stop, $start, 0);//间隔时间段
  142. $front = bcsub($start, $space, 0);//第一个时间段
  143. $front = date('Y/m/d H:i:s', $front);
  144. $start = date('Y/m/d H:i:s', $start);
  145. $stop = date('Y/m/d H:i:s', $stop);
  146. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  147. }
  148. $frontPrice = $repository->dateOrderPrice($front . '-' . $end, $merId);
  149. $afterPrice = $repository->dateOrderPrice($start . '-' . date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  150. $chartInfo = $repository->chartTimePrice($start, date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  151. $data['chart'] = $chartInfo;//营业额图表数据
  152. $data['time'] = $afterPrice;//时间区间营业额
  153. $increase = (float)bcsub((string)$afterPrice, (string)$frontPrice, 2); //同比上个时间区间增长营业额
  154. $growthRate = abs($increase);
  155. if ($growthRate == 0) $data['growth_rate'] = 0;
  156. else if ($frontPrice == 0) $data['growth_rate'] = bcmul($growthRate, 100, 0);
  157. else $data['growth_rate'] = (int)bcmul((string)bcdiv((string)$growthRate, (string)$frontPrice, 2), '100', 0);//时间区间增长率
  158. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  159. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  160. return app('json')->success($data);
  161. }
  162. /**
  163. * @param StoreOrderRepository $repository
  164. * @return Json
  165. * @author xaboy
  166. * @day 2020/8/27
  167. */
  168. public function payNumber($merId, StoreOrderRepository $repository)
  169. {
  170. list($start, $stop, $month) = $this->request->params([
  171. ['start', strtotime(date('Y-m'))],
  172. ['stop', time()],
  173. 'month'
  174. ], true);
  175. if ($month) {
  176. $start = date('Y/m/d', strtotime(getStartModelTime('month')));
  177. $stop = date('Y/m/d H:i:s', strtotime('+ 1day'));
  178. $front = date('Y/m/d', strtotime('first Day of this month', strtotime('-1 day', strtotime('first Day of this month'))));
  179. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  180. } else {
  181. if ($start == $stop) return app('json')->fail('参数有误');
  182. if ($start > $stop) {
  183. $middle = $stop;
  184. $stop = $start;
  185. $start = $middle;
  186. }
  187. $space = bcsub($stop, $start, 0);//间隔时间段
  188. $front = bcsub($start, $space, 0);//第一个时间段
  189. $front = date('Y/m/d H:i:s', $front);
  190. $start = date('Y/m/d H:i:s', $start);
  191. $stop = date('Y/m/d H:i:s', $stop);
  192. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  193. }
  194. $frontNumber = $repository->dateOrderNum($front . '-' . $end, $merId);
  195. $afterNumber = $repository->dateOrderNum($start . '-' . date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  196. $chartInfo = $repository->chartTimeNum($start . '-' . date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  197. $data['chart'] = $chartInfo;//订单数图表数据
  198. $data['time'] = $afterNumber;//时间区间订单数
  199. $increase = $afterNumber - $frontNumber; //同比上个时间区间增长订单数
  200. $growthRate = abs($increase);
  201. if ($growthRate == 0) $data['growth_rate'] = 0;
  202. else if ($frontNumber == 0) $data['growth_rate'] = bcmul($growthRate, 100, 0);
  203. else $data['growth_rate'] = (int)bcmul((string)bcdiv((string)$growthRate, (string)$frontNumber, 2), '100', 0);//时间区间增长率
  204. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  205. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  206. return app('json')->success($data);
  207. }
  208. public function getFormData($merId)
  209. {
  210. $config = [
  211. 'mer_from_com',
  212. 'mer_from_name',
  213. 'mer_from_tel',
  214. 'mer_from_addr',
  215. 'mer_config_siid',
  216. 'mer_config_temp_id'
  217. ];
  218. $data = merchantConfig($merId,$config);
  219. return app('json')->success($data);
  220. }
  221. }