RenewController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\set\classes\SetClass;
  4. use biz\sj\classes\SjClass;
  5. use biz\sj\services\MerchantExtendService;
  6. use biz\sj\services\MerchantService;
  7. use bizGhs\shop\classes\RenewClass;
  8. use bizHd\saas\services\RenewService;
  9. use bizHd\saas\services\SetMealService;
  10. use bizHd\user\services\UserService;
  11. use bizHd\wx\classes\WxOpenClass;
  12. use common\components\dict;
  13. use common\components\httpUtil;
  14. use common\components\orderSn;
  15. use common\components\util;
  16. use Yii;
  17. class RenewController extends BaseController
  18. {
  19. //需要续费 ssh 20240426
  20. public function actionNeedRemind()
  21. {
  22. $need = 1;
  23. $shop = $this->shop;
  24. if ($shop->needRenew == 0) {
  25. $need = 0;
  26. }
  27. if ($shop->hasRenew == 1) {
  28. $need = 0;
  29. }
  30. $date = date("Y_m_d");
  31. $staff = $this->shopAdmin;
  32. $staffId = $staff->id ?? 0;
  33. $cacheKey = 'no_remind_' . $date . '_' . $staffId;
  34. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  35. if (!empty($has)) {
  36. $need = 0;
  37. }
  38. $deadline = $shop->deadline ?? '';
  39. $newTime = bcadd(strtotime($deadline), 31536000);
  40. $newDeadline = date("Y-m-d H:i:s", $newTime);
  41. $old = substr($deadline, 0, 11);
  42. $new = substr($newDeadline, 0, 11);
  43. $hasRenew = $shop->hasRenew ?? 0;
  44. util::success(['need' => $need, 'currentDeadline' => $old, 'newDeadline' => $new, 'hasRenew' => $hasRenew]);
  45. }
  46. //当天不需要再提醒 ssh 20240426
  47. public function actionNoRemind()
  48. {
  49. $date = date("Y_m_d");
  50. $staff = $this->shopAdmin;
  51. $staffId = $staff->id ?? 0;
  52. $cacheKey = 'no_remind_' . $date . '_' . $staffId;
  53. $mainId = $this->mainId;
  54. $time = 86400;
  55. if (getenv('YII_ENV') == 'production') {
  56. //莱漫和国恋需要更多提醒次数
  57. if ($mainId == 7704 || $mainId == 65) {
  58. $time = 15000;
  59. }
  60. } else {
  61. if ($mainId == 644) {
  62. $time = 86400;
  63. }
  64. }
  65. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, $time, 'has']);
  66. util::complete();
  67. }
  68. public function actionList()
  69. {
  70. $where = [];
  71. $list = RenewService::getRenewList($where);
  72. util::success($list);
  73. }
  74. //创建续费的订单 ssh 20240425
  75. public function actionCreate()
  76. {
  77. ini_set('date.timezone', 'Asia/Shanghai');
  78. $get = Yii::$app->request->get();
  79. $year = $get['year'] ?? 1;
  80. $orderSn = orderSn::getRenewSn();
  81. $staff = $this->shopAdmin;
  82. $staffId = $staff->id ?? 0;
  83. $shopAdminName = $staff->name ?? '';
  84. $shop = $this->shop;
  85. $shopName = $shop->shopName ?? '';
  86. $shopId = $shop->id ?? 0;
  87. $sjId = $shop->sjId ?? 0;
  88. $sj = SjClass::getById($sjId, true);
  89. $sjName = $sj->name ?? '';
  90. $mainId = $shop->mainId ?? 0;
  91. $pfLevel = $shop->pfLevel ?? 0;
  92. $deadline = $shop->deadline ?? '';
  93. $beforeDeadline = $shop->beforeDeadline ?? '';
  94. if (getenv('YII_ENV') == 'production') {
  95. $unitPrice = $pfLevel == 0 ? 980 : 1980;
  96. if ($mainId == 50) {
  97. $unitPrice = $pfLevel == 0 ? 0.1 : 0.2;
  98. }
  99. } else {
  100. $unitPrice = $pfLevel == 0 ? 0.1 : 0.2;
  101. }
  102. $price = bcmul($unitPrice, $year, 2);
  103. $data = [
  104. 'orderSn' => $orderSn,
  105. 'prePrice' => $price,
  106. 'actPrice' => $price,
  107. 'shopAdminId' => $staffId,
  108. 'shopAdminName' => $shopAdminName,
  109. 'sjId' => $sjId,
  110. 'sjName' => $sjName,
  111. 'mainId' => $mainId,
  112. 'shopId' => $shopId,
  113. 'shopName' => $shopName,
  114. 'pfLevel' => $pfLevel,
  115. 'year' => $year,
  116. 'deadline' => $deadline,
  117. 'beforeDeadline' => $beforeDeadline,
  118. 'status' => 0,
  119. ];
  120. $ret = RenewClass::add($data, true);
  121. $id = $ret->id ?? 0;
  122. $now = time();
  123. $expireTime = $now + 300;//订单5分钟后过期
  124. $totalFee = $price;
  125. $name = "续费{$year}年";
  126. $admin = $this->admin;
  127. $openId = $admin->ghsMiniOpenId;
  128. $capitalType = dict::getDict('capitalType', 'xhRenew', 'id');
  129. $attach = 'capitalType=' . $capitalType;
  130. $wx = Yii::getAlias("@vendor/weixin");
  131. require_once($wx . '/lib/WxPay.Api.php');
  132. require_once($wx . '/example/WxPay.JsApiPay.php');
  133. $input = new \WxPayUnifiedOrder();
  134. $input->SetBody($name);
  135. $input->SetOut_trade_no($orderSn);
  136. $input->SetTotal_fee($totalFee * 100);
  137. $input->SetTime_start(date("YmdHis", $now));
  138. $input->SetAttach($attach);
  139. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  140. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  141. $input->SetTrade_type("JSAPI");
  142. //花卉宝代为申请的微信支付
  143. $sjExtend = WxOpenClass::getGhsWxInfo();
  144. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  145. $input->SetSub_openid($openId);
  146. } else {
  147. $input->SetOpenid($openId);
  148. }
  149. //小程序使用miniAppId
  150. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  151. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  152. $tools = new \JsApiPay();
  153. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  154. $newParams = json_decode($jsApiParameters, true);
  155. $newParams['id'] = $id;
  156. util::success($newParams);
  157. }
  158. //微信支付购买 ssh 2020.1.11
  159. public function actionWxPay()
  160. {
  161. ini_set('date.timezone', 'Asia/Shanghai');
  162. $shopAdminId = $this->shopAdminId;
  163. $addData['shopAdminId'] = $shopAdminId;
  164. $shopAdmin = $this->shopAdmin->attributes;
  165. $adminName = $shopAdmin['name'] ?? '';
  166. $sjId = $shopAdmin['sjId'];
  167. $addData['shopAdminName'] = $adminName;
  168. $set = SetClass::getByCondition(['style' => SetClass::SET_STYLE_GHS], true, 'id asc');
  169. if (empty($set)) {
  170. util::fail('没有找到套餐');
  171. }
  172. $price = $set->price;
  173. $prePrice = $price;
  174. $actPrice = $price;
  175. $setId = $set->id;
  176. $addData['actPrice'] = $actPrice;
  177. $addData['prePrice'] = $prePrice;
  178. $addData['sjId'] = $this->sjId;
  179. $now = time();
  180. $expireTime = $now + 300;//订单5分钟后过期
  181. $addData['deadline'] = date("Y-m-d H:i:s", $expireTime);
  182. $order = RenewService::addOrder($addData);
  183. $orderId = $order['id'];
  184. $orderSn = $order['orderSn'];
  185. $couponId = 0;
  186. $name = $set->name . '开通及续费';
  187. $totalFee = $actPrice;
  188. $admin = $this->admin->attributes;
  189. //小程序使用miniOpenId
  190. $openId = $admin['ghsMiniOpenId'];
  191. $capitalType = dict::getDict('capitalType', 'xhRenew', 'id');
  192. //将流水类型、优惠卷传过去
  193. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&setId=' . $setId . '&sjId=' . $sjId;
  194. $wx = Yii::getAlias("@vendor/weixin");
  195. require_once($wx . '/lib/WxPay.Api.php');
  196. require_once($wx . '/example/WxPay.JsApiPay.php');
  197. $input = new \WxPayUnifiedOrder();
  198. $input->SetBody($name);
  199. $input->SetOut_trade_no($orderSn);
  200. $input->SetTotal_fee($totalFee * 100);
  201. $input->SetTime_start(date("YmdHis", $now));
  202. $input->SetAttach($attach);
  203. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  204. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  205. $input->SetTrade_type("JSAPI");
  206. //花卉宝代为申请的微信支付
  207. $sjExtend = WxOpenClass::getGhsWxInfo();
  208. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  209. $input->SetSub_openid($openId);
  210. } else {
  211. $input->SetOpenid($openId);
  212. }
  213. //小程序使用miniAppId
  214. if (httpUtil::isMiniProgram()) {
  215. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  216. }
  217. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  218. $tools = new \JsApiPay();
  219. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  220. $newParams = json_decode($jsApiParameters, true);
  221. $newParams['orderId'] = $orderId;
  222. $newParams['sjId'] = $this->sjId;
  223. util::success($newParams);
  224. }
  225. }