2023-11-21 15:14:59 +08:00

1 line
3.7 KiB
PHP

<?php
namespace app\applet\controller;
use app\applet\model\Applet as AppletModel;
use app\applet\model\Apply as ApplyModel;
use app\applet\model\Setting;
use hema\wechat\Driver;
use think\facade\View;
use think\facade\Session;
/**
* 微信小程序管理
*/
class Wxapp extends Controller
{
/**
* 小程序设置
*/
public function index()
{
$model = AppletModel::get($this->applet_id);
if ($this->request->isAjax()) {
if ($model->edit($this->postData('data'))){
$applet = AppletModel::get($this->applet_id);
Session::set('hema_applet.applet',$applet);
return $this->renderSuccess('更新成功');
}
$error = $model->getError() ?: '更新失败';
return $this->renderError($error);
}
$apply = [];
$infor = [];
$wx = new Driver;
$url = '';//获取授权页面地址
if(empty($model['app_id'])){
$apply = ApplyModel::getApply([
'apply_mode' => 10,
'applet_id' => $this->applet_id
]);//注册信息
$url = $wx->authUrl($this->applet_id,2);//获取授权页面地址
}elseif($model['status']['value'] == 1){
$config = Setting::getItem('wxopen',0);
$model['serve_domain'] = $config['api_domain'];//获取最新服务器域名列表
//获取已授权小程序设置信息
if(!$result = $wx->getInfor($this->applet_id)){
return $this->renderError($wx->getError());
}
$infor = $result;
}
return View::fetch('index', compact('model','infor','apply','url'));
}
/**
* 设置头像
*/
public function sethead()
{
$model = AppletModel::get($this->applet_id);
if ($this->request->isGet()) {
if($model){
return $this->renderSuccess('', '', compact('model'));
}
return $this->renderError('获取失败');
}
$data = $this->postData('data');
//判断是否更换了头像
if($data['head_img'] != $model['head_img']){
if(empty($data['head_img'])){
return $this->renderError('请选择一个头像图片');
}
$wx = new Driver;
//上传临时素材
if(!$media_id = $wx->upTempMaterial($data['head_img'],$this->applet_id)){
return $this->renderError($wx->getError());
}
//设置头像
if(!$wx->modifyHeadImage($media_id,$this->applet_id)){
return $this->renderError($wx->getError());
}
if (!$model->save($data)){
return $this->renderError('设置失败');
}
}
return $this->renderSuccess('设置成功', url('wxapp/index'));
}
/**
* 同步小程序信息
*/
public function synchronization($app_id)
{
$wx = new Driver;
//获取授权应用的帐号基本信息
if(!$result = $wx->getAppInfo($app_id)){
return $this->renderError($wx->getError());
}
$app = $result['authorizer_info'];
$applet = AppletModel::get($this->applet_id);
$api_domain = implode(';',$app['MiniProgramInfo']['network']['RequestDomain']);
$api_domain = str_replace('http://','',$api_domain);
$api_domain = str_replace('https://','',$api_domain);
$applet->save([
'app_name' => isset($app['nick_name'])?$app['nick_name']:'', //昵称
'head_img' => isset($app['head_img'])?$app['head_img']:'', //头像
'qrcode_url' => isset($app['qrcode_url'])?$app['qrcode_url']:'', //二维码
'user_name' => $app['user_name'], //原始ID
'principal_name' => $app['principal_name'], //主体名称
'signature' => isset($app['signature'])?$app['signature']:'', //账号介绍
'api_domain' => $api_domain, //账号介绍
]);
return $this->renderSuccess('同步成功', url('wxapp/index'));
}
}