OrderController.php 133 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591
  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. } else if ($type == 101) {
  1936. //咨询订单 ask_order
  1937. } else if ($type == 102) {
  1938. //旁听订单 ask_sit_order
  1939. }
  1940. DB::commit();
  1941. $r['code'] = 200;
  1942. $r['orderSn'] = $orderSn;
  1943. $r['total_fee'] = $total_fee;
  1944. return $r;
  1945. } catch (\Exception $e) {
  1946. DB::rollBack();
  1947. _logger_(__file__, __line__, $e->getTrace());
  1948. $r['code'] = 200002;
  1949. return $r;
  1950. }
  1951. }
  1952. /**
  1953. * 统一下单地址
  1954. * @param Request $request
  1955. * @param $type 类型:1:会员 类型:2:充电 7:付费圈子
  1956. * @return \Illuminate\Http\JsonResponse
  1957. */
  1958. public function order(Request $request)
  1959. {
  1960. $uid = $request->uid;
  1961. $r = $this->_create_order($request);
  1962. if($r['code'] != 200){
  1963. return $this->fail($r['code'], [], _array_key($r, 'msg', ''));
  1964. }
  1965. $openid = WxUser::where('id', $uid)->value('weixin_openid');
  1966. $appid = Settings::get('app_id');
  1967. $mch_id = Settings::get('mch_id');
  1968. $key = Settings::get('mch_secret');
  1969. $out_trade_no = $r['orderSn'];
  1970. if(_empty_($openid)){
  1971. return $this->fail(200043, [
  1972. 'title' => '未绑定微信',
  1973. 'content' => '还没有获取到您的小程序openId,无法拉起支付',
  1974. 'confirmText' => '去绑定',
  1975. 'target_type' => 6,
  1976. 'target_id' => '/pagesA/mine/editmine/accountbind'
  1977. ], '未绑定微信');
  1978. }
  1979. return $this->payHandler($uid, 'wxpay', 'mini', $r['total_fee'], $r['body'], $out_trade_no, 1);
  1980. // 发起支付
  1981. // $payObject = new WeixinPay($appid, $openid, $mch_id, $key, $out_trade_no, $r['body'], $r['total_fee']);
  1982. // $pay_result = $payObject->pay();
  1983. // if(_array_key($pay_result, 'return_code', '') == 'FAIL'){
  1984. // return $this->fail(200014, [], $pay_result['return_msg']);
  1985. // }else{
  1986. // return $this->success($pay_result);
  1987. // }
  1988. }
  1989. public function orderfun($uid,$data)
  1990. {
  1991. // $r = $this->_create_order($data);
  1992. // 推荐自己
  1993. $body = '推荐自己';
  1994. $data['body'] = '推荐自己';
  1995. $total_fee = $data['price'];
  1996. // $orderSn = Utils::getSn(1);
  1997. $orderSn = $data['order_sn'];
  1998. $parame = serialize($data);
  1999. // 创建订单
  2000. $this->createOrder($uid,15,$body,$total_fee,$total_fee,$orderSn,$parame);
  2001. $openid = WxUser::where('id', $uid)->value('weixin_openid');
  2002. $appid = Settings::get('app_id');
  2003. $mch_id = Settings::get('mch_id');
  2004. $key = Settings::get('mch_secret');
  2005. $out_trade_no = $orderSn;
  2006. if(_empty_($openid)){
  2007. return $this->fail(200043, [
  2008. 'title' => '未绑定微信',
  2009. 'content' => '还没有获取到您的小程序openId,无法拉起支付',
  2010. 'confirmText' => '去绑定',
  2011. 'target_type' => 6,
  2012. 'target_id' => '/pagesA/mine/editmine/accountbind'
  2013. ], '未绑定微信');
  2014. }
  2015. return $this->payHandler($uid, 'wxpay', 'mini', $total_fee, $data['body'], $out_trade_no, 1);
  2016. }
  2017. /**
  2018. * app端下单
  2019. * @param Request $request
  2020. * @return \Illuminate\Http\JsonResponse
  2021. */
  2022. public function orderForApp(Request $request){
  2023. if(!in_array($request->provider, ['wxpay', 'alipay', 'apple'])){
  2024. return $this->fail(200004, [], 'provider参数仅支持wxpay/alipay');
  2025. }
  2026. if(!in_array($request->pay_type, ['app', 'h5', 'yi', 'code', 'code_pc'])){
  2027. return $this->fail(200004, [], 'pay_type参数仅支持app/h5/yi/code');
  2028. }
  2029. $uid = $request->uid;
  2030. if( $request->provider == 'apple' ){
  2031. $parame = $request->parame;
  2032. if(_empty_($parame) || _empty_(_array_key($parame, 'productId', ''))){
  2033. return $this->fail(200001);
  2034. }
  2035. $productId = _array_key($parame, 'productId', '');
  2036. return $this->applePayHandler($uid, $productId);
  2037. }else{
  2038. $r = $this->_create_order($request);
  2039. if($r['code'] != 200){
  2040. return $this->fail($r['code'], [], _array_key($r, 'msg', ''));
  2041. }
  2042. // 共同的资料
  2043. $total_fee = $r['total_fee'];
  2044. $body = $r['body'];
  2045. $out_trade_no = $r['orderSn'];
  2046. $pay_type = $request->pay_type;
  2047. global $__MINI_GLOBAL_DEVICE__;
  2048. if($__MINI_GLOBAL_DEVICE__ == 'h5' && _empty_default_($request->h5_browser, '') == 'wxclient'){
  2049. $pay_type = 'mp';
  2050. }
  2051. return $this->payHandler($uid, $request->provider, $pay_type, $total_fee, $body, $out_trade_no);
  2052. }
  2053. }
  2054. public function applePayHandler($uid, $productId){
  2055. DB::beginTransaction();
  2056. try {
  2057. if(strpos($productId, 'apple_pay_product_recharge') === 0){
  2058. $orderSn = Utils::getSn(1);
  2059. $num = (int)(str_replace('apple_pay_product_recharge_', '', $productId));
  2060. if(!in_array($num, [1, 3, 6, 12, 28, 48, 78, 98, 128, 168, 198, 228, 298])){
  2061. return $this->fail(200004, [], '充值余额'.$num.'未在选项中');
  2062. }
  2063. // 创建订单
  2064. $model = $this->createOrder($uid,3,'余额充值',$num,$num, $orderSn);
  2065. if($model){
  2066. DB::commit();
  2067. return $this->success(['orderId'=>$model->id, 'productId'=>$productId, 'user_id'=>$uid, 'order_number'=>$orderSn]);
  2068. }
  2069. }else if(strpos($productId, 'apple_pay_product_vip') === 0){
  2070. $orderSn = Utils::getSn(1);
  2071. $order_price = (int)(str_replace('apple_pay_product_vip_', '', $productId));
  2072. $vip_options = json_decode(Settings::get('app_vip_options', '[]'), true);
  2073. if(_empty_($vip_options)){
  2074. return $this->fail(200004, [], '管理员暂未配置会员套餐');
  2075. }
  2076. $flag = false;
  2077. $days = 0;
  2078. foreach ($vip_options as $option){
  2079. if($option && $option['price'] == $order_price){
  2080. $flag = true;
  2081. $days = $option['days'];
  2082. break;
  2083. }
  2084. }
  2085. if(!$flag){
  2086. return $this->fail(200004, [], '会员套餐¥'.$order_price.'未在选项中');
  2087. }
  2088. // 创建订单
  2089. $model = $this->createOrder($uid,1,'开通会员'.$days.'天',$order_price,$order_price,$orderSn);
  2090. if($model){
  2091. DB::commit();
  2092. return $this->success(['orderId'=>$model->id, 'productId'=>$productId, 'user_id'=>$uid, 'order_number'=>$orderSn]);
  2093. }
  2094. }
  2095. DB::rollBack();
  2096. return $this->fail(200004);
  2097. } catch (\Exception $e) {
  2098. DB::rollBack();
  2099. _logger_(__file__, __line__, $e->getMessage());
  2100. return $this->fail(200004);
  2101. }
  2102. }
  2103. /**
  2104. * 创建订单
  2105. */
  2106. public function createOrder($user_id,$order_type,$order_information,$order_price,$order_pay_price,$order_number,$parame='')
  2107. {
  2108. $orderModel = new WxOrder();
  2109. $orderModel->user_id = $user_id;
  2110. $orderModel->order_type = $order_type;
  2111. $orderModel->order_information = $order_information;
  2112. $orderModel->order_price = $order_price;
  2113. $orderModel->order_pay_price = $order_pay_price;
  2114. $orderModel->order_number = $order_number;
  2115. $orderModel->parame = $parame;
  2116. $orderModel->save();
  2117. return $orderModel;
  2118. }
  2119. public function processPayPost($uid, $post_id, $scene, $orderSn, $coins_num, $coin_blance, $coin_rate){
  2120. $res_arr = ['code'=>0, 'tip'=>''];
  2121. if(_empty_($post_id) || _empty_($scene)){
  2122. $res_arr['code'] = 200001;
  2123. return $res_arr;
  2124. }
  2125. $post_obj = WxPost::where('id', $post_id)->first(['user_id','pay_content_id', 'circle_id']);
  2126. if(_empty_($post_obj)){
  2127. $res_arr['code'] = 200003;
  2128. return $res_arr;
  2129. }
  2130. $post_user_id = $post_obj->user_id;
  2131. $pay_content_id = $post_obj->pay_content_id;
  2132. $circle_id = $post_obj->circle_id;
  2133. if(_empty_($pay_content_id)){
  2134. $res_arr['code'] = 200004;
  2135. return $res_arr;
  2136. }
  2137. if($uid == $post_user_id){
  2138. $res_arr['code'] = 200038;
  2139. return $res_arr;
  2140. }
  2141. if(WxUserPay::where([
  2142. ['user_id', '=', $uid],
  2143. ['post_id', '=', $post_id],
  2144. ['pay_content_id', '=', $pay_content_id]
  2145. ])->exists()){
  2146. $res_arr['code'] = 200035;
  2147. return $res_arr;
  2148. }
  2149. $pay_content_obj = WxPostsPayContent::find($pay_content_id);
  2150. if(_empty_($pay_content_obj)){
  2151. $res_arr['code'] = 200003;
  2152. return $res_arr;
  2153. }
  2154. $price = $pay_content_obj->price;
  2155. $credit_type = $pay_content_obj->credit_type;
  2156. $user = WxUser::find($uid);
  2157. $user_name = $user->user_name;
  2158. $is_vip = $user->is_member;
  2159. $vip_discount = _between_(Settings::get('app_vip_paycontent_discount', 0), 0, 100);
  2160. $vip_price_tip = '';
  2161. if($vip_discount > 0 && $is_vip){
  2162. if($credit_type == 0){
  2163. $vip_price = (int)($price * ( (100 - $vip_discount) / 100));
  2164. }else{
  2165. $vip_price = round($price * ( (100 - $vip_discount) / 100), 2);
  2166. }
  2167. $vip_price_tip = '会员价';
  2168. }else{
  2169. $vip_price = $price;
  2170. }
  2171. if($credit_type == 0){
  2172. $res_arr['code'] = 200004;
  2173. $res_arr['tip'] = '该接口仅能购买余额付费内容';
  2174. return $res_arr;
  2175. }else if($credit_type == 1){
  2176. // 修改使用金币的数量
  2177. if($vip_price <= $coin_blance){
  2178. $coin_blance = $vip_price;
  2179. $coins_num = (int)($coin_blance * $coin_rate);
  2180. }
  2181. if($coins_num > 0){
  2182. $coin_pay_percent_every = _between_(Settings::get('app_coin_pay_percent_every', 30), 0, 100);
  2183. if($coin_pay_percent_every > 0){
  2184. if( ($vip_price * $coin_pay_percent_every / 100) < $coin_blance ){
  2185. $res_arr['code'] = 200004;
  2186. $res_arr['tip'] = Settings::get('app_coin_name', '硬币').'抵扣不能超过'.$coin_pay_percent_every.'%';
  2187. return $res_arr;
  2188. }
  2189. }
  2190. }
  2191. $balance = UserUtils::user_balance($uid);
  2192. if($balance - $vip_price + $coin_blance < 0){
  2193. $res_arr['code'] = 200012;
  2194. return $res_arr;
  2195. }
  2196. $circle_info = WxCircle::where('id', $circle_id)->first();
  2197. $paycontent_division = 0;
  2198. $circle_user_id = 0;
  2199. $circle_user_reward = 0;
  2200. if($circle_info){
  2201. $paycontent_division = $circle_info->circle_user_paycontent_division;
  2202. $circle_user_id = $circle_info->user_id;
  2203. $circle_user_reward = round($paycontent_division * $price, 2);
  2204. }
  2205. $platform_division = _between_(Settings::get('app_paypost_platform_percent', 0), 0, 100);
  2206. $platform_reward = round($platform_division * $price / 100, 2);
  2207. $orderModel = $this->createOrder($uid,4,'购买付费内容',$vip_price,$vip_price,$orderSn);
  2208. if($coins_num > 0){
  2209. $need_pay_price = $vip_price - $coin_blance;
  2210. if($need_pay_price < 0.01){
  2211. // 金币支付
  2212. $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);
  2213. if($pay_res2){
  2214. if($circle_user_reward >= 0.01 || $platform_reward >= 0.01){
  2215. UserUtils::update_user_financial($post_user_id, 12, ($price - $circle_user_reward - $platform_reward),
  2216. '<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 : '').')');
  2217. if($circle_user_reward >= 0.01){
  2218. UserUtils::update_user_financial($circle_user_id, 14, $circle_user_reward, '圈子成员的付费内容被购买,订单金额¥'.$price.',您获得分成¥'.$circle_user_reward.'(分成比例:'.($paycontent_division * 100).'%)');
  2219. }
  2220. }else{
  2221. 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);
  2222. }
  2223. WxOrder::where('order_number', $orderSn)->update(['order_state' => 1]);
  2224. $user_pay = new WxUserPay();
  2225. $user_pay->user_id = $uid;
  2226. $user_pay->post_id = $post_id;
  2227. $user_pay->pay_content_id = $pay_content_id;
  2228. $user_pay->save();
  2229. WxCommentRepositores::add($post_id, 0, 0, $uid, '💰'.$vip_price_tip.'购买了付费内容', '', 1);
  2230. // todo:
  2231. // 返回刷新内容
  2232. $data = WxPost::where('id', $post_id)->get();
  2233. $data = PostsRepositores::postsParame($data, $uid, $scene);
  2234. $res_arr['code'] = 1;
  2235. $res_arr['tip'] = '购买成功! 请手动刷新页面';
  2236. $res_arr['data'] = $data;
  2237. return $res_arr;
  2238. }else{
  2239. $res_arr['code'] = 200011;
  2240. return $res_arr;
  2241. }
  2242. }else{
  2243. $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);
  2244. if($r){
  2245. $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);
  2246. if($pay_res2){
  2247. if($circle_user_reward >= 0.01){
  2248. UserUtils::update_user_financial($post_user_id, 12, ($price - $circle_user_reward),
  2249. '<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.')');
  2250. UserUtils::update_user_financial($circle_user_id, 14, $circle_user_reward, '圈子成员的付费内容被购买,订单金额¥'.$price.',您获得分成¥'.$circle_user_reward.'(分成比例:'.($paycontent_division * 100).'%)');
  2251. }else{
  2252. 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);
  2253. }
  2254. WxOrder::where('order_number', $orderSn)->update(['order_state' => 1]);
  2255. $user_pay = new WxUserPay();
  2256. $user_pay->user_id = $uid;
  2257. $user_pay->post_id = $post_id;
  2258. $user_pay->pay_content_id = $pay_content_id;
  2259. $user_pay->save();
  2260. WxCommentRepositores::add($post_id, 0, 0, $uid, '💰'.$vip_price_tip.'购买了付费内容', '', 1);
  2261. // todo:
  2262. // 返回刷新内容
  2263. $data = WxPost::where('id', $post_id)->get();
  2264. $data = PostsRepositores::postsParame($data, $uid, $scene);
  2265. $res_arr['code'] = 1;
  2266. $res_arr['tip'] = '购买成功! 请手动刷新页面';
  2267. $res_arr['data'] = $data;
  2268. return $res_arr;
  2269. }else{
  2270. $res_arr['code'] = 200011;
  2271. return $res_arr;
  2272. }
  2273. }
  2274. $res_arr['code'] = 200037;
  2275. return $res_arr;
  2276. }
  2277. }else{
  2278. $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);
  2279. if($r){
  2280. if($circle_user_reward >= 0.01){
  2281. UserUtils::update_user_financial($post_user_id, 12, ($price - $circle_user_reward),
  2282. '「<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.')');
  2283. UserUtils::update_user_financial($circle_user_id, 14, $circle_user_reward, '圈子成员的付费内容被购买,订单金额¥'.$price.',您获得分成¥'.$circle_user_reward.'(分成比例:'.($paycontent_division * 100).'%)');
  2284. }else {
  2285. 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);
  2286. }
  2287. WxOrder::where('order_number', $orderSn)->update(['order_state' => 1]);
  2288. $user_pay = new WxUserPay();
  2289. $user_pay->user_id = $uid;
  2290. $user_pay->post_id = $post_id;
  2291. $user_pay->pay_content_id = $pay_content_id;
  2292. $user_pay->save();
  2293. WxCommentRepositores::add($post_id, 0, 0, $uid, '💰'.$vip_price_tip.'购买了付费内容', '', 1);
  2294. // todo:
  2295. // 返回刷新内容
  2296. $data = WxPost::where('id', $post_id)->get();
  2297. $data = PostsRepositores::postsParame($data, $uid, $scene);
  2298. $res_arr['code'] = 1;
  2299. $res_arr['tip'] = '购买成功! 请手动刷新页面';
  2300. $res_arr['data'] = $data;
  2301. return $res_arr;
  2302. }
  2303. $res_arr['code'] = 200037;
  2304. return $res_arr;
  2305. }
  2306. }
  2307. }
  2308. /** app端公共查询订单状态函数
  2309. * @param $query_state_no
  2310. * @return \Illuminate\Http\JsonResponse
  2311. */
  2312. public function orderAppCommonStatus(Request $request)
  2313. {
  2314. $query_state_no = _empty_default_($request->query_state_no, '');
  2315. if(_empty_($query_state_no)){
  2316. return $this->fail(200001, ['msg'=>'query_state_no为空']);
  2317. }
  2318. if(strpos($query_state_no, '_shoporder_') !== false){
  2319. $order_id = Utils::outTradeNoPre($query_state_no, '');
  2320. //1=未付款 2=已付款 3=已退款
  2321. $shop_order = WxShopOrder::withTrashed()->where('order_id', $order_id)->first();
  2322. if($shop_order){
  2323. $tip = '支付失败';
  2324. if(in_array($shop_order->pay_status, [2])){
  2325. $tip = '支付成功';
  2326. }
  2327. $status_tip = [0=>'正常', 1=>'未发货', 2=>'已发货', 3=>'确认收货', 4=>'已退货', 5=>'取消', 6=>'用户删除', 7=>'退款中', 8=>'自提发货待确定', 9=>'虚拟发货待确定'][$shop_order->status];
  2328. if($shop_order->status == 8){
  2329. ShopOrderDeliverInfoManage::dispatch('query', $shop_order->id, null);
  2330. }else if($shop_order->status == 10){
  2331. ShopOrderDeliverInfoManage::dispatch('virtual-query', $shop_order->id, null);
  2332. }else if($shop_order->status == 9){
  2333. ShopOrderDeliverInfoManage::dispatch('virtual-upload', $shop_order->id, null);
  2334. }
  2335. return $this->success(['tip'=>$tip, 'order_type'=>'shop_order', 'pay_status'=>$shop_order->pay_status, 'status'=>$shop_order->status, 'msg'=>'ok',
  2336. 'serial_number'=>$shop_order->serial_number, 'serial_platform'=>$shop_order->serial_platform,
  2337. 'serial_platform_type'=>$shop_order->serial_platform_type, 'status_tip'=>$status_tip]);
  2338. }else{
  2339. return $this->fail(200003, ['msg'=>'查询不到order_id:'.$order_id.'的商品订单记录']);
  2340. }
  2341. }else if(strpos($query_state_no, '_order_') !== false){
  2342. $order_number = Utils::outTradeNoPre($query_state_no, '');
  2343. // (0未付款,1已付款,1已退款)
  2344. $order = WxOrder::where('order_number', $order_number)->first();
  2345. if($order){
  2346. $tip = '支付失败';
  2347. if(in_array($order->order_state, [1, 3, 4, 5, 6])){
  2348. $tip = '支付成功';
  2349. }
  2350. $status_tip = [0 => '未付款', 1 => '已付款', 2 => '已退款', 3=>'待小程序发货', 4=>'小程序发货中', 5=>'待小程序确定收货', 6=>'小程序确定收货中'][$order->order_state];
  2351. if($order->order_state == 3){
  2352. OrderDeliverInfoManage::dispatch('upload', $order->id);
  2353. }else if($order->order_state == 5){
  2354. OrderDeliverInfoManage::dispatch('query', $order->id);
  2355. }
  2356. // 注意:这里 $status + 1 目的是和商城订单的支付状态统一
  2357. return $this->success(['tip'=>$tip, 'order_type'=>'order', 'pay_status'=>$order->order_state, 'order_state'=>$order->order_state, 'msg'=>'ok',
  2358. 'serial_number'=>$order->order_serial_number, 'serial_platform'=>$order->order_serial_platform,
  2359. 'serial_platform_type'=>$order->order_serial_platform_type, 'status_tip'=>$status_tip]);
  2360. }else{
  2361. return $this->fail(200003, ['msg'=>'查询不到order_number:'.$order_number.'的订单记录']);
  2362. }
  2363. }
  2364. return $this->fail(200004, ['msg'=>'query_state_no不正确']);
  2365. }
  2366. }