Card.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace app\controller\api;
  3. use app\extra\basic\Base;
  4. use app\extra\jhfPay\Pay;
  5. use app\extra\tools\CodeExtend;
  6. use app\middleware\WxMiddleware;
  7. use app\model\saas\SaasCombo;
  8. use app\model\saas\SaasUser;
  9. use app\model\saas\SaasUserBuy;
  10. use app\model\saas\SaasUserLog;
  11. use LinFly\Annotation\Route\Controller;
  12. use LinFly\Annotation\Route\Middleware;
  13. use LinFly\Annotation\Route\Route;
  14. use support\Request;
  15. use support\Response;
  16. #[Controller(prefix: "/wx_api/card"),Middleware(WxMiddleware::class)]
  17. class Card extends Base
  18. {
  19. #[Route(path: "data",methods: "get")]
  20. public function getCardData(Request $request): Response
  21. {
  22. try {
  23. $param = $this->_valid([
  24. "shop.require" => "参数错误"
  25. ]);
  26. if (!is_array($param)) return error($param);
  27. $memberCard = (new SaasUser)->where("shop_id",$param['shop'])->where("openid",$request->user['openid'])->with(["shop" => function($query){
  28. $query->field("shop_id,shop_name,user_card_price,user_card");
  29. }])->findOrEmpty();
  30. if ($memberCard->isEmpty()) return success("",[],2);
  31. $memberCard['balance'] = format_money($memberCard['balance']/100,2);
  32. $memberCard['end_at'] = date("Y-m-d",strtotime($memberCard['create_at']));
  33. $isRecharge = (new SaasUserBuy)->where("shop_id",$param['shop'])->where("openid",$request->user['openid'])->where("status",1)->sum("money");
  34. $cardPrice = [];
  35. if ($memberCard['shop']['user_card'] < 3) {
  36. if ($memberCard['shop']['user_card'] == 2) { // 自定义套餐
  37. $cardPrice = array_values($memberCard['shop']['user_card_price']);
  38. } else {
  39. $cardPrice = (new SaasCombo)->where("type",2)->select();
  40. }
  41. $cardPrice = array_filter($cardPrice, function($item) use ($isRecharge) {
  42. if ($isRecharge > 0) {
  43. return $item['is_first'] != '1'; // 注意:这里使用松散比较,因为数据中有字符串'1'
  44. } else {
  45. return $item;
  46. }
  47. });
  48. foreach ($cardPrice as $key=>$val) {
  49. $cardPrice[$key] = $val;
  50. $cardPrice[$key]['money'] = $val['money'];
  51. $cardPrice[$key]['old_money'] = $val['old_money'];
  52. }
  53. $cardPrice = array_values($cardPrice);
  54. }
  55. $memberCard['card'] = array_values($cardPrice);
  56. return success("ok",$memberCard->toArray());
  57. } catch (\Throwable $throwable) {
  58. echo getDateFull()."==会员卡报错\n";
  59. echo $throwable->getLine()."\n";
  60. echo $throwable->getFile()."\n";
  61. return error($throwable->getMessage());
  62. }
  63. }
  64. /**
  65. * 会员卡消费明细
  66. * @param Request $request
  67. * @return Response
  68. */
  69. #[Route(path: "log",methods: "get")]
  70. public function getOrderList(Request $request): Response
  71. {
  72. try {
  73. $param = $this->_valid([
  74. "page.require" => "参数错误",
  75. "size.require" => "参数错误",
  76. "shop.require" => "参数错误",
  77. "card.require" => "参数错误"
  78. ]);
  79. if (!is_array($param)) return error($param);
  80. $map = ["openid" => $request->user['openid'],"shop_id" => $param['shop']];
  81. $resp = (new SaasUserLog)->where($map)->order("create_at desc")->paginate([
  82. "list_rows" => $param['size'],
  83. "page" => $param['page']
  84. ]);
  85. return success("ok",$resp->toArray());
  86. } catch (\Throwable $throwable) {
  87. return error($throwable->getMessage());
  88. }
  89. }
  90. /**
  91. * 下单
  92. * @param Request $request
  93. * @return Response|void|null
  94. */
  95. #[Route(path: "create",methods: "post")]
  96. public function createOrder(Request $request)
  97. {
  98. try {
  99. $param = $this->_valid([
  100. "card.require" => "参数错误",
  101. "shop.require" => "参数错误"
  102. ],"post");
  103. if (!is_array($param)) return error($param);
  104. $map = ["openid" => $request->user['openid'],"shop_id" => $param['shop']];
  105. $card = (new SaasUser)->where($map)->with(['shop' => function($query){
  106. $query->field("shop_id,shop_name");
  107. }])->findOrEmpty();
  108. if ($card->isEmpty()) return error("尚未开通会员卡");
  109. return error("充值通道暂时关闭");
  110. $orderSn = strtoupper(CodeExtend::random(12,3));
  111. $buyCard = json_decode($param['card'],true);
  112. $state = (new SaasUserBuy)->insertGetId([
  113. "openid" => $request->user['openid'],
  114. "order_sn" => $orderSn,
  115. "shop_id" => $param['shop'],
  116. "card_no" => $card['card_no'],
  117. "money" => $buyCard['money'] * 100,
  118. "total_money" => $buyCard['money'] * 100 + $buyCard['old_money'] * 100,
  119. "remark" => "赠送金额".$buyCard['old_money']
  120. ]);
  121. if ($state) {
  122. $param_data = [];
  123. $param_data["order_no"] = $orderSn;
  124. $param_data["app_id"] = sConf("wechat.jhf_appid");
  125. $param_data["pay_channel"] = "wx_lite";
  126. $param_data["pay_amt"] = format_money($buyCard['money'],2);
  127. $param_data["goods_title"] = $card['shop']['shop_name']."-会员卡充值";
  128. $param_data["device_info"] = array("device_ip" => $request->getRealIp());
  129. $param_data['notify_url'] = "https://panel.huiyinduo.cn/notify/recharge";
  130. $param_data["expend"] = [
  131. "wx_app_id" => sConf("wechat.mini_appid"),
  132. "open_id" => $request->user['openid']
  133. ];
  134. $respJhf = (new Pay)->config([
  135. "appid" => sConf("wechat.jhf_appid"),
  136. "mch_id" => sConf("wechat.jhf_mch_id"),
  137. "aeskey" => sConf("wechat.jhf_aeskey"),
  138. "pubkey" => sConf("wechat.jhf_pubkey"),
  139. "prikey" => sConf("wechat.jhf_prikey"),
  140. ])->createPay($param_data);
  141. if (isset($respJhf['code'])) {
  142. return error("发起支付失败");
  143. }
  144. // 创建JSAPI参数签名
  145. $resp = json_decode($respJhf['expend']['pay_info'],true);
  146. $resp['timestamp'] = $resp['timeStamp'];
  147. // $wechat = new \WeChat\Pay($this->getWxConfig());
  148. // $options = [
  149. // 'body' => $card['shop']['shop_name']."-会员卡充值",
  150. // 'out_trade_no' => $orderSn."-".CodeExtend::random(8),
  151. // "attach" => $orderSn,
  152. // 'total_fee' => $buyCard['money'] * 100,
  153. // 'openid' => $request->user['openid'],
  154. // 'trade_type' => 'JSAPI',
  155. // 'notify_url' => 'https://panel.huiyinduo.cn/notify/recharge',
  156. // 'spbill_create_ip' => $request->getRealIp(),
  157. // ];
  158. // // 生成预支付码
  159. // $result = $wechat->createOrder($options);
  160. // // 创建JSAPI参数签名
  161. // $resp = $wechat->createParamsForJsApi($result['prepay_id']);
  162. return success("ok",$resp);
  163. }
  164. return error("发起充值失败");
  165. } catch (\Throwable $throwable) {
  166. return error($throwable->getMessage());
  167. }
  168. }
  169. #[Route(path: "del",methods: "post")]
  170. public function delCard(Request $request): Response
  171. {
  172. try {
  173. $param = $this->_valid([
  174. "shop.require" => "参数错误"
  175. ],"post");
  176. if (!is_array($param)) return error($param);
  177. $map = ["openid" => $request->user['openid'],"shop_id" => $param['shop']];
  178. $card = (new SaasUser)->where($map)->findOrEmpty();
  179. if ($card->isEmpty()) return error("会员卡不存在");
  180. $state = $card->delete();
  181. if ($state) return success("注销成功");
  182. return error("注销失败");
  183. } catch (\Throwable $throwable) {
  184. return error($throwable->getMessage());
  185. }
  186. }
  187. /**
  188. * 小程序配置
  189. * @return array
  190. */
  191. protected function getWxConfig(): array
  192. {
  193. return [
  194. 'token' => 'test',
  195. 'appid' => sConf("wechat.mini_appid"),
  196. 'appsecret' => sConf("wechat.mini_secret"),
  197. 'encodingaeskey' => 'BJIUzE0gqlWy0GxfPp4J1oPTBmOrNDIGPNav1YFH5Z5',
  198. // 配置商户支付参数(可选,在使用支付功能时需要)
  199. 'mch_id' => sConf("wechat.mch_id"),
  200. 'mch_key' => sConf("wechat.mch_key")
  201. ];
  202. }
  203. }