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.
 
 
 
 
 
 

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