微擎版禾匠小程序打包自动写siteinfo.js参数

禾匠商城官方版本,并不提供小程序后台打包,并不提供小程序前端代码。

经过一些列整理,共赢传播特地分享,并贴出代码,如下:

图片[1]-微擎版禾匠小程序打包自动写siteinfo.js参数-共赢传播官博 ||

修改文件路径:

addons/zjhj_mall/core/modules/mch/controllers/StoreController.php

//修改规格
public function actionAttrDelete()
{
$form = new AttrDeleteForm();
$form->attributes = \Yii::$app->request->get();
$form->store_id = $this->store->id;
return $form->save();
}

//小程序发布,793-875行代码
public function actionWxapp($branch = null)
{
if (\Yii::$app->request->isPost) {
$action = \Yii::$app->request->post('action');
$jump_appid_list = Option::get('jump_appid_list', $this->store->id, '', []);
if ($action == 'download') {
$this->_wxapp_write_api_file();
$download_url = $this->_wxapp_zip_dir();
return [
'code' => 0,
'msg' => 'success',
'data' => $download_url,
];
} elseif ($action == 'wxdev_tool_login') {
$form = new WxdevToolLoginForm();
$form->store_id = $this->store->id;
$form->acid = $this->store->acid;
$form->branch = $branch;
return $form->getResult();
} elseif ($action == 'wxdev_tool_preview') {
$form = new WxdevToolPreviewForm();
$form->store_id = $this->store->id;
$form->appid = $this->wechat->appId;
$form->branch = $branch;
$form->jump_appid_list = $jump_appid_list;
return $form->getResult();
} elseif ($action == 'wxdev_tool_upload') {
$form = new WxdevToolUploadForm();
$form->store_id = $this->store->id;
$form->appid = $this->wechat->appId;
$form->branch = $branch;
$form->jump_appid_list = $jump_appid_list;
return $form->getResult();
}
} else {
return $this->render('wxapp', [
'branch' => $branch,
'jump_appid_list' => Option::get('jump_appid_list', $this->store->id, '', []),
]);
}
}

//小程序发布(无多商户)
public function actionWxappNomch($branch = 'nomch')
{
return $this->actionWxapp($branch);
}

// 配置微信小程序跳转appid列表
public function actionJumpAppid()
{
if (\Yii::$app->request->isPost) {
$jump_appid_list = \Yii::$app->request->post('jump_appid_list', []);
$new_list = [];
foreach ($jump_appid_list as $item) {
$item = trim($item);
if (!$item || !is_string($item) || mb_strlen($item) > 32) {
continue;
}
if (count($new_list) >= 10) {
break;
}
$new_list[] = $item;
}
Option::set('jump_appid_list', $new_list, $this->store->id);
return [
'code' => 0,
'msg' => '保存成功。',
];
} else {
return [
'code' => 1,
'msg' => '非post请求。',
];
}
}

//获取小程序二维码
public function actionWxappQrcode()
{
if (\Yii::$app->request->isPost) {
$save_file = md5($this->wechat->appId . $this->wechat->appSecret) . '.png';
$save_dir = \Yii::$app->basePath . '/web/temp/' . $save_file;
$web_dir = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/temp/' . $save_file;
if (file_exists($save_dir)) {
return [
'code' => 0,
'msg' => 'success',
'data' => $web_dir,
];
}
$access_token = $this->wechat->getAccessToken();
$api = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$access_token}";
$data = \Yii::$app->serializer->encode([
'scene' => '0',
'path' => '/pages/index/index',
'width' => 480,
]);
$this->wechat->curl->post($api, $data);
if (in_array('Content-Type: image/jpeg', $this->wechat->curl->response_headers)) {
FileHelper::filePutContents($save_dir, $this->wechat->curl->response);
return [
'code' => 0,
'msg' => 'success',
'data' => $web_dir,
];
} else {
return [
'code' => 1,
'msg' => '获取小程序码失败',
];
}
} else {
return [
'code' => 1,
];
}
}

//1.设置api.js文件
private function _wxapp_write_api_file()
{
$_acid = $this->store->acid; //意思是从数据库获取 ACID 的值
$app_root = str_replace('\\', '/', \Yii::$app->basePath) . '/';
$api_root = str_replace('http://', 'https://', \Yii::$app->request->hostInfo) . \Yii::$app->urlManager->scriptUrl . "?store_id={$this->store->id}&r=api/";
$api_siteroot = str_replace('http://', 'https://', \Yii::$app->request->hostInfo) . "/app/index.php";
$api_tpl_file = $app_root . 'wechatapp/siteinfo.tpl.js';
$api_file_content = file_get_contents($api_tpl_file);
//$api_file_content = str_replace('{$_api_root}', $api_siteroot, $api_file_content);
$api_file_content = str_replace(['{$_api_root}','{$_api_acid}'],[$api_siteroot,$_acid],$api_file_content);//替换两个,分别是小程序的 Acid 和 Siteroot,这里是很重要的
$api_file = $app_root . 'wechatapp/siteinfo.js';

FileHelper::filePutContents($api_file, $api_file_content);
}

//2.zip打包目录
private function _wxapp_zip_dir()
{
$app_root = str_replace('\\', '/', \Yii::$app->basePath) . '/';
$wxapp_root = $app_root . 'wechatapp';
$zip_name = 'wechatapp' . date('Ymd') . rand(1000, 9999) . '.zip';
FileHelper::mkDir($app_root . 'web/temp/');
$zip = Zip::create($app_root . 'web/temp/' . $zip_name);
$zip->add($wxapp_root);
return \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/temp/' . $zip_name;
}

//首页导航图标

修改后的文件已经分享到百度网盘

链接: https://pan.baidu.com/s/1_I0_ho992-n8G1DukDvEqQ 提取码: y2ue

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片