You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

743 lines
27 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2019/11/9
  6. * Time: 10:00 下午
  7. */
  8. namespace addons\unishop\controller;
  9. use addons\nzf\AliPay;
  10. use addons\nzf\PayService;
  11. use addons\unishop\extend\Hashids;
  12. use addons\unishop\extend\Redis;
  13. use addons\unishop\model\Area;
  14. use addons\unishop\model\Config;
  15. use addons\unishop\model\Evaluate;
  16. use addons\unishop\model\Product;
  17. use app\admin\model\unishop\Coupon as CouponModel;
  18. use addons\unishop\model\DeliveryRule as DeliveryRuleModel;
  19. use addons\unishop\model\OrderRefund;
  20. use app\admin\model\unishop\OrderRefundProduct;
  21. use think\Db;
  22. use think\Exception;
  23. use addons\unishop\model\Address as AddressModel;
  24. use think\Hook;
  25. use think\Loader;
  26. /**
  27. * 订单相关接口
  28. * Class Order
  29. * @package addons\unishop\controller
  30. */
  31. class Order extends Base
  32. {
  33. /**
  34. * 允许频繁访问的接口
  35. * @var array
  36. */
  37. protected $frequently = ['getorders'];
  38. protected $noNeedLogin = ["addQRCode"];
  39. /**
  40. * 创建订单
  41. */
  42. public function create()
  43. {
  44. $activityTime=Config::getByName('activity_time')['value'];
  45. if (strtotime($activityTime)<time()){
  46. $this->error(__('Activity is end'));
  47. }
  48. $productId = $this->request->post('id', 0);
  49. try {
  50. $user_id = $this->auth->id;
  51. // 单个商品
  52. if ($productId) {
  53. $productId = \addons\unishop\extend\Hashids::decodeHex($productId);
  54. $product = (new Product)->where(['id' => $productId, 'switch' => Product::SWITCH_ON, 'deletetime' => null])->find();
  55. /** 产品基础数据 **/
  56. $spec = $this->request->post('spec', '');
  57. $productData[0] = $product->getDataOnCreateOrder($spec);
  58. } else {
  59. // 多个商品
  60. $cart = $this->request->post('cart');
  61. $carts = (new \addons\unishop\model\Cart)
  62. ->whereIn('id', $cart)
  63. ->with(['product'])
  64. ->order(['id' => 'desc'])
  65. ->select();
  66. foreach ($carts as $cart) {
  67. if ($cart->product instanceof Product) {
  68. $productData[] = $cart->product->getDataOnCreateOrder($cart->spec ? $cart->spec : '', $cart->number);
  69. }
  70. }
  71. }
  72. if (empty($productData) || !$productData) {
  73. $this->error(__('Product not exist'));
  74. }
  75. /** 默认地址 **/
  76. // $address = (new AddressModel)->where(['user_id' => $user_id, 'is_default' => AddressModel::IS_DEFAULT_YES])->find();
  77. // if ($address) {
  78. // $area = (new Area)->whereIn('id', [$address->province_id, $address->city_id, $address->area_id])->column('name', 'id');
  79. // $address = $address->toArray();
  80. // $address['province']['name'] = $area[$address['province_id']];
  81. // $address['city']['name'] = $area[$address['city_id']];
  82. // $address['area']['name'] = $area[$address['area_id']];
  83. // }
  84. /** 可用优惠券 **/
  85. $coupon = CouponModel::all(function ($query) {
  86. $time = time();
  87. $query
  88. ->where(['switch' => CouponModel::SWITCH_ON])
  89. ->where('starttime', '<', $time)
  90. ->where('endtime', '>', $time);
  91. });
  92. if ($coupon) {
  93. $coupon = collection($coupon)->toArray();
  94. }
  95. /** 运费数据 **/
  96. // $cityId = $address['city_id'] ? $address['city_id'] : 0;
  97. // $delivery = (new DeliveryRuleModel())->getDelivetyByArea($cityId);
  98. foreach ($productData as &$product) {
  99. $product['image'] = Config::getImagesFullUrl($product['image']);
  100. $product['sales_price'] = number_format($product['sales_price'], 2);
  101. $product['market_price'] = number_format($product['market_price'], 2);
  102. }
  103. $this->success('', [
  104. 'product' => $productData,
  105. // 'address' => $address,
  106. 'coupon' => $coupon,
  107. // 'delivery' => $delivery['list']
  108. ]);
  109. } catch (Exception $e) {
  110. $this->error($e->getMessage(), false);
  111. }
  112. }
  113. /**
  114. * 提交订单
  115. */
  116. public function submit()
  117. {
  118. $data = $this->request->post();
  119. try {
  120. $validate = Loader::validate('\\addons\\unishop\\validate\\Order');
  121. if (!$validate->check($data, [], 'submit')) {
  122. throw new Exception($validate->getError());
  123. }
  124. Db::startTrans();
  125. // 判断创建订单的条件
  126. if (empty(Hook::get('create_order_before'))) {
  127. Hook::add('create_order_before', 'addons\\unishop\\behavior\\Order');
  128. }
  129. // 减少商品库存,增加"已下单未支付数量"
  130. if (empty(Hook::get('create_order_after'))) {
  131. Hook::add('create_order_after', 'addons\\unishop\\behavior\\Order');
  132. }
  133. $orderModel = new \addons\unishop\model\Order();
  134. $result = $orderModel->createOrder($this->auth->id, $data);
  135. Db::commit();
  136. $this->success('', $result);
  137. } catch (Exception $e) {
  138. Db::rollback();
  139. $this->error($e->getMessage(), false);
  140. }
  141. }
  142. /**
  143. * 获取运费模板
  144. */
  145. public function getDelivery()
  146. {
  147. $cityId = $this->request->get('city_id', 0);
  148. $delivery = (new DeliveryRuleModel())->getDelivetyByArea($cityId);
  149. $this->success('', $delivery['list']);
  150. }
  151. /**
  152. * 获取订单信息
  153. */
  154. public function getOrders()
  155. {
  156. // 0=全部,1=待付款,2=待发货,3=待收货,4=待评价,5=售后
  157. $type = $this->request->get('type', 0);
  158. $page = $this->request->get('page', 1);
  159. $pagesize = $this->request->get('pagesize', 10);
  160. try {
  161. $orderModel = new \addons\unishop\model\Order();
  162. $result = $orderModel->getOrdersByType($this->auth->id, $type, $page, $pagesize);
  163. $this->success('', $result);
  164. } catch (Exception $e) {
  165. $this->error($e->getMessage());
  166. }
  167. }
  168. /**
  169. * 取消订单
  170. * 未支付的订单才叫取消,已支付的叫退货
  171. */
  172. public function cancel()
  173. {
  174. $order_id = $this->request->get('order_id', 0);
  175. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  176. $orderModel = new \addons\unishop\model\Order();
  177. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  178. if (!$order) {
  179. $this->error(__('Order not exist'));
  180. }
  181. switch ($order['status']) {
  182. case \addons\unishop\model\Order::STATUS_REFUND:
  183. $this->error('此订单已退款,无法取消');
  184. break;
  185. case \addons\unishop\model\Order::STATUS_CANCEL:
  186. $this->error('此订单已取消, 无需再取消');
  187. break;
  188. }
  189. if ($order['have_paid'] != \addons\unishop\model\Order::PAID_NO) {
  190. $this->error('此订单已支付,无法取消');
  191. }
  192. // $redis = new Redis();
  193. // $flash = (new \addons\unishop\model\Order)->alias("o")->
  194. // join("unishop_order_product","unishop_order_product.order_id =o.id")
  195. // ->where(["o.id"=>(int)$order_id])
  196. // ->field("flash_id,product_id")->find();
  197. // if ($flash){
  198. // $redis->handler->hIncrBy('flash_sale_' . $flash->flash_id. '_' . $flash->product_id, 'sold', -1);
  199. // }
  200. $orderProduct = new OrderProduct();
  201. $list =$orderProduct->where([
  202. 'order_id' => $order_id
  203. ])->select();
  204. $list = collection($list)->toArray();
  205. $product = new \app\admin\model\unishop\Product();
  206. foreach ($list as $val){
  207. $product->where(["id"=>$val['product_id']])->setInc("stock",$val["number"])->setInc("real_sales",-$val["number"]);
  208. }
  209. if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL && $order['have_paid'] == \addons\unishop\model\Order::PAID_NO) {
  210. $order->status = \addons\unishop\model\Order::STATUS_CANCEL;
  211. $order->save();
  212. $this->success('取消成功', true);
  213. }
  214. }
  215. /**
  216. * 删除订单
  217. * 只能删除已取消或已退货的订单
  218. */
  219. public function delete()
  220. {
  221. $order_id = $this->request->get('order_id', 0);
  222. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  223. $orderModel = new \addons\unishop\model\Order();
  224. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  225. if (!$order) {
  226. $this->error(__('Order not exist'));
  227. }
  228. if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL) {
  229. $this->error('只能删除已取消或已退货的订单');
  230. }
  231. if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND && $order['refund_status'] == \addons\unishop\model\Order::REFUND_STATUS_APPLY) {
  232. $this->error('订单退款中,不可删除订单');
  233. }
  234. $order->delete();
  235. $this->success('删除成功', true);
  236. }
  237. /**
  238. * 确认收货
  239. */
  240. public function received()
  241. {
  242. $order_id = $this->request->get('order_id', 0);
  243. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  244. $orderModel = new \addons\unishop\model\Order();
  245. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  246. if (!$order) {
  247. $this->error(__('Order not exist'));
  248. }
  249. if ($order->have_delivered == 0) {
  250. $this->error('未发货,不能确认收货');
  251. }
  252. $order->have_received = time();
  253. $order->save();
  254. $this->success('已确认收货', true);
  255. }
  256. /**
  257. * 发表评论
  258. */
  259. public function comment()
  260. {
  261. $rate = $this->request->post('rate', 5);
  262. $anonymous = $this->request->post('anonymous', 0);
  263. $comment = $this->request->post('comment');
  264. $order_id = $this->request->post('order_id', 0);
  265. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  266. $product_id = $this->request->post('product_id');
  267. $product_id = \addons\unishop\extend\Hashids::decodeHex($product_id);
  268. $orderProductModel = new \addons\unishop\model\OrderProduct();
  269. $orderProduct = $orderProductModel->where(['product_id' => $product_id, 'order_id' => $order_id, 'user_id' => $this->auth->id])->find();
  270. $orderModel = new \addons\unishop\model\Order();
  271. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  272. if (!$orderProduct || !$order) {
  273. $this->error(__('Order not exist'));
  274. }
  275. if ($order->have_received == $orderModel::RECEIVED_NO) {
  276. $this->error(__('未收货,不可评价'));
  277. }
  278. $result = false;
  279. try {
  280. $evaluate = new Evaluate();
  281. $evaluate->user_id = $this->auth->id;
  282. $evaluate->order_id = $order_id;
  283. $evaluate->product_id = $product_id;
  284. $evaluate->rate = $rate;
  285. $evaluate->anonymous = $anonymous;
  286. $evaluate->comment = $comment;
  287. $evaluate->spec = $orderProduct->spec;
  288. $result = $evaluate->save();
  289. if ($result) {
  290. $order->have_commented = time();
  291. $order->save();
  292. }
  293. } catch (Exception $e) {
  294. $this->error($e->getMessage());
  295. }
  296. if ($result !== false) {
  297. $this->success(__('Thanks for the evaluation'));
  298. } else {
  299. $this->error(__('Evaluation failure'));
  300. }
  301. }
  302. /**
  303. * 获取订单数量
  304. */
  305. public function count()
  306. {
  307. if (!$this->auth->isLogin()) {
  308. $this->error('');
  309. }
  310. $order = new \addons\unishop\model\Order();
  311. $list = $order
  312. ->where([
  313. 'user_id' => $this->auth->id,
  314. ])
  315. ->where('status', '<>', \addons\unishop\model\Order::STATUS_CANCEL)
  316. ->where(function ($query) {
  317. $query
  318. ->whereOr([
  319. 'have_paid' => \addons\unishop\model\Order::PAID_NO,
  320. 'have_delivered' => \addons\unishop\model\Order::DELIVERED_NO,
  321. 'have_received' => \addons\unishop\model\Order::RECEIVED_NO,
  322. 'have_commented' => \addons\unishop\model\Order::COMMENTED_NO
  323. ])
  324. ->whereOr('refund_status', '>', \addons\unishop\model\Order::REFUND_STATUS_NONE);
  325. })
  326. ->field('have_paid,have_delivered,have_received,have_commented,refund_status,had_refund')
  327. ->select();
  328. $data = [
  329. 'unpaid' => 0,
  330. 'undelivered' => 0,
  331. 'unreceived' => 0,
  332. 'uncomment' => 0,
  333. 'refund' => 0
  334. ];
  335. foreach ($list as $item) {
  336. switch (true) {
  337. case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] > 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  338. $data['uncomment']++;
  339. break;
  340. case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  341. $data['unreceived']++;
  342. break;
  343. case $item['have_paid'] > 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  344. $data['undelivered']++;
  345. break;
  346. case $item['have_paid'] == 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  347. $data['unpaid']++;
  348. break;
  349. case $item['refund_status'] > 0 && $item['had_refund'] == 0 && $item['refund_status'] != 3:
  350. $data['refund']++;
  351. break;
  352. }
  353. }
  354. $this->success('', $data);
  355. }
  356. /**
  357. * 订单详情细节
  358. */
  359. public function detail()
  360. {
  361. $order_id = $this->request->get('order_id', 0);
  362. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  363. try {
  364. $orderModel = new \addons\unishop\model\Order();
  365. $order = $orderModel
  366. ->with([
  367. 'products' => function ($query) {
  368. $query->field('id,order_id,image,number,price,spec,title,product_id');
  369. },
  370. 'extend' => function ($query) {
  371. $query->field('id,order_id,address_id,address_json,express_number');
  372. },
  373. 'evaluate' => function ($query) {
  374. $query->field('id,order_id,product_id');
  375. }
  376. ])
  377. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  378. if ($order) {
  379. $order = $order->append(['state', 'paidtime', 'deliveredtime', 'receivedtime', 'commentedtime', 'pay_type_text', 'refund_status_text'])->toArray();
  380. // 快递单号
  381. $order['express_number'] = $order['extend']['express_number'];
  382. // 送货地址
  383. // $address = json_decode($order['extend']['address_json'], true);
  384. // $area = (new \addons\unishop\model\Area())
  385. // ->whereIn('id', [$address['province_id'], $address['city_id'], $address['area_id']])
  386. // ->column('name', 'id');
  387. // $delivery['username'] = $address['name'];
  388. // $delivery['mobile'] = $address['mobile'];
  389. // $delivery['address'] = $area[$address['province_id']] . ' ' . $area[$address['city_id']] . ' ' . $area[$address['area_id']] . ' ' . $address['address'];
  390. // $order['delivery'] = $delivery;
  391. // 是否已评论
  392. $evaluate = array_column($order['evaluate'], 'product_id');
  393. foreach ($order['products'] as &$product) {
  394. $product['image'] = Config::getImagesFullUrl($product['image']);
  395. if (in_array($product['id'], $evaluate)) {
  396. $product['evaluate'] = true;
  397. } else {
  398. $product['evaluate'] = false;
  399. }
  400. }
  401. unset($order['evaluate']);
  402. unset($order['extend']);
  403. }
  404. $this->success('', $order);
  405. } catch (Exception $e) {
  406. $this->error($e->getMessage());
  407. }
  408. }
  409. /**
  410. * 申请售后信息
  411. */
  412. public function refundInfo()
  413. {
  414. $order_id = $this->request->post('order_id');
  415. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  416. $orderModel = new \addons\unishop\model\Order();
  417. $order = $orderModel
  418. ->with([
  419. 'products' => function ($query) {
  420. $query->field('id,order_id,image,number,price,spec,title,product_id,(1) as choose');
  421. },
  422. 'refund',
  423. 'refundProducts'
  424. ])
  425. ->field('id,status,total_price,delivery_price,have_commented,have_delivered,have_paid,have_received,refund_status')
  426. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  427. if (!$order) {
  428. $this->error(__('Order not exist'));
  429. }
  430. $order = $order->append(['refund_status_text'])->toArray();
  431. foreach ($order['products'] as &$product) {
  432. $product['image'] = Config::getImagesFullUrl($product['image']);
  433. $product['choose'] = 0;
  434. // 如果是已提交退货的全选
  435. if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND) {
  436. foreach ($order['refund_products'] as $refundProduct) {
  437. if ($product['order_product_id'] == $refundProduct['order_product_id']) {
  438. $product['choose'] = 1;
  439. }
  440. }
  441. }
  442. }
  443. unset($order['refund_products']);
  444. $this->success('', $order);
  445. }
  446. /**
  447. * 申请售后
  448. * @throws \think\db\exception\DataNotFoundException
  449. * @throws \think\db\exception\ModelNotFoundException
  450. * @throws \think\exception\DbException
  451. */
  452. public function refund()
  453. {
  454. $order_id = $this->request->post('order_id');
  455. $order_id = Hashids::decodeHex($order_id);
  456. $orderModel = new \addons\unishop\model\Order();
  457. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  458. if (!$order) {
  459. $this->error(__('Order not exist'));
  460. }
  461. if ($order['have_paid'] == 0) {
  462. $this->error(__('订单未支付,可直接取消,无需申请售后'));
  463. }
  464. $amount = $this->request->post('amount', 0);
  465. $serviceType = $this->request->post('service_type');
  466. $receivingStatus = $this->request->post('receiving_status');
  467. $reasonType = $this->request->post('reason_type');
  468. $refundExplain = $this->request->post('refund_explain');
  469. $orderProductId = $this->request->post('order_product_id');
  470. if (!$orderProductId) {
  471. $this->error(__('Please select goods'));
  472. }
  473. if (!in_array($receivingStatus, [OrderRefund::UNRECEIVED, OrderRefund::RECEIVED])) {
  474. $this->error(__('Please select goods status'));
  475. }
  476. if (!in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN, OrderRefund::TYPE_EXCHANGE])) {
  477. $this->error(__('Please select service type'));
  478. }
  479. if (in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN]) && $order['total_price'] > 0) {
  480. if (!$amount) {
  481. $this->error(__('Please fill in the refund amount'));
  482. }
  483. }
  484. try {
  485. Db::startTrans();
  486. $orderRefund = new OrderRefund();
  487. $orderRefund->user_id = $this->auth->id;
  488. $orderRefund->order_id = $order_id;
  489. $orderRefund->receiving_status = $receivingStatus;
  490. $orderRefund->service_type = $serviceType;
  491. $orderRefund->reason_type = $reasonType;
  492. $orderRefund->amount = $amount;
  493. $orderRefund->refund_explain = $refundExplain;
  494. $orderRefund->save();
  495. $productIdArr = explode(',', $orderProductId);
  496. $refundProduct = [];
  497. foreach ($productIdArr as $orderProductId) {
  498. $tmp['order_product_id'] = $orderProductId;
  499. $tmp['order_id'] = $order_id;
  500. $tmp['user_id'] = $this->auth->id;
  501. $tmp['refund_id'] = $orderRefund['id'];
  502. $tmp['createtime'] = time();
  503. $refundProduct[] = $tmp;
  504. }
  505. (new OrderRefundProduct)->insertAll($refundProduct);
  506. $order->status = \addons\unishop\model\Order::STATUS_REFUND;
  507. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  508. $order->save();
  509. Db::commit();
  510. $this->success(__('Commit'), 1);
  511. } catch (Exception $e) {
  512. Db::rollback();
  513. $this->error($e->getMessage());
  514. }
  515. }
  516. public function doRefund(){
  517. $this->error("暂不支持");
  518. $order_id = $this->request->post('order_id');
  519. $order_id = Hashids::decodeHex($order_id);
  520. $orderModel = new \addons\unishop\model\Order();
  521. $order = $orderModel->where([
  522. 'id' => $order_id,
  523. 'user_id' => $this->auth->id,
  524. 'status'=>1,//订单状态正常
  525. 'have_paid'=>[">",0],//已支付
  526. 'have_received'=>0,//订单完成前
  527. 'had_refund'=>0
  528. ])->find();
  529. if (!$order){
  530. $this->error("订单信息错误");
  531. }
  532. // $redis = new Redis();
  533. // $flash = $orderModel->alias("o")->
  534. // join("unishop_order_product","unishop_order_product.order_id =o.id")
  535. // ->join("unishop_flash_sale","unishop_order_product.flash_id=unishop_flash_sale.id")
  536. // ->where(["o.id"=>(int)$order_id,"unishop_flash_sale.status"=>0,"unishop_flash_sale.switch"=>1,"unishop_flash_sale.deletetime"=>0])
  537. // ->field("flash_id,product_id,endtime")->find();
  538. // if (!$flash || $flash->endtime < time()){
  539. // $this->error("活动已结束,不支持退款");
  540. // }
  541. // $sold = $redis->handler->hIncrBy('flash_sale_' . $flash->flash_id. '_' . $flash->product_id, 'sold', -1);
  542. $order->status = \addons\unishop\model\Order::STATUS_REFUND;
  543. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_AGREE;
  544. $result = $order->save();
  545. if ($result !== false){
  546. //order_id:订单ID name:订单名称 total_fee:总金额-元 refund_fee退款金额
  547. $param = [
  548. "order_id"=>$order['out_trade_no'],
  549. "total_fee"=>$order['total_price'],
  550. "refund_fee"=>$order['total_price'],
  551. "memo"=>'订单退款',
  552. ];
  553. PayService::cancel($param,$order["pay_type"]);
  554. $this->success("success",[]);
  555. }
  556. $this->error("订单信息错误");
  557. }
  558. /**
  559. * 售后发货
  560. */
  561. public function refundDelivery()
  562. {
  563. $orderId = $this->request->post('order_id');
  564. $expressNumber = $this->request->post('express_number');
  565. if (!$expressNumber) {
  566. $this->error(__('Please fill in the express number'));
  567. }
  568. $orderId = Hashids::decodeHex($orderId);
  569. $orderModel = new \addons\unishop\model\Order();
  570. $order = $orderModel
  571. ->where(['id' => $orderId, 'user_id' => $this->auth->id])
  572. ->with(['refund'])->find();
  573. if (!$order || !$order->refund) {
  574. $this->error(__('Order not exist'));
  575. }
  576. try {
  577. Db::startTrans();
  578. $order->refund->express_number = $expressNumber;
  579. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  580. if ($order->refund->save() && $order->save()) {
  581. Db::commit();
  582. $this->success('', 1);
  583. } else {
  584. throw new Exception(__('Operation failed'));
  585. }
  586. } catch (Exception $e) {
  587. Db::rollback();
  588. $this->success($e->getMessage());
  589. }
  590. }
  591. /**
  592. * Des: 生成二维码
  593. * Name: addQRCode
  594. * @param $qCode string 生成的内容
  595. * @param $QRFile string 二维码图片路径
  596. * @param int $reType 1:返回成功或失败 2 返回图片数据流
  597. * @param bool|false $isCreate 生成的图片是否保留 当$reType=2才会有效
  598. * @param bool $logo 是否添加logo图
  599. * @param string $logoUrl logo图地址
  600. * @return array
  601. * @author 倪宗锋
  602. */
  603. public function addQRCode()
  604. {
  605. include ROOT_PATH . '/addons/unishop/library/phpqrcode/phpqrcode.php';
  606. $value = $this->request->request('url');//二维码内容
  607. $value = str_replace("&amp;","&",$value);
  608. $errorCorrectionLevel = 'H';//容错级别
  609. $matrixPointSize = 6;//生成图片大小
  610. $QRFile = ROOT_PATH . '/addons/unishop/library/phpqrcode/' . time().rand(1000,9999) . '.png';
  611. //生成二维码图片
  612. \QRcode::png($value, $QRFile, $errorCorrectionLevel, $matrixPointSize, 2);
  613. $QR = imagecreatefromstring(file_get_contents($QRFile));
  614. //二维码
  615. $logoUrl = imagecreatefromstring(file_get_contents(ROOT_PATH . '/addons/unishop/library/phpqrcode/logo.png'));
  616. $QR_width = imagesx($QR);//二维码图片宽度
  617. $logo_width = imagesx($logoUrl);//logo图片宽度
  618. $logo_height = imagesy($logoUrl);//logo图片高度
  619. $logo_qr_width = $QR_width / 5;
  620. $scale = $logo_width / $logo_qr_width;
  621. $logo_qr_height = $logo_height / $scale;
  622. $from_width = ($QR_width - $logo_qr_width) / 2;
  623. //重新组合图片并调整大小
  624. imagecopyresampled($QR, $logoUrl, $from_width, $from_width, 0, 0, $logo_qr_width,
  625. $logo_qr_height, $logo_width, $logo_height);
  626. //输出图片
  627. Header("Content-type: image/png");
  628. ImagePng($QR);
  629. @unlink($QRFile);
  630. die;
  631. }
  632. }