311 lines
15 KiB
PHP
311 lines
15 KiB
PHP
<?php
|
||
/**
|
||
* SAAS应用系统 --- 十年开发经验汇集巨献!
|
||
* ==========================================================
|
||
* Copy right 2020-2050 成都众联思索科技有限公司,保留所有权利。
|
||
* ----------------------------------------------------------
|
||
* 官方网址: https://www.zoomtk.com
|
||
* 这不是自由软件!未经允许不得用于商业目或程序代码摘取及修改。
|
||
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布传播。
|
||
* 唯一发布渠道www.zoomtk.com;非官方渠道统一视为侵权行为。
|
||
* ==========================================================
|
||
*/
|
||
|
||
namespace addon\wxoplatform\shop\controller;
|
||
|
||
use addon\weapp\model\Weapp;
|
||
use addon\weapp\model\Config as WeappConfigModel;
|
||
use addon\wechat\model\Config as WechatConfigModel;
|
||
use addon\wxoplatform\model\OpenPlatform;
|
||
use addon\wxoplatform\model\Oplatform as OplatformModel;
|
||
use addon\wxoplatform\model\Config as ConfigModel;
|
||
use app\model\system\UniAccount;
|
||
use app\model\upload\Upload;
|
||
use app\shop\controller\BaseShop;
|
||
|
||
/**
|
||
* 微信开放平台基础功能
|
||
*/
|
||
class Oplatform extends BaseShop
|
||
{
|
||
|
||
//小程序类型
|
||
public $service_type = array(
|
||
0 => "小程序",
|
||
);
|
||
|
||
//小程序认证类型
|
||
public $verify_type = array(
|
||
-1 => "未认证",
|
||
0 => "微信认证",
|
||
);
|
||
|
||
/**
|
||
* 去授权
|
||
*/
|
||
public function auth()
|
||
{
|
||
$auth_type = input("auth_type", "wechat");//授权类型 wechat 公众号 weapp 小程序
|
||
$oplatform_model = new OplatformModel();
|
||
$callback = addon_url("wxoplatform://shop/oplatform/callback", ["auth_type" => $auth_type]);
|
||
$result = $oplatform_model->getPreAuthorizationUrl(["callback" => $callback]);
|
||
if ($result['code'] >= 0) {
|
||
$this->redirect($result['data']);
|
||
} else {
|
||
$this->error($result['message']);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 开放平台授权回调
|
||
*/
|
||
public function callback()
|
||
{
|
||
$auth_type = input("auth_type", "wechat");//授权类型 wechat 公众号 weapp 小程序
|
||
$oplatform_model = new OplatformModel();
|
||
$result = $oplatform_model->handleAuthorize();
|
||
$error = '';
|
||
$is_success = false;
|
||
if ($result["code"] >= 0) {
|
||
$arr = $result["data"]['authorization_info'];
|
||
$config_model = new ConfigModel();
|
||
$oplatform_result = $config_model->getOplatformConfig();
|
||
$oplatform_config = $oplatform_result["data"];
|
||
//拉取授权方账号信息
|
||
$data = array(
|
||
'appid' => $arr["authorizer_appid"] ?? '',//授权方 appid
|
||
'authorizer_appid' => $arr["authorizer_appid"] ?? '',//授权方 appid
|
||
'authorizer_access_token' => $arr["authorizer_access_token"] ?? '',//接口调用令牌(在授权的公众号/小程序具备 API 权限时,才有此返回值)
|
||
'authorizer_refresh_token' => $arr["authorizer_refresh_token"] ?? '',//刷新令牌(在授权的公众号具备API权限时,才有此返回值),刷新令牌主要用于第三方平台获取和刷新已授权用户的 authorizer_access_token。一旦丢失,只能让用户重新授权,才能再次拿到新的刷新令牌。用户重新授权后,之前的刷新令牌会失效
|
||
'is_authopen' => 1,//是否是开放平台授权,
|
||
'component_appid' => $oplatform_config['value']['appid']
|
||
);
|
||
$auth_info_result = $oplatform_model->getAuthorizer(["authorizer_appid" => $arr["authorizer_appid"]]);
|
||
$upload_model = new Upload($this->site_id);
|
||
if ($auth_info_result["code"] >= 0) {
|
||
if ($auth_type == "wechat") {//公众号授权
|
||
$wechat_model = new \addon\wechat\model\Wechat();
|
||
$auth_info = $auth_info_result["data"];
|
||
$authorizer_info = $auth_info["authorizer_info"];//授权信息
|
||
//拉取头像和二维码
|
||
$headimg = '';
|
||
$qrcode_path = '';
|
||
if(isset($authorizer_info["head_img"])){
|
||
$headimg_result = $upload_model->setPath("headimg/wechat/" . date("Ymd") . '/', 'qrcode_' . $this->site_id . '.png')->remotePull($authorizer_info["head_img"]);
|
||
if ($headimg_result["code"] >= 0) {
|
||
$headimg = $headimg_result["data"]["pic_path"];
|
||
}
|
||
}
|
||
if(isset($authorizer_info["qrcode_url"])){
|
||
$qrcode_url_result = $upload_model->setPath("qrcode/wechat/" . date("Ymd") . '/', 'qrcode_' . $this->site_id . '.png')->remotePull($authorizer_info["qrcode_url"]);
|
||
if ($qrcode_url_result["code"] >= 0) {
|
||
$qrcode_path = $qrcode_url_result["data"]["pic_path"];
|
||
}
|
||
}
|
||
$wechat_config_model = new WechatConfigModel();
|
||
$data["appsecret"] = '';//
|
||
$data["service_type"] = $authorizer_info["service_type_info"];//公众号类型
|
||
$data["verify_type"] = $authorizer_info["verify_type_info"];//公众号认证类型
|
||
$data["service_type_info"] = $wechat_model->service_type[$authorizer_info["service_type_info"]["id"]];//公众号类型
|
||
$data["verify_type_info"] = $wechat_model->verify_type[$authorizer_info["verify_type_info"]["id"]];//公众号认证类型
|
||
$data["wechat_name"] = $authorizer_info["nick_name"];//昵称
|
||
$data["nick_name"] = $authorizer_info["nick_name"];//昵称
|
||
$data["head_img"] = $headimg;//头像
|
||
$data["user_name"] = $authorizer_info["user_name"];//原始 ID
|
||
$data["wechat_original"] = $authorizer_info["user_name"];//原始 ID
|
||
$data["principal_name"] = $authorizer_info["principal_name"];//主体名称
|
||
$data["alias"] = $authorizer_info["alias"];//公众号所设置的微信号,可能为空
|
||
$data["business_info"] = $authorizer_info["business_info"];//功能开通情况
|
||
$data["qrcode"] = $qrcode_path;//二维码
|
||
$res = $wechat_config_model->setWechatConfig($data, 1, $this->site_id);
|
||
if ($res["code"] >= 0) {
|
||
//成功
|
||
$is_success = true;
|
||
$this->assign('name', $authorizer_info["nick_name"]);
|
||
}
|
||
} else {
|
||
$weapp_config_model = new WeappConfigModel();
|
||
$auth_info = $auth_info_result["data"];
|
||
$authorizer_info = $auth_info["authorizer_info"];//授权信息
|
||
//拉取头像和二维码
|
||
$headimg_result = $upload_model->setPath("headimg/weapp/" . date("Ymd") . '/')->remotePull($authorizer_info["head_img"]);
|
||
$headimg = '';
|
||
if ($headimg_result["code"] >= 0) {
|
||
$headimg = $headimg_result["data"]["pic_path"];
|
||
}
|
||
$qrcode_url_result = $upload_model->setPath("qrcode/weapp/" . date("Ymd") . '/')->remotePull($authorizer_info["qrcode_url"]);
|
||
$qrcode_path = '';
|
||
if ($qrcode_url_result["code"] >= 0) {
|
||
$qrcode_path = $qrcode_url_result["data"]["pic_path"];
|
||
}
|
||
$data["appsecret"] = '';//
|
||
$data["service_type_info"] = $this->service_type[$authorizer_info["service_type_info"]["id"]];//小程序类型
|
||
$data["verify_type_info"] = $this->verify_type[$authorizer_info["verify_type_info"]["id"]];//小程序认证类型
|
||
$data["weapp_name"] = $authorizer_info["nick_name"];//昵称
|
||
$data["head_img"] = $headimg;//头像
|
||
$data["weapp_original"] = $authorizer_info["user_name"];//原始 ID
|
||
$data["user_name"] = $authorizer_info["user_name"];//原始 ID
|
||
$data["principal_name"] = $authorizer_info["principal_name"];//主体名称
|
||
$data["business_info"] = $authorizer_info["business_info"]; //功能开通情况
|
||
$data["qrcode"] = $qrcode_path;//二维码
|
||
$data["signature"] = $authorizer_info["signature"];//帐号介绍
|
||
$data["miniprograminfo"] = $authorizer_info["MiniProgramInfo"];//小程序配置,根据这个字段判断是否为小程序类型授权
|
||
//配置小程序合法域名
|
||
$domain = $oplatform_config['value'];
|
||
$network = array(
|
||
'RequestDomain' => explode(';', $domain['request_url']),
|
||
'WsRequestDomain' => explode(';', $domain['socket_url']),
|
||
'UploadDomain' => explode(';', $domain['upload_url']),
|
||
'DownloadDomain' => explode(';', $domain['download_url']),
|
||
);
|
||
$data['miniprograminfo']['network'] = $network;
|
||
$config = new UniAccount();
|
||
$condition=[
|
||
'appid'=>$data['appid'],
|
||
'site_id'=>$this->site_id,
|
||
];
|
||
$config->setUniConfig($condition,$data,'weapp','WEAPP_CONFIG',$data["weapp_original"]);
|
||
$weapp_config_model->setWeappConfig($data, 1, $this->site_id);//设置系统配置//后期移除
|
||
$open_platform_model = new OpenPlatform($this->site_id);
|
||
$Domain = array(
|
||
'action' => 'set',
|
||
'requestdomain' => $network['RequestDomain'],
|
||
'wsrequestdomain' => $network['WsRequestDomain'],
|
||
'uploaddomain' => $network['UploadDomain'],
|
||
'downloaddomain' => $network['DownloadDomain'],
|
||
);
|
||
$open_platform_model->modifyDomain($Domain);//设置域名
|
||
$is_success=true;
|
||
$this->assign('name', $authorizer_info["nick_name"]);
|
||
$data['site_id']=$this->site_id;
|
||
event('WeappAuth',$data);
|
||
}
|
||
if ($auth_info_result["data"]["authorizer_info"]["verify_type_info"]["id"] == -1) {
|
||
// 查询授权是否已绑定
|
||
$error='您的主体未认证部分功能使用会被限制!';
|
||
}
|
||
}
|
||
} else {
|
||
$error = $result['message'];
|
||
}
|
||
$type_name = $auth_type == 'wechat' ? '公众号' : '小程序';
|
||
$this->assign('type_name', $type_name);
|
||
$this->assign("error", $error);//错误信息
|
||
$this->assign("is_success", $is_success);//成功或失败
|
||
//通知展示页
|
||
return $this->fetch('oplatform/notify');
|
||
}
|
||
|
||
/**
|
||
* 微信公众号配置
|
||
*/
|
||
public function wechat()
|
||
{
|
||
$wechat_config_model = new WechatConfigModel();
|
||
$config_result = $wechat_config_model->getWechatConfig($this->site_id);
|
||
$this->assign("config", $config_result["data"]);
|
||
return $this->fetch('oplatform/wechat');
|
||
}
|
||
|
||
/**
|
||
* 微信公众号授权绑定
|
||
*/
|
||
public function wechatSettled()
|
||
{
|
||
if (request()->isAjax()) {
|
||
$wechat_config_model = new WechatConfigModel();
|
||
$config_result = $wechat_config_model->getWechatConfig($this->site_id);
|
||
$config = $config_result["data"];
|
||
if (!empty($config["value"])) {
|
||
return $wechat_config_model->success();
|
||
} else {
|
||
return $wechat_config_model->error();
|
||
}
|
||
} else {
|
||
return $this->fetch('oplatform/wechat_settled');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 微信小程序配置
|
||
*/
|
||
public function weapp()
|
||
{
|
||
$weapp_config_model = new WeappConfigModel();
|
||
$config_result = $weapp_config_model->getWeappConfig($this->site_id);
|
||
if (request()->isAjax()) {
|
||
$RequestDomain = input('RequestDomain', []);
|
||
$WsRequestDomain = input('WsRequestDomain', []);
|
||
$UploadDomain = input('UploadDomain', []);
|
||
$DownloadDomain = input('DownloadDomain', []);
|
||
$value = $config_result['data']['value'] ?? [];
|
||
$value['appsecret'] = input('appsecret', '');
|
||
$network = array(
|
||
'RequestDomain' => $RequestDomain,
|
||
'WsRequestDomain' => $WsRequestDomain,
|
||
'UploadDomain' => $UploadDomain,
|
||
'DownloadDomain' => $DownloadDomain,
|
||
);
|
||
$value['miniprograminfo']['network'] = $network;
|
||
$res = $weapp_config_model->setWeAppConfig($value, 1, $this->site_id);
|
||
$open_platform_model = new OpenPlatform($this->site_id);
|
||
$data = array(
|
||
'action' => 'set',
|
||
'requestdomain' => $network['RequestDomain'],
|
||
'wsrequestdomain' => $network['WsRequestDomain'],
|
||
'uploaddomain' => $network['UploadDomain'],
|
||
'downloaddomain' => $network['DownloadDomain'],
|
||
);
|
||
$result = $open_platform_model->modifyDomain($data);
|
||
return $result;
|
||
} else {
|
||
$this->assign("config", $config_result["data"]);
|
||
return $this->fetch('oplatform/weapp');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 微信小程序授权绑定
|
||
*/
|
||
public function weappSettled()
|
||
{
|
||
if (request()->isAjax()) {
|
||
$weapp_config_model = new WeappConfigModel();
|
||
$config_result = $weapp_config_model->getWeappConfig($this->site_id);
|
||
$config = $config_result["data"];
|
||
if (!empty($config["value"])) {
|
||
return $weapp_config_model->success();
|
||
} else {
|
||
return $weapp_config_model->error();
|
||
}
|
||
} else {
|
||
return $this->fetch('oplatform/weapp_settled');
|
||
}
|
||
}
|
||
|
||
public function notify()
|
||
{
|
||
$this->assign("is_success", 0);//成功或失败
|
||
return $this->fetch('oplatform/notify');
|
||
}
|
||
|
||
public function bingWxOpen()
|
||
{
|
||
$appid = input('appid');
|
||
$return_url = input('return_url', '');
|
||
$oplatform_model = new OplatformModel();
|
||
$callback = addon_url("wxoplatform://shop/oplatform/setWxOpen", ["appid" => $appid]);
|
||
$optional = [
|
||
'category_id_list' => '',
|
||
'category_id_list' => '',
|
||
];
|
||
$result = $oplatform_model->getPreAuthorizationUrl(["callback" => $callback]);
|
||
}
|
||
|
||
|
||
public function setWxOpen()
|
||
{
|
||
$appid = input('appid');
|
||
}
|
||
}
|