WechatMenuConfigForm.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\WechatConfig;
  9. use app\models\WechatMenuConfig;
  10. use EasyWeChat\Factory;
  11. use EasyWeChat\Kernel\Exceptions\HttpException;
  12. use yii\base\Model;
  13. use GuzzleHttp\Exception\RequestException;
  14. class WechatMenuConfigForm extends Model
  15. {
  16. public $id;
  17. public $name;
  18. public $type;
  19. public $parent_id;
  20. public $sort;
  21. // type in [0, 1]
  22. public $value;
  23. // type = 2
  24. public $appid;
  25. public $pagePath;
  26. // type = 3
  27. public $title;
  28. public $desc;
  29. public $pic;
  30. public $link;
  31. public $app;
  32. public function rules()
  33. {
  34. return [
  35. [['name', 'value', 'appid', 'pagePath', 'title', 'desc', 'pic', 'link'],
  36. 'string'],
  37. [['id', 'type', 'parent_id', 'sort'], 'integer'],
  38. ['value', 'default', 'value'=> ''],
  39. ];
  40. }
  41. public function __construct($config = [])
  42. {
  43. parent::__construct($config);
  44. $store_id = get_store_id();
  45. $app = null;
  46. $wechatConfig = WechatConfig::findOne(['store_id' => $store_id, 'is_delete' => 0, 'type' => 2]);
  47. if (!empty($wechatConfig) && $wechatConfig->app_id && $wechatConfig->app_secret) {
  48. $config = [
  49. 'app_id' => $wechatConfig->app_id,
  50. 'secret' => $wechatConfig->app_secret,
  51. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  52. 'response_type' => 'array',
  53. ];
  54. $app = Factory::officialAccount($config);
  55. }
  56. $this->app = $app;
  57. }
  58. public function getMenuList () {
  59. try {
  60. $store_id = get_store_id();
  61. $p_menus = WechatMenuConfig::find()->where(['store_id' => $store_id, 'parent_id' => 0, 'is_delete' => 0])->asArray()->all();
  62. $num = 0;
  63. $data = [];
  64. foreach ($p_menus as $key => $p_menu) {
  65. $p_menu['value'] = json_decode($p_menu['value'], true);
  66. $p_menu['type'] = (int)$p_menu['type'];
  67. $p_menu['created_at'] = date('Y-m-d H:i:s', $p_menu['created_at']);
  68. $p_menu['update_at'] = date('Y-m-d H:i:s', $p_menu['update_at']);
  69. $p_menu['parent_name'] = '';
  70. $p_menu['parent_id'] = (int)$p_menu['parent_id'];
  71. $id = $p_menu['id'];
  72. $data = array_merge($data, [
  73. $p_menu
  74. ]);
  75. ++$num;
  76. $s_menu = WechatMenuConfig::find()->where(['store_id' => $store_id, 'parent_id' => $id, 'is_delete' => 0])->asArray()->all();
  77. foreach ($s_menu as $item) {
  78. $item['value'] = json_decode($item['value'], true);
  79. $item['type'] = (int)$item['type'];
  80. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  81. $item['update_at'] = date('Y-m-d H:i:s', $item['update_at']);
  82. $item['parent_name'] = $p_menu['name'];
  83. $item['parent_id'] = (int)$item['parent_id'];
  84. $data = array_merge($data, [$item]);
  85. ++$num;
  86. }
  87. }
  88. return [
  89. 'code' => 0,
  90. 'msg' => '成功',
  91. 'data' => [
  92. 'list' => $data
  93. ]
  94. ];
  95. } catch (\Exception $e) {
  96. return [
  97. 'code' => 1,
  98. 'msg' => $e->getMessage()
  99. ];
  100. }
  101. }
  102. /**
  103. * 获取微信公众号菜单配置
  104. */
  105. public function setMenuConfig() {
  106. $t = \Yii::$app->db->beginTransaction();
  107. try {
  108. $store_id = get_store_id();
  109. $type = (int)$this->type;
  110. $name = $this->name;
  111. $appid = $this->appid;
  112. $pagePath = $this->pagePath;
  113. $title = $this->title;
  114. $desc = $this->desc;
  115. $pic = $this->pic;
  116. $link = $this->link;
  117. $value = $this->value;
  118. $parent_id = (int)$this->parent_id;
  119. $sort = (int)$this->sort;
  120. $id = $this->id;
  121. if (is_null($type) || !in_array($type, [0, 1, 2, 3])) {
  122. throw new \Exception('类型错误');
  123. }
  124. //修改菜单
  125. if ($id) {
  126. $wechat_menu = WechatMenuConfig::findOne(['store_id' => $store_id, 'id' => $id]);
  127. }else{
  128. $wechat_menu = new WechatMenuConfig();
  129. }
  130. if (in_array($type, [0, 1])) {
  131. if (trim($value) === '') {
  132. throw new \Exception('值未填写');
  133. }
  134. }
  135. //小程序
  136. if ($type === 2) {
  137. if (trim($appid) == '') {
  138. return [
  139. 'code' => 1,
  140. 'msg' => '小程序的appid不能为空',
  141. ];
  142. }
  143. if (trim($pagePath) == '') {
  144. return [
  145. 'code' => 1,
  146. 'msg' => '小程序的pagePath不能为空',
  147. ];
  148. }
  149. $value = json_encode([
  150. 'appid' => $appid,
  151. 'pagepath' => $pagePath,
  152. ]);
  153. }
  154. //图文
  155. if ($type === 3) {
  156. if (trim($title) == '') {
  157. return [
  158. 'code' => 1,
  159. 'msg' => '图文消息的标题不能为空',
  160. ];
  161. }
  162. if (trim($desc) == '') {
  163. return [
  164. 'code' => 1,
  165. 'msg' => '图文消息的描述不能为空',
  166. ];
  167. }
  168. if (trim($pic) == '') {
  169. return [
  170. 'code' => 1,
  171. 'msg' => '图文消息的图片不能为空',
  172. ];
  173. }
  174. if (trim($link) == '') {
  175. return [
  176. 'code' => 1,
  177. 'msg' => '图文消息的链接不能为空',
  178. ];
  179. }
  180. $value = json_encode([
  181. 'title' => $title,
  182. 'desc' => $desc,
  183. 'pic' => $pic,
  184. 'link' => $link,
  185. ]);
  186. }
  187. $wechat_menu->name = $name;
  188. $wechat_menu->parent_id = $parent_id;
  189. $wechat_menu->store_id = $store_id;
  190. $wechat_menu->value = $value;
  191. $wechat_menu->sort = $sort;
  192. $wechat_menu->type = $type;
  193. if (!$wechat_menu->save()) {
  194. throw new \Exception(json_encode($wechat_menu->errors));
  195. }
  196. $result = $this->updateMenu();
  197. if ($result['code'] === 0) {
  198. $t->commit();
  199. return [
  200. 'code' => 0,
  201. 'msg' => '设置成功'
  202. ];
  203. } else {
  204. throw new \Exception($result['msg']);
  205. }
  206. } catch (\Exception $e) {
  207. $t->rollBack();
  208. return [
  209. 'code' => 1,
  210. 'msg' => $e->getMessage()
  211. ];
  212. }
  213. }
  214. public function delMenu() {
  215. $t = \Yii::$app->db->beginTransaction();
  216. try {
  217. $id = $this->id;
  218. $store_id = get_store_id();
  219. $menu_config = WechatMenuConfig::findOne(['store_id' => $store_id, 'id' => $id, 'is_delete' => 0]);
  220. if (!$menu_config) {
  221. throw new \Exception('微信公众号菜单不存在或已经删除');
  222. }
  223. $sub = WechatMenuConfig::findOne(['store_id' => $store_id, 'parent_id' => $id]);
  224. if ($sub) {
  225. throw new \Exception('菜单名称“'.$menu_config->name.'”下还有子菜单,请先删除子菜单');
  226. }
  227. $menu_config->is_delete = 1;
  228. if (!$menu_config->save()) {
  229. throw new \Exception(json_encode($menu_config->errors));
  230. } else {
  231. $app = $this->app;
  232. $result = $app->menu->delete();
  233. if(intval($result['errcode']) === 0){
  234. $t->commit();
  235. return [
  236. 'code' => 0,
  237. 'msg' => '设置成功'
  238. ];
  239. }else{
  240. throw new \Exception($result['errmsg']);
  241. }
  242. }
  243. } catch (\Exception $e) {
  244. $t->rollBack();
  245. return [
  246. 'code' => 1,
  247. 'msg' => $e->getMessage(),
  248. ];
  249. }
  250. }
  251. public function getMenuInfo() {
  252. $id = $this->id;
  253. $store_id = get_store_id();
  254. $menu_config = WechatMenuConfig::find()->where(['store_id' => $store_id, 'id' => $id, 'is_delete' => 0])->asArray()->one();
  255. if ($menu_config) {
  256. $menu_config['parent_id'] = (int)$menu_config['parent_id'];
  257. $menu_config['type'] = (int)$menu_config['type'];
  258. $value = json_decode($menu_config['value'], true);
  259. $menu_config['appid'] = $value['appid'] ?: '';
  260. $menu_config['pagePath'] = $value['pagepath'] ?: '';
  261. $menu_config['title'] = $value['title'] ?: '';
  262. $menu_config['desc'] = $value['desc'] ?: '';
  263. $menu_config['pic'] = $value['pic'] ?: '';
  264. $menu_config['link'] = $value['link'] ?: '';
  265. }
  266. $ret = WechatMenuConfig::find()->where(['store_id' => $store_id, 'parent_id' => 0, 'is_delete' => 0])->orderBy('sort asc')->asArray()->all();
  267. return [
  268. 'code' => 0,
  269. 'msg' => '获取成功',
  270. 'data' => [
  271. 'info' => $menu_config ?: [
  272. 'id' => 0,
  273. 'name' => '',
  274. 'sort' => 0,
  275. 'value' => '',
  276. 'parent_id' => 0,
  277. 'type' => 0,
  278. 'appid' => '',
  279. 'pagePath' => '',
  280. 'title' => '',
  281. 'desc' => '',
  282. 'pic' => '',
  283. 'link' => ''
  284. ],
  285. 'parent_list' => $ret
  286. ]
  287. ];
  288. }
  289. public function updateMenu(){
  290. $store_id = get_store_id();
  291. $button = [];
  292. $ret = WechatMenuConfig::find()->where(['store_id' => $store_id, 'parent_id' => 0, 'is_delete' => 0])->orderBy('sort asc')->asArray()->all();
  293. if($ret){
  294. foreach($ret as $k=>$v){
  295. $button[$k]['name'] = $v['name'];
  296. $ret2 = WechatMenuConfig::find()->where(['store_id' => $store_id, 'parent_id' => $v['id'], 'is_delete' => 0])
  297. ->orderBy('id asc')->asArray()->all();
  298. if($ret2){
  299. foreach($ret2 as $kk=>$vv){
  300. $button[$k]['sub_button'][$kk]['name'] = $vv['name'];
  301. if(intval($vv['type']) === 0){
  302. // $vv['value'] = str_replace('{id}', $id, $vv['value']);
  303. $button[$k]['sub_button'][$kk]['url'] = $vv['value'];
  304. $button[$k]['sub_button'][$kk]['type'] = "view";
  305. }elseif(intval($vv['type']) === 1){
  306. $button[$k]['sub_button'][$kk]['key'] = $vv['value'];
  307. $button[$k]['sub_button'][$kk]['type'] = "click";
  308. } elseif (intval($vv['type']) === 2) {
  309. $miniprogram = json_decode($vv['value']);
  310. $button[$k]['sub_button'][$kk]['url'] = 'http://';
  311. $button[$k]['sub_button'][$kk]['type'] = "miniprogram";
  312. $button[$k]['sub_button'][$kk]['appid'] = $miniprogram->appid;
  313. $button[$k]['sub_button'][$kk]['pagepath'] = $miniprogram->pagepath;
  314. } elseif (intval($vv['type']) === 3) {
  315. $button[$k]['sub_button'][$kk]['key'] = 'news_' . $vv['id'];
  316. $button[$k]['sub_button'][$kk]['type'] = "click";
  317. }
  318. }
  319. }else{
  320. if(intval($v['type']) === 0){
  321. // $v['value'] = str_replace('{id}', $id, $v['value']);
  322. $button[$k]['url'] = $v['value'];
  323. $button[$k]['type'] = "view";
  324. }elseif(intval($v['type']) === 1){
  325. $button[$k]['key'] = $v['value'];
  326. $button[$k]['type'] = "click";
  327. } else if (intval($v['type']) === 2) {
  328. $miniprogram = \json_decode($v['value']);
  329. $button[$k]['url'] = 'http://';
  330. $button[$k]['type'] = "miniprogram";
  331. $button[$k]['appid'] = $miniprogram->appid;
  332. $button[$k]['pagepath'] = $miniprogram->pagepath;
  333. } else if (intval($v['type']) === 3) {
  334. $button[$k]['key'] = 'news_' . $v['id'];
  335. $button[$k]['type'] = "click";
  336. }
  337. }
  338. }
  339. }
  340. if ($this->app) {
  341. $app = $this->app;
  342. if (!empty($button)) {
  343. $res = $app->menu->create($button);
  344. if(intval($res['errcode']) === 0){
  345. return [
  346. 'code' => 0,
  347. 'msg' => '设置成功'
  348. ];
  349. }else{
  350. return [
  351. 'code' => 1,
  352. 'msg' => $res['errmsg']
  353. ];
  354. }
  355. }
  356. }
  357. return [
  358. 'code' => 1,
  359. 'msg' => '设置失败'
  360. ];
  361. }
  362. }