123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\DwbsUser;
- use App\Models\OrderDetail;
- use App\Models\OrderRefund;
- use App\Models\ShopCar;
- use Illuminate\Http\Request;
- use App\Models\User;
- use App\Models\Goods;
- use App\Models\Order;
- use App\Models\Store;
- use App\Models\Scan;
- use App\Models\Comment;
- use GuzzleHttp\Client;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Validator;
- class StoreController extends Controller
- {
- public function exportUser(){
- $ids=DwbsUser::where('level',3)->whereNull('deleted_at')->pluck('id');
- $data=Store::whereIn('user_id',$ids)->select('phone','username','idCard','name')->get();
- return $this->success($data);
- }
- //获取店铺首页信息
- // public function getUserStoreDiff(Request $request)
- // {
- // $user_id = Auth::user()->id;
- // $user = User::where('id', $user_id)->first();
- // $store_id = $request->input('store_id');
- // $data['type'] = 'noReplace';
- // if (empty($user->store_id)) {//未绑定
- // if ($user->store_id == $store_id) {
- // return $this->error('450011', '您未关联店铺信息,请联系代理扫码登陆');
- // } else {
- // $store = Store::where('id', $request->input('store_id'))
- // ->select('id','name','img','phone','status','user_id')->first();
- // if (empty($store)) {
- // return $this->error('450001', '扫码店铺信息不存在');
- // } else {
- // if ($store->status == 1)
- // return $this->error('450001', '店铺已被禁用');
- // User::where('id', $user->id)->update(['store_id' => $request->input('store_id')]);
- // Scan::create([
- // 'user_id' => $user->id,
- // 'store_id' => $store->id,
- // 'is_store' => 2,//店铺
- // ]);
- // }
- // }
- // } else {//已绑定
- // if (!empty($store_id) && $user->store_id != $store_id) {
- // $data['type'] = 'replace';
- // $data['store_id'] = $store_id;
- // $data['is_vip'] = $user->is_vip;
- // $data['before_store']=Store::where('id',$user->store_id)->value('name');
- // $data['after_store']=Store::where('id',$store_id)->value('name');
- // }
- // $store = Store::where('id', $user->store_id)->first();
- // if (empty($store))
- // return $this->error('450001', '用户绑定店铺信息不存在');
- // if ($store->status == 1)
- // return $this->error('450001', $store->name . '已被禁用');
- // }
- // return $this->success($data);//需要替换店铺
- // }
- //获取店铺信息
- public function getStoreIndex(Request $request){
- $store_id=$request->input('store_id');
- $goods_id=$request->input('goods_id');
- $store=Store::where('id',$store_id)->first();
- if(empty($store)){
- return $this->error('450011', '店铺信息不存在,请联系代理扫码进入店铺');
- }
- // $star=Comment::where('store_id',$store_id)->where('is_reply',1)->avg('star');
- // $store->star=floor($star);
- $arr_test=Store::getTest();
- if(in_array($store->id,$arr_test)){
- $store->is_test=1;
- }else{
- $store->is_test=0;
- }
- if(!empty($goods_id)){
- $goods_ids=explode(',',$goods_id);
- $goods=Goods::where('is_delete',0)->where('is_shelves',1)->whereIn('id',$goods_ids)->get();
- }else{
- $goods=Goods::where('is_delete',0)->where('is_shelves',1)->get();
- }
- $user_id=Auth::user()->id;
- $user=User::where('id',$user_id)->first();
- if($store_id && $goods_id) {
- $scan = Scan::where('user_id', $user_id)->where('store_id', $store_id)->first();
- if (!$scan) {
- Scan::create([
- 'user_id' => $user_id,
- 'store_id' => $store_id,
- 'goods_id' =>$goods_id,
- 'order_no' => null,
- 'is_store' => 1
- ]);
- }
- }
- if($store_id && !$goods_id){
- $scan = Scan::where('user_id', $user_id)->where('store_id', $store_id)->first();
- if (!$scan) {
- Scan::create([
- 'user_id'=>$user_id,
- 'store_id'=>$store_id,
- 'goods_id'=>null,
- 'order_no'=>null,
- 'is_store'=>2
- ]);
- }
- }
- $dwbsUser = DwbsUser::where('mobile',$user->phone)
- ->where(function($query){
- $query->where('cert_status',3)
- ->orWhere('cert_status',6);
- })
- ->where('service_status',0)
- ->whereNull('deleted_at')->first();
- if($dwbsUser){
- $data['is_agent'] = 1;
- }else{
- $data['is_agent'] = 0;
- }
- $data['store']=$store;
- $data['goods']=$goods;
- return $this->success($data);
- }
- //获取购物车商品数量
- public function getShopCarNum(){
- $user_id=Auth::user()->id;
- $car_num=ShopCar::where('user_id',$user_id)->sum('num');
- return $this->success($car_num);
- }
- //替换店铺信息
- public function replaceStore(Request $request){
- $user_id=Auth::user()->id;
- $store_id=$request->input('store_id');
- $store=Store::where('id',$store_id)->first();
- if(empty($store)){
- return $this->error('450001','扫码店铺信息有误');
- }
- $res=User::where('id',$user_id)->update(['store_id'=>$store_id,'is_vip'=>0]);
- if($res){
- return $this->success([]);
- }else{
- return $this->error('450001','更换店铺失败');
- }
- }
- //发布店铺评论
- public function addStoreComment(Request $request)
- {
- $rules=[
- 'content'=>'max:255',
- ];
- $message=[
- 'content.max'=>'评价内容不能大于255个字符.'
- ];
- $validator = Validator::make($request->input(),$rules,$message);
- if($validator->fails()){
- return $this->error('400013',$validator->errors()->first());
- }
- try{
- DB::transaction(function()use($request){
- $user=Auth::user();
- $star=$request->input('star') ?? 0;
- $user_id=$user->id;
- $content=$request->input('content');
- $order_no=$request->input('order_no');
- $order=Order::where('order_no',$order_no)->first();
- if($order){
- $order->is_comment=1;
- $order->save();
- $order_id=$order->id;
- }else{
- $order_id=null;
- }
- Comment::create([
- 'user_id'=>$user_id,
- 'store_id'=>$order->store_id,
- 'content'=>$content,
- 'order_id'=>$order_id,
- 'comment_id'=>0,
- 'star'=>$star,
- 'is_reply'=>1,
- 'is_see'=>0
- ]);
- $star=Comment::where('store_id',$order->store_id)
- ->where('is_reply',1)->avg('star');
- Store::lockForUpdate()->where('id',$order->store_id)->update(['star'=>floor($star)]);
- },5);
- return $this->success([]);
- }catch(\Exception $e){
- return $this->error('450001',$e->getMessage());
- }
- }
- //查看授权
- public function getAgentAuth(Request $request){
- $user_id=$request->input('user_id');
- $client=new Client();
- $url="http://api.app.jiuweiyun.cn/api/store_goods/auth_code";
- $array=[
- 'query' => [
- 'id' => $user_id,
- ]
- ];
- $data=$client->request('GET',$url,$array);
- $userInfo = json_decode($data->getBody()->getContents(),true);
- if($userInfo && !empty($userInfo['url'])){
- return $this->success($userInfo['url']);
- }else{
- return $this->error('450001','代理证书不存在');
- }
- }
- public function getCommentStart(){
- $store_ids=Comment::where('is_reply',1)->groupBy('store_id')->pluck('store_id');
- foreach($store_ids as $val){
- $star=Comment::where('store_id',$val)
- ->where('is_reply',1)->avg('star');
- Store::lockForUpdate()->where('id',$val)->update(['star'=>floor($star)]);
- }
- }
- }
|