ShopController.php 3.5 KB
<?php
/**
 * Created by PhpStorm.
 * auther: sgj
 * Date: 2018/12/17
 * Time: 16:18
 */

namespace app\portal\controller;


use app\portal\model\GoodsModel;
use app\user\model\UserModel;
use cmf\controller\WeChatBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;
use think\Request;

class ShopController extends  WeChatBaseController
{
    protected $GoodsModel;


    public function __construct(GoodsModel $GoodsModel)
    {
        parent::__construct();
        $this->GoodsModel = $GoodsModel;
    }


    public function index()
    {
        $goods=$this->GoodsModel->getList()->toArray();
        $this->assign('goods',$goods);
        return $this->fetch();
    }

    public function goodinfo(){
        $id=input('id');
        $good=$this->GoodsModel->getGoodInfo($id);

        $this->assign('good',$good);

       // dump($good);
        return $this->fetch();

    }

    /*
     * 确认订单
     */
    public function confirmOrder(){
       if(\request()->isAjax()) {
           //消耗的总积分量
           if (empty(cmf_get_current_user_id())){
               $this->error();
           }
           $data=input('');
           $good=$this->GoodsModel->getGoodInfo($data['id']);
           $sum=$data['num']*$good['fee'];
           $User=new UserModel();
           $user=$User->getMyself(cmf_get_current_user_id())->toArray();
          if ($user['score']<$sum){
              $this->error('积分不足,下单失败!');
          }else{
              /*
             * 开启事务 处理订单
             */
                $insert['order_sn']='JYH-'.uniqid();
                $insert['num']=$data['num'];
                $insert['good_id']=$good['id'];
                $insert['fee']=$sum;
                $insert['pay_type']=1;
                $insert['delivery_tel']=$data['tel'];
                $insert['delivery_name']=$data['name'];
                $insert['delivery_address']=$data['address'];
                $insert['remark']=$data['content'];
                $insert['user_id']=cmf_get_current_user_id();
                $insert['add_time']=time();
              Db::startTrans();
              try{
                  $log_insert['user_id']=cmf_get_current_user_id();
                  $log_insert['create_time']=time();
                  $log_insert['score']=-$sum;
                  $log_insert['action']='积分消费';
                  Db::name('order')->insert($insert);
                  Db::name('user')->where('id',cmf_get_current_user_id())->setDec('score',$sum);
                  \db('user_score_log')->insert($log_insert);
                  // 提交事务
                  Db::commit();
              } catch (\Exception $e) {
                  // 回滚事务
                  Db::rollback();
                $this->error();
              }

               $this->success('下单成功!');


          }
       }else{
           /*jsSDK加入*/
           $config=config('wechat_config');
           $Wechat=new Application($config);
           $js = $Wechat->js;
           $api[]='openAddress';
           $sdk=$js->config($api, false);

           $good_id=input('id');
           $num=input('num');
           $good=$this->GoodsModel->getGoodInfo($good_id);
           $good['num']=$num;
           $total_fee=$good['fee']*$num;
            /*
             * 获取上次的地址
             *
             */
            

           $this->assign('good',$good);
           $this->assign('sdk',$sdk);
           $this->assign('total_fee',$total_fee);
           return $this->fetch();
       }
    }







}