ecshopX接口书写和调用

  • 后台接口书写
    找到路由文件,例如:routes/api/trade.php在里面加上需要的路由
//新增合伙人排名
        $api->get('/members/memberpartner',  ['name'=>'合伙人排名','middleware'=>'activated',  'as' => '', 'uses' =>'Order@getMemberPartner']);

找到路由中指向的文件Order,在里面创建方法getMemberPartner

public function getMemberPartner(Request $request)
    {
        $orderAssociationService = new OrderAssociationService();
        $MemberCardService = new MemberCardService();
        $result = $MemberCardService->getMemberGradeNameIdServices('2');
        $arr = ['grade_id' => $result];
         //开始的时间
         $time_start_begin = $request->input('time_start_begin');
         //结束的时间
         $time_start_end = $request->input('time_start_end');
         if (!$time_start_begin && !$time_start_end){
             $time_start_begin='0';
             $time_start_end='999999999999999';
         }
        //data->二级合伙人 
        $data = $this->memberService->getMemberList($arr);
        foreach($data as $key => $value){
           $is_buy = 0;
           //二级合伙人下级所有的会员,包括无效的
           $inviterData[$key][] = $this->memberService->getInviterIdService($value['user_id']);
            foreach($inviterData[$key][0] as $k => $v){
                //查询每个会员是否购买商品,是否是有效会员
                $is_buy += $this->memberService->getIsBuyService($v['user_id'],0);
                $data[$key]['userId'][] = $v['user_id'];
            }
            //有效会员的数量
            $data[$key]['memberCount'] = $is_buy;
        }

        foreach($data as $key => $value){
            foreach($value['userId'] as $k => $v){
                 //有效订单金额,根据时间筛选订单金额
                $data[$key]['orderAmount'][] = $orderAssociationService->sumCountOrderAmount($v,'SUCCESS',$time_start_begin,$time_start_end,'CANCEL');
                 //有效订单数量,根据时间筛选订单数量
                $data[$key]['orderCount'][] = $orderAssociationService->vaildCountService($v,'SUCCESS',$time_start_begin,$time_start_end,'CANCEL');
            }
            //有效订单金额
            $data[$key]['orderAmount'] = array_sum($data[$key]['orderAmount']);
            //有效订单金额转换
            if(!$data[$key]['orderAmount']){
                $data[$key]['orderAmount'] = "0";
            }else {
                //单位是分转换成元
                $data[$key]['orderAmount'] = $data[$key]['orderAmount'] / 100;
            }
            //有效订单数量
            $data[$key]['orderCount'] = array_sum($data[$key]['orderCount']);
            //所获得的返佣总金额
            $data[$key]['rebateAmount'] = $orderAssociationService->rebateAmountService($value['user_id']);
            //返佣总金额转换
            if(!$data[$key]['rebateAmount']){
                $data[$key]['rebateAmount'] = "0";
            }else {
                //单位是分转换成元
                $data[$key]['rebateAmount'] = $data[$key]['rebateAmount'] / 100;
            }
            //二级合伙人总数量
            $data[$key]['total_count'] = $this->memberService->getGradeIdCountService($result);
         }
        //排序,默认以返佣金额进行排序 
        $lastOrderAmount = array_column($data,'rebateAmount');
        array_multisort($lastOrderAmount,SORT_DESC,$data);
        return $data;
    }
  • Repository层的写法
    Repository层主要就是用来执行sql的,里面可以使用多张表,通常情况一个Repository对应的就是一个表
###引用表popularize_promoter
use PopularizeBundle\Entities\Promoter;
class MembersRepository extends EntityRepository
{
    //定义需要操作的表名称
    public $popularize_promoter = 'popularize_promoter';

    //查询二级合伙人的总数量
    public function getGradeIdCount($filter,$status='0')
    {
        $conn = app('registry')->getConnection('default');
        $qb = $conn->createQueryBuilder();
        $qb->select('count(*)')  ###查询的字段
            ->from($this->popularize_promoter)  ###表名称
            ->andWhere($qb->expr()->andX(
                $qb->expr()->eq('user_id', $qb->expr()->literal($filter))
            ))   ###相当于where条件,传过来的不能是数组,必须是string类型
            ->andWhere($qb->expr()->andX(
                $qb->expr()->neq('is_buy', $qb->expr()->literal($status))
            ));
        return $qb->execute()->fetchAll();  ###执行语句   
    }
}
  • Service层的写法写
    Service层主要就是用来调用Repository
###指定Repository
public $membersRepository;

###调用Repository
public function getIsBuyService($filter,$status)
    {
        $res = $this->membersRepository->getIsBuy($filter,$status);
        if($res) {
            return $res[0]['count(*)'];
        }
        return [];
    }
  • 接口
###引用Service
use PopularizeBundle\Services\PromoterService;
use MembersBundle\Services\MemberService;

###调用Service
//第一种方法
    public $memberService;

    public function __construct()
    {
        $this->memberService = new MemberService;
        $this->limit = 100;
    }

//第二种方法
$PromoterService = new PromoterService();
  • 后台sql注释
eq相等 
ne、neq不相等 
gt大于 
lt小于 
gte、ge大于等于 
lte、le小于等于 
not非 
mod求模
  • 后台vue调用接口
    找到接口路由文件
app/src/api/member.js

###加上
export function getMemberPartner (query) {  ###需要参数就传
  return fetch({
    url: '/members/memberpartner', ###对应后端的路由地址
    method: 'get',  ###和后端路由同步请求方式
    params:query
  })
}

###引用接口
 import {
    mapGetters
  } from 'vuex'
  import {
    getMemberPartner,
  } from '../../../api/member'

###请求接口
 getSelectMessage() {
        this.loading = true
        ###this.params就是参数
        getMemberPartner(this.params).then(response => {
          this.memberData = response.data.data
          this.total_count = Number(response.data.data[0].total_count)
          this.loading = false
        })
      },

###参数定义
data() {
      return {
        loading: false,
        mobile: '',
        total_count: 0,
        memberData: [],
        params: {
          page: 1,
          pageSize: 3,
          order: 0,
          time_start_begin: '',
          time_start_end: ''
        },
        create_time:'',
      }
    },

###加载页面自动请求接口
mounted () {
      this.getSelectMessage()  ###上面调用接口的方法名
    }

ishow.bind-time.com Copyright © 2020 小肥青王二狗

添加新评论

无论有多困难,都坚强地抬头挺胸,告诉所有人,你并非他们想象的那样不堪一击。

每个人心里都有一段伤痕,时间才是最好的疗剂。

人总是珍惜未得到的,而遗忘了所拥有的。

退一步,并不象征我认输;放手,并不表示我放弃;微笑,并不意味我快乐!

人海中再回首,朋友真诚依旧,生命里重逢,心境平和温柔,往事如风,岁月如歌,漫漫人生路,苍桑几许,幸福几何!