文件
mashangyoujiu/Application/Retail/Controller/BindingController.class.php

576 行
23 KiB
PHP
可执行文件

<?php
namespace Retail\Controller;
// header("Access-Control-Allow-Origin: *");
// header("Access-Control-Allow-Methods: POST");
// header('Access-Control-Allow-Headers:x-requested-with,content-type');
use Common\Business\CacheHandle;
use Think\Controller;
use Think\Verify;
use Think\Jssdk;
//[1.8.50]
class BindingController extends Controller
{
public static $SET; //全局静态配置
//微信缓存
protected static $_wxappid;
protected static $_wxappsecret;
protected static $_cardinfo;
protected static $uid;
protected static $debug = 0;
public function _initialize()
{
//定义全局门店id
if (self::$debug) {
$_SESSION['openid'] = 'orLyYtyz8G3grwTOvjz5StmeFe7M';
} else {
self::$SET = $_SESSION['SET'] = $this->checkSet();
$_url = getServerUrl() . $_SERVER['REQUEST_URI'];
// 用户自定义配置微信公众号
self::$_wxappid = self::$SET['appid'];
self::$_wxappsecret = self::$SET['appsecret'];
//var_dump(strpos($_SERVER["HTTP_USER_AGENT"], "MicroMessenger"));die;
if (!$_SESSION['sqmode']) {
if (strpos($_SERVER["HTTP_USER_AGENT"], "MicroMessenger")) {
//判断code是否存在
if ($_GET['code']) {
if ($_GET['code'] != 'authdeny') {
//用户授权
$options['appid'] = self::$_wxappid;
$options['appsecret'] = self::$_wxappsecret;
$wx = new \Util\Wx\Wechat($options);
$re = $wx->getOauthAccessToken($_GET['code']); //
$openid = $re['openid'];
if ($re) {
$_SESSION['sqmode'] = 'wecha';
$_SESSION['openid'] = $openid;
}
} else {
$this->error(0, '本应用需要您的授权才可以使用!');
}
} else {
$options['appid'] = self::$_wxappid;
$options['appsecret'] = self::$_wxappsecret;
$wx = new \Util\Wx\Wechat($options);
$squrl = $wx->getOauthRedirect($_url, '1', 'snsapi_base');
header("Location:" . $squrl);
exit;
}
} else {
$this->error('请使用微信浏览器访问本应用!');
}
}
$this->getJssdk();
}
}
//第三方授权功能
//设置全局变量 全局缓存SET
public function checkSet()
{
$set = M('partner_wxconfig')->find();
if (!$set) {
$this->error('合伙人未配置公众号!');
exit;
}
$oldSet = session('SET');
if ($set['appid'] != $oldSet['appid'] || $set['appsecret'] != $oldSet['appsecret']) {
session('sqmode', null);
}
return $set ? $set : $this->error('系统全局设置未定义!');
}
//禁止不动的失败提示的信息
public function diemsg($status, $msg)
{
//成功为1,失败为0
$this->error($msg);
$this->display('Base_diemsg');
die();
}
//生成微信jssdk 参数 ,用于页面展示图片和分享链接
public function getJssdk()
{
self::$_wxappid = self::$SET['appid'];
self::$_wxappsecret = self::$SET['appsecret'];
$jssdk = new Jssdk(self::$_wxappid, self::$_wxappsecret);
$signPackage = $jssdk->GetSignPackage();
$this->assign('signPackage', $signPackage);
}
/* 加载模板 */
public function index()
{
$openid = session('openid');
if (empty($openid)) {
$this->assign('msg', '未获取到用户openid');
} else {
$token = I('get.token');
$map['start_time'] = array('lt', time());
$map['end_time'] = array('gt', time());
$map['token'] = $token;
$map['openid'] = array('exp', 'IS NULL');
$map['status'] = 0;
$info = M('auth_token')->where($map)->find();
if (!empty($info)) {
M('auth_token')->where(array('id' => $info['id']))->save(array('openid' => $openid, 'status' => 1));
switch ($info['user_type']) {
case '0':
//自建用户
$find = M('jurisdiction_user')->where(array('openid' => $openid, 'user_type' => 0))->find();
if ($find) {
$this->assign('msg', '该微信已绑定自建用户:' . $find['name']);
} else {
$userInfo = M('jurisdiction_user')->where(array('id' => $info['user_id']))->find();
$firstUserInfo = M('jurisdiction_user')->where(array('id' => $userInfo['first_pid']))->find();
if ($firstUserInfo['user_type'] == 1) {
$userInfo['type'] = '合伙人';
} elseif ($firstUserInfo['user_type'] == 2) {
$userInfo['type'] = '代理商';
} elseif ($firstUserInfo['user_type'] == 2) {
$userInfo['type'] = '商户';
} elseif ($firstUserInfo['user_type'] == 4) {
$userInfo['type'] = '门店';
} elseif ($firstUserInfo['user_type'] == 5) {
$userInfo['type'] = '导购员';
}
$this->assign('userInfo', $userInfo);
}
break;
case '1':
//合伙人
$find = M('jurisdiction_user')->where(array('openid' => $openid, 'user_type' => 1))->find();
if ($find) {
$this->assign('msg', '该微信已绑定合伙人:' . $find['name']);
} else {
$userInfo = M('jurisdiction_user')->where(array('id' => $info['user_id']))->find();
$userInfo['type'] = '合伙人';
$this->assign('userInfo', $userInfo);
}
break;
case '2':
//代理商
$find = M('jurisdiction_user')->where(array('openid' => $openid, 'user_type' => 2))->find();
if ($find) {
$this->assign('msg', '该微信已绑定代理商:' . $find['name']);
} else {
$userInfo = M('jurisdiction_user')->where(array('id' => $info['user_id']))->find();
$userInfo['type'] = '代理商';
$this->assign('userInfo', $userInfo);
}
break;
case '3':
//商户
$find = M('jurisdiction_user')->where(array('openid' => $openid, 'user_type' => 3))->find();
if ($find) {
$this->assign('msg', '该微信已绑定商户:' . $find['name']);
} else {
$userInfo = M('jurisdiction_user')->where(array('id' => $info['user_id']))->find();
$userInfo['type'] = '商户';
$this->assign('userInfo', $userInfo);
}
break;
case '4':
//店长
$find = M('jurisdiction_user')->where(array('openid' => $openid, 'user_type' => 4))->find();
if ($find) {
$this->assign('msg', '该微信已绑定店长:' . $find['name']);
} else {
$userInfo = M('jurisdiction_user')->where(array('id' => $info['user_id']))->find();
$userInfo['type'] = '店长';
$this->assign('userInfo', $userInfo);
}
break;
case '5':
//导购
$find = M('jurisdiction_user')->where(array('openid' => $openid, 'user_type' => 5))->find();
if ($find) {
$this->assign('msg', '该微信已绑定导购员:' . $find['name']);
} else {
$userInfo = M('jurisdiction_user')->where(array('id' => $info['user_id']))->field('name')->find();
$userInfo['type'] = '导购员';
$this->assign('userInfo', $userInfo);
}
break;
}
$this->assign('token', $token);
} else {
$this->assign('msg', '授权码已过期');
}
}
$this->display();
}
/* 绑定操作 */
public function checkUser()
{
$openid = I('session.openid');
$token = I('post.token');
$map['start_time'] = array('lt', time());
$map['end_time'] = array('gt', time());
$map['token'] = $token;
$map['openid'] = $openid;
$map['status'] = 1;
$info = M('auth_token')->where($map)->find();
if (!empty($info)) {
$up['openid'] = $openid;
$jurChange = D('JurisdictionUser')->updateUser(array('id' => $info['user_id']), $up);
$save = M('auth_token')->where(array('id' => $info['id']))->save(array('openid' => $openid, 'status' => 2));
if ($save) {
M('auth_token')->where(array('id' => $info['id']))->save(array('openid' => $openid, 'status' => 2));
$res['status'] = 1;
$res['msg'] = '绑定成功';
} else {
$res['status'] = 0;
$res['msg'] = '绑定失败,请重试';
}
} else {
$res['status'] = 0;
$res['msg'] = '授权码过期';
}
$this->ajaxReturn($res);
}
public function cancelAuth()
{
if (IS_POST) {
$openid = session('openid');
$token = I('post.token');
if (!empty($openid) && !empty($token)) {
$update = M('auth_token')->where(array('token' => $token, 'openid' => $openid, 'status' => 1))->save(array('status' => 3));
if ($update) {
$res['status'] = 1;
$res['msg'] = '取消成功';
} else {
$res['status'] = 0;
$res['msg'] = '取消失败,请重试';
}
} else {
$res['status'] = 0;
$res['msg'] = '缺少参数';
}
$this->ajaxReturn($res);
}
}
/* 扫码登陆页 */
public function scan_login($token)
{
if ($token) {
M('login_token')->where(array('token' => $token))->setField('status', 2);
$openid = I('session.openid');
$arr = array();
/* ls_jurisdiction_user表所有用户 */
$allUser = M('jurisdiction_user')->where(array('openid' => $openid, 'status' => 1, 'state' => 1, 'user_type' => ['neq', 6]))->select();
foreach ($allUser as $v) {
$time = time();
if ($v['user_type'] == 0) {
//自建login_name
$selfUser = $this->selfUser($v);
if ($selfUser['status'] == 1) {
array_push($arr, $selfUser['data']);
}
} elseif ($v['user_type'] == 1) {
//合伙人
$data['id'] = $v['id'];
$data['name'] = $v['name'];
array_push($arr, $data);
} elseif ($v['user_type'] == 2) {
//代理商
$agent = M('retail_agent')->where(array('id' => $v['system_user_id']))->find();
if ($agent['begin_time'] < $time && $time < $agent['end_time']) {
$data['id'] = $v['id'];
$data['name'] = $v['name'];
array_push($arr, $data);
}
} elseif ($v['user_type'] == 3) {
//商户
$merchant = M('retail_merchant')->where(array('id' => $v['system_user_id']))->find();
if ($merchant['begin_time'] < $time && $time < $merchant['end_time']) {
$data['id'] = $v['id'];
$data['name'] = $v['name'];
array_push($arr, $data);
}
} elseif ($v['user_type'] == 4) {
//门店
$manager = M('retail_manager')->where(array('id' => $v['system_user_id']))->find();
$merchant = M('retail_merchant')->where(array('id' => $manager['merchantid']))->find(); //商户是否过期
if ($merchant['begin_time'] < $time && $time < $merchant['end_time']) {
$store = M('retail_store')->where(array('id' => $manager['storeid']))->find(); //门店是否删除
if ($store['state'] == 0) {
$data['id'] = $v['id'];
$data['name'] = $v['name'];
array_push($arr, $data);
}
}
} elseif ($v['user_type'] == 5) {
//导购员
$guide = M('retail_guide')->where(array('id' => $v['system_user_id']))->find();
$merchant = M('retail_merchant')->where(array('id' => $guide['merchantid']))->find(); //商户是否过期
if ($merchant['begin_time'] < $time && $time < $merchant['end_time']) {
$store = M('retail_store')->where(array('id' => $guide['storeid']))->find(); //门店是否删除
if ($store['state'] == 0) {
$data['id'] = $v['id'];
$data['name'] = $v['name'];
array_push($arr, $data);
}
}
}
}
$this->assign('userList', json_encode($arr));
$this->assign('canScan', $arr ? 1 : 0);
$this->assign('token', $token);
};
$this->display();
}
/* 自建用户处理 */
public function selfUser($user)
{
$id = $user['pid'];
$parent = M('jurisdiction_user')->where(array('id' => $id))->find();
if ($parent['status'] == 0 || $parent['state'] == 0) {
$res['status'] = 0;
$res['msg'] = '该系统已被删除或禁用';
return $res;
}
$time = time();
if ($parent['user_type'] == 1) {
//合伙人的内部用户
$res['status'] = 1;
$data['id'] = $user['id'];
$data['name'] = $user['name'];
$res['data'] = $data;
return $res;
} elseif ($parent['user_type'] == 2) {
//代理商的内部用户
$agent = M('retail_agent')->where(array('id' => $parent['system_user_id']))->find();
if ($agent['begin_time'] < $time && $time < $agent['end_time']) {
$res['status'] = 1;
$data['id'] = $user['id'];
$data['name'] = $user['name'];
$res['data'] = $data;
return $res;
} else {
$res['status'] = 0;
$res['msg'] = '该代理商已过期';
return $res;
}
} elseif ($parent['user_type'] == 3) {
//商户的内部用户
$merchant = M('retail_merchant')->where(array('id' => $parent['system_user_id']))->find();
if ($merchant['begin_time'] < $time && $time < $merchant['end_time']) {
$res['status'] = 1;
$data['id'] = $user['id'];
$data['name'] = $user['name'];
$res['data'] = $data;
return $res;
} else {
$res['status'] = 0;
$res['msg'] = '该商户已过期';
return $res;
}
} elseif ($parent['user_type'] == 4) {
//门店的内部用户
$manager = M('retail_manager')->where(array('id' => $parent['system_user_id']))->find();
$merchant = M('retail_merchant')->where(array('id' => $manager['merchantid']))->find(); //商户是否过期
$store = M('retail_store')->where(array('id' => $manager['storeid']))->find(); //门店是否正常
if ($merchant['begin_time'] < $time && $time < $merchant['end_time']) {
if ($store['state'] == 0) {
$res['status'] = 1;
$data['id'] = $user['id'];
$data['name'] = $user['name'];
$res['data'] = $data;
return $res;
} else {
$res['status'] = 0;
$res['msg'] = '该门店已不存在';
return $res;
}
} else {
$res['status'] = 0;
$res['msg'] = '该商户已过期';
return $res;
}
}
}
/* 用户登陆 */
public function userLogin()
{
$openid = I('session.openid');
$jurId = I('post.jurId');
$token = I('post.token');
if (empty($openid) || empty($jurId) || empty($token)) {
$res['status'] = 0;
$res['msg'] = '授权超时';
$this->ajaxReturn($res);
}
$token_find = M('login_token')->where(array('token' => $token))->find();
if (!$token_find) {
$res['status'] = 0;
$res['msg'] = '验证错误';
$this->ajaxReturn($res);
}
if ($token_find['status'] == 5) {
$res['status'] = 0;
$res['msg'] = '验证过期';
$this->ajaxReturn($res);
} else if ($token_find['status'] == 2) {
$up['user_id'] = $jurId;
$up['openid'] = $openid;
$up['status'] = 3;
$token_up = M('login_token')->where(array('id' => $token_find['id']))->save($up); // 用户登陆
if ($token_up) {
$res['status'] = 1;
$res['msg'] = '登录成功';
} else {
$res['status'] = 0;
$res['msg'] = '登录失败';
}
} else {
$res['status'] = 0;
$res['msg'] = '验证错误';
}
$this->ajaxReturn($res);
}
/* 用户取消 */
public function user_call_off()
{
$token = I('post.token');
if (!$token) {
$res['msg'] = '验证失败,网页即将关闭';
$this->ajaxReturn($res);
}
$token_find = M('login_token')->where(array('token' => $token))->find();
if (!$token_find) {
$res['msg'] = '验证失败,网页即将关闭';
$this->ajaxReturn($res);
}
if ($token_find['status'] == 5) {
$res['msg'] = '验证过期,网页即将关闭';
$this->ajaxReturn($res);
} else if ($token_find['status'] == 2) {
$token_up = M('login_token')->where(array('id' => $token_find['id']))->setField('status', 4); // 用户登陆
if ($token_up) {
$res['msg'] = '取消成功,网页即将关闭';
} else {
$res['msg'] = '取消失败,网页即将关闭';
}
} else {
$res['msg'] = '验证失败,网页即将关闭';
}
$this->ajaxReturn($res);
}
/* 用户关闭网页 */
public function userClose()
{
$token = I('post.token');
if (!$token) {
$res['msg'] = '验证失败,网页即将关闭';
$this->ajaxReturn($res);
}
$token_find = M('login_token')->where(array('token' => $token))->find();
if (!$token_find) {
$res['msg'] = '验证失败,网页即将关闭';
$this->ajaxReturn($res);
}
if ($token_find['status'] == 5) {
$res['msg'] = '验证过期,网页即将关闭';
$this->ajaxReturn($res);
} else if ($token_find['status'] == 2) {
$token_up = M('login_token')->where(array('id' => $token_find['id']))->setField('status', 4); // 用户登陆
if ($token_up) {
$res['msg'] = '取消成功,网页即将关闭';
} else {
$res['msg'] = '取消失败,网页即将关闭';
}
} else {
$res['msg'] = '验证失败,网页即将关闭';
}
$this->ajaxReturn($res);
}
/**
* 微信扫码获取用户列表
*/
public function resetPassword()
{
$ApiRedis = new CacheHandle();
$token = I('get.token');
$code = $ApiRedis->getSingleCache('wx_reset_password_' . $token);
if ($code) {
$ApiRedis->addTimeCache('wx_reset_password_' . $token, 300, 2);
}
$this->display();
}
/**
* 微信扫码获取对应用户列表
*/
public function getWxUserList()
{
$ApiRedis = new CacheHandle();
$token = I('post.token');
$code = $ApiRedis->getSingleCache('wx_reset_password_' . $token);
if (!$code) {
$this->ajaxReturn(['status' => 0, 'msg' => '页面异常请刷新重载']);
}
$param['openId'] = session('openid');
$param['type'] = 2;
$CodeLogin = new \Retail\Business\CodeLogin();
$res = $CodeLogin->getLinkUser($param);
$this->ajaxReturn($res);
}
/**
* 微信扫码重置密码
*/
public function wxResetPassword()
{
if (IS_GET) {
$this->display();
} else {
$ApiRedis = new CacheHandle();
$token = I('post.token');
$code = $ApiRedis->getSingleCache('wx_reset_password_' . $token);
if (!$code) {
$this->ajaxReturn(['status' => 0, 'msg' => '二维码已过期,请刷新重试!']);
} else {
$param['token'] = $token;
$param['newPassWordFir'] = I('post.newPassWordFir');
$param['newPassWordSec'] = I('post.newPassWordSec');
$param['id'] = I('post.id');
$param['openid'] = session('openid');
$param['type'] = 2;
$CodeLogin = new \Retail\Business\CodeLogin();
$res = $CodeLogin->setNewPassWord($param);
$this->ajaxReturn($res);
}
}
}
/**
* 重置成功
*/
public function wxResetSuccess()
{
$this->display();
}
}