<?php
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
$settings = $this->module['config'];

load()->func('communication');
$op =$_GPC['op'];
if($op=='callbackcode'){
   ijson(array('code'=>200,'msg'=>'成功'));
}
/**
 * 识别图片
 * */
if($op=='expressthumb'){
    $appid = $settings['baiduappid'];
    $apikey = $settings['baiduapikey'];
    $secretkey = $settings['baidusecretkey'];
    $client = new AipOcr($appid, $apikey, $secretkey);
    load()->classs('weixin.account');
    $account_api = WeAccount::create();
    $account_api->clearAccessToken();
    $access_token = $account_api->getAccessToken();
    $serverId = $_GPC['serverId'];
    if(!empty($serverId)){
        $thumb = getmedia($access_token,$serverId,"serverId");
    }
    $img = file_get_contents('../attachment/'.$thumb);
    // 带参数调用通用文字识别, 图片参数为远程url图片
     $data = $client->basicAccurate($img);
    foreach ($data['words_result'] as $v){
        if(preg_match("/^1[34578]{1}\d{9}$/",$v['words'])){
            $mobile = $v['words'];
        }
        if(!preg_match('/['.chr(0xa1).'-'.chr(0xff).']/', $v['words'])&&strlen($v['words'])>12){
            $expresno = $v['words'];
        }
    }
    $data=json_decode($data,true);
    exit(json_encode(array('code'=>200,'data'=>['mobile'=>$mobile,'expresno'=>$expresno,'url'=>$url])));
}
if($op=='expressmobile'){
    $appid = $settings['baiduappid'];
    $apikey = $settings['baiduapikey'];
    $secretkey = $settings['baidusecretkey'];
    $client = new AipOcr($appid, $apikey, $secretkey);
    load()->classs('weixin.account');
    $account_api = WeAccount::create();
    $account_api->clearAccessToken();
    $access_token = $account_api->getAccessToken();
    $serverId = $_GPC['serverId'];
    if(!empty($serverId)){
        $thumb = getmedia($access_token,$serverId,"serverId");
    }
    //$url =toimage($thumb); //
    $img = file_get_contents('../attachment/'.$thumb);
    // 带参数调用通用文字识别, 图片参数为远程url图片
    $data = $client->basicAccurate($img);
    foreach ($data['words_result'] as $v){
        if(preg_match("/^1[34578]{1}\d{9}$/",$v['words'])){
            $mobile = $v['words'];
        }
    }

    exit(json_encode(array('code'=>200,'data'=>['mobile'=>$mobile,'url'=>$url])));
}
if($op=='code'){
   $log = array();
   $settings = $this->module['config'];
   $mobile = $_GPC['mobile'];
   if(empty($_W['openid'])){
       exit(json_encode(array('code'=>203,'msg'=>'请在微信内打开!')));
   }
   $code =generate_code(6);
   $data['code'] = $code;
   $data['openid'] = $_W['openid'];
   $data['uniacid'] = $_W['uniacid'];
   $data['nickname'] =$_W['fans']['nickname'];
   $data['avatar'] =$_W['fans']['tag']['avatar'];
   $data['address'] =$_W['fans']['city'];
   $member = pdo_fetch("SELECT * FROM".tablename("evzg_express_member")."WHERE openid='{$_W['openid']}' AND uniacid='{$_W['uniacid']}'");
   if ($settings['enabled']==1){
       $logtype="阿里大鱼短信接口";
       $sendSmsResults = sendSms($mobile,$settings['dayu']['dayu_temp'],array('code'=>$code),$settings,$log=array());
       $sendSmsResults = json_decode(json_encode($sendSmsResults),true);
       if($sendSmsResults['Code']=='OK'){
           $log['status'] = 1;
       }
   }else{
       $logtype="聚合短信接口";
       $key =$settings['juhe']['juhe_appkey'];
       $tpl_value ='#code#='.$code;
       $tpl_id = $settings['juhe']['juhe_temp'];
       $sendSmsResults = juHeSms($key, $mobile, $tpl_id, $tpl_value,$log=array());
   }
   if(empty($member)){
     $result = pdo_insert('evzg_express_member',$data);
   }else{
     $result = pdo_update('evzg_express_member',$data,array('openid'=>$_W['openid']));
   }

    $log['oid'] = 0;
    $log['sid'] = 0;
    $log['openid'] = $_W['openid'];
    $log['operator'] = "官方";
    $log['content'] = "{$member['nickname']}发送验证码：{$code} {$logtype}";
    $log['addtime']=time();
    $log['msg'] = json_encode($sendSmsResults);
    $log['type'] = 1;
    msgLog($log);
   if($result){
     exit(json_encode(array('code'=>200)));
   }else{
      exit(json_encode(array('code'=>201,'msg'=>'验证码发送失败')));
   }
}
if($op=='shop'){
    $mobile = $_GPC['mobile'];
    $code =generate_code(6);
    $data['code'] = $code;
    $data['openid'] = $_W['openid'];
    $data['uniacid'] = $_W['uniacid'];
    $data['mobile'] = $mobile;
    $shop = pdo_fetch("SELECT * FROM".tablename("evzg_express_shop")."WHERE openid='{$_W['openid']}'");
    if ($settings['enabled']==1){
        $sendSmsResults = sendSms($mobile,$settings['dayu']['dayu_temp'],array('code'=>$code),$settings);
        $sendSmsResults = json_decode(json_encode($sendSmsResults),true);
        if($sendSmsResults['Code']=='OK'){
            $log['status'] = 1;
        }
    }else{
        $key =$settings['juhe']['juhe_appkey'];
        $tpl_value ='#code#='.$code;
        $tpl_id = $settings['juhe']['juhe_temp'];
        juHeSms($key, $mobile, $tpl_id, $tpl_value);
    }

    $log['oid'] = 0;
    $log['sid'] = 0;
    $log['openid'] = $_W['openid'];
    $log['operator'] = "官方";
    $log['content'] = "{$member['nickname']}发送注册店铺的 验证码：{$code} {$logtype}";
    $log['addtime']=time();
    $log['msg'] = json_encode($sendSmsResults);
    $log['type'] = 1;
    msgLog($log);
    if(empty($shop)){
        $result = pdo_insert('evzg_express_shop',$data);
    }else{
        $result = pdo_update('evzg_express_shop',$data,array('openid'=>$_W['openid']));
    }
    if($result){
        exit(json_encode(array('code'=>200)));
    }else{
        exit(json_encode(array('code'=>201,'msg'=>'验证码发送失败')));
    }
}
if($op=='addnum'){
    if(empty($_GPC['num'])){
        exit(json_encode(array('code'=>400,'msg'=>'非法请求')));
    }
    $rootset = pdo_fetch("SELECT * FROM".tablename("evzg_express_rootuser")."WHERE uniacid='{$_GPC['uniacid']}'");
    $data['num'] =intval($_GPC['num'])+$rootset['num'];
    $data['price'] = $_GPC['price'];
    pdo_update("evzg_express_rootuser",$data,array('uniacid'=>$_GPC['uniacid']));
    exit(json_encode(array('code'=>200)));
}
