StoreController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\DwbsUser;
  4. use App\Models\OrderDetail;
  5. use App\Models\OrderRefund;
  6. use App\Models\ShopCar;
  7. use Illuminate\Http\Request;
  8. use App\Models\User;
  9. use App\Models\Goods;
  10. use App\Models\Order;
  11. use App\Models\Store;
  12. use App\Models\Scan;
  13. use App\Models\Comment;
  14. use GuzzleHttp\Client;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Log;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Support\Facades\Validator;
  19. class StoreController extends Controller
  20. {
  21. public function exportUser(){
  22. $ids=DwbsUser::where('level',3)->whereNull('deleted_at')->pluck('id');
  23. $data=Store::whereIn('user_id',$ids)->select('phone','username','idCard','name')->get();
  24. return $this->success($data);
  25. }
  26. //获取店铺首页信息
  27. // public function getUserStoreDiff(Request $request)
  28. // {
  29. // $user_id = Auth::user()->id;
  30. // $user = User::where('id', $user_id)->first();
  31. // $store_id = $request->input('store_id');
  32. // $data['type'] = 'noReplace';
  33. // if (empty($user->store_id)) {//未绑定
  34. // if ($user->store_id == $store_id) {
  35. // return $this->error('450011', '您未关联店铺信息,请联系代理扫码登陆');
  36. // } else {
  37. // $store = Store::where('id', $request->input('store_id'))
  38. // ->select('id','name','img','phone','status','user_id')->first();
  39. // if (empty($store)) {
  40. // return $this->error('450001', '扫码店铺信息不存在');
  41. // } else {
  42. // if ($store->status == 1)
  43. // return $this->error('450001', '店铺已被禁用');
  44. // User::where('id', $user->id)->update(['store_id' => $request->input('store_id')]);
  45. // Scan::create([
  46. // 'user_id' => $user->id,
  47. // 'store_id' => $store->id,
  48. // 'is_store' => 2,//店铺
  49. // ]);
  50. // }
  51. // }
  52. // } else {//已绑定
  53. // if (!empty($store_id) && $user->store_id != $store_id) {
  54. // $data['type'] = 'replace';
  55. // $data['store_id'] = $store_id;
  56. // $data['is_vip'] = $user->is_vip;
  57. // $data['before_store']=Store::where('id',$user->store_id)->value('name');
  58. // $data['after_store']=Store::where('id',$store_id)->value('name');
  59. // }
  60. // $store = Store::where('id', $user->store_id)->first();
  61. // if (empty($store))
  62. // return $this->error('450001', '用户绑定店铺信息不存在');
  63. // if ($store->status == 1)
  64. // return $this->error('450001', $store->name . '已被禁用');
  65. // }
  66. // return $this->success($data);//需要替换店铺
  67. // }
  68. //获取店铺信息
  69. public function getStoreIndex(Request $request){
  70. $store_id=$request->input('store_id');
  71. $goods_id=$request->input('goods_id');
  72. $store=Store::where('id',$store_id)->first();
  73. if(empty($store)){
  74. return $this->error('450011', '店铺信息不存在,请联系代理扫码进入店铺');
  75. }
  76. // $star=Comment::where('store_id',$store_id)->where('is_reply',1)->avg('star');
  77. // $store->star=floor($star);
  78. $arr_test=Store::getTest();
  79. if(in_array($store->id,$arr_test)){
  80. $store->is_test=1;
  81. }else{
  82. $store->is_test=0;
  83. }
  84. if(!empty($goods_id)){
  85. $goods_ids=explode(',',$goods_id);
  86. $goods=Goods::where('is_delete',0)->where('is_shelves',1)->whereIn('id',$goods_ids)->get();
  87. }else{
  88. $goods=Goods::where('is_delete',0)->where('is_shelves',1)->get();
  89. }
  90. $user_id=Auth::user()->id;
  91. $user=User::where('id',$user_id)->first();
  92. if($store_id && $goods_id) {
  93. $scan = Scan::where('user_id', $user_id)->where('store_id', $store_id)->first();
  94. if (!$scan) {
  95. Scan::create([
  96. 'user_id' => $user_id,
  97. 'store_id' => $store_id,
  98. 'goods_id' =>$goods_id,
  99. 'order_no' => null,
  100. 'is_store' => 1
  101. ]);
  102. }
  103. }
  104. if($store_id && !$goods_id){
  105. $scan = Scan::where('user_id', $user_id)->where('store_id', $store_id)->first();
  106. if (!$scan) {
  107. Scan::create([
  108. 'user_id'=>$user_id,
  109. 'store_id'=>$store_id,
  110. 'goods_id'=>null,
  111. 'order_no'=>null,
  112. 'is_store'=>2
  113. ]);
  114. }
  115. }
  116. $dwbsUser = DwbsUser::where('mobile',$user->phone)
  117. ->where(function($query){
  118. $query->where('cert_status',3)
  119. ->orWhere('cert_status',6);
  120. })
  121. ->where('service_status',0)
  122. ->whereNull('deleted_at')->first();
  123. if($dwbsUser){
  124. $data['is_agent'] = 1;
  125. }else{
  126. $data['is_agent'] = 0;
  127. }
  128. $data['store']=$store;
  129. $data['goods']=$goods;
  130. return $this->success($data);
  131. }
  132. //获取购物车商品数量
  133. public function getShopCarNum(){
  134. $user_id=Auth::user()->id;
  135. $car_num=ShopCar::where('user_id',$user_id)->sum('num');
  136. return $this->success($car_num);
  137. }
  138. //替换店铺信息
  139. public function replaceStore(Request $request){
  140. $user_id=Auth::user()->id;
  141. $store_id=$request->input('store_id');
  142. $store=Store::where('id',$store_id)->first();
  143. if(empty($store)){
  144. return $this->error('450001','扫码店铺信息有误');
  145. }
  146. $res=User::where('id',$user_id)->update(['store_id'=>$store_id,'is_vip'=>0]);
  147. if($res){
  148. return $this->success([]);
  149. }else{
  150. return $this->error('450001','更换店铺失败');
  151. }
  152. }
  153. //发布店铺评论
  154. public function addStoreComment(Request $request)
  155. {
  156. $rules=[
  157. 'content'=>'max:255',
  158. ];
  159. $message=[
  160. 'content.max'=>'评价内容不能大于255个字符.'
  161. ];
  162. $validator = Validator::make($request->input(),$rules,$message);
  163. if($validator->fails()){
  164. return $this->error('400013',$validator->errors()->first());
  165. }
  166. try{
  167. DB::transaction(function()use($request){
  168. $user=Auth::user();
  169. $star=$request->input('star') ?? 0;
  170. $user_id=$user->id;
  171. $content=$request->input('content');
  172. $order_no=$request->input('order_no');
  173. $order=Order::where('order_no',$order_no)->first();
  174. if($order){
  175. $order->is_comment=1;
  176. $order->save();
  177. $order_id=$order->id;
  178. }else{
  179. $order_id=null;
  180. }
  181. Comment::create([
  182. 'user_id'=>$user_id,
  183. 'store_id'=>$order->store_id,
  184. 'content'=>$content,
  185. 'order_id'=>$order_id,
  186. 'comment_id'=>0,
  187. 'star'=>$star,
  188. 'is_reply'=>1,
  189. 'is_see'=>0
  190. ]);
  191. $star=Comment::where('store_id',$order->store_id)
  192. ->where('is_reply',1)->avg('star');
  193. Store::lockForUpdate()->where('id',$order->store_id)->update(['star'=>floor($star)]);
  194. },5);
  195. return $this->success([]);
  196. }catch(\Exception $e){
  197. return $this->error('450001',$e->getMessage());
  198. }
  199. }
  200. //查看授权
  201. public function getAgentAuth(Request $request){
  202. $user_id=$request->input('user_id');
  203. $client=new Client();
  204. $url="http://api.app.jiuweiyun.cn/api/store_goods/auth_code";
  205. $array=[
  206. 'query' => [
  207. 'id' => $user_id,
  208. ]
  209. ];
  210. $data=$client->request('GET',$url,$array);
  211. $userInfo = json_decode($data->getBody()->getContents(),true);
  212. if($userInfo && !empty($userInfo['url'])){
  213. return $this->success($userInfo['url']);
  214. }else{
  215. return $this->error('450001','代理证书不存在');
  216. }
  217. }
  218. public function getCommentStart(){
  219. $store_ids=Comment::where('is_reply',1)->groupBy('store_id')->pluck('store_id');
  220. foreach($store_ids as $val){
  221. $star=Comment::where('store_id',$val)
  222. ->where('is_reply',1)->avg('star');
  223. Store::lockForUpdate()->where('id',$val)->update(['star'=>floor($star)]);
  224. }
  225. }
  226. }