UserController.php 4.7 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\user\controller;

use cmf\controller\WeChatBaseController;
use think\Validate;
use cmf\controller\HomeBaseController;
use app\user\model\UserModel;
use app\admin\service;
use think\Db;
use EasyWeChat\Foundation\Application;

class UserController extends WeChatBaseController
{

    public function _initialize()
    {
        parent::_initialize();
        $this->checkWeChatUserLogin();
    }

    /**
     * 登录
     */
    public function myLight()
    {
        $re=$this->checkLogin();
        $data=Db::name('light_order')
            ->alias('lo')
            ->join('light l','lo.light_id=l.id')
            ->join('temple t','l.temple_id=t.id')
            ->where(['lo.users_id'=>$re['id'],'lo.status'=>1,'lo.end_time'=>['>',time()]])
            ->field('t.name as tname,t.thumbnail,lo.*')
            ->select();
        $this->assign('list',$data);
        return $this->fetch(":myLight");
    }

    public function details(){
        $re=$this->checkLogin();
        $param=$this->request->param();
        $data=Db::name('light_order')
            ->alias('lo')
            ->join('light l','lo.light_id=l.id')
            ->join('temple_area ta','l.area_id=ta.id')
            ->join('temple t','l.temple_id=t.id')
            ->where('lo.id',$param['id'])
            ->field('t.name as tname,ta.name as aname,l.row,l.column,l.term,lo.*')
            ->find();
        $data['user_name']=$re['user_name'];
        $this->assign('list',$data);
        return $this->fetch(':details');
    }


    public function edit(){
        $this->checkLogin();

        $options = [
            'app_id' => config('wechat_config.app_id'),
            'secret' => config('wechat_config.secret'),
            'payment' => config('wechat_config.payment'),
        ];
        $app = new Application($options);
        $js = $app->js;
        $jss = $js->config(['chooseImage', 'uploadImage', 'previewImage'], $debug = false, $beta = false, $json = true);
        $this->assign('js', $jss);

        $param=$this->request->param();
        $data=Db::name('light_order')->where('id',$param['id'])->find();
        $this->assign('list',$data);
        return $this->fetch(':edit');
    }

    public function editPost(){
        $user=$this->checkLogin();
        $param = $this->request->param();
        if ($param['media']!="") {
            $param['avatar'] = $this->upload_wx_pic_mul($param['media']);
        }
        unset($param['media']);
        $data=Db::name('light_order')->where('id',$param['order_id'])->find();
        $re=Db::name('light_order')->where('id',$param['order_id'])->update(['name'=>$param['name'],'content'=>$param['content'],'avatar'=>$param['avatar']]);

        $lightService= new service\LightService();
        $lightService->lightChange($data['light_id'],'01','01',$param['name']);
        if ($re){
            return 1;
        }else{
            return 2;
        }
    }
    public function upload_wx_pic_mul()
    {
        require_once VENDOR_PATH . "jssdk/jssdk.php";
        $jssdk = new \JSSDK(Config('WX_APPID'), Config('WX_APP_SECRET'));
        $access_token = $jssdk->getAccessToken();
        $img_str = $this->request->param('media');
        $file = $this->getmedia($access_token,$img_str, date('Ymd'));
        return $file;
    }
    public function getmedia($access_token, $media_id, $foldername)
    {
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" . $access_token . "&media_id=" . $media_id;
        if (!file_exists("./upload/" . $foldername)) {
            mkdir("./upload/" . $foldername, 0777, true);
        }
        $file_name = date('YmdHis') . rand(1000, 9999) . '.jpg';
        $targetName = './upload/' . $foldername . '/' . $file_name;
        $saveName = $foldername . '/' . $file_name;
        $ch = curl_init($url); // 初始化
        $fp = fopen($targetName, 'wb'); // 打开写入
        curl_setopt($ch, CURLOPT_FILE, $fp); // 设置输出文件的位置,值是一个资源类型
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_exec($ch);
        curl_close($ch);
        fclose($fp);
        return $saveName;
    }
}