作者 SHW\戥岁。。

分类购物车数量

... ... @@ -36,17 +36,19 @@ class Cart extends Api
$number = $this->request->post('number');
if (!is_numeric($sku_id) || !is_numeric($goods_id) || !is_numeric($number)) $this->error('参数不合法');
$category_id = (new \app\api\model\Goods())->where('id', $goods_id)->value('category_id');
$data = [
'user_id' => $this->auth->id,
'category_id' => $category_id,
'sku_id' => $sku_id,
'goods_id' => $goods_id,
];
$model = new \app\api\model\Cart();
$cart = $model->where($data)->find();
if ($cart){
$cart->setInc('number',$number);
}else{
if ($cart) {
$cart->setInc('number', $number);
} else {
$data['number'] = $number;
$model->isUpdate(false)->save($data);
}
... ... @@ -68,12 +70,12 @@ class Cart extends Api
{
$ids = $this->request->post('cart_ids');
if (!$ids) $this->error('参数不可为空');
$ids = explode(',',$ids);
foreach ($ids as $key => $value){
$ids = explode(',', $ids);
foreach ($ids as $key => $value) {
if (!is_numeric($value)) $this->error('参数不合法');
}
$model = new \app\api\model\Cart();
$model->whereIn('id',$ids)->delete();
$model->whereIn('id', $ids)->delete();
$this->success('SUCCESS');
}
... ... @@ -96,9 +98,9 @@ class Cart extends Api
if (!is_numeric($id) || !is_numeric($number)) $this->error('参数不合法');
$model = new \app\api\model\Cart();
$order = $model->with('sku')->where('id',$id)->find();
if ($number+$order['number'] > $order['sku']['stock_num']) $this->error('已达到最大库存,请勿重复提交');
$model->where('id',$id)->setInc('number',$number);
$order = $model->with('sku')->where('id', $id)->find();
if ($number + $order['number'] > $order['sku']['stock_num']) $this->error('已达到最大库存,请勿重复提交');
$model->where('id', $id)->setInc('number', $number);
$this->success('SUCCESS');
}
... ... @@ -121,10 +123,10 @@ class Cart extends Api
if (!is_numeric($id) || !is_numeric($number)) $this->error('参数不合法');
$model = new \app\api\model\Cart();
if ($model->where('id',$id)->value('number') ==1){
if ($model->where('id', $id)->value('number') == 1) {
$this->error('购物车数量不可再减少');
}else{
$model->where('id',$id)->setDec('number',$number);
} else {
$model->where('id', $id)->setDec('number', $number);
}
$this->success('SUCCESS');
}
... ... @@ -169,22 +171,22 @@ class Cart extends Api
{
$model = new \app\api\model\Cart();
$list = $model
->with(['goods','sku'])
->where('user_id',$this->auth->id)
->with(['goods', 'sku'])
->where('user_id', $this->auth->id)
->order('createtime desc')
->select();
foreach ($list as $key => $value){
$value->getRelation('goods')->visible(['goods_name','goods_id']);
$value->getRelation('sku')->visible(['goods_spec_id','goods_price','spec_sku_id']);
foreach ($list as $key => $value) {
$value->getRelation('goods')->visible(['goods_name', 'goods_id']);
$value->getRelation('sku')->visible(['goods_spec_id', 'goods_price', 'spec_sku_id']);
}
$list = collection($list)->toArray();
foreach ($list as $k => &$val){
foreach ($list as $k => &$val) {
$skuids = explode('_', $val['sku']['spec_sku_id']);
$sku = Db::name('litestore_spec_value')->where('id','in',$skuids)->column('spec_value');
$skuname = !empty($sku)?implode(' ',$sku):'';
$sku = Db::name('litestore_spec_value')->where('id', 'in', $skuids)->column('spec_value');
$skuname = !empty($sku) ? implode(' ', $sku) : '';
$val['sku']['sku_name'] = $skuname;
}
$this->success('SUCCESS',$list);
$this->success('SUCCESS', $list);
}
/**
... ... @@ -208,23 +210,23 @@ class Cart extends Api
$json = $this->request->param('data_json');
if (!$json) $this->error('data_json参数不能为空');
// $json = '[{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}]';
$json = str_replace('"','"',$json);
$data = json_decode($json,true);
$json = str_replace('"', '"', $json);
$data = json_decode($json, true);
$goodsmodel = new \app\api\model\Goods();
$skumodel = new GoodsSpec();
$sum_price = 0; //总价格
foreach ($data as $key => $value){
if (!is_numeric($value['goods_id']) || !is_numeric($value['goods_sku_id']) || !is_numeric($value['number'])){
foreach ($data as $key => $value) {
if (!is_numeric($value['goods_id']) || !is_numeric($value['goods_sku_id']) || !is_numeric($value['number'])) {
$this->error('参数不合法');
}
$goods = $goodsmodel->where('goods_id',$value['goods_id'])->field('goods_id,goods_name,image,spec_type')->find();
$goods = $goodsmodel->where('goods_id', $value['goods_id'])->field('goods_id,goods_name,image,spec_type')->find();
if (!$goods) $this->error('商品不存在');
$sku = $skumodel->where('goods_spec_id',$value['goods_sku_id'])
$sku = $skumodel->where('goods_spec_id', $value['goods_sku_id'])
->field('goods_spec_id,spec_sku_id,goods_price')->find();
if (!$sku) $this->error('商品规格不存在');
$sum_price = bcadd($sum_price,bcmul($sku['goods_price'],$value['number'],2),2);
$sum_price = bcadd($sum_price, bcmul($sku['goods_price'], $value['number'], 2), 2);
}
$this->success('购物车页面价格',['price'=>$sum_price]);
$this->success('购物车页面价格', ['price' => $sum_price]);
}
}
\ No newline at end of file
... ...
... ... @@ -598,7 +598,6 @@ class Classification extends Api
{
$model = new Category();
$list = $model->field('id,name')->order('weigh desc')->select();
$cart_sum = 0;
foreach ($list as $key => &$value) {
$model = new Goods();
if ($this->auth->isLogin()) {
... ... @@ -608,14 +607,18 @@ class Classification extends Api
->where('goods_status', '10')
->field('goods_id')
->select()->toArray();
//如果该分类下没商品,则分类下购物车数量为零
if (empty($goods_list)) {
$value['category_cart_number'] = 0;
continue;
}
foreach ($goods_list as &$item) {
$sum = Db::name('cart')
$value['category_cart_number'] = Db::name('cart')
->where('user_id', $this->auth->id)
->where('goods_id', $item['goods_id'])
// ->where('goods_id', $item['goods_id'])
->where('category_id', $value['id'])
->sum('number');
$cart_sum += $sum;
}
$value['category_cart_number'] = $cart_sum;
} else {
$value['category_cart_number'] = 0;
}
... ...
... ... @@ -18,7 +18,7 @@ use think\Validate;
*/
class User extends Api
{
protected $noNeedLogin = ['third','joinUs','developLogin','notice'];
protected $noNeedLogin = ['third', 'joinUs', 'developLogin', 'notice'];
protected $noNeedRight = '*';
public function _initialize()
... ... @@ -48,12 +48,12 @@ class User extends Api
{
$data = [];
$data['nickname'] = $this->auth->nickname;
$data['avatar'] = cdnurl($this->auth->avatar,true);
$data['avatar'] = cdnurl($this->auth->avatar, true);
$data['mobile'] = $this->auth->mobile;
$data['pay'] = Db::name('litestore_order')->where('user_id',$this->auth->id)->where('pay_status','10')->count();
$data['wait_send'] = Db::name('litestore_order')->where('user_id',$this->auth->id)->where('freight_status','10')->count();
$data['wait_collect'] = Db::name('litestore_order')->where('user_id',$this->auth->id)->where('receipt_status','10')->count();
$data['wait_comment'] = Db::name('litestore_order')->where('user_id',$this->auth->id)->where('receipt_status','20')->count();
$data['pay'] = Db::name('litestore_order')->where('user_id', $this->auth->id)->where('pay_status', '10')->count();
$data['wait_send'] = Db::name('litestore_order')->where('user_id', $this->auth->id)->where('freight_status', '10')->count();
$data['wait_collect'] = Db::name('litestore_order')->where('user_id', $this->auth->id)->where('receipt_status', '10')->count();
$data['wait_comment'] = Db::name('litestore_order')->where('user_id', $this->auth->id)->where('receipt_status', '20')->count();
$this->success('会员中心', ['welcome' => $data]);
}
... ... @@ -100,7 +100,7 @@ class User extends Api
$nickname = $this->request->post('nickname');
$avatar = $this->request->post('avatar');
$invite_id = $this->request->post('invite_id');
if (!$code || !$nickname || !$avatar){
if (!$code || !$nickname || !$avatar) {
$this->error('后台所需参数缺失请完善参数');
}
$param = [];
... ... @@ -108,40 +108,40 @@ class User extends Api
$param['grant_type'] = 'authorization_code';
$param['secret'] = Config::get('site.secret');
$param['appid'] = Config::get('site.appid');
$wxapi = Http::get('https://api.weixin.qq.com/sns/jscode2session',$param);//请求openid
$wxapi = json_decode($wxapi,true);
if (isset($wxapi['errcode'])){
$wxapi = Http::get('https://api.weixin.qq.com/sns/jscode2session', $param);//请求openid
$wxapi = json_decode($wxapi, true);
if (isset($wxapi['errcode'])) {
$this->error($wxapi['errmsg']);
}
$third = new \app\api\model\Third();
$userid = $third->where('openid',$wxapi['openid'])->value('user_id');
if ($userid){
$this->auth->direct($userid,$invite_id);
$third->isUpdate()->save(['session_key'=>$wxapi['session_key']],['user_id'=>$userid]);
$this->success('登录成功',['token'=>$this->auth->getToken(),'user_type'=>$this->auth->user_type]);
}else{
if ($invite_id > 0){
$userid = $this->auth->register($nickname,'','','',['avatar'=>$avatar,'invite_user_id'=>$invite_id,'invite_time'=>time()]);
}else{
$userid = $this->auth->register($nickname,'','','',['avatar'=>$avatar]);
}
if ($userid){
$third->isUpdate(false)->save(['openid'=>$wxapi['openid'],'user_id'=>$userid,'session_key'=>$wxapi['session_key']]);
$userid = $third->where('openid', $wxapi['openid'])->value('user_id');
if ($userid) {
$this->auth->direct($userid, $invite_id);
$third->isUpdate()->save(['session_key' => $wxapi['session_key']], ['user_id' => $userid]);
$this->success('登录成功', ['token' => $this->auth->getToken(), 'user_type' => $this->auth->user_type]);
} else {
if ($invite_id > 0) {
$userid = $this->auth->register($nickname, '', '', '', ['avatar' => $avatar, 'invite_user_id' => $invite_id, 'invite_time' => time()]);
} else {
$userid = $this->auth->register($nickname, '', '', '', ['avatar' => $avatar]);
}
if ($userid) {
$third->isUpdate(false)->save(['openid' => $wxapi['openid'], 'user_id' => $userid, 'session_key' => $wxapi['session_key']]);
// 注册送优惠券
$coupon = Db::name('coupon')->where('id',3)->find();
$coupon = Db::name('coupon')->where('id', 3)->find();
$time = time();
if($coupon['register_send_switch'] == '1' && $time >= $coupon['register_send_starttime'] && $time <= $coupon['register_send_endtime']){
if ($coupon['register_send_switch'] == '1' && $time >= $coupon['register_send_starttime'] && $time <= $coupon['register_send_endtime']) {
UserCoupon::create([
'user_id' => $userid,
'coupon_id' => $coupon['id'],
'name' => $coupon['name'],
'price' => $coupon['price'],
'full_price' => $coupon['full_price'],
'endtime' => $time+$coupon['days']*86400
'endtime' => $time + $coupon['days'] * 86400,
]);
}
$this->success('登录成功',['token'=>$this->auth->getToken(),'user_type'=>$this->auth->user_type]);
}else{
$this->success('登录成功', ['token' => $this->auth->getToken(), 'user_type' => $this->auth->user_type]);
} else {
$this->error('注册失败');
}
}
... ... @@ -177,7 +177,7 @@ class User extends Api
}
// 获取session_key
$user = $this->auth->getUser();
$third = \app\api\model\Third::where('user_id',$user['id'])->field('session_key')->find();
$third = \app\api\model\Third::where('user_id', $user['id'])->field('session_key')->find();
empty($third) && $this->error('请先登录');
// 获取小程序配置
$app = Factory::miniProgram([
... ... @@ -188,7 +188,7 @@ class User extends Api
// 更新手机号
$user->mobile = $res['purePhoneNumber'];
$user->save();
$this->success('授权成功',['mobile' => $res['purePhoneNumber']]);
$this->success('授权成功', ['mobile' => $res['purePhoneNumber']]);
}
/**
... ... @@ -220,12 +220,12 @@ class User extends Api
$model = new UserCoupon();
// 更新已过期状态
$model->where('status','0')->where('user_id',$this->auth->id)->where('endtime','<',time())->update(['status'=>'2']);
$model->where('status', '0')->where('user_id', $this->auth->id)->where('endtime', '<', time())->update(['status' => '2']);
$type = $this->request->post('type');
if (!in_array($type,[0,1,2])) $this->error('type参数不合法');
$list = $model->where('status',$type)->where('user_id',$this->auth->id)->select();
$this->success('用户优惠券列表',$list);
if (!in_array($type, [0, 1, 2])) $this->error('type参数不合法');
$list = $model->where('status', $type)->where('user_id', $this->auth->id)->select();
$this->success('用户优惠券列表', $list);
}
/**
... ... @@ -255,8 +255,8 @@ class User extends Api
public function userAddressList()
{
$model = new UserAddress();
$list = $model->where('user_id',$this->auth->id)->order('normal_status','desc')->select();
$this->success('用户地址列表',$list);
$list = $model->where('user_id', $this->auth->id)->order('normal_status', 'desc')->select();
$this->success('用户地址列表', $list);
}
... ... @@ -319,7 +319,7 @@ class User extends Api
$validate->message($msg);
if (!$validate->check($data)) $this->error($validate->getError());
$model = new UserAddress();
if ($type == 1){
if ($type == 1) {
$data = [
'mobile' => $mobile,
'normal_status' => $normal,
... ... @@ -329,10 +329,10 @@ class User extends Api
'address' => $address,
'address_detail' => $address_detail,
];
if ($normal == 1) $model->isUpdate()->save(['normal_status'=>'0'],['user_id'=>$this->auth->id]);
$model->isUpdate()->save($data,['id'=>$id]);
}else{
$model->where('id',$id)->delete();
if ($normal == 1) $model->isUpdate()->save(['normal_status' => '0'], ['user_id' => $this->auth->id]);
$model->isUpdate()->save($data, ['id' => $id]);
} else {
$model->where('id', $id)->delete();
}
$this->success('SUCCESS');
}
... ... @@ -402,7 +402,7 @@ class User extends Api
'address' => $address,
'address_detail' => $address_detail,
];
if ($normal == 1) $model->isUpdate()->save(['normal_status'=>'0'],['user_id'=>$this->auth->id]);
if ($normal == 1) $model->isUpdate()->save(['normal_status' => '0'], ['user_id' => $this->auth->id]);
$model->isUpdate(false)->save($data);
$this->success('SUCCESS');
... ... @@ -472,8 +472,8 @@ class User extends Api
if (!$validate->check($data)) $this->error($validate->getError());
// 禁止连点
!empty(cache('joinus_'.$username)) && $this->error('操作频繁,请稍后再试');
cache('joinus_'.$username,'123',5);
!empty(cache('joinus_' . $username)) && $this->error('操作频繁,请稍后再试');
cache('joinus_' . $username, '123', 5);
$data['createtime'] = time();
Db::name('join_us')->insert($data);
... ... @@ -512,9 +512,9 @@ class User extends Api
$page = $this->request->post('page');
if (!is_numeric($page)) $this->error('页数参数不合法');
$list = Db::name('helps')
->order('id','desc')
->paginate(15,false,['page'=>$page]);
$this->success('SUCCESS',['list'=>$list,'rider_status'=>$this->auth->rider,'mobile'=>Config::get('site.work_mobile')]);
->order('id', 'desc')
->paginate(15, false, ['page' => $page]);
$this->success('SUCCESS', ['list' => $list, 'rider_status' => $this->auth->rider, 'mobile' => Config::get('site.work_mobile')]);
}
/**
... ... @@ -537,13 +537,13 @@ class User extends Api
$mobile = $this->request->post('mobile');
if (!$cotent) $this->error('请填写意见描述');
if (mb_strlen($cotent) > 100) $this->error('请将意见描述控制在100字以内');
if (!$mobile || !preg_match('/^1\d{10}$/',$mobile)) $this->error('请正确填写联系方式');
if (!$mobile || !preg_match('/^1\d{10}$/', $mobile)) $this->error('请正确填写联系方式');
$data = [
'user_id' => $this->auth->id,
'cotent' => $cotent,
'images' => $images,
'mobile' => $mobile,
'createtime' => time()
'createtime' => time(),
];
Db::name('opinion')->insert($data);
$this->success('SUCCESS');
... ... @@ -561,7 +561,7 @@ class User extends Api
*/
public function riderApply()
{
if (in_array($this->auth->rider,[1,2])){
if (in_array($this->auth->rider, [1, 2])) {
$this->error('您已申请,不可重复申请');
}
$user = $this->auth->getUser();
... ... @@ -597,19 +597,19 @@ class User extends Api
public function inviteReward()
{
$model = new \app\api\model\User();
$coupon = Db::name('coupon')->where('id',1)->field('price,full_price,endtime')->find();
$coupon['endtime'] = date('Y年m月d日',$coupon['endtime']);
$coupon = Db::name('coupon')->where('id', 1)->field('price,full_price,endtime')->find();
$coupon['endtime'] = date('Y年m月d日', $coupon['endtime']);
$coupon['rule'] = Config::get('site.invite_rule');
$coupon['user_list'] = $model
->where('invite_user_id',$this->auth->id)
->where('invite_status','1')
->where('invite_user_id', $this->auth->id)
->where('invite_status', '1')
->limit(3)
->select();
foreach ($coupon['user_list'] as $key => $value){
foreach ($coupon['user_list'] as $key => $value) {
$value->visible(['nickname']);
}
$coupon['invite_id'] = $this->auth->id;
$this->success('邀请有奖',$coupon);
$this->success('邀请有奖', $coupon);
}
/**
... ... @@ -640,15 +640,15 @@ class User extends Api
*/
public function taxList()
{
$page = $this->request->post('page',1);
$page = $this->request->post('page', 1);
$model = new Tax();
$list = $model
->where('user_id',$this->auth->id)
->where('del_status','normal')
->where('user_id', $this->auth->id)
->where('del_status', 'normal')
->field('id,company_name,username,type,price,tax_time')
->order('id','desc')
->paginate(5,false,['page'=>$page]);
$this->success('发票管理列表',$list);
->order('id', 'desc')
->paginate(5, false, ['page' => $page]);
$this->success('发票管理列表', $list);
}
... ... @@ -686,7 +686,7 @@ class User extends Api
$eamil = $this->request->post('eamil');
$order_no = $this->request->post('order_no');
//校验数据
if (!in_array($type,[1,2])) $this->error('type参数不合法');
if (!in_array($type, [1, 2])) $this->error('type参数不合法');
$data = [
'mobile' => $mobile,
'eamil' => $eamil,
... ... @@ -702,9 +702,9 @@ class User extends Api
'eamil' => '电子邮箱格式不正确',
'order_no' => '请输入订单编号',
];
$validate = new \think\Validate($rule,$msg);
$validate = new \think\Validate($rule, $msg);
if (!$validate->check($data)) $this->error($validate->getError());
if ($type == 2){
if ($type == 2) {
$data = [
'company_name' => $company_name,
'company_mobile' => $company_mobile,
... ... @@ -726,9 +726,9 @@ class User extends Api
'company_address' => '请填写公司地址',
'bank' => '请填写开户银行',
];
$validate = new \think\Validate($rule,$msg);
$validate = new \think\Validate($rule, $msg);
if (!$validate->check($data)) $this->error($validate->getError());
}else{
} else {
$data = [
'username' => $username,
];
... ... @@ -738,12 +738,12 @@ class User extends Api
$msg = [
'username' => '请填写个人/非企业单位名称',
];
$validate = new \think\Validate($rule,$msg);
$validate = new \think\Validate($rule, $msg);
if (!$validate->check($data)) $this->error($validate->getError());
}
//校验订单编号
$orderModel = new \app\api\model\Order();
$order = $orderModel::get(['order_no'=>$order_no]);
$order = $orderModel::get(['order_no' => $order_no]);
if (!$order) $this->error('订单编号不存在');
if ($order['user_id'] != $this->auth->id) $this->error('当前用户无权使用此订单编号');
// 插入数据
... ... @@ -783,12 +783,12 @@ class User extends Api
{
$ids = $this->request->post('ids');
//校验数据
$ids = explode(',',$ids);
foreach ($ids as $key => $value){
$ids = explode(',', $ids);
foreach ($ids as $key => $value) {
if (!is_numeric($value)) $this->error('ids参数不合法');
}
$model = new Tax();
$model->isUpdate()->save(['del_status' => 'hidden'],['id' => ['in',$ids]]);
$model->isUpdate()->save(['del_status' => 'hidden'], ['id' => ['in', $ids]]);
$this->success('SUCCESS');
}
... ... @@ -799,7 +799,7 @@ class User extends Api
*/
public function developLogin()
{
$account = $this->request->post('account');
$account = $this->request->param('account');
if (!$account) {
$this->error(__('Invalid parameters'));
}
... ... @@ -821,14 +821,15 @@ class User extends Api
* 测试
* @ApiInternal
*/
public function notice(){
public function notice()
{
$client = new Client();
$response = $client->request('POST', request()->domain().":2121/", [
$response = $client->request('POST', request()->domain() . ":2121/", [
'form_params' => [
'type' => 'publish',
'content' => '这个是推送的测试数据',
'to' => '',
]
],
]);
halt($response->getBody()->getContents());
}
... ...