where([ 'store_id' => $store_id, 'user_id' => $user_id, 'status' => $status, 'is_send' =>$is_send, 'is_delete' => 0, ]); }else{ $query = ReportInfo::find()->where([ 'store_id' => $store_id, 'user_id' => $user_id, 'is_delete' => 0, ]); } //查询报单号 if ($order_no) { $query->andWhere(['like', 'order_no', $order_no]); } $count = $query->count(); $pagination = new Pagination(['totalCount' => $count, 'page' => $page - 1, 'pageSize' => $limit]); $list = $query->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->asArray()->all(); foreach ($list as $key => &$value) { $value['date'] = date('Y-m-d H:i:s', $value['created_at']); $value['content'] = $value['desc']; //状态 if ($value['status'] == 0 && $value['is_send'] == 0){ //待审核 $value['status_text'] = '待审核'; $value['status'] = 0; }else if ($value['status'] == 1 && $value['is_send'] == 0){ //待到账 $value['status_text'] = '待到账'; $value['status'] = 1; }else if ($value['status'] == 2 && $value['is_send'] == 0){ //审核失败 $value['status_text'] = '审核失败'; $value['status'] = 2; }else if ($value['status'] == 1 && $value['is_send'] == 1){ //已完成 $value['status_text'] = '已完成'; $value['status'] = 4; } $value['audit_info'] = Json::decode($value['audit_info']); } //查询待审核条数 $query_no_audit_count = ReportInfo::find()->where([ 'store_id' => $store_id, 'user_id' => $user_id, ])->andWhere(['status' => 0, 'is_send' =>0,'is_delete'=>0])->count(); //查询审核失败条数 $query_fail_audit_count = ReportInfo::find()->where([ 'store_id' => $store_id, 'user_id' => $user_id, ])->andWhere(['status' => 2, 'is_send' =>0,'is_delete'=>0])->count(); //查询待到账条数 $query_wait_count = ReportInfo::find()->where([ 'store_id' => $store_id, 'user_id' => $user_id, ])->andWhere(['status' => 1, 'is_send' =>0,'is_delete'=>0])->count(); //查询已完成条数 $query_over_count = ReportInfo::find()->where([ 'store_id' => $store_id, 'user_id' => $user_id, ])->andWhere(['status' => 1, 'is_send' =>1,'is_delete'=>0])->count(); return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'query_no_audit_count'=>$query_no_audit_count, 'query_fail_audit_count'=>$query_fail_audit_count, 'query_wait_count'=>$query_wait_count, 'query_over_count'=>$query_over_count, 'list' => $list, 'page_count' => $pagination->getPageCount(), 'row_count' => $count,//总数 ] ]); }catch (\Exception $e) { return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage() ]); } } /** * 删除报单 */ public function actionReportDelete() { $report_id = post_params('report_id',''); $report_info = ReportInfo::findOne($report_id); if (!$report_info) { return $this->asJson([ 'code' => 1, 'msg' => '报单不存在', ]); } if ($report_info->status !== 2){ return $this->asJson([ 'code' => 1, 'msg' => '失败状态才能删除', ]); } $report_info->is_delete = 1; $report_info->save(); $report_detail = ReportPoolDetail::updateAll(['is_delete' => 1],['report_id' => $report_id]); $report_level = ReportPoolLevel::updateAll(['is_delete' => 1],['report_id' => $report_id]); return $this->asJson([ 'code' => 0, 'msg' => '删除成功', ]); } /** * 报单表单信息 * @return void */ public function actionReportFormInfo() { $store_id = get_store_id(); $report_setting = ReportSetting::findOne(['store_id' => $store_id]); $report_form = ReportForm::find()->where(['report_setting_id'=>$report_setting->id,'store_id' => $store_id,'is_delete'=> 0]) ->orderBy('id asc,sort asc') ->asArray() ->all()??[]; if (empty($report_form)){ return $this->asJson([ 'code' => 1, 'msg' => '后台未配置表单规则', ]); } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data'=>$report_form ]); } /** *新增报单 */ public function actionReportAdd(){ $report_id = post_params('report_id',''); $audit_info = post_params('audit_info',''); if (!$audit_info){ return $this->asJson([ 'code' => 1, 'msg' => '审核信息不能为空', ]); } $report_info = new ReportInfo(); if ($report_id){ $report_info = ReportInfo::findOne($report_id); if (!$report_info){ return $this->asJson([ 'code' => 1, 'msg' => '该报单不存在' ]); } if ($report_info->status !== 2){ return $this->asJson([ 'code' => 1, 'msg' => '失败状态才能重新编辑' ]); } $report_info->updated_at = time(); }else{ $report_setting = ReportSetting::findOne(['store_id' => get_store_id()]); //查询是否超过提交限制 if ($report_setting->reporr_limit){ $todayStart = strtotime(date('Y-m-d 00:00:00')); $todayEnd = strtotime(date('Y-m-d 23:59:59')); $report_count = ReportInfo::find()->where(['store_id' => get_store_id(),'is_delete' => 0,'user_id'=>get_user_id()]) ->andWhere(['>=','created_at',$todayStart]) ->andWhere(['<=','created_at',$todayEnd]) ->count(); if ($report_count >= $report_setting->reporr_limit){ return $this->asJson([ 'code' => 1, 'msg' => '超过当日提交限制' ]); } } if (!$report_setting->dividend_day){ return $this->asJson([ 'code' => 1, 'msg' => '平台还未配置分红周期' ]); } //检查是否有本期分红池 $pool_info = ReportPool::find()->where(['store_id'=>get_store_id(), 'is_send' => 0]) ->andWhere(['<=','start_time',time()]) ->andWhere(['>=','end_time',time()]) ->orderBy('id desc') ->one(); //如果没有奖金池创建第一个池子 if (!$pool_info) { $pool_info = new ReportPool(); $pool_info->store_id = get_store_id(); $pool_info->start_time = time(); $pool_info->end_time = time() + $report_setting->dividend_day * 24 * 60 * 60; $pool_info->created_at = time(); $pool_info_copy = ReportPool::find()->where(['store_id'=>get_store_id()]) ->orderBy('id desc') ->one(); if ($pool_info_copy){ $pool_info->number = $pool_info_copy->number+1; }else{ $pool_info->number =1; } if (!$pool_info->save()){ return $this->asJson([ 'code' => 1, 'msg' => '添加失败' ]); } } $report_info->store_id = get_store_id(); $report_info->user_id = get_user_id(); $report_info->saas_id = get_saas_user_id(); $report_info->created_at = time(); $report_info->is_send = 0; $report_info->pool_id = $pool_info->id; $report_info->order_no = OrderNo::getOrderNo(OrderNo::REPORT_INFO); } $report_info->audit_info = Json::encode($audit_info); $report_info->status = 0; $report_info->is_delete = 0; if (!$report_info->save()){ return $this->asJson([ 'code' => 1, 'msg' => '添加失败' ]); } return $this->asJson([ 'code' => 0, 'msg' => '添加成功' ]); } /** * 积分记录 */ public function actionIntegralLog(){ $store_id = get_store_id(); $report_id = get_params('report_id'); $query = ReportPoolLevel::find()->alias('rp') ->leftJoin(['r' => ReportInfo::tableName()], 'r.id=rp.report_id') ->leftJoin(['u' => User::tableName()], 'u.id=rp.user_id') ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding') ->where(['r.is_delete' => 0, 'r.store_id' => $store_id,'rp.user_id'=>get_user_id()]); if ($report_id){ $query->andWhere(['rp.report_id' => $report_id]); } $query->orderBy('r.status ASC,r.created_at DESC') ->select([ 'rp.*','r.order_no', 'su.name new_name', 'su.id saas_user_id', 'su.avatar new_avatar', 'u.nickname', 'u.avatar_url', 'u.binding' ]); $pagination = pagination_make($query); $list = $pagination['list']; foreach($list as &$value){ //查询级别 if ($value['level'] == 0){ $value['level_name'] = '一级分销积分'; }else if ($value['level'] == 1){ $value['level_name'] = '二级分销积分'; }else{ $value['level_name'] = '三级分销积分'; } //提交时间 $value['created_at'] = $value['created_at']?date('Y-m-d H:i:s',$value['created_at']):''; //发放时间 $value['send_time'] = $value['send_time']?date('Y-m-d H:i:s',$value['send_time']):''; } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $list, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'] ], ]); } /** * 奖金池 */ public function actionBonusPool() { $pool_id = get_params('pool_id',''); $pool_info = ReportPool::findOne($pool_id); if (!$pool_info){ return $this->asJson([ 'code' => 1, 'msg' => '奖池不存在' ]); } $time_str = date('Y-m-d',$pool_info->start_time).' ~ '.date('Y-m-d',$pool_info->end_time); $number = $pool_info->number; //参与人数 $pool_peopel_count = ReportPoolLevel::find()->where(['pool_id'=>$pool_id,'is_delete'=>0])->groupBy('user_id')->count()??0; //报单次数 $pool_order_count = ReportInfo::find()->where(['pool_id'=>$pool_id,'is_delete'=>0])->count()??0; //奖金池奖金 $pool_bonus =$pool_info->reward_amount??0; //奖金池累计积分 $pool_integral_sum = ReportPoolLevel::find()->where(['pool_id'=>$pool_id,'is_delete'=>0])->sum('money')??0; //奖池结束剩余天数 $timestamp1 = $pool_info->end_time; // August 1, 2021 $timestamp2 = time(); // September 1, 2021 $dateTime1 = new \DateTime("@$timestamp1"); $dateTime2 = new \DateTime("@$timestamp2"); $interval = $dateTime1->diff($dateTime2); $remaining_day = $interval->days; //参与记录 $query = ReportPool::find()->alias('rp') ->leftJoin(['r' => ReportInfo::tableName()], 'r.pool_id=rp.id') ->where(['r.is_delete' => 0, 'rp.store_id' => get_store_id(),'r.user_id'=>get_user_id()]); $query->orderBy('rp.created_at DESC') ->select([ 'rp.*','r.order_no','r.user_id', ]); $pagination = pagination_make($query); $list = $pagination['list']; foreach($list as &$value){ //奖池期数 $value['number'] = $value['number']; //开始结束时间 $value['time_str'] = date('Y/m/d',$value['start_time']).' ~ '.date('Y/m/d',$value['end_time']); //报单次数 $value['report_num'] = ReportInfo::find()->where(['is_delete'=>0,'pool_id'=>$value['id'],'store_id'=>get_store_id(),'user_id'=>$value['user_id'],'status'=>1])->count(); //积分奖励 $value['report_integral'] = ReportPoolLevel::find()->where(['is_delete'=>0,'store_id'=>get_store_id(),'pool_id'=>$value['id']])->sum('money')??0; //奖励金额 $value['report_money'] = $value['reward_amount']??0; $value['send_time'] =$value['is_send']?date('Y-m-d H:i:s',$value['send_time']):date('Y-m-d H:i:s',$value['end_time']) ; } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'time_str'=>$time_str, 'number'=>$number, 'pool_peopel_count'=>$pool_peopel_count, 'pool_bonus'=>$pool_bonus, 'pool_order_count'=>$pool_order_count, 'pool_integral_sum'=>$pool_integral_sum, 'remaining_day'=>$remaining_day, 'data' => $list, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'] ], ]); } /** * 全域分佣页面 */ public function actionGlobalInfo() { //用户头像昵称 $user = User::findOne(['id' => get_user_id()]); $nickname = $user->nickname; $avatar_url = $user->avatar_url; //累计奖励 $global_money_total = $user->global_money_total; //累计积分 $sum_integral_total = ReportPoolLevel::find()->where(['store_id'=>get_store_id(),'is_send'=>1,'user_id'=>$user->id,'is_delete'=>0])->sum('money')??0; //已发放奖励 $send_money =ReportPoolLevel::find()->where(['store_id'=>get_store_id(),'is_send'=>1,'user_id'=>$user->id,'is_delete'=>0])->sum('dividend_money')??0; //未发放 $un_send_money =ReportPoolLevel::find()->where(['store_id'=>get_store_id(),'is_send'=>0,'user_id'=>$user->id,'is_delete'=>0])->sum('dividend_money')??0; //已提现 // $withdraw_money = 0; $withdraw_money = Cash::find()->where(['store_id' => get_store_id(), 'user_id' => get_user_id(),'cash_type' => 17])->andWhere(['in', 'status', [2, 4]])->sum('price')??0; //最新奖池信息 $pool_info = ReportPool::find()->where(['store_id'=>get_store_id(),'is_send'=>0])->orderBy('id desc')->one()??[]; $report_num = 0; $report_integral = 0; $report_money = 0; if ($pool_info){ //报单次数 $report_num = ReportInfo::find()->where(['is_delete'=>0,'pool_id'=>$pool_info->id,'store_id'=>get_store_id(),'user_id'=>$user->id,'status'=>1])->count(); //奖励积分 $report_integral = ReportPoolLevel::find()->where(['is_delete'=>0,'store_id'=>get_store_id(),'is_send'=>0,'pool_id'=>$pool_info->id])->sum('money')??0; //奖励金额 $report_money = $pool_info->reward_amount??0; $global_number = $pool_info->number; $pool_id = $pool_info->id; }else{ $global_number = ''; $pool_id = ""; } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'nickname' => $nickname, 'avatar_url' => $avatar_url, 'global_money_total' => $global_money_total, 'sum_integral_total' => $sum_integral_total, 'send_money' => $send_money, 'un_send_money' => $un_send_money, 'withdraw_money' => $withdraw_money, 'report_num' => $report_num, 'report_integral' => $report_integral, 'report_money' => $report_money, 'global_number' => $global_number, 'pool_id' => $pool_id, ], ]); } /** * 提现页面 */ public function actionGetPrice() { $store_id = get_store_id(); $user_id = get_user_id(); $is_area = input_params('is_area'); $user = User::findOne(['store_id' => $store_id, 'is_delete' => 0,'id' => $user_id]); if (!$user) { return $this->asJson([ 'code' => 1, 'msg' => '用户不存在' ]); } $cash = get_user()->global_money; $res = [ 'cash_profit' => $cash, ]; $groups = 'bonus_pool'; // 新提现方式 $res['cash_method_list'] = []; $saas_user = get_saas_user(); $cash_method = []; $lg_info = Lg::find()->where(['user_id' => $saas_user->id, 'store_id' => get_store_id(),'status'=>1,'is_delete'=>0])->one(); if (!empty($saas_user->withdraw_method)) { $cash_method = json_decode($saas_user->withdraw_method, true); if ($lg_info){ //灵工 $cash_method = array_merge($cash_method, [[ 'type' => 'lg' ]]); } if (is_array($cash_method)) { foreach($cash_method as $i => $item){ if ($item['type'] === 'wechat' && Option::get('pay_wechat', $this->store_id, $groups, 0)['value'] == 0) { unset($cash_method[$i]); } if ($item['type'] === 'alipay' && Option::get('pay_alipay', $this->store_id, $groups, 0)['value'] == 0) { unset($cash_method[$i]); } if ($item['type'] === 'bank_card' && Option::get('bank', $this->store_id, $groups, 0)['value'] == 0) { unset($cash_method[$i]); } if ($item['type'] === 'lg' && Option::get('lg', $this->store_id, $groups, 0)['value'] == 0) { unset($cash_method[$i]); } } } }else{ if($lg_info){ //灵工 $cash_method[]['type'] = 'lg'; $res['cash_method_list'] = array_values($cash_method); } } $cash_method = array_merge($cash_method, [[ 'type' => 'money' ]]); $res['cash_method_list'] = $cash_method; return $this->asJson(['code' => 0, 'msg' => 'success', 'data' => $res]); } /** * 提现操作 * @return \yii\web\Response * @throws \yii\db\Exception */ public function actionCashApply() { $price = post_params('cash'); $type = post_params('type'); if (!$price || !in_array($type, ['alipay', 'bank_card', 'wechat', 'money','lg'])) { return $this->asJson([ 'code' => 1, 'msg' => '参数非法', ]); } $user_id = get_user_id(); $user = get_user(); $store_id = get_store_id(); $groups = 'bonus_pool'; $cash_type = '17'; // $cash_max_day = Option::get('cash_max_day', $store_id, $groups, 0)['value']; // $cash_max_single_day = Option::get('cash_max_single_day', $store_id, $groups, 0)['value']; // $min_money = Option::get('min_money', $store_id, $groups, 0)['value']; // if($price < $min_money){ // return $this->asJson([ // 'code' => 1, // 'msg' => '最小提现'. $min_money .'元' // ]); // } // $cash_sum = Cash::find()->where([ // 'store_id' => $store_id, // 'is_delete' => 0, // 'status' => [0, 1, 2, 5, 4], // 'cash_type' => $cash_type, // ])->andWhere([ // 'AND', // ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))], // ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))], // ])->sum('price'); // if ($cash_max_day) { // $cash_max_day = $cash_max_day - ($cash_sum ?: 0); // if ($price > $cash_max_day) { // return $this->asJson([ // 'code' => 1, // 'msg' => '提现金额不能超过' . $cash_max_day . '元' // ]); // } // } // if ($cash_max_single_day) { // $cash_max_single_day_ = $cash_max_single_day - ($cash_sum ?: 0); // if ($price > $cash_max_single_day_) { // return $this->asJson([ // 'code' => 1, // 'msg' => '每人每天提现金额不能超过' . $cash_max_single_day . '元' // ]); // } // } // //提现手续费金额 // $shareHolderCashProfit = Option::get('shareHolderCashProfit', get_store_id(), $groups, 0)['value']; // $share_holder = ShareHolder::findOne(['store_id' => $store_id, 'is_delete' => 0, 'status' => 1, 'user_id' => $user_id]); // if (!$share_holder) { // return $this->asJson([ // 'code' => 1, // 'msg' => '未拥有提现权限' // ]); // } if ($price > $user->global_money) { return $this->asJson([ 'code' => 1, 'msg' => '当前大于可提现金额' ]); } $exit = Cash::find()->andWhere(['=', 'status', 0])->andWhere(['user_id' => $user_id, 'store_id' => $store_id, 'cash_type' => $cash_type])->exists(); if ($exit) { // return $this->asJson([ // 'code' => 1, // 'msg' => '尚有未完成的提现申请' // ]); } $t = \Yii::$app->db->beginTransaction(); $cash = new Cash(); $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_CASH); $cash->is_delete = 0; $cash->status = 0; $cash->price = $price; $cash->created_at = time(); $cash->user_id = $user_id; $cash->store_id = $store_id; $saas_user = get_saas_user(); if (!empty($saas_user->withdraw_method)) { $withdraw_method = Json::decode($saas_user->withdraw_method); $withdraw_method = array_column($withdraw_method, null, 'type'); if ($type == 'wechat') { $cash->type = 0; $cash->name = $withdraw_method['wechat']['name']; $cash->mobile = $withdraw_method['wechat']['account']; } if ($type == 'alipay') { $cash->type = 1; $cash->name = $withdraw_method['alipay']['name']; $cash->mobile = $withdraw_method['alipay']['account']; } if ($type == 'bank_card') { $cash->type = 2; $cash->name = $withdraw_method['bank_card']['name']; $cash->mobile = $withdraw_method['bank_card']['account']; $cash->bank_name = $withdraw_method['bank_card']['bank']; $cash->bank_branch = $withdraw_method['bank_card']['branch']; } if ($type == 'lg') { //灵工提现 $lg_info = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0,'status'=>1])->one(); $cash->type = 4; $cash->name = $lg_info->name; $cash->mobile = $lg_info->mobile; } }else{ if ($type == 'lg') { //灵工提现 $lg_info = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0,'status'=>1])->one(); $cash->type = 4; $cash->name = $lg_info->name; $cash->mobile = $lg_info->mobile; } } if ($type == 'money') { $cash->type = 3; } $cash->pay_time = 0; $cash->service_charge = 0; $cash->cash_type = $cash_type; if ($cash->save()) { $user->global_money -= $cash->price; if (!$user->save()) { $t->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => $user->errors ]); } $t->commit(); return $this->asJson([ 'code' => 0, 'msg' => '申请成功' ]); } else { $t->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => $cash->errors ]); } } /** * 提现明细 * @return \yii\web\Response */ public function actionCashDetail() { $page = get_params('page', 1); $limit = get_params('limit', 20); $status = get_params('status', -1); $store_id = get_store_id(); $user_id = get_user_id(); $query = Cash::find()->where(['store_id' => $store_id, 'user_id' => $user_id]); $cash_type_where = ['cash_type' => 17]; $query->andWhere($cash_type_where); if ($status != -1) { // 待审核 if ($status == 0) { $query->andWhere(['status' => $status]); } // 已打款 if ($status == 1) { $query->andWhere(['in', 'status', [2, 4]]); } // 已拒绝 if ($status == 2) { $query->andWhere(['status' => 3]); } } $count = $query->count(); $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit, 'page' => $page - 1]); $list = $query->select('*')->limit($pagination->limit) ->offset($pagination->offset)->orderBy('created_at desc')->asArray()->all(); foreach ($list as &$value) { $value['price'] = sprintf('%.2f', ($value['price'] - ($value['price'] * ($value['service_charge'] / 100)))); $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']); $value['audit_time'] = date('Y-m-d H:i:s', $value['updated_at']); if ($value['status'] == 2 || $value['status'] == 4) { $value['audit_time'] = date('Y-m-d H:i:s', $value['pay_time']); } } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'list' => $list, 'page_count' => $pagination->pageCount, 'row_count' => $count ] ]); } /** * 用户中心 * @return \yii\web\Response */ public function actionIndex() { $data = self::getDefaultIndexData(); $data = [ 'code' => 0, 'data' => $data, 'msg' => 'success' ]; $saasUser = get_saas_user(); $is_md_staff = 0; $is_md_manager = 0; $md_staff = null; $is_admin = 0; if ($saasUser) { $md_staff = MdStaff::findOne(['saas_user_id' => $saasUser['id'], 'is_delete' => 0, 'is_disable' => 0, 'store_id' => get_store_id()]); $is_admin = !empty($saasUser['id']) ? Admin::findOne(['saas_user_id' => $saasUser['id'], 'is_delete' => 0,'type'=>"admin"]) : ""; } if ($md_staff) { $is_md_staff = 1; $is_md_manager = $md_staff->is_manager; } if (is_platform()) { $user_data_info = [ 'access_token' => $saasUser['access_token'], 'avatar_url' => $saasUser['avatar'], 'binding' => $saasUser['mobile'], 'id' => $saasUser['id'], 'nickname' => $saasUser['name'], 'saas_money' => $saasUser['share_profit'], 'is_salesman' => $saasUser['is_salesman'], 'is_md_staff' => $is_md_staff, 'is_md_manager' => $is_md_manager, 'is_admin' => empty($is_admin) ? 0 : 1 ]; $data['data']['user_info'] = $user_data_info; } elseif (get_user()) { $user = User::find()->alias('u') ->leftJoin(['l' => Level::tableName()], 'u.level=l.level and l.store_id='.get_store_id()) ->leftJoin(['p' => User::tableName()], 'u.old_parent_id=p.id') ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=p.binding') ->where(['u.id' => get_user_id(), 'u.store_id' => get_store_id()])->select('u.*, IFNULL(l.name, "普通用户") as `level_name`, IFNULL(su.name, "总店") as `parent`') ->asArray()->one(); //$admin = Admin::find()->where(['mobile' => $user['binding'], 'is_delete' => 0, 'type' => 'store'])->select('id, type_id')->one(); $is_admin = 0; if (\Yii::$app->prod_is_dandianpu() && get_user()->is_admin == 0) { $store = Store::findOne(get_store_id()); if ($store && $store->admin_id > 0) { $store_admin = Admin::findOne(['id' => $store->admin_id, 'is_delete' => 0]); if ($store_admin) { $admin_user = User::findOne(['binding' => $store_admin->mobile, 'store_id' => get_store_id(), 'is_delete' => 0]); if ($admin_user) { $is_admin = 1; $admin_user->is_admin = 1; $admin_user->save(); } } } } else { $is_admin = (get_user()->is_admin . ''); } $user_data_info = [ 'access_token' => $saasUser['access_token'], 'avatar_url' => $saasUser['avatar'], 'binding' => $user['binding'], 'blacklist' => $user['blacklist'], 'id' => $user['id'], 'is_admin' => $is_admin, 'is_clerk' => $user['is_clerk'], 'is_saas_clerk' => (int)$user['is_saas_clerk'], 'is_delivery' => $user['is_delivery'], 'is_distributor' => $user['is_distributor'], 'is_live' => 0, 'is_real' => $user['is_real'], 'level' => $user['level'], 'level_name' => $user['level_name'], 'nickname' => $saasUser['name'], 'parent' => $user['parent'], 'integral' => $user['integral'] ? $user['integral'] : 0, 'money' => $user['money'] ? $user['money'] : "0.00", 'price' => $user['price'] ? $user['price'] : '0.00', 'gender' => $saasUser['gender'], 'saas_money' => SaasUser::findOne(['mobile' => $user['binding']])->share_profit, 'is_salesman' => $saasUser['is_salesman'], 'is_md_staff' => $is_md_staff, 'is_md_manager' => $is_md_manager, 'user_id' => get_user_id(), ]; if (is_h5()) { $wechat_config = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => WechatConfig::TYPE_CONFIG_MP]); if (!empty($wechat_config)) { // h5端,如果openid为空,返回授权链接 Wechat::init(get_store_id(), Wechat::WECHAT_KIND_OFFICIAL, WechatConfig::TYPE_CONFIG_MP); // $baseUrl = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl; // $store_id = get_store_id(); // $redirect_url = $baseUrl . '/index.php?r=client/v1/user/update-user-info&store_id='. $store_id .'&access_token=' . $saasUser['access_token']; // $user_data_info['oauth_redirect_url'] = Wechat::$wechat_official->oauth->scopes(['snsapi_userinfo'])->redirect($redirect_url); $user_data_info['oauth_redirect_url'] = LoginForm::getAuthLink(); } } if (\Yii::$app->prod_is_dandianpu()) { $user_data_info['is_show_toggle_store'] = 0; // 大于0表示显示切换,并且该值是store_id $user_data_info['has_store'] = 0; // 当前用户是否有商城 $admin = Admin::find()->where(['mobile' => $saasUser['mobile'], 'type' => 'store', 'is_delete' => 0])->orderBy('id DESC')->one(); if (!$admin) { $admin = Admin::find()->where(['saas_user_id' => $saasUser['id'], 'is_delete' => 0, 'type' => 'store'])->orderBy('id DESC')->one(); } if ($admin) { $store = Store::find()->where(['admin_id' => $admin->id, 'is_delete' => 0])->orderBy('id DESC')->one(); if (!$store && $admin->type_id > 0) { $store = Store::findOne(['id' => $admin->type_id, 'is_delete' => 0]); } if ($store) { $user_data_info['has_store'] = $store->id; } if ($store && $store->id != get_store_id()) { $user_data_info['is_show_toggle_store'] = $store->id; } } } $share_holder = ShareHolder::findOne(['user_id' => get_user_id(), 'is_delete' => 0, 'store_id' => get_store_id(), 'status' => 1]); $user_data_info['is_holder'] = $share_holder ? 1 : 0; //抖品作者 $videoAuthor = VideoGoodsAuthor::findOne(['user_id' => get_user_id(), 'is_delete' => 0, 'store_id' => get_store_id(), 'status' => 1]); $user_data_info['is_video_author'] = $videoAuthor ? 1 : 0; $data['data']['user_info'] = $user_data_info; } /** * 获取用户优惠券数量 */ $coupon_num = UserCoupon::find()->where([ 'user_id' => get_user_id(), 'is_delete' => 0, 'is_use' => 0, 'is_expire' => 0, ])->count(); // 获取核销卡数量 $verify_num = VerifyCardSale::find()->where([ 'user_id' => get_user_id(), 'is_delete' => 0, ])->count(); /** * 获取用户可用卡券数量 */ $card_num = UserCard::find()->where([ 'user_id' => get_user_id(), 'store_id' => get_store_id(), 'is_use' => 0, 'is_delete' => 0, ])->count(); /** * 获取浏览记录数量(包含商品和店铺的总数) */ $browse_log_num = BrowseLog::find()->where([ 'user_id' => get_user_id(), 'store_id' => get_store_id(), 'is_delete' => 0, ])->count(); /** * 获取用户收藏商品数量 */ $favorite_goods_num = Favorite::find()->where([ 'store_id' => get_store_id(), 'user_id' => get_user_id(), 'is_delete' => 0, 'type' => Favorite::FAVORITE_GOODS ])->count(); if (isset($user) && isset($user['level'])) { $next_level = Level::find()->where(['store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1]) ->andWhere(['>', 'level', $user['level']])->orderBy(['level' => SORT_ASC, 'id' => SORT_DESC])->asArray()->one(); } else { $next_level = 0; } $shareForm = new ShareForm(); $shareForm->store_id = get_store_id(); $share_setting = $shareForm->getShareSetting()['data']; $share_msg = $shareForm->getShareMsg(); //我的钱包 选项 $wallet['integral'] = 1; // $wallet['re'] = User$balance['recharge']; $wallet['re'] = UserCenterForm::get('recharge_custom_status', get_store_id(), 'recharge', 1); $order_count = OrderListForm::getCountData(get_store_id(), get_user_id()); // start 获取核销码 // TODO 这里要做支付宝小程序兼容 $code_url = ''; // TODO 影响速度,暂时注释 // if (get_user_id() > 0) { // if (is_wechat_platform() && !is_platform() && !is_h5()) { // 微信平台 // try { // $clerkCode = ShareQrcode::wxQrcode('user/clerk/clerk', "user_id:" . get_user_id() . ",store:" . get_store_id()); // if (isset($clerkCode['code']) && $clerkCode['code'] == 0) { // $code_url = $clerkCode['url_path']; // } // } catch (HttpException $e) { // } // } // } if (isset($data['data']['user_info'])) { $data['data']['user_info']['is_can_wechat'] = 0; if (strpos($data['data']['user_info']['nickname'], '****') !== false) { $data['data']['user_info']['is_can_wechat'] = 1; } } // $user_model = $user = get_user(); $saas_user_model = SaasUser::findOne(['mobile' => $user_model->binding]); if(!$saas_user_model->league_price){ $league_price = 0; }else{ $league_price = $saas_user_model->league_price; } $data['data']['user_info']['league_price'] = $league_price; //是否开启余额转佣金 $balance_to_commission = (int)Option::get('balance_to_commission', get_store_id(), 'recharge', 0)['value']; $recharge_wallet_status = (int)Option::get('recharge_wallet_status', get_store_id(), 'recharge', 0)['value']; $balancToCash = (int)Option::get(OptionSetting::BALANCE_TO_CASH, get_store_id(), 'recharge', 0)['value']; $arr = [ 'code' => 0, 'msg' => 'success', 'data' => [ 'order_count' => $order_count, 'show_customer_service' => 1, 'contact_tel' => UserCenterForm::get('contact_tel', get_store_id(), 'store'), 'share_setting' => $share_setting, 'share_msg' => $share_msg, 'user_info' => isset($data['data']['user_info']) ? $data['data']['user_info'] : null, 'next_level' => $next_level, 'orders' => $data['data']['orders'], 'coupon_num' => $coupon_num, 'favorite_goods_num' => $favorite_goods_num, 'card_num' => $card_num, 'browse_log_num' => $browse_log_num, 'wallet' => $wallet, 'code_url' => $code_url, 'verify_num' => $verify_num, 'balanc_to_cash' => $balancToCash, 'balance_to_commission' => $balance_to_commission, 'recharge_wallet_status' => $recharge_wallet_status ], ]; return $this->asJson($arr); } // 更新公众号用户信息 public function actionUpdateUserInfo() { $baseUrl = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl; $redirect_url = $baseUrl . '/h5/#/pages/my/my'; try { $code = get_params('code'); Wechat::init(get_store_id(), Wechat::WECHAT_KIND_OFFICIAL, WechatConfig::TYPE_CONFIG_MP); $result = Wechat::$wechat_official->oauth->userFromCode($code); $result = $result->toArray(); if (isset($result['raw']['openid'])) { $user = get_user(); $saas_user = get_saas_user(); if ($user) { $user->wechat_platform_open_id = $result['raw']['openid']; if (isset($result['raw']['unionid'])) { $user->wechat_union_id = $result['raw']['unionid']; } if (strpos($user->nickname, '******') !== false) { $user->nickname = $result['raw']['nickname']; } if (strpos($user->avatar_url, 'avatar.png') !== false) { $user->avatar_url = $result['raw']['headimgurl']; } $user->save(); } if ($saas_user) { if (strpos($saas_user->name, '******') !== false) { $saas_user->name = $result['raw']['nickname']; } if (strpos($saas_user->avatar, 'avatar.png') !== false) { $saas_user->avatar = $result['raw']['headimgurl']; } $saas_user->save(); } } return $this->redirect($redirect_url); } catch (\Exception $e) { return $this->redirect($redirect_url); } } /** * 修改用户信息 */ public function actionEditUserInfo() { $saasUser = get_saas_user(); if ($saasUser) { $avatar_url = input_params('avatar_url', ''); $nickname = input_params('nickname', ''); $gender = input_params('gender', ''); $openid = input_params('openid', ''); if (!empty($avatar_url)) { $saasUser->avatar = $avatar_url; } if (!empty($nickname)) { $saasUser->name = $nickname; } if (!empty($gender) && in_array($gender, [0, 1, 2])) { $saasUser->gender = $gender; } if ($saasUser->save()) { return $this->asJson([ 'code' => 0, 'msg' => '保存成功', ]); } return $this->asJson([ 'code' => 1, 'msg' => '保存失败', ]); } return $this->asJson([ 'code' => 1, 'msg' => '非法访问', ]); } /** * 更新用户wechat_app_open_id */ public function actionUpdateWechatAppOpenId() { $id = input_params('user_id'); $openid = input_params('open_id'); if (!$id || !$openid) { return $this->asJson([ 'code' => 1, 'msg' => '缺少参数', ]); } $user = User::findOne($id); if (!$user) { return $this->asJson([ 'code' => 1, 'msg' => '用户未找到', ]); } $user->wechat_app_open_id = $openid; $user->save(); return $this->asJson([ 'code' => 0, 'msg' => '更新成功', ]); } /** * 更新用户wechat_app_open_id */ public function actionUpdateWechatOpenId() { if (\Yii::$app->prod_is_dandianpu()) { $saas_user = get_saas_user(); $binding = $saas_user->mobile; // $wechat = \Yii::$app->controller->wechat; $wechat = WechatMini::getWechatConfig(get_store_id()); $code = post_params('code'); $session = $wechat->auth->session($code); if (!empty($session['openid']) && !empty($binding)) { User::updateAll(['wechat_open_id' => $session['openid']], ['binding' => $binding]); } } else { $user_id = get_user_id(); $user = User::findOne($user_id); $wechat = \Yii::$app->controller->wechat; $code = post_params('code'); $session = $wechat->auth->session($code); if (!empty($session['openid']) && !empty($user)) { $user->wechat_open_id = $session['openid']; if($session['unionid']){ $user->wechat_union_id = $session['unionid']; } $user->save(); } } } /** * Undocumented function * * @Author LGL 24963@qq.com * @DateTime 2021-02-04 * @desc: 绑定手机号 * @return string */ public function actionUserBinding() { $form = new UserForm(); $form->platform = $this->platform; $form->iv = post_params('iv'); $form->encryptedData = post_params('encryptedData'); $form->code = post_params('code'); return $this->asJson($form->WxBinding()); } // 授权手机号确认 public function actionUserEmpower() { $form = new UserForm(); $form->attributes = post_params(); $form->user_id = get_user_id(); $form->store_id = get_store_id(); return $this->asJson($form->userEmpower()); } private static function getDefaultIndexData() { $form = new UserCenterForm(); $form->store_id = get_store_id(); $data = $form->getData(); return $data['data']; } /** * 获取收货地址 * @return \yii\web\Response */ public function actionAddressList() { $query = Address::find()->where([ 'is_delete' => 0, 'user_id' => get_saas_user_id(), ]); // if (is_platform()) { // $query->andWhere([ // 'user_id' => get_saas_user_id() // ]); // } else { // $query->andWhere([ // 'user_id' => get_user_id() // ]); // } $list = $query->orderBy('is_default DESC,addtime DESC')->select('id,name,mobile,province_id,province,city_id,city,district_id,district,detail,is_default')->asArray()->all(); foreach ($list as $i => $item) { $list[$i]['address'] = $item['province'] . $item['city'] . $item['district'] . $item['detail']; } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'list' => $list, ], ]); } /** * 保存收货地址 * @return \yii\web\Response */ public function actionAddressSave() { $form = new AddressSaveForm(); $form->attributes = post_params(); $form->store_id = get_store_id(); // if (is_platform()) { $form->user_id = get_saas_user_id(); // } else { // $form->user_id = get_user_id(); // } return $this->asJson($form->save()); } /** * 设为默认收货地址 * @return \yii\web\Response */ public function actionAddressSetDefault() { $form = new AddressSetDefaultForm(); $form->attributes = get_params(); $form->store_id = get_store_id(); // if (is_platform()) { $form->user_id = get_saas_user_id(); // } else { // $form->user_id = get_user_id(); // } return $this->asJson($form->save()); } /** * 收货地址详情 * @return \Yii\web\Response */ public function actionAddressDetail() { $form = new AddressSetDefaultForm(); $form->address_id = get_params('id'); $form->store_id = get_store_id(); // if (is_platform()) { $form->user_id = get_saas_user_id(); // } else { // $form->user_id = get_user_id(); // } return $this->asJson($form->info()); } /** * 删除收货地址 * @return \yii\web\Response */ public function actionAddressDelete() { $form = new AddressSetDefaultForm(); $form->attributes = get_params(); $form->store_id = get_store_id(); // if (is_platform()) { $form->user_id = get_saas_user_id(); // } else { // $form->user_id = get_user_id(); // } return $this->asJson($form->delete()); } /** * 添加商品或店铺或技师到我的喜欢 */ public function actionFavoriteAdd() { $form = new FavoriteAddForm(); $form->attributes = post_params(); $form->store_id = get_store_id(); $form->user_id = get_user_id(); return $this->asJson($form->save()); } /** * 喜欢的商品或店铺列表 */ public function actionFavoriteList() { $form = new FavoriteListForm(); $form->attributes = get_params(); $form->user_id = get_user_id(); $form->store_id = get_store_id(); return $this->asJson($form->search()); } /** * 取消喜欢的商品或店铺列表 */ public function actionRemoveFavorite() { $id = post_params('id'); if (empty($id)) { return $this->asJson([ 'code' => 1, 'msg' => '参数错误' ]); } // if (!is_array($id)) { // $res = Favorite::updateAll(['is_delete' => 1], ['goods_id' => $id, 'user_id' => get_user_id(), 'type' => post_params('type', 0)]); // } else { // $res = Favorite::updateAll(['is_delete' => 1], ['and', ['in', 'goods_id', $id], ['user_id' => get_user_id(), 'type' => post_params('type', 0)]]); // } $res = Favorite::updateAll(['is_delete' => 1], ['id' => $id, 'user_id' => get_user_id(), 'type' => post_params('type', 0)]); if ($res) { return $this->asJson([ 'code' => 0, 'msg' => '删除成功', ]); } else { return $this->asJson([ 'code' => 1, 'msg' => 'fail', 'err' => $res ]); } } /** * 收藏专题列表 */ public function actionTopicFavoriteList() { $form = new TopicFavoriteListForm(); $form->attributes = get_params(); $form->store_id = get_store_id(); $form->user_id = get_user_id(); return $this->asJson($form->search()); } /** * 收藏|取消收藏专题 */ public function actionTopicFavorite() { $form = new TopicFavoriteForm(); $form->attributes = get_params(); $form->store_id = get_store_id(); $form->user_id = get_user_id(); return $this->asJson($form->save()); } /** * 用户卡券 * @return \yii\web\Response */ public function actionCard() { $form = new CardListForm(); $form->store_id = $this->store_id; $form->user_id = get_user_id(); $form->attributes = get_params(); return $this->asJson($form->search()); } /** * 卡券二维码 * @return \yii\web\Response */ public function actionCardQrcode() { $user_card_id = get_params('user_card_id'); $form = new QrcodeForm(); $form->page = "user/card-clerk/card-clerk"; $form->width = 100; $form->scene = "{$user_card_id}"; $form->store_id = $this->store_id; return $this->asJson($form->getQrcode()); } /** * 卡券核销 * @return \yii\web\Response */ public function actionCardClerk() { $user_card_id = get_params('user_card_id'); if (\Yii::$app->cache->get('card_id_' . $user_card_id)) { return $this->asJson([ 'code' => 1, 'msg' => '卡券核销中,请稍后重试' ]); } \Yii::$app->cache->set('card_id_' . $user_card_id, true); $user_card = UserCard::findOne(['id' => $user_card_id]); if ($user_card->is_use != 0) { \Yii::$app->cache->set('card_id_' . $user_card_id, false); return $this->asJson([ 'code' => 1, 'msg' => '卡券已核销' ]); } $user = get_user(); if ($user->is_clerk != 1) { \Yii::$app->cache->set('card_id_' . $user_card_id, false); return $this->asJson([ 'code' => 1, 'msg' => '不是核销员禁止核销' ]); } $user_card->clerk_id = $user->id; $user_card->shop_id = $user->shop_id; $user_card->clerk_time = time(); $user_card->is_use = 1; if ($user_card->save()) { \Yii::$app->cache->set('card_id_' . $user_card_id, false); return $this->asJson([ 'code' => 0, 'msg' => '核销成功' ]); } else { \Yii::$app->cache->set('card_id_' . $user_card_id, false); return $this->asJson([ 'code' => 1, 'msg' => '核销失败' ]); } } /** * 卡券详情 * @return \yii\web\Response */ public function actionCardDetail() { $form = new CardListForm(); $form->store_id = $this->store_id; $form->user_id = get_user_id(); $form->user_card_id = get_params('user_card_id'); return $this->asJson($form->detail()); } // 短信验证是否开启 public function actionSmsSetting() { $option = UserCenterForm::get('user_center_data', get_store_id(), 'store'); // $option = json_decode($option, true); $option['manual_mobile_auth'] = 1; if ($option['manual_mobile_auth'] == 1) { return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => 1 ]); } else { return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => 0 ]); } } // 短信验证 public function actionUserHandBinding() { $form = new Sms(); $code = mt_rand(0, 999999); return $this->asJson($form->send_text(get_store_id(), $code, post_params('content'))); } // 会员权益 public function actionMember() { $_form = post_params('_form'); $level = get_user()->level; $money = get_user()->money; $list = Level::find()->select(['id', 'image', 'level', 'name', 'price', 'buy_prompt', 'detail', 'synopsis', 'money']) ->where(['store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1])->andWhere(['<>', 'price', '0']) ->orderBy('level asc')->asArray()->all(); foreach ($list as &$val) { $val['synopsis'] = $val['synopsis'] == '[]' || !$val['synopsis'] ? [] : json_decode($val['synopsis'], true); } $now_level = Level::find()->where(['store_id' => get_store_id(), 'level' => $level, 'is_delete' => 0])->asArray()->one(); if ($now_level && $now_level['synopsis']) { $synopsis = json_decode($now_level['synopsis'], true); $now_level['synopsis'] = $synopsis; } else { $now_level['synopsis'] = []; } $user_info = [ 'nickname' => get_saas_user()->name, 'avatar_url' => get_saas_user()->avatar, 'id' => get_user()->id, 'level' => $level, 'level_name' => !empty($now_level['name']) ? $now_level['name'] : "普通会员" ]; $time = time(); $after_sale_time = Option::get('after_sale_time', get_store_id(), 'store'); $sale_time = $time - ($after_sale_time['value'] * 86400); $next_level = Level::find()->where(['store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1]) ->andWhere(['>', 'level', $level])->orderBy(['level' => SORT_ASC, 'id' => SORT_DESC])->asArray()->one(); $order_money = Order::find()->where(['store_id' => get_store_id(), 'user_id' => get_user_id(), 'is_delete' => 0]) ->andWhere(['is_pay' => 1, 'trade_status' => Order::ORDER_FLOW_CONFIRM])->andWhere(['<=', 'confirm_time', $sale_time])->select([ 'sum(pay_price)' ])->scalar(); $percent = 100; $s_money = 0; $order_money = $order_money ? $order_money : 0; if ($next_level) { if ($next_level['money'] != 0) { $percent = round($order_money / $next_level['money'] * 100, 2); } $s_money = round($next_level['money'] - $order_money, 2); } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'pay_type_list' => $this->getPayTypeList($_form), 'user_info' => $user_info, 'next_level' => $next_level, 'now_level' => $now_level, 'order_money' => $order_money, 'percent' => $percent, 's_money' => $s_money, 'money' => $money, 'list' => $list, ], ]); } /** * 会员支付 */ public function actionSubmitMember() { $form = new OrderMemberForm(); $form->store_id = get_store_id(); $form->user = get_user(); $form->attributes = post_params(); return $this->asJson($form->save()); } /** * 支付 * @return \yii\web\Response */ public function actionMemberPayData() { $form = new LevelPayDataForm(); $form->attributes = get_params(); $form->store_id = get_store_id(); $form->user = get_user(); return $this->asJson($form->search()); } // 获取支付方式TODO临时 protected function getPayTypeList($_form = '') { $pay_type_list_json = Option::get('payment', get_store_id(), 'store', '{"wechat":{"text":"微信支付","value":1}}'); $pay_type_list = Json::decode($pay_type_list_json['value']); if (!(is_array($pay_type_list) || $pay_type_list instanceof \ArrayObject)) { return []; } $new_list = []; foreach ($pay_type_list as $index => $value) { // if ($index == 'wechat' && $value['value'] == 1) { // $new_list[] = [ // 'name' => '微信支付', // 'payment' => 0, // ]; // } // // if ($index == 'alipay' && $value['value'] == 1) { // $new_list[] = [ // 'name' => '支付宝支付', // 'payment' => 4, // ]; // } if ((is_wechat_platform() || $_form === 'app' || $_form === 'h5') && $index == 'wechat' && $value['value'] == 1) { $new_list[] = [ 'name' => '微信支付', 'payment' => 1, // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png' ]; } if ((is_alipay_platform() || $_form === 'app') && $index == 'alipay' && $value['value'] == 1) { $new_list[] = [ 'name' => '支付宝支付', 'payment' => 4, // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-alipay.png' ]; } if ($index == 'balance' && $value['value'] == 1) { $balance = Option::get('recharge_wallet_status', get_store_id(), 'recharge'); if ($balance && $balance['value'] == 1) { $new_list[] = [ 'name' => '余额支付', 'payment' => 3, ]; } } } debug_log($new_list, 'pay_type.log'); return $new_list; } /** * 修改saas user信息 * @return \yii\web\Response */ public function actionSaasUserModify() { $form = new SaasUserForm(); $form->mobile = post_params('mobile'); $form->attributes = post_params(); return $this->asJson($form->modify()); } public function actionBalanceToCommission() { $price = (float)post_params('price'); if ($price <= 0) { return $this->asJson([ 'code' => 1, 'msg' => '参数错误', ]); } $user = get_user(); $store_id = get_store_id(); if ($user->is_distributor != 1) { return $this->asJson([ 'code' => 1, 'msg' => '您还不是分销商,暂不能把余额转到佣金', ]); } if ($price > $user->money) { return $this->asJson([ 'code' => 1, 'msg' => '待转的余额不能大于已有余额', ]); } $transaction = \Yii::$app->db->beginTransaction(); try { $money = $user->money; $user->money -= $price; $user->price += $price; $user->total_price += $price; $user->save(); $log = new AccountLog(); $log->store_id = $store_id; $log->user_id = $user->id; $log->type = AccountLog::TYPE_BALANCE; $log->log_type = AccountLog::LOG_TYPE_EXPEND; $log->amount = $price; $log->desc = '余额转分销佣金'; $log->before = $money; $log->after = $user->money; $log->operator = ''; $log->operator_id = 0; $log->order_type = AccountLog::TYPE_BALANCE_TO_COMMISSION; $log->order_id = 0; $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL; $log->created_at = time(); $res = $log->save(); if ($res) { $transaction->commit(); return $this->asJson([ 'code' => 0, 'msg' => '操作成功', ]); } $transaction->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => '操作失败!', ]); } catch (\Exception $e) { \Yii::error($e->getMessage()); $transaction->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => '操作失败!', ]); } } public function actionCodeToOpenid() { $code = post_params('code', ''); if (empty($code)) { return $this->asJson([ 'code' => 1, 'msg' => 'code为空' ]); } try { Wechat::init(get_store_id(), Wechat::WECHAT_KIND_OFFICIAL, WechatConfig::TYPE_CONFIG_MP); $result = Wechat::$wechat_official->oauth->userFromCode($code); if ($result && $result->getId()) { \Yii::error($result->getRaw()); $raw = $result->getRaw(); $user = get_user(); $user->wechat_platform_open_id = $result->getId(); if ($raw['unionid']) { $user->wechat_union_id = $raw['unionid']; } if (strpos($user->nickname, '******') !== false && $result->getNickname()) { $user->nickname = $result->getNickname(); } if (strpos($user->avatar_url, 'avatar.png') !== false && $raw['headimgurl']) { $user->avatar_url = $raw['headimgurl']; } if ($user->save()) { $saas_user = get_saas_user(); if (strpos($saas_user->name, '******') !== false) { $saas_user->name = $result->getNickname(); } if (strpos($saas_user->avatar, 'avatar.png') !== false) { $saas_user->avatar = $raw['headimgurl']; } $saas_user->save(); return $this->asJson([ 'code' => 0, 'msg' => '授权成功' ]); } } return $this->asJson([ 'code' => 1, 'msg' => '授权失败' ]); } catch (\Exception $e) { return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage() ]); } } //确定代理已经安装 public function actionAgentIsInstall() { $t = \Yii::$app->db->beginTransaction(); try { $order_detail_id = get_params('order_detail_id'); $order_detail = OrderDetail::findOne($order_detail_id); if (empty($order_detail)) { throw new \Exception("订单未找到"); } $agent_install_log = AgentGoodsInstallLog::findOne(['order_detail_id' => $order_detail_id]); if (!in_array($agent_install_log->status, [0, 1])) { throw new \Exception("已经操作完成,不可重复操作"); } $agent_install_log->status = 2; if (!$agent_install_log->save()) { throw new \Exception(json_encode($agent_install_log->errors)); } //判断是否全部确认收货 $install_all_log = AgentGoodsInstallLog::find()->where(['AND', ['order_id' => $agent_install_log->order_id]])->asArray()->all(); if ($install_all_log) { $count = OrderDetail::find()->where(['order_id' => $agent_install_log->order_id])->count(); if (count($install_all_log) >= $count) { $order = Order::findOne($agent_install_log->order_id); $order->trade_status = Order::ORDER_FLOW_CONFIRM; if (!$order->save()) { throw new \Exception("订单信息错误"); } } } //判断是否已过售后时间 $time = time(); $after_sale_time = Option::get(OptionSetting::STORE_AFTER_SALE_TIME, $this->store_id)['value']; $sale_time = $time - ($after_sale_time * 86400); $order = Order::find()->alias('o') ->where([ 'and', ['o.store_id' => get_store_id()], ['o.is_delete' => 0, 'o.trade_status' => Order::ORDER_FLOW_CONFIRM], ['<=', 'o.confirm_time', $sale_time], ['o.id' => $order_detail->order_id], ])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL]) ->leftJoin(OrderRefund::tableName() . ' r', "r.order_id = o.id and r.is_delete = 0") ->select(['o.*'])->groupBy('o.id') ->andWhere([ 'or', 'isnull(r.id)', ['r.type' => 2], ['in', 'r.status', [2, 3]], ])->asArray()->one(); if (!empty($order) && (int)$order['is_sale'] === 1) { $goods = Goods::findOne($order_detail->goods_id); if (!(!empty($goods) && $goods->cloud_goods_id)) { throw new \Exception('非云仓商品,不可操作'); } $agent_goods_bind_goods = AgentGoodsBindGoods::findOne(['cloud_goods_id' => $goods->cloud_goods_id]); if (empty($agent_goods_bind_goods)) { throw new \Exception('未绑定产品代理,不可操作'); } $option = Option::get('goods_agent_price_config', 0, 'saas', [])['value']; $option = json_decode($option, true); $goods_agent_install_percent = $option['goods_agent_install_percent'] ?? 0; $supplier = Supplier::findOne(['cloud_supplier_id' => $agent_goods_bind_goods->cloud_supplier_id]); if (empty($supplier)) { throw new \Exception('供货商查询失败,不可操作'); } $supplier_rate = $supplier->rate; //计算上门安装佣金 $admin = Admin::findOne($agent_install_log->goods_agent_admin_id); if (!empty($admin)) { $agent_rebate = $order_detail->total_price * ($supplier_rate / 100) * ($goods_agent_install_percent / 100); //给用户增加佣金 $SaasUser = SaasUser::findOne($admin->saas_user_id); $SaasUser->share_profit = ($SaasUser->share_profit * 1) + ($agent_rebate * 1); if (!$SaasUser->save()) { throw new \Exception(json_encode($SaasUser->errors)); } $StoreShareMoney = new StoreShareMoney(); $StoreShareMoney->user_id = $SaasUser->id; $StoreShareMoney->store_id = $order['store_id']; $StoreShareMoney->profit = $goods_agent_install_percent; $StoreShareMoney->total_price = $order_detail->total_price; $StoreShareMoney->desc = '产品代理上门安装佣金发放,安装商品' . $order_detail->goods_name; $StoreShareMoney->order_id = $order['id']; $StoreShareMoney->created_at = time(); $StoreShareMoney->type = 1; $StoreShareMoney->status = 2; $StoreShareMoney->commission = $agent_rebate; $StoreShareMoney->is_send = 1; if (!$StoreShareMoney->save()) { throw new \Exception(json_encode($StoreShareMoney->errors)); } } } $t->commit(); return $this->asJson([ 'code' => 0, 'msg' => '操作成功' ]); } catch (\Exception $e) { $t->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage() ]); } } public function actionCommissionToBalance() { $transaction = \Yii::$app->db->beginTransaction(); try { $price = post_params('price'); if (empty($price)) { return $this->asJson([ 'code' => 1, 'msg' => '缺少必要参数', ]); } $user = get_user(); $store_id = get_store_id(); if ($user->is_distributor != 1) { return $this->asJson([ 'code' => 1, 'msg' => '您还不是分销商,暂不能把佣金转到余额', ]); } if ($price > $user->price) { return $this->asJson([ 'code' => 1, 'msg' => '待转的佣金不能大于已有佣金数额', ]); } $money = $user->money; // $user->money += $price; //计算手续费 $profit = Option::get('commission_to_balance', get_store_id(), 'recharge', 0)['value']; $profit = (float)sprintf("%.2f", $price * ($profit / 100)); $user->price -= $price; $user->save(); $price = sprintf("%.2f", ($price - $profit)); AccountLog::saveLog($user->id, $price, 2, 1, 0, 0, '分销佣金转余额,手续费:' . $profit . '元', 3); $transaction->commit(); return $this->asJson([ 'code' => 0, 'msg' => '操作成功', ]); } catch (\Exception $e) { $transaction->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage() ]); } } //积分 / 余额转赠 public function actionGiveAssets() { $t = \Yii::$app->db->beginTransaction(); try { $user = get_user(); $saas_user = get_saas_user(); $give_user_id = post_params('give_user_id'); $money = post_params('money', 0); $type = post_params('type', 2); // 2 余额 1 积分 if (!in_array($type, [1, 2])) { throw new \Exception('参数类型错误'); } $give_user = User::find()->alias('u') ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile') ->where(['u.id' => $give_user_id, 'u.is_delete' => 0]) ->select('u.id, su.name')->asArray()->one(); if (!$give_user) { throw new \Exception('用户查找失败'); } if ((int)$type === 2) { //计算手续费 $profit = Option::get('give_balance_profit', get_store_id(), 'recharge', 0)['value']; if ($user->money < $money) { throw new \Exception('可转赠余额不足'); } } else { //计算手续费 $profit = Option::get('give_integral_profit', get_store_id(), 'recharge', 0)['value']; if ($user->integral < $money) { throw new \Exception('可转赠积分数量不足'); } } $profit = (float)sprintf("%.2f", $money * ($profit / 100)); AccountLog::saveLog($user->id, $money, $type, 2, 0, 0, '转赠给用户' . $give_user['name'] . ',手续费' . $profit, 1); $money = (float)sprintf("%.2f", ($money - $profit)); AccountLog::saveLog($give_user['id'], $money, $type, 1, 0, 0, '用户' . $saas_user->name . '转赠', 2); $t->commit(); return $this->asJson([ 'code' => 0, 'msg' => '操作成功' ]); } catch (\Exception $e) { $t->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage() ]); } } //查询用户 public function actionSearchUser() { try { $mobile = get_params('mobile'); if ($mobile) { $give_user = User::find()->alias('u') ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile') ->where(['u.binding' => $mobile, 'u.is_delete' => 0, 'u.store_id' => get_store_id()]) ->select('u.id, su.name, su.avatar')->asArray()->one(); if (empty($give_user)) { $give_user = null; } if ((int)$give_user['id'] === (int)get_user_id()) { $give_user = null; } } else { $give_user = null; } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => $give_user ]); } catch (\Exception $e) { return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage() ]); } } //获取佣金 余额 积分 转赠记录 public function actionGetAccountLog() { try { $type = (int)get_params('type', 0); $log_type = get_params('log_type'); $page = get_params('page', 1); $limit = get_params('limit', 10); $user_id = get_user_id(); $store_id = get_store_id(); switch ($type) { case 1: //积分 且 赠送 $type = 2; $from = [1, 2]; break; case 2://余额 且 佣金转 $type = 2; $from = 3; break; default: //余额 且 赠送 $type = 1; $from = [1, 2]; break; } $query = AccountLog::find()->where([ 'store_id' => $store_id, 'user_id' => $user_id, 'type' => $type, // 余额 'from' =>$from ]); if (in_array($log_type, [1, 2])) { $query->andWhere(['log_type' => $log_type]); } $count = $query->count(); $pagination = new Pagination(['totalCount' => $count, 'page' => $page - 1, 'pageSize' => $limit]); $list = $query->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->asArray()->all(); foreach ($list as $key => &$value) { $value['date'] = date('Y-m-d H:i:s', $value['created_at']); $value['content'] = $value['desc']; } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'list' => $list, 'page_count' => $pagination->getPageCount(), 'row_count' => $count,//总数 ] ]); }catch (\Exception $e) { return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage() ]); } } //获取手续费 public function actionGetProfit() { //计算手续费 $commission_to_balance = Option::get('commission_to_balance', get_store_id(), 'recharge', 0)['value']; //计算手续费 $give_balance_profit = Option::get('give_balance_profit', get_store_id(), 'recharge', 20)['value']; //计算手续费 $give_integral_profit = Option::get('give_integral_profit', get_store_id(), 'recharge', 25)['value']; return $this->asJson([ 'code' => '0', 'msg' => '获取成功', 'data' => [ "commission_to_balance" => $commission_to_balance, "give_balance_profit" => $give_balance_profit, "give_integral_profit" => $give_integral_profit ] ]); } /** * 修改手机号 */ public function actionUpdateMobile() { $t = \Yii::$app->db->beginTransaction(); try { $store_id = get_store_id(); $mobile = \input_params('mobile'); $code = \input_params('code'); if (!$mobile || !$code) { throw new \Exception('参数错误'); } $form = new LoginForm(); $form->store_id = $store_id; $form->phone = $mobile; $form->verify_code = $code; $verifySmsCode = $form->verifySmsCode(LoginForm::CACHE_KEY_BIND_PHONE); if ($verifySmsCode['code'] != 0) { throw new \Exception($verifySmsCode['msg']); } $is = User::findOne(['binding' => $mobile, 'is_delete' => 0]); if ($is) { throw new \Exception('该手机号已被绑定'); } $currentUser = get_user(); $users = User::find()->where(['binding' => $currentUser->binding, 'is_delete' => 0])->all(); foreach ($users as $user) { $user->binding = $mobile; $user->save(); } $saasUser = SaasUser::findOne(['mobile' => $currentUser->binding, 'is_delete' => 0]); if ($saasUser) { $saasUser->mobile = $mobile; $saasUser->save(); } $t->commit(); return $this->asJson([ 'code' => 0, 'msg' => '修改成功', ]); } catch (\Exception $e) { $t->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => $e->getMessage() ]); } } }