Merchant.php 4.8 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/5
 * Time: 15:47
 */

namespace app\home\controller;


use app\common\controller\WechatBase;
use EasyWeChat\Foundation\Application;
use think\Db;

class Merchant extends WechatBase
{
    protected $user_id;
    function _initialize()
    {
        parent::_initialize();
        //判断是否授权
        $user_id = get_current_user_id();
        if(empty($user_id)){
            $this->redirect('user/authorization_view');
        }
        $this->user_id = $user_id;
    }
    public function index(){
        $data = Db::name('page')->where(['id'=>1])->find();
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function enter_view(){
        $domain_name = $this->request->domain();//域名
        $data = Db::name('merchant_audit')->where(['user_id'=>$this->user_id])->find();
        if(!empty($data)){
            if($data['status'] == 1 || $data['status'] == 2){
                //审核中或者审核通过提示
                $this->redirect('audit_status');
            }
            $business_images = explode(',',$data['business_images']);
//            foreach($business_images as $key => $b_i){
//                $business_images[$key] = $domain_name.$b_i;
//            }
            $data['business_images'] = $business_images;

            if(!empty($data['other_images'])){
                $other_images = explode(',',$data['other_images']);
//                foreach($other_images as $key => $o_i){
//                    $other_images[$key] = $domain_name.$o_i;
//                }
                $data['other_images'] = $other_images;
            }
        }
        $this->assign('user_id',$this->user_id);
        $this->assign('data',$data);
        $this->assign('title','商家入驻');
        $options = [
            'app_id' => config('wechat.app_id'),
            'secret' => config('wechat.secret'),
        ];
        $app = new Application($options);
        $js = $app->js;
        $jssdk = $js->config(['chooseImage', 'uploadImage', 'previewImage'], $debug = false, $beta = false, $json = true);
        $this->assign('jssdk',$jssdk);
        return $this->fetch();
    }
    public function audit_status(){
        $data = Db::name('merchant_audit')->where(['user_id'=>$this->user_id])->find();
        if(empty($data)){
            $this->redirect('enter_view');
        }
        $title = '';
        if($data['status'] == 3){
            $this->redirect('enter_view');
        }else if($data['status'] == 2){
            $title = "审核通过";
        }else if($data['status'] == 1){
            $title = "审核中";
        }
        $this->assign('title',$title);
        $this->assign('data',$data);
        return $this->fetch();
    }
    /**
     * 提交申请
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function merchant_enter_submit(){
        $param = $this->request->param();
        $validate = new \think\Validate([
            'user_id' => 'require',
            'merchant_title' => 'require',
            'merchant_name' => 'require',
            'merchant_mobile' => 'require',
            'business_images' => 'require',
        ]);
        $validate->message([
            'user_id' => 'user_id参数错误',
            'merchant_title' => 'merchant_title参数错误',
            'merchant_name' => 'merchant_name参数错误',
            'merchant_mobile' => 'merchant_mobile参数错误',
            'business_images' => 'business_images参数错误',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        if(!empty($param['other_images'])){
            $param['other_images'] = implode(',',$param['other_images']);
        }else{
            $param['other_images'] = null;
        }
        $param['business_images'] = implode(',',$param['business_images']);
        $data = Db::name('merchant_audit')->where(['user_id'=>$this->user_id])->find();
        if(!empty($data)){
            if($data['expiration_time'] > time() && $data['status'] == 3){
                $this->error("请于$data[expiration_time]后再来提交");
            }else if($data['status'] == 2){
                $this->error("您已经是商家了");
            }else if($data['status'] == 1){
                $this->error('正在审核中');
            }else{
                $param['status'] = 1;
                Db::name('merchant_audit')->where(['user_id'=>$param['user_id']])->update($param);
            }
        }else{
            $param['createtime'] = time();
            Db::name('merchant_audit')->insert($param);
        }
        $this->success('success');
    }
}