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.
 
 
 
 
 
 

748 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\OrderProduct;
  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. try {
  209. $product->where(["id"=>$val['product_id']])->setInc("stock", $val["number"])->setInc("real_sales",-$val["number"]);
  210. }catch (Exception $e){
  211. print_r($e);die;
  212. }
  213. }
  214. if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL && $order['have_paid'] == \addons\unishop\model\Order::PAID_NO) {
  215. $order->status = \addons\unishop\model\Order::STATUS_CANCEL;
  216. $order->save();
  217. $this->success('取消成功', true);
  218. }
  219. }
  220. /**
  221. * 删除订单
  222. * 只能删除已取消或已退货的订单
  223. */
  224. public function delete()
  225. {
  226. $order_id = $this->request->get('order_id', 0);
  227. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  228. $orderModel = new \addons\unishop\model\Order();
  229. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  230. if (!$order) {
  231. $this->error(__('Order not exist'));
  232. }
  233. if ($order['status'] == \addons\unishop\model\Order::STATUS_NORMAL) {
  234. $this->error('只能删除已取消或已退货的订单');
  235. }
  236. if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND && $order['refund_status'] == \addons\unishop\model\Order::REFUND_STATUS_APPLY) {
  237. $this->error('订单退款中,不可删除订单');
  238. }
  239. $order->delete();
  240. $this->success('删除成功', true);
  241. }
  242. /**
  243. * 确认收货
  244. */
  245. public function received()
  246. {
  247. $order_id = $this->request->get('order_id', 0);
  248. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  249. $orderModel = new \addons\unishop\model\Order();
  250. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  251. if (!$order) {
  252. $this->error(__('Order not exist'));
  253. }
  254. if ($order->have_delivered == 0) {
  255. $this->error('未发货,不能确认收货');
  256. }
  257. $order->have_received = time();
  258. $order->save();
  259. $this->success('已确认收货', true);
  260. }
  261. /**
  262. * 发表评论
  263. */
  264. public function comment()
  265. {
  266. $rate = $this->request->post('rate', 5);
  267. $anonymous = $this->request->post('anonymous', 0);
  268. $comment = $this->request->post('comment');
  269. $order_id = $this->request->post('order_id', 0);
  270. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  271. $product_id = $this->request->post('product_id');
  272. $product_id = \addons\unishop\extend\Hashids::decodeHex($product_id);
  273. $orderProductModel = new \addons\unishop\model\OrderProduct();
  274. $orderProduct = $orderProductModel->where(['product_id' => $product_id, 'order_id' => $order_id, 'user_id' => $this->auth->id])->find();
  275. $orderModel = new \addons\unishop\model\Order();
  276. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  277. if (!$orderProduct || !$order) {
  278. $this->error(__('Order not exist'));
  279. }
  280. if ($order->have_received == $orderModel::RECEIVED_NO) {
  281. $this->error(__('未收货,不可评价'));
  282. }
  283. $result = false;
  284. try {
  285. $evaluate = new Evaluate();
  286. $evaluate->user_id = $this->auth->id;
  287. $evaluate->order_id = $order_id;
  288. $evaluate->product_id = $product_id;
  289. $evaluate->rate = $rate;
  290. $evaluate->anonymous = $anonymous;
  291. $evaluate->comment = $comment;
  292. $evaluate->spec = $orderProduct->spec;
  293. $result = $evaluate->save();
  294. if ($result) {
  295. $order->have_commented = time();
  296. $order->save();
  297. }
  298. } catch (Exception $e) {
  299. $this->error($e->getMessage());
  300. }
  301. if ($result !== false) {
  302. $this->success(__('Thanks for the evaluation'));
  303. } else {
  304. $this->error(__('Evaluation failure'));
  305. }
  306. }
  307. /**
  308. * 获取订单数量
  309. */
  310. public function count()
  311. {
  312. if (!$this->auth->isLogin()) {
  313. $this->error('');
  314. }
  315. $order = new \addons\unishop\model\Order();
  316. $list = $order
  317. ->where([
  318. 'user_id' => $this->auth->id,
  319. ])
  320. ->where('status', '<>', \addons\unishop\model\Order::STATUS_CANCEL)
  321. ->where(function ($query) {
  322. $query
  323. ->whereOr([
  324. 'have_paid' => \addons\unishop\model\Order::PAID_NO,
  325. 'have_delivered' => \addons\unishop\model\Order::DELIVERED_NO,
  326. 'have_received' => \addons\unishop\model\Order::RECEIVED_NO,
  327. 'have_commented' => \addons\unishop\model\Order::COMMENTED_NO
  328. ])
  329. ->whereOr('refund_status', '>', \addons\unishop\model\Order::REFUND_STATUS_NONE);
  330. })
  331. ->field('have_paid,have_delivered,have_received,have_commented,refund_status,had_refund')
  332. ->select();
  333. $data = [
  334. 'unpaid' => 0,
  335. 'undelivered' => 0,
  336. 'unreceived' => 0,
  337. 'uncomment' => 0,
  338. 'refund' => 0
  339. ];
  340. foreach ($list as $item) {
  341. switch (true) {
  342. case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] > 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  343. $data['uncomment']++;
  344. break;
  345. case $item['have_paid'] > 0 && $item['have_delivered'] > 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  346. $data['unreceived']++;
  347. break;
  348. case $item['have_paid'] > 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  349. $data['undelivered']++;
  350. break;
  351. case $item['have_paid'] == 0 && $item['have_delivered'] == 0 && $item['have_received'] == 0 && $item['have_commented'] == 0 && $item['refund_status'] == 0:
  352. $data['unpaid']++;
  353. break;
  354. case $item['refund_status'] > 0 && $item['had_refund'] == 0 && $item['refund_status'] != 3:
  355. $data['refund']++;
  356. break;
  357. }
  358. }
  359. $this->success('', $data);
  360. }
  361. /**
  362. * 订单详情细节
  363. */
  364. public function detail()
  365. {
  366. $order_id = $this->request->get('order_id', 0);
  367. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  368. try {
  369. $orderModel = new \addons\unishop\model\Order();
  370. $order = $orderModel
  371. ->with([
  372. 'products' => function ($query) {
  373. $query->field('id,order_id,image,number,price,spec,title,product_id');
  374. },
  375. 'extend' => function ($query) {
  376. $query->field('id,order_id,address_id,address_json,express_number');
  377. },
  378. 'evaluate' => function ($query) {
  379. $query->field('id,order_id,product_id');
  380. }
  381. ])
  382. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  383. if ($order) {
  384. $order = $order->append(['state', 'paidtime', 'deliveredtime', 'receivedtime', 'commentedtime', 'pay_type_text', 'refund_status_text'])->toArray();
  385. // 快递单号
  386. $order['express_number'] = $order['extend']['express_number'];
  387. // 送货地址
  388. // $address = json_decode($order['extend']['address_json'], true);
  389. // $area = (new \addons\unishop\model\Area())
  390. // ->whereIn('id', [$address['province_id'], $address['city_id'], $address['area_id']])
  391. // ->column('name', 'id');
  392. // $delivery['username'] = $address['name'];
  393. // $delivery['mobile'] = $address['mobile'];
  394. // $delivery['address'] = $area[$address['province_id']] . ' ' . $area[$address['city_id']] . ' ' . $area[$address['area_id']] . ' ' . $address['address'];
  395. // $order['delivery'] = $delivery;
  396. // 是否已评论
  397. $evaluate = array_column($order['evaluate'], 'product_id');
  398. foreach ($order['products'] as &$product) {
  399. $product['image'] = Config::getImagesFullUrl($product['image']);
  400. if (in_array($product['id'], $evaluate)) {
  401. $product['evaluate'] = true;
  402. } else {
  403. $product['evaluate'] = false;
  404. }
  405. }
  406. unset($order['evaluate']);
  407. unset($order['extend']);
  408. }
  409. $this->success('', $order);
  410. } catch (Exception $e) {
  411. $this->error($e->getMessage());
  412. }
  413. }
  414. /**
  415. * 申请售后信息
  416. */
  417. public function refundInfo()
  418. {
  419. $order_id = $this->request->post('order_id');
  420. $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
  421. $orderModel = new \addons\unishop\model\Order();
  422. $order = $orderModel
  423. ->with([
  424. 'products' => function ($query) {
  425. $query->field('id,order_id,image,number,price,spec,title,product_id,(1) as choose');
  426. },
  427. 'refund',
  428. 'refundProducts'
  429. ])
  430. ->field('id,status,total_price,delivery_price,have_commented,have_delivered,have_paid,have_received,refund_status')
  431. ->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  432. if (!$order) {
  433. $this->error(__('Order not exist'));
  434. }
  435. $order = $order->append(['refund_status_text'])->toArray();
  436. foreach ($order['products'] as &$product) {
  437. $product['image'] = Config::getImagesFullUrl($product['image']);
  438. $product['choose'] = 0;
  439. // 如果是已提交退货的全选
  440. if ($order['status'] == \addons\unishop\model\Order::STATUS_REFUND) {
  441. foreach ($order['refund_products'] as $refundProduct) {
  442. if ($product['order_product_id'] == $refundProduct['order_product_id']) {
  443. $product['choose'] = 1;
  444. }
  445. }
  446. }
  447. }
  448. unset($order['refund_products']);
  449. $this->success('', $order);
  450. }
  451. /**
  452. * 申请售后
  453. * @throws \think\db\exception\DataNotFoundException
  454. * @throws \think\db\exception\ModelNotFoundException
  455. * @throws \think\exception\DbException
  456. */
  457. public function refund()
  458. {
  459. $order_id = $this->request->post('order_id');
  460. $order_id = Hashids::decodeHex($order_id);
  461. $orderModel = new \addons\unishop\model\Order();
  462. $order = $orderModel->where(['id' => $order_id, 'user_id' => $this->auth->id])->find();
  463. if (!$order) {
  464. $this->error(__('Order not exist'));
  465. }
  466. if ($order['have_paid'] == 0) {
  467. $this->error(__('订单未支付,可直接取消,无需申请售后'));
  468. }
  469. $amount = $this->request->post('amount', 0);
  470. $serviceType = $this->request->post('service_type');
  471. $receivingStatus = $this->request->post('receiving_status');
  472. $reasonType = $this->request->post('reason_type');
  473. $refundExplain = $this->request->post('refund_explain');
  474. $orderProductId = $this->request->post('order_product_id');
  475. if (!$orderProductId) {
  476. $this->error(__('Please select goods'));
  477. }
  478. if (!in_array($receivingStatus, [OrderRefund::UNRECEIVED, OrderRefund::RECEIVED])) {
  479. $this->error(__('Please select goods status'));
  480. }
  481. if (!in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN, OrderRefund::TYPE_EXCHANGE])) {
  482. $this->error(__('Please select service type'));
  483. }
  484. if (in_array($serviceType, [OrderRefund::TYPE_REFUND_NORETURN, OrderRefund::TYPE_REFUND_RETURN]) && $order['total_price'] > 0) {
  485. if (!$amount) {
  486. $this->error(__('Please fill in the refund amount'));
  487. }
  488. }
  489. try {
  490. Db::startTrans();
  491. $orderRefund = new OrderRefund();
  492. $orderRefund->user_id = $this->auth->id;
  493. $orderRefund->order_id = $order_id;
  494. $orderRefund->receiving_status = $receivingStatus;
  495. $orderRefund->service_type = $serviceType;
  496. $orderRefund->reason_type = $reasonType;
  497. $orderRefund->amount = $amount;
  498. $orderRefund->refund_explain = $refundExplain;
  499. $orderRefund->save();
  500. $productIdArr = explode(',', $orderProductId);
  501. $refundProduct = [];
  502. foreach ($productIdArr as $orderProductId) {
  503. $tmp['order_product_id'] = $orderProductId;
  504. $tmp['order_id'] = $order_id;
  505. $tmp['user_id'] = $this->auth->id;
  506. $tmp['refund_id'] = $orderRefund['id'];
  507. $tmp['createtime'] = time();
  508. $refundProduct[] = $tmp;
  509. }
  510. (new OrderRefundProduct)->insertAll($refundProduct);
  511. $order->status = \addons\unishop\model\Order::STATUS_REFUND;
  512. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  513. $order->save();
  514. Db::commit();
  515. $this->success(__('Commit'), 1);
  516. } catch (Exception $e) {
  517. Db::rollback();
  518. $this->error($e->getMessage());
  519. }
  520. }
  521. public function doRefund(){
  522. $this->error("暂不支持");
  523. $order_id = $this->request->post('order_id');
  524. $order_id = Hashids::decodeHex($order_id);
  525. $orderModel = new \addons\unishop\model\Order();
  526. $order = $orderModel->where([
  527. 'id' => $order_id,
  528. 'user_id' => $this->auth->id,
  529. 'status'=>1,//订单状态正常
  530. 'have_paid'=>[">",0],//已支付
  531. 'have_received'=>0,//订单完成前
  532. 'had_refund'=>0
  533. ])->find();
  534. if (!$order){
  535. $this->error("订单信息错误");
  536. }
  537. // $redis = new Redis();
  538. // $flash = $orderModel->alias("o")->
  539. // join("unishop_order_product","unishop_order_product.order_id =o.id")
  540. // ->join("unishop_flash_sale","unishop_order_product.flash_id=unishop_flash_sale.id")
  541. // ->where(["o.id"=>(int)$order_id,"unishop_flash_sale.status"=>0,"unishop_flash_sale.switch"=>1,"unishop_flash_sale.deletetime"=>0])
  542. // ->field("flash_id,product_id,endtime")->find();
  543. // if (!$flash || $flash->endtime < time()){
  544. // $this->error("活动已结束,不支持退款");
  545. // }
  546. // $sold = $redis->handler->hIncrBy('flash_sale_' . $flash->flash_id. '_' . $flash->product_id, 'sold', -1);
  547. $order->status = \addons\unishop\model\Order::STATUS_REFUND;
  548. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_AGREE;
  549. $result = $order->save();
  550. if ($result !== false){
  551. //order_id:订单ID name:订单名称 total_fee:总金额-元 refund_fee退款金额
  552. $param = [
  553. "order_id"=>$order['out_trade_no'],
  554. "total_fee"=>$order['total_price'],
  555. "refund_fee"=>$order['total_price'],
  556. "memo"=>'订单退款',
  557. ];
  558. PayService::cancel($param,$order["pay_type"]);
  559. $this->success("success",[]);
  560. }
  561. $this->error("订单信息错误");
  562. }
  563. /**
  564. * 售后发货
  565. */
  566. public function refundDelivery()
  567. {
  568. $orderId = $this->request->post('order_id');
  569. $expressNumber = $this->request->post('express_number');
  570. if (!$expressNumber) {
  571. $this->error(__('Please fill in the express number'));
  572. }
  573. $orderId = Hashids::decodeHex($orderId);
  574. $orderModel = new \addons\unishop\model\Order();
  575. $order = $orderModel
  576. ->where(['id' => $orderId, 'user_id' => $this->auth->id])
  577. ->with(['refund'])->find();
  578. if (!$order || !$order->refund) {
  579. $this->error(__('Order not exist'));
  580. }
  581. try {
  582. Db::startTrans();
  583. $order->refund->express_number = $expressNumber;
  584. $order->refund_status = \addons\unishop\model\Order::REFUND_STATUS_APPLY;
  585. if ($order->refund->save() && $order->save()) {
  586. Db::commit();
  587. $this->success('', 1);
  588. } else {
  589. throw new Exception(__('Operation failed'));
  590. }
  591. } catch (Exception $e) {
  592. Db::rollback();
  593. $this->success($e->getMessage());
  594. }
  595. }
  596. /**
  597. * Des: 生成二维码
  598. * Name: addQRCode
  599. * @param $qCode string 生成的内容
  600. * @param $QRFile string 二维码图片路径
  601. * @param int $reType 1:返回成功或失败 2 返回图片数据流
  602. * @param bool|false $isCreate 生成的图片是否保留 当$reType=2才会有效
  603. * @param bool $logo 是否添加logo图
  604. * @param string $logoUrl logo图地址
  605. * @return array
  606. * @author 倪宗锋
  607. */
  608. public function addQRCode()
  609. {
  610. include ROOT_PATH . '/addons/unishop/library/phpqrcode/phpqrcode.php';
  611. $value = $this->request->request('url');//二维码内容
  612. $value = str_replace("&amp;","&",$value);
  613. $errorCorrectionLevel = 'H';//容错级别
  614. $matrixPointSize = 6;//生成图片大小
  615. $QRFile = ROOT_PATH . '/addons/unishop/library/phpqrcode/' . time().rand(1000,9999) . '.png';
  616. //生成二维码图片
  617. \QRcode::png($value, $QRFile, $errorCorrectionLevel, $matrixPointSize, 2);
  618. $QR = imagecreatefromstring(file_get_contents($QRFile));
  619. //二维码
  620. $logoUrl = imagecreatefromstring(file_get_contents(ROOT_PATH . '/addons/unishop/library/phpqrcode/logo.png'));
  621. $QR_width = imagesx($QR);//二维码图片宽度
  622. $logo_width = imagesx($logoUrl);//logo图片宽度
  623. $logo_height = imagesy($logoUrl);//logo图片高度
  624. $logo_qr_width = $QR_width / 5;
  625. $scale = $logo_width / $logo_qr_width;
  626. $logo_qr_height = $logo_height / $scale;
  627. $from_width = ($QR_width - $logo_qr_width) / 2;
  628. //重新组合图片并调整大小
  629. imagecopyresampled($QR, $logoUrl, $from_width, $from_width, 0, 0, $logo_qr_width,
  630. $logo_qr_height, $logo_width, $logo_height);
  631. //输出图片
  632. Header("Content-type: image/png");
  633. ImagePng($QR);
  634. @unlink($QRFile);
  635. die;
  636. }
  637. }