|
@@ -10,6 +10,7 @@
|
|
|
namespace Modules\WechatPlatform\Services;
|
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
+use Modules\Channel\Services\WechatOpenPlatform\WechatOpenPlatformService;
|
|
|
use Modules\Common\Services\BaseService;
|
|
|
use Modules\WechatPlatform\Models\WechatAccountMenuDetail;
|
|
|
use Modules\WechatPlatform\Models\WechatMenu;
|
|
@@ -48,38 +49,73 @@ class WechatMenuService extends BaseService
|
|
|
if (is_empty($info)) {
|
|
|
self::throwErrMsg('该菜单不存在或已删除');
|
|
|
}
|
|
|
- $appIds = WechatAccountMenuDetail::where(['status' => 1, 'meun_id' => $info->id])->pluck('id')->toArray();
|
|
|
- WechatMenu::query()->where('id', $id)->update($param);
|
|
|
+ $appIds = WechatAccountMenuDetail::where(['status' => 1, 'menu_id' => $info->id])->pluck('id')->toArray();
|
|
|
+ WechatMenu::where('id', $id)->update($param);
|
|
|
$data = [
|
|
|
'type' => $param['type'],
|
|
|
'content' => $param['content'],
|
|
|
'msg_content' => $param['msg_content']
|
|
|
];
|
|
|
- WechatAccountMenuDetail::where('meun_id', $id)->update($data);
|
|
|
+ WechatAccountMenuDetail::where('menu_id', $id)->update($data);
|
|
|
// 更新公众号菜单
|
|
|
- self::createWechatMenus($appIds);
|
|
|
+ self::createWechatMenus($appIds,$id);
|
|
|
return ['msg' => '操作成功'];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 创建微信菜单
|
|
|
+ * 创建微信菜单
|
|
|
* name: createWechatMenus
|
|
|
* @param array $appIds
|
|
|
+ * @param $menuId
|
|
|
* @return bool
|
|
|
- * date 2023/07/12 07:16
|
|
|
+ * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
|
|
+ * @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
+ * date 2023/07/12 14:29
|
|
|
*/
|
|
|
- private static function createWechatMenus(array $appIds)
|
|
|
+ private static function createWechatMenus(array $appIds,$menuId)
|
|
|
{
|
|
|
if (empty($appIds)) {
|
|
|
return true;
|
|
|
}
|
|
|
- $info = WechatAccountMenuDetail::query()->whereIn('id', $appIds)->get();
|
|
|
+ $info = WechatAccountMenuDetail::query()->whereIn('wechat_authorization_info_id', $appIds)->where('menu_id',$menuId)->get();
|
|
|
foreach ($info as $val) {
|
|
|
+ $app = self::getofficialAccount($val->wechat_authorization_info_id);
|
|
|
+ if ($val->wx_menuid) {
|
|
|
+ // 有菜单则删除菜单
|
|
|
+ $app->menu->delete($val->wx_menuid);
|
|
|
+ }
|
|
|
+ $matchRule = [
|
|
|
+ "client_platform_type" => $val->type == "iso" ? "1" : "2",
|
|
|
+ ];
|
|
|
+ $res = $app->menu->create($val->content, $matchRule);
|
|
|
+ myLog('wx-meun')->info(['res' => $res]);
|
|
|
|
|
|
+ $errcode = getProp($res,'errcode',"");
|
|
|
+ if (intval($errcode) == 65303){
|
|
|
+ $res = $app->menu->create($val->content);
|
|
|
+ }
|
|
|
+ myLog('wx-meun')->info(['res' => $res]);
|
|
|
+ $wxMenuId = getProp($res, 'menuid', "");
|
|
|
+ $res = $app->menu->create($val->content, $matchRule);
|
|
|
+ myLog('wx-meun')->info(['res' => $res,'mid' => $wxMenuId]);
|
|
|
+ if ($wxMenuId) {
|
|
|
+ WechatAccountMenuDetail::query()->where('id', $val->id)->update(['wx_menuid' => $wxMenuId]);
|
|
|
+ }
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ public static function getofficialAccount($id)
|
|
|
+ {
|
|
|
+ $info = WechatOpenPlatformService::getAppInfoById($id);
|
|
|
+ $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($info->component_appid);
|
|
|
+ $openPlatform = WechatOpenPlatformService::buildApplication($componentInfo);
|
|
|
+ $app = $openPlatform->officialAccount($info->authorizer_appid, getProp($info, 'authorizer_refresh_token'));
|
|
|
+ unset($appInfo);
|
|
|
+ return $app;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 菜单详情
|
|
|
* name: detail
|
|
@@ -147,6 +183,13 @@ class WechatMenuService extends BaseService
|
|
|
$info = WechatAccountMenuDetail::query()->whereIn('id', $applyIds)->get();
|
|
|
foreach ($info as $val) {
|
|
|
|
|
|
+ if ($val->wx_menuid) {
|
|
|
+ // 有菜单则删除菜单
|
|
|
+ $app = self::getofficialAccount($val->wechat_authorization_info_id);
|
|
|
+ $app->menu->delete($info->wx_menuid);
|
|
|
+ }
|
|
|
+
|
|
|
+ WechatAccountMenuDetail::query()->where('wechat_authorization_info_id', $val->id)->update(['wx_menuid' => ""]);
|
|
|
}
|
|
|
return "操作成功";
|
|
|
}
|
|
@@ -165,10 +208,10 @@ class WechatMenuService extends BaseService
|
|
|
self::throwErrMsg('该菜单不存在或已删除');
|
|
|
}
|
|
|
// 已分配的
|
|
|
- $appIds = WechatAccountMenuDetail::where(['status' => 1, 'meun_id' => $info->id])->pluck('id')->toArray();
|
|
|
+ $appIds = WechatAccountMenuDetail::where(['status' => 1, 'menu_id' => $info->id])->pluck('wechat_authorization_info_id')->toArray();
|
|
|
$list = [];
|
|
|
$delAppId = [];
|
|
|
- $createAddIp = [];
|
|
|
+ $createAddIp = [];
|
|
|
|
|
|
if (empty($wxAuthIds)) {
|
|
|
$data['wechat_accounts'] = [];
|
|
@@ -188,16 +231,18 @@ class WechatMenuService extends BaseService
|
|
|
if (count($canNotUsed) > 0) {
|
|
|
self::throwErrMsg("优化师对id:为:" . implode(',', $canNotUsed) . "的公众号没有使用权限");
|
|
|
}
|
|
|
- foreach ($appIds as $val){
|
|
|
- if (!in_array($val,$wxAuthIds)){
|
|
|
+ foreach ($appIds as $val) {
|
|
|
+ if (!in_array($val, $wxAuthIds)) {
|
|
|
$delAppId[] = $val;
|
|
|
}
|
|
|
}
|
|
|
- $allSet = WechatAccountMenuDetail::query()->where('user_id', $info->user_id)->get();
|
|
|
+ // $allSet = WechatAccountMenuDetail::query()->where('user_id', $info->user_id)->get();
|
|
|
+
|
|
|
foreach ($wechatAccountInfos as $val) {
|
|
|
- if (in_array($val->id,$appIds)){
|
|
|
+ if (!in_array($val->id, $appIds)) {
|
|
|
$createAddIp[] = $val->id;
|
|
|
}
|
|
|
+
|
|
|
$data['wechat_accounts'][] = [
|
|
|
'id' => $val->id,
|
|
|
'appid' => $val->authorizer_appid,
|
|
@@ -206,7 +251,7 @@ class WechatMenuService extends BaseService
|
|
|
];
|
|
|
$appIds[] = $val->authorizer_appid;
|
|
|
$list[] = [
|
|
|
- 'meun_id' => $info->id,
|
|
|
+ 'menu_id' => $info->id,
|
|
|
'user_id' => $info->user_id,
|
|
|
'puser_id' => $info->puser_id,
|
|
|
'miniprogram_id' => $info->miniprogram_id,
|
|
@@ -214,37 +259,41 @@ class WechatMenuService extends BaseService
|
|
|
'wechat_authorization_info_id' => $val->id,
|
|
|
'nick_name' => $val->nick_name,
|
|
|
'content' => $info->content,
|
|
|
- 'msg_content' => $info->msg_content,
|
|
|
+ 'msg_content' => $info->msg_content,
|
|
|
'status' => 1,
|
|
|
];
|
|
|
}
|
|
|
- unset($wechatAccountInfos,$appIds,$val,$canNotUsed);
|
|
|
+ unset($wechatAccountInfos, $appIds, $val, $canNotUsed);
|
|
|
}
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
- if (!empty($delAppId)){
|
|
|
- WechatAccountMenuDetail::whereIn('id',$delAppId)->update(['status' => 0]);
|
|
|
- foreach ($list as $val){
|
|
|
+ if (!empty($list)) {
|
|
|
+ foreach ($list as $val) {
|
|
|
WechatAccountMenuDetail::updateOrCreate(
|
|
|
[
|
|
|
- 'meun_id' => $val['meun_id'],
|
|
|
+ 'menu_id' => $val['menu_id'],
|
|
|
'miniprogram_id' => $val['miniprogram_id'],
|
|
|
'wechat_authorization_info_id' => $val['wechat_authorization_info_id']
|
|
|
- ],$val);
|
|
|
+ ], $val);
|
|
|
+
|
|
|
}
|
|
|
- }else{
|
|
|
- WechatAccountMenuDetail::where('meun_id',$id)->update(['status' => 0]);
|
|
|
+ } else {
|
|
|
+ WechatAccountMenuDetail::where('menu_id', $id)->update(['status' => 0]);
|
|
|
}
|
|
|
- WechatMenu::query()->where('id',$id)->update($data);
|
|
|
+ if (!empty($delAppId)) {
|
|
|
+ WechatAccountMenuDetail::whereIn('id', $delAppId)->update(['status' => 0]);
|
|
|
+ }
|
|
|
+ WechatMenu::query()->where('id', $id)->update($data);
|
|
|
DB::commit();
|
|
|
- }catch (\Exception $exception){
|
|
|
+ } catch (\Exception $exception) {
|
|
|
self::throwErrMsg('操作失败');
|
|
|
}
|
|
|
- if (!empty($createAddIp)){
|
|
|
- self::createWechatMenus($createAddIp);
|
|
|
+ myLog('wx-meun')->info(['c' => $createAddIp,'del' => $delAppId]);
|
|
|
+ if (!empty($createAddIp)) {
|
|
|
+ self::createWechatMenus($createAddIp,$id);
|
|
|
}
|
|
|
- if(!empty($delAppId)){
|
|
|
- self::delWechatAccountMenu($delAppId);
|
|
|
+ if (!empty($delAppId)) {
|
|
|
+ self::delWechatAccountMenuByMeunId($delAppId,$id);
|
|
|
}
|
|
|
return ['msg' => "操作成功"];
|
|
|
}
|
|
@@ -257,19 +306,19 @@ class WechatMenuService extends BaseService
|
|
|
*/
|
|
|
public static function list(array $param)
|
|
|
{
|
|
|
- $sql = self::getQuery($param)->orderBy('id','desc');
|
|
|
- $isAll = getProp($param,'is_all',false);
|
|
|
- if($isAll){
|
|
|
+ $sql = self::getQuery($param)->orderBy('id', 'desc');
|
|
|
+ $isAll = getProp($param, 'is_all', false);
|
|
|
+ if ($isAll) {
|
|
|
$list = $sql->get();
|
|
|
- }else{
|
|
|
- $list = $sql->paginate(getProp($param,'limit',10));
|
|
|
+ } else {
|
|
|
+ $list = $sql->paginate(getProp($param, 'limit', 10));
|
|
|
}
|
|
|
return $list;
|
|
|
}
|
|
|
|
|
|
private static function getQuery(array $param)
|
|
|
{
|
|
|
- $sql = WechatMenu::query()->where('is_del',0);
|
|
|
+ $sql = WechatMenu::query()->where('is_del', 0);
|
|
|
if (getProp($param, 'puser_id')) {
|
|
|
$sql->where('puser_id', $param['puser_id']);
|
|
|
}
|
|
@@ -279,13 +328,30 @@ class WechatMenuService extends BaseService
|
|
|
if (getProp($param, 'keyword')) {
|
|
|
$sql->where('keyword', "like", "%" . $param['keyword'] . "%");
|
|
|
}
|
|
|
- if(getProp($param,'miniprogram_id')){
|
|
|
- $sql->where('miniprogram_id', $param['miniprogram_id']);
|
|
|
+ if (getProp($param, 'miniprogram_id')) {
|
|
|
+ $sql->where('miniprogram_id', $param['miniprogram_id']);
|
|
|
}
|
|
|
- if(getProp($param,'wechat_authorization_info_id')){
|
|
|
- $sql->whereJsonContains('wechat_accounts->id',$param['wechat_authorization_info_id']);
|
|
|
+ if (getProp($param, 'wechat_authorization_info_id')) {
|
|
|
+ $sql->whereJsonContains('wechat_accounts->id', $param['wechat_authorization_info_id']);
|
|
|
}
|
|
|
return $sql;
|
|
|
}
|
|
|
|
|
|
+ private static function delWechatAccountMenuByMeunId(array $delAppId, $menuId)
|
|
|
+ {
|
|
|
+ $info = WechatAccountMenuDetail::query()->whereIn('wechat_authorization_info_id', $delAppId)->where('menu_id',$menuId)->get();
|
|
|
+
|
|
|
+ foreach ($info as $val) {
|
|
|
+
|
|
|
+ if ($val->wx_menuid) {
|
|
|
+ // 有菜单则删除菜单
|
|
|
+ $app = self::getofficialAccount($info->wechat_authorization_info_id);
|
|
|
+ $app->menu->delete($val->wx_menuid);
|
|
|
+ }
|
|
|
+
|
|
|
+ WechatAccountMenuDetail::query()->where('wechat_authorization_info_id', $val->id)->update(['wx_menuid' => ""]);
|
|
|
+ }
|
|
|
+ return "操作成功";
|
|
|
+ }
|
|
|
+
|
|
|
}
|