_valid([ "shop.require" => "参数错误" ]); if (!is_array($param)) return error($param); $memberCard = (new SaasUser)->where("shop_id",$param['shop'])->where("openid",$request->user['openid'])->with(["shop" => function($query){ $query->field("shop_id,shop_name,user_card_price,user_card"); }])->findOrEmpty(); if ($memberCard->isEmpty()) return success("",[],2); $memberCard['balance'] = format_money($memberCard['balance']/100,2); $memberCard['end_at'] = date("Y-m-d",strtotime($memberCard['create_at'])); $isRecharge = (new SaasUserBuy)->where("shop_id",$param['shop'])->where("openid",$request->user['openid'])->where("status",1)->sum("money"); $cardPrice = []; if ($memberCard['shop']['user_card'] < 3) { if ($memberCard['shop']['user_card'] == 2) { // 自定义套餐 $cardPrice = array_values($memberCard['shop']['user_card_price']); } else { $cardPrice = (new SaasCombo)->where("type",2)->select(); } $cardPrice = array_filter($cardPrice, function($item) use ($isRecharge) { if ($isRecharge > 0) { return $item['is_first'] != '1'; // 注意:这里使用松散比较,因为数据中有字符串'1' } else { return $item; } }); foreach ($cardPrice as $key=>$val) { $cardPrice[$key] = $val; $cardPrice[$key]['money'] = $val['money']; $cardPrice[$key]['old_money'] = $val['old_money']; } $cardPrice = array_values($cardPrice); } $memberCard['card'] = array_values($cardPrice); return success("ok",$memberCard->toArray()); } catch (\Throwable $throwable) { echo getDateFull()."==会员卡报错\n"; echo $throwable->getLine()."\n"; echo $throwable->getFile()."\n"; return error($throwable->getMessage()); } } /** * 会员卡消费明细 * @param Request $request * @return Response */ #[Route(path: "log",methods: "get")] public function getOrderList(Request $request): Response { try { $param = $this->_valid([ "page.require" => "参数错误", "size.require" => "参数错误", "shop.require" => "参数错误", "card.require" => "参数错误" ]); if (!is_array($param)) return error($param); $map = ["openid" => $request->user['openid'],"shop_id" => $param['shop']]; $resp = (new SaasUserLog)->where($map)->order("create_at desc")->paginate([ "list_rows" => $param['size'], "page" => $param['page'] ]); return success("ok",$resp->toArray()); } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } /** * 下单 * @param Request $request * @return Response|void|null */ #[Route(path: "create",methods: "post")] public function createOrder(Request $request) { try { $param = $this->_valid([ "card.require" => "参数错误", "shop.require" => "参数错误" ],"post"); if (!is_array($param)) return error($param); $map = ["openid" => $request->user['openid'],"shop_id" => $param['shop']]; $card = (new SaasUser)->where($map)->with(['shop' => function($query){ $query->field("shop_id,shop_name"); }])->findOrEmpty(); if ($card->isEmpty()) return error("尚未开通会员卡"); return error("充值通道暂时关闭"); $orderSn = strtoupper(CodeExtend::random(12,3)); $buyCard = json_decode($param['card'],true); $state = (new SaasUserBuy)->insertGetId([ "openid" => $request->user['openid'], "order_sn" => $orderSn, "shop_id" => $param['shop'], "card_no" => $card['card_no'], "money" => $buyCard['money'] * 100, "total_money" => $buyCard['money'] * 100 + $buyCard['old_money'] * 100, "remark" => "赠送金额".$buyCard['old_money'] ]); if ($state) { $param_data = []; $param_data["order_no"] = $orderSn; $param_data["app_id"] = sConf("wechat.jhf_appid"); $param_data["pay_channel"] = "wx_lite"; $param_data["pay_amt"] = format_money($buyCard['money'],2); $param_data["goods_title"] = $card['shop']['shop_name']."-会员卡充值"; $param_data["device_info"] = array("device_ip" => $request->getRealIp()); $param_data['notify_url'] = "https://panel.huiyinduo.cn/notify/recharge"; $param_data["expend"] = [ "wx_app_id" => sConf("wechat.mini_appid"), "open_id" => $request->user['openid'] ]; $respJhf = (new Pay)->config([ "appid" => sConf("wechat.jhf_appid"), "mch_id" => sConf("wechat.jhf_mch_id"), "aeskey" => sConf("wechat.jhf_aeskey"), "pubkey" => sConf("wechat.jhf_pubkey"), "prikey" => sConf("wechat.jhf_prikey"), ])->createPay($param_data); if (isset($respJhf['code'])) { return error("发起支付失败"); } // 创建JSAPI参数签名 $resp = json_decode($respJhf['expend']['pay_info'],true); $resp['timestamp'] = $resp['timeStamp']; // $wechat = new \WeChat\Pay($this->getWxConfig()); // $options = [ // 'body' => $card['shop']['shop_name']."-会员卡充值", // 'out_trade_no' => $orderSn."-".CodeExtend::random(8), // "attach" => $orderSn, // 'total_fee' => $buyCard['money'] * 100, // 'openid' => $request->user['openid'], // 'trade_type' => 'JSAPI', // 'notify_url' => 'https://panel.huiyinduo.cn/notify/recharge', // 'spbill_create_ip' => $request->getRealIp(), // ]; // // 生成预支付码 // $result = $wechat->createOrder($options); // // 创建JSAPI参数签名 // $resp = $wechat->createParamsForJsApi($result['prepay_id']); return success("ok",$resp); } return error("发起充值失败"); } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } #[Route(path: "del",methods: "post")] public function delCard(Request $request): Response { try { $param = $this->_valid([ "shop.require" => "参数错误" ],"post"); if (!is_array($param)) return error($param); $map = ["openid" => $request->user['openid'],"shop_id" => $param['shop']]; $card = (new SaasUser)->where($map)->findOrEmpty(); if ($card->isEmpty()) return error("会员卡不存在"); $state = $card->delete(); if ($state) return success("注销成功"); return error("注销失败"); } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } /** * 小程序配置 * @return array */ protected function getWxConfig(): array { return [ 'token' => 'test', 'appid' => sConf("wechat.mini_appid"), 'appsecret' => sConf("wechat.mini_secret"), 'encodingaeskey' => 'BJIUzE0gqlWy0GxfPp4J1oPTBmOrNDIGPNav1YFH5Z5', // 配置商户支付参数(可选,在使用支付功能时需要) 'mch_id' => sConf("wechat.mch_id"), 'mch_key' => sConf("wechat.mch_key") ]; } }