| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\controllers;
- use app\constants\OptionSetting;
- use app\jobs\LgCashJob;
- use app\models\Cash;
- use app\models\CashExt;
- use app\models\Color;
- use app\models\Lg;
- use app\models\Option;
- use app\models\OrderDetail;
- use app\models\Qrcode;
- use app\models\Share;
- use app\models\ShareGroupPurchaseUser;
- use app\models\Shop;
- use app\models\ShopShare;
- use app\models\TeamGrades;
- use app\models\User;
- use app\models\UserShareMoney;
- use app\models\WechatConfig;
- use app\modules\admin\models\CashForm;
- use app\modules\admin\models\CashListForm;
- use app\modules\admin\models\QrcodeForm;
- use app\modules\admin\models\ShareListForm;
- use app\modules\admin\models\ShareOrderForm;
- use app\modules\admin\models\ShareSettingForm;
- use app\utils\Alipay\Alipay;
- use app\utils\LgApi;
- use app\utils\Notice\NoticeSend;
- use yii\helpers\Json;
- use app\models\SaasUser;
- use app\models\AccountLog;
- use app\models\Store;
- use app\models\StoreMoneyLog;
- use app\modules\admin\models\RechargeForm;
- use app\modules\client\models\v1\RcCommissionForm;
- class ShareController extends BaseController
- {
- /**
- * @return string
- * 分销商列表
- */
- public function actionList()
- {
- $store_id = get_store_id();
- $form = new ShareListForm();
- $form->attributes = get_params();
- $form->store_id = $store_id;
- return $this->asJson($form->getList());
- }
- /**
- * 设置分销商等级
- */
- public function actionSetShareLevel()
- {
- $store_id = get_store_id();
- $form = new ShareListForm();
- $form->attributes = post_params();
- $form->store_id = $store_id;
- return $this->asJson($form->setShareLevel());
- }
- /**
- * @return mixed|string
- * 佣金设置
- */
- public function actionBasicSetting()
- {
- $store_id = get_store_id();
- $shareOption = Option::findOne([
- 'store_id' => $store_id, 'group' => OptionSetting::SHARE_GROUP_NAME,
- 'name' => OptionSetting::SHARE_BASIC_SETTING
- ]);
- if (!$shareOption) {
- $shareOption = new Option();
- }
- $form = new ShareSettingForm();
- $form->data = post_params();
- $form->model = $shareOption;
- $form->store_id = get_store_id();
- return $this->asJson($form->setBasicSetting());
- }
- /**
- * @return mixed|string
- * 佣金设置
- */
- public function actionSaasBasicSetting()
- {
- $store_id = -1;
- $shareOption = Option::findOne([
- 'store_id' => $store_id, 'group' => OptionSetting::SHARE_GROUP_NAME,
- 'name' => OptionSetting::SHARE_BASIC_SETTING
- ]);
- if (!$shareOption) {
- $shareOption = new Option();
- }
- $form = new ShareSettingForm();
- $form->data = post_params();
- $form->model = $shareOption;
- $form->store_id = $store_id;
- return $this->asJson($form->setSaasBasicSetting());
- }
- /**
- * @return mixed|string
- * 佣金设置
- */
- public function actionMoneySetting()
- {
- $first_name = post_params('first_name');
- if (empty($first_name)) {
- $first_name = '一级';
- }
- $second_name = post_params('second_name');
- if (empty($second_name)) {
- $second_name = '二级';
- }
- $third_name = post_params('third_name');
- if (empty($third_name)) {
- $third_name = '三级';
- }
- $expire_day = !empty(post_params('expire_day')) ? post_params('expire_day') : 0;
- $scale = !empty(post_params('scale')) ? post_params('scale') : 0;
- // 佣金类型:1.会员佣金,2.订单佣金
- $commission_type = !empty(post_params('commission_type')) ? post_params('commission_type') : '';
- // 佣金发放类型:0.佣金,1.积分,2.余额
- $giveType = !empty(post_params('giveType')) ? post_params('giveType') : '';
-
- $value = [
- 'level_one' => [
- 'text' => $first_name,
- 'value' => post_params('first'),
- ],
- 'level_two' => [
- 'text' => $second_name,
- 'value' => post_params('second'),
- ],
- 'level_three' => [
- 'text' => $third_name,
- 'value' => post_params('third'),
- ],
- 'commission_type' => $commission_type,
- 'giveType' => $giveType,
- 'expire_day' => $expire_day,
- 'scale' => $scale,
- ];
- if (
- !is_numeric(post_params('first')) || !is_numeric(post_params('second')) ||
- !is_numeric(post_params('third'))
- ) {
- return $this->asJson(['code' => 1, 'msg' => '佣金请输入数字!']);
- }
- $store_id = get_store_id();
- $shareOption = Option::findOne([
- 'store_id' => $store_id, 'group' => OptionSetting::SHARE_GROUP_NAME,
- 'name' => OptionSetting::SHARE_MONEY_SETTING
- ]);
- if (!$shareOption) {
- $shareOption = new Option();
- }
- $form = new ShareSettingForm();
- $form->data = $value;
- $form->model = $shareOption;
- $form->store_id = $store_id;
- return $this->asJson($form->setMoneySetting());
- }
- /**
- * 分销商详情
- */
- public function actionDetail()
- {
- $form = new ShareOrderForm();
- $form->attributes = post_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->detail());
- }
- /**
- * 分销商订单
- */
- public function actionShareOrder()
- {
- $form = new ShareOrderForm();
- $form->attributes = get_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->shareOrder());
- }
- /**
- * @return mixed|string
- * 佣金设置获取
- */
- public function actionGetShareSetting()
- {
- $form = new ShareSettingForm();
- $form->name = get_params('name');
- $form->store_id = get_store_id();
- return $this->asJson($form->getShareSetting());
- }
- /**
- * @return mixed|string
- * saas佣金设置获取
- */
- public function actionGetSaasShareSetting()
- {
- $form = new ShareSettingForm();
- $form->name = get_params('name');
- $form->store_id = -1;
- return $this->asJson($form->getShareSetting());
- }
- /**
- * 添加备注
- * @return array
- */
- public function actionSellerComments()
- {
- $id = post_params('id');
- $seller_comments = post_params('seller_comments');
- $share = Share::findOne([
- 'store_id' => get_store_id(), 'is_delete' => Share::SHARE_NOT_DELETE,
- 'id' => $id
- ]);
- $share->seller_comments = $seller_comments;
- if ($share->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '保存成功'
- ]);
- } else {
- $this->asJson(['code' => 1, 'msg' => '保存异常']);
- }
- }
- /**
- * @param int $id
- * @param int $status
- * @return mixed|string
- * @DateTime 2021-12-09
- * 申请审核
- */
- public function actionStatus()
- {
- $id = post_params('id');
- $status = intval(post_params('status'));
- $store_id = get_store_id();
- if (is_array($id)) {
- $share = Share::find()->where(['in','id',$id, 'is_delete' => Share::SHARE_NOT_DELETE, 'store_id' => $store_id])->asArray()->all();
- } else {
- $share = Share::findOne(['id' => $id, 'is_delete' => Share::SHARE_NOT_DELETE, 'store_id' => $store_id]);
- }
-
- if (!$share) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '网络异常,请刷新重试'
- ]);
- }
-
- if (!in_array($status, [1, 2])) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '网络异常,请刷新重试'
- ]);
- }
-
- if ($status == 1) {
- if (is_array($share)) {
- $shares = SHare::updateAll(['status' => $status, 'created_at' => time()], ['in', 'id', $id]);
- foreach ($share as &$val) {
- User::updateAll(['time' => time(), 'is_distributor' => User::IS_DISTRIBUTOR], ['id' => $val['user_id'],'store_id' => $store_id]);
- NoticeSend::ShareExamine($val['user_id'], $val['mobile'], '通过', '分销商');
- }
-
- } else {
- $shares = SHare::updateAll(['status' => $status, 'created_at' => time()], ['=', 'id', $id]);
- User::updateAll(['time' => time(), 'is_distributor' => User::IS_DISTRIBUTOR], ['id' => $share->user_id,'store_id' => $store_id]);
- NoticeSend::ShareExamine($share->user_id, $share->mobile, '通过', '分销商');
- }
-
- } else {
- if (is_array($share)) {
- $shares = SHare::updateAll(['status' => $status, 'created_at' => time()], ['in', 'id', $id]);
- foreach ($share as &$val) {
- User::updateAll(['time' => time(), 'is_distributor' => User::NOT_DISTRIBUTOR], ['id' => $val['user_id'],'store_id' => $store_id]);
- NoticeSend::ShareExamine($val['user_id'], $val['mobile'], '不通过', '分销商');
- }
-
- } else {
- $shares = SHare::updateAll(['status' => $status, 'created_at' => time()], ['=', 'id', $id]);
- User::updateAll(['time' => time(), 'is_distributor' => User::NOT_DISTRIBUTOR], [
- 'id' => $share->user_id,
- 'store_id' => $store_id
- ]);
- NoticeSend::ShareExamine($share->user_id, $share->mobile, '不通过', '分销商');
- }
-
- }
- if ($shares > 0 ) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功'
- ]);
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '请刷新重试'
- ]);
- }
- }
- /**
- * @param int $id
- * @return mixed|string
- * @throws \yii\db\Exception
- * 删除分销商
- */
- public function actionDel()
- {
- $id = post_params('id');
- $share = Share::findOne(['id' => $id, 'is_delete' => Share::SHARE_NOT_DELETE]);
- if (!$share) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '网络异常'
- ]);
- }
- $t = \Yii::$app->db->beginTransaction();
- $count1 = Share::updateAll(['is_delete' => Share::SHARE_IS_DELETE], 'id=:id', [':id' => $id]);
- $user = User::findOne($share->user_id);
- $user->is_distributor = User::NOT_DISTRIBUTOR;
- // $user->parent_id = 0;
- // $user->old_parent_id = 0;
- $user->time = time();
- $user->save();
- // User::updateAll(['parent_id' => 0], 'parent_id=:parent_id', [':parent_id' => $share->user_id]);
- if ($count1 != 0) {
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功'
- ]);
- } else {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '网络异常'
- ]);
- }
- }
- /**
- * 分销提现列表
- * @return \yii\web\Response
- */
- public function actionCash()
- {
- $store_id = get_store_id();
- $form = new CashForm();
- $form->attributes = all_params();
- $form->store_id = $store_id;
- if(get_mch_id()){
- $form->mch = 1;
- $form->mch_id = get_mch_id();
- $form->cash_type = Cash::IS_CASH_TYPE_MCH;
- }
- return $this->asJson($form->getShareCashList());
- }
- /**
- * 用户分销提现列表
- */
- public function actionUserCash()
- {
- $store_id = get_store_id();
- $form = new CashForm();
- $form->attributes = get_params();
- $form->store_id = $store_id;
- return $this->asJson($form->shareCashUserList());
- }
- /**
- * 用户分销提现列表
- */
- public function actionSaasCash()
- {
- $store_id = -1; //saascash store_id 固定为-1
- $form = new CashForm();
- $form->attributes = get_params();
- $form->store_id = $store_id;
- return $this->asJson($form->shareCashSaasList());
- }
- /**
- * 修改分销商备注
- */
- public function actionSetShareSeller()
- {
- $id = post_params('id');
- $seller_comments = post_params('seller_comments');
- $share = Share::findOne(['id' => (int)$id]);
- if (!$share) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '分销商不存在'
- ]);
- }
- $share->seller_comments = $seller_comments;
- if ($share->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '保存成功'
- ]);
- }
- }
- /**
- * 提现申请审核
- * @return \yii\web\Response
- */
- public function actionApply()
- {
- $id = post_params('id');
- $status = post_params('status');
- $store_id = get_store_id();
- $cash = Cash::findOne([
- 'id' => $id, 'is_delete' => Cash::IS_DELETE_NO,
- 'store_id' => $store_id
- ]);
- if (!$cash) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提现记录不存在,请刷新重试'
- ]);
- }
- if (!$cash->order_no) {
- $order_no = null;
- while (true) {
- $order_no = date('YmdHis') . mt_rand(100000, 999999);
- $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
- if (!$exist_order_no) {
- break;
- }
- }
- $cash->order_no = $order_no;
- $cash->save();
- }
- \Yii::$app->cache->set('cash_cache_' . $id, $cash->order_no);
- if (!in_array($status, [1, 3])) {
- \Yii::$app->cache->set('cash_cache_' . $id, false);
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提现记录已审核,请刷新重试'
- ]);
- }
- $cash->status = $status;
- if ($status == Cash::STATUS_REFUSE) {
- $user = User::findOne(['id' => $cash->user_id]);
- // if (! $user) {
- // return $this->asJson([
- // 'code' => 1,
- // 'msg' => '用户数据不存在'
- // ]);
- // }
- cash::cashRefuse($cash->id);
- NoticeSend::CashFail($cash->user_id, $user->binding, $cash->price, '提现被驳回', '提现被驳回');
- }
- if ($cash->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功'
- ]);
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '网络异常,请刷新重试'
- ]);
- }
- }
- /**
- * 商盟saas提现申请审核
- * @return \yii\web\Response
- */
- public function actionSaasApply()
- {
- $id = post_params('id');
- $status = post_params('status');
- $store_id = -1; //saas 商盟店铺固定id为-1
- $cash = Cash::findOne([
- 'id' => $id, 'is_delete' => Cash::IS_DELETE_NO,
- 'store_id' => $store_id
- ]);
- if (!$cash) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提现记录不存在,请刷新重试'
- ]);
- }
- if (!$cash->order_no) {
- $order_no = null;
- while (true) {
- $order_no = date('YmdHis') . mt_rand(100000, 999999);
- $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
- if (!$exist_order_no) {
- break;
- }
- }
- $cash->order_no = $order_no;
- $cash->save();
- }
- \Yii::$app->cache->set('cash_cache_' . $id, $cash->order_no);
- if (!in_array($status, [1, 3])) {
- \Yii::$app->cache->set('cash_cache_' . $id, false);
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提现记录已审核,请刷新重试'
- ]);
- }
- $cash->status = $status;
- if ($status == Cash::STATUS_REFUSE) {
- $saas = SaasUser::findOne(['id' => $cash->saas_id]);
- if (! $saas) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '用户数据不存在'
- ]);
- }
- if ($cash->cash_type == 0) {
- $saas->price += $cash->price;
- if (!$saas->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '网络异常'
- ]);
- }
- }
- // else {
- // $user->tuan_price += $cash->price;
- // if (!$user->save()) {
- // return $this->asJson([
- // 'code' => 1,
- // 'msg' => '网络异常'
- // ]);
- // } else {
- // $shop = Shop::findOne(['user_id'=> $user->id, 'store_id' => $user->store_id, 'is_delete' => 0,'shop_audit' => 1]);
- // $shop_form = new ShopShare();
- // $shop_form->store_id = $user->store_id;
- // $shop_form->shop_id = $shop->id;
- // $shop_form->type = 0;
- // $shop_form->amount = $cash->price;
- // $shop_form->the_desc = '团长'.$user->nickname.'提现驳回';
- // $shop_form->created_at = time();
- // $shop_form->status = 1;
- // $shop_form->save();
- // }
- // }
- //NoticeSend::CashFail($cash->user_id, $user->binding, $cash->price, '提现被驳回', '提现被驳回');
- }
- if ($cash->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功'
- ]);
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '网络异常,请刷新重试'
- ]);
- }
- }
- public function actionConfirm()
- {
- $id = post_params('id');
- $status = post_params('status');
- $store_id = get_store_id();
- $store = Store::findOne($store_id);
- $cash = Cash::findOne([
- 'id' => $id,
- 'is_delete' => Cash::IS_DELETE_NO,
- 'store_id' => $store_id
- ]);
- if (!$cash) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提现记录不存在,请刷新重试'
- ]);
- }
- if ($status == Cash::STATUS_GIVEN) {
- if($store->is_platform_transfers == 1){
- $store_wechat_cash = json_decode(Option::get('store_wechat_cash', $store_id, 'store')['value'], true);
- $val = 0;
- foreach ((array)$store_wechat_cash as $value) {
- if (!empty($value)) {
- $val = 1;
- break;
- }
- }
- if(!$val){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '未配置平台微信提现设置'
- ]);
- }
- }
- }
- if (!$cash->order_no) {
- $order_no = null;
- while (true) {
- $order_no = date('YmdHis') . mt_rand(100000, 999999);
- $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
- if (!$exist_order_no) {
- break;
- }
- }
- $cash->order_no = $order_no;
- $cash->save();
- }
- if ($cash->status != 1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作错误,请刷新重试'
- ]);
- }
- $res = [];
- $cashExt = CashExt::findOne(['cash_id' => $cash['id'], 'cash_price_type' => CashExt::CASH_PRICE_TYPE_AMOUNT]);
- if ($cashExt) {
- $price = $cashExt->real_price;
- } else {
- $price = Cash::getServiceMoney($cash);
- }
- $servePrice = $cash->price * ($cash->service_charge / 100);
- if ($store->store_balance < $price && $store->is_platform_transfers == 1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '店铺剩余提现余额不足'
- ]);
- }
- $wechat_type = 1;
- $wechat_cash = Option::get('wechat_cash', $store_id, 'store')['value'];
- if($store->is_platform_transfers == 1){
- $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value'];
- $wechat_type = 0;
- }
- $t = \Yii::$app->db->beginTransaction();
- if ($status == Cash::STATUS_GIVEN) { //微信自动打款
- $cash->status = Cash::STATUS_GIVEN;
- $cash->pay_time = time();
- $cash->pay_type = Cash::PAY_TYPE_WX;
- $cash->wx_cash_status = -1;
- $user = User::findOne(['id' => $cash->user_id]);
- $data = [
- 'partner_trade_no' => $cash->order_no,
- 'openid' => $user->wechat_open_id,
- 'check_name' => 'NO_CHECK',
- 'amount' => $price * 100,
- 'desc' => '转账',
- 'user_name' => $cash->name
- ];
- if ((int)$cash->type === 0) {
- if (\Yii::$app->prod_is_dandianpu() && !\app\models\Store::hasIncoming($store_id)) {
- //商城是否进件
- //商城余额是否充足
- $storeMoney = \app\models\StoreCash::getMaxCash($store);
- if($storeMoney < $price){
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作错误,商城余额不足! 当前余额:¥' . $storeMoney
- ]);
- }
- //扣除商城余额
- $subMoney = \app\models\Store::subMoney($store, $price);
- if(!$subMoney){
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '网络异常,subMoney失败1',
- ]);
- }
- } else {
- if (\Yii::$app->prod_is_duli()) {
- // $WechatConfig = WechatConfig::findOne(['store_id' => get_store_id()]);
- // if (empty($WechatConfig->mch_id) || empty($WechatConfig->pay_key) || empty($WechatConfig->app_id)|| empty($WechatConfig->cert_pem)|| empty($WechatConfig->key_pem)) {
- // return $this->asJson([
- // 'code' => 1,
- // 'msg' => "后台参数配置错误,请检查参数后重试"
- // ]);
- // }
- }
- }
- if ($wechat_type == 0) {
- $cashExt = CashExt::findOne(['cash_id' => $cash->id, 'cash_price_type' => CashExt::CASH_PRICE_TYPE_AMOUNT]);
- $before = $store->store_balance;
- $store->store_balance -= $cashExt ? $cashExt->real_price : $price;
- $store->price -= $cashExt ? $cashExt->real_price : $price;
- $store->store_withdrawn_cash += $cashExt ? $cashExt->real_price : $price;
- $after = $store->store_balance;
- $cashExtServePrice = $cashExt ? bcsub($cashExt->price, $cashExt->real_price, 2) : $servePrice;
- $cashExtPrice = $cashExt ? $cashExt->price : $cash->price;
- $cashExtRealPrice = $cashExt ? $cashExt->real_price : $price;
- if ($store->save()) {
- StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHARE, $cashExtRealPrice, " ID{$id}:用户提现{$cashExtPrice}元,扣除手续费{$cashExtServePrice}元,提现实际扣除{$cashExtRealPrice}元", $before, $after, $wechat_type);
- }
- }
- $wechat = \Yii::$app->controller->wechatPay;
- // $res = $wechat->transfer->toBalance($data);
- $res = (new \app\utils\WechatMerchant\WxV3($wechat))->transferBatches(get_store_id(), $data);
- //判断是否使用新版本转账 增加转账标识
- $wechat_cash = json_decode($wechat_cash, true);
- if (intval($wechat_cash['is_open']) === 2) {
- if (!$res['code']) {
- $cash->is_platform_transfers = intval($store->is_platform_transfers);
- $cash->wx_cash_type = Cash::WX_CASH_TYPE_NEW;
- $cash->wx_cash_state = $res['data']['state'];
- $cash->wx_cash_result_info = json_encode($res['data'], JSON_UNESCAPED_UNICODE);
- $cash->save();
- }
- }
- NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '微信自动打款', ($cash->price - $price));
- } elseif ((int)$cash->type === 1) {
- $order = (object)[
- 'store_id' => $cash->store_id,
- 'order_no' => $cash->order_no,
- 'pay_price' => sprintf("%.2f", $price),
- 'name' => $cash->name
- ];
- $result = Alipay::transfer($order, $cash->mobile);
- if (isset($result['code']) && $result['code'] == 1) {
- if (strpos($result['msg'], 'aop.invalid-app-auth-token-no-api')) {
- $result['msg'] = '接口未授权,请前往支付宝开放平台查询是否开通产品或授权支付宝转账产品';
- }
- if (strpos($result['msg'], 'PAYEE_NOT_EXIST')) {
- $result['msg'] = '收款账号不存在或姓名有误,建议核实账号和姓名是否准确';
- }
- if (strpos($result['msg'], 'BALANCE_IS_NOT_ENOUGH')) {
- $result['msg'] = '商户余额不足';
- }
- return $this->asJson($result);
- } else {
- // TODO 扣除店铺提现金额
- if ($wechat_type == 0) {
- $cashExt = CashExt::findOne(['cash_id' => $cash->id, 'cash_price_type' => CashExt::CASH_PRICE_TYPE_AMOUNT]);
- $before = $store->store_balance;
- $store->store_balance -= $cashExt ? $cashExt->real_price : $price;
- $store->price -= $cashExt ? $cashExt->real_price : $price;
- $store->store_withdrawn_cash += $cashExt ? $cashExt->real_price : $price;
- $after = $store->store_balance;
- $cashExtServePrice = $cashExt ? bcsub($cashExt->price, $cashExt->real_price, 2) : $servePrice;
- $cashExtPrice = $cashExt ? $cashExt->price : $cash->price;
- $cashExtRealPrice = $cashExt ? $cashExt->real_price : $price;
- if ($store->save()) {
- StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHARE, $cashExtRealPrice, " ID{$id}:用户提现{$cashExtPrice}元,扣除手续费{$cashExtServePrice}元,提现实际扣除{$cashExtRealPrice}元", $before, $after, $wechat_type);
- }
- }
- }
- $cash->save();
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功'
- ]);
- }elseif ((int)$cash->type === 4) {
- $user = User::findOne($cash->user_id);
- $saas = SaasUser::findOne(['mobile' => $user->binding]);
- $lg_info = Lg::find()->where(['user_id'=>$saas->id,'status'=>1,'is_delete'=>0])->one();
- $lgApi = new LgApi($cash->store_id);
- //灵工提现
- $post_data = [
- 'store_id' => $cash->store_id,
- 'outTradeNo' => $cash->order_no,//唯一批次号
- 'accNo' => bcmul($price,100),
- 'amt' => bcmul($price,100),
- 'name' => $lg_info->name,
- 'certCard' => $lg_info->cert_card//身份证号
- ];
- $result = $lgApi->FlexiblePay($post_data);
- if (isset($result['status']) && $result['status'] == 999) {
- return $this->asJson($result);
- }
- $cash->status = 6;//灵工待打款
- $cash->save();
- $t->commit();
- //灵工提现接口调用后消息队列查询状态
- \queue_push(new LgCashJob(['id'=>$cash->id,'store_id'=>$store_id,'type'=>0,'retry' => 5]), 60);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功'
- ]);
- }
- } elseif ($status == Cash::STATUS_HAND) { //手动打款
- $user = User::findOne($cash->user_id);
- // TODO 扣除店铺提现金额
- if ($wechat_type == 0) {
- $cashExt = CashExt::findOne(['cash_id' => $cash->id, 'cash_price_type' => CashExt::CASH_PRICE_TYPE_AMOUNT]);
- $before = $store->store_balance;
- $store->store_balance -= $cashExt ? $cashExt->real_price : $price;
- $store->price -= $cashExt ? $cashExt->real_price : $price;
- $store->store_withdrawn_cash += $cashExt ? $cashExt->real_price : $price;
- $after = $store->store_balance;
- $cashExtServePrice = $cashExt ? bcsub($cashExt->price, $cashExt->real_price, 2) : $servePrice;
- $cashExtPrice = $cashExt ? $cashExt->price : $cash->price;
- $cashExtRealPrice = $cashExt ? $cashExt->real_price : $price;
- if ($store->save()) {
- StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHARE, $cashExtRealPrice, " ID{$id}:用户提现{$cashExtPrice}元,扣除手续费{$cashExtServePrice}元,提现实际扣除{$cashExtRealPrice}元", $before, $after, $wechat_type);
- } else {
- $t->rollBack();
- }
- }
- $cash->status = Cash::STATUS_HAND;
- $cash->pay_time = time();
- $cash->pay_type = Cash::PAY_TYPE_HAND;
- if ($cash->type == Cash::TYPE_RECHARGE) {
- $cashExt = CashExt::findOne(['cash_id' => $cash->id, 'cash_price_type' => CashExt::CASH_PRICE_TYPE_AMOUNT]);
- if ($cashExt && $cashExt->real_price > 0) {
- AccountLog::saveLog($cash->user_id, $cashExt->real_price, 2, 1, 0, 0, '佣金提现打款');
- }
- }
- $res['result_code'] = "SUCCESS";
- NoticeSend::CashSuccess($cash->user_id, $user->binding, $cashExt ? $cashExt->real_price : $price, '手动打款', ($cash->price - ($cashExt ? $cashExt->real_price : $price)));
- }
- if (isset($res['result_code']) && $res['result_code'] == 'SUCCESS') {
- $cash->save();
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功'
- ]);
- } else {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => !empty($res['err_code_des']) ? $res['err_code_des'] : '请稍后重试',
- 'data' => $res
- ]);
- }
- }
- //saas确认打款
- public function actionSaasConfirm()
- {
- $id = post_params('id');
- $status = post_params('status');
- $store_id = get_store_id();
- $cash = Cash::findOne(['id' => $id, 'is_delete' => Cash::IS_DELETE_NO]);
- if (!$cash) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提现记录不存在,请刷新重试'
- ]);
- }
- if (!$cash->order_no) {
- $order_no = null;
- while (true) {
- $order_no = date('YmdHis') . mt_rand(100000, 999999);
- $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
- if (!$exist_order_no) {
- break;
- }
- }
- $cash->order_no = $order_no;
- $cash->save();
- }
- if ($cash->status != 1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '操作错误,请刷新重试'
- ]);
- }
- $res = [];
- $price = Cash::getServiceMoney($cash);
- if ($status == Cash::STATUS_GIVEN) { //微信自动打款
- $cash->status = Cash::STATUS_GIVEN;
- $cash->pay_time = time();
- $cash->pay_type = Cash::PAY_TYPE_WX;
- $saas = SaasUser::findOne(['id' => $cash->saas_id]);
- $data = [
- 'partner_trade_no' => $cash->order_no,
- 'openid' => $saas->platform_open_id,
- 'check_name' => 'NO_CHECK',
- 'amount' => $price * 100,
- 'desc' => '转账',
- 'user_name' => $cash->name
- ];
- // $WechatConfig = WechatConfig::findOne(['store_id' => get_store_id()]);
- // if (empty($WechatConfig->mch_id) || empty($WechatConfig->pay_key) || empty($WechatConfig->app_id)|| empty($WechatConfig->cert_pem)|| empty($WechatConfig->key_pem)) {
- // return $this->asJson([
- // 'code' => 1,
- // 'msg' => "后台参数配置错误,请检查参数后重试"
- // ]);
- // }
- $store = Store::findOne($store_id);
- $wechat_cash = Option::get('wechat_cash', $store_id, 'store')['value'];
- if($store->is_platform_transfers == 1){
- $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value'];
- }
- $wechat = \Yii::$app->controller->wechatPay;
- // $res = $wechat->transfer->toBalance($data);
- $res = (new \app\utils\WechatMerchant\WxV3($wechat))->transferBatches(get_store_id(), $data);
- //判断是否使用新版本转账 增加转账标识
- $wechat_cash = json_decode($wechat_cash, true);
- if (intval($wechat_cash['is_open']) === 2) {
- if (!$res['code']) {
- $cash->is_platform_transfers = intval($store->is_platform_transfers);
- $cash->wx_cash_type = Cash::WX_CASH_TYPE_NEW;
- $cash->wx_cash_state = $res['data']['state'];
- $cash->wx_cash_result_info = json_encode($res['data'], JSON_UNESCAPED_UNICODE);
- $cash->save();
- }
- }
- //NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '微信自动打款', ($cash->price - $price));
- } elseif ($status == Cash::STATUS_HAND) { //手动打款
- $cash->status = Cash::STATUS_GIVEN;
- $cash->pay_time = time();
- $cash->pay_type = Cash::PAY_TYPE_HAND;
- // if ($cash->type == Cash::TYPE_RECHARGE) {
- // $user = User::findOne(['id' => $cash->user_id]);
- // $user->money += doubleval($price);
- // if (!$user->save()) {
- // \Yii::$app->cache->set('cash_cache_' . $id, false);
- // foreach ($user->errors as $error) {
- // return $this->asJson([
- // 'code' => 1,
- // 'msg' => $error
- // ]);
- // }
- // }
- // }
- $res['result_code'] = "SUCCESS";
- //NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '手动打款', ($cash->price - $price));
- }
- if (isset($res['result_code']) && $res['result_code'] == 'SUCCESS') {
- $cash->save();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功'
- ]);
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => !empty($res['err_code_des']) ? $res['err_code_des'] : '请稍后重试',
- 'data' => $res
- ]);
- }
- }
- /**
- * 订单
- * @return \yii\web\Response
- */
- public function actionOrder()
- {
- $store_id = get_store_id();
- $form = new ShareOrderForm();
- $form->attributes = get_params();
- $form->store_id = $store_id;
- $form->parent_id = get_params('parent_id') ? (int)get_params('parent_id') : false;
- $data = $form->search();
- foreach ($data['data']['data'] as &$item) {
- $order_detail = OrderDetail::find()->where(['order_id' => $item['id']])->select('delivery_type')->one();
- $item['delivery_type'] = $order_detail['delivery_type'];
- }
- $return = [
- 'code' => $data['code'],
- 'msg' => $data['msg'],
- 'data' => $data['data'],
- 'parent' => []
- ];
- if (get_params('parent_id')) {
- $user = User::findOne(['store_id' => $store_id, 'id' => get_params('parent_id')]);
- $return['parent'] = $user;
- }
- return $this->asJson($return);
- }
- /**
- * 充值订单
- * @return \yii\web\Response
- */
- public function actionTopUpOrder()
- {
- $store_id = get_store_id();
- $form = new RechargeForm();
- $form->attributes = get_params();
- $form->store_id = $store_id;
- $form->phone = get_params('phone');
- $form->order_no = get_params('order_no');
- $data = $form->search();
- $return = [
- 'code' => $data['code'],
- 'msg' => $data['msg'],
- 'data' => $data['data'],
- 'parent' => []
- ];
-
- return $this->asJson($return);
- }
- /**
- * @return string
- * 获取用户分销二维码
- */
- public function actionGetQrcode()
- {
- $form = new QrcodeForm();
- $form->attributes = get_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->search());
- }
- /**
- * @return string
- * 设置推广海报
- */
- public function actionQrcode()
- {
- $store_id = get_store_id();
- //兼容联盟
- $is_saas = post_params('is_saas',0);
- if($is_saas){
- $store_id = -1;
- }
- $qrcode = Qrcode::findOne(['store_id' => $store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_SHARE]);
- if (!$qrcode) {
- $qrcode = new Qrcode();
- }
- $form = new QrcodeForm();
- $form->attributes = post_params();
- $form->store_id = $store_id;
- $form->qrcode = $qrcode;
- $form->type = Qrcode::TYPE_SHARE;
- return $this->asJson($form->save());
- }
- /**
- * @return \yii\web\Response
- */
- public function actionGetQrcodeSetting()
- {
- //兼容联盟
- $is_saas = get_params('is_saas',0);
- if($is_saas){
- $store_id = -1;
- }else{
- $store_id = get_store_id();
- }
- $qrcode = Qrcode::findOne(['store_id' => $store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_SHARE]);
- // \Yii::error($qrcode->font);
- $color = Color::find()->select('id, color')->andWhere(['is_delete' => 0])->asArray()->all();
- $font_position = json_decode($qrcode->font_position, true);
- $qrcode_position = json_decode($qrcode->qrcode_position, true);
- $avatar_position = json_decode($qrcode->avatar_position, true);
- $avatar_size = json_decode($qrcode->avatar_size, true);
- $qrcode_size = json_decode($qrcode->qrcode_size, true);
- $font_size = json_decode($qrcode->font, true);
- $invite_position = json_decode($qrcode->invite_position, true);
- // \Yii::error($font_size);
- $first = Color::findOne(['color' => $font_size['color']]);
- $res = [
- 'qrcode_bg' => $qrcode->qrcode_bg ?: \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/2_1.png',
- 'qrcode_pic' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/1.png',
- 'avatar' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png',
- 'qrcode' => $qrcode,
- 'color' => $color,
- 'first' => $first->id,
- 'font_c' => Json::decode($qrcode->font, true)['color'] ? Json::decode($qrcode->font, true)['color'] : ($first->color ?: "#000"),
- 'avatar_w' => $avatar_size['w'] ?: '63',
- 'avatar_x' => $avatar_position['x'] ?: '50',
- 'avatar_y' => $avatar_position['y'] ?: '160',
- 'qrcode_w' => $qrcode_size['w'] ?: '169',
- 'qrcode_c' => empty($qrcode_size['c']) ? 1 : (($qrcode_size['c'] == "true") ? 1 : 0),
- 'qrcode_x' => $qrcode_position['x'] ?: '65',
- 'qrcode_y' => $qrcode_position['y'] ?: '240',
- 'font_x' => $font_position['x'] ?: '124',
- 'font_y' => $font_position['y'] ?: '180',
- 'font_w' => $font_size['size'] ?: '20',
- 'invite_x' => $invite_position['x'] ?: '0',
- 'invite_y' => $invite_position['y'] ?: '0',
- ];
- return $this->asJson(['code' => 0, 'msg' => 'success', 'data' => $res]);
- }
- /**
- * 提现明细列表
- */
- public function actionCashDetail()
- {
- $form = new CashListForm();
- $form->attributes = get_params();
- $form->store_id = get_store_id();
- $form->user_id = get_user_id();
- return $this->asJson($form->getList());
- }
- public function actionAddShare()
- {
- $user_id = post_params('user_id', 0);
- $mobile = post_params('mobile', '');
- $name = post_params('name', '');
- $store_id = get_store_id();
- $user = User::findOne($user_id);
- if (!$user) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '用户信息查询失败'
- ]);
- }
- $share = Share::findOne(['user_id' => $user_id, 'is_delete' => 0, 'status' => [0, 1]]);
- if ($user->is_distributor || $share) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '用户已经是分销商或审核中'
- ]);
- }
- $share = new Share();
- $share->user_id = $user_id;
- $share->mobile = $mobile;
- $share->name = $name;
- $share->is_delete = 0;
- $share->store_id = $store_id;
- $share->status = 1;
- $share->created_at = time();
- if (!$share->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => implode(';', array_values($share->firstErrors))
- ]);
- }
- $user->time = time();
- $user->is_distributor = 1;
- if (!$user->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => implode(';', array_values($user->firstErrors))
- ]);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => "添加分销商成功"
- ]);
- }
- public function actionShareChildList()
- {
- $form = new ShareOrderForm();
- $form->attributes = get_params();
- $form->store_id = get_store_id();
- return $this->asJson($form->shareChildList());
- }
- }
|