【实例简介】此实例为小程序的后端api接口,前端程序在这里 http://www.haolizi.net/example/view_14959.html
appid 数据库名称修改成自己的 就可以了
【实例截图】
【核心代码】
orders.php
defined('BASEPATH') OR exit('No direct script access allowed');
use \QCloud_WeApp_SDK\Auth\LoginService as LoginService;
use QCloud_WeApp_SDK\Constants as Constants;
class Orders extends MY_Controller {
public function __construct() {
parent::__construct();
$this->load->driver('cache', array('adapter' => 'file'));
}
/**
* orders
*/
public function index() {
$get = $this->input->get(NULL, TRUE);
$act = isset($get['act']) ? $get['act'] : '';
if ($act == 'list') {
$this->get_list();
} else if ($act == 'add') {
$this->add();
} else if ($act == 'info') {
$this->info();
} else {
$this->json([
'code' => -1,
'msg' => '参数异常!',
'data' => []
]);
}
}
/**
* 订单列表
*/
public function get_list() {
$get = $this->input->get(NULL, TRUE);
$session3rd = isset($get['session3rd']) ? $get['session3rd'] : '';
$sessionid = $this->cache->get($session3rd);
$page = isset($get['page']) ? $get['page'] : 1;
$status = isset($get['status']) ? $get['status'] : 'all';
$limit = isset($get['limit']) ? $get['limit'] : 10;
if ($sessionid) {
$dat = explode('-', $sessionid);
$openid = $dat[0];
switch ($status){
case 'submitted': //已提交
$map=array('openid' => $openid,'status'=>1);
break;
case 'confirmed': //已确认,
$map=array('openid' => $openid,'status'=>2);
break;
case 'finished': //已完成
$map=array('openid' => $openid,'status'=>3);
break;
case 'canceled': //已取消
$map=array('openid' => $openid,'status'=>4);
break;
default:
$map=array('openid' => $openid);
}
$this->db->where($map);
$result['total'] = $this->db->count_all_results('orders');
if ($limit > 0) {
$this->db->limit($limit, ($page - 1) * $limit);
}
$result['items']=array();
$items= $this->db->get('orders')->result_array();
$result['totalPage'] = ceil($result['total'] / $limit);
foreach($items as $key=>$v){
$result['items'][$key]=$v;
$result['items'][$key]['goods']=$this->getOrderGood($v['orderid']);
}
$this->json([
'code' => 0,
'msg' => '成功!',
'data' => $result
]);
} else {
$this->json([
'code' => -4,
'msg' => '登录过期!',
'data' => []
]);
}
}
/**
* 提交订单
* @return booleant
*/
public function add() {
$get = $this->input->get(NULL, TRUE);
$address_id = isset($get['address_id']) ? $get['address_id'] : '';
$session3rd = isset($get['session3rd']) ? $get['session3rd'] : '';
$sessionid = $this->cache->get($session3rd);
if ($sessionid) {
$totalprice = 0;
$dat = explode('-', $sessionid);
$params = $this->db->where(array('openid' => $dat[0], 'selected' => 1))->get('cart')->result_array();
if ($params) {
foreach ($params as $v) {
$totalprice = abs($v['price'] * $v['num']);
$result = $this->db->where(array('id' => $v['good_id']))->get('product')->row_array();
if (empty($result)) {
$this->json([ 'code' => -1, 'msg' => '产品不存在!', 'data' => []]);
return false;
}
}
$address = $this->getAddress($address_id, $dat[0]);
$order = array(
'ordersn' => time() . rand(10000, 99999),
'openid' => $dat[0],
'username' => $dat[0],
'totalprice' => $totalprice,
'address_id' => $address_id,
'address' => $address,
'status' =>1,
'addtime' => time()
);
$insert=$this->db->insert('orders', $order);
$insertid=$this->db->insert_id();
if ($insert) {
$this->addGoods($insertid,$params);
$this->json([
'code' => 0,
'msg' => '成功!',
'data' => array('id' =>$insertid)
]);
} else {
$this->json([
'code' => -1,
'msg' => '失败!',
'data' => []
]);
}
} else {
$this->json([
'code' => -1,
'msg' => '失败!',
'data' => []
]);
}
} else {
$this->json([
'code' => -4,
'msg' => '登录过期!',
'data' => []
]);
}
}
/**
* 订单详情
*/
public function info() {
$get = $this->input->get(NULL, TRUE);
$orderid = isset($get['id']) ? $get['id'] : '';
$session3rd = isset($get['session3rd']) ? $get['session3rd'] : '';
$sessionid = $this->cache->get($session3rd);
if ($sessionid) {
$dat = explode('-', $sessionid);
$result = $this->db->where(array('openid' => $dat[0], 'orderid' => $orderid))->get('orders')->row_array();
if ($result) {
$address = $this->db->where(array('openid' => $dat[0], 'id' => $result['address_id']))->get('address')->row_array();
$result['address'] = $address;
$result['items'] = $this->getOrderGood($orderid);
$this->json([
'code' => 0,
'msg' => '成功!',
'data' => $result
]);
} else {
$this->json([
'code' => -1,
'msg' => '失败!',
'data' => []
]);
}
} else {
$this->json([
'code' => -4,
'msg' => '登录过期!',
'data' => []
]);
}
}
/**
* 收货地址
* @param type $address_id
* @param type $openid
* @return type
*/
protected function getAddress($address_id,$openid) {
$data = $this->db->where(array('id' => $address_id,'openid'=>$openid))->get('address')->row_array();
if($data){
return $data['name'].'---'.$data['tel'].'---'.$data['address'];
}else{
return '';
}
}
/**
* 订单商品
* @param type $address_id
* @param type $openid
* @return type
*/
protected function getOrderGood($orderid) {
$data = $this->db->where(array('orderid' => $orderid))->get('order_info')->result_array();
return $data;
}
/**
* 订单商品添加
* @param type $orderid 订单id
* @param type $params 订单商品
*/
protected function addGoods($orderid, $params) {
foreach ($params as $v) {
$data = array(
'orderid' => $orderid,
'good_id' => $v['good_id'],
'good_name' => $v['good_name'],
'image' => $v['image'],
'price' => $v['price'],
'num' => $v['num'],
'totalprice' => abs($v['price'] * $v['num'])
);
$this->db->insert('order_info', $data);
$this->db->where(array('openid' => $v['openid'], 'good_id' => $v['good_id']))->delete('cart');
}
}
}
cart.php
defined('BASEPATH') OR exit('No direct script access allowed');
use \QCloud_WeApp_SDK\Auth\LoginService as LoginService;
use QCloud_WeApp_SDK\Constants as Constants;
class Cart extends MY_Controller {
public function __construct() {
parent::__construct();
$this->load->driver('cache', array('adapter' => 'file'));
}
/**
* 购物车
*/
public function index() {
$get = $this->input->get(NULL, TRUE);
$act = isset($get['act']) ? $get['act'] : '';
if($act=='list'){
$this->get_list();
}else if($act=='add'){
$this->add();
}else if($act=='update'){
$this->edit();
}else if($act=='updates'){
$this->updates();
}else if($act=='del'){
$this->del();
}else{
$this->json([
'code' => -1,
'msg' => '参数异常!',
'data' => []
]);
}
}
public function get_list(){
$get = $this->input->get(NULL, TRUE);
$session3rd = isset($get['session3rd']) ? $get['session3rd'] : '';
$sessionid=$this->cache->get($session3rd);
$page=isset($get['page']) ? $get['page'] : 1;
$limit=isset($get['limit']) ? $get['limit'] : 5;
if($sessionid){
$dat= explode('-', $sessionid);
$openid=$dat[0];
$this->db->where(array('openid'=>$openid));
$result['total']=$this->db->count_all_results('cart');
if ($limit > 0) {
$this->db->limit($limit, ($page-1)*$limit);
}
$result['items'] = $this->db->get('cart')->result_array();
$result['params']=array('page'=>$page);
$result['totalPage']= ceil($result['total']/$limit);
$this->json([
'code' => 0,
'msg' => '成功!',
'data' => $result
]);
}else{
$this->json([
'code' => -4,
'msg' => '登录过期!',
'data' => []
]);
}
}
public function add(){
$get = $this->input->get(NULL, TRUE);
$id= isset($get['id']) ? $get['id'] : ''; //产品id
$session3rd = isset($get['session3rd']) ? $get['session3rd'] : '';
$sessionid=$this->cache->get($session3rd);
if($sessionid){
$dat= explode('-', $sessionid);
if($id){
$result=$this->db->where(array('id'=>$id))->get('product')->row_array();
if(!$result){
$this->json([
'code' => -1,
'msg' => '产品不存在!',
'data' => []
]);
return false;
}
$data=array(
'openid'=>$dat[0],
'good_id'=>$id,
'good_name'=>$result['product_name'],
'image'=>$result['product_img'],
'num'=>1,
'price'=>$result['product_price'],
'selected'=>1,
'addtime'=>time()
);
$cartdata=$this->db->where(array('openid'=>$dat[0],'good_id'=>$id))->get('cart')->row_array();
if($cartdata){
$this->db->where(array('openid'=>$dat[0],'good_id'=>$id));
$this->db->set('num','num 1',FALSE);
$this->db->update('cart');
$this->json([
'code' => 0,
'msg' => '添加成功!',
'data' => []
]);
}else{
$insert=$this->db->insert('cart',$data);
if($insert){
$this->json([
'code' => 0,
'msg' => '成功!',
'data' => $data
]);
}else{
$this->json([
'code' => -1,
'msg' => '失败!',
'data' => []
]);
}
}
}else{
$this->json([
'code' => -1,
'msg' => '失败!',
'data' => []
]);
}
}else{
$this->json([
'code' => -4,
'msg' => '登录过期!',
'data' => []
]);
}
}
public function edit(){
$get = $this->input->get(NULL, TRUE);
$id= isset($get['id']) ? $get['id'] : ''; //产品id
$num= isset($get['total']) ? $get['total'] : ''; //产品数量
$session3rd = isset($get['session3rd']) ? $get['session3rd'] : '';
$sessionid=$this->cache->get($session3rd);
if($sessionid){
$dat= explode('-', $sessionid);
if($id){
$result=$this->db->where(array('id'=>$id))->get('product')->row_array();
if(!$result){
$this->json([
'code' => -1,
'msg' => '商品不存在!',
'data' => []
]);
return false;
}
$data=array('num'=>$num);
$cartdata=$this->db->where(array('openid'=>$dat[0],'good_id'=>$id))->get('cart')->row_array();
if($cartdata){
$this->db->where(array('openid'=>$dat[0],'good_id'=>$id));
$this->db->update('cart',$data);
$this->json([
'code' => 0,
'msg' => '更新成功!',
'data' => []
]);
}else{
$this->json([
'code' => -1,
'msg' => '失败!',
'data' => []
]);
}
}else{
$this->json([
'code' => -1,
'msg' => '商品不存在!',
'data' => []
]);
}
}else{
$this->json([
'code' => -4,
'msg' => '登录过期!',
'data' => []
]);
}
}
public function updates(){
$get = $this->input->get(NULL, TRUE);
$cartids= isset($get['cartid']) ? json_decode($get['cartid'],true) : ''; //产品id
$session3rd = isset($get['session3rd']) ? $get['session3rd'] : '';
$sessionid=$this->cache->get($session3rd);
if($sessionid){
$dat= explode('-', $sessionid);
if($cartids){
foreach($cartids as $v){
$data=array('num'=>$v['num']);
$this->db->where(array('openid'=>$dat[0],'good_id'=>$v['good_id']));
$this->db->update('cart',$data);
}
$this->json([
'code' => 0,
'msg' => '成功!',
'data' => $cartids
]);
}else{
$this->json([
'code' => -1,
'msg' => '商品不存在!',
'data' => []
]);
}
}else{
$this->json([
'code' => -4,
'msg' => '登录过期!',
'data' => []
]);
}
}
public function del(){
$get = $this->input->get(NULL, TRUE);
$id= isset($get['id']) ? $get['id'] : ''; //产品id
$session3rd = isset($get['session3rd']) ? $get['session3rd'] : '';
$sessionid=$this->cache->get($session3rd);
if($sessionid){
$dat= explode('-', $sessionid);
if($id){
$result=$this->db->where(array('id'=>$id))->get('product')->row_array();
if(!$result){
$this->json([
'code' => -1,
'msg' => '商品不存在!',
'data' => []
]);
return false;
}
$cartdata=$this->db->where(array('openid'=>$dat[0],'good_id'=>$id))->get('cart')->row_array();
if($cartdata){
$this->db->where(array('openid'=>$dat[0],'good_id'=>$id));
$this->db->delete('cart');
$this->json([
'code' => 0,
'msg' => '删除成功!',
'data' =>[]
]);
}else{
$this->json([
'code' => -1,
'msg' => '失败!',
'data' => []
]);
}
}else{
$this->json([
'code' => -1,
'msg' => '商品不存在!',
'data' => []
]);
}
}else{
$this->json([
'code' => -4,
'msg' => '登录过期!',
'data' => []
]);
}
}
}