Order.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. namespace app\data\controller\api\business;
  3. use app\data\model\DataUser;
  4. use app\data\model\ShopGoodsItem;
  5. use app\data\model\ShopOrder;
  6. use app\data\model\ShopOrderItem;
  7. use app\data\model\ShopOrderSend;
  8. use app\data\model\SystemUser;
  9. use app\data\model\SystemUserAddress;
  10. use think\admin\Controller;
  11. use hg\apidoc\annotation\Title;
  12. use hg\apidoc\annotation\Method;
  13. use hg\apidoc\annotation\Param;
  14. use hg\apidoc\annotation\Returned;
  15. use Yansongda\Pay\Exceptions\GatewayException;
  16. use Yansongda\Pay\Pay;
  17. use app\data\model\ShopGoods;
  18. use app\data\model\ShopOrderPay;
  19. use think\admin\model\SystemConfig;
  20. /**
  21. * 商家订单数据接口
  22. */
  23. class Order extends Controller
  24. {
  25. /**
  26. * @Title ("主页订单上标数量")
  27. * @Method ("get")
  28. * @return void
  29. * @throws \think\db\exception\DbException
  30. */
  31. public function order_statistics(){
  32. $admin_id=$this->uuid();
  33. $count = ['待付款','待发货','待收货','退款/售后'];
  34. $where=[];
  35. foreach ($count as &$v){
  36. switch ($v){
  37. case '待付款':
  38. $array = '2';
  39. break;
  40. case '待发货':
  41. $array = '4';
  42. break;
  43. case '待收货':
  44. $array = '5';
  45. break;
  46. case '退款/售后':
  47. $array = '7,8';
  48. $where['refund_del'] = 1;
  49. break;
  50. }
  51. $v=ShopOrder::mk()->where('admin_id',$admin_id->id)->whereIn('status',$array)->where($where)->count();
  52. }
  53. $this->success('商城订单上标数量',$count);
  54. }
  55. /**
  56. * @Title ("商城订单上标数量")
  57. * @Method ("get")
  58. * @return void
  59. * @throws \think\db\exception\DbException
  60. */
  61. public function order_count(){
  62. $admin_id=$this->uuid();
  63. $count = ['全部','待付款','待发货','待收货','已完成'];
  64. foreach ($count as &$v){
  65. switch ($v){
  66. case '全部':
  67. $array = '0,1,2,3,4,5,6';
  68. break;
  69. case '待付款':
  70. $array = '2';
  71. break;
  72. case '待发货':
  73. $array = '4';
  74. break;
  75. case '待收货':
  76. $array = '5';
  77. break;
  78. case '已完成':
  79. $array = '6';
  80. break;
  81. }
  82. $v=ShopOrder::mk()->where('admin_id',$admin_id->id)->whereIn('status',$array)->count();
  83. }
  84. $this->success('商城订单上标数量',$count);
  85. }
  86. /**
  87. * @Title("我的订单")
  88. * @Method("post")
  89. * @Param ("page",desc="页码")
  90. * @Param("order_name",desc="搜索名称")
  91. * @Param ("status",desc="1全部 2 待支付 3代发货 4 待收货 5已完成")
  92. */
  93. public function order_list(){
  94. $admin_id=$this->uuid();
  95. $status = input('status',1);
  96. if(!empty($status)){
  97. switch ($status){
  98. case 1:
  99. $array = [0,1,2,3,4,5];
  100. break;
  101. case 2:
  102. $array = [2];
  103. break;
  104. case 3:
  105. $array = [4];
  106. break;
  107. case 4:
  108. $array = [5];
  109. break;
  110. case 5:
  111. $array = [6];
  112. break;
  113. }
  114. }
  115. $query = ShopOrder::mQuery()->like('order_name');
  116. $list = $query ->where('admin_id',$admin_id['id'])
  117. ->whereIn('status',$array)->order('id desc')->page(true, false, false, 10);
  118. foreach ($list['list'] as $k=>$v){
  119. $list['list'][$k]['goods_item']=ShopOrderItem::mk()->where('order_no',$list['list'][$k]['order_no'])->select();
  120. $list['list'][$k]['user_address']=ShopOrderSend::mk()->where('order_no',$list['list'][$k]['order_no'])->find();
  121. $list['list'][$k]['huanxinID'] = DataUser::mk()->where('id',$v['uuid'])->value('huanxinID');
  122. }
  123. $this->success('店铺订单列表',$list);
  124. }
  125. /**
  126. * @Title ("订单详情")
  127. * @Method ("post")
  128. * @Param ("order_id",desc="订单id")
  129. * @return void
  130. * @throws \think\db\exception\DataNotFoundException
  131. * @throws \think\db\exception\DbException
  132. * @throws \think\db\exception\ModelNotFoundException
  133. */
  134. public function order_info(){
  135. $admin_id=$this->uuid();
  136. $order_id = strval(input('order_id'));
  137. $str_admin_id = strval($admin_id['id']);
  138. $arr = [
  139. 'admin_id' => $str_admin_id,
  140. 'id' => $order_id
  141. ];
  142. $list = ShopOrder::mk()->where($arr)->find();
  143. $list['time']=0;
  144. if($list['status']==2){
  145. $list['time'] = strtotime($list['create_at'])+30*60-time();
  146. /* if($list['list']['time']<=3){
  147. ShopOrder::mk()->where('id',$list['list']['id'])->save(['status'=>0]);
  148. }*/
  149. }
  150. $list['user']=DataUser::mk()->where('id',$list['uuid'])->field('nickname,phone')->find();
  151. $list['goods_item']=ShopOrderItem::mk()->where('order_no',$list['order_no'])->select();
  152. $list['user_address']=ShopOrderSend::mk()->where('order_no',$list['order_no'])->find();
  153. $list['user_info']=DataUser::mk()->where('id',$list['uuid'])->field('headimg,nickname')->find();
  154. $list['huanxinID'] = DataUser::mk()->where('id',$list['uuid'])->value('huanxinID');
  155. $this->success('店铺订单列表',$list);
  156. }
  157. /**
  158. * @Title ("退款订单列表")
  159. * @Method ("get")
  160. * @Param ("page",desc="页码")
  161. * @return void
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\DbException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. */
  166. public function refund_list(){
  167. $admin_id=$this->uuid();
  168. $status =[7,8];
  169. $query = ShopOrder::mQuery()->like('order_name');
  170. $list = $query ->where('admin_id',$admin_id['id'])
  171. ->whereIn('status',$status)->where('refund_del',1)->order('id desc')->page(true, false, false, 10);
  172. foreach ($list['list'] as $k=>$v){
  173. $list['list'][$k]['goods_item']=ShopOrderItem::mk()->where('order_no',$list['list'][$k]['order_no'])->select();
  174. $list['list'][$k]['user_address']=ShopOrderSend::mk()->where('order_no',$list['list'][$k]['order_no'])->find();
  175. }
  176. $this->success('退款订单',$list);
  177. }
  178. /**
  179. * @Title ("退款操作接口")
  180. * @Method ("post")
  181. * @Param ("order_id",desc="订单id")
  182. * @Param ("type",desc="退款操作 type=1同意退款 type=2 不同意驳回")
  183. * @Param ("reason",desc="拒绝退款原因")
  184. * @return void
  185. * @throws \think\db\exception\DataNotFoundException
  186. * @throws \think\db\exception\DbException
  187. * @throws \think\db\exception\ModelNotFoundException
  188. */
  189. public function examine_refund(){
  190. $admnd_id=$this->uuid();
  191. $data = $this->_vali([
  192. 'order_id.require'=>'退款订单不能为空',
  193. 'type.require'=>'操作类型能为空',
  194. 'reason.default'=>'',
  195. ]);
  196. $info = ShopOrder::mk()->where(['id'=>$data['order_id'],'admin_id'=>$admnd_id['id'],'status'=>7])->find();
  197. $pay_money = ShopOrderPay::mk()->where(['status' => 2, 'pay_no'=>$info['payment_trade']])->value('money');
  198. if(empty($info)){
  199. $this->error('订单信息有误');
  200. }
  201. else{
  202. // 退款操作
  203. if($data['type']==1){
  204. //同意退款
  205. if($info['payment_type']=='支付宝支付') {
  206. $result = json_decode($this->refund_order($info['payment_trade'], $info['refund_money']), true);
  207. }
  208. if($info['payment_type']=='微信支付') {
  209. $result = json_decode($this->wxrefund_order($info['payment_trade'], $info['refund_money'],$pay_money), true);
  210. }
  211. if($result) {
  212. ShopOrder::mk()->where(['id' => $data['order_id'], 'admin_id' => $admnd_id['id']])->save(['status' => 8, 'refund_status' => 2, 'dk_time' => date('Y-m-d H:i:s')]);
  213. $this->success('已同意退款,支付金额原路退回');
  214. }
  215. else{
  216. $this->error('金额未退回查看支付账户');
  217. }
  218. }
  219. //统一退货
  220. else {
  221. ShopOrder::mk()->where(['id'=>$data['order_id'],'admin_id'=>$admnd_id['id']])->save(['status' => 4,'refund_status'=>3,'refuse_reason'=>$data['reason'],'dk_time'=>date('Y-m-d H:i:s')]);
  222. //不同意退款
  223. $this->success('不同意退款');
  224. }
  225. }
  226. }
  227. public static function refund_order($pay_no,$money){
  228. $config = [
  229. 'app_id' => '2021003161685907',
  230. 'notify_url' => '',
  231. 'return_url' => '',
  232. 'ali_public_key' =>'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0+yqR2nQ7uZBHoHeeLspwvZcB/jgMyjAxkJk7Dm9IR5VhTTSXNQYmOOjs+g628G52xxHQ/Z1pPK3dFshqIBTQC0VrrTyWFhypb8veKeo5RFzgWKvl1awvOdf5r6qXM6b3OjaBYqiqvpLdpZdVkEY9rLOwh6LM8XKYE3CfM466KypxYBrUeXRmDhweWVScSuOJGuhmd1O2RI2TIHjosxG10v4/s3nfNZ+aGQ+UnASRZbJ0GyyFlqyJ+mZGyAJBahWI4YYG6ZJMp2YNNUAWeb++gVG1Zw+HYMG57C0W6PCZ0LvS454ZRyx72WP+nKi7LMj8fkintEPbOkRd3M/OF42gQIDAQAB',
  233. 'private_key' =>'MIIEogIBAAKCAQEA0+yqR2nQ7uZBHoHeeLspwvZcB/jgMyjAxkJk7Dm9IR5VhTTSXNQYmOOjs+g628G52xxHQ/Z1pPK3dFshqIBTQC0VrrTyWFhypb8veKeo5RFzgWKvl1awvOdf5r6qXM6b3OjaBYqiqvpLdpZdVkEY9rLOwh6LM8XKYE3CfM466KypxYBrUeXRmDhweWVScSuOJGuhmd1O2RI2TIHjosxG10v4/s3nfNZ+aGQ+UnASRZbJ0GyyFlqyJ+mZGyAJBahWI4YYG6ZJMp2YNNUAWeb++gVG1Zw+HYMG57C0W6PCZ0LvS454ZRyx72WP+nKi7LMj8fkintEPbOkRd3M/OF42gQIDAQABAoIBACIGS0hv6sWQXkAhpbBIjGnq19fH02cjUeAC5OUwV/crQZvDYUraGqDhW3UVAe4qakaNgbudpEG5Xk7mGr9M6xDQrDyCfK5GdRG7YknC0V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Cp6/k7Lt73lkF9sJ4JRP4Fw3R3ddskYu33CzI=',
  234. 'log' => [ // optional
  235. 'file' => './logs/alipay.log',
  236. 'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
  237. 'type' => 'single', // optional, 可选 daily.
  238. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
  239. ],
  240. 'http' => [ // optional
  241. 'timeout' => 5.0,
  242. 'connect_timeout' => 5.0,
  243. // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
  244. ],
  245. // 'mode' => 'dev', // optional,设置此参数,将进入沙箱模式
  246. ];
  247. try{
  248. $order = [
  249. 'out_trade_no' => $pay_no,
  250. 'refund_amount' => $money,
  251. ];
  252. Pay::alipay($config)->refund($order);
  253. return true;
  254. }
  255. catch(GatewayException $e){
  256. return false;
  257. }
  258. }
  259. public static function wxrefund_order($pay_no,$money,$pay_money){
  260. $config = [
  261. 'appid' => 'wxa5033cf91977e574', // APP APPID
  262. 'app_id' => 'wxa5033cf91977e574', // 公众号 APPID
  263. 'miniapp_id' => 'wxa5033cf91977e574', // 小程序 APPID
  264. 'mch_id' => '1634830915',
  265. 'key' => '7aa3fg0htful8nttb135rnusbvmeggbi',
  266. 'notify_url' => '',
  267. 'cert_client' => '/www/wwwroot/ship-expert.zhousi.hdlkeji.com/ship-expert/public/cert/apiclient_cert.pem', // optional, 退款,红包等情况时需要用到
  268. 'cert_key' => '/www/wwwroot/ship-expert.zhousi.hdlkeji.com/ship-expert/public/cert/apiclient_key.pem',// optional, 退款,红包等情况时需要用到
  269. 'log' => [ // optional
  270. 'file' => './logs/wechat.log',
  271. 'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
  272. 'type' => 'single', // optional, 可选 daily.
  273. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
  274. ],
  275. 'http' => [ // optional
  276. 'timeout' => 5.0,
  277. 'connect_timeout' => 5.0,
  278. // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
  279. ],
  280. // 'mode' => 'dev',
  281. ];
  282. try{
  283. $order = [
  284. 'out_trade_no' => $pay_no,
  285. 'out_refund_no' => time(),
  286. 'total_fee' => $pay_money*100,
  287. 'refund_fee' => $money*100,
  288. 'refund_desc' => '退款',
  289. ];
  290. Pay::wechat($config)->refund($order); // 返回 `Yansongda\Supports\Collection` 实例,可以通过 `$result->xxx` 访问服务器返回的数据。
  291. return true;
  292. }
  293. catch(GatewayException $e){
  294. return false;
  295. }
  296. }
  297. /**
  298. * @Title ("立即发货")
  299. * @Method ("post")
  300. * @Param ("order_no",desc="订单编号")
  301. * @Param ("send_number",desc="快递编号")
  302. * @Param ("company_name",desc="快递公司")
  303. * @Param ("name",desc="发货姓名")
  304. * @Param ("phone",desc="发货联系号码")
  305. * @Param ("province",desc="省")
  306. * @Param ("city",desc="城市")
  307. * @Param ("area",desc="地区")
  308. * @Param ("address",desc="详细地址")
  309. * @return void
  310. */
  311. public function order_send(){
  312. $admin_id = $this->uuid();
  313. $data = $this->_vali(
  314. [
  315. 'order_no.require'=>'订单信息不能为空',
  316. 'send_number.require'=>'快递单号不能为空',
  317. 'company_name.require'=>'快递公司名称不能为空',
  318. 'name.require'=>'发货商家联系人',
  319. 'phone.require'=>'商家联系号码',
  320. 'province.require'=>'发货商家省',
  321. 'city.require'=>'商家市',
  322. 'area.require'=>'发货商家区县',
  323. 'address.require'=>'详细地址'
  324. ]
  325. );
  326. $send_data = [
  327. 'send_number'=>$data['send_number'],
  328. 'company_name'=>$data['company_name'],
  329. 'send_datetime'=>date('Y-m-d H:i:s'),
  330. 'fa_name'=>$data['name'],
  331. 'fa_phone'=>$data['phone'],
  332. 'fa_address'=>$data['province'].$data['city'].$data['area'].$data['address'],
  333. 'shou_name'=>$data['name'],
  334. 'shou_phone'=>$data['phone'],
  335. 'shou_address'=>$data['province'].$data['city'].$data['area'].$data['address'],
  336. ];
  337. ShopOrderSend::mk()->where('order_no',$data['order_no'])->save($send_data);
  338. ShopOrder::mk()->where('order_no',$data['order_no'])->save(['status'=>5]);
  339. $this->success('发货成功');
  340. }
  341. /**
  342. * @Title ("删除退款记录")
  343. * @Method ("post")
  344. * @Param ("order_id",desc="订单id")
  345. * @return void
  346. */
  347. public function del_refund(){
  348. $this->uuid();
  349. $data = $this->_vali(
  350. [
  351. 'order_id.require'=>'订单id不能为空',
  352. ]
  353. );
  354. ShopOrder::mk()->where('id',$data['order_id'])->save(['refund_del'=>2]);
  355. $this->success('退款记录已清除');
  356. }
  357. public function uuid(){
  358. $purchase_model = new Common($this->app);
  359. $uuid = $purchase_model->uuid();
  360. return $uuid;
  361. }
  362. }