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

486 行
18 KiB
PHP
可执行文件

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace Retail\Controller;
use Common\Model\AppweixinModel;
use Common\Model\WxappinfoModel;
use Common\Model\WxconfigModel;
//[1.8.67]
/**
* 设置 单个商户的公众号配置信息
* @author ocean
*/
class WxconfigController extends BaseController
{
/**
* 上传微信缩略图 素材上传接口
*/
public function upWxImg($url)
{
$config = $this->getWxconfig();
$wechat = new \Util\Wx\Wechat($config);
$data['media'] = '@' . getcwd() . $url;
$type = 'image';
$re = $wechat->uploadForeverMedia($data, $type);
return $re['media_id'];
}
/**
* 获取配置参数
* @return mixed
*/
public function getWxconfig()
{
$WxconfigModel = new WxconfigModel();
$config = $WxconfigModel->getFind(['merchantid' => $this->merchant_id]);
// 判断用户选择微信授权方式
if ($config['type'] == 1 || $config['type'] == 0) {
$config['appid'] = $config['appid'];
$config['appsercet'] = $config['appsercet'];
$config['token'] = $config['token'];
} else if ($config['type'] == 2) {
$config['appid'] = $config['authorizer_appid'];
$config['access_token'] = get_authorizer_access_token($config['merchantid']);
}
return $config;
}
/**
* 公众号去授权跳转前页面
*/
public function authindex()
{
$AppweixinModel = new AppweixinModel();
$appid = $AppweixinModel->getFindField(['id' => 1], 'appid');
$access = get_acc_token();
$urly = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=' . $access;
$dat = array(
'component_appid' => $appid
);
$postdata = json_encode($dat);
$responsey = curl_get($urly, $postdata);
$resulty = json_decode($responsey, true);
//插入数据库
$data['component_access_token'] = $access;
$data['pre_auth_code'] = $resulty['pre_auth_code'];
//点击授权
$pre_auth_code = $resulty['pre_auth_code'];
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
|| (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
$redirect_uri = $http_type . C("adminhttp") . '/index.php/Retail/Wxconfig/authSuccess';
$urls = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=' . $appid . '&auth_type=1&pre_auth_code=' . $pre_auth_code . '&redirect_uri=' . $redirect_uri;
header('location:' . $urls);
}
/**
* 公众号授权成功处理
*/
public function authSuccess()
{
if ($_GET['auth_code']) {
$AppweixinModel = new AppweixinModel();
$appid = $AppweixinModel->getFindField(['id' => 1], 'appid');
$access = get_acc_token();
//获取第三方接口调用凭据
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=' . $access;
$data = array(
'component_appid' => $appid,
'authorization_code' => $_GET['auth_code']
);
$data = json_encode($data);
$response = curl_get($url, $data);
$result = json_decode($response, true);
$da['authorizer_appid'] = $result['authorization_info']['authorizer_appid'];
$da['authorizer_access_token'] = $result['authorization_info']['authorizer_access_token'];
$da['authorizer_refresh_token'] = $result['authorization_info']['authorizer_refresh_token'];
//type 2 是授权的appid
$da['type'] = 2;
$da['access_token_time'] = time();
// 判断当前用户是否授权或者设置过授权信息
//绑定验重
$repeatMap = [
[
'appid' => $da['authorizer_appid'],
'authorizer_appid' => $da['authorizer_appid'],
'_logic' => 'OR',
],
'merchantid' => ['neq', $this->merchant_id],
];
$WxconfigModel = new WxconfigModel();
$repeat = $WxconfigModel->getFind($repeatMap);
if ($repeat) {
$this->assign('status', 2);
$this->display();
exit;
}
$aut_count = $WxconfigModel->getCount(['merchantid' => $this->merchant_id]);
if ($aut_count == 0) {
//添加一个微信的配置信息
$da['merchantid'] = $this->merchant_id;
$apparray['appid'] = $da['authorizer_appid'];
$apparray['authorizer_appid'] = $da['authorizer_appid'];
$apparray['_Logic'] = "or";
$WxconfigModel->addOrder($da);
$this->get_authorizer_info();
} else {
$WxconfigModel = new WxconfigModel();
$WxconfigModel->updateOrder(['merchantid' => $this->merchant_id], $da);
$this->get_authorizer_info();
}
$this->assign('status', 1);
} else {
$this->assign('status', 0);
}
$this->display();
}
/**
* 公众号配置页面
*/
public function wxconfig()
{
//获取当前的微信的配置的信息
$WxconfigModel = new WxconfigModel();
$conf = $WxconfigModel->getFind(['merchantid' => $this->merchant_id]);
$this->assign('wxconfig', $conf);
$arr = array(
'authindex' => '/index.php/Retail/Wxconfig/authindex',
'index' => '/index.php/Retail/Wxconfig/index',
);
$authArr = checkFunction($arr);
$this->assign('authArr', $authArr);
$this->display();
}
public function get_authorizer_info()
{
$AppweixinModel = new AppweixinModel();
$appid = $AppweixinModel->getFindField(['id' => 1], 'appid');
$map = [
'merchantid' => $this->merchant_id
];
$WxconfigModel = new WxconfigModel();
$authorizer_appid = $WxconfigModel->getFindField($map, 'authorizer_appid');
$access = get_acc_token();
// 获取授权方的账号基本信息
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=' . $access;
$data = array(
'component_appid' => $appid,
'authorizer_appid' => $authorizer_appid
);
$data = json_encode($data);
$response = curl_get($url, $data);
$result = json_decode($response, true);
// 插入数据库
$info['name'] = $result['authorizer_info']['nick_name'];
$info['head_img'] = $result['authorizer_info']['head_img'];
$info['service_type_info'] = $result['authorizer_info']['service_type_info']['id'];
$info['user_name'] = $result['authorizer_info']['user_name'];
$info['signature'] = $result['authorizer_info']['signature'];
$info['principal_name'] = $result['authorizer_info']['principal_name'];
$info['qrcode_url'] = $result['authorizer_info']['qrcode_url'];
$info['authorizer_appid'] = $result['authorization_info']['authorizer_appid'];
$WxconfigModel->updateOrder($map, $info);
}
/*
* 显示修改页面信息
*/
public function index()
{
//显示页面
$WxconfigModel = new WxconfigModel();
$cache = $WxconfigModel->getFind(['merchantid' => $this->merchant_id, 'type' => 1]);
$this->assign('cache', $cache);
$this->display();
}
//上传文件
public function upload()
{
ajax_upload('/Uploads/');
}
public function getwxImg()
{
$media_id = I('post.id');
$config = $this->getWxconfig();
$wechat = new \Util\Wx\Wechat($config);
$imgdata = $wechat->getForeverMedias($media_id);
$url = "./Uploads/Wx/" . $media_id . ".jpg";
file_put_contents($url, $imgdata);
if ($imgdata) {
$data['url'] = "/" . $url;
$data['status'] = 1;
} else {
$data['status'] = 0;
}
$this->ajaxReturn($data);
}
/*
* 修改微信配置信息
*/
public function wxupdate()
{
$data = $_POST;
if (!empty($data)) {
$WxconfigModel = new WxconfigModel();
$info = $WxconfigModel->getFind(['merchantid' => $this->merchant_id]);
$datas['post'] = I('post.');
if (empty($info)) {
$item['merchantid'] = $this->merchant_id;
$item['appid'] = I('post.appid');
$item['appsecret'] = I('post.appsecret');
$item['name'] = I('post.name');
$item['addtime'] = time();
$item['token'] = I('post.token');
$item['wxjssrc'] = I('post.wxjssrc');
$item['status'] = 1;
$item['type'] = 1;
$ret = $WxconfigModel->addOrder($item);
if ($ret) {
$datas['status'] = 1;
$datas['msg'] = "修改成功";
//操作日志记录
$source = 0;//请求来源: 0pc系统 1小助手
$type = 3;//平台:1合伙人端 2代理商端 3商户端 4门店端
$request_data = json_encode($_POST);//请求参数 json数据
$desc = '修改微信配置信息';//操作行为描述
action_log($source, $type, $request_data, $desc);
//end
} else {
$datas['status'] = 0;
$datas['msg'] = "添加失败,请检查数据";
}
} else {
$item['appid'] = I('post.appid');
$item['appsecret'] = I('post.appsecret');
$item['name'] = I('post.name');
$item['addtime'] = time();
$item['token'] = I('post.token');
$item['status'] = 1;
$item['type'] = 1;
$WxconfigModel = new WxconfigModel();
$ret = $WxconfigModel->updateOrder(['merchantid' => $this->merchant_id], $item);
if ($ret) {
$datas['status'] = 1;
$datas['msg'] = "修改成功";
} else {
$datas['status'] = 0;
$datas['msg'] = "修改失败,请检查数据";
}
}
} else {
$datas['status'] = 0;
$datas['msg'] = "修改失败,请检查数据";
}
$this->ajaxReturn($datas);
}
/*
* 重置用户的微信信息
*/
public function reset()
{
$WxconfigModel = new WxconfigModel();
$res = $WxconfigModel->deleteOrder(['merchantid' => $this->merchant_id]);
if ($res) {
$data['status'] = 1;
//操作日志记录
$source = 0;//请求来源: 0pc系统 1小助手
$type = 3;//平台:1合伙人端 2代理商端 3商户端 4门店端
$request_data = json_encode($_POST);//请求参数 json数据
$desc = '重置公众号信息';//操作行为描述
action_log($source, $type, $request_data, $desc);
//end
} else {
$data['status'] = 0;
}
$this->ajaxReturn($data);
}
/**
* getMenu 获取微信菜单
*/
public function getMenu()
{
$WxconfigModel = new WxconfigModel();
$merConf = $WxconfigModel->getFind(['merchantid' => $this->merchant_id]);
// 判断用户选择微信授权方式
if ($merConf['type'] == 1 || $merConf['type'] == 0) {
$config['appid'] = $merConf['appid'];
$config['appsecret'] = $merConf['appsecret'];
$config['token'] = $merConf['token'];
} else if ($merConf['type'] == 2) {
$config['appid'] = $merConf['authorizer_appid'];
$config['access_token'] = get_authorizer_access_token($merConf['merchantid']);
} else {
$this->ajaxReturn(callBack(statusCode()['error'], '未设置公众号信息'));
}
$weChat = new \Util\Wx\Wechat($config);
$menu = $weChat->getMenu();
$this->ajaxReturn(callBack(statusCode()['success'], '获取成功', $menu ? $menu['menu']['button'] : []));
}
/**
* 更新微信菜单
*/
public function upMenu()
{
//上传菜单信息
$WxappinfoModel = new WxappinfoModel();
$config = $WxappinfoModel->getFind(['merchant_id' => $this->merchant_id]);
if (IS_POST) {
$menu = $_POST['data'];
//处理菜单中未上传到微信服务器上图片
foreach ($menu as &$val) {
if ($val['type'] == 'media_id') {
if (isset($val['media_url'])) {
$val['media_id'] = $this->upWxImg($val['media_url']);
unset($val['media_url']);
}
} else if ($val['type'] == 'view_limited') {
unset($val['media_ids']);
} else if ($val['type'] == 'custom') {
switch ($val['index']) {
case 1:
$val['type'] = "miniprogram";
$val['url'] = C("shop_http");
$val['appid'] = $val['appid'] ? $val['appid'] : $config["authorizer_appid"];
$val['pagepath'] = $val['pagepath'] ? $val['pagepath'] : "pages/store/store";
break;
case 2:
$val['type'] = "view";
$val['url'] = $this->getplugurl("shop")["url"];
break;
}
}
if (!empty($val['sub_button'])) {
unset($val['type']);
unset($val['media_id']);
unset($val['key']);
foreach ($val['sub_button'] as &$vals) {
if ($vals['type'] == 'media_id') {
if (isset($vals['media_url'])) {
$vals['media_id'] = $this->upWxImg($vals['media_url']);
unset($vals['media_url']);
}
} else if ($vals['type'] == 'view_limited') {
unset($vals['media_ids']);
} else if ($vals['type'] == 'custom') {
switch ($vals['index']) {
case 1:
$vals['type'] = "miniprogram";
$vals['url'] = C("shop_http");
$vals['appid'] = $vals['appid'] ? $vals['appid'] : $config["authorizer_appid"];
$vals['pagepath'] = $vals['pagepath'] ? $vals['pagepath'] : "pages/store/store";
break;
case 2:
$vals['type'] = "view";
$vals['url'] = $this->getPlugUrl("shop")["url"];
break;
}
}
}
}
}
$WxconfigModel = new WxconfigModel();
$WxConfig = $WxconfigModel->getFind(['merchantid' => $this->merchant_id]);
// 判断用户选择微信授权方式
if ($WxConfig['type'] == 1 || $WxConfig['type'] == 0) {
$config['appid'] = $WxConfig['appid'];
$config['appsecret'] = $WxConfig['appsecret'];
$config['token'] = $WxConfig['token'];
} else if ($WxConfig['type'] == 2) {
$config['appid'] = $WxConfig['authorizer_appid'];
$config['access_token'] = get_authorizer_access_token($WxConfig['merchantid']);
}
$wechat = new \Util\Wx\Wechat($config);
$button['button'] = $menu;
$re = $wechat->createMenu($button);
if ($re) {
$data['message'] = "添加成功";
$data['status'] = 1;
//操作日志记录
$source = 0;//请求来源: 0pc系统 1小助手
$type = 3;//平台:1合伙人端 2代理商端 3商户端 4门店端
$request_data = json_encode($_POST);//请求参数 json数据
$desc = '自定义菜单配置并发布';//操作行为描述
action_log($source, $type, $request_data, $desc);
//end
} else {
$data['message'] = "添加失败" . $wechat->errMsg;
$data['status'] = 0;
$data['button'] = $button;
}
} else {
$data['statuus'] = 0;
$data['message'] = "非法操作";
}
$this->ajaxReturn($data);
}
/*
* 微信自定义菜单页面展示
*/
public function menu()
{
//获取门店id
$WxconfigModel = new WxconfigModel();
$conf = $WxconfigModel->getFind(['merchantid' => $this->merchant_id], 'name');
// 判断用户选择微信授权方式
if (empty($conf)) {
echo '你暂未配置微信公众号,请配置公众号后添加自定义菜单信息';
exit;
}
$this->assign('merConf', $conf);
$this->display();
}
/**
* 获取插件路径
* @param $type
* @return mixed
*/
public function getPlugUrl($type)
{
switch ($type) {
//暂时只写会员卡和大转盘
case 'vip':
$url = C('shop_http') . "/index.php/Vip/Index/index.html?merchantid=" . $this->merchant_id;
break;
case 'takeFood':
$url = C('shop_http') . "/index.php/Delicacy/index/index/merchantid/" . $this->merchant_id;
break;
case 'shop':
$url = C('shop_http') . "/index.php/Vip/ScoreMall/index/merchantid/" . $this->merchant_id;
break;
}
$data['success'] = 1;
$data['url'] = $url;
return $data;
}
public function morejs()
{
$this->display();
}
}