Browse Source

feat:创建微信公众号模块,开发图片资源注册引用方法,开发微信公众号自定义菜单操作页面及操作方法

wechat_public_accounts
linyaostalker 5 years ago
parent
commit
3f20dd42fc
  1. 24
      backend/modules/wx_public_account/Module.php
  2. BIN
      backend/modules/wx_public_account/assets/img/wechat-top.png
  3. 65
      backend/modules/wx_public_account/assets/js/custom_menu.js
  4. 47
      backend/modules/wx_public_account/controllers/PublicAccountController.php
  5. 109
      backend/modules/wx_public_account/logic/PublicAccountManager.php
  6. 63
      backend/modules/wx_public_account/models/ars/WxPublicAccountConfig.php
  7. 17
      backend/modules/wx_public_account/views/public-account/custom_menu.php
  8. 22
      backend/modules/wx_public_account/web/WxPublicAccountAsset.php
  9. BIN
      backend/modules/wx_public_account/web/img/wechat-top.png

24
backend/modules/wx_public_account/Module.php

@ -0,0 +1,24 @@
<?php
namespace backend\modules\wechat_public_account;
/**
* shop module definition class
*/
class Module extends \yii\base\Module
{
/**
* {@inheritdoc}
*/
public $controllerNamespace = 'backend\modules\wechat_public_account\controllers';
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
// custom initialization code goes here
}
}

BIN
backend/modules/wx_public_account/assets/img/wechat-top.png

After

Width: 320  |  Height: 64  |  Size: 12 KiB

65
backend/modules/wx_public_account/assets/js/custom_menu.js
File diff suppressed because it is too large
View File

47
backend/modules/wx_public_account/controllers/PublicAccountController.php

@ -0,0 +1,47 @@
<?php
namespace backend\modules\wechat_public_account\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use backend\modules\shop\models\ars\Config;
use backend\modules\wechat_public_account\logic\PublicAccountManager;
/**
* PublicAccountController manage WeChat public account platform.
*/
class PublicAccountController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all AfterSale models.
* @return mixed
*/
public function actionCustomMenu()
{
$config = Config::find()->one();
if (Yii::$app->request->isPost) {
Yii::$app->response->format = 'json';
$data = Yii::$app->request->post('data');
}
return $this->render('custom_menu', [
'customPageList' => [],
'data' => $config->menu_setting ?: '[]'
]);
}
}

109
backend/modules/wx_public_account/logic/PublicAccountManager.php

@ -0,0 +1,109 @@
<?php
namespace backend\modules\wechat_public_account\logic;
use backend\modules\shop\models\ars\Config;
use Yii;
use yii\base\Exception;
use yii\web\ServerErrorHttpException;
use EasyWeChat\Factory;
class PublicAccountManager
{
public $response_type = 'array';
//自定义菜单类型
const TYPE_VIEW = 'view';//跳转网页
const TYPE_MINI_PROGRAM = 'miniprogram';//跳转小程序
const TYPE_CUSTOM_PAGE = 'customPage';//自定义页面
private static function _getEasyApp()
{
$configModel = Config::find()->one();
if (!$configModel || !$configModel->)
$config = [
'app_id' =>
];
}
/**
* @param $data
* @param Config|$configModel
* @throws ServerErrorHttpException
*/
public static function dealCustomMenuData($data, $configModel)
{
$tra = Yii::$app->db->beginTransaction();
try {
$configModel->menu_setting = json_encode($data);
if (!$configModel->save()) {
throw new Exception(self::dealSaveErrorsString($configModel->errors));
}
$button = self
} catch (Exception $e) {
throw new ServerErrorHttpException($e->getMessage());
}
}
public static function dealSaveErrorsString($arr): string
{
$data = [];
foreach ($arr as $k => $v) {
$data[] = implode(' ', $v);
}
return implode(' ', $data);
}
/**
* @param $data
* @param $ret
* @return mixed
* 处理前端数据,递归处理
*/
private static function dealData($data, $ret)
{
foreach ($data as $k => $button) {
$name = $button['title'];
$type = $button['content']['type'];
$value = $button['content']['value'];
$children = $button['children'] ?? [];
$ret[$k] = self::spliceData($type, $value, $name);
if ($children) {
$ret[$k]['sub_button'] = [];
$ret[$k]['sub_button'] = self::dealData($children, $ret[$k]['sub_button']);
}
}
return $ret;
}
/**
* @param $type
* @param $value
* @param $name
* @return mixed
* 拼接数据为微信菜单格式
*/
private static function spliceData($type, $value, $name)
{
$ret['name'] = $name;
if ($type) {
$ret['type'] = $type;
if ($type == self::TYPE_VIEW) {
$ret['url'] = $value;
} elseif ($type == self::TYPE_MINI_PROGRAM) {
$ret['appid'] = $value['appId'];
$ret['pagepath'] = $value['url'];
$ret['url'] = $value['spareWebUrl'];
} elseif ($type == self::TYPE_CUSTOM_PAGE) {
// $ret['type'] = self::TYPE_MINI_PROGRAM;
// $page = PageLayout::findOne($value);
// $ret['appid'] = Yii::$app->config->getConfig()->mini_program_appId;
// if ($page->is_home) {
// $ret['pagepath'] = Yii::$app->params['pageIndex'];
// } else {
// $ret['pagepath'] = Yii::$app->params['pageOther'] . $value;
// }
// $ret['url'] = Yii::$app->config->getConfig()->wx_domain_name . '#/' . $ret['pagepath'];
}
}
return $ret;
}
}

63
backend/modules/wx_public_account/models/ars/WxPublicAccountConfig.php

@ -0,0 +1,63 @@
<?php
namespace backend\modules\wx_public_account\models\ars;
use yii\behaviors\TimestampBehavior;
use yii\redis\ActiveRecord;
/**
* Class WxPayConfig
* @package backend\modules\shop\models\ars
* @property int $id
* @property int $user_id 用户id
* @property int $mch_id 商户号
* @property string $key 支付秘钥
* @property string $cert_path cert文件路径
* @property string $key_path key文件路径
*/
class WxPayConfig extends ActiveRecord
{
/**
* 主键 默认为 id
*
* @return array|string[]
*/
public static function primaryKey()
{
return ['id'];
}
/**
* 模型对应记录的属性列表
*
* @return array
*/
public function attributes()
{
return ['id', 'user_id', 'mch_id', 'key', 'cert_path', 'key_path', 'created_at', 'updated_at'];
}
/**
* @author linyao
* @email 602604991@qq.com
* @created Nov 8, 2019
*
* 行为存储创建时间和更新时间
*/
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => function () {
return time();
},
],
];
}
}

17
backend/modules/wx_public_account/views/public-account/custom_menu.php

@ -0,0 +1,17 @@
<?php
use backend\modules\wx_public_account\web\WxPublicAccountAsset;
/* @var $this yii\web\View */
/* @var $searchModel vendor\iron\coupon\src\models\searchs\ServicesDomainSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = '自定义菜单';
$this->params['breadcrumbs'][] = $this->title;
WxPublicAccountAsset::register($this);
?>
<script>
var customPageList = <?= json_encode($customPageList) ?>;
var data = <?= $data ?>;
</script>

22
backend/modules/wx_public_account/web/WxPublicAccountAsset.php

@ -0,0 +1,22 @@
<?php
namespace backend\modules\goods\web;
use yii\web\AssetBundle;
/**
* Main backend application asset bundle.
*/
class GoodsAttributeAsset extends AssetBundle
{
public $sourcePath = "@backend/modules/goods/assets";
public $css = [
];
public $js = [
// 'custom/sku.49a56a9198d9c3ec233c.js'
'custom/sku.52802ed907a316501cd7.js'
];
public $depends = [
'yii\web\YiiAsset',
];
}

BIN
backend/modules/wx_public_account/web/img/wechat-top.png

After

Width: 320  |  Height: 64  |  Size: 12 KiB

Loading…
Cancel
Save