No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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