MenuController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace addons\Wechat\merchant\controllers;
  3. use Yii;
  4. use yii\data\Pagination;
  5. use addons\Wechat\common\models\Menu;
  6. use addons\Wechat\common\enums\MenuTypeEnum;
  7. use common\helpers\ResultHelper;
  8. /**
  9. * 自定义菜单
  10. *
  11. * Class MenuController
  12. * @package addons\Wechat\merchant\controllers
  13. * @author jianyan74 <751393839@qq.com>
  14. */
  15. class MenuController extends BaseController
  16. {
  17. /**
  18. * 自定义菜单首页
  19. *
  20. * @return string
  21. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  22. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  23. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  24. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  25. * @throws \Psr\SimpleCache\InvalidArgumentException
  26. * @throws \yii\web\UnprocessableEntityHttpException
  27. */
  28. public function actionIndex()
  29. {
  30. $type = Yii::$app->request->get('type', MenuTypeEnum::CUSTOM);
  31. $data = Menu::find()
  32. ->where(['type' => $type])
  33. ->andFilterWhere(['merchant_id' => $this->getMerchantId()]);;
  34. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
  35. $models = $data->offset($pages->offset)
  36. ->orderBy('status desc, id desc')
  37. ->limit($pages->limit)
  38. ->all();
  39. // 查询下菜单
  40. !$models && Yii::$app->services->base->getWechatError(Yii::$app->wechat->app->menu->current());
  41. return $this->render('index', [
  42. 'pages' => $pages,
  43. 'models' => $models,
  44. 'type' => $type,
  45. 'types' => MenuTypeEnum::getMap(),
  46. ]);
  47. }
  48. /**
  49. * 创建菜单
  50. *
  51. * @return array|string
  52. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  53. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  54. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  55. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  56. * @throws \Psr\SimpleCache\InvalidArgumentException
  57. * @throws \yii\web\UnprocessableEntityHttpException
  58. */
  59. public function actionEdit()
  60. {
  61. $request = Yii::$app->request;
  62. $id = $request->get('id');
  63. $type = $request->get('type');
  64. $model = $this->findModel($id);
  65. if (Yii::$app->request->isPost) {
  66. $postInfo = Yii::$app->request->post();
  67. $model = $this->findModel($postInfo['id']);
  68. $model->attributes = $postInfo;
  69. if (!isset($postInfo['list'])) {
  70. return ResultHelper::json(422, '请添加菜单');
  71. }
  72. try {
  73. Yii::$app->wechatService->menu->createSave($model, $postInfo['list']);
  74. return ResultHelper::json(200, "修改成功");
  75. } catch (\Exception $e) {
  76. return ResultHelper::json(422, $e->getMessage());
  77. }
  78. }
  79. return $this->render('edit', [
  80. 'model' => $model,
  81. 'menuTypes' => MenuTypeEnum::type(),
  82. 'type' => $type,
  83. 'fansTags' => Yii::$app->wechatService->fansTags->getList()
  84. ]);
  85. }
  86. /**
  87. * 删除菜单
  88. *
  89. * @param $id
  90. * @param $type
  91. * @return mixed
  92. * @throws \Throwable
  93. * @throws \yii\db\StaleObjectException
  94. */
  95. public function actionDelete($id, $type)
  96. {
  97. $model = $this->findModel($id);
  98. if ($model->delete()) {
  99. // 个性化菜单删除
  100. !empty($model['menu_id']) && Yii::$app->wechat->app->menu->delete($model['menu_id']);
  101. return $this->message("删除成功", $this->redirect(['index', 'type' => $type]));
  102. }
  103. return $this->message("删除失败", $this->redirect(['index', 'type' => $type]), 'error');
  104. }
  105. /**
  106. * 替换菜单为当前的菜单
  107. *
  108. * @param $id
  109. * @return mixed|\yii\web\Response
  110. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  111. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  112. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  113. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  114. * @throws \Psr\SimpleCache\InvalidArgumentException
  115. * @throws \yii\web\UnprocessableEntityHttpException
  116. */
  117. public function actionSave($id)
  118. {
  119. if ($id) {
  120. $model = $this->findModel($id);
  121. $model->save();
  122. // 创建微信菜单
  123. $createReturn = Yii::$app->wechat->app->menu->create($model->menu_data);
  124. // 解析微信接口是否报错
  125. if ($error = Yii::$app->services->base->getWechatError($createReturn, false)) {
  126. return $this->message($error, $this->redirect(['index']), 'error');
  127. }
  128. }
  129. return $this->redirect(['index']);
  130. }
  131. /**
  132. * 同步菜单
  133. *
  134. * @return array
  135. * @throws \Psr\SimpleCache\InvalidArgumentException
  136. */
  137. public function actionSync()
  138. {
  139. try {
  140. Yii::$app->wechatService->menu->sync();
  141. return ResultHelper::json(200, '同步菜单成功');
  142. } catch (\Exception $e) {
  143. return ResultHelper::json(422, $e->getMessage());
  144. }
  145. }
  146. /**
  147. * 返回模型
  148. *
  149. * @param $id
  150. * @return array|Menu|null|\yii\db\ActiveRecord
  151. */
  152. protected function findModel($id)
  153. {
  154. if (empty($id) || empty(($model = Menu::findOne(['id' => $id, 'merchant_id' => $this->getMerchantId()])))) {
  155. $model = new Menu;
  156. return $model->loadDefaultValues();
  157. }
  158. return $model;
  159. }
  160. }
粤ICP备19079148号