OrderController.php 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Api\Repositories\PostsRepositores;
  4. use App\Http\Controllers\Api\Repositories\WxCircleRepositores;
  5. use App\Http\Controllers\Api\Repositories\WxCommentRepositores;
  6. use App\Http\Controllers\Api\Repositories\WxUserRepositores;
  7. use App\Jobs\Order\OrderDeliverInfoManage;
  8. use App\Jobs\Shop\ShopOrderDeliverInfoManage;
  9. use App\Models\Posts\WxPost;
  10. use App\Models\Circle\WxCircle;
  11. use App\Models\Posts\WxPostsPayContent;
  12. use App\Models\Posts\WxPostsWeightRefresh;
  13. use App\Models\Shop\WxShopAddress;
  14. use App\Models\Shop\WxShopCart;
  15. use App\Models\Shop\WxShopContact;
  16. use App\Models\Shop\WxShopGoods;
  17. use App\Models\Shop\WxShopGoodsProduct;
  18. use App\Models\Shop\WxShopOrder;
  19. use App\Models\Shop\WxShopOrderGoods;
  20. use App\Models\Used\WxUsedGood;
  21. use App\Models\User\WxUserContactApply;
  22. use App\Models\User\WxUserPay;
  23. use App\Models\User\WxUserSetting;
  24. use App\Models\WxDomainAuth;
  25. use App\Wen\Utils\OrderUtils;
  26. use App\Wen\Utils\Settings;
  27. use App\Wen\Utils\ShopUtils;
  28. use App\Wen\Utils\StrUtils;
  29. use App\Wen\Utils\UserUtils;
  30. use App\Wen\Utils\Utils;
  31. use App\Http\Controllers\Api\Traits\PayTrait;
  32. use App\Models\WxOrder;
  33. use App\Models\User\WxUser;
  34. use Carbon\Carbon;
  35. use Illuminate\Http\Request;
  36. use Illuminate\Support\Facades\DB;
  37. class OrderController extends BaseController
  38. {
  39. use PayTrait;
  40. public function coin_pay(Request $request){
  41. DB::beginTransaction();
  42. try {
  43. // todo:
  44. $uid = $request->uid;
  45. $order = $request->order;
  46. if(_array_key($order, 'type', 0) == 1){
  47. // 商品兑换
  48. if(_empty_(_array_key($order, 'cart_id', 0))){
  49. DB::rollBack();
  50. return $this->fail(200001);
  51. }
  52. $aid = $order['aid'];
  53. $cart_id = $order['cart_id'];
  54. $user_remark = $order['user_remark'];
  55. $CartItem = WxShopCart::find($cart_id);
  56. if(_empty_($CartItem)){
  57. DB::rollBack();
  58. return $this->fail(200004, [], '数据被清除,请重新下单再试');
  59. }
  60. $product_id = $CartItem->product_id;
  61. $WxShopGoodsProduct = WxShopGoodsProduct::find($product_id);
  62. if(_empty_($WxShopGoodsProduct)){
  63. DB::rollBack();
  64. return $this->fail(200004, [], '规格数据被清除,请重新下单再试');
  65. }
  66. $num = $CartItem->num;
  67. $goods_id = $CartItem->goods_id;
  68. $WxShopGoods = WxShopGoods::find($goods_id);
  69. if(_empty_($WxShopGoods)){
  70. DB::rollBack();
  71. return $this->fail(200004, [], '商品不存在,请重新下单再试');
  72. }
  73. if($WxShopGoodsProduct->credit_type === 0){
  74. $price = (int)$WxShopGoodsProduct->price;
  75. $vip_price = (int)$WxShopGoodsProduct->vip_price;
  76. $price_total = $price * $num;
  77. $vip_price_total = $vip_price * $num;
  78. $buy_user = WxUser::where('id', $uid)->first();
  79. $is_vip = $buy_user->is_member;
  80. $order_total = $is_vip ? $vip_price_total : $price_total;
  81. $has_coins = get_user_meta($uid, 'coins', 'n');
  82. if(_empty_($has_coins)){
  83. $has_coins = 0;
  84. }
  85. if($has_coins < _abs($order_total)){
  86. DB::rollBack();
  87. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  88. }
  89. // todo:
  90. $res = UserUtils::update_user_coins($uid, 23, -$order_total, '兑换了商品(id:'.$goods_id.' 规格:'.$WxShopGoodsProduct->param_value.' * '.$num.'),'.Settings::get('app_coin_name', '硬币') .'-'.$order_total );
  91. if($res) {
  92. $NotifyController = new NotifyController();
  93. $orderSn = Utils::getSn(1);//生成订单号
  94. if($WxShopGoods->type == 5){
  95. $addsinfo = [
  96. 'id' => 0,
  97. 'name' => '',
  98. 'mobile' => '',
  99. 'province' => '',
  100. 'city' => '',
  101. 'county' => '',
  102. 'adds' => '',
  103. ];
  104. }else{
  105. $addsinfo = WxShopAddress::where('user_id', $uid)->where('id', $aid)->first();//获取配送地址
  106. if (!$addsinfo) {
  107. DB::rollBack();
  108. return $this->fail(800003);
  109. }
  110. }
  111. //生成SHOP订单
  112. $model = new WxShopOrder();
  113. $model->user_id = $uid;
  114. $model->order_id = $orderSn;//订单号
  115. $model->goods_amount = 0;//商品总价
  116. $model->discounts_amount = 0;//优惠金额
  117. $model->order_amount = 0;//订单总额
  118. $model->goods_amount_coin = $price_total;//商品总价
  119. $model->discounts_amount_coin = $is_vip ? ($price_total - $vip_price_total) : 0;//优惠金额
  120. $model->order_amount_coin = $order_total;//订单总额
  121. $model->adds_id = $addsinfo['id'];//收货地址id
  122. $model->adds_name = $addsinfo['name'];//收货人姓名
  123. $model->adds_mobile = $addsinfo['mobile'];//收货人电话
  124. $model->address = $addsinfo['province'] . $addsinfo['city'] . $addsinfo['county'] . $addsinfo['adds'];//收货详细地址
  125. $model->contact_id = $WxShopGoods->contact_id ?: 0;
  126. $model->user_remark = $user_remark;//用户备注
  127. $model->pay_status = 1;//支付状态 1=未付款 2=已付款 3=已退款
  128. $model->status = 0;//发货状态 0=正常 1=未发货 2=已发货 3=确认收货 4=已退货 5=取消
  129. $model->order_goods_id = [ $WxShopGoodsProduct->id ];
  130. $model->save();
  131. $orderId = $model->id;//获取订单id
  132. //
  133. //批量添加订单商品
  134. $orderGoods = [];
  135. $the_product = $WxShopGoodsProduct;
  136. // 规格存在并且有存量时
  137. if ($the_product) {
  138. $k = 0;
  139. $WxShopGoods = WxShopGoods::where('id', $the_product->goods_id)->first();
  140. $good_servers = $WxShopGoods->service_id;
  141. if (_empty_($good_servers)) {
  142. $good_servers = '[]';
  143. }
  144. $created_at = date('Y-m-d H:i:s', time());
  145. $good_servers = json_decode($good_servers, true);
  146. $refund_deadline = Carbon::now()->addDays(7);
  147. if (in_array(1, $good_servers)) {
  148. $refund_deadline = Carbon::now();
  149. $created_at = $refund_deadline;
  150. }
  151. $recharge = $is_vip ? $the_product->vip_price * $num : $the_product->price * $num;
  152. $orderGoods[$k]['pic'] = $the_product->pic;
  153. $orderGoods[$k]['name'] = $WxShopGoods->name;
  154. $orderGoods[$k]['product'] = $the_product->param_value;
  155. $orderGoods[$k]['vip_price'] = $the_product->vip_price;
  156. $orderGoods[$k]['price'] = $the_product->price;
  157. $orderGoods[$k]['credit_type'] = $the_product->credit_type;
  158. $orderGoods[$k]['recharge'] = $recharge;
  159. $orderGoods[$k]['quantity'] = $num;
  160. $orderGoods[$k]['order_id'] = $orderId;
  161. $orderGoods[$k]['seller_user_id'] = $WxShopGoods->user_id ?: 0;
  162. $orderGoods[$k]['buyer_user_id'] = $uid;
  163. $orderGoods[$k]['goods_id'] = $the_product->goods_id;
  164. $orderGoods[$k]['goods_type'] = $WxShopGoods->type;
  165. $orderGoods[$k]['product_id'] = $the_product->id;
  166. $orderGoods[$k]['created_at'] = $created_at;
  167. $orderGoods[$k]['refund_deadline'] = $refund_deadline;
  168. }
  169. $ogModel = new WxShopOrderGoods();
  170. $ogModel->addAll($orderGoods);
  171. //修改购物车数据
  172. if (_array_key($order, 'cart_id', 0) > 0) {
  173. WxShopCart::where('id', $order['cart_id'])->update(['state' => 2]);
  174. }
  175. WxShopOrder::where('id', $orderId)
  176. ->update(['pay_status' => 2, 'status' => 1, 'coins_num' => 0, 'coins_pay' => 0, 'pay_way' => ShopUtils::generate_payway_text(0, $price_total - $vip_price_total, 0, 0, 0, $order_total)]);
  177. // 理论上闲置商品不可以金币购买
  178. $NotifyController->usedGoodPaied($orderId);
  179. ShopUtils::paied_content_process($orderId);
  180. // 理论上不需要
  181. ShopUtils::split_shop_order_by_seller($orderId);
  182. ShopUtils::split_shop_order_by_type($orderId);
  183. ShopUtils::order_buys_and_stock($orderId);
  184. }
  185. }else{
  186. return $this->fail(200004, [], '该商品不是兑换商品');
  187. }
  188. }else if(_array_key($order, 'type', 0) == 6){
  189. }
  190. DB::commit();
  191. return $this->success();
  192. } catch (\Exception $e) {
  193. DB::rollBack();
  194. _logger_(__file__, __line__, $e->getMessage());
  195. return $this->fail(200006, [], $e->getMessage());
  196. }
  197. }
  198. public function balance_pay(Request $request){
  199. DB::beginTransaction();
  200. try {
  201. $uid = $request->uid;
  202. $order = $request->order;
  203. $paycode = $request->paycode;
  204. global $__MINI_GLOBAL_TENANT_ID__;
  205. if(_empty_($paycode)){
  206. return $this->fail(200004, [], '请先输入支付密码');
  207. }
  208. if(is_array($paycode) && str_replace(',', '', implode(',', $paycode)) !== get_user_meta($uid, 'paycode', 's')){
  209. return $this->fail(200043, [
  210. 'title' => '支付密码错误',
  211. 'content' => '已忘记,前往修改?',
  212. 'confirmText' => '去修改',
  213. 'target_type' => 6,
  214. 'target_id' => '/pagesA/mine/paycode/forget'
  215. ], '支付密码错误');
  216. }
  217. if(is_string($paycode) && $paycode !== get_user_meta($uid, 'paycode', 's')){
  218. return $this->fail(200004, [], '支付密码不匹配');
  219. }
  220. $user_coins = UserUtils::user_coins($uid);
  221. if( _array_key($order, 'coins_num', 0) <= 0){
  222. $order['coins_num'] = 0;
  223. }else if($user_coins < $order['coins_num']){
  224. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  225. }
  226. $buy_user = WxUser::where('id', $uid)->first();
  227. $is_vip = $buy_user->is_member;
  228. $balance = UserUtils::user_balance($uid);
  229. $orderSn = Utils::getSn(1);
  230. $coin_rate = Settings::get('app_coin_pay_rate', 100);
  231. $coin_blance = round($order['coins_num'] / $coin_rate, 2);
  232. if($coin_blance < 0.01){
  233. $coin_blance = 0;
  234. $order['coins_num'] = 0;
  235. }
  236. // 开会员
  237. if (_array_key($order, 'type', 0) == 1) {
  238. // 修改使用金币的数量
  239. if($order['price'] <= $coin_blance){
  240. $coin_blance = $order['price'];
  241. $order['coins_num'] = (int)($coin_blance * $coin_rate);
  242. }
  243. if($order['coins_num'] > 0){
  244. $coin_pay_percent_every = _between_(Settings::get('app_coin_pay_percent_every', 30), 0, 100);
  245. if($coin_pay_percent_every > 0){
  246. if( ($order['price'] * $coin_pay_percent_every / 100) < $coin_blance ){
  247. DB::rollBack();
  248. return $this->fail(200004, [], Settings::get('app_coin_name', '硬币').'抵扣不能超过'.$coin_pay_percent_every.'%');
  249. }
  250. }
  251. }
  252. if(_empty_(_array_key($order, 'price', 0)) || _empty_(_array_key($order, 'days', 0))){
  253. return $this->fail(200004);
  254. }
  255. if($balance - $order['price'] + $coin_blance < 0){
  256. return $this->fail(200012);
  257. }
  258. $vip_options = json_decode(Settings::get('app_vip_options', '[]'), true);
  259. if(_empty_($vip_options)){
  260. return $this->fail(200004, [], '管理员暂未配置会员套餐');
  261. }
  262. $flag = false;
  263. $coins = 0;
  264. foreach ($vip_options as $option){
  265. if($option && $option['days'] == $order['days'] && $option['price'] == $order['price']){
  266. $flag = true;
  267. $coins = $option['coins'];
  268. break;
  269. }
  270. }
  271. if($flag){
  272. $orderModel = $this->createOrder($uid,1,'开通会员',$order['price'],$order['price'],$orderSn);
  273. if($order['coins_num'] > 0){
  274. $need_pay_price = $order['price'] - $coin_blance;
  275. if($need_pay_price < 0.01){
  276. // 金币直接结清
  277. $pay_res2 = UserUtils::update_user_coins($uid, 3, -$order['coins_num'], '开通'.$order['days'].'天会员('. $order['coins_num']. Settings::get('app_coin_name', '硬币') . '抵扣了¥'.$coin_blance.'):金币-'.$order['coins_num']);
  278. if($pay_res2){
  279. WxUserRepositores::vip($uid, $order['days']);
  280. if($coins > 0){
  281. UserUtils::update_user_coins($uid, 13, $coins, '开通'.$order['days'].'天会员,赠送'.$coins.Settings::get('app_coin_name', '硬币'));
  282. }
  283. // 上级奖励
  284. }else{
  285. DB::rollBack();
  286. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  287. }
  288. }else{
  289. $pay_res = UserUtils::update_user_financial($uid, 11, $need_pay_price, '您花费了¥'.$need_pay_price.'余额+'.$order['coins_num'].Settings::get('app_coin_name', '硬币').'开通了'.$order['days'].'天会员');
  290. if($pay_res){
  291. $pay_res2 = UserUtils::update_user_coins($uid, 3, -$order['coins_num'], '开通'.$order['days'].'天会员('. $order['coins_num'].Settings::get('app_coin_name', '硬币').'抵扣了¥'.$coin_blance.'):'. Settings::get('app_coin_name', '硬币') .'-'.$order['coins_num']);
  292. if($pay_res2){
  293. WxUserRepositores::vip($uid, $order['days']);
  294. if($coins > 0){
  295. UserUtils::update_user_coins($uid,13, $coins, '开通'.$order['days'].'天会员,赠送'.$coins.Settings::get('app_coin_name', '硬币'));
  296. }
  297. }else{
  298. DB::rollBack();
  299. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  300. }
  301. }else{
  302. DB::rollBack();
  303. return $this->fail(200012);
  304. }
  305. }
  306. }else{
  307. $pay_res = UserUtils::update_user_financial($uid, 11, $order['price'], '您花费了¥'.$order['price'].'余额开通了'.$order['days'].'天会员');
  308. if($pay_res){
  309. WxUserRepositores::vip($uid, $order['days']);
  310. if($coins > 0){
  311. UserUtils::update_user_coins($uid,13, $coins, '开通'.$order['days'].'天会员,赠送'.$coins.Settings::get('app_coin_name', '硬币'));
  312. }
  313. }else{
  314. DB::rollBack();
  315. return $this->fail(200012);
  316. }
  317. }
  318. }else{
  319. return $this->fail(200004, [], '管理员暂未配置¥'.$option['price'].'的'.$order['days'].'天套餐');
  320. }
  321. WxOrder::where('order_number', $orderSn)->update(['order_state' => 1]);
  322. // 创建订单
  323. DB::commit();
  324. return $this->success(['tip'=>'恭喜,会员天数增加'.$order['days'].'天']);
  325. }
  326. // 充电
  327. else if (_array_key($order, 'type', 0) == 2) {
  328. if(_array_key($order, 'rewardPrice', 0) <= 0){
  329. return $this->fail(200004);
  330. }
  331. $order['postsId'] = _abs($order['postsId']);
  332. // 修改使用金币的数量
  333. if($order['rewardPrice'] <= $coin_blance){
  334. $coin_blance = $order['rewardPrice'];
  335. $order['coins_num'] = (int)($coin_blance * $coin_rate);
  336. }
  337. if($order['coins_num'] > 0){
  338. $coin_pay_percent_every = _between_(Settings::get('app_coin_pay_percent_every', 30), 0, 100);
  339. if($coin_pay_percent_every > 0){
  340. if( ($order['rewardPrice'] * $coin_pay_percent_every / 100) < $coin_blance ){
  341. DB::rollBack();
  342. return $this->fail(200004, [], Settings::get('app_coin_name', '硬币').'抵扣不能超过'.$coin_pay_percent_every.'%');
  343. }
  344. }
  345. }
  346. if($balance - $order['rewardPrice'] + $coin_blance < 0){
  347. return $this->fail(200012);
  348. }
  349. $parame = serialize($order);
  350. // 创建订单
  351. $orderModel = $this->createOrder($uid,2,'充电',$order['rewardPrice'],$order['rewardPrice'],$orderSn,$parame);
  352. if($order['coins_num'] > 0){
  353. // 仅金币支付
  354. $need_pay_price = $order['rewardPrice'] - $coin_blance;
  355. if($need_pay_price < 0.01){
  356. $pay_res2 = UserUtils::update_user_coins($uid,3, -$order['coins_num'], '充电了内容(id: '.$order['postsId'].')¥'.$order['rewardPrice'].'('. $order['coins_num'].Settings::get('app_coin_name', '硬币').'抵扣了¥'.$coin_blance.'):金币-'.$order['coins_num']);
  357. if($pay_res2){
  358. // 创建订单
  359. OrderUtils::exceptional($orderModel);
  360. }else{
  361. DB::rollBack();
  362. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  363. }
  364. }else{
  365. // 金币+余额支付
  366. $pay_res = UserUtils::update_user_financial($uid, 10, $need_pay_price, '您通过余额付费(消耗'.$order['coins_num'].Settings::get('app_coin_name', '硬币').'抵扣了'.$coin_blance.')充电了「<a href="/pages/sticky/sticky?id='.$order['postsId'].'">笔记(id:'.$order['postsId'].')</a>」,充电金额¥'.$order['rewardPrice'], $order['postsId']);
  367. // 处理通知
  368. if($pay_res){
  369. $pay_res2 = UserUtils::update_user_coins($uid,3, -$order['coins_num'], '充电了内容(id: '.$order['postsId'].')¥'.$order['rewardPrice'].'('. $order['coins_num'].Settings::get('app_coin_name', '硬币').'抵扣了¥'.$coin_blance.'):'.Settings::get('app_coin_name', '硬币').'-'.$order['coins_num']);
  370. if($pay_res2){
  371. // 创建订单
  372. OrderUtils::exceptional($orderModel);
  373. }else{
  374. DB::rollBack();
  375. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  376. }
  377. }else{
  378. DB::rollBack();
  379. return $this->fail(200012);
  380. }
  381. }
  382. }else{
  383. // 余额支付
  384. $pay_res = UserUtils::update_user_financial($uid, 10, $order['rewardPrice'], '您通过余额付费充电了「<a href="/pages/sticky/sticky?id='.$order['postsId'].'">笔记(id:'.$order['postsId'].')</a>」,充电金额¥'.$order['rewardPrice'], $order['postsId']);
  385. // 处理通知
  386. if($pay_res){
  387. OrderUtils::exceptional($orderModel);
  388. }else{
  389. DB::rollBack();
  390. return $this->fail(200012);
  391. }
  392. }
  393. WxOrder::where('order_number', $orderSn)->update(['order_state' => 1]);
  394. DB::commit();
  395. return $this->success(['tip'=>'充电成功!']);
  396. }
  397. else if (_array_key($order, 'type', 0) == 4) {
  398. // 购买付费
  399. if(_empty_(_array_key($order, 'post_id', 0)) || _empty_(_array_key($order, 'scene', 0))){
  400. return $this->fail(200004);
  401. }
  402. $res = $this->processPayPost($uid, $order['post_id'], $order['scene'], $orderSn, $order['coins_num'], $coin_blance, $coin_rate);
  403. if($res['code'] == 1){
  404. DB::commit();
  405. return $this->success($res['data']);
  406. }else{
  407. DB::rollBack();
  408. return $this->fail($res['code'], [], $res['tip']);
  409. }
  410. }
  411. else if (_array_key($order, 'type', 0) == 5) {
  412. if(!isset($order['goods_amount']) || !isset($order['discounts_amount']) || !isset($order['order_amount']) || !isset($order['order_goods'])){
  413. return $this->fail(200004);
  414. }
  415. $goods_amount = $order['goods_amount'];
  416. $discounts_amount = $order['discounts_amount'];
  417. $order_amount = $order['order_amount'];
  418. $order_goods = $order['order_goods'];
  419. $user_remark = $order['user_remark'];
  420. $aid = $order['aid'];
  421. $seller_user_id = null;
  422. // 0:电商 6:二手
  423. $order_from = 0;
  424. $contact_id = 0;
  425. $order_goods_id = [];
  426. $notify_controller = new NotifyController();
  427. $acculate_amount = 0;
  428. $acculate_vip_amount = 0;
  429. foreach ($order_goods as $order_good){
  430. if($order_good['goods_type'] == 6){
  431. // 闲置
  432. $the_used_good = WxUsedGood::where('id', $order_good['goods_id'])->where('status', 1)->first();
  433. if($the_used_good){
  434. $order_from = 6;
  435. $seller_user_id = $the_used_good->user_id;
  436. $order_goods_id[] = $the_used_good->id;
  437. if($the_used_good->price != $order_good['price']){
  438. DB::rollBack();
  439. return $this->fail(200004, [], '参数1检验失败,疑似参数被纂改');
  440. }else{
  441. $acculate_amount += ($the_used_good->price * 1);
  442. $acculate_vip_amount += ($the_used_good->price * 1);
  443. }
  444. }else{
  445. DB::rollBack();
  446. return $this->fail(200003, [], '订单中存在已售出的闲置商品');
  447. }
  448. }else{
  449. $the_product = WxShopGoodsProduct::where([['id', '=', $order_good['product_id']],['goods_id','=', $order_good['goods_id']],['state', '=', 0]])->first();
  450. if($the_product){
  451. $order_goods_id[] = $the_product->goods_id;
  452. if($order_good['goods_type'] == 5){
  453. $WxShopGoods = WxShopGoods::where('id', $the_product->goods_id)->first();
  454. $contact_id = $WxShopGoods->contact_id;
  455. }
  456. if($the_product->price != $order_good['price'] || $the_product->vip_price != $order_good['vip_price']){
  457. DB::rollBack();
  458. return $this->fail(200004, [], '参数1检验失败,疑似参数被纂改');
  459. }else{
  460. $product_seller = WxShopGoods::where('id', $the_product->goods_id)->value('user_id');
  461. if(_empty_($seller_user_id)){
  462. $seller_user_id = $product_seller;
  463. }else{
  464. if($seller_user_id == $product_seller){
  465. }else{
  466. $seller_user_id = null;
  467. }
  468. }
  469. $acculate_amount += ($the_product->price * $order_good['num']);
  470. $acculate_vip_amount += ($the_product->vip_price * $order_good['num']);
  471. }
  472. }else{
  473. DB::rollBack();
  474. return $this->fail(200003);
  475. }
  476. }
  477. }
  478. $acculate_order_amount = $is_vip ? $acculate_vip_amount : $acculate_amount;
  479. if(_abs($acculate_order_amount - $order_amount) >= 0.5 || _abs(($acculate_amount - $acculate_order_amount) - $discounts_amount) >= 0.5){
  480. DB::rollBack();
  481. return $this->fail(200004, [], '参数2检验失败,疑似参数被纂改');
  482. }
  483. // 纠正前端数据
  484. $acculate_amount = round($acculate_amount, 2);
  485. $acculate_vip_amount = round($acculate_vip_amount, 2);
  486. $goods_amount = $acculate_amount;
  487. $discounts_amount = $is_vip ? round($acculate_amount - $acculate_vip_amount, 2) : 0;
  488. $order_amount = $is_vip ? $acculate_vip_amount : $acculate_amount;
  489. // 纠正使用金币的数量
  490. if($order_amount <= $coin_blance){
  491. $coin_blance = $order_amount;
  492. $order['coins_num'] = (int)($coin_blance * $coin_rate);
  493. }
  494. if( $order['coins_num'] > 0 ){
  495. $coin_pay_percent_every = _between_(Settings::get('app_coin_pay_percent_every', 30), 0, 100);
  496. if($coin_pay_percent_every > 0){
  497. if( ($order_amount * $coin_pay_percent_every / 100) < $coin_blance ){
  498. DB::rollBack();
  499. return $this->fail(200004, [], Settings::get('app_coin_name', '硬币').'抵扣不能超过'.$coin_pay_percent_every.'%');
  500. }
  501. }
  502. }
  503. if($balance - $order_amount + $coin_blance < 0){
  504. return $this->fail(200012);
  505. }
  506. $orderSn = Utils::getSn(1);//生成订单号
  507. if($contact_id > 0 && _empty_($aid)){
  508. $addsinfo = [
  509. 'id' => 0,
  510. 'name' => '',
  511. 'mobile' => '',
  512. 'province' => '',
  513. 'city' => '',
  514. 'county' => '',
  515. 'adds' => '',
  516. ];
  517. }else{
  518. $addsinfo = WxShopAddress::where('user_id', $uid)->where('id', $aid)->first();//获取配送地址
  519. if(!$addsinfo){
  520. DB::rollBack();
  521. return $this->fail(800003);
  522. }
  523. }
  524. //生成SHOP订单
  525. $order_model = new WxShopOrder();
  526. $order_model->user_id = $uid;
  527. $order_model->order_id = $orderSn;//订单号
  528. $order_model->goods_amount = $goods_amount;//商品总价
  529. $order_model->discounts_amount = $discounts_amount;//优惠金额
  530. $order_model->order_amount = $order_amount;//订单总额
  531. $order_model->adds_id = $addsinfo['id'];//收货地址id
  532. $order_model->adds_name = $addsinfo['name'];//收货人姓名
  533. $order_model->adds_mobile = $addsinfo['mobile'];//收货人电话
  534. $order_model->address = $addsinfo['province'] . $addsinfo['city'] . $addsinfo['county'] . $addsinfo['adds'];//收货详细地址
  535. $order_model->contact_id = $contact_id;
  536. $order_model->user_remark = $user_remark;//用户备注
  537. $order_model->pay_status = 1;//支付状态 1=未付款 2=已付款 3=已退款
  538. $order_model->status = 0;//发货状态 0=正常 1=未发货 2=已发货 3=确认收货 4=已退货 5=取消 6=用户删除 7=退款中
  539. $order_model->order_from = $order_from;
  540. $order_model->order_goods_id = $order_goods_id;
  541. if($seller_user_id > 0){
  542. // 单个的商户则直接补充 seller_user_id, 多个商户由于不确定是哪个商户,直接为null,拆订单的时候再补充
  543. $order_model->seller_user_id = $seller_user_id;
  544. }
  545. $order_model->save();
  546. $orderId = $order_model->id;//获取订单id
  547. //
  548. $_goods_amount = 0;
  549. $_order_amount = 0;
  550. //批量添加订单商品
  551. $orderGoods = [];
  552. foreach ($order_goods as $k => $v) {
  553. if($v['goods_type'] == 6){
  554. $the_used_good = WxUsedGood::where('id', $v['goods_id'])->where('status', 1)->first();
  555. if ($the_used_good) {
  556. if($the_used_good->image_urls){
  557. $the_used_good->pic = _array_key($the_used_good->image_urls[0], 'url', '') . '';
  558. }
  559. $created_at = date('Y-m-d H:i:s', time());
  560. $good_servers = [2];
  561. $refund_deadline = Carbon::now()->addDays(7);
  562. if(in_array(1, $good_servers)){
  563. $refund_deadline = Carbon::now();
  564. $created_at = $refund_deadline;
  565. }
  566. $recharge = $the_used_good->price * 1;
  567. $_order_amount += $recharge;
  568. $_goods_amount += $the_used_good->price * 1;
  569. $orderGoods[$k]['pic'] = $the_used_good->pic;
  570. $orderGoods[$k]['name'] = $the_used_good->title;
  571. $orderGoods[$k]['product'] = '';
  572. $orderGoods[$k]['vip_price'] = $the_used_good->price;
  573. $orderGoods[$k]['price'] = $the_used_good->price;
  574. $orderGoods[$k]['recharge'] = $recharge;
  575. $orderGoods[$k]['quantity'] = 1;
  576. $orderGoods[$k]['order_id'] = $orderId;
  577. $orderGoods[$k]['seller_user_id'] = $the_used_good->user_id;
  578. $orderGoods[$k]['buyer_user_id'] = $uid;
  579. $orderGoods[$k]['goods_id'] = $the_used_good->id;
  580. $orderGoods[$k]['goods_type'] = 6;
  581. $orderGoods[$k]['product_id'] = 0;
  582. $orderGoods[$k]['created_at'] = $created_at;
  583. $orderGoods[$k]['refund_deadline'] = $refund_deadline;
  584. }else{
  585. DB::rollBack();
  586. return $this->fail(200003);
  587. }
  588. }else{
  589. $the_product = WxShopGoodsProduct::where('id', $v['product_id'])->first();
  590. // 规格存在并且有存量时
  591. if ($the_product) {
  592. $WxShopGoods = WxShopGoods::where('id', $the_product->goods_id)->first();
  593. $good_servers = $WxShopGoods->service_id;
  594. if(_empty_($good_servers)){
  595. $good_servers = '[]';
  596. }
  597. $created_at = date('Y-m-d H:i:s', time());
  598. $good_servers = json_decode($good_servers, true);
  599. $refund_deadline = Carbon::now()->addDays(7);
  600. if(in_array(1, $good_servers)){
  601. $refund_deadline = Carbon::now();
  602. $created_at = $refund_deadline;
  603. }
  604. $recharge = $is_vip ? $the_product->vip_price * $v['num'] : $the_product->price * $v['num'];
  605. $_order_amount += $recharge;
  606. $_goods_amount += $the_product->price * $v['num'];
  607. $orderGoods[$k]['pic'] = $the_product->pic;
  608. $orderGoods[$k]['name'] = $v['goods_name'];
  609. $orderGoods[$k]['product'] = $the_product->param_value;
  610. $orderGoods[$k]['vip_price'] = $the_product->vip_price;
  611. $orderGoods[$k]['price'] = $the_product->price;
  612. $orderGoods[$k]['recharge'] = $recharge;
  613. $orderGoods[$k]['quantity'] = $v['num'];
  614. $orderGoods[$k]['order_id'] = $orderId;
  615. $orderGoods[$k]['seller_user_id'] = $WxShopGoods->user_id ?: 0;
  616. $orderGoods[$k]['buyer_user_id'] = $uid;
  617. $orderGoods[$k]['goods_id'] = $the_product->goods_id;
  618. $orderGoods[$k]['goods_type'] = $WxShopGoods->type;
  619. $orderGoods[$k]['product_id'] = $the_product->id;
  620. $orderGoods[$k]['created_at'] = $created_at;
  621. $orderGoods[$k]['refund_deadline'] = $refund_deadline;
  622. }else{
  623. DB::rollBack();
  624. return $this->fail(200003);
  625. }
  626. }
  627. }
  628. $ogModel = new WxShopOrderGoods();
  629. $ogModel->addAll($orderGoods);
  630. //修改购物车数据
  631. if(_array_key($order, 'cart_id', 0) > 0){
  632. WxShopCart::where('id', $order['cart_id'])->update(['state' => 2]);
  633. }else{
  634. WxShopCart::where('user_id', $uid)->where('is_check', 1)->where('state', 0)
  635. ->update(['state' => 2]);
  636. }
  637. if($order_amount < 0.01){
  638. WxShopOrder::where('id', $orderId)
  639. ->update(['pay_status' => 2, 'status' => 1, 'coins_num'=>$order['coins_num'], 'coins_pay'=>$coin_blance, 'pay_way'=>ShopUtils::generate_payway_text($discounts_amount,0, 0, 0, 0, $order['coins_num'])]);
  640. $notify_controller->usedGoodPaied($orderId);
  641. ShopUtils::paied_content_process($orderId);
  642. ShopUtils::split_shop_order_by_seller($orderId);
  643. ShopUtils::split_shop_order_by_type($orderId);
  644. ShopUtils::order_buys_and_stock($orderId);
  645. WxOrder::where('order_number', $orderSn)->update(['order_state' => 1]);
  646. DB::commit();
  647. return $this->success(['tip'=>'订单结算成功!']);
  648. }
  649. if($order['coins_num'] > 0){
  650. // 仅金币支付
  651. $need_pay_price = $order_amount - $coin_blance;
  652. if($need_pay_price < 0.01){
  653. $pay_res2 = UserUtils::update_user_coins($uid,3, -$order['coins_num'], '购物车订单结算(商品订单id: '.$orderId.')¥'.$order_amount.'('. $order['coins_num'].Settings::get('app_coin_name', '硬币').'抵扣了¥'.$coin_blance.'):'.Settings::get('app_coin_name', '硬币').'-'.$order['coins_num']);
  654. if($pay_res2){
  655. WxShopOrder::where('id', $orderId)
  656. ->update(['pay_status' => 2, 'status' => 1, 'coins_num'=>$order['coins_num'], 'coins_pay'=>$coin_blance, 'pay_way'=>ShopUtils::generate_payway_text($discounts_amount,0, 0, 0, 0, $order['coins_num'])]);
  657. $notify_controller->usedGoodPaied($orderId);
  658. ShopUtils::paied_content_process($orderId);
  659. ShopUtils::split_shop_order_by_seller($orderId);
  660. ShopUtils::split_shop_order_by_type($orderId);
  661. ShopUtils::order_buys_and_stock($orderId);
  662. }else{
  663. DB::rollBack();
  664. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  665. }
  666. }else{
  667. // 金币+余额支付
  668. $pay_res = UserUtils::update_user_financial($uid, 13, $need_pay_price, '您通过余额付费(消耗'.$order['coins_num'].Settings::get('app_coin_name', '硬币').'抵扣了'.$coin_blance.')结算了「<a href="/pagesA/shop/order/order">商品订单(id:'.$orderId.')</a>」,订单金额¥'.$order_amount);
  669. // 处理通知
  670. if($pay_res){
  671. $pay_res2 = UserUtils::update_user_coins($uid,3, -$order['coins_num'], '结算了商品订单(id: '.$orderId.')¥'.$order_amount.'('. $order['coins_num'].Settings::get('app_coin_name', '硬币').'抵扣了¥'.$coin_blance.'):'.Settings::get('app_coin_name', '硬币').'-'.$order['coins_num']);
  672. if($pay_res2){
  673. // 创建订单
  674. WxShopOrder::where('id', $orderId)
  675. ->update(['pay_status' => 2, 'status' => 1, 'coins_num'=>$order['coins_num'], 'coins_pay'=>$coin_blance, 'pay_way'=>ShopUtils::generate_payway_text($discounts_amount, 0,0, 0, $need_pay_price, $order['coins_num'])]);
  676. $notify_controller->usedGoodPaied($orderId);
  677. ShopUtils::paied_content_process($orderId);
  678. ShopUtils::split_shop_order_by_seller($orderId);
  679. ShopUtils::split_shop_order_by_type($orderId);
  680. ShopUtils::order_buys_and_stock($orderId);
  681. }else{
  682. DB::rollBack();
  683. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  684. }
  685. }else{
  686. DB::rollBack();
  687. return $this->fail(200012);
  688. }
  689. }
  690. }else{
  691. // 余额支付
  692. $pay_res = UserUtils::update_user_financial($uid, 13, $order_amount, '您通过余额付费结算了「<a href="/pagesA/shop/order/order">商品订单(id:'.$orderId.')</a>」,订单金额¥'.$order_amount);
  693. // 处理通知
  694. if($pay_res){
  695. WxShopOrder::where('id', $orderId)
  696. ->update(['pay_status' => 2, 'status' => 1, 'pay_way'=>ShopUtils::generate_payway_text($discounts_amount,0, 0, 0, $order_amount, 0)]);
  697. $notify_controller->usedGoodPaied($orderId);
  698. ShopUtils::paied_content_process($orderId);
  699. ShopUtils::split_shop_order_by_seller($orderId);
  700. ShopUtils::split_shop_order_by_type($orderId);
  701. ShopUtils::order_buys_and_stock($orderId);
  702. }else{
  703. DB::rollBack();
  704. return $this->fail(200012);
  705. }
  706. }
  707. WxOrder::where('order_number', $orderSn)->update(['order_state' => 1]);
  708. DB::commit();
  709. return $this->success(['tip'=>'订单结算成功!']);
  710. }
  711. else if (_array_key($order, 'type', 0) == 6) {
  712. // 付费推广
  713. $is_promotion_refresh = false;
  714. $time_list = [];
  715. if(_array_key($order, 'is_refresh', 0) == 1){
  716. if( _empty_(_array_key($order, 'post_id', 0)) || _empty_(_array_key($order, 'time_list', [])) ){
  717. return $this->fail(200004);
  718. }
  719. $is_promotion_refresh = true;
  720. }else{
  721. if(_empty_(_array_key($order, 'post_id', 0)) || _empty_(_array_key($order, 'index', 0), true)){
  722. return $this->fail(200004);
  723. }
  724. }
  725. $post_id = _array_key($order, 'post_id', 0);
  726. $post_id = _abs($post_id);
  727. $the_post = WxPost::where('id', $post_id)->where([['is_examine' ,'=', 1], ['posts_state' , '=', 0]])->first();
  728. if(!$the_post){
  729. DB::rollBack();
  730. return $this->fail(200004, [], '笔记不存在或非正常推送状态');
  731. }
  732. if($is_promotion_refresh){
  733. $time_list = _array_key($order, 'time_list', []);
  734. if(!is_array($time_list)){
  735. return $this->fail(200004);
  736. }
  737. $the_order_price = bcdiv(Settings::get('app_promoted_post_refresh_cost', 1.0, true) * count($time_list), 1, 2);
  738. $order_info_des = '笔记推广自动刷新'.count($time_list).'次';
  739. }else{
  740. $tmp_tenant_id = -1;
  741. if($__MINI_GLOBAL_TENANT_ID__ != $the_post->tenant_id){
  742. $tmp_tenant_id = $__MINI_GLOBAL_TENANT_ID__;
  743. $__MINI_GLOBAL_TENANT_ID__ = $the_post->tenant_id;
  744. }
  745. $post_options = Settings::get('app_promoted_post_options', [], true);
  746. if($tmp_tenant_id > 0){
  747. $__MINI_GLOBAL_TENANT_ID__ = $tmp_tenant_id;
  748. }
  749. $index_option = _array_key($order, 'index', 0);
  750. if(_empty_($post_options) || !isset($post_options[$index_option])){
  751. DB::rollBack();
  752. return $this->fail(200004, [], '后台未配置index为'.$index_option.'的付费推广套餐');
  753. }
  754. $the_option = $post_options[$index_option];
  755. if($the_option['credit_type'] != 1){
  756. DB::rollBack();
  757. return $this->fail(200004, [], '该接口只能进行余额付费推广');
  758. }
  759. $the_order_price = $the_option['price'];
  760. $order_info_des = '笔记付费置顶'.$the_option['hour'].'小时';
  761. }
  762. // 修改使用金币的数量
  763. if($the_order_price <= $coin_blance){
  764. $coin_blance = $the_order_price;
  765. $order['coins_num'] = (int)($coin_blance * $coin_rate);
  766. }
  767. if($order['coins_num'] > 0){
  768. $coin_pay_percent_every = _between_(Settings::get('app_coin_pay_percent_every', 30), 0, 100);
  769. if($coin_pay_percent_every > 0){
  770. if( ($the_order_price * $coin_pay_percent_every / 100) < $coin_blance ){
  771. DB::rollBack();
  772. return $this->fail(200004, [], Settings::get('app_coin_name', '硬币').'抵扣不能超过'.$coin_pay_percent_every.'%');
  773. }
  774. }
  775. }
  776. if($balance - $the_order_price + $coin_blance < 0){
  777. return $this->fail(200012);
  778. }
  779. $orderModel = $this->createOrder($uid,6,$order_info_des,$the_order_price,$the_order_price,$orderSn);
  780. if($order['coins_num'] > 0){
  781. $need_pay_price = $the_order_price - $coin_blance;
  782. if($need_pay_price < 0.01){
  783. // 金币直接结清
  784. if($is_promotion_refresh){
  785. $pay_res2 = UserUtils::update_user_coins($uid, 3, -$order['coins_num'], '笔记(id: '.$post_id.')付费推广自动刷新'.count($time_list).'次('. $order['coins_num']. Settings::get('app_coin_name', '硬币') . '抵扣了¥'.$coin_blance.'):金币-'.$order['coins_num']);
  786. if($pay_res2){
  787. $insert_data = [];
  788. sort($time_list);
  789. foreach ($time_list as $timestap){
  790. $insert_data[] = [
  791. 'user_id' => $uid,
  792. 'post_id' => $post_id,
  793. 'expect_time' => Carbon::createFromTimestamp((int)($timestap / 1000)),
  794. 'refresh_time' => null,
  795. 'balance_cost' => 0,
  796. 'coin_cost' => (int)($order['coins_num'] / count($time_list)),
  797. 'status' => 0
  798. ];
  799. }
  800. WxPostsWeightRefresh::insert($insert_data);
  801. }else{
  802. DB::rollBack();
  803. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  804. }
  805. }else{
  806. $pay_res2 = UserUtils::update_user_coins($uid, 3, -$order['coins_num'], '笔记(id: '.$post_id.')付费推广'.$the_option['tip'].'('. $order['coins_num']. Settings::get('app_coin_name', '硬币') . '抵扣了¥'.$coin_blance.'):金币-'.$order['coins_num']);
  807. if($pay_res2){
  808. $the_weight = $the_post->weight;
  809. if($the_weight < time()){
  810. $the_weight = time();
  811. }
  812. $the_weight += $the_option['hour'] * 3600;
  813. WxPost::withoutSyncingToSearch(function () use ($post_id, $the_weight) {
  814. WxPost::where('id', $post_id)->update(['weight'=>timetostr($the_weight)]);
  815. });
  816. }else{
  817. DB::rollBack();
  818. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  819. }
  820. }
  821. }else{
  822. if($is_promotion_refresh){
  823. $pay_res = UserUtils::update_user_financial($uid, 21, $need_pay_price, '您花费了¥'.$need_pay_price.'余额+'.$order['coins_num'].Settings::get('app_coin_name', '硬币').'推广了笔记'.$post_id.'(自动刷新:'.count($time_list).'次)', $post_id);
  824. if($pay_res){
  825. $pay_res2 = UserUtils::update_user_coins($uid, 3, -$order['coins_num'], '笔记(id: '.$post_id.')付费推广自动刷新'.count($time_list).'次('. $order['coins_num'].Settings::get('app_coin_name', '硬币').'抵扣了¥'.$coin_blance.'):'. Settings::get('app_coin_name', '硬币') .'-'.$order['coins_num']);
  826. if($pay_res2){
  827. $post_refresh_cost = floor(($need_pay_price / count($time_list)) * 100) / 100;
  828. $insert_data = [];
  829. sort($time_list);
  830. foreach ($time_list as $timestap){
  831. $insert_data[] = [
  832. 'user_id' => $uid,
  833. 'post_id' => $post_id,
  834. 'expect_time' => Carbon::createFromTimestamp((int)($timestap / 1000)),
  835. 'refresh_time' => null,
  836. 'coin_cost' => (int)($order['coins_num'] / count($time_list)),
  837. 'balance_cost' => $post_refresh_cost,
  838. 'status' => 0
  839. ];
  840. }
  841. WxPostsWeightRefresh::insert($insert_data);
  842. }else{
  843. DB::rollBack();
  844. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  845. }
  846. }else{
  847. DB::rollBack();
  848. return $this->fail(200012);
  849. }
  850. }else{
  851. $pay_res = UserUtils::update_user_financial($uid, 21, $need_pay_price, '您花费了¥'.$need_pay_price.'余额+'.$order['coins_num'].Settings::get('app_coin_name', '硬币').'推广了笔记'.$post_id.'(增加时长:'.$the_option['tip'].')');
  852. if($pay_res){
  853. $pay_res2 = UserUtils::update_user_coins($uid, 3, -$order['coins_num'], '笔记(id: '.$post_id.')付费推广'.$the_option['tip'].'('. $order['coins_num'].Settings::get('app_coin_name', '硬币').'抵扣了¥'.$coin_blance.'):'. Settings::get('app_coin_name', '硬币') .'-'.$order['coins_num']);
  854. if($pay_res2){
  855. $the_weight = $the_post->weight;
  856. if($the_weight < time()){
  857. $the_weight = time();
  858. }
  859. $the_weight += $the_option['hour'] * 3600;
  860. WxPost::withoutSyncingToSearch(function () use ($post_id, $the_weight) {
  861. WxPost::where('id', $post_id)->update(['weight'=>timetostr($the_weight)]);
  862. });
  863. }else{
  864. DB::rollBack();
  865. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  866. }
  867. }else{
  868. DB::rollBack();
  869. return $this->fail(200012);
  870. }
  871. }
  872. }
  873. }else{
  874. if($is_promotion_refresh){
  875. $pay_res = UserUtils::update_user_financial($uid, 21, $the_order_price, '您花费¥'.$the_order_price.'推广了笔记'.$post_id.'(自动刷新:'.count($time_list).'次)', $post_id);
  876. if($pay_res){
  877. $post_refresh_cost = floor(($the_order_price / count($time_list)) * 100) / 100;
  878. $insert_data = [];
  879. sort($time_list);
  880. foreach ($time_list as $timestap){
  881. $insert_data[] = [
  882. 'user_id' => $uid,
  883. 'post_id' => $post_id,
  884. 'expect_time' => Carbon::createFromTimestamp((int)($timestap / 1000)),
  885. 'refresh_time' => null,
  886. 'balance_cost' => $post_refresh_cost,
  887. 'status' => 0
  888. ];
  889. }
  890. WxPostsWeightRefresh::insert($insert_data);
  891. }else{
  892. DB::rollBack();
  893. return $this->fail(200012);
  894. }
  895. }else{
  896. $pay_res = UserUtils::update_user_financial($uid, 21, $the_option['price'], '您花费¥'.$the_option['price'].'推广了笔记'.$post_id.'(增加时长:'.$the_option['tip'].')');
  897. if($pay_res){
  898. $the_weight = $the_post->weight;
  899. if($the_weight < time()){
  900. $the_weight = time();
  901. }
  902. $the_weight += $the_option['hour'] * 3600;
  903. WxPost::withoutSyncingToSearch(function () use ($post_id, $the_weight) {
  904. WxPost::where('id', $post_id)->update(['weight'=>timetostr($the_weight)]);
  905. });
  906. }else{
  907. DB::rollBack();
  908. return $this->fail(200012);
  909. }
  910. }
  911. }
  912. WxOrder::where('order_number', $orderSn)->update(['order_state' => 1]);
  913. // 创建订单
  914. DB::commit();
  915. return $this->success(['tip'=>'恭喜,推广成功']);
  916. }
  917. else if (_array_key($order, 'type', 0) == 7) {
  918. // 付费圈子
  919. if(_empty_(_array_key($order, 'circle_id', 0))){
  920. return $this->fail(200004);
  921. }
  922. $circle_id = $order['circle_id'];
  923. $circle = WxCircle::find($circle_id);
  924. if(_empty_($circle)){
  925. return $this->fail(200003);
  926. }
  927. $order['price'] = $circle->follow_pay_num;
  928. $follow_pay_credit = $circle->follow_pay_credit;
  929. if($follow_pay_credit != 1){
  930. return $this->fail(200004, [], '改接口仅处理余额付费'.env('circle_call', '圈子'));
  931. }
  932. // 修改使用金币的数量
  933. if($order['price'] <= $coin_blance){
  934. $coin_blance = $order['price'];
  935. $order['coins_num'] = (int)($coin_blance * $coin_rate);
  936. }
  937. if($order['coins_num'] > 0){
  938. $coin_pay_percent_every = _between_(Settings::get('app_coin_pay_percent_every', 30), 0, 100);
  939. if($coin_pay_percent_every > 0){
  940. if( ($order['price'] * $coin_pay_percent_every / 100) < $coin_blance ){
  941. DB::rollBack();
  942. return $this->fail(200004, [], Settings::get('app_coin_name', '硬币').'抵扣不能超过'.$coin_pay_percent_every.'%');
  943. }
  944. }
  945. }
  946. if($balance - $order['price'] + $coin_blance < 0){
  947. return $this->fail(200012);
  948. }
  949. $platform_division = _between_(Settings::get('app_circle_follow_pay_division', 0), 0, 100);
  950. $platform_reward = round($platform_division * $order['price'] / 100, 2);
  951. $circle_user_reward = round($order['price'] - $platform_reward, 2);
  952. $orderModel = $this->createOrder($uid,7,'加入付费'.env('circle_call', '圈子'),$order['price'],$order['price'],$orderSn);
  953. if($order['coins_num'] > 0){
  954. $need_pay_price = $order['price'] - $coin_blance;
  955. if($need_pay_price < 0.01){
  956. // 金币直接结清
  957. $pay_res2 = UserUtils::update_user_coins($uid, 3, -$order['coins_num'], '付费加入「'.$circle->circle_name.'」'.env('circle_call', '圈子'). $order['coins_num']. Settings::get('app_coin_name', '硬币') . '抵扣了¥'.$coin_blance.'):金币-'.$order['coins_num']);
  958. if($pay_res2){
  959. $r = WxCircleRepositores::userFollowCircle($uid, $circle->id, Carbon::now()->addDays((int)$circle->follow_pay_duration)->toDateTimeString());
  960. if($r){
  961. if($circle_user_reward >= 0.01){
  962. $pay_res_ = UserUtils::update_user_financial($circle->user_id, 23, $circle_user_reward, '有新的用户(id: '.$uid.')加入您的付费'.env('circle_call', '圈子').',扣掉平台分成'.$platform_division.'%,您获得'.$circle_user_reward.'余额。');
  963. }
  964. }else{
  965. DB::rollBack();
  966. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  967. }
  968. }else{
  969. DB::rollBack();
  970. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  971. }
  972. }else{
  973. $pay_res = UserUtils::update_user_financial($uid, 22, $need_pay_price, '您花费了¥'.$need_pay_price.'余额+'.$order['coins_num'].Settings::get('app_coin_name', '硬币').'加入了「'.$circle->circle_name.'」'.env('circle_call', '圈子'));
  974. if($pay_res){
  975. $pay_res2 = UserUtils::update_user_coins($uid, 3, -$order['coins_num'], '加入「'.$circle->circle_name.'」'.env('circle_call', '圈子').'('. $order['coins_num'].Settings::get('app_coin_name', '硬币').'抵扣了¥'.$coin_blance.'):'. Settings::get('app_coin_name', '硬币') .'-'.$order['coins_num']);
  976. if($pay_res2){
  977. $r = WxCircleRepositores::userFollowCircle($uid, $circle->id, Carbon::now()->addDays((int)$circle->follow_pay_duration)->toDateTimeString());
  978. if($r){
  979. if($circle_user_reward >= 0.01){
  980. $pay_res_ = UserUtils::update_user_financial($circle->user_id, 23, $circle_user_reward, '有新的用户(id: '.$uid.')加入您的付费'.env('circle_call', '圈子').',扣掉平台分成'.$platform_division.'%,您获得'.$circle_user_reward.'余额。');
  981. }
  982. }else{
  983. DB::rollBack();
  984. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  985. }
  986. }else{
  987. DB::rollBack();
  988. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  989. }
  990. }else{
  991. DB::rollBack();
  992. return $this->fail(200012);
  993. }
  994. }
  995. }else{
  996. $pay_res = UserUtils::update_user_financial($uid, 22, $order['price'], '您花费了¥'.$order['price'].'余额加入了「'.$circle->circle_name.'」'.env('circle_call', '圈子'));
  997. if($pay_res){
  998. $r = WxCircleRepositores::userFollowCircle($uid, $circle->id, Carbon::now()->addDays((int)$circle->follow_pay_duration)->toDateTimeString());
  999. if($r){
  1000. if($circle_user_reward >= 0.01){
  1001. $pay_res_ = UserUtils::update_user_financial($circle->user_id, 23, $circle_user_reward, '有新的用户(id: '.$uid.')加入您的付费'.env('circle_call', '圈子').',扣掉平台分成'.$platform_division.'%,您获得'.$circle_user_reward.'余额。');
  1002. }
  1003. }else{
  1004. DB::rollBack();
  1005. return $this->fail(200011, [], '余额不足');
  1006. }
  1007. }else{
  1008. DB::rollBack();
  1009. return $this->fail(200012);
  1010. }
  1011. }
  1012. WxOrder::where('order_number', $orderSn)->update(['order_state' => 1]);
  1013. // 创建订单
  1014. DB::commit();
  1015. return $this->success(['tip'=>'恭喜,成功加入「'.$circle->circle_name.'」'.env('circle_call', '圈子').',付费时长:'.$circle->follow_pay_duration.'天']);
  1016. }else if (_array_key($order, 'type', 0) == 9) {
  1017. // 购买模块
  1018. if(get_mini_domain() == 'mini.minisns.cn'){
  1019. if(_empty_(_array_key($order, 'num', ''), true) || _empty_(_array_key($order, 'domain', ''))){
  1020. DB::rollBack();
  1021. return $this->fail(200004);
  1022. }
  1023. $domain = _array_key($order, 'domain', '');
  1024. // todo:
  1025. $body = '购买模块'.$order['num'];
  1026. $r['body'] = $body;
  1027. $model_paycode_map = [
  1028. 1 => 2473,
  1029. 2 => 2474,
  1030. 3 => 2477,
  1031. 4 => 2475,
  1032. 5 => 2472,
  1033. 6 => 2476,
  1034. 7 => 2478,
  1035. 8 => 2479,
  1036. 9 => 2480,
  1037. 10 => 2481,
  1038. 11 => 2482,
  1039. 12 => 2483,
  1040. 13 => 2733,
  1041. 14 => 2484,
  1042. 15 => 2794,
  1043. 16 => 2680,
  1044. 17 => 2492,
  1045. 18 => 2551,
  1046. 19 => 2573,
  1047. 20 => 2669,
  1048. 21 => 2715,
  1049. 22 => 2719,
  1050. 23 => 2722,
  1051. 24 => 2729,
  1052. 25 => 2776,
  1053. 26 => 2740,
  1054. 27 => 2748,
  1055. 28 => 2797,
  1056. 29 => 2805,
  1057. 30 => 2817,
  1058. 31 => 2824,
  1059. 32 => 2886,
  1060. 33 => 2895,
  1061. 34 => 2897,
  1062. 36 => 2908,
  1063. 0 => 2899,
  1064. ];
  1065. if(!isset($model_paycode_map[$order['num']])){
  1066. DB::rollBack();
  1067. return $this->fail(200003, [], '没有对应的模块');
  1068. }
  1069. if(_empty_(_array_key($order, 'domain', ''))){
  1070. $domain_auth = WxDomainAuth::where('user_id', $uid)->first();
  1071. }else{
  1072. $domain_auth = WxDomainAuth::where('domain_1', $domain)->orWhere('domain_2', $domain)
  1073. ->orWhere('domain_3', $domain)->orWhere('domain_4', $domain)
  1074. ->orWhere('domain_5', $domain)->orWhere('domain_6', $domain)
  1075. ->orWhere('domain_7', $domain)->first();
  1076. }
  1077. if($domain_auth){
  1078. // 检查这个用户需不需要购买该模块
  1079. $base_pays = explode(',', $domain_auth->pays);
  1080. if(in_array($order['num'], $base_pays)){
  1081. DB::rollBack();
  1082. return $this->fail(200010, [], '该用户已经拥有模块'.$order['num'].'的授权,无需重复购买');
  1083. }
  1084. }else{
  1085. // 没有授权的情况怎么办
  1086. if(_array_key($order, 'user_id', 0) > 0){
  1087. }else{
  1088. DB::rollBack();
  1089. return $this->fail(200004, [], '缺少user_id参数');
  1090. }
  1091. }
  1092. if($is_vip){
  1093. $total_fee = WxShopGoodsProduct::where('goods_id', $model_paycode_map[$order['num']])->value('vip_price');
  1094. }else{
  1095. $total_fee = WxShopGoodsProduct::where('goods_id', $model_paycode_map[$order['num']])->value('price');
  1096. }
  1097. // 判断能否代理价格
  1098. $is_agent_price = false;
  1099. $my_domain_auth = WxDomainAuth::where('user_id', $uid)->first();
  1100. // 条件一:自己有授权
  1101. if($my_domain_auth){
  1102. if($domain_auth){
  1103. // 条件二:不是给自己买模块
  1104. if($my_domain_auth->id != $domain_auth->id){
  1105. $is_agent_price = true;
  1106. }
  1107. }else{
  1108. // 自己有授权,但这次不是自己的
  1109. $is_agent_price = true;
  1110. }
  1111. }
  1112. if($is_agent_price){
  1113. if($order['num'] > 0){
  1114. $total_fee = round(0.8 * $total_fee, 2);
  1115. }else{
  1116. $total_fee = 300;
  1117. }
  1118. }
  1119. // user_id 应对利用域名查询不出来的情况
  1120. $orderModel = $this->createOrder($uid,9,$body,$total_fee,$total_fee,$orderSn, serialize(['num'=>$order['num'], 'domain'=>_array_key($order, 'domain', ''), 'user_id'=>_array_key($order, 'user_id', 0)]));
  1121. $pay_res = UserUtils::update_user_financial($uid, 27, $total_fee, '您花费了¥'.$total_fee.'余额购买了「模块'.$order['num'].'」');
  1122. if($pay_res){
  1123. if($domain_auth){
  1124. $base_pays = explode(',', $domain_auth->pays);
  1125. $base_pays[] = $order['num'];
  1126. $base_pays = array_unique($base_pays);
  1127. if($domain_auth){
  1128. WxDomainAuth::where('id', $domain_auth->id)->update(['pays' => implode(',', $base_pays)]);
  1129. }
  1130. if($order['num'] == 0){
  1131. if($domain_auth && $domain_auth->user_id != $uid){
  1132. WxUser::where('id', $domain_auth->user_id)->update([
  1133. 'who' => $uid
  1134. ]);
  1135. }
  1136. }
  1137. }else{
  1138. // 还有一种情况该用户已经有授权,只是不是这个域名
  1139. // todo:
  1140. if(_array_key($order, 'user_id', 0) > 0){
  1141. // 新建授权
  1142. $model = new WxDomainAuth();
  1143. $model->user_id = $order['user_id'];
  1144. $model->domain_1 = $order['domain'];
  1145. $model->tenant_1 = '0,999';
  1146. $model->pays = $order['num'];
  1147. $r = $model->save();
  1148. if($order['num'] == 0){
  1149. if($order['user_id'] != $uid){
  1150. WxUser::where('id', $model->user_id)->update([
  1151. 'who' => $uid
  1152. ]);
  1153. }
  1154. }
  1155. }
  1156. }
  1157. // 分账
  1158. if($model_paycode_map[$order['num']] > 0){
  1159. $users_ledger = WxShopGoods::where('id', $model_paycode_map[$order['num']])->value('users_ledger');
  1160. if(!_empty_($users_ledger)){
  1161. $users_ledger = json_decode($users_ledger, true);
  1162. if(!_empty_($users_ledger)){
  1163. foreach ($users_ledger as $item){
  1164. if(_empty_(_array_key($item, 'user_id', 0)) || _empty_(_array_key($item, 'rate', 0))){
  1165. continue;
  1166. }
  1167. if($total_fee > 0){
  1168. $recharge_division = round( $total_fee * $item['rate'] * 0.01, 2);
  1169. if($recharge_division >= 0.01){
  1170. UserUtils::update_user_financial($item['user_id'], 20, $recharge_division,
  1171. '您获得一笔来自商品(id:<a href="/pagesA/shop/goods-details/goods-details?id='.$model_paycode_map[$order['num']].'">'.$model_paycode_map[$order['num']].'</a>)的分账,该商品共消费¥'.$total_fee.',您获得其中'.$item['rate'].'%');
  1172. }
  1173. }
  1174. }
  1175. }
  1176. }
  1177. }
  1178. }else{
  1179. DB::rollBack();
  1180. return $this->fail(200012, [], '余额不足');
  1181. }
  1182. WxOrder::where('order_number', $orderSn)->update(['order_state' => 1]);
  1183. DB::commit();
  1184. return $this->success(['tip'=>'您花费了¥'.$total_fee.'余额购买了「模块'.$order['num'].'」']);
  1185. }
  1186. }else if (_array_key($order, 'type', 0) == 10) {
  1187. if(_empty_(_array_key($order, 'user_id', 0)) || _empty_(_array_key($order, 'user_state', 0))){
  1188. return $this->fail(200004);
  1189. }
  1190. $user_id = $order['user_id'];
  1191. $the_user = WxUser::find($user_id);
  1192. if(_empty_($the_user)){
  1193. return $this->fail(200003);
  1194. }
  1195. if($order['user_state'] != $the_user->user_state){
  1196. return $this->fail(200004, [], '参数检验不合法,请刷新重试');
  1197. }
  1198. $order['price'] = 0;
  1199. $body = '';
  1200. if($the_user->user_state === 2){
  1201. $ban_until = get_user_meta($the_user->id, 'ban_until', 's');
  1202. if(time() > $ban_until){
  1203. // 解除封号
  1204. if(UserUtils::unlock_user($the_user, 2)){
  1205. $the_user->state_tip = '正常用户';
  1206. $the_user->user_state = 0;
  1207. }
  1208. }
  1209. if($ban_until && $the_user->user_state === 2){
  1210. $ban_until_ = Carbon::createFromTimestamp($ban_until);
  1211. $secondsDifference = $ban_until_->diffInSeconds(Carbon::now());
  1212. $hoursDifference = round($secondsDifference / 3600, 2);
  1213. $tmp_tenant_id = -1;
  1214. if($__MINI_GLOBAL_TENANT_ID__ != $the_user->tenant_id){
  1215. $tmp_tenant_id = $__MINI_GLOBAL_TENANT_ID__;
  1216. $__MINI_GLOBAL_TENANT_ID__ = $the_user->tenant_id;
  1217. }
  1218. $order['price'] = round($hoursDifference * Settings::get('app_unban_one_hour_balance', 1.0, true), 2);
  1219. if($tmp_tenant_id > 0){
  1220. $__MINI_GLOBAL_TENANT_ID__ = $tmp_tenant_id;
  1221. }
  1222. if($the_user->id == $uid){
  1223. $body = '自助解封账号';
  1224. }else{
  1225. $body = '帮他人(uid:'.$the_user->id.')解封账号';
  1226. }
  1227. }
  1228. }else if($the_user->user_state === 3){
  1229. $mute_until = get_user_meta($the_user->id, 'mute_until', 's');
  1230. if(time() > $mute_until){
  1231. // 解除禁言
  1232. if(UserUtils::unlock_user($the_user, 3)){
  1233. $the_user->state_tip = '正常用户';
  1234. $the_user->user_state = 0;
  1235. }
  1236. }
  1237. if($mute_until && $the_user->user_state === 3){
  1238. $mute_until_ = Carbon::createFromTimestamp($mute_until);
  1239. $secondsDifference = $mute_until_->diffInSeconds(Carbon::now());
  1240. $hoursDifference = round($secondsDifference / 3600, 2);
  1241. $tmp_tenant_id = -1;
  1242. if($__MINI_GLOBAL_TENANT_ID__ != $the_user->tenant_id){
  1243. $tmp_tenant_id = $__MINI_GLOBAL_TENANT_ID__;
  1244. $__MINI_GLOBAL_TENANT_ID__ = $the_user->tenant_id;
  1245. }
  1246. $order['price'] = round($hoursDifference * Settings::get('app_unmute_one_hour_balance', 0.5, true), 2);
  1247. if($tmp_tenant_id > 0){
  1248. $__MINI_GLOBAL_TENANT_ID__ = $tmp_tenant_id;
  1249. }
  1250. if($the_user->id == $uid){
  1251. $body = '自助解除禁言';
  1252. }else{
  1253. $body = '帮他人(uid:'.$the_user->id.')解除禁言';
  1254. }
  1255. }
  1256. }else{
  1257. return $this->fail(200009, [], '无法解除');
  1258. }
  1259. if(_empty_($order['price'])){
  1260. return $this->fail(200009, [], '无法解除');
  1261. }
  1262. // 修改使用金币的数量
  1263. if($order['price'] <= $coin_blance){
  1264. $coin_blance = $order['price'];
  1265. $order['coins_num'] = (int)($coin_blance * $coin_rate);
  1266. }
  1267. if($order['coins_num'] > 0){
  1268. $coin_pay_percent_every = _between_(Settings::get('app_coin_pay_percent_every', 30), 0, 100);
  1269. if($coin_pay_percent_every > 0){
  1270. if( ($order['price'] * $coin_pay_percent_every / 100) < $coin_blance ){
  1271. DB::rollBack();
  1272. return $this->fail(200004, [], Settings::get('app_coin_name', '硬币').'抵扣不能超过'.$coin_pay_percent_every.'%');
  1273. }
  1274. }
  1275. }
  1276. if($balance - $order['price'] + $coin_blance < 0){
  1277. return $this->fail(200012);
  1278. }
  1279. $orderModel = $this->createOrder($uid,10,$body,$order['price'],$order['price'],$orderSn, serialize(['user_id'=>$the_user->id, 'user_state' => $the_user->user_state]));
  1280. if($order['coins_num'] > 0){
  1281. $need_pay_price = $order['price'] - $coin_blance;
  1282. if($need_pay_price < 0.01){
  1283. // 金币直接结清
  1284. $pay_res2 = UserUtils::update_user_coins($uid, 3, -$order['coins_num'], $body . $order['coins_num']. Settings::get('app_coin_name', '硬币') . '抵扣了¥'.$coin_blance.'):金币-'.$order['coins_num']);
  1285. if($pay_res2){
  1286. UserUtils::unlock_user($the_user, $the_user->user_state);
  1287. }else{
  1288. DB::rollBack();
  1289. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  1290. }
  1291. }else{
  1292. $pay_res = UserUtils::update_user_financial($uid, 30, $need_pay_price, '您花费了¥'.$need_pay_price.'余额+'.$order['coins_num'].Settings::get('app_coin_name', '硬币').$body);
  1293. if($pay_res){
  1294. $pay_res2 = UserUtils::update_user_coins($uid, 3, -$order['coins_num'], $body.'('. $order['coins_num'].Settings::get('app_coin_name', '硬币').'抵扣了¥'.$coin_blance.'):'. Settings::get('app_coin_name', '硬币') .'-'.$order['coins_num']);
  1295. if($pay_res2){
  1296. UserUtils::unlock_user($the_user, $the_user->user_state);
  1297. }else{
  1298. DB::rollBack();
  1299. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  1300. }
  1301. }else{
  1302. DB::rollBack();
  1303. return $this->fail(200012);
  1304. }
  1305. }
  1306. }else{
  1307. $pay_res = UserUtils::update_user_financial($uid, 30, $order['price'], '您花费了¥'.$order['price'].'余额'.$body);
  1308. if($pay_res){
  1309. UserUtils::unlock_user($the_user, $the_user->user_state);
  1310. }else{
  1311. DB::rollBack();
  1312. return $this->fail(200012);
  1313. }
  1314. }
  1315. if($uid != $the_user->id){
  1316. UserUtils::assistant_notice($the_user->id, '用户(<a href="/pages/user/user?id='.$uid.'">'.UserUtils::get_cached_user_name($uid).' id:'.$uid.'</a>)花费了¥'.$order['price'].'帮您解除了账号的'.($the_user->user_state == 2 ? '封号' : '禁言').'限制');
  1317. }
  1318. WxOrder::where('order_number', $orderSn)->update(['order_state' => 1]);
  1319. // 创建订单
  1320. DB::commit();
  1321. return $this->success(['tip'=>'恭喜,'.$body.'成功']);
  1322. }else if (_array_key($order, 'type', 0) == 11) {
  1323. if(_empty_(_array_key($order, 'object_id', 0)) || _empty_(_array_key($order, 'is_wechat_account', 0), true) || _empty_(_array_key($order, 'is_phone', 0), true)){
  1324. return $this->fail(200004);
  1325. }
  1326. $object_id = $order['object_id'];
  1327. $is_wechat_account = _empty_default_(_array_key($order, 'is_wechat_account', 0), 0);
  1328. $is_phone = _empty_default_(_array_key($order, 'is_phone', 0), 0);
  1329. $the_user = WxUser::find($object_id);
  1330. if(_empty_($the_user)){
  1331. return $this->fail(200003);
  1332. }
  1333. if($the_user->id == $uid){
  1334. return $this->fail(200000, [], '双方账号不能相同');
  1335. }
  1336. $contact_apply = WxUserContactApply::where('user_id', $uid)->where('object_id', $the_user->id)->first();
  1337. $user_settings = WxUserSetting::where('user_id', $the_user->id)->first();
  1338. if(_empty_($user_settings)){
  1339. return $this->fail(200000, [], '请退出登录,重新登录一下试试');
  1340. }
  1341. if($contact_apply){
  1342. if($contact_apply->status == 0){
  1343. return $this->fail(200000, [], '你已经申请过了该用户的联系方式');
  1344. }
  1345. }
  1346. $total_fee = 0;
  1347. $body = '';
  1348. if(_empty_($contact_apply) || $contact_apply->status == 2){
  1349. if($is_wechat_account == 1){
  1350. if($user_settings->privacy_get_my_wechat != 5){
  1351. return [
  1352. 'code'=>200000,
  1353. 'msg' => '该用户未开放微信号'
  1354. ];
  1355. }else{
  1356. $total_fee += $user_settings->privacy_get_my_wechat_pay;
  1357. if(!_empty_($body)){
  1358. $body .= '、';
  1359. }
  1360. $body .= 'v';
  1361. }
  1362. }
  1363. if($is_phone == 1){
  1364. if($user_settings->privacy_get_my_phone != 5){
  1365. return $this->fail(200000, [], '该用户未开放手机号');
  1366. }else{
  1367. $total_fee += $user_settings->privacy_get_my_phone_pay;
  1368. if(!_empty_($body)){
  1369. $body .= '、';
  1370. }
  1371. $body .= 'tel';
  1372. }
  1373. }
  1374. }else{
  1375. if($is_wechat_account == 1){
  1376. if($contact_apply->is_wechat_account != 1){
  1377. if($user_settings->privacy_get_my_wechat != 5){
  1378. return $this->fail(200000, [], '该用户未开放微信号');
  1379. }else{
  1380. $total_fee += $user_settings->privacy_get_my_wechat_pay;
  1381. if(!_empty_($body)){
  1382. $body .= '、';
  1383. }
  1384. $body .= 'v';
  1385. }
  1386. }else{
  1387. $is_wechat_account = 0;
  1388. }
  1389. }
  1390. if($is_phone == 1){
  1391. if($contact_apply->is_phone != 1){
  1392. if($user_settings->privacy_get_my_phone != 5){
  1393. return $this->fail(200000, [], '该用户未开放手机号');
  1394. }else{
  1395. $total_fee += $user_settings->privacy_get_my_phone_pay;
  1396. if(!_empty_($body)){
  1397. $body .= '、';
  1398. }
  1399. $body .= 'tel';
  1400. }
  1401. }else{
  1402. $is_phone = 0;
  1403. }
  1404. }
  1405. }
  1406. $reason = StrUtils::limit(strip_tags(trim(_empty_default_(_array_key($order, 'reason', ''), ''))));
  1407. $order['price'] = $total_fee;
  1408. // 修改使用金币的数量
  1409. if($order['price'] <= $coin_blance){
  1410. $coin_blance = $order['price'];
  1411. $order['coins_num'] = (int)($coin_blance * $coin_rate);
  1412. }
  1413. if($order['coins_num'] > 0){
  1414. $coin_pay_percent_every = _between_(Settings::get('app_coin_pay_percent_every', 30), 0, 100);
  1415. if($coin_pay_percent_every > 0){
  1416. if( ($order['price'] * $coin_pay_percent_every / 100) < $coin_blance ){
  1417. DB::rollBack();
  1418. return $this->fail(200004, [], Settings::get('app_coin_name', '硬币').'抵扣不能超过'.$coin_pay_percent_every.'%');
  1419. }
  1420. }
  1421. }
  1422. if($balance - $order['price'] + $coin_blance < 0){
  1423. return $this->fail(200012);
  1424. }
  1425. $orderModel = $this->createOrder($uid,11,$body,$order['price'],$order['price'],$orderSn, serialize(['object_id'=>$the_user->id, 'is_wechat_account'=>$is_wechat_account == 1 ? 1 : 0, 'is_phone' => $is_phone == 1 ? 1 : 0, 'reason'=>$reason]));
  1426. if($order['coins_num'] > 0){
  1427. $need_pay_price = $order['price'] - $coin_blance;
  1428. if($need_pay_price < 0.01){
  1429. // 金币直接结清
  1430. $pay_res2 = UserUtils::update_user_coins($uid, 3, -$order['coins_num'], $body . $order['coins_num']. Settings::get('app_coin_name', '硬币') . '抵扣了¥'.$coin_blance.'):金币-'.$order['coins_num']);
  1431. if($pay_res2){
  1432. OrderUtils::order_obtains($orderModel);
  1433. }else{
  1434. DB::rollBack();
  1435. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  1436. }
  1437. }else{
  1438. $pay_res = UserUtils::update_user_financial($uid, 32, $need_pay_price, '您花费了¥'.$need_pay_price.'余额+'.$order['coins_num'].Settings::get('app_coin_name', '硬币').$body);
  1439. if($pay_res){
  1440. $pay_res2 = UserUtils::update_user_coins($uid, 3, -$order['coins_num'], $body.'('. $order['coins_num'].Settings::get('app_coin_name', '硬币').'抵扣了¥'.$coin_blance.'):'. Settings::get('app_coin_name', '硬币') .'-'.$order['coins_num']);
  1441. if($pay_res2){
  1442. OrderUtils::order_obtains($orderModel);
  1443. }else{
  1444. DB::rollBack();
  1445. return $this->fail(200011, [], Settings::get('app_coin_name', '硬币').'不足');
  1446. }
  1447. }else{
  1448. DB::rollBack();
  1449. return $this->fail(200012);
  1450. }
  1451. }
  1452. }else{
  1453. $pay_res = UserUtils::update_user_financial($uid, 32, $order['price'], '您花费了¥'.$order['price'].'余额'.$body);
  1454. if($pay_res){
  1455. OrderUtils::order_obtains($orderModel);
  1456. }else{
  1457. DB::rollBack();
  1458. return $this->fail(200012);
  1459. }
  1460. }
  1461. WxOrder::where('order_number', $orderSn)->update(['order_state' => 1]);
  1462. // 创建订单
  1463. DB::commit();
  1464. return $this->success(['tip'=>'恭喜,'.$body.'成功']);
  1465. }
  1466. } catch (\Exception $e) {
  1467. DB::rollBack();
  1468. _logger_(__file__, __line__, $e->getMessage());
  1469. return $this->fail(200002);
  1470. }
  1471. }
  1472. private function _create_order(Request $request){
  1473. DB::beginTransaction();
  1474. try {
  1475. $r = ['code'=>0, 'orderSn'=>'', 'total_fee'=>0, 'body'=>'', 'msg'=>''];
  1476. $uid = $request->uid;
  1477. $type = $request->type;
  1478. $parame = $request->parame;
  1479. $orderSn = Utils::getSn(1);
  1480. global $__MINI_GLOBAL_TENANT_ID__;
  1481. // 开会员
  1482. if ($type == 15) {
  1483. // 推荐自己
  1484. $body = '推荐自己';
  1485. $r['body'] = '推荐自己';
  1486. $total_fee = $request->price;
  1487. $parame = serialize($parame);
  1488. // 创建订单
  1489. $this->createOrder($uid,15,$body,$total_fee,$total_fee,$orderSn,$parame);
  1490. } else if ($type == 1) {
  1491. if(_empty_(_array_key($parame, 'price', 0)) || _empty_(_array_key($parame, 'days', 0))){
  1492. return $this->fail(200004);
  1493. }
  1494. $total_fee = $parame['price'];
  1495. $r['body'] = '开通会员'.$parame['days'].'天';
  1496. $vip_options = json_decode(Settings::get('app_vip_options', '[]'), true);
  1497. if(_empty_($vip_options)){
  1498. return [
  1499. 'code'=>200004,
  1500. 'msg' => '管理员暂未配置会员套餐'
  1501. ];
  1502. }
  1503. $flag = false;
  1504. foreach ($vip_options as $option){
  1505. if($option && $option['days'] == $parame['days'] && $option['price'] == $parame['price']){
  1506. $flag = true;
  1507. break;
  1508. }
  1509. }
  1510. if($flag){
  1511. $this->createOrder($uid,1,'开通会员'.$parame['days'].'天',$total_fee,$total_fee,$orderSn);
  1512. }else{
  1513. return [
  1514. 'code'=>200004,
  1515. 'msg' => '管理员暂未配置¥'.$parame['price'].'的'.$parame['days'].'天套餐'
  1516. ];
  1517. }
  1518. }else if ($type == 2) {
  1519. $body = '充电';
  1520. $r['body'] = '充电';
  1521. $total_fee = $parame['rewardPrice'];
  1522. $parame = serialize($parame);
  1523. // 创建订单
  1524. $this->createOrder($uid,2,$body,$total_fee,$total_fee,$orderSn,$parame);
  1525. }else if ($type == 3) {
  1526. $body = '余额充值';
  1527. $r['body'] = '余额充值';
  1528. $total_fee = $parame['recharge'];
  1529. $recharge_list_arr = Settings::get('app_balance_recharge_list', []);
  1530. $flag = false;
  1531. if($recharge_list_arr){
  1532. foreach ($recharge_list_arr as $option){
  1533. if($option['rmb'] == $total_fee){
  1534. $flag = true;
  1535. }
  1536. }
  1537. }
  1538. if($flag){
  1539. // 创建订单
  1540. $this->createOrder($uid,3,$body,$total_fee,$total_fee,$orderSn);
  1541. }else{
  1542. return [
  1543. 'code'=>200004,
  1544. 'msg' => '充值套餐未包含'.$total_fee.'的充值'
  1545. ];
  1546. }
  1547. }else if( $type == 6){
  1548. // 付费推广
  1549. $is_promotion_refresh = false;
  1550. $time_list = [];
  1551. if(_array_key($parame, 'is_refresh', 0) == 1){
  1552. if( _empty_(_array_key($parame, 'post_id', 0)) || _empty_(_array_key($parame, 'time_list', [])) ){
  1553. return $this->fail(200004);
  1554. }
  1555. $is_promotion_refresh = true;
  1556. }else{
  1557. if(_empty_(_array_key($parame, 'post_id', 0)) || _empty_(_array_key($parame, 'index', 0), true)){
  1558. return [
  1559. 'code'=>200004,
  1560. 'msg' => '缺少必要的参数'
  1561. ];
  1562. }
  1563. }
  1564. $post_id = _abs($parame['post_id']);
  1565. $the_post = WxPost::where('id', $post_id)->where([['is_examine' ,'=', 1], ['posts_state' , '=', 0]])->first();
  1566. if(!$the_post){
  1567. return [
  1568. 'code'=>200004,
  1569. 'msg' => '笔记不存在或非正常推送状态'
  1570. ];
  1571. }
  1572. $__MINI_GLOBAL_TENANT_ID__ = $the_post->tenant_id;
  1573. if($is_promotion_refresh){
  1574. $time_list = _array_key($parame, 'time_list', []);
  1575. if(!is_array($time_list)){
  1576. return $this->fail(200004);
  1577. }
  1578. $the_order_price = bcdiv(Settings::get('app_promoted_post_refresh_cost', 1.0, true) * count($time_list), 1, 2);
  1579. $order_info_des = '笔记推广自动刷新'.count($time_list).'次';
  1580. }else{
  1581. $index_option = $parame['index'] ?? 0;
  1582. $tmp_tenant_id = -1;
  1583. if($__MINI_GLOBAL_TENANT_ID__ != $the_post->tenant_id){
  1584. $tmp_tenant_id = $__MINI_GLOBAL_TENANT_ID__;
  1585. $__MINI_GLOBAL_TENANT_ID__ = $the_post->tenant_id;
  1586. }
  1587. $post_options = Settings::get('app_promoted_post_options', [], true);
  1588. if($tmp_tenant_id > 0){
  1589. $__MINI_GLOBAL_TENANT_ID__ = $tmp_tenant_id;
  1590. }
  1591. if(_empty_($post_options) || !isset($post_options[$index_option])){
  1592. return [
  1593. 'code'=>200004,
  1594. 'msg' => '后台未配置index为'.$index_option.'的付费推广套餐'
  1595. ];
  1596. }
  1597. $the_option = $post_options[$index_option];
  1598. if($the_option['credit_type'] != 1){
  1599. return [
  1600. 'code'=>200004,
  1601. 'msg' => '该接口只能进行余额付费推广'
  1602. ];
  1603. }
  1604. $the_order_price = $the_option['price'];
  1605. $order_info_des = '笔记付费置顶'.$the_option['hour'].'小时';
  1606. }
  1607. if($the_order_price >= 0.01){
  1608. if($is_promotion_refresh){
  1609. $body = StrUtils::limit('付费推广笔记(id:'.$the_post->id.')自动刷新'.count($time_list).'次');
  1610. $parame = serialize(['post_id'=>$the_post->id, 'time_list'=>json_encode($time_list)]);
  1611. }else{
  1612. $body = StrUtils::limit('付费推广笔记(id:'.$the_post->id.')'.$the_option['tip']);
  1613. $parame = serialize(['post_id'=>$the_post->id, 'index'=>$index_option, 'hour'=>$the_option['hour'], 'tip'=>$the_option['tip'], 'price'=>$the_option['price'], 'credit_type'=>$the_option['credit_type']]);
  1614. }
  1615. $r['body'] = $body;
  1616. $total_fee = $the_order_price;
  1617. // 创建订单
  1618. $this->createOrder($uid,6,$body,$total_fee,$total_fee,$orderSn,$parame);
  1619. }else{
  1620. return [
  1621. 'code'=>200004,
  1622. 'msg' => '付费推广小于0.01,无法支付'
  1623. ];
  1624. }
  1625. }else if( $type == 7){
  1626. // 付费圈子
  1627. if(_empty_(_array_key($parame, 'circle_id', 0))){
  1628. return [
  1629. 'code'=>200004,
  1630. 'msg' => '缺少circle_id参数'
  1631. ];
  1632. }
  1633. $circle_id = $parame['circle_id'];
  1634. $circle = WxCircle::find($circle_id);
  1635. if(_empty_($circle)){
  1636. return [
  1637. 'code'=>200003,
  1638. 'msg' => '圈子实际不存在或状态不对'
  1639. ];
  1640. }
  1641. $follow_pay_credit = $circle->follow_pay_credit;
  1642. if($follow_pay_credit != 1){
  1643. return [
  1644. 'code'=>200004,
  1645. 'msg' => '改接口仅处理余额付费圈子'
  1646. ];
  1647. }
  1648. if($circle->follow_pay_num >= 0.01){
  1649. $body = StrUtils::limit('加入圈子('. $circle->circle_name . '/' . $circle->follow_pay_duration . '天)', 20);
  1650. $r['body'] = $body;
  1651. $total_fee = $circle->follow_pay_num;
  1652. $parame = serialize($parame);
  1653. // 创建订单
  1654. $this->createOrder($uid,7,$body,$total_fee,$total_fee,$orderSn,$parame);
  1655. }else{
  1656. return [
  1657. 'code'=>200004,
  1658. 'msg' => '圈子付费数额小于0.01,无法支付'
  1659. ];
  1660. }
  1661. }else if( $type == 8){
  1662. $body = '店铺保证金充值';
  1663. $r['body'] = $body;
  1664. $total_fee = $parame['recharge'];
  1665. $this->createOrder($uid,8,$body,$total_fee,$total_fee,$orderSn);
  1666. }else if( $type == 9){
  1667. // 购买模块
  1668. if(get_mini_domain() == 'mini.minisns.cn'){
  1669. // todo:
  1670. $body = '购买模块'.$parame['num'];
  1671. $r['body'] = $body;
  1672. $model_paycode_map = [
  1673. 1 => 2473,
  1674. 2 => 2474,
  1675. 3 => 2477,
  1676. 4 => 2475,
  1677. 5 => 2472,
  1678. 6 => 2476,
  1679. 7 => 2478,
  1680. 8 => 2479,
  1681. 9 => 2480,
  1682. 10 => 2481,
  1683. 11 => 2482,
  1684. 12 => 2483,
  1685. 13 => 2733,
  1686. 14 => 2484,
  1687. 15 => 2794,
  1688. 16 => 2680,
  1689. 17 => 2492,
  1690. 18 => 2551,
  1691. 19 => 2573,
  1692. 20 => 2669,
  1693. 21 => 2715,
  1694. 22 => 2719,
  1695. 23 => 2722,
  1696. 24 => 2729,
  1697. 25 => 2776,
  1698. 26 => 2740,
  1699. 27 => 2748,
  1700. 28 => 2797,
  1701. 29 => 2805,
  1702. 30 => 2817,
  1703. 31 => 2824,
  1704. 32 => 2886,
  1705. 33 => 2895,
  1706. 34 => 2897,
  1707. 36 => 2908,
  1708. 0 => 2899
  1709. ];
  1710. $total_fee = WxShopGoodsProduct::where('goods_id', $model_paycode_map[$parame['num']])->value('price');
  1711. // user_id 应对利用域名查询不出来的情况
  1712. $this->createOrder($uid,9,$body,$total_fee,$total_fee,$orderSn, serialize(['num'=>$parame['num'], 'domain'=>_array_key($parame, 'domain', ''), 'user_id'=>_array_key($parame, 'user_id', 0)]));
  1713. }
  1714. }else if( $type == 10){
  1715. global $__MINI_GLOBAL_CURRENT_USER_ID__;
  1716. $user_id = $parame['user_id'];
  1717. if(_empty_($user_id)){
  1718. return [
  1719. 'code'=>200001,
  1720. 'msg' => '缺少user_id参数'
  1721. ];
  1722. }
  1723. $front_user_state = $parame['user_state'];
  1724. $the_user = WxUser::find($user_id);
  1725. if(_empty_($the_user)){
  1726. return [
  1727. 'code'=>200004,
  1728. 'msg' => 'user_id无效'
  1729. ];
  1730. }
  1731. if($front_user_state != $the_user->user_state){
  1732. return [
  1733. 'code'=>200004,
  1734. 'msg' => '参数检验不合法,请刷新重试'
  1735. ];
  1736. }
  1737. $total_fee = 0;
  1738. $body = '';
  1739. if($the_user->user_state === 2){
  1740. $ban_until = get_user_meta($the_user->id, 'ban_until', 's');
  1741. if(time() > $ban_until){
  1742. // 解除封号
  1743. if(UserUtils::unlock_user($the_user, 2)){
  1744. $the_user->state_tip = '正常用户';
  1745. $the_user->user_state = 0;
  1746. }
  1747. }
  1748. if($ban_until && $the_user->user_state === 2){
  1749. $ban_until_ = Carbon::createFromTimestamp($ban_until);
  1750. $secondsDifference = $ban_until_->diffInSeconds(Carbon::now());
  1751. $hoursDifference = round($secondsDifference / 3600, 2);
  1752. $tmp_tenant_id = -1;
  1753. if($__MINI_GLOBAL_TENANT_ID__ != $the_user->tenant_id){
  1754. $tmp_tenant_id = $__MINI_GLOBAL_TENANT_ID__;
  1755. $__MINI_GLOBAL_TENANT_ID__ = $the_user->tenant_id;
  1756. }
  1757. $total_fee = round($hoursDifference * Settings::get('app_unban_one_hour_balance', 1.0, true), 2);
  1758. if($tmp_tenant_id > 0){
  1759. $__MINI_GLOBAL_TENANT_ID__ = $tmp_tenant_id;
  1760. }
  1761. if($the_user->id == $__MINI_GLOBAL_CURRENT_USER_ID__){
  1762. $body = '自助解封账号';
  1763. }else{
  1764. $body = '帮他人(uid:'.$the_user->id.')解封账号';
  1765. }
  1766. }
  1767. }else if($the_user->user_state === 3){
  1768. $mute_until = get_user_meta($the_user->id, 'mute_until', 's');
  1769. if(time() > $mute_until){
  1770. // 解除禁言
  1771. if(UserUtils::unlock_user($the_user, 3)){
  1772. $the_user->state_tip = '正常用户';
  1773. $the_user->user_state = 0;
  1774. }
  1775. }
  1776. if($mute_until && $the_user->user_state === 3){
  1777. $mute_until_ = Carbon::createFromTimestamp($mute_until);
  1778. $secondsDifference = $mute_until_->diffInSeconds(Carbon::now());
  1779. $hoursDifference = round($secondsDifference / 3600, 2);
  1780. global $__MINI_GLOBAL_TENANT_ID__;
  1781. $tmp_tenant_id = -1;
  1782. if($__MINI_GLOBAL_TENANT_ID__ != $the_user->tenant_id){
  1783. $tmp_tenant_id = $__MINI_GLOBAL_TENANT_ID__;
  1784. $__MINI_GLOBAL_TENANT_ID__ = $the_user->tenant_id;
  1785. }
  1786. $total_fee = round($hoursDifference * Settings::get('app_unmute_one_hour_balance', 0.5, true), 2);
  1787. if($tmp_tenant_id > 0){
  1788. $__MINI_GLOBAL_TENANT_ID__ = $tmp_tenant_id;
  1789. }
  1790. if($the_user->id == $__MINI_GLOBAL_CURRENT_USER_ID__){
  1791. $body = '自助解除禁言';
  1792. }else{
  1793. $body = '帮他人(uid:'.$the_user->id.')解除禁言';
  1794. }
  1795. }
  1796. }else{
  1797. return [
  1798. 'code'=>200009,
  1799. 'msg' => '无法解除'
  1800. ];
  1801. }
  1802. $r['body'] = $body;
  1803. if(_empty_($total_fee) || _empty_($body)){
  1804. return [
  1805. 'code'=>200009,
  1806. 'msg' => '无需解除'
  1807. ];
  1808. }
  1809. $this->createOrder($uid,10,$body,$total_fee,$total_fee,$orderSn, serialize(['user_id'=>$the_user->id, 'user_state'=>$the_user->user_state]));
  1810. }else if( $type == 11){
  1811. global $__MINI_GLOBAL_CURRENT_USER_ID__;
  1812. $object_user_id = $parame['object_id'];
  1813. if(_empty_($object_user_id)){
  1814. return [
  1815. 'code'=>200001,
  1816. 'msg' => '缺少user_id参数'
  1817. ];
  1818. }
  1819. $the_user = WxUser::find($object_user_id);
  1820. if(_empty_($the_user)){
  1821. return [
  1822. 'code'=>200004,
  1823. 'msg' => 'user_id无效'
  1824. ];
  1825. }
  1826. $is_wechat_account = $parame['is_wechat_account'];
  1827. $is_phone = $parame['is_phone'];
  1828. if(_empty_($is_wechat_account, true)){
  1829. return [
  1830. 'code'=>200001,
  1831. 'msg' => '缺少is_wechat_account参数'
  1832. ];
  1833. }
  1834. if(_empty_($is_phone, true)){
  1835. return [
  1836. 'code'=>200001,
  1837. 'msg' => '缺少is_phone参数'
  1838. ];
  1839. }
  1840. if($the_user->id == $__MINI_GLOBAL_CURRENT_USER_ID__){
  1841. return [
  1842. 'code'=>200004,
  1843. 'msg' => '双方账号不能相同'
  1844. ];
  1845. }
  1846. $contact_apply = WxUserContactApply::where('user_id', $__MINI_GLOBAL_CURRENT_USER_ID__)->where('object_id', $the_user->id)->first();
  1847. $user_settings = WxUserSetting::where('user_id', $the_user->id)->first();
  1848. if(_empty_($user_settings)){
  1849. return [
  1850. 'code'=>200004,
  1851. 'msg' => '请退出登录,重新登录一下试试'
  1852. ];
  1853. }
  1854. if($contact_apply){
  1855. if($contact_apply->status == 0){
  1856. return [
  1857. 'code'=>200000,
  1858. 'msg' => '你已经申请过了该用户的联系方式'
  1859. ];
  1860. }
  1861. }
  1862. $total_fee = 0;
  1863. $body = '';
  1864. if(_empty_($contact_apply) || $contact_apply->status == 2){
  1865. if($is_wechat_account == 1){
  1866. if($user_settings->privacy_get_my_wechat != 5){
  1867. return [
  1868. 'code'=>200000,
  1869. 'msg' => '该用户未开放微信号'
  1870. ];
  1871. }else{
  1872. $total_fee += $user_settings->privacy_get_my_wechat_pay;
  1873. if(!_empty_($body)){
  1874. $body .= '、';
  1875. }
  1876. $body .= 'v';
  1877. }
  1878. }
  1879. if($is_phone == 1){
  1880. if($user_settings->privacy_get_my_phone != 5){
  1881. return [
  1882. 'code'=>200000,
  1883. 'msg' => '该用户未开放手机号'
  1884. ];
  1885. }else{
  1886. $total_fee += $user_settings->privacy_get_my_phone_pay;
  1887. if(!_empty_($body)){
  1888. $body .= '、';
  1889. }
  1890. $body .= 'tel';
  1891. }
  1892. }
  1893. }else{
  1894. if($is_wechat_account == 1){
  1895. if($contact_apply->is_wechat_account != 1){
  1896. if($user_settings->privacy_get_my_wechat != 5){
  1897. return [
  1898. 'code'=>200000,
  1899. 'msg' => '该用户未开放微信号'
  1900. ];
  1901. }else{
  1902. $total_fee += $user_settings->privacy_get_my_wechat_pay;
  1903. if(!_empty_($body)){
  1904. $body .= '、';
  1905. }
  1906. $body .= 'v';
  1907. }
  1908. }else{
  1909. $is_wechat_account = 0;
  1910. }
  1911. }
  1912. if($is_phone == 1){
  1913. if($contact_apply->is_phone != 1){
  1914. if($user_settings->privacy_get_my_phone != 5){
  1915. return [
  1916. 'code'=>200000,
  1917. 'msg' => '该用户未开放手机号'
  1918. ];
  1919. }else{
  1920. $total_fee += $user_settings->privacy_get_my_phone_pay;
  1921. if(!_empty_($body)){
  1922. $body .= '、';
  1923. }
  1924. $body .= 'tel';
  1925. }
  1926. }else{
  1927. $is_phone = 0;
  1928. }
  1929. }
  1930. }
  1931. $reason = StrUtils::limit(strip_tags(trim(_empty_default_(_array_key($parame, 'reason', ''), ''))));
  1932. $body = '用户(id:'.$the_user->id.')的'.$body;
  1933. $r['body'] = $body;
  1934. $this->createOrder($uid,11,$body,$total_fee,$total_fee,$orderSn, serialize(['object_id'=>$the_user->id, 'is_wechat_account'=>$is_wechat_account == 1 ? 1 : 0, 'is_phone' => $is_phone == 1 ? 1 : 0, 'reason'=>$reason]));
  1935. }
  1936. DB::commit();
  1937. $r['code'] = 200;
  1938. $r['orderSn'] = $orderSn;
  1939. $r['total_fee'] = $total_fee;
  1940. return $r;
  1941. } catch (\Exception $e) {
  1942. DB::rollBack();
  1943. _logger_(__file__, __line__, $e->getTrace());
  1944. $r['code'] = 200002;
  1945. return $r;
  1946. }
  1947. }
  1948. /**
  1949. * 统一下单地址
  1950. * @param Request $request
  1951. * @param $type 类型:1:会员 类型:2:充电 7:付费圈子
  1952. * @return \Illuminate\Http\JsonResponse
  1953. */
  1954. public function order(Request $request)
  1955. {
  1956. $uid = $request->uid;
  1957. $r = $this->_create_order($request);
  1958. if($r['code'] != 200){
  1959. return $this->fail($r['code'], [], _array_key($r, 'msg', ''));
  1960. }
  1961. $openid = WxUser::where('id', $uid)->value('weixin_openid');
  1962. $appid = Settings::get('app_id');
  1963. $mch_id = Settings::get('mch_id');
  1964. $key = Settings::get('mch_secret');
  1965. $out_trade_no = $r['orderSn'];
  1966. if(_empty_($openid)){
  1967. return $this->fail(200043, [
  1968. 'title' => '未绑定微信',
  1969. 'content' => '还没有获取到您的小程序openId,无法拉起支付',
  1970. 'confirmText' => '去绑定',
  1971. 'target_type' => 6,
  1972. 'target_id' => '/pagesA/mine/editmine/accountbind'
  1973. ], '未绑定微信');
  1974. }
  1975. return $this->payHandler($uid, 'wxpay', 'mini', $r['total_fee'], $r['body'], $out_trade_no, 1);
  1976. // 发起支付
  1977. // $payObject = new WeixinPay($appid, $openid, $mch_id, $key, $out_trade_no, $r['body'], $r['total_fee']);
  1978. // $pay_result = $payObject->pay();
  1979. // if(_array_key($pay_result, 'return_code', '') == 'FAIL'){
  1980. // return $this->fail(200014, [], $pay_result['return_msg']);
  1981. // }else{
  1982. // return $this->success($pay_result);
  1983. // }
  1984. }
  1985. public function orderfun($uid,$data)
  1986. {
  1987. // $r = $this->_create_order($data);
  1988. // 推荐自己
  1989. $body = '推荐自己';
  1990. $data['body'] = '推荐自己';
  1991. $total_fee = $data['price'];
  1992. // $orderSn = Utils::getSn(1);
  1993. $orderSn = $data['order_sn'];
  1994. $parame = serialize($data);
  1995. // 创建订单
  1996. $this->createOrder($uid,15,$body,$total_fee,$total_fee,$orderSn,$parame);
  1997. $openid = WxUser::where('id', $uid)->value('weixin_openid');
  1998. $appid = Settings::get('app_id');
  1999. $mch_id = Settings::get('mch_id');
  2000. $key = Settings::get('mch_secret');
  2001. $out_trade_no = $orderSn;
  2002. if(_empty_($openid)){
  2003. return $this->fail(200043, [
  2004. 'title' => '未绑定微信',
  2005. 'content' => '还没有获取到您的小程序openId,无法拉起支付',
  2006. 'confirmText' => '去绑定',
  2007. 'target_type' => 6,
  2008. 'target_id' => '/pagesA/mine/editmine/accountbind'
  2009. ], '未绑定微信');
  2010. }
  2011. return $this->payHandler($uid, 'wxpay', 'mini', $total_fee, $data['body'], $out_trade_no, 1);
  2012. }
  2013. /**
  2014. * app端下单
  2015. * @param Request $request
  2016. * @return \Illuminate\Http\JsonResponse
  2017. */
  2018. public function orderForApp(Request $request){
  2019. if(!in_array($request->provider, ['wxpay', 'alipay', 'apple'])){
  2020. return $this->fail(200004, [], 'provider参数仅支持wxpay/alipay');
  2021. }
  2022. if(!in_array($request->pay_type, ['app', 'h5', 'yi', 'code', 'code_pc'])){
  2023. return $this->fail(200004, [], 'pay_type参数仅支持app/h5/yi/code');
  2024. }
  2025. $uid = $request->uid;
  2026. if( $request->provider == 'apple' ){
  2027. $parame = $request->parame;
  2028. if(_empty_($parame) || _empty_(_array_key($parame, 'productId', ''))){
  2029. return $this->fail(200001);
  2030. }
  2031. $productId = _array_key($parame, 'productId', '');
  2032. return $this->applePayHandler($uid, $productId);
  2033. }else{
  2034. $r = $this->_create_order($request);
  2035. if($r['code'] != 200){
  2036. return $this->fail($r['code'], [], _array_key($r, 'msg', ''));
  2037. }
  2038. // 共同的资料
  2039. $total_fee = $r['total_fee'];
  2040. $body = $r['body'];
  2041. $out_trade_no = $r['orderSn'];
  2042. $pay_type = $request->pay_type;
  2043. global $__MINI_GLOBAL_DEVICE__;
  2044. if($__MINI_GLOBAL_DEVICE__ == 'h5' && _empty_default_($request->h5_browser, '') == 'wxclient'){
  2045. $pay_type = 'mp';
  2046. }
  2047. return $this->payHandler($uid, $request->provider, $pay_type, $total_fee, $body, $out_trade_no);
  2048. }
  2049. }
  2050. public function applePayHandler($uid, $productId){
  2051. DB::beginTransaction();
  2052. try {
  2053. if(strpos($productId, 'apple_pay_product_recharge') === 0){
  2054. $orderSn = Utils::getSn(1);
  2055. $num = (int)(str_replace('apple_pay_product_recharge_', '', $productId));
  2056. if(!in_array($num, [1, 3, 6, 12, 28, 48, 78, 98, 128, 168, 198, 228, 298])){
  2057. return $this->fail(200004, [], '充值余额'.$num.'未在选项中');
  2058. }
  2059. // 创建订单
  2060. $model = $this->createOrder($uid,3,'余额充值',$num,$num, $orderSn);
  2061. if($model){
  2062. DB::commit();
  2063. return $this->success(['orderId'=>$model->id, 'productId'=>$productId, 'user_id'=>$uid, 'order_number'=>$orderSn]);
  2064. }
  2065. }else if(strpos($productId, 'apple_pay_product_vip') === 0){
  2066. $orderSn = Utils::getSn(1);
  2067. $order_price = (int)(str_replace('apple_pay_product_vip_', '', $productId));
  2068. $vip_options = json_decode(Settings::get('app_vip_options', '[]'), true);
  2069. if(_empty_($vip_options)){
  2070. return $this->fail(200004, [], '管理员暂未配置会员套餐');
  2071. }
  2072. $flag = false;
  2073. $days = 0;
  2074. foreach ($vip_options as $option){
  2075. if($option && $option['price'] == $order_price){
  2076. $flag = true;
  2077. $days = $option['days'];
  2078. break;
  2079. }
  2080. }
  2081. if(!$flag){
  2082. return $this->fail(200004, [], '会员套餐¥'.$order_price.'未在选项中');
  2083. }
  2084. // 创建订单
  2085. $model = $this->createOrder($uid,1,'开通会员'.$days.'天',$order_price,$order_price,$orderSn);
  2086. if($model){
  2087. DB::commit();
  2088. return $this->success(['orderId'=>$model->id, 'productId'=>$productId, 'user_id'=>$uid, 'order_number'=>$orderSn]);
  2089. }
  2090. }
  2091. DB::rollBack();
  2092. return $this->fail(200004);
  2093. } catch (\Exception $e) {
  2094. DB::rollBack();
  2095. _logger_(__file__, __line__, $e->getMessage());
  2096. return $this->fail(200004);
  2097. }
  2098. }
  2099. /**
  2100. * 创建订单
  2101. */
  2102. public function createOrder($user_id,$order_type,$order_information,$order_price,$order_pay_price,$order_number,$parame='')
  2103. {
  2104. $orderModel = new WxOrder();
  2105. $orderModel->user_id = $user_id;
  2106. $orderModel->order_type = $order_type;
  2107. $orderModel->order_information = $order_information;
  2108. $orderModel->order_price = $order_price;
  2109. $orderModel->order_pay_price = $order_pay_price;
  2110. $orderModel->order_number = $order_number;
  2111. $orderModel->parame = $parame;
  2112. $orderModel->save();
  2113. return $orderModel;
  2114. }
  2115. public function processPayPost($uid, $post_id, $scene, $orderSn, $coins_num, $coin_blance, $coin_rate){
  2116. $res_arr = ['code'=>0, 'tip'=>''];
  2117. if(_empty_($post_id) || _empty_($scene)){
  2118. $res_arr['code'] = 200001;
  2119. return $res_arr;
  2120. }
  2121. $post_obj = WxPost::where('id', $post_id)->first(['user_id','pay_content_id', 'circle_id']);
  2122. if(_empty_($post_obj)){
  2123. $res_arr['code'] = 200003;
  2124. return $res_arr;
  2125. }
  2126. $post_user_id = $post_obj->user_id;
  2127. $pay_content_id = $post_obj->pay_content_id;
  2128. $circle_id = $post_obj->circle_id;
  2129. if(_empty_($pay_content_id)){
  2130. $res_arr['code'] = 200004;
  2131. return $res_arr;
  2132. }
  2133. if($uid == $post_user_id){
  2134. $res_arr['code'] = 200038;
  2135. return $res_arr;
  2136. }
  2137. if(WxUserPay::where([
  2138. ['user_id', '=', $uid],
  2139. ['post_id', '=', $post_id],
  2140. ['pay_content_id', '=', $pay_content_id]
  2141. ])->exists()){
  2142. $res_arr['code'] = 200035;
  2143. return $res_arr;
  2144. }
  2145. $pay_content_obj = WxPostsPayContent::find($pay_content_id);
  2146. if(_empty_($pay_content_obj)){
  2147. $res_arr['code'] = 200003;
  2148. return $res_arr;
  2149. }
  2150. $price = $pay_content_obj->price;
  2151. $credit_type = $pay_content_obj->credit_type;
  2152. $user = WxUser::find($uid);
  2153. $user_name = $user->user_name;
  2154. $is_vip = $user->is_member;
  2155. $vip_discount = _between_(Settings::get('app_vip_paycontent_discount', 0), 0, 100);
  2156. $vip_price_tip = '';
  2157. if($vip_discount > 0 && $is_vip){
  2158. if($credit_type == 0){
  2159. $vip_price = (int)($price * ( (100 - $vip_discount) / 100));
  2160. }else{
  2161. $vip_price = round($price * ( (100 - $vip_discount) / 100), 2);
  2162. }
  2163. $vip_price_tip = '会员价';
  2164. }else{
  2165. $vip_price = $price;
  2166. }
  2167. if($credit_type == 0){
  2168. $res_arr['code'] = 200004;
  2169. $res_arr['tip'] = '该接口仅能购买余额付费内容';
  2170. return $res_arr;
  2171. }else if($credit_type == 1){
  2172. // 修改使用金币的数量
  2173. if($vip_price <= $coin_blance){
  2174. $coin_blance = $vip_price;
  2175. $coins_num = (int)($coin_blance * $coin_rate);
  2176. }
  2177. if($coins_num > 0){
  2178. $coin_pay_percent_every = _between_(Settings::get('app_coin_pay_percent_every', 30), 0, 100);
  2179. if($coin_pay_percent_every > 0){
  2180. if( ($vip_price * $coin_pay_percent_every / 100) < $coin_blance ){
  2181. $res_arr['code'] = 200004;
  2182. $res_arr['tip'] = Settings::get('app_coin_name', '硬币').'抵扣不能超过'.$coin_pay_percent_every.'%';
  2183. return $res_arr;
  2184. }
  2185. }
  2186. }
  2187. $balance = UserUtils::user_balance($uid);
  2188. if($balance - $vip_price + $coin_blance < 0){
  2189. $res_arr['code'] = 200012;
  2190. return $res_arr;
  2191. }
  2192. $circle_info = WxCircle::where('id', $circle_id)->first();
  2193. $paycontent_division = 0;
  2194. $circle_user_id = 0;
  2195. $circle_user_reward = 0;
  2196. if($circle_info){
  2197. $paycontent_division = $circle_info->circle_user_paycontent_division;
  2198. $circle_user_id = $circle_info->user_id;
  2199. $circle_user_reward = round($paycontent_division * $price, 2);
  2200. }
  2201. $platform_division = _between_(Settings::get('app_paypost_platform_percent', 0), 0, 100);
  2202. $platform_reward = round($platform_division * $price / 100, 2);
  2203. $orderModel = $this->createOrder($uid,4,'购买付费内容',$vip_price,$vip_price,$orderSn);
  2204. if($coins_num > 0){
  2205. $need_pay_price = $vip_price - $coin_blance;
  2206. if($need_pay_price < 0.01){
  2207. // 金币支付
  2208. $pay_res2 = UserUtils::update_user_coins($uid,3, -$coins_num, '购买了付费内容(id: '.$post_id.')'.$vip_price_tip.'¥'.$vip_price.'('. $coins_num.Settings::get('app_coin_name', '硬币').'抵扣了¥'.$coin_blance.'):'.Settings::get('app_coin_name', '硬币').'-'.$coins_num);
  2209. if($pay_res2){
  2210. if($circle_user_reward >= 0.01 || $platform_reward >= 0.01){
  2211. UserUtils::update_user_financial($post_user_id, 12, ($price - $circle_user_reward - $platform_reward),
  2212. '<a href="/pages/user/user?id='.$uid.'">'.$user_name.'</a>购买了您的付费内容(id:<a href="/pages/sticky/sticky?id='.$post_id.'">'.$post_id.'</a>),获得余额¥'.($price - $circle_user_reward).'('.env('circle_user_call', '圈主').'获得¥'.$circle_user_reward.($platform_reward >= 0.01 ? ',平台获得¥'.$platform_reward : '').')');
  2213. if($circle_user_reward >= 0.01){
  2214. UserUtils::update_user_financial($circle_user_id, 14, $circle_user_reward, '圈子成员的付费内容被购买,订单金额¥'.$price.',您获得分成¥'.$circle_user_reward.'(分成比例:'.($paycontent_division * 100).'%)');
  2215. }
  2216. }else{
  2217. UserUtils::update_user_financial($post_user_id, 12, $price, '<a href="/pages/user/user?id='.$uid.'">'.$user_name.'</a>购买了您的付费内容(id:<a href="/pages/sticky/sticky?id='.$post_id.'">'.$post_id.'</a>),获得余额+'.$price);
  2218. }
  2219. WxOrder::where('order_number', $orderSn)->update(['order_state' => 1]);
  2220. $user_pay = new WxUserPay();
  2221. $user_pay->user_id = $uid;
  2222. $user_pay->post_id = $post_id;
  2223. $user_pay->pay_content_id = $pay_content_id;
  2224. $user_pay->save();
  2225. WxCommentRepositores::add($post_id, 0, 0, $uid, '💰'.$vip_price_tip.'购买了付费内容', '', 1);
  2226. // todo:
  2227. // 返回刷新内容
  2228. $data = WxPost::where('id', $post_id)->get();
  2229. $data = PostsRepositores::postsParame($data, $uid, $scene);
  2230. $res_arr['code'] = 1;
  2231. $res_arr['tip'] = '购买成功! 请手动刷新页面';
  2232. $res_arr['data'] = $data;
  2233. return $res_arr;
  2234. }else{
  2235. $res_arr['code'] = 200011;
  2236. return $res_arr;
  2237. }
  2238. }else{
  2239. $r = UserUtils::update_user_financial($uid, 7, $need_pay_price, '(余额¥'.$need_pay_price.'+'. $coins_num .Settings::get('app_coin_name', '硬币').')'.$vip_price_tip.'购买付费内容(id:<a href="/pages/sticky/sticky?id='.$post_id.'">'.$post_id.'</a>)', $post_id);
  2240. if($r){
  2241. $pay_res2 = UserUtils::update_user_coins($uid,3, -$coins_num, '购买了付费内容(id: '.$post_id.')'.$vip_price_tip.'¥'.$vip_price.'('. $coins_num.Settings::get('app_coin_name', '硬币').'抵扣了¥'.$coin_blance.'):'.Settings::get('app_coin_name', '硬币').'-'.$coins_num);
  2242. if($pay_res2){
  2243. if($circle_user_reward >= 0.01){
  2244. UserUtils::update_user_financial($post_user_id, 12, ($price - $circle_user_reward),
  2245. '<a href="/pages/user/user?id='.$uid.'">'.$user_name.'</a>购买了您的付费内容(id:<a href="/pages/sticky/sticky?id='.$post_id.'">'.$post_id.'</a>),获得余额¥'.($price - $circle_user_reward).'('.env('circle_user_call', '圈主').'获得¥'.$circle_user_reward.')');
  2246. UserUtils::update_user_financial($circle_user_id, 14, $circle_user_reward, '圈子成员的付费内容被购买,订单金额¥'.$price.',您获得分成¥'.$circle_user_reward.'(分成比例:'.($paycontent_division * 100).'%)');
  2247. }else{
  2248. UserUtils::update_user_financial($post_user_id, 12, $price, '「<a href="/pages/user/user?id='.$uid.'">'.$user_name.'</a>购买了您的付费内容(id:<a href="/pages/sticky/sticky?id='.$post_id.'">」'.$post_id.'</a>),获得余额¥'.$price);
  2249. }
  2250. WxOrder::where('order_number', $orderSn)->update(['order_state' => 1]);
  2251. $user_pay = new WxUserPay();
  2252. $user_pay->user_id = $uid;
  2253. $user_pay->post_id = $post_id;
  2254. $user_pay->pay_content_id = $pay_content_id;
  2255. $user_pay->save();
  2256. WxCommentRepositores::add($post_id, 0, 0, $uid, '💰'.$vip_price_tip.'购买了付费内容', '', 1);
  2257. // todo:
  2258. // 返回刷新内容
  2259. $data = WxPost::where('id', $post_id)->get();
  2260. $data = PostsRepositores::postsParame($data, $uid, $scene);
  2261. $res_arr['code'] = 1;
  2262. $res_arr['tip'] = '购买成功! 请手动刷新页面';
  2263. $res_arr['data'] = $data;
  2264. return $res_arr;
  2265. }else{
  2266. $res_arr['code'] = 200011;
  2267. return $res_arr;
  2268. }
  2269. }
  2270. $res_arr['code'] = 200037;
  2271. return $res_arr;
  2272. }
  2273. }else{
  2274. $r = UserUtils::update_user_financial($uid, 7, $vip_price, '余额¥'.$vip_price.$vip_price_tip.'购买了付费内容(id:<a href="/pages/sticky/sticky?id='.$post_id.'">'.$post_id.'</a>)', $post_id);
  2275. if($r){
  2276. if($circle_user_reward >= 0.01){
  2277. UserUtils::update_user_financial($post_user_id, 12, ($price - $circle_user_reward),
  2278. '「<a href="/pages/user/user?id='.$uid.'">'.$user_name.'</a>」购买了您的付费内容(id:<a href="/pages/sticky/sticky?id='.$post_id.'">'.$post_id.'</a>),获得余额¥'.($price - $circle_user_reward).'('.env('circle_user_call', '圈主').'获得¥'.$circle_user_reward.')');
  2279. UserUtils::update_user_financial($circle_user_id, 14, $circle_user_reward, '圈子成员的付费内容被购买,订单金额¥'.$price.',您获得分成¥'.$circle_user_reward.'(分成比例:'.($paycontent_division * 100).'%)');
  2280. }else {
  2281. UserUtils::update_user_financial($post_user_id, 12, $price, '「<a href="/pages/user/user?id=' . $uid . '">' . $user_name . '</a>」购买了您的付费内容(id:<a href="/pages/sticky/sticky?id=' . $post_id . '">' . $post_id . '</a>),获得余额¥' . $price);
  2282. }
  2283. WxOrder::where('order_number', $orderSn)->update(['order_state' => 1]);
  2284. $user_pay = new WxUserPay();
  2285. $user_pay->user_id = $uid;
  2286. $user_pay->post_id = $post_id;
  2287. $user_pay->pay_content_id = $pay_content_id;
  2288. $user_pay->save();
  2289. WxCommentRepositores::add($post_id, 0, 0, $uid, '💰'.$vip_price_tip.'购买了付费内容', '', 1);
  2290. // todo:
  2291. // 返回刷新内容
  2292. $data = WxPost::where('id', $post_id)->get();
  2293. $data = PostsRepositores::postsParame($data, $uid, $scene);
  2294. $res_arr['code'] = 1;
  2295. $res_arr['tip'] = '购买成功! 请手动刷新页面';
  2296. $res_arr['data'] = $data;
  2297. return $res_arr;
  2298. }
  2299. $res_arr['code'] = 200037;
  2300. return $res_arr;
  2301. }
  2302. }
  2303. }
  2304. /** app端公共查询订单状态函数
  2305. * @param $query_state_no
  2306. * @return \Illuminate\Http\JsonResponse
  2307. */
  2308. public function orderAppCommonStatus(Request $request)
  2309. {
  2310. $query_state_no = _empty_default_($request->query_state_no, '');
  2311. if(_empty_($query_state_no)){
  2312. return $this->fail(200001, ['msg'=>'query_state_no为空']);
  2313. }
  2314. if(strpos($query_state_no, '_shoporder_') !== false){
  2315. $order_id = Utils::outTradeNoPre($query_state_no, '');
  2316. //1=未付款 2=已付款 3=已退款
  2317. $shop_order = WxShopOrder::withTrashed()->where('order_id', $order_id)->first();
  2318. if($shop_order){
  2319. $tip = '支付失败';
  2320. if(in_array($shop_order->pay_status, [2])){
  2321. $tip = '支付成功';
  2322. }
  2323. $status_tip = [0=>'正常', 1=>'未发货', 2=>'已发货', 3=>'确认收货', 4=>'已退货', 5=>'取消', 6=>'用户删除', 7=>'退款中', 8=>'自提发货待确定', 9=>'虚拟发货待确定'][$shop_order->status];
  2324. if($shop_order->status == 8){
  2325. ShopOrderDeliverInfoManage::dispatch('query', $shop_order->id, null);
  2326. }else if($shop_order->status == 10){
  2327. ShopOrderDeliverInfoManage::dispatch('virtual-query', $shop_order->id, null);
  2328. }else if($shop_order->status == 9){
  2329. ShopOrderDeliverInfoManage::dispatch('virtual-upload', $shop_order->id, null);
  2330. }
  2331. return $this->success(['tip'=>$tip, 'order_type'=>'shop_order', 'pay_status'=>$shop_order->pay_status, 'status'=>$shop_order->status, 'msg'=>'ok',
  2332. 'serial_number'=>$shop_order->serial_number, 'serial_platform'=>$shop_order->serial_platform,
  2333. 'serial_platform_type'=>$shop_order->serial_platform_type, 'status_tip'=>$status_tip]);
  2334. }else{
  2335. return $this->fail(200003, ['msg'=>'查询不到order_id:'.$order_id.'的商品订单记录']);
  2336. }
  2337. }else if(strpos($query_state_no, '_order_') !== false){
  2338. $order_number = Utils::outTradeNoPre($query_state_no, '');
  2339. // (0未付款,1已付款,1已退款)
  2340. $order = WxOrder::where('order_number', $order_number)->first();
  2341. if($order){
  2342. $tip = '支付失败';
  2343. if(in_array($order->order_state, [1, 3, 4, 5, 6])){
  2344. $tip = '支付成功';
  2345. }
  2346. $status_tip = [0 => '未付款', 1 => '已付款', 2 => '已退款', 3=>'待小程序发货', 4=>'小程序发货中', 5=>'待小程序确定收货', 6=>'小程序确定收货中'][$order->order_state];
  2347. if($order->order_state == 3){
  2348. OrderDeliverInfoManage::dispatch('upload', $order->id);
  2349. }else if($order->order_state == 5){
  2350. OrderDeliverInfoManage::dispatch('query', $order->id);
  2351. }
  2352. // 注意:这里 $status + 1 目的是和商城订单的支付状态统一
  2353. return $this->success(['tip'=>$tip, 'order_type'=>'order', 'pay_status'=>$order->order_state, 'order_state'=>$order->order_state, 'msg'=>'ok',
  2354. 'serial_number'=>$order->order_serial_number, 'serial_platform'=>$order->order_serial_platform,
  2355. 'serial_platform_type'=>$order->order_serial_platform_type, 'status_tip'=>$status_tip]);
  2356. }else{
  2357. return $this->fail(200003, ['msg'=>'查询不到order_number:'.$order_number.'的订单记录']);
  2358. }
  2359. }
  2360. return $this->fail(200004, ['msg'=>'query_state_no不正确']);
  2361. }
  2362. }