LeagueController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\modules\alliance\controllers;
  3. use app\models\Admin;
  4. use app\models\SaasUser;
  5. use app\models\Store;
  6. use app\models\StoreCash;
  7. use app\modules\alliance\models\LeagueForm;
  8. use app\utils\OrderNo;
  9. use yii\web\Response;
  10. class LeagueController extends BaseController
  11. {
  12. public function beforeAction($action)
  13. {
  14. if (!parent::beforeAction($action)) {
  15. return false; // 如果父类返回false,停止执行
  16. }
  17. $access_token = input_params('access_token');
  18. $saasUser = SaasUser::findOne(['access_token' => $access_token]);
  19. if (!$saasUser) {
  20. \Yii::$app->response->format = Response::FORMAT_JSON;
  21. \Yii::$app->response->data = [
  22. 'code' => 401,
  23. 'msg' => '登录失败'
  24. ];
  25. return false;
  26. }
  27. if (!is_merchant()) {
  28. return true;
  29. }
  30. $admin = Admin::findOne(['saas_user_id' => $saasUser->id, 'is_delete' => 0, 'type' => Admin::ADMIN_TYPE_STORE]);
  31. $store = Store::findOne(['id' => $admin->type_id, 'is_delete' => 0]);
  32. if (!$store) {
  33. \Yii::$app->response->format = Response::FORMAT_JSON;
  34. \Yii::$app->response->data = [
  35. 'code' => 1,
  36. 'msg' => '用户未绑定商城'
  37. ];
  38. return false;
  39. }
  40. $this->store_id = $store->id;
  41. return true;
  42. }
  43. /**
  44. * 联盟券获取金额信息
  45. */
  46. public function actionStoreMoney() {
  47. $store_id = $this->store_id;
  48. $store = Store::findOne($store_id);
  49. return $this->asJson([
  50. 'code' => 0,
  51. 'msg' => '',
  52. 'data' => [
  53. 'league_price' => $store->league_price,
  54. ]
  55. ]);
  56. }
  57. /**
  58. * 联盟券获取支付方式
  59. */
  60. /**
  61. * 联盟券提现
  62. */
  63. public function actionCashApply() {
  64. $price = post_params('cash');
  65. $type = post_params('type');
  66. $saas_user = get_saas_user();
  67. if (!$price || !in_array($type, ['alipay', 'bank_card', 'wechat', 'wechat_auto'])) {
  68. return $this->asJson([
  69. 'code' => 1,
  70. 'msg' => '参数非法',
  71. ]);
  72. }
  73. $store_id = $this->store_id;
  74. $store = Store::findOne($store_id);
  75. if ($price > $store->league_price) {
  76. return $this->asJson([
  77. 'code' => 1,
  78. 'msg' => '提现金额不能大于联盟券余额',
  79. ]);
  80. }
  81. $t = \Yii::$app->db->beginTransaction();
  82. try {
  83. $cash = new StoreCash();
  84. $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_CASH);
  85. $cash->is_delete = 0;
  86. $cash->status = 0;
  87. $cash->price = $price;
  88. $cash->created_at = time();
  89. $cash->user_id = 0;
  90. $cash->store_id = $store->id;
  91. $cash->saas_id = $saas_user->id;
  92. $withdraw_method = json_decode($saas_user->withdraw_method, true);
  93. if ($withdraw_method) {
  94. $withdraw_method = array_column($withdraw_method, null, 'type');
  95. if ($type == 'wechat') {
  96. $cash->type = 0;
  97. $cash->name = $withdraw_method['wechat']['name'];
  98. $cash->mobile = $withdraw_method['wechat']['account'];
  99. }
  100. if ($type == 'alipay') {
  101. $cash->type = 1;
  102. $cash->name = $withdraw_method['alipay']['name'];
  103. $cash->mobile = $withdraw_method['alipay']['account'];
  104. }
  105. if ($type == 'bank_card') {
  106. $cash->type = 2;
  107. $cash->name = $withdraw_method['bank_card']['name'];
  108. $cash->mobile = $withdraw_method['bank_card']['account'];
  109. $cash->bank_name = $withdraw_method['bank_card']['bank'];
  110. }
  111. }
  112. if ($type == 'wechat_auto') {
  113. $cash->type = 4; // 微信自动打款
  114. }
  115. $cash->pay_time = 0;
  116. $cash->cash_type = StoreCash::CASH_TYPE_ALLIANCE_COUPON;
  117. if (!$cash->save()) {
  118. throw new \Exception('申请失败');
  119. }
  120. $before = $store->league_price;
  121. $store->updateCounters(['league_price' => -floatval($price)]);
  122. \app\models\SaaSLeaguePriceLog::setLeaguePriceLog(
  123. $store->id,
  124. $cash->saas_id,
  125. $cash->price,
  126. $before,
  127. \app\models\SaaSLeaguePriceLog::TYPE_WITHDRAW,
  128. \app\models\SaaSLeaguePriceLog::TAKE_TYPE,
  129. \app\models\SaaSLeaguePriceLog::ROLE_STORE,
  130. $cash->id
  131. );
  132. $t->commit();
  133. return $this->asJson([
  134. 'code' => 0,
  135. 'msg' => '申请成功',
  136. ]);
  137. } catch (\Exception $e) {
  138. $t->rollBack();
  139. return $this->asJson([
  140. 'code' => 1,
  141. 'msg' => '申请失败',
  142. ]);
  143. }
  144. }
  145. }