auth_s = new Auth(); $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token'))); $this->auth_s->init($token); $this->price_status= Db::name('kuerp_user_config')->where(['name'=>'price_status'])->value('value');//是否显示价格 } /** * 支付配置 */ public function getConfig(){ $config_data = Db::name('kuerp_user_sets')->column('value','name'); $config = [ 'app_id' => (isset($config_data['appid'])?$config_data['appid']:''), 'mch_id' => isset($config_data['mch_id'])?$config_data['mch_id']:'', 'key' => isset($config_data['pay_key'])?$config_data['pay_key']:'', 'notify_url' => url('notify', [], true, true) ]; return $config; } /** * 支付 */ public function wxpay(){ $user = $this->auth_s->getUserinfo(); if(!isset($user['id'])){ $this->error('请先登陆','',250); } $pay_status = Db::name('kuerp_user_config')->where(['name'=>'pay_status'])->value('value'); if($pay_status!=1){ $this->error('系统未开启此功能','',250); } $data = input(); $order_num = isset($data['order_num'])?$data['order_num']:''; if(empty($order_num)){ $this->error('请选择要支付的订单','',400); } $where = []; $where['order_num'] = $order_num; $where['user_id'] = $user['id']; $where['pay_status'] = '0'; $order = Db::name('kuerp_user_order')->where($where)->find(); if(empty($order)){ $this->error('未找到相关订单','',400); } $openid = Db::name('kuerp_user_oauth')->where(['user_id'=>$user['id']])->value('openid'); if(empty($openid)){ $this->error('未找到用户凭证','',400); } $config = $this->getConfig(); $app = Factory::payment($config); $detail = '订货通付款'; // 支付详情 $pay_info = [ 'trade_type' => 'JSAPI', // 支付方式,小程序支付使用JSAPI 'body' => '订货通付款', // 订单说明 'out_trade_no' => trim($order['order_num']), // 自定义订单号 'total_fee' => $order['order_price'] * 100, // 单位:分 'openid' => $openid, // 当前用户的openId 'notify_url' => $config['notify_url'], 'detail' => $detail, ]; $result = $app->order->unify($pay_info); if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') { // $prepayId = $result['prepay_id']; //$jssdk = $app->jssdk; // $config = $jssdk->sdkConfig($prepayId); //不能使用返回的配置参数,否则会报签名错误 //必须使用二次签名 $appId = $result['appid']; $nonceStr = $result['nonce_str']; $prepay_id = $result['prepay_id']; $timeStamp = time(); //$key = $payment['app_key']; $key = $config['key']; $paySign = md5("appId=$appId&nonceStr=$nonceStr&package=prepay_id=$prepay_id&signType=MD5&timeStamp=$timeStamp&key=$key"); // 这个地方就是我所说的二次签名! // 返回给小程序使用 $config=[]; $config['nonceStr']=$nonceStr; $config['timeStamp']=strval($timeStamp); //小程序支付的timeStamp参数,必须使用这个timeStamp,因为已经计算到了paySign中 $config['package']="prepay_id=".$prepay_id; $config['paySign']=$paySign; $config['signType']='MD5'; $this->success('成功', ['config'=>$config,'url'=>$pay_info['notify_url']],200); } if ($result['return_code'] == 'FAIL' && array_key_exists('return_msg', $result)) { $this->error('错误:'.$result['return_msg'],'',400); } $this->error('错误:'.$result['err_code_des'],'',400); } /** * 支付回调 */ public function notify() { $config = $this->getConfig(); $app = Factory::payment($config); $response = $app->handlePaidNotify(function($message, $fail){ // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单 $order_num = $message['out_trade_no']; $where = []; $where['order_num'] = $order_num; $where['pay_status'] = '0'; $order = Db::name('kuerp_user_order')->where($where)->find(); if (empty($order)) { // 如果订单不存在 或者 订单已经支付过了 return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了 } if ($message['return_code'] === 'SUCCESS') { //return_code 表示通信状态,不代表支付状态 // 用户是否支付成功 if ($message['result_code'] === 'SUCCESS') { Db::name('kuerp_user_order')->where(['id'=>$order['id']])->update(['pay_status'=>'1','pay_time'=>time()]); } elseif ($message['result_code'] === 'FAIL') { // 用户支付失败 Db::name('kuerp_user_order')->where(['id'=>$order['id']])->update(['pay_status'=>'2']); } } else { return '通信失败,请稍后再通知我'; } return true; // 返回处理完成 }); $response->send(); // return $response; } public function Message($type,$order_num){ $where = []; $where['order_num'] = $order_num; $order = Db::name('kuerp_user_order')->where($where)->find(); if(empty($order)){ return ['status'=>0,'msg'=>'未找到相关订单']; } $openid = Db::name('kuerp_user_oauth')->where(['user_id'=>$order['user_id']])->value('openid'); if(empty($openid)){ return ['status'=>0,'msg'=>'未找到用户凭证']; } $goods_list = Db::name('kuerp_user_orderinfo')->where(['order_id'=>$order['id']]) ->select(); $goods_info = ''; foreach ($goods_list as $goods_list_k=>$goods_list_v){ if($goods_list_k<5){ $goods_info.=$goods_list_v['goods_name'].'*'.$goods_list_v['number'].' '; } } if(sizeof($goods_list)>=5){ $goods_info.= '...'; } $config = Db::name('kuerp_user_sets')->column('value','name'); $order_status = ''; switch ($order['status']){ case 0: $order_status = '待审核'; break; case 1: $order_status = '已审核'; break; case 2: $order_status = '待发货'; break; case 3: $order_status = '待收货'; break; case 4: $order_status = '已收货'; break; } //发送内容 //$type = ['fh','sh','xd']; $page = '/pages/customer/customer?find_type=1'; $template_id = $config['template_'.$type]; $template['fh'] = [ "character_string1"=>[ 'value' => $order['order_num']//订单编号 ], "name5"=>[ 'value' => $order['name']//收货人 ], 'thing7'=>[ 'value'=>'您的订单已发货,点击查看物流详情。' ], "thing13"=>[ 'value' => $order['address']//收货地址 ], 'thing17'=>[ 'value'=>$order['diver_name'].' '.$order['diver_phone']//送货人 ], ]; $template['sh'] = [ "character_string1"=>[ 'value' => $order['order_num']//订单编号 ], "phrase2"=>[ 'value' => $order_status//订单状态 ], 'thing4'=>[ 'value'=>'您的订货单已审核通过,点击查看订单详情' ], "time3"=>[ 'value' => date('Y-m-d H:i:s',$order['updatetime']) ], ]; $template['xd'] = [ "phrase8"=>[ 'value' => $order_status//订单状态 ], "character_string6"=>[ 'value' => $order['order_num']//订单编号 ], 'date5'=>[ 'value'=>date('Y-m-d H:i:s',$order['createtime'])//下单时间 ], "thing4"=>[ 'value' => $order['name'].' '.$order['address']// '收货信息' ], 'thing1'=>[ 'value'=>$goods_info ], ]; $data = [ 'touser'=>$openid,//接收者(用户)的 openid 'template_id'=>$template_id,//所需下发的订阅模板id 'page'=>$page,//点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。 'miniprogram_state'=>'formal',//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版 'lang'=>'zh_CN', 'data'=>$template[$type] //模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } } ]; $res = $this->sendMessage($data); return ['status'=>1,'msg'=>'发送成功']; } function sendMessage($data = []) { $wechat = new Wechat('wxMiniProgram'); $access_token = $wechat->getAccessToken(); $access_token = isset($access_token['access_token'])?$access_token['access_token']:''; if(!empty($access_token)){ //请求url $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $access_token ; return self::curlPost($url,json_encode($data)) ; } return false; } function sendMessages($data = []) { $access_token = Cache::get('access_token'); if(empty($access_token)){ $config = Db::name('kuerp_user_sets')->column('value','name'); $res=[ 'appid'=>$config['appid'], 'appsecret'=>$config['appsecret'] ]; $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $res['appid'] . '&secret=' . $res['appsecret']; //json_encode 对变量进行 JSON 编码 file_get_contents() 把整个文件读入一个字符串中。 $res = json_decode(file_get_contents($url),true); $access_token = $res['access_token'] ; Cache::set('access_token',$access_token,7000); } //请求url $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $access_token ; return self::curlPost($url,json_encode($data)) ; } //发送post请求 小程序发送一次性订阅消息 static function curlPost($url,$data) { $ch = curl_init(); $params[CURLOPT_URL] = $url; //请求url地址 $params[CURLOPT_HEADER] = FALSE; //是否返回响应头信息 $params[CURLOPT_SSL_VERIFYPEER] = false; $params[CURLOPT_SSL_VERIFYHOST] = false; $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回 $params[CURLOPT_POST] = true; $params[CURLOPT_POSTFIELDS] = $data; curl_setopt_array($ch, $params); //传入curl参数 $content = curl_exec($ch); //执行 curl_close($ch); //关闭连接 return $content; } public function getArea($id){ $area = Db::name('kuerp_area')->where(['id'=>$id])->find(); if($area){ return ['status'=>200,'msg'=>$area]; }else{ return ['status'=>500,'msg'=>'查询失败']; } } public function address_list(){ $page = $this->request->post('page'); $page = empty($page)?1:$page; $limit = $this->request->post('limit'); $limit = empty($limit)?10:$limit; $app_page = config('app_page'); $pageStatus = true; if($app_page){ if($page<0){ $pageStatus = false; } } if($pageStatus){ $list = Db::name('kuerp_address')->page($page,$limit) ->field("id,nickname,connect_name,connect_phone,connect_mobile,province,city,area,detail_address,post_code,is_default,FROM_UNIXTIME(createtime,'%Y-%m-%d %H:%i:%s') as createtime")->select(); }else{ $list = Db::name('kuerp_address') ->field("id,nickname,connect_name,connect_phone,connect_mobile,province,city,area,detail_address,post_code,is_default,FROM_UNIXTIME(createtime,'%Y-%m-%d %H:%i:%s') as createtime")->select(); } foreach ($list as $list_k=>$list_v){ $area = $this->getArea($list_v['province']); if($area['status']==200){ $list[$list_k]['province_name'] = $area['msg']['name']; }else{ $list[$list_k]['province_name'] = ''; } $area = $this->getArea($list_v['city']); if($area['status']==200){ $list[$list_k]['city_name'] = $area['msg']['name']; }else{ $list[$list_k]['city_name'] = ''; } $area = $this->getArea($list_v['area']); if($area['status']==200){ $list[$list_k]['area_name'] = $area['msg']['name']; }else{ $list[$list_k]['area_name'] = ''; } } $count = Db::name('kuerp_address')->count(); $list = ['list'=>$list,'count'=>$count]; $this->success('查询成功',$list,200); } /** * 轮播 */ public function banner() { $detail = input(); $check = (new \app\api\model\kuerp\UserAddress)->checkParameter($detail,[ 'type_id|广告位'=>'require|number', ]); if(isset($check['status'])){ $this->error($check['msg'],'',500); }else{ $type_id = $detail['type_id']; $list = (new Banner())->where(['type_id'=>$type_id,'status'=>'1'])->field('id,name,image,link,status,FROM_UNIXTIME(createtime,\'%Y-%m-%d\') as createtime')->select(); if(!empty($list)){ foreach ($list as $list_k=>$list_v){ $list[$list_k]['image'] = $list_v['image'] ? cdnurl($list_v['image'], true) : ''; } $this->success('查询成功',$list,200); }else{ $this->error('查询失败','',406); } } } /** * 获取商品分类 * @ApiMethod (POST) * @param int $pid 父级ID */ public function goodsTypeList() { $data = input(); $pid = isset($data['pid'])?$data['pid']:0; $where = []; if($pid){ $where['pid'] = $pid; }else{ $where['pid'] = 0; } $where['status'] = '1';$where['order_status'] = '1'; $list = Db::name('kuerp_goods_type')->where($where) ->field('id,pid,supplier_type_name as name,supplier_type_image as image,status,FROM_UNIXTIME(createtime,\'%Y-%m-%d\') as createtime,FROM_UNIXTIME(updatetime,\'%Y-%m-%d\') as updatetime') ->select(); $user = $this->auth_s->getUserinfo(); $user['id'] = isset($user['id'])?$user['id']:0; foreach ($list as $list_k=>$list_v){ $where = []; $goods_type_ids = Db::name('kuerp_goods_type')->where(['path'=>['like','%,'.$list_v['id'].',%']])->column('id'); $goods_type_ids[] = $list_v['id']; $where['goods_type_id'] = ['in',$goods_type_ids]; $goods_ids = Db::name('kuerp_goods')->where($where)->column('id'); $goods_id = Db::name('kuerp_user_cart')->where(['user_id'=>$user['id'],'goods_id'=>['in',$goods_ids]])->column('goods_id'); $list[$list_k]['goods_id'] = $goods_id; $cart_number = Db::name('kuerp_user_cart')->where(['user_id'=>$user['id'],'goods_id'=>['in',$goods_ids]])->sum('number'); $list[$list_k]['cart_number'] = $cart_number; } $this->success('查询成功',$list,200); } /** * 获取商品列表 * @ApiMethod (POST) */ public function goodsGoodsList() { $data = input(); $page = isset($data['page'])?$data['page']:1; $page = empty($page)?1:$page; $limit = isset($data['limit'])?$data['limit']:10; $limit = empty($limit)?10:$limit; $where = []; $keyword = isset($data['keyword'])?$data['keyword']:''; if(!empty($keyword)){ $where['g.goods_code|g.goods_name|g.goods_barcode'] = ['like','%'.$keyword.'%']; } $promotion = isset($data['promotion'])?$data['promotion']:''; if(!empty($promotion)){ $where['g.promotion'] = '1'; } $goods_type_id = isset($data['goods_type_id'])?$data['goods_type_id']:''; if(!empty($goods_type_id)){ $goods_type_ids = Db::name('kuerp_goods_type')->where(['path'=>['like','%,'.$goods_type_id.',%']])->column('id'); $goods_type_ids[] = $goods_type_id; $where['g.goods_type_id'] = ['in',$goods_type_ids]; } $where['g.status'] = '1';$where['g.order_status'] = '1'; $tag = isset($data['tag'])?$data['tag']:''; if(!empty($tag)){ $where['g.tag'] = ['like','%'.$tag.'%']; } $fieldInfo = $this->getPriceField(); $customerlevel = $fieldInfo['customerlevel'];$field = $fieldInfo['field']; $price_decimal = \think\Db::name('kuerp_sets')->where(['name'=>'price_decimal'])->value('value'); $price_decimal = (int)$price_decimal; $list = Db::name('kuerp_goods')->alias('g') ->join('kuerp_unit u','g.unit_id=u.id') ->where($where) ->page($page,$limit) ->field('g.id,g.goods_code,g.goods_type_id,g.goods_name,g.goods_barcode,g.note,g.goods_specification_id as goods_spec,g.unit_id,u.name as unit_name,g.more_unit ,g.'.$field.' as price,g.discount,g.discount2,g.goods_image,g.inventory as stock,g.status,FROM_UNIXTIME(g.updatetime,\'%Y-%m-%d %H:%i:%s\') as updatetime') ->order(['g.sale_number'=>'DESC','g.id'=>'DESC']) ->select(); $user = $this->auth_s->getUserinfo(); $user['id'] = isset($user['id'])?$user['id']:0; foreach ($list as $list_k=>$list_v){ if(empty($list_v['price'])){ $unit_data = $this->getGoodsUnit($list_v['id'],$list_v['unit_id']); $list[$list_k]['price'] = isset($unit_data[0]['price'])?$unit_data[0]['price']:0; $list_v['price'] = isset($unit_data[0]['price'])?$unit_data[0]['price']:0; } if(empty($list_v['goods_image'])){ $list[$list_k]['goods_image'] = $this->goods_image; } $list[$list_k]['goods_image'] = $list_v['goods_image'] ? cdnurl($list_v['goods_image'], true) : ''; if(!$fieldInfo['login']){ $list[$list_k]['price'] = ''; }else{ if($customerlevel==4){ $list[$list_k]['price'] = round(($list_v['price']*$list_v['discount']/100),$price_decimal); }else if($customerlevel==5){ $list[$list_k]['price'] = round(($list_v['price']*$list_v['discount2']/100),$price_decimal); } } unset($list[$list_k]['discount']);unset($list[$list_k]['discount2']); $unit = Db::name('kuerp_unit')->where(['id'=>$list_v['unit_id']])->field('id,name')->find(); if($unit){ $unit_name = $unit['name']; $rowadvance_rate = '1'; if($list_v['more_unit']==1){ $more_unit = Db::name('kuerp_unit_conversion')->alias('uc') ->join('kuerp_unit u','u.id=uc.vice_unit') ->where(['uc.main_unit'=>$unit['id']]) ->order('uc.advance_rate','ASC') ->field('uc.vice_unit,u.name,u.id,uc.advance_rate') ->select(); foreach ($more_unit as $unit_k=>$unit_v){ $unit_name .= ','.$unit_v['name']; $rowadvance_rate .= ':'.$unit_v['advance_rate']; } $list[$list_k]['unit_name'] = $unit_name.'('.$rowadvance_rate.')'; } } if(!empty($list_v['goods_type_id'])){ $path = Db::name('kuerp_goods_type')->where(['id'=>$list_v['goods_type_id']])->value('path'); $list[$list_k]['parent_goods_type_id'] = Db::name('kuerp_goods_type')->where(['id'=>['in',$path],'pid'=>0])->value('id'); }else{ $list[$list_k]['parent_goods_type_id'] = 0; } $goods_id = Db::name('kuerp_user_cart')->where(['user_id'=>$user['id'],'goods_id'=>$list_v['id']])->column('id'); if($goods_id){ $list[$list_k]['cart_status'] = 1; }else{ $list[$list_k]['cart_status'] = 0; } $list[$list_k]['cart_number'] = Db::name('kuerp_user_cart')->where(['user_id'=>$user['id'],'goods_id'=>$list_v['id']])->sum('number'); } $count = Db::name('kuerp_goods')->alias('g') ->join('kuerp_unit u','g.unit_id=u.id') ->where($where)->count(); foreach ($list as $list_k=>$list_v){ if($list_v['price']===''){ $list[$list_k]['decimal'] = ''; }else{ $price = explode('.',$list_v['price']); if(isset($price[1])){ $list[$list_k]['price'] = $price[0]; if(strlen($price[1]) > 2){ $price[1] = substr($price[1], 1 ,2); }else if (strlen($price[1])==1){ $price[1] = $price[1].'0'; }else if (strlen($price[1])==0){ $price[1] = '00'; } $list[$list_k]['decimal'] = $price[1]; }else{ $list[$list_k]['decimal'] = '00'; } } } $result['list'] = $list;$result['count'] = $count; $this->success('查询成功',$result,200); } /** * 获取商品详情 * @ApiMethod (POST) */ public function goodsGoodsInfo(){ $detail = input(); $check = (new \app\api\model\kuerp\UserAddress)->checkParameter($detail,[ 'id|商品'=>'require|number', ]); if(isset($check['status'])){ $this->error($check['msg'],'',500); }else{ $goods_unit = isset($detail['goods_unit'])?$detail['goods_unit']:''; $goods_attr = isset($detail['goods_attr'])?$detail['goods_attr']:''; $row = Db::name('kuerp_goods')->where(['id'=>$detail['id'],'status'=>'1']) ->field('id,goods_code,goods_name,goods_barcode,goods_specification_id as goods_spec,goods_type_id,unit_id, goods_image,goods_images,note,more_unit,content') ->find(); if(empty($row)){ $this->error('未查询到商品数据','',500); }else{ if(!empty($row['goods_type_id'])){ $path = Db::name('kuerp_goods_type')->where(['id'=>$row['goods_type_id']])->value('path'); $row['parent_goods_type_id'] = Db::name('kuerp_goods_type')->where(['id'=>['in',$path],'pid'=>0])->value('id'); }else{ $row['parent_goods_type_id'] = 0; } if(empty($row['goods_image'])){ $row['goods_image'] = $this->goods_image; } $row['goods_image'] = $row['goods_image'] ? cdnurl($row['goods_image'], true) : ''; if(!empty($row['goods_images'])){ $goods_images = explode(',',$row['goods_images']); foreach ($goods_images as $imgk=>$imgv){ $goods_images[$imgk] = $imgv ? cdnurl($imgv, true) : ''; } $row['goods_images'] = $goods_images; } $list = $this->getGoodsUnit($row['id'],$goods_unit,$goods_attr); foreach ($list as $list_k=>$list_v){ if($list_v['price']===''){ $list[$list_k]['decimal'] = ''; }else{ $price = explode('.',$list_v['price']); if(isset($price[1])){ $list[$list_k]['price'] = $price[0]; if(strlen($price[1]) > 2){ $price[1] = substr($price[1], 1 ,2); }else if (strlen($price[1])==1){ $price[1] = $price[1].'0'; }else if (strlen($price[1])==0){ $price[1] = '00'; } $list[$list_k]['decimal'] = $price[1]; }else{ $list[$list_k]['decimal'] = '00'; } } } $row['unit_data'] = $list; $row['attr_data'] = $this->getGoodsAuxiliary($row['id']); //添加浏览记录 $user = $this->auth_s->getUserinfo(); if(isset($user['id'])){ $date = date('Y-m-d'); $where = []; $where['user_id'] = $user['id']; $where['goods_id'] = $detail['id']; $where['look_date'] = $date; $history = (new UserHistory())->where($where)->find(); if(empty($history)){ $where['goods_unit'] = $goods_unit;$where['goods_attr'] = $goods_attr; (new UserHistory())->allowField(true)->save($where); } $cart_number = Db::name('kuerp_user_cart')->where(['user_id'=>$user['id'],'goods_id'=>$row['id']])->sum('number'); $row['cart_number'] = $cart_number; }else{ $row['cart_number'] = 0; } $this->success('查询成功',$row,200); } } } /** * 获取商品浏览历史列表 * @ApiMethod (POST) */ public function getHistory() { $data = input(); $page = isset($data['page'])?$data['page']:1; $page = empty($page)?1:$page; $limit = isset($data['limit'])?$data['limit']:10; $limit = empty($limit)?10:$limit; $list_type = isset($data['list_type'])?$data['list_type']:1; $list_type = $list_type==2?2:1; $user = $this->auth_s->getUserinfo(); if(isset($user['id'])){ $fieldInfo = $this->getPriceField(); $customerlevel = $fieldInfo['customerlevel'];$field = $fieldInfo['field']; $price_decimal = \think\Db::name('kuerp_sets')->where(['name'=>'price_decimal'])->value('value'); $price_decimal = (int)$price_decimal; if($list_type==2){ $goods_list = Db::name('kuerp_user_history')->alias('h') ->join('kuerp_goods g','g.id=h.goods_id') ->where(['h.user_id'=>$user['id']]) ->page($page,$limit) ->field('g.id,g.goods_name,g.'.$field.' as price,g.goods_image,g.discount,g.discount2,h.goods_unit,h.goods_attr') ->select(); foreach ($goods_list as $goods_list_k=>$goods_list_v){ if(!$fieldInfo['login']){ $goods_list[$goods_list_k]['price'] = ''; }else{ if($customerlevel==4){ $goods_list[$goods_list_k]['price'] = round(($goods_list_v['price']*$goods_list_v['discount']/100),$price_decimal); }else if($customerlevel==5){ $goods_list[$goods_list_k]['price'] = round(($goods_list_v['price']*$goods_list_v['discount2']/100),$price_decimal); } } unset($goods_list[$goods_list_k]['discount']);unset($goods_list[$goods_list_k]['discount2']); if(empty($goods_list_v['goods_image'])){ $goods_list[$goods_list_k]['goods_image'] = $this->goods_image; } $goods_list[$goods_list_k]['goods_image'] = $goods_list_v['goods_image'] ? cdnurl($goods_list_v['goods_image'], true) : ''; } foreach ($goods_list as $list_k=>$list_v){ if($list_v['price']===''){ $goods_list[$list_k]['decimal'] = ''; }else{ $price = explode('.',$list_v['price']); if(isset($price[1])){ $goods_list[$list_k]['price'] = $price[0]; if(strlen($price[1]) > 2){ $price[1] = substr($price[1], 1 ,2); }else if (strlen($price[1])==1){ $price[1] = $price[1].'0'; }else if (strlen($price[1])==0){ $price[1] = '00'; } $goods_list[$list_k]['decimal'] = $price[1]; }else{ $goods_list[$list_k]['decimal'] = '00'; } } } $list = $goods_list; $count = $goods_list = Db::name('kuerp_user_history')->alias('h') ->join('kuerp_goods g','g.id=h.goods_id') ->where(['h.user_id'=>$user['id']])->count(); }else{ $where = []; $where['user_id'] = $user['id']; $list = (new UserHistory())->where($where)->group('look_date')->order(['look_date'=>'DESC'])->field('look_date')->page($page,$limit)->select(); $count = (new UserHistory())->where($where)->group('look_date')->count('look_date'); foreach ($list as $list_k=>$list_v){ $goods_list = Db::name('kuerp_user_history')->alias('h') ->join('kuerp_goods g','g.id=h.goods_id') ->where(['h.look_date'=>$list_v['look_date'],'h.user_id'=>$user['id']]) ->field('g.id,g.goods_name,g.'.$field.' as price,g.goods_image,g.discount,g.discount2,h.goods_unit,h.goods_attr') ->select(); foreach ($goods_list as $goods_list_k=>$goods_list_v){ if(!$fieldInfo['login']){ $goods_list[$goods_list_k]['price'] = ''; }else{ if($customerlevel==4){ $goods_list[$goods_list_k]['price'] = round(($goods_list_v['price']*$goods_list_v['discount']/100),$price_decimal); }else if($customerlevel==5){ $goods_list[$goods_list_k]['price'] = round(($goods_list_v['price']*$goods_list_v['discount2']/100),$price_decimal); } } unset($goods_list[$goods_list_k]['discount']);unset($goods_list[$goods_list_k]['discount2']); if(empty($goods_list_v['goods_image'])){ $goods_list[$goods_list_k]['goods_image'] = $this->goods_image; } $goods_list[$goods_list_k]['goods_image'] = $goods_list_v['goods_image'] ? cdnurl($goods_list_v['goods_image'], true) : ''; } foreach ($goods_list as $list_k2=>$list_v2){ if($list_v2['price']===''){ $goods_list[$list_k2]['decimal'] = ''; }else{ $price = explode('.',$list_v2['price']); if(isset($price[1])){ $goods_list[$list_k2]['price'] = $price[0]; if(strlen($price[1]) > 2){ $price[1] = substr($price[1], 1 ,2); }else if (strlen($price[1])==1){ $price[1] = $price[1].'0'; }else if (strlen($price[1])==0){ $price[1] = '00'; } $goods_list[$list_k2]['decimal'] = $price[1]; }else{ $goods_list[$list_k2]['decimal'] = '00'; } } } $list[$list_k]['goods_list'] = $goods_list; } } $this->success('查询成功',['list'=>$list,'count'=>$count],200); }else{ $this->success('查询成功',[],200); } } /** * 获取购物车数量 * @ApiMethod (POST) */ public function getCartNumber(){ $user = $this->auth_s->getUserinfo(); if(!isset($user['id'])){ $this->error('请先登陆','',250); } $data = []; $data['user_id'] = $user['id']; $cart = (new UserCart())->where($data)->sum('number'); $this->success('查询成功',$cart,200); } /** * 获取购物车 * @ApiMethod (POST) */ public function getCart(){ $user = $this->auth_s->getUserinfo(); if(!isset($user['id'])){ $this->error('请先登陆','',250); } $data = []; $data['user_id'] = $user['id']; $cart = (new UserCart())->where($data)->select(); if($cart){ foreach ($cart as $cart_k=>$cart_v){ $goods_unit = $cart_v['goods_unit']; $goods_attr = $cart_v['goods_attr']; $row = Db::name('kuerp_goods')->where(['id'=>$cart_v['goods_id'],'status'=>'1']) ->field('id,goods_code,goods_name,goods_barcode,goods_specification_id as goods_spec,goods_type_id,unit_id, goods_image,goods_images,note,more_unit') ->find(); if($row){ if(empty($row['goods_image'])){ $row['goods_image'] = $this->goods_image; } $row['goods_image'] = $row['goods_image'] ? cdnurl($row['goods_image'], true) : ''; if(!empty($row['goods_images'])){ $goods_images = explode(',',$row['goods_images']); foreach ($goods_images as $imgk=>$imgv){ $goods_images[$imgk] = $imgv ? cdnurl($imgv, true) : ''; } $row['goods_images'] = $goods_images; } $unit_data = $this->getGoodsUnit($row['id'],$goods_unit,$goods_attr); $row['price'] = ''; $row['unit_name'] = ''; foreach ($unit_data as $unit_data_k=>$unit_data_v){ if($unit_data_v['unit']==$goods_unit){ $row['price'] = $unit_data_v['price']; $row['unit_name'] = $unit_data_v['name']; } } $row['attr_name'] = ''; $attr_data = $this->getGoodsAuxiliary($row['id']); foreach ($attr_data as $attr_data_k=>$attr_data_v){ if($attr_data_v['id']==$goods_attr){ $row['attr_name'] = $attr_data_v['attr_name']; } } }else{ $row['goods_images'] = []; $row['unit_data'] = []; $row['attr_data'] = []; } if($row['price']===''){ $row['decimal'] = ''; }else{ $price = explode('.',$row['price']); if(isset($price[1])){ $row['price'] = $price[0]; if(strlen($price[1]) > 2){ $price[1] = substr($price[1], 1 ,2); }else if (strlen($price[1])==1){ $price[1] = $price[1].'0'; }else if (strlen($price[1])==0){ $price[1] = '00'; } $row['decimal'] = $price[1]; }else{ $row['decimal'] = '00'; } } $cart[$cart_k]['goods'] = $row; } $this->success('查询成功',$cart,200); }else{ $this->error('购物车空空如也','',500); } } /** * 添加购物车 * @ApiMethod (POST) */ public function addCart(){ $detail = input(); $check = (new \app\api\model\kuerp\UserAddress)->checkParameter($detail,[ 'goods_id|商品'=>'require|number', 'goods_unit|商品单位'=>'require|number', 'number|数量'=>'require|number', ]); if(isset($check['status'])){ $this->error($check['msg'],'',500); }else{ $row = Db::name('kuerp_goods')->where(['id'=>$detail['goods_id'],'status'=>'1'])->value('id'); if(empty($row)){ $this->error('未查询到商品数据','',500); }else{ $user = $this->auth_s->getUserinfo(); if(!isset($user['id'])){ $this->error('请先登陆','',250); } $data = []; $data['user_id'] = $user['id']; $data['goods_id'] = $detail['goods_id']; $data['goods_unit'] = $detail['goods_unit']; $data['goods_attr'] = isset($detail['goods_attr'])?$detail['goods_attr']:0; $cart = (new UserCart())->where($data)->find(); if($cart){ $res = (new UserCart())->where($data)->update(['number'=>($detail['number']+$cart['number'])]); }else{ if($detail['number']<=0){ $this->error('数量不能等于0','',500); } $data['number'] = $detail['number']; $res = (new UserCart())->save($data); } if($res){ $this->success('添加成功',$res,200); } $this->error('添加失败','',500); } } } /** * 修改购物车 * @ApiMethod (POST) */ public function editCart(){ $detail = input(); $check = (new \app\api\model\kuerp\UserAddress)->checkParameter($detail,[ 'number|数量'=>'require|number', ]); if(isset($check['status'])){ $this->error($check['msg'],'',500); }else{ $detail['cart_id'] = isset($detail['cart_id'])?$detail['cart_id']:0; $detail['goods_id'] = isset($detail['goods_id'])?$detail['goods_id']:0; if($detail['cart_id']<=0 && $detail['goods_id']<=0){ $this->error('参数错误','',250); } $user = $this->auth_s->getUserinfo(); if(!isset($user['id'])){ $this->error('请先登陆','',250); } $data = []; $data['user_id'] = $user['id']; if($detail['cart_id']>0){ $data['id'] = $detail['cart_id']; }else{ $data['goods_id'] = $detail['goods_id']; } $cart = (new UserCart())->where($data)->find(); if($cart){ if($detail['number']<=0){ $this->error('数量不能等于0','',500); } $res = (new UserCart())->where($data)->update(['number'=>$detail['number']]); }else{ $this->error('未找到相关信息','',500); } if($res){ $this->success('编辑成功',$res,200); } $this->error('编辑失败','',500); } } /** * 删除购物车商品 * @ApiMethod (POST) */ public function delCart(){ $detail = input(); $check = (new \app\api\model\kuerp\UserAddress)->checkParameter($detail,[ 'cart_id|购物车'=>'require', ]); if(isset($check['status'])){ $this->error($check['msg'],'',500); }else{ $user = $this->auth_s->getUserinfo(); if(!isset($user['id'])){ $this->error('请先登陆','',250); } $data = []; $data['user_id'] = $user['id']; $data['id'] = ['in',$detail['cart_id']]; $cart = (new UserCart())->where($data)->find(); if($cart){ $res = (new UserCart())->where($data)->delete(); }else{ $this->error('未找到相关信息','',500); } if($res){ $this->success('操作成功',$res,200); } $this->error('操作失败','',500); } } /** * 清空购物车 * @ApiMethod (POST) */ public function cancelCart(){ $user = $this->auth_s->getUserinfo(); if(!isset($user['id'])){ $this->error('请先登陆','',250); } $data = []; $data['user_id'] = $user['id']; $cart = (new UserCart())->where($data)->find(); if($cart){ $res = (new UserCart())->where($data)->delete(); }else{ $this->error('未找到相关信息','',500); } if($res){ $this->success('操作成功',$res,200); } $this->error('操作失败','',500); } /** * 创建订货单 * @ApiMethod (POST) */ public function createOrder(){ $detail = input(); $check = (new \app\api\model\kuerp\UserAddress)->checkParameter($detail,[ 'cart_id|购物车'=>'require', ]); if(isset($check['status'])){ $this->error($check['msg'],'',500); }else{ $user = $this->auth_s->getUserinfo(); if(!isset($user['id'])){ $this->error('请先登陆','',250); } $data = []; $data['user_id'] = $user['id']; $data['id'] = ['in',$detail['cart_id']]; $detail['diver_type'] = isset($detail['diver_type'])?$detail['diver_type']:1; $diver_type = $detail['diver_type']==1?1:2; $store_id = isset($detail['store_id'])?$detail['store_id']:0; if($diver_type==2){ if(empty($store_id)){ $this->error('请选择自提门店','',400); } $addr = Db::name('kuerp_address')->where(['id'=>$store_id])->find(); if(empty($addr)){ $this->error('选择的自提门店不可用','',400); } } $cart = (new UserCart())->where($data)->select(); if($cart){ $address = isset($detail['address_id'])?$detail['address_id']:''; if($diver_type!=2){ if(empty($address)){ $address = (new UserAddress())->where(['user_id'=>$user['id'],'is_default'=>'1'])->find(); if(empty($address)){ $this->error('未设置默认收货地址,订货单创建失败','',500); } }else{ $address = (new UserAddress())->where(['user_id'=>$user['id'],'id'=>$address])->find(); if(empty($address)){ $this->error('未找到收货地址,订货单创建失败','',500); } } } $order_price = 0;$info = []; foreach ($cart as $cart_k=>$cart_v){ $goods_unit = $cart_v['goods_unit']; $goods_attr = $cart_v['goods_attr']; $row = Db::name('kuerp_goods')->where(['id'=>$cart_v['goods_id'],'status'=>'1']) ->field('id,unit_id,goods_name,goods_image') ->find(); if($row){ $unit_data = $this->getGoodsUnit($row['id'],$goods_unit,$goods_attr); $row['price'] = ''; $row['unit_name'] = ''; foreach ($unit_data as $unit_data_k=>$unit_data_v){ if($unit_data_v['unit']==$goods_unit){ $row['price'] = $unit_data_v['price']; $row['unit_name'] = $unit_data_v['name']; } } $row['attr_name'] = ''; $attr_data = $this->getGoodsAuxiliary($row['id']); foreach ($attr_data as $attr_data_k=>$attr_data_v){ if($attr_data_v['id']==$goods_attr){ $row['attr_name'] = $attr_data_v['attr_name']; } } if(!empty($row['price'])){ $order_price += ($row['price']*$cart_v['number']); unset($cart_v['id']); $info_row = []; $info_row['user_id'] = $user['id']; $info_row['goods_id'] = $cart_v['goods_id']; $info_row['goods_unit'] = $cart_v['goods_unit']; $info_row['goods_attr'] = $cart_v['goods_attr']; $info_row['goods_name'] = $row['goods_name']; $info_row['goods_image'] = $row['goods_image']; $info_row['unit_name'] = $row['unit_name']; $info_row['attr_name'] = $row['attr_name']; $info_row['number'] = $cart_v['number']; $info_row['price'] = $row['price']; $info_row['money'] = ($row['price']*$cart_v['number']); $info_row['createtime'] = time(); $info_row['updatetime'] = time(); $info[] = $info_row; } } } if(empty($info) || $order_price<=0){ $this->error('数据异常,订货单创建失败','',500); } $orderNum = $this->getOrderNum(); $order = [ 'user_id' => $user['id'], 'name' => isset($address['connect_name'])?$address['connect_name']:'', 'phone' => isset($address['connect_mobile'])?$address['connect_mobile']:'', 'address' => isset($address['detail_address'])?$address['detail_address']:'', 'order_num' => $orderNum, 'order_price' => $order_price, 'diver_type' => $diver_type, 'store_id' => $store_id, 'createtime' => time(), 'updatetime' => time(), ]; $order_id = false; Db::startTrans(); try { $order_id = Db::name('kuerp_user_order')->insertGetId($order); foreach ($info as $info_k=>$info_v){ $info[$info_k]['order_id'] = $order_id; } Db::name('kuerp_user_orderinfo')->insertAll($info); (new UserCart())->where($data)->delete(); Db::commit(); } catch (Exception $e) { Db::rollback(); } if($order_id){ $this->Message('xd',$orderNum); $this->success('订单创建成功',$orderNum,200); } $this->error('订单创建失败','',500); }else{ $this->error('未找到相关信息','',500); } } } /** * 获取订单列表 * @ApiMethod (POST) */ public function getOrderList() { $data = input(); $page = isset($data['page'])?$data['page']:1; $page = empty($page)?1:$page; $limit = isset($data['limit'])?$data['limit']:10; $limit = empty($limit)?10:$limit; $user = $this->auth_s->getUserinfo(); if(isset($user['id'])){ $where = []; $where['user_id'] = $user['id']; if(isset($data['status']) && $data['status']!==''){ $where['status'] = $data['status']; } $list = (new UserOrder())->where($where)->page($page,$limit)->order(['id'=>'DESC'])->select(); $count = (new UserOrder())->where($where)->count(); foreach ($list as $list_k=>$list_v){ $goods_list = Db::name('kuerp_user_orderinfo')->where(['order_id'=>$list_v['id']]) ->select(); foreach ($goods_list as $list_k2=>$list_v2){ $goods_list[$list_k2]['goods_image'] = $list_v2['goods_image'] ? cdnurl($list_v2['goods_image'], true) : ''; if($list_v2['price']===''){ $goods_list[$list_k2]['decimal'] = ''; }else{ $price = explode('.',$list_v2['price']); if(isset($price[1])){ $goods_list[$list_k2]['price'] = $price[0]; if(strlen($price[1]) > 2){ $price[1] = substr($price[1], 1 ,2); }else if (strlen($price[1])==1){ $price[1] = $price[1].'0'; }else if (strlen($price[1])==0){ $price[1] = '00'; } $goods_list[$list_k2]['decimal'] = $price[1]; }else{ $goods_list[$list_k2]['decimal'] = '00'; } } if($list_v2['money']===''){ $goods_list[$list_k2]['money_decimal'] = ''; }else{ $price = explode('.',$list_v2['money']); if(isset($price[1])){ $goods_list[$list_k2]['money'] = $price[0]; if(strlen($price[1]) > 2){ $price[1] = substr($price[1], 1 ,2); }else if (strlen($price[1])==1){ $price[1] = $price[1].'0'; }else if (strlen($price[1])==0){ $price[1] = '00'; } $goods_list[$list_k2]['money_decimal'] = $price[1]; }else{ $goods_list[$list_k2]['money_decimal'] = '00'; } } } $list[$list_k]['goods_list'] = $goods_list; } foreach ($list as $list_k2=>$list_v2){ if($list_v2['order_price']===''){ $list[$list_k2]['decimal'] = ''; }else{ $price = explode('.',$list_v2['order_price']); if(isset($price[1])){ $list[$list_k2]['order_price'] = $price[0]; if(strlen($price[1]) > 2){ $price[1] = substr($price[1], 1 ,2); }else if (strlen($price[1])==1){ $price[1] = $price[1].'0'; }else if (strlen($price[1])==0){ $price[1] = '00'; } $list[$list_k2]['decimal'] = $price[1]; }else{ $list[$list_k2]['decimal'] = '00'; } } } $this->success('查询成功',['list'=>$list,'count'=>$count],200); }else{ $this->success('查询成功',[],200); } } /** * 订单详情 * @ApiMethod (POST) */ public function orderInfo(){ $detail = input(); $check = (new \app\api\model\kuerp\UserAddress)->checkParameter($detail,[ 'order_num|订单号'=>'require', ]); if(isset($check['status'])){ $this->error($check['msg'],'',500); }else{ $user = $this->auth_s->getUserinfo(); if(!isset($user['id'])){ $this->error('请先登陆','',250); } $where = []; $where['user_id'] = $user['id']; $where['order_num'] = $detail['order_num']; $order = (new UserOrder())->where($where)->find(); if($order){ if($order['order_price']===''){ $order['decimal'] = ''; }else{ $price = explode('.',$order['order_price']); if(isset($price[1])){ $order['order_price'] = $price[0]; if(strlen($price[1]) > 2){ $price[1] = substr($price[1], 1 ,2); }else if (strlen($price[1])==1){ $price[1] = $price[1].'0'; }else if (strlen($price[1])==0){ $price[1] = '00'; } $order['decimal'] = $price[1]; }else{ $order['decimal'] = '00'; } } $goods_list = Db::name('kuerp_user_orderinfo')->where(['order_id'=>$order['id']]) ->select(); foreach ($goods_list as $list_k2=>$list_v2){ $goods_list[$list_k2]['goods_image'] = $list_v2['goods_image'] ? cdnurl($list_v2['goods_image'], true) : ''; if($list_v2['price']===''){ $goods_list[$list_k2]['decimal'] = ''; }else{ $price = explode('.',$list_v2['price']); if(isset($price[1])){ $goods_list[$list_k2]['price'] = $price[0]; if(strlen($price[1]) > 2){ $price[1] = substr($price[1], 1 ,2); }else if (strlen($price[1])==1){ $price[1] = $price[1].'0'; }else if (strlen($price[1])==0){ $price[1] = '00'; } $goods_list[$list_k2]['decimal'] = $price[1]; }else{ $goods_list[$list_k2]['decimal'] = '00'; } } if($list_v2['money']===''){ $goods_list[$list_k2]['money_decimal'] = ''; }else{ $price = explode('.',$list_v2['money']); if(isset($price[1])){ $goods_list[$list_k2]['money'] = $price[0]; if(strlen($price[1]) > 2){ $price[1] = substr($price[1], 1 ,2); }else if (strlen($price[1])==1){ $price[1] = $price[1].'0'; }else if (strlen($price[1])==0){ $price[1] = '00'; } $goods_list[$list_k2]['money_decimal'] = $price[1]; }else{ $goods_list[$list_k2]['money_decimal'] = '00'; } } } $order['goods_list'] = $goods_list; $this->success('查询成功',$order,200); }else{ $this->error('未找到相关信息','',500); } } } /** * 确认收货 * @ApiMethod (POST) */ public function receiving(){ $detail = input(); $check = (new \app\api\model\kuerp\UserAddress)->checkParameter($detail,[ 'order_num|订单号'=>'require', ]); if(isset($check['status'])){ $this->error($check['msg'],'',500); }else{ $user = $this->auth_s->getUserinfo(); if(!isset($user['id'])){ $this->error('请先登陆','',250); } $where = []; $where['user_id'] = $user['id']; $where['order_num'] = $detail['order_num']; $order = (new UserOrder())->where($where)->find(); if($order){ switch ($order['status']){ case 0: $this->error('此订单未审核,不可确认收货','',500); break; case 1: $this->error('此订单未发货,不可确认收货','',500); break; case 2: $this->error('此订单待发货,不可确认收货','',500); break; case 3: $status = (new UserOrder())->where($where)->update(['status'=>'4']); if($status){ $this->success('确认收货成功','',200); }else{ $this->error('确认收货失败,请稍后再试','',500); } break; case 4: $this->error('此订单已收货,不可重复收货','',500); break; case 5: $this->error('此订单已申请退款,不可确认收货','',500); break; case 6: $this->error('此订单已退款,不可确认收货','',500); break; case 7: $this->error('此订单已取消,不可确认收货','',500); break; case 8: $this->error('此订单已完成,不可确认收货','',500); break; default: $this->error('状态异常,不可确认收货','',500); } }else{ $this->error('未找到相关信息','',500); } } } /** * 取消订单 * @ApiMethod (POST) */ public function cancelOrder(){ $detail = input(); $check = (new \app\api\model\kuerp\UserAddress)->checkParameter($detail,[ 'order_num|订单号'=>'require', ]); if(isset($check['status'])){ $this->error($check['msg'],'',500); }else{ $user = $this->auth_s->getUserinfo(); if(!isset($user['id'])){ $this->error('请先登陆','',250); } $where = []; $where['user_id'] = $user['id']; $where['order_num'] = $detail['order_num']; $order = (new UserOrder())->where($where)->find(); if($order){ switch ($order['status']){ case 0: $status = (new UserOrder())->where($where)->delete(); if($status){ $this->success('取消订单成功','',200); }else{ $this->error('取消订单失败,请稍后再试','',500); } break; case 1: $this->error('此订单未发货,不可取消','',500); break; case 2: $this->error('此订单待发货,不可取消','',500); break; case 3: $this->error('此订单已发货,不可取消','',500); break; case 4: $this->error('此订单已收货,不可取消','',500); break; case 5: $this->error('此订单已申请退款,不可取消','',500); break; case 6: $this->error('此订单已退款,不可取消','',500); break; case 7: $this->error('此订单已取消,不可取消','',500); break; case 8: $this->error('此订单已完成,不可取消','',500); break; default: $this->error('状态异常,不可取消','',500); } }else{ $this->error('未找到相关信息','',500); } } } /** * 获取商品多单位以及单位价格 * @param int $id 商品ID * @return array */ public function getGoodsUnit($id,$goods_unit='',$goods_attr=''){ $fieldInfo = $this->getPriceField(); $customerlevel = $fieldInfo['customerlevel'];$field = $fieldInfo['field']; $price_decimal = \think\Db::name('kuerp_sets')->where(['name'=>'price_decimal'])->value('value'); $price_decimal = (int)$price_decimal; $row = Db::name('kuerp_goods')->where(['id'=>$id])->field('more_unit,unit_id,more_unit_id,'.$field.' as price,discount,discount2,auxiliary')->find(); if(empty($row)){ return []; } $where = []; $where['goods_id'] = $id; if($goods_attr){ $attr_id = Db::name('kuerp_goods_attr')->where($where)->where(['id'=>$goods_attr]) ->value('attribute_id'); if($attr_id){ $where['attribute_id'] = $attr_id; } } $stock = Db::name('kuerp_goods_stock')->where($where)->sum('stock'); if($row['more_unit']!=1){ $unit_name = Db::name('kuerp_unit')->where(['id'=>$row['unit_id']])->value('name'); if($row['auxiliary']==1){ $where = []; if($goods_attr){ $where['id'] = $goods_attr; } $goods_attr = Db::name('kuerp_goods_attr')->where($where)->where(['goods_id'=>$id]) ->field($field.' as price,discount,discount2')->find(); $row['price'] = isset($goods_attr['price'])?$goods_attr['price']:0; $row['discount'] = isset($goods_attr['discount'])?$goods_attr['discount']:0; $row['discount2'] = isset($goods_attr['discount2'])?$goods_attr['discount2']:0; } $goods_price = []; $goods_price[] = [ 'unit' => $row['unit_id'], 'price' => $row['price'], 'discount' => $row['discount'], 'discount2' => $row['discount2'], 'is_default' => 1, 'name' => $unit_name, 'stock' => $stock ]; }else{ $main_unit = $row['more_unit_id']; $unit_list = Db::name('kuerp_unit_conversion') ->where(['main_unit'=>$main_unit]) ->order('advance_rate','ASC') ->field('vice_unit,advance_rate') ->select(); $unit_id = [$main_unit]; foreach ($unit_list as $unit_list_k=>$unit_list_v){ $unit_id[] = $unit_list_v['vice_unit']; } $unit_list_id = Db::name('kuerp_unit_conversion') ->where(['main_unit'=>$main_unit]) ->column('advance_rate','vice_unit'); $goods_price = Db::name('kuerp_goods_price')->alias('g') ->join('kuerp_unit u','u.id=g.unit') ->where(['g.goods_id'=>$id]) ->field('g.unit,g.'.$field.' as price,g.discount,g.discount2,g.is_default,u.name') ->order('g.is_default DESC')->select(); } foreach ($goods_price as $list_k=>$list_v){ if($list_v['is_default']==1){ $goods_price[$list_k]['stock'] = is_null($stock)?0:$stock; }else{ if(isset($unit_list_id[$list_v['unit']]) && $unit_list_id[$list_v['unit']]!=0){ $goods_price[$list_k]['stock'] = round($stock/$unit_list_id[$list_v['unit']],2); }else{ $goods_price[$list_k]['stock'] = is_null($stock)?0:$stock; } } if(!$fieldInfo['login']){ $goods_price[$list_k]['price'] = ''; }else{ if($customerlevel==4){ $goods_price[$list_k]['price'] = round(($list_v['price']*$list_v['discount']/100),$price_decimal); }else if($customerlevel==5){ $goods_price[$list_k]['price'] = round(($list_v['price']*$list_v['discount2']/100),$price_decimal); } } unset($goods_price[$list_k]['discount']);unset($goods_price[$list_k]['discount2']); } return $goods_price; } /** * 获取商品属性信息 * @param int $id 商品ID * @return array */ public function getGoodsAuxiliary($id){ $fieldInfo = $this->getPriceField(); $customerlevel = $fieldInfo['customerlevel'];$field = $fieldInfo['field']; $price_decimal = \think\Db::name('kuerp_sets')->where(['name'=>'price_decimal'])->value('value'); $price_decimal = (int)$price_decimal; $auxiliary = Db::name('kuerp_auxiliary')->where(['status'=>1])->column('name','id'); $goods_attr = Db::name('kuerp_goods_attr')->where(['goods_id'=>$id]) ->field('id,attribute_id,attr_code,barcode,unit,'.$field.' as price,discount,discount2')->select(); foreach ($goods_attr as $goods_attr_k=>$goods_attr_v){ $aux_combination = Db::name('kuerp_aux_combination')->where(['id'=>$goods_attr_v['attribute_id']])->value('attr_ids'); $goods_attr[$goods_attr_k]['attr_name'] = ''; if($aux_combination){ $aux_combination = explode('&',$aux_combination); foreach ($aux_combination as $aux_combination_k=>$aux_combination_v){ $aux_combination[$aux_combination_k] = isset($auxiliary[$aux_combination_v])?$auxiliary[$aux_combination_v]:''; } $aux_combination = implode('/',$aux_combination); $goods_attr[$goods_attr_k]['attr_name'] = $aux_combination; } if(!$fieldInfo['login']){ $goods_attr[$goods_attr_k]['price'] = ''; }else{ if($customerlevel==4){ $goods_attr[$goods_attr_k]['price'] = round(($goods_attr_v['price']*$goods_attr_v['discount']/100),$price_decimal); }else if($customerlevel==5){ $goods_attr[$goods_attr_k]['price'] = round(($goods_attr_v['price']*$goods_attr_v['discount2']/100),$price_decimal); } } unset($goods_attr[$goods_attr_k]['discount']);unset($goods_attr[$goods_attr_k]['discount2']);unset($goods_attr[$goods_attr_k]['attribute_id']); } $list = $goods_attr; foreach ($list as $list_k=>$list_v){ if($list_v['price']===''){ $list[$list_k]['decimal'] = ''; }else{ $price = explode('.',$list_v['price']); if(isset($price[1])){ $list[$list_k]['price'] = $price[0]; if(strlen($price[1]) > 2){ $price[1] = substr($price[1], 1 ,2); }else if (strlen($price[1])==1){ $price[1] = $price[1].'0'; }else if (strlen($price[1])==0){ $price[1] = '00'; } $list[$list_k]['decimal'] = $price[1]; }else{ $list[$list_k]['decimal'] = '00'; } } } return $list; } /** * 获取客户登录后的查询价格字段 * @ApiMethod (POST) */ public function getPriceField(){ $user = $this->auth_s->getUserinfo(); $customerlevel = 0;$field = 'market_sell_price'; $login = false; if(isset($user['binding']) && isset($user['customers_id']) && $user['binding']==1 && !empty($user['customers_id'])){ $customerlevel = Db::name('kuerp_customers')->where(['id'=>$user['customers_id']])->value('customerlevel_id'); switch ($customerlevel){ case 1: $field = 'market_sell_price'; break; case 2: $field = 'wholesale_price'; break; case 3: $field = 'vip_price'; break; } $login = true; } return ['login'=>$login,'customerlevel'=>$customerlevel,'field'=>$field]; } /** * 获取订单号 * @ApiMethod (POST) */ function getOrderNum($prefix='DH',$db='kuerp_user_order',$whereField='order_num'){ list($s1, $s2) = explode(' ', microtime()); $hao = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); $orderNum = $prefix.date('YmdHis').substr($hao,8,5).rand(100000,999999); if(!empty($db) && !empty($where)){ $order = Db::name($db)->where([$whereField=>$orderNum])->value('id'); if($order){ return $this->getOrderNum(); } } return $orderNum; } }