# Hello Wechat 項目實戰
1、復制demo模塊,更改項目目錄名稱
> 注意:模塊命名規則為 模塊類型_模塊名稱,模塊類型為(we,wemini),如此項目是提供給微信公眾號的,則項目命名為 we_hellowechat
2、修改模塊下各文件命名空間,需要修改的文件為
```
/we_hellowechat/controller/WechatIndex.php
改為:namespace addons\we_hellowechat\controller
```
```
/we_hellowechat/controller/WechatAdmin.php
改為:namespace addons\we_hellowechat\controller
```
```
/we_hellowechat/controller/WechatMiniIndex.php
改為:namespace addons\we_hellowechat\controller
```
```
/we_hellowechat/controller/WechatMiniAdmin.php
改為:namespace addons\we_hellowechat\controller
```
```
/we_hellowechat/controller/WechatMiniAdmin.php
改為:namespace addons\we_hellowechat\controller
```
```
/we_hellowechat/Processor.php
改為:namespace addons\we_hellowechat
```
3、于 WechatIndex.php 中開發前臺業務~~~
```
namespace addons\we_hellowechat\controller;
use addon\WechatIndexBasic;
class WechatIndex extends WechatIndexBasic
{
public function entry()
{
//全局變量$_WE 可在 業務控制器介紹->前臺控制器中查看詳細數據結構
global $_WE;
//TODO...業務邏輯
//渲染前臺模板 默認前臺模板為default
return $this->fetch($this->addon_template.'/wechat_index/entry',[
]);
}
//某個頁面
public function helloWechat()
{
$name = '戴晨';
//渲染前臺模板 默認前臺模板為default,后端變量賦值于前端頁面需要在fetch第二個參數中數組傳遞
return $this->fetch($this->addon_template.'/wechat_index/hello_wechat',[
'name'=>$name
]);
}
//某個api接口 無需oauth授權 請與config目錄下config.php配置文件中做如下配置,屏蔽某路由進行oauth授權
/*
* return [
'white_oauth'=>[
'we_hellowechat-WechatIndex-apiGetUserInfo'
],
];
*/
public function apiGetWechatUserInfo()
{
// 對應原生 $_GET
$id = $this->request->get('id');
// 對應原生 $_POST
$id = $this->request->post('id');
// 對應原生 array_merage($_GET,$_POST,$_ ...建議用此方法)
$id = $this->request->param('id');
}
}
```
4、于 /we_hellowechat/view/defalut/wechat_index/hello_wechat.html 中開發前臺業務。
資源文件位置:/public/static/addons/we_hellowechat/default
資源文件類型文件夾:/css /images /js ....
資源文件引入:
```
引入模塊css
<link rel="stylesheet" href="{$module_static_path}/css/main.css">
```
```
引入模塊js
<script src="{$module_static_path}/js/main.js"></script>
```
```
引入第三方庫
<script src="{$module_static_path}/js/main.js"></script>
```
demo如下
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--引入模塊css-->
<link rel="stylesheet" href="{$sys['module_static_path']}/main.css">
<!--引入第三方類css-->
<link rel="stylesheet" href="{$sys['static_extend_path']}/main.css">
<!--引入公共css-->
<link rel="stylesheet" href="{$sys['static_common_path']}/main.css">
</head>
<body>
<h1>{$name} - hello Wechat</h1>
<!--引入公共js類庫-->
<script src="{$sys['static_common_path']}/jquery.min.js"></script>
<!--引入第三方類庫-->
<script src="{$sys['static_extend_path']/layui/layui.js"></script>
<!--引入模塊js-->
<script src="{$sys['module_static_path']}/main.js"></script>
</body>
</html>
```
5、模塊初始化,找吳飛寧進行后臺初始化,并對測試公眾號進行模塊授權,后面訪問需攜帶acid=xx的參數 acid為公眾號id
6、上傳至github,生產環境gitpull
6、測試
路由規則 :{模塊名(同模塊目錄)-WechatIndex(控制器)-entry(方法)}
http://host/addons/execute/we_hellowechat-WechatIndex-entry