123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Http\Controllers\Controller;
- use App\Models\Admin;
- use App\Models\Amount;
- use App\Models\Apply;
- use App\Models\Agent\AgentDwbsUser;
- use App\Models\Comment;
- use App\Models\DwbsUser;
- use App\Models\DwbsWarea;
- use App\Models\DwbsAdmin;
- use App\Models\GiftOrders;
- use App\Models\Order;
- use App\Models\Store;
- use App\Models\User;
- use App\Handlers\SignHandler;
- use App\Models\Warea;
- use Carbon\Carbon;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class StoreController extends Controller
- {
- //获取数据信息
- public function getDataInfo(){
- $admin_id=Auth::user()->id;
- $admin=Admin::where('id',$admin_id)->first();
- $lists=[];
- if($admin && in_array($admin->role_id,[22,24])){//超级管理员
- // $agent_ids=DwbsUser::where('status',0)->where('cert_status',6)->whereNull('deleted_at')->where('service_status',0)->pluck('id');
- $apply=Store::query()->groupBy('is_apply')->select(DB::raw('count(id) as num'),'is_apply')->pluck('num','is_apply');//->whereIn('user_id',$agent_ids)
- }else{
- $agent_ids=DwbsUser::whereHas('warea',function($query)use($admin_id){
- $query->where('admin_id',$admin_id);
- })->where('status',0)->where('cert_status',6)->whereNull('deleted_at')->where('service_status',0)->pluck('id');
- $apply=Store::whereIn('user_id',$agent_ids)->groupBy('is_apply')->select(DB::raw('count(id) as num'),'is_apply')->pluck('num','is_apply');
- }
- $lists['no_cert']=isset($apply[0])?$apply[0]:0;
- $lists['authing']=isset($apply[1])?$apply[1]:0;
- $lists['wait_sign']=isset($apply[2])?$apply[2]:0;
- $lists['finish']=isset($apply[3])?$apply[3]:0;
- $lists['rejected']=isset($apply[4])?$apply[4]:0;
- $lists['frozen']=isset($apply[5])?$apply[5]:0;
- $lists['delete']=Store::onlyTrashed()->where('is_failure',0)->count();
- return $this->success($lists);
- }
- //获取所有门店
- public function list(Request $request){
- $page_size=$request->input('page_size');
- $page_index=$request->input('page_index');
- $num=$page_size*($page_index-1);
- $admin_id=Auth::user()->id;
- $admin=Admin::where('id',$admin_id)->first();
- Log::info('--mendian--'.microtime());
- if($admin && in_array($admin->role_id,[22,24])){//超级管理员
- Log::info('--mendian1.1--'.microtime());
- $agent_ids=DwbsUser::where('status',0)->where('cert_status',6)->whereNull('deleted_at')->where('service_status',0)->pluck('id');
- $total=count($agent_ids);//所有可开通店铺人数
- Log::info('--mendian1.2--'.microtime());
- $data=Store::where('is_failure',0);
- }else{
- $warea_ids=DwbsWarea::where('admin_id',$admin_id)->pluck('id');
- if($request->has('is_delete') && $request->input('is_delete')==1){
- $agent_ids=DwbsUser::withTrashed()->where('status',0)->where('cert_status',6)->whereNotNull('deleted_at')->where('service_status',0)->whereIn('warea_id',$warea_ids)->pluck('id');
- }else{
- $agent_ids=DwbsUser::where('status',0)->where('cert_status',6)->whereNull('deleted_at')->where('service_status',0)->whereIn('warea_id',$warea_ids)->pluck('id');
- }
- $total=count($agent_ids);//政委可开通店铺人数
- $store_ids=Store::withTrashed()->where('is_failure',0)->whereIn('user_id',$agent_ids)->pluck('id');
- $data=Store::where('is_failure',0)->whereIn('id',$store_ids);
- }
- $data->with(['user' => function($query){
- $query->with(['warea:id,name','crown:id,nickname'])->select('id','nickname','mobile','level','agent_id','crown_id','warea_id','deleted_at');
- }]);
- $nums=$data->count();
- Log::info('--mendian2.2--'.microtime());
- //审核状态筛选
- if($request->has('is_apply') && $request->input('is_apply') <> 'all'){
- $data->where('is_apply',$request->input('is_apply'));
- }
- //代理筛选
- if($request->has('search_name') && $request->input('search_name')) {
- $data->where(function($query)use($request){
- $query->where('name','like','%'.$request->input('search_name').'%')
- ->orWhere('phone','like','%'.$request->input('search_name').'%');
- });
- }
- if($request->has('is_delete') && $request->input('is_delete')==1){
- $data->onlyTrashed()->where('is_failure',0);
- }
- $count=$data->count();
- Log::info('--mendian2.3--'.microtime());
- $data->select('id','name','img','user_id','created_at','status','is_apply','created_at','integral','cycle_inte',
- 'phone','star','province','city','area','address','username','idCard','account_bank','account_number','deleted_at','is_failure');
- $list=$data->orderBy('id','desc')->skip($num)->take($page_size)->get();
- Log::info('--mendian2.4--'.microtime());
- if(Cache::has('gift_orders')){
- $gift_orders=Cache::get('gift_orders');
- }else{
- $gift_orders=GiftOrders::where('type',0)->groupBy('store_id')
- ->select('store_id',DB::raw('sum(num*bonus) as integral'))->pluck('integral','store_id')->toArray();
- Cache::add('gift_orders', $gift_orders, 30);
- }
- Log::info('代理时间计算-3-'.microtime());
- foreach($list as $key=>$val){
- // $second=mt_rand(2880,7200);
- $warea=$val->user->warea;
- $list[$key]->warea=$warea['name'];
- $crown=$val->user->crown;
- $list[$key]->company=$crown['nickname'];
- if(array_key_exists($val->id, $gift_orders)){
- $list[$key]->finish_integral=$gift_orders[$val->id];
- }else{
- $list[$key]->finish_integral=0;
- }
- if(!empty($val->user) && $val->user->level==3){
- $list[$key]->company=$val->user->nickname;
- }
- }
- Log::info('代理时间计算-4-'.microtime());
- $lists['list']=$list;
- $lists['num']=$nums;
- $lists['total']=$total;
- Log::info('代理时间计算-5-'.microtime());
- return $this->success_list($lists,'成功',$count);
- }
- //导出所有门店
- public function export(Request $request){
- ini_set('memory_limit', '1280M');
- $admin_id=Auth::user()->id;
- $admin=Admin::where('id',$admin_id)->first();
- Log::info('--mendian--'.microtime());
- if($admin && in_array($admin->role_id,[22,24])){//超级管理员
- $data=Store::where('is_failure',0);
- }else{
- $warea_ids=DwbsWarea::where('admin_id',$admin_id)->pluck('id');
- $agent_ids=DwbsUser::where('status',0)->where('cert_status',6)->whereNull('deleted_at')->where('service_status',0)->whereIn('warea_id',$warea_ids)->pluck('id');
- $data=Store::where('is_failure',0)
- ->whereIn('user_id',$agent_ids);
- }
- // $data->with('user:id,nickname,mobile,level,agent_id,crown_id,warea_id,deleted_at');
- $data->with(['user' => function($query){
- $query->with(['warea:id,name','crown:id,nickname'])->select('id','nickname','mobile','level','agent_id','crown_id','warea_id','deleted_at');
- }]);
- //审核状态筛选
- if($request->has('is_apply') && $request->input('is_apply') <> 'all'){
- $data->where('is_apply',$request->input('is_apply'));
- }
- // //代理筛选
- if($request->has('search_name') && $request->input('search_name')) {
- $data->where(function($query)use($request){
- $query->where('name','like','%'.$request->input('search_name').'%')
- ->orWhere('phone','like','%'.$request->input('search_name').'%');
- });
- }
- if($request->has('search_name') && $request->input('is_delete')==1){
- $data->onlyTrashed()->where('is_failure',0);
- }
- $data->select('id','name','img','user_id','created_at','status','is_apply','created_at','integral','cycle_inte',
- 'phone','star','province','city','area','address','username','idCard','account_bank','account_number','deleted_at','is_failure');
- $list=$data->orderBy('id','desc')->get();
- //return $this->success($list);
- foreach($list as $key=>$val){
- $warea=$val->user->warea;
- $list[$key]->warea=$warea['name'];
- $crown=$val->user->crown;
- $list[$key]->company=$crown['nickname'];
- // if(array_key_exists($val->id, $gift_orders)){
- // $list[$key]->finish_integral=$gift_orders[$val->id];
- // }else{
- // $list[$key]->finish_integral=0;
- // }
- if(!empty($val->user) && $val->user->level==3){
- $list[$key]->company=$val->user->nickname;
- }
- }
- return $this->success($list);
- $admin_id=Auth::user()->id;
- $admin=Admin::where('id',$admin_id)->first();
- if($admin && in_array($admin->role_id,[22,24])){//超级管理员
- $store_user_ids=Store::pluck('user_id');
- $data=Store::with('user:id,nickname,mobile,level,agent_id');
- }else{
- $warea_ids=DwbsWarea::where('admin_id',$admin_id)->pluck('id');
- $agent_ids=DwbsUser::whereIn('warea_id',$warea_ids)->pluck('id');
- $store_ids=Store::whereIn('user_id',$agent_ids)->pluck('id');
- $store_user_ids = Store::whereIn('id',$store_ids)->pluck('user_id');
- $data=Store::with('user:id,nickname,mobile,level,agent_id')->whereIn('id',$store_ids);
- }
- //审核状态筛选
- if($request->has('is_apply') && ($request->input('is_apply') || $request->input('is_apply')==0) && $request->input('is_apply') <> 'all'){
- $data->where('is_apply',$request->input('is_apply'));
- }
- //代理筛选
- if($request->has('search_name') && $request->input('search_name')) {
- $data->where(function($query)use($request){
- $query->where('name','like','%'.$request->input('search_name').'%')
- ->orWhere('phone','like','%'.$request->input('search_name').'%');
- });
- }
- $list=$data->with(['comment','gift_order'])->orderBy('id','desc')->get();
- $wareas=Cache::remember('wareas',Carbon::now()->addHours(2),function (){
- return DwbsWarea::pluck('name','id');
- });
- $warea_user_ids=Cache::remember('warea_user_ids',Carbon::now()->addHours(2),function()use($store_user_ids){
- return DwbsUser::withTrashed()->whereIn('id',$store_user_ids)->pluck('warea_id','id');
- });
- if(Cache::has('gift_orders')){
- $gift_orders=Cache::get('gift_orders');
- }else{
- $gift_orders=GiftOrders::where('type',0)->groupBy('store_id')
- ->select('store_id',DB::raw('sum(num*bonus) as integral'))->pluck('integral','store_id')->toArray();
- Cache::add('gift_orders', $gift_orders, 30);
- }
- $result=$data->orderBy('id','desc')
- ->chunk('1000',function($lists)use($wareas,$warea_user_ids,$gift_orders){
- foreach($lists as $key=>$val){
- $list=$val;
- $second=mt_rand(86400,259200);
- if ($val->comment){
- $star=floor($val->comment->avg('star'));
- $list->star=$star==null?0:$star;
- }else{
- $list->star=0;
- }
- if(empty($val->user)){
- Log::info('为空拍给你们');
- Log::info($val);
- }
- if(!empty($val->user)){
- if(Cache::has('warea-'.$val->user->id)){
- $list->warea=Cache::get('warea-'.$val->user->id);
- }else{
- $warea_id=$warea_user_ids[$val->user_id];
- $list->warea=$wareas[$warea_id];
- Cache::add('warea-'.$val->user->id, $wareas[$warea_id], $second);
- }
- }
- if(array_key_exists($val->id, $gift_orders)){
- $list->finish_integral=$gift_orders[$val->id];
- }else{
- $list->finish_integral=0;
- }
- if(!empty($val->user) && $val->user->level==2){
- if(Cache::has('company-'.$val->user->id)){
- $list->company=Cache::get('company-'.$val->user->id);
- }else{
- $company=DwbsUser::withTrashed()->where('id',$val->user->agent_id)->value('nickname');
- $list->company=$company;
- Cache::add('company-'.$val->user->id, $company, $second);
- }
- }
- if(!empty($val->user) && $val->user->level==1){
- if(Cache::has('company-'.$val->user->id)){
- $list->company=Cache::get('company-'.$val->user->id);
- }else{
- $agent=DwbsUser::withTrashed()->with('agent:id,nickname')->where('id',$val->user->agent_id)->first();
- if($agent->level==3){
- $company=$agent->nickname;
- }else{
- $company=$agent->agent->nickname;
- }
- $list->company=$company;
- Cache::put('company-'.$val->user->id, $company, $second);
- }
- }
- if(!empty($val->user) && $val->user->level==3){
- $list->company=$val->user->nickname;
- }
- $GLOBALS['store'][] = $list;
- }
- });
- return $this->success($GLOBALS['store']);
- // $list=$data->has('user')->with(['comment','gift_order'])->orderBy('id','desc')->get();
- // return $this->success($list);
- return $this->success($list);
- }
- // //导出所有门店
- // public function export123(Request $request){
- // $admin_id=Auth::user()->id;
- // $admin=Admin::where('id',$admin_id)->first();
- // if($admin && in_array($admin->role_id,[22,24])){//超级管理员
- // $data=Store::with('user:id,nickname,mobile,level,agent_id');
- // }else{
- // $warea_ids=DwbsWarea::where('admin_id',$admin_id)->pluck('id');
- // $agent_ids=DwbsUser::whereIn('warea_id',$warea_ids)->pluck('id');
- // $store_ids=Store::whereIn('user_id',$agent_ids)->pluck('id');
- // $data=Store::with('user:id,nickname,mobile,level,agent_id')->whereIn('id',$store_ids);
- // }
- // //审核状态筛选
- // if($request->has('is_apply') && ($request->input('is_apply') || $request->input('is_apply')==0) && $request->input('is_apply') <> 'all'){
- // $data->where('is_apply',$request->input('is_apply'));
- // }
- // //代理筛选
- // if($request->has('search_name') && $request->input('search_name')) {
- // $data->where('name','like','%'.$request->input('search_name').'%')
- // ->orWhere('phone','like','%'.$request->input('search_name').'%');
- // }
- // $list=$data->orderBy('id','desc')->get();
- // $wareas=DwbsWarea::pluck('name','id');
- // $store_user_ids=Store::pluck('user_id');
- // $warea_user_ids=DwbsUser::withTrashed()->whereIn('id',$store_user_ids)->pluck('warea_id','id')->toArray();
- // $gift_orders=GiftOrders::where('type',0)->groupBy('store_id')
- // ->select('store_id',DB::raw('sum(num*bonus) as integral'))->pluck('integral','store_id')->toArray();
- // foreach($list as $key=>$val){
- // $star=Comment::where('store_id',$val->id)->average('star');
- // $list[$key]->star=floor($star);
- // $warea_id=$warea_user_ids[$val->user_id];
- // $list[$key]->warea=$wareas[$warea_id];
- // if(array_key_exists($val->id, $gift_orders)){
- // $list[$key]->finish_integral=$gift_orders[$val->id];
- // }else{
- // $list[$key]->finish_integral=0;
- // }
- // if(!empty($val->user) && $val->user->level==2){
- // $list[$key]->company=DwbsUser::where('id',$val->user->agent_id)->value('nickname');
- // }
- // if(!empty($val->user) && $val->user->level==1){
- // $agent_id=DwbsUser::where('id',$val->user->agent_id)->value('agent_id');
- // $list[$key]->company=DwbsUser::where('id',$agent_id)->value('nickname');
- // }
- // if(!empty($val->user) && $val->user->level==3){
- // $list[$key]->company=$val->user->nickname;
- // }
- // }
- // return $this->success($list);
- // }
- //获取所有用户
- public function getAllUser(Request $request){
- $page_size=$request->input('page_size');
- $page_index=$request->input('page_index');
- $store_id=$request->input('store_id');
- $num=$page_size*($page_index-1);
- $data=Order::leftJoin('user as u','u.id','=','order.user_id')->where('order.store_id',$store_id)
- ->where('order.is_pay',1)->whereIn('order.apply_cancel',[0,1,3])->whereIn('order.is_refund',[0,1])
- ->groupBy('order.user_id')->select('u.nickname','u.phone','u.avatar',DB::raw('sum(order.total) as total'),DB::raw('sum(order.account) as account'));
- // $data=User::with('store:id,name')->where('store_id',$store_id)
- // ->select('id','nickname','name','phone','avatar','status','store_id');
- if($request->has('search_name') && $request->input('search_name')){
- $data->where(function($query)use($request){
- $query->where('u.phone',$request->input('search_name'))
- ->orWhere('u.nickname','like','%'.$request->input('search_name').'%');
- });
- }
- // if($store_id){
- // $data->where('store_id',$store_id);
- // }
- $count=$data->get()->count();
- $list=$data->orderByDesc('total')->orderByDesc('account')->OrderBy('u.id')->skip($num)->take($page_size)->get();
- return $this->success_list($list,'成功',$count);
- }
- //店铺禁用
- public function disabled(Request $request){
- $store=Store::where('id',$request->input('id'))->first();
- $store->status=$store->status ? 0 : 1;
- $res=$store->save();
- if($res){
- return $this->success([]);
- }else{
- return $this->error();
- }
- }
- //删除店铺
- public function deleteStore(Request $request)
- {
- $admin_id=Auth::user()->id;
- $admin=Admin::where('id',$admin_id)->first();
- $id=$request->input('id');
- try{
- DB::beginTransaction();
- $store=Store::where('id',$id)->first();
- $store->remark .= '|'.$admin->name.'-'.date("Y-m-d H:i:s").'删除店铺';
- $store->save();
- Store::where('id',$id)->delete();
- DB::commit();
- return $this->success([]);
- }catch(Exception $e){
- DB::rollBack();
- return $this->error($e->getMessage());
- }
- }
- //恢复店铺删除
- public function resDelStore(Request $request)
- {
- $admin_id=Auth::user()->id;
- $admin=Admin::where('id',$admin_id)->first();
- $id=$request->input('id');
- try{
- DB::beginTransaction();
- Store::withTrashed()->where('is_failure',0)->where('id',$id)->restore();
- $store=Store::where('id',$id)->first();
- $store->remark .= '|'.$admin->name.'-'.date("Y-m-d H:i:s").'恢复店铺删除';
- $store->save();
- DB::commit();
- return $this->success([]);
- }catch(Exception $e){
- DB::rollBack();
- return $this->error($e->getMessage());
- }
- }
- //重开店铺
- public function newStore(Request $request)
- {
- $admin_id=Auth::user()->id;
- $admin=Admin::where('id',$admin_id)->first();
- $id=$request->input('id');
- try{
- DB::beginTransaction();
- $store=Store::onlyTrashed()->where('id',$id)->first();
- $store->is_failure = 1;
- $store->save();
- $user=DwbsUser::where('id',$store->user_id)->first();
- Store::create([
- 'img'=>$user->headimgurl,
- 'name'=>$user->nickname,
- 'user_id'=>$user->id,
- 'phone'=>$user->mobile,
- 'remark'=>$admin->name.'-'.date("Y-m-d H:i:s").'重开店铺'
- ]);
- DB::commit();
- return $this->success([]);
- }catch(Exception $e){
- DB::rollBack();
- return $this->error($e->getMessage());
- }
- }
- //查询进件状态
- public function query(Request $request){
- $store_id=$request->input('id');
- $store=Store::withTrashed()->where('is_failure',0)->where('id',$store_id)->first();
- $applyment_id=$store->applyment_id??$request->input('applyment_id');//申请编号
- if(empty($applyment_id)){
- Log::info($store->name.'未提交认证');
- return $this->success([]);
- }
- $handle=new SignHandler();
- $url='https://api.mch.weixin.qq.com/v3/ecommerce/applyments/'.$applyment_id;//查询地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$handle->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$handle->nonce_str();//随机字符串
- $body="";
- $sign=$handle->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $handle->getzhengshu()//获取平台证书序列号
- ];
- $result=$handle->curl($url,'',$header,'GET');
- $result=json_decode($result,true);
- $apply=Apply::where('store_id',$store->id)->where('applyment_id',$applyment_id)->OrderBy('id','desc')->first();
- if($result['applyment_state']=="ACCOUNT_NEED_VERIFY"){
- Apply::where('id',$apply->id)->update(['audit_detail'=>$result['legal_validation_url']]);
- }
- if($result['applyment_state']=="NEED_SIGN"){
- Store::withTrashed()->where('is_failure',0)->where('id',$store_id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>2]);
- Apply::where('id',$apply->id)->update(['sub_mchid'=>$result['sub_mchid'], 'audit_detail'=>$result['sign_url']]);
- }
- if($result['applyment_state']=="FINISH" && $store->is_apply != 3){
- Store::withTrashed()->where('is_failure',0)->where('id',$store_id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>3]);//'is_cert'=>1,
- Apply::where('id',$apply->id)->update(['sub_mchid'=>$result['sub_mchid']]);
- }
- if($result['applyment_state']=="REJECTED"){//已驳回
- Store::withTrashed()->where('is_failure',0)->where('id',$store_id)->update(['is_apply'=>4]);
- Apply::where('id',$apply->id)->update(['audit_detail'=>$result['audit_detail'][0]['reject_reason']]);
- }
- if($result['applyment_state']=="FROZEN"){
- Store::withTrashed()->where('is_failure',0)->where('id',$store_id)->update(['is_apply'=>5]);//已冻结
- Apply::where('id',$apply->id)->update(['audit_detail'=>$result['audit_detail'][0]['reject_reason']]);
- }
- if($apply->applyment_state != $result['applyment_state'] && $apply->applyment_state_desc != $result['applyment_state_desc']){
- Apply::where('id',$apply->id)->update(['applyment_state'=>$result['applyment_state'],'applyment_state_desc'=>$result['applyment_state_desc']]);
- }
- return $this->success($result);
- // $result['applyment_state']=="FINISH"; //完成签约 其它状态:CHECKING:资料校验中 ACCOUNT_NEED_VERIFY:待账户验 AUDITING:审核中 REJECTED:已驳回 NEED_SIGN:待签约 FINISH:完成 FROZEN:已冻结
- }
- //获取账户实时余额
- public function getAccount($user_id){
- $handle=new SignHandler();
- $sub_mchid=Store::where('user_id',$user_id)->value('sub_mchid');
- $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/balance/'.$sub_mchid;//查询地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$handle->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$handle->nonce_str();//随机字符串
- $body="";
- $sign=$handle->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $handle->getzhengshu()//获取平台证书序列号
- ];
- $result=$handle->curl($url,'',$header,'GET');
- $result=json_decode($result,true);
- return $result;
- }
- //获取账户实时余额
- public function getAccountInfo(Request $request){
- $handle=new SignHandler();
- $id=$request->input('id');
- $sub_mchid=Store::withTrashed()->where('is_failure',0)->where('id',$id)->value('sub_mchid');
- // return $sub_mchid;
- $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/balance/'.$sub_mchid;//查询地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$handle->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$handle->nonce_str();//随机字符串
- $body="";
- $sign=$handle->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $handle->getzhengshu()//获取平台证书序列号
- ];
- $result=$handle->curl($url,'',$header,'GET');
- $result=json_decode($result,true);
- if($result['sub_mchid']){
- Store::withTrashed()->where('is_failure',0)->where('sub_mchid',$result['sub_mchid'])
- ->update([
- 'available_amount'=>round($result['available_amount']/100,2),
- 'pending_amount'=>round($result['pending_amount']/100,2)
- ]);
- }
- return $this->success($result);
- }
- //商户提现状态查询
- public function getWithdrawalState(Request $request){
- $handle=new SignHandler();
- $id=$request->input('id');
- $withdraw_id=$request->input('withdraw_id');
- $out_request_no=$request->input('out_request_no');
- $sub_mchid=Store::withTrashed()->where('is_failure',0)->where('id',$id)->value('sub_mchid');
- $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/'.$withdraw_id.'?sub_mchid='.$sub_mchid;//查询地址
- // $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/out-request-no/'.$out_request_no.'?sub_mchid='.$sub_mchid;
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$handle->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$handle->nonce_str();//随机字符串
- $body="";
- $sign=$handle->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $handle->getzhengshu()//获取平台证书序列号
- ];
- $result=$handle->curl($url,'',$header,'GET');
- $result=json_decode($result,true);
- if(!isset($result['status'])){
- return $this->error('450001',$result['message']);
- }
- if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
- $reason=$result['reason'];
- }else{
- $reason=null;
- }
- Amount::where('transaction_id',$withdraw_id)
- ->update([
- 'status'=>0,
- 'status_code'=>$result['status'],
- 'reason'=>$reason,
- ]);
- return $this->success($result);
- }
- //获取收款记录
- public function getAccountDetail(Request $request){
- $id=$request->input('id');
- $type=$request->input('type');
- $cycle=$request->input('cycle');
- $page_size=$request->input('page_size');
- $page_index=$request->input('page_index');
- $num=$page_size*($page_index-1);
- $data= Amount::where('store_id',$id)->where('type',$type);
- switch($cycle){
- case 'year':
- $data->whereYear('created_at',date("Y"));
- break;
- case 'today':
- $data->whereDate('created_at',date("Y_m-d"));
- break;
- case 'week':
- $data->whereBetween('created_at',[date("Y-m-d",strtotime("-7 day")),date("Y-m-d 23:59:59")]);
- break;
- case 'month':
- $data->whereBetween('created_at',[date("Y-m-01"),date("Y-m-d 23:59:59")]);
- break;
- case 'quarter':
- $start=(ceil(date("m")/3)-1)*3+1;
- $data->whereBetween('created_at',[date("Y-".$start."-01"),date("Y-m-d 23:59:59")]);
- break;
- default :
- // $data->whereDate('created_at',date("Y-m-d"));
- }
- $count=$data->count();
- if($type==3){
- $cc=clone $data;
- $ll['account']=$cc->where('status_code','SUCCESS')->sum('money');
- }else{
- $ll['account']=$data->sum('money');
- }
- $ll['list']=$data->OrderByDesc('id')->skip($num)->take($page_size)->get();
- return $this->success_list($ll,'',$count);
- }
- //重置审核状态
- public function setWxAuth(Request $request){
- $store_id=$request->input('store_id');
- $store=Store::where('id',$store_id)->first();
- if($store->is_apply!=2 && $store->is_apply!=4){
- return $this->error('450001','当前状态不能重置');
- }
- $res=Store::where('id',$store_id)->update([
- 'out_request_no'=>null,
- 'is_apply'=>0
- ]);
- if($res){
- return $this->success([]);
- }else{
- return $this->error();
- }
- }
- //代理详情
- public function getUserDetails(Request $request)
- {
- $input = $request->all();
- $user = DwbsUser::withTrashed()->with(['recom:id,nickname,mobile', 'agent:id,nickname,mobile'
- ])->where('mobile', $input['phone'])->first();
- if ($user) {
- // if ($user->level == '3') {
- // $childAgent = DwbsUser::where('agent_id', $user->id)->select('id', 'nickname', 'level', 'mobile')->get()->toArray();
- // $childAgentIds = array_column($childAgent, 'id');
- // if ($childAgentIds) {
- // $childChildAgent = DwbsUser::whereIn('agent_id', $childAgentIds)->select('id', 'nickname', 'level', 'mobile')->get()->toArray();
- // $user->child_agent = array_merge($childAgent, $childChildAgent);
- // } else {
- // $user->child_agent = $childAgent;
- // }
- // $user->childAgentCount = count($user->child_agent);
- // } elseif ($user->level == '2') {
- // $user->child_agent = DwbsUser::where('agent_id', $user->id)->select('id', 'nickname', 'level', 'mobile')->get()->toArray();
- // $user->childAgentCount = count($user->child_agent);
- // $user->crown=$this->get_crown($user->id);
- // } else {
- // $user->child_agent = [];
- // $user->childAgentCount = 0;
- // $user->crown=$this->get_crown($user->id);
- // }
- if ($user->warea_id) {
- $warea = DwbsWarea::where('id', $user->warea_id)->first();
- $warea->warea_name = $warea->name;
- if ($warea->admin_id) {
- $warea->commissar = DwbsAdmin::where('id', $warea->admin_id)->value('name');
- }
- $user->warea=$warea;
- }
- if (!empty($user->deleted_at)) {
- $user->delete = '1';//已删除
- } else {
- $user->delete = '0';//未删除
- }
- // $user->up_team=$this->getUpTeam($user->id);
- return $this->success($user);
- } else {
- return $this->error('400004', '代理信息不存在');
- }
- }
- public function GetStoreUser(){
- $user_ids=Store::pluck('user_id');
- $ids=DwbsUser::onlyTrashed()->whereIn('id',$user_ids)->pluck('id');
- // ->select('id','nickname','mobile','level','deleted_at')->get();
- // Store::whereIn('user_id',$ids)->update([
- // 'deleted_at'=>Carbon::now(),
- // 'remark'=>DB::raw('remark').'|统一删除被删除代理店铺'
- // ]);
- return count($ids);
- $user_ids=DwbsUser::onlyTrashed()->pluck('id');
- return count($user_ids);
- $count=Store::whereIn('user_id',$user_ids)->count();
- return $count;
- // $data=Store::query();
- // $data->where('id',22071);
- //// dump($data->count());
- //// $data->with('user:id,nickname,mobile,level,agent_id,crown_id,warea_id,deleted_at');
- // $data->whereHas('dwuser',function(Builder $builder){
- // $builder->from(DB::raw("app.users"));
- // });
- // dump($data->count());
- // dd($data->get());
- }
- }
|