StoreOrder.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\model\store;
  12. use app\wap\model\special\Special;
  13. use app\wap\model\special\SpecialBuy;
  14. use app\wap\model\user\User;
  15. use app\wap\model\user\UserAddress;
  16. use app\wap\model\user\UserBill;
  17. use app\wap\model\user\WechatUser;
  18. use basic\ModelBasic;
  19. use behavior\wap\StoreProductBehavior;
  20. use behavior\wechat\PaymentBehavior;
  21. use service\AlipayTradeWapService;
  22. use service\HookService;
  23. use service\SystemConfigService;
  24. use service\WechatService;
  25. use service\WechatTemplateService;
  26. use think\Cache;
  27. use think\Url;
  28. use traits\ModelTrait;
  29. use app\wap\model\user\MemberShip;
  30. use service\GroupDataService;
  31. class StoreOrder extends ModelBasic
  32. {
  33. use ModelTrait;
  34. protected $insert = ['add_time'];
  35. protected static $payType = ['weixin' => '微信支付', 'yue' => '余额支付', 'offline' => '线下支付', 'zhifubao' => '支付宝'];
  36. protected static $deliveryType = ['send' => '商家配送', 'express' => '快递配送'];
  37. protected function setAddTimeAttr()
  38. {
  39. return time();
  40. }
  41. protected function setCartIdAttr($value)
  42. {
  43. return is_array($value) ? json_encode($value) : $value;
  44. }
  45. protected function getCartIdAttr($value)
  46. {
  47. return json_decode($value, true);
  48. }
  49. public static function getOrderPriceGroup($cartInfo)
  50. {
  51. $storePostage = 0;
  52. $totalPrice = self::getOrderTotalPrice($cartInfo);
  53. $costPrice = self::getOrderCostPrice($cartInfo);
  54. foreach ($cartInfo as $cart) {
  55. if (!$cart['productInfo']['is_postage']){
  56. $storePostage = bcadd($storePostage, $cart['productInfo']['postage'], 2);
  57. if(bcsub($cart['productInfo']['free_shipping'],$cart['cart_num'],0)<=0){
  58. $storePostage =0;
  59. }
  60. }
  61. }
  62. return compact('storePostage', 'totalPrice', 'costPrice');
  63. }
  64. public static function getOrderTotalPrice($cartInfo)
  65. {
  66. $totalPrice = 0;
  67. foreach ($cartInfo as $cart) {
  68. $totalPrice = bcadd($totalPrice, bcmul($cart['cart_num'], $cart['truePrice'], 2), 2);
  69. }
  70. return $totalPrice;
  71. }
  72. public static function getOrderCostPrice($cartInfo)
  73. {
  74. $costPrice = 0;
  75. foreach ($cartInfo as $cart) {
  76. $costPrice = bcadd($costPrice, bcmul($cart['cart_num'], $cart['costPrice'], 2), 2);
  77. }
  78. return $costPrice;
  79. }
  80. public static function getPinkOrderId($id)
  81. {
  82. return self::where('id', $id)->value('order_id');
  83. }
  84. public static function cacheOrderInfo($uid, $cartInfo, $priceGroup, $cacheTime = 600)
  85. {
  86. $site_url = SystemConfigService::get('site_url');
  87. $subjectUrl=getUrlToDomain($site_url);
  88. $key = md5(time());
  89. Cache::store("redis")->set($subjectUrl.'user_order_' . $uid . $key, compact('cartInfo', 'priceGroup'), $cacheTime);
  90. return $key;
  91. }
  92. public static function getCacheOrderInfo($uid, $key)
  93. {
  94. $site_url = SystemConfigService::get('site_url');
  95. $subjectUrl=getUrlToDomain($site_url);
  96. $cacheName = $subjectUrl.'user_order_' . $uid . $key;
  97. if (!Cache::store("redis")->has($cacheName)) return null;
  98. return Cache::store("redis")->get($cacheName);
  99. }
  100. public static function clearCacheOrderInfo($uid, $key)
  101. {
  102. $site_url = SystemConfigService::get('site_url');
  103. $subjectUrl=getUrlToDomain($site_url);
  104. Cache::store("redis")->clear($subjectUrl.'user_order_' . $uid . $key);
  105. }
  106. public static function getSpecialIds($uid)
  107. {
  108. return self::where(['is_del' => 0, 'paid' => 1, 'uid' => $uid, 'is_gift' => 0])->column('cart_id');
  109. }
  110. /**
  111. * 获取专题订单列表
  112. * @param $type
  113. * @param $page
  114. * @param $limit
  115. * @param $uid
  116. * @return array
  117. * @throws \think\Exception
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. * @throws \think\exception\DbException
  121. */
  122. public static function getSpecialOrderList($type, $page, $limit, $uid)
  123. {
  124. $model = self::where(['a.is_del' => 0, 's.is_del' => 0, 'a.uid' => $uid, 'a.paid' => 1])->order('a.add_time desc')->alias('a')->join('__SPECIAL__ s', 'a.cart_id=s.id');
  125. switch ($type) {
  126. case 1:
  127. $model= $model->where(['a.is_gift' => 1, 'a.combination_id' => 0, 'a.pink_id' => 0,'a.type'=>0]);
  128. break;
  129. case 2:
  130. $model=$model->where(['a.is_gift' => 0, 'a.combination_id' => 0, 'a.pink_id' => 0,'a.type'=>0]);
  131. break;
  132. case 3:
  133. $model= $model->where('a.is_gift', 0)->where('a.type', 0)->where('a.combination_id', 'NEQ', 0)->where('a.pink_id', 'NEQ', 0);
  134. break;
  135. }
  136. $list = $model->field(['a.*', 's.title', 's.image', 's.money', 's.pink_number'])->page($page, $limit)->select();
  137. $list = count($list) ? $list->toArray() : [];
  138. foreach ($list as &$item) {
  139. $item['image'] = get_oss_process($item['image'], 4);
  140. $item['pink'] = [];
  141. $item['stop_time'] = 0;
  142. $item['is_draw'] = false;
  143. if ($type === 3 && $item['pink_id'] != 0) {
  144. if ($pink = StorePink::where('order_id', $item['order_id'])->find()) {
  145. $pink=$pink->toArray();
  146. $item['pink_status'] = $pink['is_refund'] ? 4 : $pink['status'];
  147. $item['stop_time'] = $pink['stop_time'];
  148. $item['pink_id'] = $pink['id'];
  149. StorePink::setPinkIng($pink, $item['uid']);
  150. }
  151. } else if ($type === 1) {
  152. if ($uid = self::where(['gift_order_id' => $item['order_id']])->value('uid')) {
  153. $item['is_draw'] = true;
  154. $userAvatar=User::where('uid', $uid)->field(['nickname', 'avatar'])->find();
  155. if($userAvatar){
  156. $item['gift_user'] = $userAvatar->toArray();
  157. }else{
  158. $item['gift_user'] =['nickname'=>'','avatar'=>''];
  159. }
  160. }
  161. }
  162. }
  163. $page++;
  164. return compact('list', 'page');
  165. }
  166. /**
  167. * 获取礼物领取记录
  168. * @param $order_id 订单号
  169. * @return array|bool
  170. * @throws \think\db\exception\DataNotFoundException
  171. * @throws \think\db\exception\ModelNotFoundException
  172. * @throws \think\exception\DbException
  173. */
  174. public static function getOrderIdGiftReceive($order_id)
  175. {
  176. $is_gift = false;
  177. $user = [];
  178. $order = self::where(['is_del' => 0, 'order_id' => $order_id])->find();
  179. if (!$order) return self::setErrorInfo('订单不存在');
  180. if (!$order->cart_id) return self::setErrorInfo('订单专题不存在!');
  181. $add_time = date('m-d H:i', $order->add_time);
  182. $image = Special::PreWhere()->where(['id' => $order->cart_id])->value('image');
  183. $title = Special::PreWhere()->where(['id' => $order->cart_id])->value('title');
  184. $uid = self::where(['is_del' => 0, 'gift_order_id' => $order->order_id])->value('uid');
  185. if ($uid) {
  186. $is_gift = true;
  187. $user = User::where('uid', $uid)->field(['avatar', 'nickname'])->find();
  188. }
  189. return compact('user', 'is_gift', 'image', 'add_time', 'title');
  190. }
  191. /**
  192. * 获取订单的专题详情信息
  193. * @param $order_id 订单号
  194. * @return bool
  195. * @throws \think\db\exception\DataNotFoundException
  196. * @throws \think\db\exception\ModelNotFoundException
  197. * @throws \think\exception\DbException
  198. */
  199. public static function getOrderIdToSpecial($order_id,$uid)
  200. {
  201. $order = self::where(['is_del' => 0, 'order_id' => $order_id,'uid'=>$uid])->find();
  202. if (!$order) return self::setErrorInfo('订单不存在或给订单不是您的!');
  203. if (!$order->cart_id) return self::setErrorInfo('订单专题不存在!');
  204. $special = Special::PreWhere()->where(['id' => $order->cart_id])->find();
  205. if (!$special) return self::setErrorInfo('赠送的专题已下架,或已被删除!');
  206. $special->abstract = self::HtmlToMbStr($special->abstract);
  207. return $special->toArray();
  208. }
  209. /**
  210. * 创建领取礼物订单
  211. * @param $orderId 订单号
  212. * @param $uid
  213. * @return bool
  214. * @throws \think\db\exception\DataNotFoundException
  215. * @throws \think\db\exception\ModelNotFoundException
  216. * @throws \think\exception\DbException
  217. */
  218. public static function createReceiveGift($orderId, $uid)
  219. {
  220. $order = self::where(['is_del' => 0, 'order_id' => $orderId, 'paid' => 1])->find();
  221. if (!$order) return self::setErrorInfo('赠送的礼物订单不存在');
  222. if ($order->total_num == $order->gift_count) return self::setErrorInfo('礼物已被领取完');
  223. if (SpecialBuy::be(['special_id' => $order['cart_id'], 'uid' => $uid])) return self::setErrorInfo('您已拥有此专题无法,进行领取');
  224. $data = [
  225. 'uid' => $uid,
  226. 'order_id' => self::getNewOrderId(),
  227. 'cart_id' => $order->cart_id,
  228. 'total_num' => 1,
  229. 'total_price' => $order->total_price,
  230. 'gift_count' => 1,
  231. 'pay_price' => 0,
  232. 'paid' => 1,
  233. 'pay_time' => time(),
  234. 'is_receive_gift' => 1,
  235. 'mark' => '礼物领取订单',
  236. 'unique' => md5(time() . '' . $uid . $order->cart_id),
  237. 'cost' => $order->total_price,
  238. 'pay_type' => 'weixin',
  239. 'gift_order_id' => $orderId,
  240. ];
  241. $order->gift_count += 1;
  242. if ($order->save() && ($order = self::set($data))) {
  243. SpecialBuy::setBuySpecial($order['order_id'], $order['uid'], $order['cart_id'], 2);
  244. StoreOrderStatus::status($order['id'], 'cache_key_create_order', '订单生成');
  245. return true;
  246. } else
  247. return self::setErrorInfo('领取礼物订单生成失败');
  248. }
  249. /**
  250. * 创建订单专题订单
  251. * @param $special
  252. * @param $pinkId
  253. * @param $pay_type
  254. * @param $uid
  255. * @param $payType
  256. * @param int $link_pay_uid
  257. * @param int $total_num
  258. * @return bool|object
  259. */
  260. public static function createSpecialOrder($special, $pinkId, $pay_type, $uid, $payType, $link_pay_uid = 0, $total_num = 1)
  261. {
  262. if (!array_key_exists($payType, self::$payType)) return self::setErrorInfo('选择支付方式有误!');
  263. $userInfo = User::getUserInfo($uid);
  264. if (!$userInfo) return self::setErrorInfo('用户不存在!');
  265. $total_price = 0;
  266. $combination_id = 0;
  267. switch ((int)$pay_type) {
  268. case 1:
  269. //送朋友
  270. //$total_price = $special->is_pink ? $special->pink_money : $special->money;
  271. $total_price = $special->money;
  272. if(isset($userInfo['level']) && $userInfo['level'] > 0 && $special->member_pay_type == 1 && $special->member_money > 0){
  273. $total_price = $special->member_money;
  274. }
  275. break;
  276. case 2:
  277. //自己买
  278. $total_price = $special->money;
  279. if(isset($userInfo['level']) && $userInfo['level'] > 0 && $special->member_pay_type == 1 && $special->member_money > 0){
  280. $total_price = $special->member_money;
  281. }
  282. break;
  283. case 3:
  284. if (!$special->is_pink && $special->pink_end_time < date('Y-m-d H:i:s',time())) return self::setErrorInfo('拼团已结束或暂没有开团');
  285. $total_price = $special->pink_money;
  286. $combination_id = $special->id;
  287. break;
  288. }
  289. $orderInfo = [
  290. 'uid' => $uid,
  291. 'order_id' => self::getNewOrderId(),
  292. 'cart_id' => $special->id,
  293. 'total_num' => $total_num,
  294. 'total_price' => $total_price,
  295. 'pay_price' => $total_price,
  296. 'pay_type' => $payType,
  297. 'combination_id' => $combination_id,
  298. 'is_gift' => $pay_type == 1 ? 1 : 0,
  299. 'pink_time' => $pay_type == 3 ? $special->pink_time : 0,
  300. 'paid' => 0,
  301. 'pink_id' => $pinkId,
  302. 'unique' => md5(time() . '' . $uid . $special->id),
  303. 'cost' => $total_price,
  304. 'link_pay_uid' => $userInfo['spread_uid'] ? 0 : $link_pay_uid,
  305. 'spread_uid' => $userInfo['spread_uid'] ? $userInfo['spread_uid'] : 0,
  306. 'is_del' => 0,
  307. ];
  308. $order = self::set($orderInfo);
  309. if (!$order) return self::setErrorInfo('订单生成失败!');
  310. StoreOrderStatus::status($order['id'], 'cache_key_create_order', '订单生成');
  311. return $order;
  312. }
  313. /**
  314. * 创建商品订单
  315. * @param $uid
  316. * @param $key
  317. * @param $addressId
  318. * @param $payType
  319. * @param string $mark
  320. * @return bool|object
  321. * @throws \think\db\exception\DataNotFoundException
  322. * @throws \think\db\exception\ModelNotFoundException
  323. * @throws \think\exception\DbException
  324. */
  325. public static function cacheKeyCreateOrder($uid, $key, $addressId, $payType,$mark = '')
  326. {
  327. if (!array_key_exists($payType, self::$payType)) return self::setErrorInfo('选择支付方式有误!');
  328. if (self::be(['unique' => $key, 'uid' => $uid])) return self::setErrorInfo('请勿重复提交订单');
  329. $userInfo = User::getUserInfo($uid);
  330. if (!$userInfo) return self::setErrorInfo('用户不存在!');
  331. $cartGroup = self::getCacheOrderInfo($uid, $key);
  332. if (!$cartGroup) return self::setErrorInfo('订单已过期,请刷新当前页面!');
  333. $cartInfo = $cartGroup['cartInfo'];
  334. $priceGroup = $cartGroup['priceGroup'];
  335. $payPrice = $priceGroup['totalPrice'];
  336. $payPostage = $priceGroup['storePostage'];
  337. if (!$addressId) return self::setErrorInfo('请选择收货地址!');
  338. if (!UserAddress::be(['uid' => $uid, 'id' => $addressId, 'is_del' => 0]) || !($addressInfo = UserAddress::find($addressId)))
  339. return self::setErrorInfo('地址选择有误!');
  340. $payPrice = bcadd($payPrice,$payPostage,2);
  341. $cartIds = [];
  342. $totalNum = 0;
  343. $giveGoldNum = 0;
  344. foreach ($cartInfo as $cart) {
  345. $cartIds[] = $cart['id'];
  346. $totalNum += $cart['cart_num'];
  347. $giveGoldNum = bcadd($giveGoldNum, $cart['productInfo']['give_gold_num'], 2);
  348. }
  349. $orderInfo = [
  350. 'uid' => $uid,
  351. 'order_id' => self::getNewOrderId(),
  352. 'type'=>2,
  353. 'real_name' => $addressInfo['real_name'],
  354. 'user_phone' => $addressInfo['phone'],
  355. 'user_address' => $addressInfo['province'] . ' ' . $addressInfo['city'] . ' ' . $addressInfo['district'] . ' ' . $addressInfo['detail'],
  356. 'cart_id' => $cartIds,
  357. 'total_num' => $totalNum,
  358. 'total_price' => $priceGroup['totalPrice'],
  359. 'total_postage' => $priceGroup['storePostage'],
  360. 'pay_price' => $payPrice,
  361. 'pay_postage' => $payPostage,
  362. 'paid' => 0,
  363. 'pay_type' => $payType,
  364. 'gain_gold_num' => $giveGoldNum,
  365. 'mark' => htmlspecialchars($mark),
  366. 'cost' => $priceGroup['costPrice'],
  367. 'unique' => $key
  368. ];
  369. $order = self::set($orderInfo);
  370. if (!$order) return self::setErrorInfo('订单生成失败!');
  371. $res5 = true;
  372. foreach ($cartInfo as $cart) {
  373. //减库存加销量
  374. $res5 = $res5 && StoreProduct::decProductStock($cart['cart_num'], $cart['productInfo']['id'], isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  375. }
  376. //保存购物车商品信息
  377. $res4 = false !== StoreOrderCartInfo::setCartInfo($order['id'], $cartInfo);
  378. //购物车状态修改
  379. $res6 = false !== StoreCart::where('id', 'IN', $cartIds)->update(['is_pay' => 1]);
  380. if (!$res4 || !$res5 || !$res6) return self::setErrorInfo('订单生成失败!');
  381. try {
  382. HookService::listen('store_product_order_create', $order, compact('cartInfo', 'addressId'), false, StoreProductBehavior::class);
  383. } catch (\Exception $e) {
  384. return self::setErrorInfo($e->getMessage());
  385. }
  386. self::clearCacheOrderInfo($uid, $key);
  387. self::commitTrans();
  388. StoreOrderStatus::status($order['id'], 'cache_key_create_order', '订单生成');
  389. return $order;
  390. }
  391. /**创建会员订单
  392. * @param $uid
  393. * @param $kid
  394. * @return bool|object
  395. * @throws \think\db\exception\DataNotFoundException
  396. * @throws \think\db\exception\ModelNotFoundException
  397. * @throws \think\exception\DbException
  398. */
  399. public static function cacheMemberCreateOrder($uid, $id,$payType)
  400. {
  401. if (!array_key_exists($payType, self::$payType)) return self::setErrorInfo('选择支付方式有误!');
  402. $userInfo = User::getUserInfo($uid);
  403. if (!$userInfo) return self::setErrorInfo('用户不存在!');
  404. if($userInfo['level'] && $userInfo['is_permanent']) return self::setErrorInfo('您是永久会员,无需续费!');
  405. $member=MemberShip::where('id',$id)->where('is_publish',1)->where('is_del',0)->where('type',1)->find();
  406. $orderInfo = [
  407. 'uid' => $uid,
  408. 'order_id' => self::getNewOrderId(),
  409. 'type'=>1,
  410. 'member_id' => $id,
  411. 'total_num' => 1,
  412. 'total_price' => $member['original_price'],
  413. 'pay_price' => $member['price'],
  414. 'pay_type' => $payType,
  415. 'combination_id' => 0,
  416. 'is_gift' => 0,
  417. 'pink_time' => 0,
  418. 'paid' => 0,
  419. 'pink_id' => 0,
  420. 'unique' => md5(time() . '' . $uid . $id),
  421. 'cost' => $member['original_price'],
  422. 'link_pay_uid' => 0,
  423. 'spread_uid' => $userInfo['spread_uid'] ? $userInfo['spread_uid'] : 0,
  424. 'is_del' => 0,
  425. ];
  426. $order = self::set($orderInfo);
  427. if (!$order) return self::setErrorInfo('订单生成失败!');
  428. StoreOrderStatus::status($order['id'], 'cache_key_create_order', '订单生成');
  429. return $order;
  430. }
  431. public static function getNewOrderId()
  432. {
  433. $count = (int)self::where('add_time', ['>=', strtotime(date("Y-m-d"))], ['<', strtotime(date("Y-m-d", strtotime('+1 day')))])->count();
  434. return 'wx' . date('YmdHis', time()) . (10000 + $count + 1);
  435. }
  436. public static function changeOrderId($orderId)
  437. {
  438. $ymd = substr($orderId, 2, 8);
  439. $key = substr($orderId, 16);
  440. return 'wx' . $ymd . date('His') . $key;
  441. }
  442. /**
  443. * 微信支付 为 0元时 商品
  444. * @param $order_id
  445. * @param $uid
  446. * @return bool
  447. */
  448. public static function jsPayGoodsPrice($order_id, $uid)
  449. {
  450. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('type',2)->where('is_del', 0)->find();
  451. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  452. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  453. $userInfo = User::getUserInfo($uid);
  454. self::beginTrans();
  455. $res1 = UserBill::expend('购买商品', $uid, 'now_money', 'pay_goods', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '微信支付' . floatval($orderInfo['pay_price']) . '元购买商品');
  456. $res2 = self::payGoodsSuccess($order_id);
  457. $res = $res1 && $res2;
  458. self::checkTrans($res);
  459. return $res;
  460. }
  461. /**商品微信
  462. * @param $orderId
  463. * @param string $field
  464. * @return array|string
  465. * @throws \think\db\exception\DataNotFoundException
  466. * @throws \think\db\exception\ModelNotFoundException
  467. * @throws \think\exception\DbException
  468. */
  469. public static function jsPay($orderId, $field = 'order_id')
  470. {
  471. if (is_string($orderId))
  472. $orderInfo = self::where($field, $orderId)->where('type',2)->find();
  473. else
  474. $orderInfo = $orderId;
  475. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  476. if ($orderInfo['paid']) exception('支付已支付!');
  477. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  478. $openid = WechatUser::uidToOpenid($orderInfo['uid']);
  479. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'goods', SystemConfigService::get('site_name'));
  480. }
  481. /**商品余额支付
  482. * @param $order_id
  483. * @param $uid
  484. * @return bool
  485. * @throws \think\db\exception\DataNotFoundException
  486. * @throws \think\db\exception\ModelNotFoundException
  487. * @throws \think\exception\DbException
  488. */
  489. public static function yueGoodsPay($order_id, $uid)
  490. {
  491. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('type',2)->where('is_del', 0)->find();
  492. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  493. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  494. if ($orderInfo['pay_type'] != 'yue') return self::setErrorInfo('该订单不能使用余额支付!');
  495. $userInfo = User::getUserInfo($uid);
  496. if ($userInfo['now_money'] < $orderInfo['pay_price'])
  497. return self::setErrorInfo('余额不足' . floatval($orderInfo['pay_price']));
  498. self::beginTrans();
  499. $res1 = false !== User::bcDec($uid, 'now_money', $orderInfo['pay_price'], 'uid');
  500. $res2 = UserBill::expend('购买商品', $uid, 'now_money', 'pay_goods', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '余额支付' . floatval($orderInfo['pay_price']) . '元购买商品');
  501. $res3 = self::payGoodsSuccess($order_id);
  502. try {
  503. HookService::listen('yue_pay_product', $userInfo, $orderInfo, false, PaymentBehavior::class);
  504. } catch (\Exception $e) {
  505. self::rollbackTrans();
  506. return self::setErrorInfo($e->getMessage());
  507. }
  508. $res = $res1 && $res2 && $res3;
  509. self::checkTrans($res);
  510. return $res;
  511. }
  512. /**专题微信支付
  513. * @param $orderId
  514. * @param string $field
  515. * @return array|string
  516. * @throws \think\db\exception\DataNotFoundException
  517. * @throws \think\db\exception\ModelNotFoundException
  518. * @throws \think\exception\DbException
  519. */
  520. public static function jsSpecialPay($orderId, $field = 'order_id')
  521. {
  522. if (is_string($orderId))
  523. $orderInfo = self::where($field, $orderId)->find();
  524. else
  525. $orderInfo = $orderId;
  526. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  527. if ($orderInfo['paid']) exception('支付已支付!');
  528. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  529. $openid = WechatUser::uidToOpenid($orderInfo['uid']);
  530. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'special', SystemConfigService::get('site_name'));
  531. }
  532. /**专题余额支付
  533. * @param $order_id
  534. * @param $uid
  535. * @return bool
  536. * @throws \think\db\exception\DataNotFoundException
  537. * @throws \think\db\exception\ModelNotFoundException
  538. * @throws \think\exception\DbException
  539. */
  540. public static function yuePay($order_id, $uid)
  541. {
  542. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find();
  543. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  544. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  545. if ($orderInfo['pay_type'] != 'yue') return self::setErrorInfo('该订单不能使用余额支付!');
  546. $userInfo = User::getUserInfo($uid);
  547. if ($userInfo['now_money'] < $orderInfo['pay_price'])
  548. return self::setErrorInfo('余额不足' . floatval($orderInfo['pay_price']));
  549. self::beginTrans();
  550. $res1 = false !== User::bcDec($uid, 'now_money', $orderInfo['pay_price'], 'uid');
  551. $res2 = UserBill::expend('购买专题', $uid, 'now_money', 'pay_product', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '余额支付' . floatval($orderInfo['pay_price']) . '元购买专题');
  552. $res3 = self::paySuccess($order_id);
  553. try {
  554. HookService::listen('yue_pay_product', $userInfo, $orderInfo, false, PaymentBehavior::class);
  555. } catch (\Exception $e) {
  556. self::rollbackTrans();
  557. return self::setErrorInfo($e->getMessage());
  558. }
  559. $res = $res1 && $res2 && $res3;
  560. self::checkTrans($res);
  561. return $res;
  562. }
  563. /**
  564. * 微信支付 为 0元时
  565. * @param $order_id
  566. * @param $uid
  567. * @return bool
  568. */
  569. public static function jsPayPrice($order_id, $uid)
  570. {
  571. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find();
  572. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  573. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  574. $userInfo = User::getUserInfo($uid);
  575. self::beginTrans();
  576. $res1 = UserBill::expend('购买专题', $uid, 'now_money', 'pay_special', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '微信支付' . floatval($orderInfo['pay_price']) . '元购买专题');
  577. $res2 = self::paySuccess($order_id);
  578. $res = $res1 && $res2;
  579. self::checkTrans($res);
  580. return $res;
  581. }
  582. /**
  583. * 微信支付 为 0元时
  584. * @param $order_id
  585. * @param $uid
  586. * @return bool
  587. */
  588. public static function jsPayMePrice($order_id, $uid)
  589. {
  590. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find();
  591. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  592. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  593. $userInfo = User::getUserInfo($uid);
  594. self::beginTrans();
  595. $res1 = UserBill::expend('购买会员', $uid, 'now_money', 'pay_vip', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '微信支付' . floatval($orderInfo['pay_price']) . '元购买会员');
  596. $res2 = self::payMeSuccess($order_id);
  597. $res = $res1 && $res2;
  598. self::checkTrans($res);
  599. return $res;
  600. }
  601. /**会员微信支付
  602. * @param $orderId
  603. * @param string $field
  604. * @return array|string
  605. * @throws \think\db\exception\DataNotFoundException
  606. * @throws \think\db\exception\ModelNotFoundException
  607. * @throws \think\exception\DbException
  608. */
  609. public static function jsPayMember($orderId, $field = 'order_id')
  610. {
  611. if (is_string($orderId))
  612. $orderInfo = self::where($field, $orderId)->find();
  613. else
  614. $orderInfo = $orderId;
  615. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  616. if ($orderInfo['paid']) exception('支付已支付!');
  617. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  618. $openid = WechatUser::uidToOpenid($orderInfo['uid']);
  619. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'member', SystemConfigService::get('site_name'));
  620. }
  621. public static function yueRefundAfter($order)
  622. {
  623. }
  624. /**
  625. * 用户申请退款
  626. * @param $uni
  627. * @param $uid
  628. * @param string $refundReasonWap
  629. * @return bool
  630. */
  631. public static function orderApplyRefund($uni, $uid, $data)
  632. {
  633. $order = self::getUserOrderDetail($uid, $uni);
  634. if (!$order) return self::setErrorInfo('支付订单不存在!');
  635. if ($order['refund_status'] == 2) return self::setErrorInfo('订单已退款!');
  636. if ($order['refund_status'] == 1) return self::setErrorInfo('正在申请退款中!');
  637. if ($order['status'] == 1) return self::setErrorInfo('订单当前无法退款!');
  638. self::beginTrans();
  639. $res1 = false !== StoreOrderStatus::status($order['id'], 'apply_refund', '用户申请退款,原因:' . $data['refund_reason']);
  640. $res2 = false !== self::edit(['refund_status' => 1,'refund_application_time'=> time(),'refund_reason_wap' => $data['refund_reason'],'refund_reason_wap_img'=>json_encode($data['pics']),'mark'=>$data['remarks']], $order['id'], 'id');
  641. $res = $res1 && $res2;
  642. self::checkTrans($res);
  643. if (!$res)
  644. return self::setErrorInfo('申请退款失败!');
  645. else {
  646. return $res;
  647. }
  648. }
  649. /**
  650. * 自动退款
  651. * @param array $order
  652. * */
  653. public static function autoRefundY($order)
  654. {
  655. if (!$order['pink_id']) return true;
  656. $refund_data = [
  657. 'pay_price' => $order['pay_price'],
  658. 'refund_price' => $order['pay_price'],
  659. ];
  660. switch ($order['pay_type']) {
  661. case 'weixin':
  662. if ($order['is_channel']) {
  663. try {
  664. HookService::listen('routine_pay_order_refund', $order['order_id'], $refund_data, true, PaymentBehavior::class);
  665. } catch (\Exception $e) {
  666. return self::setErrorInfo($e->getMessage());
  667. }
  668. } else {
  669. try {
  670. HookService::listen('wechat_pay_order_refund', $order['order_id'], $refund_data, true, PaymentBehavior::class);
  671. } catch (\Exception $e) {
  672. return self::setErrorInfo($e->getMessage());
  673. }
  674. }
  675. break;
  676. case 'yue':
  677. ModelBasic::beginTrans();
  678. $res1 = User::bcInc($order['uid'], 'now_money', $refund_data['pay_price'], 'uid');
  679. $res2 = $res2 = UserBill::income('商品退款', $order['uid'], 'now_money', 'pay_product_refund', $refund_data['pay_price'], $order['id'], $order['pay_price'], '订单退款到余额' . floatval($refund_data['pay_price']) . '元');
  680. try {
  681. HookService::listen('store_order_yue_refund', $order, $refund_data, false, StoreProductBehavior::class);
  682. } catch (\Exception $e) {
  683. ModelBasic::rollbackTrans();
  684. return self::setErrorInfo($e->getMessage());
  685. }
  686. $res = $res1 && $res2;
  687. ModelBasic::checkTrans($res);
  688. if (!$res) return self::setErrorInfo('余额退款失败!');
  689. break;
  690. case 'zhifubao':
  691. AlipayTradeWapService::init()->AliPayRefund($order['order_id'], $order['trade_no'], $order['pay_price'], '拼团失败退款', 'refund');
  692. break;
  693. }
  694. $data = [
  695. 'refund_status' => 2,
  696. 'refund_reason_time' => time(),
  697. 'refund_price' => $order['pay_price'],
  698. 'status' => -1,
  699. ];
  700. self::edit($data, $order['id'], 'id');
  701. StorePink::setRefundPink($order['pink_id']);
  702. HookService::afterListen('store_product_order_refund_y', $data, $order['id'], false, StoreProductBehavior::class);
  703. StoreOrderStatus::status($order['id'], 'refund_price', '自动发起退款,退款给用户' . $order['pay_price'] . '元');
  704. return true;
  705. }
  706. /**
  707. * //TODO 专题支付成功后
  708. * @param $orderId
  709. * @param $notify
  710. * @return bool
  711. */
  712. public static function paySuccess($orderId)
  713. {
  714. $order = self::where('order_id', $orderId)->where('type',0)->find();
  715. if(!$order) return false;
  716. $resPink = true;
  717. User::bcInc($order['uid'], 'pay_count', 1, 'uid');
  718. $res1 = self::where('order_id', $orderId)->where('type',0)->update(['paid' => 1, 'pay_time' => time()]);
  719. if ($order->combination_id && $res1 && !$order->refund_status) {
  720. $resPink = StorePink::createPink($order);//创建拼团
  721. } else {
  722. if (!$order->is_gift) {
  723. //如果是专栏,记录专栏下所有专题购买。
  724. SpecialBuy::setAllBuySpecial($orderId, $order->uid, $order->cart_id);
  725. try {
  726. User::backOrderBrokerage($order);
  727. } catch (\Throwable $e) {
  728. }
  729. }
  730. }
  731. StoreOrderStatus::status($order->id, 'pay_success', '用户付款成功');
  732. $site_url = SystemConfigService::get('site_url');
  733. try{
  734. WechatTemplateService::sendTemplate(WechatUser::where('uid', $order['uid'])->value('openid'), WechatTemplateService::ORDER_PAY_SUCCESS, [
  735. 'first' => '亲,您购买的专题已支付成功',
  736. 'keyword1' => $orderId,
  737. 'keyword2' => $order['pay_price'],
  738. 'remark' => '点击查看订单详情'
  739. ], $site_url . Url::build('wap/special/grade_list'));
  740. WechatTemplateService::sendAdminNoticeTemplate([
  741. 'first' => "亲,您有一个新的课程订单",
  742. 'keyword1' => $orderId,
  743. 'keyword2' => $order['pay_price'],
  744. 'remark' => '请及时查看'
  745. ]);
  746. }catch (\Throwable $e){}
  747. $res = $res1 && $resPink;
  748. return false !== $res;
  749. }
  750. /**
  751. * //TODO 会员支付成功后
  752. * @param $orderId
  753. * @param $notify
  754. * @return bool
  755. */
  756. public static function payMeSuccess($orderId)
  757. {
  758. $order = self::where('order_id', $orderId)->where('type',1)->find();
  759. if(!$order) return false;
  760. $resMer = true;
  761. $res1 = self::where('order_id', $orderId)->where('type',1)->update(['paid' => 1, 'pay_time' => time()]);
  762. $oid = self::where('order_id', $orderId)->where('type',1)->value('id');
  763. $userInfo = User::getUserInfo($order['uid']);
  764. if($order['type'] && $res1 && !$order->refund_status){
  765. $resMer=MemberShip::getUserMember($order,$userInfo);
  766. }
  767. $site_url = SystemConfigService::get('site_url');
  768. try{
  769. WechatTemplateService::sendTemplate(WechatUser::where('uid', $order['uid'])->value('openid'), WechatTemplateService::ORDER_PAY_SUCCESS, [
  770. 'first' => '亲,您充值会员已支付成功',
  771. 'keyword1' => $orderId,
  772. 'keyword2' => $order['pay_price'],
  773. 'remark' => '点击查看会员详情'
  774. ], $site_url . Url::build('wap/special/member_manage'));
  775. WechatTemplateService::sendAdminNoticeTemplate([
  776. 'first' => "亲,您有一个新的会员购买订单",
  777. 'keyword1' => $orderId,
  778. 'keyword2' => $order['pay_price'],
  779. 'remark' => '请及时查看'
  780. ]);
  781. }catch (\Throwable $e){}
  782. StoreOrderStatus::status($oid, 'pay_success', '用户付款成功');
  783. $res = $res1 && $resMer;
  784. return false !== $res;
  785. }
  786. /**
  787. * //TODO 商品支付成功后
  788. * @param $orderId
  789. * @param $notify
  790. * @return bool
  791. */
  792. public static function payGoodsSuccess($orderId)
  793. {
  794. $order = self::where('order_id', $orderId)->where('type',2)->find();
  795. if(!$order) return false;
  796. $resMer = true;
  797. $res1 = self::where('order_id', $orderId)->where('type',2)->update(['paid' => 1,'pay_time' => time()]);
  798. $oid = self::where('order_id', $orderId)->where('type',2)->value('id');
  799. $site_url = SystemConfigService::get('site_url');
  800. try{
  801. WechatTemplateService::sendTemplate(WechatUser::where('uid', $order['uid'])->value('openid'), WechatTemplateService::ORDER_PAY_SUCCESS, [
  802. 'first' => '亲,您购买的商品已支付成功',
  803. 'keyword1' => $orderId,
  804. 'keyword2' => $order['pay_price'],
  805. 'remark' => '点击查看订单详情'
  806. ], $site_url . Url::build('wap/my/order_store_list'));
  807. WechatTemplateService::sendAdminNoticeTemplate([
  808. 'first' => "亲,您有一个新的商品购买订单",
  809. 'keyword1' => $orderId,
  810. 'keyword2' => $order['pay_price'],
  811. 'remark' => '请及时处理'
  812. ]);
  813. }catch (\Throwable $e){}
  814. StoreOrderStatus::status($oid, 'pay_success', '用户付款成功');
  815. $res = $res1 && $resMer;
  816. return false !== $res;
  817. }
  818. /**
  819. * 拼团完成返佣
  820. * @param array $orderAll 订单号
  821. * @return null
  822. * */
  823. public static function PinkRake($orderAll)
  824. {
  825. $list = count($orderAll) ? self::where('order_id', 'in', $orderAll)->where('is_gift', 0)->field(['link_pay_uid', 'total_price', 'uid', 'id', 'spread_uid'])->select() : [];
  826. $list = count($list) ? $list->toArray() : [];
  827. self::startTrans();
  828. try {
  829. self::commitTrans();
  830. } catch (\Exception $e) {
  831. self::rollbackTrans();
  832. }
  833. }
  834. /**
  835. * 计算普通裂变推广人返佣金额
  836. * @param int $is_promoter 推广人级别
  837. * @param float $money 返佣金额
  838. * @return float
  839. * */
  840. public static function getBrokerageMoney($is_promoter, $money)
  841. {
  842. $is_promoter = !is_int($is_promoter) ? (int)$is_promoter : $is_promoter;
  843. $systemName = 'store_brokerage_three_[###]x';
  844. //配置星级字段和设置如: store_brokerage_three_0x store_brokerage_three_1x
  845. //后台设置字段从零星开始 $is_promoter 应 -1 才能对应字段
  846. $store_brokerage_three = $is_promoter ? SystemConfigService::get(str_replace('[###]', $is_promoter - 1, $systemName)) : 100;
  847. //返佣比例为0则不返佣
  848. $store_brokerage_three = $store_brokerage_three == 0 ? 0 : bcdiv($store_brokerage_three, 100, 2);
  849. return bcmul($money, $store_brokerage_three, 2);
  850. }
  851. public static function getUserOrderDetail($uid, $key)
  852. {
  853. return self::where('order_id|unique', $key)->where('type',2)->where('uid', $uid)->where('is_del', 0)->find();
  854. }
  855. /**
  856. * TODO 订单发货
  857. * @param array $postageData 发货信息
  858. * @param string $oid orderID
  859. */
  860. public static function orderPostageAfter($postageData, $oid)
  861. {
  862. $order = self::where('id', $oid)->find();
  863. $openid = WechatUser::uidToOpenid($order['uid']);
  864. $url = Url::build('wap/Special/order', ['uni' => $order['order_id']], true, true);
  865. $group = [
  866. 'first' => '亲,您的订单已发货,请注意查收',
  867. 'remark' => '点击查看订单详情'
  868. ];
  869. if ($postageData['delivery_type'] == 'send') {//送货
  870. $goodsName = StoreOrderCartInfo::getProductNameList($order['id']);
  871. $group = array_merge($group, [
  872. 'keyword1' => $goodsName,
  873. 'keyword2' => $order['pay_type'] == 'offline' ? '线下支付' : date('Y/m/d H:i', $order['pay_time']),
  874. 'keyword3' => $order['user_address'],
  875. 'keyword4' => $postageData['delivery_name'],
  876. 'keyword5' => $postageData['delivery_id']
  877. ]);
  878. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_DELIVER_SUCCESS, $group, $url);
  879. } else if ($postageData['delivery_type'] == 'express') {//发货
  880. $group = array_merge($group, [
  881. 'keyword1' => $order['order_id'],
  882. 'keyword2' => $postageData['delivery_name'],
  883. 'keyword3' => $postageData['delivery_id']
  884. ]);
  885. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_POSTAGE_SUCCESS, $group, $url);
  886. }
  887. }
  888. public static function orderTakeAfter($order)
  889. {
  890. $openid = WechatUser::uidToOpenid($order['uid']);
  891. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_TAKE_SUCCESS, [
  892. 'first' => '亲,您的订单已成功签收!',
  893. 'keyword1' => $order['order_id'],
  894. 'keyword2' => '已收货',
  895. 'keyword3' => date('Y/m/d H:i', time()),
  896. 'keyword4' => implode(',', StoreOrderCartInfo::getProductNameList($order['id'])),
  897. 'remark' => '点击查看订单详情'
  898. ], Url::build('wap/special/order', ['uni' => $order['order_id']], true, true));
  899. }
  900. /**
  901. * 删除订单
  902. * @param $uni
  903. * @param $uid
  904. * @return bool
  905. */
  906. public static function removeOrder($uni, $uid)
  907. {
  908. $order = self::getUserOrderDetail($uid, $uni);
  909. if (!$order) return self::setErrorInfo('订单不存在!');
  910. $order = self::tidyOrder($order);
  911. if ($order['_status']['_type'] != 0 && $order['_status']['_type'] != -2 && $order['_status']['_type'] != 4)
  912. return self::setErrorInfo('该订单无法删除!');
  913. if (false !== self::edit(['is_del' => 1], $order['id'], 'id') &&
  914. false !== StoreOrderStatus::status($order['id'], 'remove_order', '删除订单'))
  915. return true;
  916. else
  917. return self::setErrorInfo('订单删除失败!');
  918. }
  919. /**
  920. * //TODO 用户确认收货
  921. * @param $uni
  922. * @param $uid
  923. */
  924. public static function takeOrder($uni, $uid)
  925. {
  926. $order = self::getUserOrderDetail($uid, $uni);
  927. if (!$order) return self::setErrorInfo('订单不存在!');
  928. $order = self::tidyOrder($order);
  929. if ($order['_status']['_type'] != 2) return self::setErrorInfo('订单状态错误!');
  930. self::beginTrans();
  931. if (false !== self::edit(['status' => 2], $order['id'], 'id') &&
  932. false !== StoreOrderStatus::status($order['id'], 'user_take_delivery', '用户已收货')) {
  933. try {
  934. HookService::listen('store_product_order_user_take_delivery', $order, $uid, false, StoreProductBehavior::class);
  935. } catch (\Exception $e) {
  936. return self::setErrorInfo($e->getMessage());
  937. }
  938. self::commitTrans();
  939. return true;
  940. } else {
  941. self::rollbackTrans();
  942. return false;
  943. }
  944. }
  945. public static function tidyOrder($order, $detail = false,$isPic=false)
  946. {
  947. if ($detail == true && isset($order['id'])) {
  948. $cartInfo = self::getDb('StoreOrderCartInfo')->where('oid', $order['id'])->column('cart_info', 'unique') ?: [];
  949. foreach ($cartInfo as $k => $cart) {
  950. $cartInfo[$k] = json_decode($cart, true);
  951. $cartInfo[$k]['unique'] = $k;
  952. }
  953. $order['cartInfo'] = $cartInfo;
  954. }
  955. $status = [];
  956. if (!$order['paid'] && $order['pay_type'] == 'offline' && !$order['status'] >= 2) {
  957. $status['_type'] = 9;
  958. $status['_title'] = '线下付款';
  959. $status['_msg'] = '等待商家处理,请耐心等待';
  960. $status['_class'] = 'nobuy';
  961. } else if (!$order['paid']) {
  962. $status['_type'] = 0;
  963. $status['_title'] = '未支付';
  964. $status['_msg'] = '立即支付订单吧';
  965. $status['_class'] = 'nobuy';
  966. } else if ($order['refund_status'] == 1) {
  967. $status['_type'] = -1;
  968. $status['_title'] = '申请退款中';
  969. $status['_msg'] = '商家审核中,请耐心等待';
  970. $status['_class'] = 'state-sqtk';
  971. } else if ($order['refund_status'] == 2) {
  972. $status['_type'] = -2;
  973. $status['_title'] = '已退款';
  974. $status['_msg'] = '已为您退款,感谢您的支持';
  975. $status['_class'] = 'state-sqtk';
  976. } else if (!$order['status']) {
  977. if (isset($order['pink_id'])) {
  978. if (StorePink::where('id', $order['pink_id'])->where('status', 1)->count()) {
  979. $status['_type'] = 1;
  980. $status['_title'] = '拼团中';
  981. $status['_msg'] = '等待其他人参加拼团';
  982. $status['_class'] = 'state-nfh';
  983. } else {
  984. $status['_type'] = 1;
  985. $status['_title'] = '未发货';
  986. $status['_msg'] = '商家未发货,请耐心等待';
  987. $status['_class'] = 'state-nfh';
  988. }
  989. } else {
  990. $status['_type'] = 1;
  991. $status['_title'] = '未发货';
  992. $status['_msg'] = '商家未发货,请耐心等待';
  993. $status['_class'] = 'state-nfh';
  994. }
  995. } else if ($order['status'] == 1) {
  996. $status['_type'] = 2;
  997. $status['_title'] = '待收货';
  998. $status['_msg'] = date('m月d日H时i分', StoreOrderStatus::getTime($order['id'], 'delivery_goods')) . '服务商已发货';
  999. $status['_class'] = 'state-ysh';
  1000. } else if ($order['status'] == 2) {
  1001. $status['_type'] = 3;
  1002. $status['_title'] = '交易完成';
  1003. $status['_msg'] = '交易完成,感谢您的支持';
  1004. $status['_class'] = 'state-ytk';
  1005. }
  1006. if($order['refund_reason_time']) $order['refund_reason_time']=date('Y-m-d H:i:s',$order['refund_reason_time']);
  1007. if($order['refund_application_time']) $order['refund_application_time']=date('Y-m-d H:i:s',$order['refund_application_time']);
  1008. if (isset($order['pay_type']))
  1009. $status['_payType'] = isset(self::$payType[$order['pay_type']]) ? self::$payType[$order['pay_type']] : '其他方式';
  1010. if (isset($order['delivery_type']))
  1011. $status['_deliveryType'] = isset(self::$deliveryType[$order['delivery_type']]) ? self::$deliveryType[$order['delivery_type']] : '其他方式';
  1012. $order['_status'] = $status;
  1013. if ($isPic) {
  1014. $order_details_images = GroupDataService::getData('order_details_images') ?: [];
  1015. foreach ($order_details_images as $image) {
  1016. if (isset($image['order_status']) && $image['order_status'] == $order['_status']['_type']) {
  1017. $order['status_pic'] = $image['pic'];
  1018. break;
  1019. }
  1020. }
  1021. }
  1022. return $order;
  1023. }
  1024. public static function statusByWhere($status, $model = null)
  1025. {
  1026. $orderId = StorePink::where('uid', User::getActiveUid())->where('status', 1)->column('order_id', 'id');//获取正在拼团的订单编号
  1027. if ($model == null) $model = new self;
  1028. if ('' === $status)
  1029. return $model;
  1030. else if ($status == 0)
  1031. return $model->where('paid', 0)->where('status', 0)->where('refund_status', 0);
  1032. else if ($status == 1)//待发货
  1033. return $model->where('paid', 1)->where('order_id', 'NOT IN', implode(',', $orderId))->where('status', 0)->where('refund_status', 0);
  1034. else if ($status == 2)
  1035. return $model->where('paid', 1)->where('status', 1)->where('refund_status', 0);
  1036. else if ($status == 3)
  1037. return $model->where('paid', 1)->where('status', 2)->where('refund_status', 0);
  1038. else if ($status == 4)
  1039. return $model->where('paid', 1)->where('refund_status','>', 0);
  1040. else if ($status == -1)
  1041. return $model->where('paid', 1)->where('refund_status', 1);
  1042. else if ($status == -2)
  1043. return $model->where('paid', 1)->where('refund_status', 2);
  1044. else if ($status == 11) {
  1045. return $model->where('order_id', 'IN', implode(',', $orderId));
  1046. } else
  1047. return $model;
  1048. }
  1049. /**商品订单列表
  1050. * @param $uid
  1051. * @param string $status
  1052. * @param int $first
  1053. * @param int $limit
  1054. * @return array
  1055. * @throws \think\db\exception\DataNotFoundException
  1056. * @throws \think\db\exception\ModelNotFoundException
  1057. * @throws \think\exception\DbException
  1058. */
  1059. public static function getUserOrderList($uid, $status = '', $first = 0, $limit = 8)
  1060. {
  1061. $list = self::statusByWhere($status)->where('type', 2)->where('is_del', 0)->where('uid', $uid)
  1062. ->field('is_gift,combination_id,id,order_id,pay_price,total_num,total_price,pay_postage,total_postage,paid,status,refund_status,pay_type,coupon_price,deduction_price,pink_id,delivery_type,refund_reason_time,refund_application_time')
  1063. ->order('add_time DESC')->limit($first, $limit)->select()->toArray();
  1064. foreach ($list as $k => $order) {
  1065. $list[$k] = self::tidyOrder($order, true);
  1066. }
  1067. return $list;
  1068. }
  1069. public static function searchUserOrder($uid, $order_id)
  1070. {
  1071. $order = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->field('is_gift,combination_id,id,order_id,pay_price,total_num,total_price,pay_postage,total_postage,paid,status,refund_status,pay_type,coupon_price,deduction_price,delivery_type')
  1072. ->order('add_time DESC')->find();
  1073. if (!$order)
  1074. return false;
  1075. else
  1076. return self::tidyOrder($order->toArray(), true);
  1077. }
  1078. public static function orderOver($oid)
  1079. {
  1080. $res = self::edit(['status' => '3'], $oid, 'id');
  1081. if (!$res) exception('评价后置操作失败!');
  1082. StoreOrderStatus::status($oid, 'check_order_over', '用户评价');
  1083. }
  1084. public static function checkOrderOver($oid)
  1085. {
  1086. $uniqueList = StoreOrderCartInfo::where('oid', $oid)->column('unique');
  1087. if (StoreProductReply::where('unique', 'IN', $uniqueList)->where('oid', $oid)->count() == count($uniqueList)) {
  1088. HookService::listen('store_product_order_over', $oid, null, false, StoreProductBehavior::class);
  1089. self::orderOver($oid);
  1090. }
  1091. }
  1092. /**
  1093. * 用户订单数据
  1094. */
  1095. public static function getOrderStatusNum($uid)
  1096. {
  1097. $noBuy = self::where('uid', $uid)->where('paid', 0)->where('type', 2)->where('is_del', 0)->where('refund_status', 0)->count();//未支付订单数量
  1098. $noDelivered = self::where('uid', $uid)->where('paid', 1)->where('type', 2)->where('is_del', 0)->where('status', 0)->where('refund_status', 0)->count();//待发货订单数量
  1099. $noTake = self::where('uid', $uid)->where('paid', 1)->where('type', 2)->where('is_del', 0)->where('status', 1)->where('refund_status', 0)->count();//待收货订单数量
  1100. $noReply = self::where('uid', $uid)->where('paid', 1)->where('type', 2)->where('is_del', 0)->where('status', 2)->where('refund_status', 0)->count();//已完成订单数量
  1101. $sum=self::where('uid', $uid)->where('is_del', 0)->where('type', 2)->count();//订单总数
  1102. $sumPrice=self::where('uid', $uid)->where('paid', 1)->where('type', 2)->where('refund_status', 0)->where('is_del', 0)->sum('pay_price');//订单总消费
  1103. $refund = self::where('uid', $uid)->where('paid', 1)->where('type', 2)->where('is_del', 0)->where('refund_status','>',0)->count();//退款订单数量
  1104. return compact('noBuy', 'noDelivered', 'noTake', 'noReply','sum','sumPrice','refund');
  1105. }
  1106. /**购买商品赠送虚拟币
  1107. * @param $order
  1108. * @return bool
  1109. * @throws \Exception
  1110. */
  1111. public static function gainUserGoldNum($order)
  1112. {
  1113. $gold_name=SystemConfigService::get('gold_name');//虚拟币名称
  1114. if ($order['gain_gold_num'] > 0) {
  1115. $userInfo = User::getUserInfo($order['uid']);
  1116. ModelBasic::beginTrans();
  1117. $res1 = false != User::where('uid', $userInfo['uid'])->update(['gold_num' => bcadd($userInfo['gold_num'], $order['gain_gold_num'], 2)]);
  1118. $res2 = false != UserBill::income('购买商品赠送'.$gold_name, $order['uid'], 'gold_num', 'gain', $order['gain_gold_num'], $order['id'], $userInfo['gold_num'], '购买商品赠送' . floatval($order['gain_gold_num']) . $gold_name);
  1119. $res = $res1 && $res2;
  1120. ModelBasic::checkTrans($res);
  1121. return $res;
  1122. }
  1123. return true;
  1124. }
  1125. /**
  1126. * 获取当前订单中有没有拼团存在
  1127. * @param $pid
  1128. * @return int|string
  1129. */
  1130. public static function getIsOrderPink($pid)
  1131. {
  1132. $uid = User::getActiveUid();
  1133. return self::where('uid', $uid)->where('pink_id', $pid)->where('refund_status', 0)->where('is_del', 0)->count();
  1134. }
  1135. /**
  1136. * 获取order_id
  1137. * @param $pid
  1138. * @return mixed
  1139. */
  1140. public static function getStoreIdPink($pid)
  1141. {
  1142. $uid = User::getActiveUid();
  1143. return self::where('uid', $uid)->where('pink_id', $pid)->where('is_del', 0)->value('order_id');
  1144. }
  1145. /**
  1146. * 删除当前用户拼团未支付的订单
  1147. */
  1148. public static function delCombination()
  1149. {
  1150. self::where('combination', 'GT', 0)->where('paid', 0)->where('uid', User::getActiveUid())->delete();
  1151. }
  1152. public static function getPinkT($pink_id)
  1153. {
  1154. $pink = StorePink::getPinkUserOne($pink_id);
  1155. if (isset($pink['is_refund']) && $pink['is_refund']) {
  1156. if ($pink['is_refund'] != $pink['id']) {
  1157. $id = $pink['is_refund'];
  1158. return self::getPinkT($id);
  1159. } else {
  1160. return self::setErrorInfo('订单已退款');
  1161. }
  1162. }
  1163. return $pink;
  1164. }
  1165. public static function getOrderSpecialInfo($orderId, $uid)
  1166. {
  1167. $is_ok = 0;//判断拼团是否完成
  1168. $userBool = 0;//判断当前用户是否在团内 0未在 1在
  1169. $pinkBool = 0;//判断当前用户是否在团内 0未在 1在
  1170. $pink_id = self::where('order_id', $orderId)->value('pink_id');
  1171. $pink = self::getPinkT($pink_id);
  1172. if (!$pink) return self::setErrorInfo('没有查到拼团信息');
  1173. if ($pink['is_refund']) return self::setErrorInfo('订单已退款,无法查看');
  1174. list($pinkAll, $pinkT, $count, $idAll, $uidAll) = StorePink::getPinkMemberAndPinkK($pink);
  1175. if ($pinkT['status'] == 2) {
  1176. $pinkBool = 1;
  1177. } else {
  1178. if (!$count) {//组团完成
  1179. $pinkBool = StorePink::PinkComplete($uidAll, $idAll, $uid, $pinkT);
  1180. } else {//拼团失败 退款
  1181. $pinkBool = StorePink::PinkFail($uid, $idAll, $pinkAll, $pinkT, (int)$count, $pinkBool, $uidAll);
  1182. }
  1183. }
  1184. if (!empty($pinkAll)) {
  1185. foreach ($pinkAll as $v) {
  1186. if ($v['uid'] == $uid) $userBool = 1;
  1187. }
  1188. }
  1189. if ($pinkT['uid'] == $uid) $userBool = 1;
  1190. $data['pinkBool'] = $pinkBool;
  1191. $data['is_ok'] = $is_ok;
  1192. $data['userBool'] = $userBool;
  1193. $data['pinkT'] = $pinkT;
  1194. $data['pinkAll'] = $pinkAll;
  1195. $data['count'] = $count;
  1196. $data['current_pink_order'] = StorePink::getCurrentPink($pink_id);
  1197. $data['special'] = Special::PreWhere()->where('id', self::where('order_id', $pinkT['order_id'])->value('cart_id'))->field(['id', 'image', 'title', 'money'])->find();
  1198. if (!$data['special']) return self::setErrorInfo('专题未查找到');
  1199. $data['special_id'] = $data['special']['id'];
  1200. return $data;
  1201. }
  1202. }