ToolController.php 7.0 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use think\log;
use think\config;
use think\Db;

class ToolController extends HomeBaseController
{
    public function get_relax_time()
    {
        $day   = $this->request->param('day');
        $uid   = $this->request->param('uid');
        if(!$day){
            $day = date('Y-m-d');
        }

        $res = Db::name('yuyue')->where(['yuyue_day'=>$day,'teach_id'=>$uid])->select()->toArray();

        $all_arr = [];
        for($t = 6;$t<=18;$t++){
            $all_arr[]=$t;
        }

        $arr = [];
        for($i = 0;$i<count($res);$i++){
            $begin = $res[$i]["yuyue_begin"];
            $end = $res[$i]["yuyue_end"];
            for($j= $begin;$j<$end;$j++){
                $arr[]=$j;
            }
        }
        $ok_arr = [];
        for($q = 0;$q< count($all_arr);$q++){
            if(in_array($all_arr[$q],$arr)){
                $ok_arr[] = "no";
            }else{
                $ok_arr[] = "yes";
            }
        }

        if ($ok_arr){
            return json(['code'=>'1','msg'=>'获取成功','data'=>json_encode($ok_arr,true)]);
        }else{
            return json(['code'=>'0','msg'=>'获取失败']);
        }
    }

    public function get_sms_code()
    {
        $mobile   = $this->request->param('mobile');
        if(!$mobile){
            return json(['code'=>'0','msg'=>'请输入手机号码']);
        }
        $code = rand(111111,999999);
        $msg = $code.'(悠乐驾验证码),30分钟内有效,请妥善保管您的验证码,以免账号被盗用。【悠乐驾】';

        $data = array(
            'content'  => $msg,
            'mobile'   => $mobile,
            'productid' => '676767',
            'xh'      => ''
        );
        $return = send_sms($data);
        $ret=explode(',',$return);
        if ($ret[0]=='1'){
            cmf_verification_code_log($mobile, $code);
            return json(['code'=>'1','msg'=>'验证码已经发送成功']);
        }else{

            $info=date('Y-m-d H:i:s')." 短信发送失败 error:".$return;
            Log::write($info,'ERROR');
            return json(['code'=>'0','msg'=>'短信发送失败']);
        }
    }

    public function get_sms_code_p($mobile,$msg)
    {

        if(!$mobile) {
            return json(['code' => '0', 'msg' => '请输入手机号码']);
        }

        $data = array(
            'content'  => $msg,
            'mobile'   => $mobile,
            'productid' => '887361',
            'xh'      => ''
        );
        send_sms($data);
    }


    public function get_acctok(){
        require_once VENDOR_PATH . 'wxjssdk/jssdk.php';
        //微信分享接口调用
        $jssdk = new \JSSDK(config::get("WECHAT_APPID"), config::get("WECHAT_APPSECRET"));
        $signPackage            = $jssdk->GetSignPackage();
        $AccessToken            = $jssdk->getAccessToken();
        return $AccessToken;
    }

    /**
     * 发送post请求
     * @param string $url
     * @param string $param
     * @return ArrayObject|mixed
     */
    function request_post($url = '', $param = '')
    {
        if (empty($url) || empty($param)) {
            return false;
        }
        $postUrl = $url;
        $curlPost = $param;
        $ch = curl_init();                                                  //初始化curl
        curl_setopt($ch, CURLOPT_URL, $postUrl);                      //抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);                    //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);            //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);                      //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);                                             //运行curl
        curl_close($ch);
        return $data;
    }

    /**
     * 发送get请求
     * @param string $url
     * @return bool|mixed
     */
    function request_get($url = '')
    {
        if (empty($url)) {
            return false;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }




// 发送模板消息
    public function wxtemsg_send($param=null)
    {
        $access_token = $this->get_acctok();
        $template = array(
            'touser'        => $param['touser'],
            'template_id'   => $param['template_id'],
            'url'           => $param['url'],
            'topcolor'      => $param['topcolor'],
            'data'          => $param['data']
        );

        $json_template      = json_encode($template);
        $post_url           = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
        $dataRes            = $this->request_post($post_url, urldecode($json_template));

        // 记录请求日志
        file_put_contents('./logs/send_template.log', 'time='.date("Y-m-d H:i:s").'req='.json_encode($template, JSON_UNESCAPED_UNICODE).'res='.$dataRes."\n", FILE_APPEND);

        $res= json_decode($dataRes);
//        if ($res->errcode == 42001 || $res->errcode == 40001) {
//            $this->appid    = C('WECHAT_APPID');
//            $this->secrect  = C('WECHAT_APPSECRET');
//            $jssdk = new JSSDK($this->appid, $this->secrect);
//            $this->accessToken = $jssdk->getAccessToken(true);
//        }
        return $res;
    }

    public function aa(){
        // 模板参数
        $param = [
            'touser'        => 'oEjC41YHyrNMyfnP1C5_vjh-C7bc',
            'topcolor'      => '#7B68EE',
            'url'           => 'http://dm161xueche.qnbug.cn',
            'template_id'   => 'kmRG7k7OOLd3m5a3sTvvMknbIySIifBtptFPxs3lgUo',
            'data'          => array(
                'first'    => array('value' => '学车预约成功'),
                'keyword1' => array('value' => date('Y-m-d'),'color'=>'#333366'),
                'keyword2' => array('value' =>  '科目二学车','color'=>'#0099FF'),
                'keyword3' => array('value' =>  '小虎哥', 'color'=>'#0099FF'),
                'keyword4' => array('value' =>  '025-66046652'),
                'keyword5' => array('value' =>  'qnbug', 'color'=>'#0099FF')
            )
        ];

        $res = $this->wxtemsg_send($param);
        dump($res);
    }




























}