Sign.php 13.5 KB
<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 2015-11-21
 */

namespace app\mobile\controller;

use app\home\logic\UsersLogic;
use app\home\model\Message;
use think\Cookie;
use think\Page;
use think\Request;
use think\Session;
use think\Verify;
use think\db;
use think\Cache;
use qcloudcos\Conf;
use qcloudcos\Myqcloudcos;
use think\Controller;


class Sign extends MobileBase
{

    public $user_id = 0;
    public $user = array();

    /**
     * 析构流函数
     */
    public function __construct()
    {
        parent::__construct();
        if (session('?user')) {
            $user = session('user');
            $user = M('users')->where("user_id", $user['user_id'])->find();
            session('user', $user);  //覆盖session 中的 user
            $this->user = $user;
            $this->user_id = $user['user_id'];
            $this->assign('user', $user); //存储用户信息

            $nologin = array(
                'getstoid',
            );
            if ($this->user_id == 0) {
                $this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId())));
            }

        } else {
            $uid = Cookie::get('user_id');
            if ($uid) {
                $user = M('users')->where("user_id", $uid)->where("store_id", getMobileStoId())->find();
                session('user', $user);  //覆盖session 中的 user
                $this->user = $user;
                $this->user_id = $user['user_id'];
                $this->assign('user', $user); //存储用户信息
            }
        }
//        if (!$this->user_id) {
//            $this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId())));
//            exit;
//        }

    }


    //培训签到
    public function index()
    {
        $getaccdb = strtolower(urldecode(urldecode(I('accdb'))));
        $getnumber = strtolower(urldecode(urldecode(I('number'))));
        $getdate = strtolower(urldecode(urldecode(I('date'))));
        $getkey = strtolower(I('key'));


        if (empty($getaccdb) || empty($getnumber) || empty($getdate) || empty($getkey)) {
            $this->assign('msg', "签到失败,参数有误!");//
            return $this->fetch();
        }


        $fieldstr = "accdb=" . $getaccdb . "&number=" . $getnumber . "&date=" . $getdate . "&key=" . getErpKey();

        //如果key不相等,报错
        if ($getkey != md5($fieldstr)) {
            $this->assign('msg', "签到失败!");//
            return $this->fetch();
        }
        $getopenid = $this->user["openid"];
        $sto = tpCache('shop_info', getMobileStoId());
        $store_logo = $sto['store_logo'];//商家LOGO
        $head_pic = $this->user["head_pic"];//会员头像
        $head_pic=str_replace('/132','/0',$head_pic);
        $api_token = $sto['api_token'];
        $this->assign('store_logo', $store_logo);//商家logo
        $this->assign('head_pic', $head_pic);//会员头像
        if ($api_token) {
            $data = array(
                'number' => $getnumber,//单据
                'key' => $getdate,//单据日期
                'openid' => $getopenid
            );//新增或者编辑时存放的数据()
            $vipinfo = getApiData('wxd.train.signin', $api_token, $data);
            mlog($vipinfo, "signindex");
            if (empty($vipinfo)) {
                $this->assign('msg', "签到失败!");//
            } else {
                $vipinfo = json_decode($vipinfo, true);
                if ($vipinfo['code'] != 1 || empty($vipinfo['data'])) {
                    $this->assign('msg', "签到失败!");//
                    $this->assign('msg1', $vipinfo['msg']);//
                } else {
                    $this->assign('msg', "签到成功!");//
                    $this->assign('msg1', "谢谢您的到临!");//
                }
                if ($vipinfo['data'] && $vipinfo['count']) {
                    $opencount = $vipinfo['count'];
                    $openlist = "";
                    foreach ($vipinfo['data'] as $k => $vo) {
                        if ($vo['openid']) {
                            if ($openlist) {
                                $openlist .= "," . $vo['openid'];
                            } else {
                                $openlist .= $vo['openid'];
                            }

                        }
                    }

                    $where['openid'] = array('in', $openlist);
                    $res = M("users")->where('store_id', getMobileStoId())
                        ->where($where)->order('reg_time desc')
                        ->field('nickname,head_pic')->select();
                    $this->assign('opencount', $opencount);//openlist
                    $this->assign('openlist', $res);//openlist
                }

            }
        }

        return $this->fetch();


    }

    //员工绑定
    public function staffbinding()
    {
        $getstoid = strtolower(urldecode(urldecode(I('stoid'))));
        $getusercode = strtolower(urldecode(urldecode(I('usercode'))));
        $getkey = strtolower(I('key'));

//        $getstoid="1";
//        $getusercode="admin";
//        $getkey="a1e42219f7d651c8e8e09f331723df7e";

        if (empty($getstoid) || empty($getusercode)  || empty($getkey)) {
            $this->assign('msg', "绑定失败!");//
            return $this->fetch();
        }
        $fieldstr = "stoid=" . $getstoid . "&usercode=" . $getusercode . "&key=" . getErpKey();
        //如果key不相等,报错
        if ($getkey != md5($fieldstr)) {
            $this->assign('msg', "绑定失败!");//
            return $this->fetch();
        }
        $getopenid = $this->user["openid"];
        $sto = tpCache('shop_info', $getstoid);
        $store_logo = $sto['store_logo'];//商家LOGO
        $head_pic = $this->user["head_pic"];//会员头像
        $head_pic=str_replace('/132','/0',$head_pic);
        $api_token = $sto['api_token'];
        $this->assign('store_logo', $store_logo);//商家logo
        $this->assign('head_pic', $head_pic);//会员头像
        if ($api_token) {
            $data = array(
                'usercode' => $getusercode,//员工编号
                'openid' => $getopenid
            );//新增或者编辑时存放的数据()
            $vipinfo = getApiData('wxdoa.staff.binding', $api_token, $data);
          //  echo $vipinfo;die;
//            mlog($vipinfo, "staffbinding");
            if (empty($vipinfo)) {
                $this->assign('msg', "绑定失败!");//
            } else {
                $vipinfo = json_decode($vipinfo, true);
                if ($vipinfo['code'] != 1) {
                    $this->assign('msg', "绑定失败!");//
                    $this->assign('msg1', $vipinfo['msg']);//
                } else {
                    $this->assign('msg', "绑定成功!");//

                }


            }
        }

        return $this->fetch();


    }


    //OA登录
    public function codelogin()
    {
        $getstoid = strtolower(urldecode(urldecode(I('stoid'))));
        $getaccdb = strtolower(urldecode(urldecode(I('accdb'))));
        $getcodekey = strtolower(I('codekey'));
        $getkey = strtolower(I('key'));

        //$getaccdb="1";
//        $getusercode="admin";
//        $getkey="a1e42219f7d651c8e8e09f331723df7e";
        $this->assign('stoid', $getstoid);//
        if (empty($getcodekey) || empty($getaccdb)  || empty($getkey)) {
            $this->assign('msg', "授权失败!");//
            return $this->fetch();
        }
        $fieldstr = "accdb=" . $getaccdb . "&codekey=" . $getcodekey . "&key=" . getErpKey();


        //如果key不相等,报错
        if ($getkey != md5($fieldstr)) {
            $this->assign('msg', "授权失败!");//
            return $this->fetch();
        }
        $getopenid = $this->user["openid"];
        $sto = tpCache('shop_info', $getstoid);
        $store_logo = $sto['store_logo'];//商家LOGO
        $head_pic = $this->user["head_pic"];//会员头像
        $head_pic=str_replace('/132','/0',$head_pic);
        $api_token = $sto['api_token'];
        $this->assign('store_logo', $store_logo);//商家logo
        $this->assign('head_pic', $head_pic);//会员头像
        if ($api_token) {
            $data = array(
                'accdb' => $getaccdb,//账套
                'codekey' => $getcodekey,//CODEKEY
                'openid' => $getopenid
            );//新增或者编辑时存放的数据()
            $vipinfo = getApiData('wxdoa.code.login', $api_token, $data);
            mlog($vipinfo, "codelogin");
            if (empty($vipinfo)) {
                $this->assign('msg', "授权失败!");//
            } else {
                $vipinfo = json_decode($vipinfo, true);
                if ($vipinfo['code'] != 1) {
                    $this->assign('msg', "授权失败!");//
                    $this->assign('msg1', $vipinfo['msg']);//
                } else {
                    $this->assign('msg', "授权成功!");//

                }


            }
        }

        return $this->fetch();


    }

    //万信达助手(微信端)
    public function wxdzsopenid()
    {
        $this->redirect('http://mshop.jmhcn.com/XinDaHelper/WeiXin/wxd_index.aspx?WX_OpenId='.session('user')['openid']);
    }

    //商家客服微信绑定
    public function store_binding()
    {
        $getstoid = urldecode(urldecode(I('stoid')));
        $getadminid = urldecode(urldecode(I('adminid')));
        $rndnum = urldecode(urldecode(I('rndnum')));
        $getkey = I('bindkey');

        if (empty($getstoid) || empty($getadminid)  || empty($rndnum) || empty($getkey)) {
            $this->assign('msg', "绑定失败1!");//
            return $this->fetch();
        }
        $fieldstr = "stoid=" . $getstoid . "&adminid=" . $getadminid . "&rndnum=" . $rndnum . "&key=" . getErpKey();
        //如果key不相等,报错
        if ($getkey != md5($fieldstr)) {
            $this->assign('msg', "绑定失败2!");//
            return $this->fetch();
        }
        $getopenid = $this->user["openid"];
        $sto = tpCache('shop_info', $getstoid);
        $store_logo = $sto['store_logo'];//商家LOGO
        $head_pic = $this->user["head_pic"];//会员头像
        $head_pic=str_replace('/132','/0',$head_pic);
        $nickname = $this->user["nickname"];//会员昵称

        $this->assign('store_logo', $store_logo);//商家logo
        $this->assign('head_pic', $head_pic);//会员头像
        $this->assign('nickname', $nickname);//会员昵称
        $this->assign('stoid', $getstoid);//

        if ($getopenid) {

            $vipinfo=M('admin')->where(array('store_id'=>$getstoid,'admin_id'=>$getadminid,'is_custom_service'=>1))->find();
            if (empty($vipinfo))
            {
                $this->assign('msg', "绑定失败3!");//
            }
            else
            {
                $update['openid_type']=1;
                $update['admin_openid']=$getopenid;
                $update['openid_nick']=$nickname;   //昵称也存储
                M('admin')->where(array('store_id'=>$getstoid,'admin_id'=>$getadminid,'is_custom_service'=>1))->save($update);
                //admin的聊天使用
                $admin_login=I('admin_login');
                $this->assign('admin_login', $admin_login);//
                $this->assign('msg', "绑定成功!");//
            }
        }

        return $this->fetch();


    }

    //商家客服微信登陆
    public function store_wxlogin()
    {
        $getstoid = urldecode(urldecode(I('stoid')));
        $getopenid = $this->user["openid"];
        $sto = tpCache('shop_info', $getstoid);
        $store_logo = $sto['store_logo'];//商家LOGO
        $head_pic = $this->user["head_pic"];//会员头像
        $head_pic=str_replace('/132','/0',$head_pic);
        $this->assign('store_logo', $store_logo);//商家logo
        $this->assign('head_pic', $head_pic);//会员头像
        $this->assign('stoid', $getstoid);//

        if ($getopenid) {

            $res=M('admin')->alias('a')
                ->join('store b','a.store_id=b.store_id','left')
                ->field('a.*,b.reg_mobile')
                ->where(array('a.store_id'=>$getstoid,'a.admin_openid'=>$getopenid,'a.openid_type'=>1,'a.is_custom_service'=>1))->find();
            if (empty($res)) {
                $this->assign('msg', "登陆失败!");//
            } else {
                if ($res['ERPId'])
                {
                    $fieldstr = "ERPId=" . $res['ERPId'] . "&ERPUser=" . $res['ERPUser'];

                }
                else{
                    $fieldstr = "mobile=" . $res['reg_mobile'] . "&ERPUser=" . $res['ERPUser'];
                }
                $gofieldstr = $fieldstr;
                $fieldstr .= "&Key=" . getErpKey();
                $fieldstr1 = strtolower($fieldstr);
                $key = md5($fieldstr1);
                $poststr = $gofieldstr . "&key=" . $key;
                $poststr = urlencode($poststr);


                $this->redirect("/admin/index/get_post/?url=".$poststr."&ismobile=1");


            }
        }

        return $this->fetch();


    }

}