微信校园行活动流程

 

自动问答处理脚本

测试

public function responseMsg() {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
#tmp
//        if (empty($postStr)){
//            echo "没有post任何消息";
//            exit;
//        }

        //extract post data
        $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
        $postParams = $this->objectToArray($postObj);
#tmp begin cmdline 传入参数
         $postParams = array(
             ‘FromUserName‘    => ‘lsm-user‘,
             ‘ToUserName‘    => ‘lsm-dev‘,
             ‘Content‘        => $GLOBALS[‘argv‘][1],//玫瑰问答
             ‘MsgType‘        => isset($GLOBALS[‘argv‘][2]) ? $GLOBALS[‘argv‘][2] : ‘text‘,
         );
        if($postParams[‘MsgType‘] == ‘image‘) {
            $postParams[‘PicUrl‘] = $postParams[‘Content‘];
            unset($postParams[‘Content‘]);
        }
#tmp end

 

// 校园行 活动开始 

$replyInfo = array();
$objWeixinUnivProgress = new WeixinUniversityTourProgress($this->openid);
$univMsg = $objWeixinUnivProgress->doUniv($postParams);    
if($univMsg) {
$replyInfo = $univMsg;
}    

 


// 校园行活动结束

if(!$replyInfo) {

......

}


微信校园行活动流程控制文件

<?php

/**
 * 微信校园行活动流程控制文件
 * @author chenbingbing  2014-10-9
 */
class WeixinUniversityTourProgress {

    private $openid; // 用户标识
    private $univid; // 学院标识
    private $univConfig; // 学院配置文件
    private $univGoing; // 活动是否有效
    private $acConfig; // 所有活动配置信息
    private $stepKey; // 匹配关键字在配置文件中的键值
    private $objTMMemcached;
    private $objUnivTourFront;
    private $imgMaxSize;

    const ACNOTGOING = ‘抱歉,此活动目前没有进行‘;
    const UPLOADREMOTEPICFAIL = ‘无法保存上传图片到服务器,请重新上传!‘;
    const UPLOADPATHUNEXISTS = ‘上传目录不存在‘;
    const UPLOADPATHCANNOTWRITE = ‘上传目录没有写权限‘;
    const UPLOADFILETOOBIG = ‘上传文件太大‘;
    const MKDIRFAILED = ‘创建目录失败‘;

    public function __construct($openid) {
        $this->univGoing = 0;
        $this->acConfig = require_once ROOT_PATH . DIRECTORY_SEPARATOR . ‘include‘ . DIRECTORY_SEPARATOR . ‘universityTour‘ . DIRECTORY_SEPARATOR . ‘activity.inc.php‘;
        $this->objTMMemcached = new TMMemcached();
        $this->objUnivTourFront = new WeixinUniversityTourFront();
        $this->openid = $openid;
        $this->imgMaxSize = 1500000; // 1.5M
    }

    public function doUniv($params) {
        $keyword = trim($params[‘Content‘]);
        // 特殊项处理
        if ($params[‘MsgType‘] == ‘text‘ && $specialReturn = $this->specialStepDeal($keyword)) {
            return $specialReturn;
        }
        // text 不是 a,b,c,d,e,f, y,n
        if ($params[‘MsgType‘] == ‘text‘ && !$this->isChoices($keyword)) {
            if ($return = $this->matchActivity($keyword)) {
                return $return;
            }
            if ($return = $this->receiveTextAnswer($keyword)) {
                return $return;
            }
        } elseif ($params[‘MsgType‘] == ‘text‘ && $this->isChoices($keyword)) {
            if ($this->isChoices($keyword, 2) && $return = $this->receiveConfirmInfo($keyword)) {
                return $return;
            }
            //A,B,C,D
            if ($this->isChoices($keyword, 1) && $return = $this->receiveRadioInfo($keyword)) {
                return $return;
            }
        } elseif ($params[‘MsgType‘] == ‘image‘) {
            //PicUrl
            $picUrl = $params[‘PicUrl‘];
            if ($return = $this->receiveImageInfo($picUrl)) {
                return $return;
            }
        }
    }

    /**
     * 接收信息:图片
     * @param type $picUrl 用户图片地址
     */
    public function receiveImageInfo($picUrl) {
        $returnMsg = ‘‘;
        $memberInfo = $this->getMemberStepInfo();
        if (empty($memberInfo)) {
            return false;
        }
        $msgs = $this->univConfig[$this->stepKey];
        if ($msgs[‘type‘] != ‘image‘) {
            return false;
        }
        // 存储用户上传图片到本地服务器
        $saveMsg = $this->saveRemotePic($picUrl);
        $status = $saveMsg[‘ok‘];
        $msg = $saveMsg[‘msg‘];

        if (!$status) {
            return $this->reWeixinTextMsg($msg);
        } else {
            $picUrl = $msg;
        }

        if (is_array($msgs[‘imgMsgs‘])) {
            $nowMsgKey = 0;
            if (!isset($memberInfo[‘data‘])) {
                $nowMsgKey = 0;
            } else {
                if (!is_array($memberInfo[‘data‘])) {
                    $memberInfo[‘data‘] = array();
                }
                $nowMsgKey = count($memberInfo[‘data‘]);
            }

            $stepData = $nowMsgKey == 0 ? array() : $memberInfo[‘data‘];
            $returnMsg = $msgs[‘imgMsgs‘][$nowMsgKey];

            $stepData[$nowMsgKey] = $picUrl;
            $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey, ‘data‘ => $stepData));
            // 图片上传完了
            if ($nowMsgKey >= count($msgs[‘imgMsgs‘]) - 1) {
                // 存储数据
                $this->saveRowData($stepData);
                if ($msgs[‘completeNext‘]) {
                    return $this->activityStep(0, 1, $returnMsg);
                }
            }
        }
        // 单张数组形式设置
        return $this->reWeixinTextMsg($returnMsg);
    }

    /**
     * 接收信息:a,b,c,d
     * @param type $keyword
     * @return boolean
     */
    public function receiveRadioInfo($keyword) {
        $returnMsg = ‘‘;
        $memberInfo = $this->getMemberStepInfo();
        if (empty($memberInfo)) {
            return false;
        }
        $keyword = strtolower($keyword);

        $msgs = $this->univConfig[$this->stepKey];
        // 按步骤需要回答N个问题
        if (is_array($msgs[‘contents‘])) {
            $nowQuestionKey = 0;
            if (!isset($memberInfo[‘data‘])) {
                $nowQuestionKey = 0;
            } else {
                $nowQuestionKey = count($memberInfo[‘data‘]);
            }

            $nextQuesKey = $nowQuestionKey + 1;
            if ($nextQuesKey >= count($msgs[‘contents‘])) {
                $nextQuesKey = ‘‘;
            }

            $stepData = $nowQuestionKey == 0 ? array() : $memberInfo[‘data‘];
            if ($msgs[‘contents‘][$nowQuestionKey][‘preg‘] && !preg_match($msgs[‘contents‘][$nowQuestionKey][‘preg‘], $keyword)) {
                $returnMsg = $msgs[‘pregFail‘]; //选择其他选项给出的提示信息
            } else {
                if (count($memberInfo[‘data‘]) < count($msgs[‘contents‘])) {
                    $stepData[$nowQuestionKey] = $keyword;
                }
                $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey, ‘data‘ => $stepData));
                // 下一个问题
                if ($nextQuesKey) {
                    $returnMsg = $msgs[‘contents‘][$nextQuesKey][‘response‘];
                } elseif(isset($msgs[‘confirm‘]) && $msgs[‘confirm‘]) {
                    $returnMsg = str_replace(‘{msg}‘, strtoupper(implode(‘,‘, $stepData)), $msgs[‘confirm‘]);
                } else {
                    // 完成多个单选,存储结果
                    // 存储数据
                    $this->saveRowData($stepData);
                    $preFixInfo = isset($msgs[‘completeResponse‘]) ? $msgs[‘completeResponse‘] : ‘‘;
                    $content = $this->activityStep(0, 1, $preFixInfo);
                    $returnMsg = $content[‘content‘];
                }
            }
        } else {

            if ($msgs[‘preg‘] && !preg_match($msgs[‘preg‘], $keyword)) {
                if (isset($msgs[‘pregFail‘])) {
                    $returnMsg = $msgs[‘pregFail‘];
                } elseif (isset($msgs[‘outOfAway‘])) {
                    $returnMsg = $msgs[‘outOfAway‘];
                }

                // right Or wrong, both save to memcache
                $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey, ‘outOfAway‘ => 1));
            } else {
                if(isset($msgs[‘confirm‘]) && $msgs[‘confirm‘]) {
                    $returnMsg = str_replace(‘{msg}‘, strtoupper($keyword), $msgs[‘confirm‘]);                    
                    // 图片类型的不存储 radio数据
                    if (!in_array($msgs[‘type‘], array(‘image‘))) {
                        $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey, ‘data‘ => $keyword));
                    }
                } else {
                    $this->saveRowData($keyword);
                    $preFixInfo = isset($msgs[‘completeResponse‘]) ? $msgs[‘completeResponse‘] : ‘‘;
                    $content = $this->activityStep(0, 1, $preFixInfo);
                    $returnMsg = $content[‘content‘];
                }
            }
        }

        return $this->reWeixinTextMsg($returnMsg);
    }

    /**
     * 接收信息Y,N
     * @param type $keyword
     * @return boolean
     */
    public function receiveConfirmInfo($keyword) {
        $memberInfo = $this->getMemberStepInfo();
        if (empty($memberInfo)) {
            return false;
        }
        $msgs = $this->univConfig[$this->stepKey];
        $keyword = strtolower($keyword);

        // 确认数据是否正确 Y N
        if ($keyword == ‘y‘) {
            // 存储数据
            $this->saveRowData($memberInfo[‘data‘]);
            return $this->activityStep(1);
        } else {
            $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $memberInfo[‘stepKey‘]));
            if ($msgs[‘confirmN‘] == ‘repeat‘) {
                return $this->activityStep();
            } else {
                if ($msgs[‘confirmN‘] && isset($this->univConfig[$msgs[‘confirmN‘]])) {
                    $this->stepKey = $msgs[‘confirmN‘];
                    $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey));
                    return $this->activityStep();
                }
                return $this->reWeixinTextMsg($msgs[‘confirmN‘]);
            }
        }
    }

    /**
     * 入库
     */
    public function saveRowData($memberData, $extinfo = array()) {
        if (!$memberData) {
            return false;
        }
        $setarr = array(
            ‘openid‘ => $this->openid,
            ‘univid‘ => $this->univid,
        );

        // 如果有真实字段,则存储真是字段
        $msgs = $this->univConfig[$this->stepKey];
        if (isset($msgs[‘realKey‘]) && isset($this->univConfig[$msgs[‘realKey‘]])) {
            $setarr[$msgs[‘realKey‘]] = $memberData;
        } else {
            $setarr[$this->stepKey] = $memberData;
        }

        if (!empty($extinfo)) {
            $setarr = array_merge($setarr, $extinfo);
        }
        $oldData = $this->objUnivTourFront->get($this->openid, $this->univid);
        if (empty($oldData)) {
            $this->objUnivTourFront->add($setarr);
        } else {
            $condition = array(‘id‘ => $oldData[‘id‘]);
            $this->objUnivTourFront->modify($setarr, $condition);
        }
    }

    /**
     * 接收文本信息,不包含 a,b,c,d Y,N
     * @param type $keyword
     * @return boolean
     */
    public function receiveTextAnswer($keyword) {
        $returnMsg = ‘‘;
        $memberInfo = $this->getMemberStepInfo();
        if (empty($memberInfo)) {
            return false;
        }
        $msgs = $this->univConfig[$this->stepKey];
        // 输入e,f等其他选项
        if ($msgs[‘type‘] == ‘radio‘) {
            if (strlen($keyword) > 1) {
                return false;
            } else {
                return $this->reWeixinTextMsg($msgs[‘pregFail‘]);
            }
        }
        if (!$memberInfo[‘data‘]) {
            if ($msgs[‘preg‘] && !preg_match($msgs[‘preg‘], $keyword)) {
                $returnMsg = $msgs[‘pregFail‘];
            } else {
                if (isset($msgs[‘confirm‘]) && $msgs[‘confirm‘]) {
                    $returnMsg = str_replace(‘{msg}‘, $keyword, $msgs[‘confirm‘]);
                    // ‘data‘ => $keyword 存储用户关键字,如果确定,则直接更新相应数据了
                    $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey, ‘data‘ => $keyword));
                } else {
                    // 用户输入的信息,不需要确认,则直接存储
                    $this->saveRowData($keyword);
                    $preFixInfo = isset($msgs[‘completeResponse‘]) ? $msgs[‘completeResponse‘] : ‘‘;
                    return $this->activityStep(0, 1, $preFixInfo); // 完成,尝试自动执行下一步
                }
            }
            return $this->reWeixinTextMsg($returnMsg);
        }
        return false;
    }

    /**
     * 根据关键词匹配所属活动
     * @param type $keyword
     * @return boolean
     */
    public function matchActivity($keyword) {

        foreach ($this->acConfig as $value) {

            if ($value[‘keyword‘] == $keyword) {
                $this->univid = $value[‘univid‘];
                $this->stepKey = $value[‘stepKey‘];

                $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey));

                $this->getUnivConfig(); // 获取活动配置信息

                if (empty($this->univConfig)) {
                    return false;
                }
                $this->checkAcGoing(); // 检查是否进行中

                if ($this->univGoing == 0) {
                    return $this->reWeixinTextMsg(self::ACNOTGOING);
                }
                // 开始活动段
                return $this->activityStep();
                break;
            }
        }
        return false;
    }

    /**
     * 进行活动段
     * 返回某一步需返回的信息 默认返回此活动段的第一响应信息
     * @param type $confirmY 是否已经确认提交的信息
     * @param type $complete  此活动段是否已经完成
     * @param type $prefixInfo  前缀信息  目前仅$complete =1时用到
     * @return type
     */
    public function activityStep($confirmY = 0, $complete = 0, $prefixInfo = ‘‘) {
        if (!$this->stepKey) {
            $this->getMemberStepInfo();
        }
        $msgs = $this->univConfig[$this->stepKey];

        // 需求字段数据检查
        if (isset($msgs[‘need‘]) && $msgs[‘need‘]) {
            $oldData = $this->objUnivTourFront->get($this->openid, $this->univid);
            if (!$oldData || !isset($oldData[$msgs[‘need‘]])) {
                $this->stepKey = $msgs[‘need‘];
                $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey));
                return $this->reWeixinTextMsg($msgs[‘needNotice‘]);
            }
        }

        if (isset($msgs[‘next‘]) && $msgs[‘next‘]) {
            $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $msgs[‘next‘]));
        }

        $returnMsg = ‘‘;

        if ($confirmY) {

            // 结束整个活动流程
            if (isset($msgs[‘end‘]) && $msgs[‘end‘]) {
                $this->delMemberMemStepInfo();
            }

            // 提示信息有指定的某步给出
            if ($msgs[‘confirmY‘] && isset($this->univConfig[$msgs[‘confirmY‘]])) {
                $this->stepKey = $msgs[‘confirmY‘];
                //, ‘data‘ => ‘y‘  仅第一步信息包含Y, N时存储
                if ($msgs[‘special‘] != 2) {
                    $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey, ‘data‘ => ‘y‘));
                } else {
                    $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey));
                }
                $return = ‘‘;
                if ($msgs[‘special‘]) {
                    $return = $this->specialStepDeal(‘y‘);
                }
                if (!$return) {
                    $return = $this->activityStep(1);
                }
                return $return;
            }

            $returnMsg .= $msgs[‘confirmY‘] ? $msgs[‘confirmY‘] : ‘‘;
            if (isset($msgs[‘confirmAutoNext‘]) && $msgs[‘confirmAutoNext‘]) {
                $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $msgs[‘confirmAutoNext‘]));
                $this->stepKey = $msgs[‘confirmAutoNext‘];
                $newMsgs = $this->univConfig[$this->stepKey];

                if ($newMsgs[‘response‘]) {
                    $returnMsg .= ($returnMsg ? "\n" : ‘‘) . $newMsgs[‘response‘];
                } elseif ($newMsgs[‘contents‘]) {
                    $returnMsg .= ($returnMsg ? "\n" : ‘‘) . $newMsgs[‘contents‘][0][‘response‘];
                }

                if ($newMsgs[‘autoNext‘]) {
                    $this->stepKey = $newMsgs[‘autoNext‘];
                    $SecnewMsgs = $this->univConfig[$this->stepKey];
                    $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey));
                    if ($SecnewMsgs[‘response‘]) {
                        $returnMsg .= "\n" . $SecnewMsgs[‘response‘];
                    } elseif ($SecnewMsgs[‘contents‘]) {
                        $returnMsg .= "\n" . $SecnewMsgs[‘contents‘][0][‘response‘];
                    }
                }
            }
        } elseif ($complete) {
            $returnMsg .= $prefixInfo;

// 结束整个活动流程
if (isset($msgs[‘end‘]) && $msgs[‘end‘]) {
$this->delMemberMemStepInfo();
}

if ($msgs[‘completeNext‘]) {
                $this->stepKey = $msgs[‘completeNext‘];
                $newMsgs = $this->univConfig[$this->stepKey];
                $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey));
                if ($newMsgs[‘response‘]) {
                    $returnMsg .= "\n" . $newMsgs[‘response‘];
                } elseif ($newMsgs[‘contents‘]) {
                    $returnMsg .= "\n" . $newMsgs[‘contents‘][0][‘response‘];
                }
            }
        } else {
            if ($msgs[‘response‘]) {
                $returnMsg = $msgs[‘response‘];
            } elseif ($msgs[‘contents‘]) {
                $returnMsg = $msgs[‘contents‘][0][‘response‘];
            }
            // 自动拼接下一个返回信息 "\n"连接
            if (isset($msgs[‘autoNext‘]) && $msgs[‘autoNext‘]) {
                $this->stepKey = $msgs[‘autoNext‘];
                $newMsgs = $this->univConfig[$this->stepKey];
                $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey));
                if (isset($newMsgs[‘response‘]) && $newMsgs[‘response‘]) {
                    $returnMsg .= "\n" . $newMsgs[‘response‘];
                } elseif ($newMsgs[‘contents‘]) {
                    $returnMsg .= "\n" . $newMsgs[‘contents‘][0][‘response‘];
                }
            }
        }
        return $this->reWeixinTextMsg($returnMsg);
    }

    /**
     * 检查活动是否有效
     */
    public function checkAcGoing() {
        if (!empty($this->univConfig) && $this->univConfig[‘startTime‘] && $this->univConfig[‘endTime‘]) {
            $nowTimestamp = time();
            if ($this->univConfig[‘startTime‘] < $nowTimestamp && $this->univConfig[‘endTime‘] > $nowTimestamp) {
                $this->univGoing = 1;
            }
        }
    }

    /**
     * 获取学院配置信息
     * @param type $force
     */
    public function getUnivConfig($force = 0) {
        $data = array();
        $key = ‘weixin_univ_tour_ac_config_‘ . $this->univid;
        $memData = $this->objTMMemcached->get($key);
        if ($this->univid && (empty($memData) || $force)) {
            $configFile = ROOT_PATH . DIRECTORY_SEPARATOR . ‘include‘ . DIRECTORY_SEPARATOR . ‘universityTour‘ . DIRECTORY_SEPARATOR . $this->univid . ‘.inc.php‘;
            if (file_exists($configFile)) {
                $memData = require $configFile;
                $this->objTMMemcached->set($key, $memData, 86400);
            }
        }
        $data = !empty($memData) ? $memData : $data;
        $this->univConfig = $data;
    }

    /**
     * 把活动进度信息存到用户缓存中
     * @param type $data
     * @return boolean
     */
    public function setMemberMemStepInfo($data) {
        $key = $this->getMemberMemKey($this->openid);
        $this->objTMMemcached->set($key, $data, 86400 * 3);
        return true;
    }

    /**
     * 取出用户的活动进度信息
     * @return type
     */
    public function getMemberMemStepInfo() {
        $key = $this->getMemberMemKey($this->openid);
        return $this->objTMMemcached->get($key);
    }

    /**
     * 
     */
    public function delMemberMemStepInfo() {
        $key = $this->getMemberMemKey($this->openid);
        $this->objTMMemcached->delete($key);
    }

    /**
     * 获取用户mem Key
     * @param type $openid
     * @return type
     */
    public function getMemberMemKey($openid) {
        return ‘weixin_univ_tour_‘ . $openid;
    }

    /**
     * 微信返回数据-text
     * @param type $content
     * @return boolean
     */
    public function reWeixinTextMsg($content) {
        if (!$content) {
            return false;
        }
        return array(‘type‘ => ‘text‘, ‘content‘ => $content);
    }

    /**
     * 判断是否是指定选择关键字
     * @param type $keyword
     * @param type $isYn 0:check all, 1: check abcd, 2: check: yn
     * @return type
     */
    public function isChoices($keyword, $isYn = 0) {
        $keyword = strtolower($keyword);
        if ($isYn == 0) {
            return in_array($keyword, array(‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘y‘, ‘n‘));
        }
        if ($isYn == 1) {
            return in_array($keyword, array(‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘y‘, ‘n‘));
        }
        if ($isYn == 2) {
            return in_array($keyword, array(‘y‘, ‘n‘));
        }
    }

    /**
     * 获取用户进度信息
     * @return boolean
     */
    public function getMemberStepInfo() {
        $memberInfo = $this->getMemberMemStepInfo();
        if (empty($memberInfo)) {
            return false;
        }
        $this->univid = $memberInfo[‘univid‘];
        $this->stepKey = $memberInfo[‘stepKey‘];
        $this->getUnivConfig();
        if (empty($this->univConfig)) {
            return false;
        }
        return $memberInfo;
    }

    /**
     * 获取兑换码
     */
    public function getExchangeCode() {
        $oldData = $this->objUnivTourFront->get($this->openid, $this->univid);
//        if ($oldData[‘exchange_code‘]) {
//            return $oldData[‘exchange_code‘];
//        }
        $num = str_pad($oldData[‘id‘], 3, ‘0‘, STR_PAD_LEFT);
        return ‘kimiss‘ . $num;
    }

    /**
     * 特殊步骤处理
     * @param type $keyword
     * @return boolean
     */
    public function specialStepDeal($keyword) {
        $memberInfo = $this->getMemberStepInfo();
        if (empty($memberInfo)) {
            return false;
        }
        $msgs = $this->univConfig[$this->stepKey];

        if (!isset($msgs[‘special‘]) || !$msgs[‘special‘]) {
            return false;
        }

        $keyword = strtolower($keyword);
        $memberInfo = $this->getMemberStepInfo();
        // radio item outOfAway
        if ($msgs[‘special‘] == 1) {
            $returnInfo = $this->specialStepDeal_1($keyword, $memberInfo, $msgs);
            if ($returnInfo) {
                return $returnInfo;
            }
        } elseif ($msgs[‘special‘] == 2) {
            $returnInfo = $this->specialStepDeal_2($keyword, $memberInfo, $msgs);
            if ($returnInfo) {
                return $returnInfo;
            }
        }
    }

    /**
     * 选择题,选择其他选项特殊处理
     * @param type $keyword
     * @param type $memberInfo
     * @param type $msgs
     * @return type
     */
    public function specialStepDeal_1($keyword, $memberInfo, $msgs) {
        if (!$memberInfo[‘data‘] && !$memberInfo[‘outOfAway‘]) {
            if ($msgs[‘preg‘] && !preg_match($msgs[‘preg‘], $keyword)) {
                if (isset($msgs[‘pregFail‘])) {
                    $returnMsg = $msgs[‘pregFail‘];
                } elseif (isset($msgs[‘outOfAway‘])) {
                    $returnMsg = $msgs[‘outOfAway‘];
                }
                // right Or wrong, both save to memcache
                $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey, ‘outOfAway‘ => 1));
                return $this->reWeixinTextMsg($returnMsg);
            }
        }
        if ($memberInfo[‘outOfAway‘] == 1) {
            if ($keyword == ‘y‘) {
                if ($msgs[‘outOfAwayY‘] && isset($this->univConfig[$msgs[‘outOfAwayY‘]])) {
                    // 存储此步选择
                    $this->saveRowData(‘y‘);
                    $this->stepKey = $msgs[‘outOfAwayY‘];
                    $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey)); //, ‘data‘ => $keyword 目的?

                    return $this->activityStep();
                }
                return $this->reWeixinTextMsg($msgs[‘outOfAwayY‘]);
            } elseif ($keyword == ‘n‘) {
                if ($msgs[‘outOfAwayN‘] == ‘repeat‘) {
                    $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey));
                    return $this->activityStep();
                } else {
                    return $this->reWeixinTextMsg($msgs[‘outOfAwayN‘]);
                }
            }
        }
    }

    /**
     * 确定项,response中含有 Y,N 特殊处理
     * @param type $keyword
     * @param type $memberInfo
     * @param type $msgs
     */
    public function specialStepDeal_2($keyword, $memberInfo, $msgs) {
        $msg = ‘‘;
        //response
        if (!$memberInfo[‘data‘] || $memberInfo[‘data‘] && !preg_match(‘#y|n#i‘, $memberInfo[‘data‘])) {
            $this->setMemberMemStepInfo(array(‘univid‘ => $this->univid, ‘stepKey‘ => $this->stepKey, ‘data‘ => $keyword));
            if ($keyword == ‘y‘) {
                //relateData
                if ($msgs[‘relateData‘]) {
                    $oldData = $this->objUnivTourFront->get($this->openid, $this->univid);

                    $msg = $oldData[$msgs[‘relateData‘]];
                }
                $returnMsg = str_replace(‘{msg}‘, $msg, $msgs[‘confirm‘]);
                return $this->reWeixinTextMsg($returnMsg);
            } elseif ($keyword == ‘n‘) {
                $extData = array();
                if ($msgs[‘exchangeCode‘]) {
                    $exchangeCode = $this->getExchangeCode();
                    $extData[‘exchange_code‘] = $exchangeCode;
                    $msg = $exchangeCode;
                }
                if ($msgs[‘end‘]) {
                    $this->delMemberMemStepInfo();
                }
                $this->saveRowData(‘n‘, $extData); // 默认输入任何字符都为N,统一选择信息
                $returnMsg = str_replace(‘{msg}‘, $msg, $msgs[‘responseN‘] . $msgs[‘suffix‘]);
                return $this->reWeixinTextMsg($returnMsg);
            }
        } elseif ($memberInfo[‘data‘] && preg_match(‘#y|n#i‘, $memberInfo[‘data‘])) {
            if ($keyword == ‘y‘) {
                $extData = array();
                if ($msgs[‘exchangeCode‘]) {
                    $exchangeCode = $this->getExchangeCode();
                    $extData[‘exchange_code‘] = $exchangeCode;
                    $msg = $exchangeCode;
                }
                if ($msgs[‘end‘]) {
                    $this->delMemberMemStepInfo();
                }
                $this->saveRowData($keyword, $extData);

                $returnMsg = str_replace(‘{msg}‘, $msg, $msgs[‘confirmY‘] . $msgs[‘suffix‘]);
                return $this->reWeixinTextMsg($returnMsg);
            }
        }
    }

    /**
     * 保存远程图片到本地
     * @param type $pic
     */
    public function saveRemotePic($pic) {
        $save_path = IMG_USER_UPLOAD_PATH;
        $cnd_url = IMG_USER_UPLOAD_URL;

        //检查目录
        if (@is_dir($save_path) === false) {
            return array(‘ok‘ => false, ‘msg‘ => self::UPLOADPATHUNEXISTS);
        }
        //检查目录写权限
        if (@is_writable($save_path) === false) {
            return array(‘ok‘ => false, ‘msg‘ => self::UPLOADPATHCANNOTWRITE);
        }
        $dir_name = ‘weixin‘;
        $save_path .= DIRECTORY_SEPARATOR . $dir_name;
        if (!is_dir($save_path)) {
            if (!mkdir($save_path, 0777, true)) {
                return array(‘ok‘ => false, ‘msg‘ => self::MKDIRFAILED . ‘saveroot/‘ . $dir_name);
            }
        }
        $ymd = date("Y") . DIRECTORY_SEPARATOR . date(‘md‘);
        $save_path .= DIRECTORY_SEPARATOR . $ymd;
        if (!is_dir($save_path)) {
            if (!mkdir($save_path, 0777, true)) {
                return array(‘ok‘ => false, ‘msg‘ => self::MKDIRFAILED . ‘saveroot/‘ . $dir_name . DIRECTORY_SEPARATOR . $ymd);
            }
        }

//        $temp_arr = explode(".", $pic);
//        $file_ext = array_pop($temp_arr);
//        $file_ext = trim($file_ext);
//        $file_ext = strtolower($file_ext);
        $file_ext = ‘jpg‘; // 微信返回的图片没有图片后缀,故指定后缀

        $file_name = date("His") . ‘_‘ . rand(10000, 99999) . ‘.‘ . $file_ext;
        $file_path = $save_path . DIRECTORY_SEPARATOR . $file_name;

        $imgContentInfo = curlRequest($pic);
        list($imgContent, $imgInfo) = $imgContentInfo;
        if ($imgInfo[‘download_content_length‘] > $this->imgMaxSize) {
            return array(‘ok‘ => false, ‘msg‘ => self::UPLOADFILETOOBIG);
        }
        if ($imgContent) {
            if (@file_put_contents($file_path, $imgContent)) {
                @chmod($file_path, 0777);
                return array(‘ok‘ => true, ‘msg‘ => $cnd_url . DIRECTORY_SEPARATOR . $dir_name . DIRECTORY_SEPARATOR . $ymd . DIRECTORY_SEPARATOR . $file_name);
            }
        }
        return array(‘ok‘ => false, ‘msg‘ => self::UPLOADREMOTEPICFAIL);
    }

}

 

微信校园行活动配置文件

 

<?php

/**
 * desc: 活动配置文件
 *        进度: 1个人资料, 2评测问答,3评测拍照, 4校园达人, 5推广大使
 * 
 *        next:自动指定下一步
 *        autoNext: 没有确定就直接下一步
 *        confirmAutoNext:确定后自动执行下一步
 *        completeNext: 此步骤完成后进入下一步
 * 
 * author: chenbingbing@kimiss.com 2015-9-14
 * 
 */
return array(
    ‘id‘ => ‘beiyu‘,
    ‘title‘ => ‘北语‘,
    ‘startTime‘ => ‘1412826681‘, //2014-10-9
    ‘endTime‘ => ‘1446912000‘, //2015-11-8
    ‘join‘ => array(
        ‘title‘ => ‘报名信息‘,
        ‘keyword‘ => ‘闺蜜女神学院‘,
        ‘response‘ => ‘HI,欢迎参加闺蜜女神学院公开课,在接下来的课堂上我们会有很多有意思的互动环节,按照闺蜜小助手的提示在这里一步步完成操作就可以拿走丰厚大礼哦,快快行动起来!‘ .
        "\n" . ‘首先,请填写你的真实资料,请按照“姓名+电话+学院”的形式发送给我。‘ .
        "\n" . ‘例如:小明+123456+经管学院。‘,
//        ‘confirm‘ => ‘您输入资料信息是“{msg}”,确认请回复Y,重新输入请回复N‘,
//        ‘confirmN‘ => ‘请重新回复你的资料信息,记得按照“姓名+电话+学院”的方式发送哦。‘,
        ‘pregFail‘ => ‘你回复的信息有误,请按照“姓名+电话+学院”的格式回复哦。‘,
        ‘completeResponse‘ => ‘感谢你完善资料,接下来的互动更精彩哦!‘, // 
        ‘preg‘ => ‘#[^\+]{2,20}[\+]?\d{11}[\+]?[^\+]{2,40}#‘,
//        ‘completeNext‘ => ‘evaluating‘,// 直接执行下一个
      ‘end‘ => ‘1‘,
    ),
    ‘evaluating‘ => array(
        ‘keyword‘ => ‘北语百人评测‘,
        ‘need‘ => ‘join‘, // 需要某项值
        ‘containFields‘ => ‘questions+uploadImg‘,
        ‘response‘ => ‘欢迎来到“百人评测”环节,按照小助手的提示做,成为美肤达人so easy!现场的美肤产品你试用了吗?快动动手指回复评测结果吧!‘,
        ‘autoNext‘ => ‘evaluating_questions‘,
        ‘needNotice‘ => ‘你还没有完成资料填写哦,请先填写你的真实资料,请按照“姓名+电话+学院”的形式发送给我。例如:小明+123456+经管学院。‘,
    ),
    ‘evaluating_questions‘ => array(
        ‘type‘ => ‘radio‘,
        ‘title‘ => ‘评测问答 - 唇釉‘,
        ‘contents‘ => array(
            array(‘response‘ => ‘小助手的第一个问题是:你对这款唇釉的外观设计感觉如何?‘ . "\n" .// prefix 小助手的第一个问题是:
                ‘A.很好   B.一般   C.没感觉‘,
                ‘preg‘ => ‘#a|b|c#‘,
                ‘a‘ => ‘很好‘,
                ‘b‘ => ‘一般‘,
                ‘c‘ => ‘没感觉‘,
            ),
            array(‘response‘ => ‘第二个问题:唇釉在使用过程中的方便程度是?‘ . "\n" .
                ‘A.很好   B.一般   C. 没感觉‘,
                ‘preg‘ => ‘#a|b|c#‘,
                ‘a‘ => ‘很好‘,
                ‘b‘ => ‘一般‘,
                ‘c‘ => ‘没感觉‘,
            ),
            array(‘response‘ => ‘第三个问题:唇釉涂抹在嘴唇上的显色程度,是否与唇釉本身保持一致?‘ . "\n" .
                ‘A.很好   B.一般   C. 没感觉‘,
                ‘preg‘ => ‘#a|b|c#‘,
                ‘a‘ => ‘很好‘,
                ‘b‘ => ‘一般‘,
                ‘c‘ => ‘没感觉‘,
            ),
            array(‘response‘ => ‘第四个问题:使用唇釉后,唇部肌肤的水润度如何?‘ . "\n" .
                ‘A.很好   B.一般   C. 没感觉‘,
                ‘preg‘ => ‘#a|b|c#‘,
                ‘a‘ => ‘很好‘,
                ‘b‘ => ‘一般‘,
                ‘c‘ => ‘没感觉‘,
            ),
            array(‘response‘ => ‘第五个问题:涂抹唇釉后一段时间,用纸巾或化妆棉轻轻按压双唇,观察唇上的妆效保持程度?‘ . "\n" .
                ‘A.很好   B.一般   C. 没感觉‘,
                ‘preg‘ => ‘#a|b|c#‘,
                ‘a‘ => ‘很好‘,
                ‘b‘ => ‘一般‘,
                ‘c‘ => ‘没感觉‘,
            ),
            array(‘response‘ => ‘第六个问题:用化妆棉沾取唇部卸妆产品,轻轻擦拭双唇上的唇釉,验证产品容易卸除的程度?‘ . "\n" .
                ‘A.很好   B.一般   C. 没感觉‘,
                ‘preg‘ => ‘#a|b|c#‘,
                ‘a‘ => ‘很好‘,
                ‘b‘ => ‘一般‘,
                ‘c‘ => ‘没感觉‘,
            ),
            array(‘response‘ => ‘第七个问题:唇釉整体的使用感如何?‘ . "\n" .
                ‘A.很好   B.一般   C. 没感觉‘,
                ‘preg‘ => ‘#a|b|c#‘,
                ‘a‘ => ‘很好‘,
                ‘b‘ => ‘一般‘,
                ‘c‘ => ‘没感觉‘,
            )
        ),
//        ‘confirm‘ => ‘对于小助手提出的问题,你的回答是“{msg}”,确认请回复Y,重新输入请回复N‘,
//        ‘confirmY‘ => ‘感谢你的配合,你已经有达人资质喽,‘,
//        ‘confirmN‘ => ‘repeat‘,
        ‘completeResponse‘ => ‘感谢你的配合,光回答问题怎么过瘾?‘,
        ‘completeNext‘ => ‘evaluating_uploadImg_two‘,
        ‘pregFail‘ => ‘请输入范围内的答案哦‘,
    ),    
    ‘evaluating_uploadImg_two‘ => array(
        ‘type‘ => ‘image‘, // 把用户的图片存储到服务器上
        ‘title‘ => ‘评测图片‘,
        ‘response‘ => ‘拍照评测可是达人们必做的功课哦,快快现场拍摄产品图片发送给我吧!小助手提示:产品的包装、细节、质地、试色过之后的照片都可以分别拍出来哦!第一张就来个产品包装图吧!‘,
        ‘imgMsgs‘ => array(
            ‘再来一张试色图看看你的拍照技术~‘,
            ‘不错哦,拍够三张可以“召唤神龙”,拿着产品和它美美合个影吧!‘,
            ‘哇卡卡,评测产品是不是超级简单?‘,
        ),
        ‘completeNext‘ => ‘expert‘,
    ),
    
    // 校园达人 - 特殊项,特殊处理
    ‘expert‘ => array(
        ‘title‘ => ‘校园达人‘,
        ‘type‘ => ‘radio‘,
//        ‘special‘ => ‘1‘,
        ‘response‘ => ‘你也可以轻松成为校园达人啦!你想成为哪一类的校园达人?快快回复告诉我,万能的闺蜜会根据你的爱好邀请专家订制专属课程,将你培养成万众瞩目的“女神”哦!‘ .
        "\n" . ‘A、 我喜爱购物,我想成为“时尚买手” ‘ .
        "\n" . ‘B、 我擅长写作,我想成为“时尚博主”‘ .
        "\n" . ‘C、 我个性张扬,我想成为“荧屏达人”‘ .
        "\n" . ‘D、 我只想做一个安静的大学生‘,
        ‘preg‘ => ‘#a|b|c|d#‘,
//        ‘confirm‘ => ‘你选择了“{msg}”确定请回复Y,重新输入请回复N‘, // 正确范围内的选择
//        ‘confirmY‘ => ‘之后我们会有工作人员根据你的选择与你取得联系。‘, // 后接 校园推广大使 msg信息
//        ‘confirmN‘ => ‘repeat‘,
//        ‘outOfAway‘ => ‘成为校园达人可是福利多多哦?你确认不想成为闺蜜校园达人么?回复Y,进入下一环节,回复N,报名校园达人。‘, // 其他选择
//        ‘outOfAwayY‘ => ‘ambassador‘,
//        ‘outOfAwayN‘ => ‘repeat‘,
        // Y  校园推广大使  N 报名  
        ‘pregFail‘ => ‘请输入范围内的答案哦‘,
        ‘a‘ => ‘A、我喜爱购物,我想成为“时尚买手”‘,
        ‘b‘ => ‘B、我擅长写作,我想成为“时尚博主”‘,
        ‘c‘ => ‘C、我个性张扬,我想成为“荧屏达人”‘,
        ‘d‘ => ‘D、我只想做一个安静的大学生‘,
        ‘completeResponse‘ => ‘之后我们会有工作人员根据你的选择与你取得联系。‘,
        ‘completeNext‘ => ‘ambassador‘,
//        ‘y‘ => ‘进入下一环节‘,
    ),
    // 校园推广大使   - 第一步回复包含 Y , N的
    ‘ambassador‘ => array(
        ‘title‘ => ‘推广大使‘,
        ‘special‘ => ‘2‘,
        ‘response‘ => ‘号外,闺蜜校园推广大使火热招聘中,应聘成功不仅可以享受免费护肤品、参与品牌线下活动、自行组织线上活动等福利,每月更有固定薪水奖励。有兴趣的话快快回复报名吧。回复:Y报名,N不我不感兴趣。‘,
        ‘relateData‘ => ‘join‘,
        ‘confirm‘ => ‘你的资料是:{msg},我们会及时和你联系,确认请回复Y,重新输入请回复N。‘,
        ‘confirmN‘ => ‘ambassador_relateData_join‘, // 完善资料
        ‘confirmY‘ => ‘恭喜你报名成功,我们会安排招聘小助手与你取得联系哦。‘, // 后接exchangeCode
        ‘responseN‘ => ‘好遗憾你没有报名应聘校园推广大使,如果想报名可在微信回复“报名校园推广大使”,我们会安排招聘小助手与你取得联系。‘, //我不感兴趣  后接exchangeCode
        ‘suffix‘ => ‘感谢你参与本次女神公开课,送给你礼物兑换码一份{msg},凭兑换码可以在活动结束后领取精美礼物一份。‘, 
        ‘exchangeCode‘ => ‘1‘,
        ‘end‘ => ‘1‘,
        ‘y‘ => ‘报名‘,
        ‘n‘ => ‘不感兴趣‘,
    ),
    ‘ambassador_relateData_join‘ => array(
        ‘title‘ => ‘再次输入资料信息‘,
        ‘special‘ => ‘3‘,// 特殊项标识,无其他任何意义
        ‘response‘ => ‘请重新回复你的资料信息,记得按照“姓名+电话+学院”的方式发送哦。‘,
        ‘confirm‘ => ‘您输入资料信息是“{msg}”,确认请回复Y,重新输入请回复N‘,
        ‘confirmN‘ => ‘请重新回复你的资料信息,记得按照“姓名+电话+学院”的方式发送哦。‘,
        ‘pregFail‘ => ‘你回复的信息有误,请按照“姓名+电话+学院”的格式回复哦。‘,
        ‘confirmY‘ => ‘ambassador‘, // 使用 ambassador 中的信息
        ‘preg‘ => ‘#[^\+]{2,20}[\+]?\d{11}[\+]?[^\+]{2,40}#‘,    
        ‘realKey‘ => ‘join‘,// 不存储此键值,更新指定的realKey值
    ),    
);

 

微信校园行活动流程

上一篇:c# – 在Skype for Business中获取通话时间


下一篇:如何在Android上正确访问LIVE相机数据? Skype如何做到这一点?