find($id); if (empty($goods)) { return json(['code' => 1, 'message' => '不存在该商品或者商品已下架']); } $goods = $this->buildGoods($goods); return json($goods); } $sf = ['time' => 'update_time', 'price' => 'sell_price', 'count' => 'sell_count','default'=>'sort']; $sort = in_array($sort, array_keys($sf)) ? $sf[$sort] : 'sort'; $order = $order == 'asc' ? 'asc' : 'desc'; if (!empty($category)) { $g = $g->whereFindInSet('category',$category); } if (!empty($price)) { $price = explode('_',$price); $min = 0;$max = 100000; if(count($price) == 1) $max = $price[0]; else { $min = $price[0]; $max = $price[1]; } $g = $g->whereBetween('sell_price',[floatval($min),floatval($max)]); } $g = $g->order($sort, $order); $total = $g->count(); $gs = $this->buildGoodsData($g->limit(($page - 1) * $size, $size)->select()); if ($list != 'no') { return json([ 'total' => $total, 'page' => $page, 'size' => $size, 'list' => $gs->toArray() ]); } return json($gs); } public function login($username = null, $password = null) { if (empty($username) || empty($password)) { return json(['code' => 1, 'message' => '用户名和密码不允许为空']); } $u = (new Users())->where('username', $username)->where('password', md5($password))->find(); if (empty($u)) { return json(['code' => 2, 'message' => '用户名或者密码错误']); } if ($u->status != 1) { return json(['code' => 3, 'message' => '用户不存在或已经被冻结']); } $token = StringUtil::encryption($u->uid, StringUtil::USER_TOKEN, 0); $u->token = $token; Cache::set('login_user_'.$u->uid, $token, 3600); return json($u); } public function reg($username = null, $password = null, $email = null) { if (empty($username) || empty($password) || empty($email)) { return json(['code' => 1, 'message' => '用户名、密码及邮箱不允许为空']); } $u = (new Users())->where('username', $username)->findOrEmpty(); if (!$u->isEmpty()) { return json(['code' => 2, 'message' => '用户名已经存在了']); } $u->username = $username; $u->password = md5($password); $u->avatar = $this->request->param('avatar','https://img2.woyaogexing.com/2020/12/01/828516da933d44ccac01302dc72e97ae!400x400.jpeg'); $u->nickname = $this->request->param('nickname'); $u->email = $email; try { $u->save(); return $u; } catch (\Exception $e) { return json(['code' => 3, 'message' => '注册用户失败了', 'error' => $e->getMessage()]); } } public function saveGoods($title, $sell_price, $picture,$origin_key = null) { $g = new Goods(); if(!empty($origin_key)){ if(!empty($g->where('origin_key',$origin_key)->find())){ return $this->success(); } } // 过滤post数组中的非数据表字段数据 $data = $this->request->only([ 'title', 'origin_price', 'sell_count', 'sell_price', 'desc', 'content', 'picture', 'category','origin_key' ]); $g->save($data); return $this->success($g->id); } }