| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 | <?php/** * 公众号菜单 * @file:WechatMenuService.php * @Date: 2023/7/11 * @Time: 10:53 */namespace Modules\WechatPlatform\Services;use Illuminate\Support\Facades\DB;use Modules\Common\Services\BaseService;use Modules\WechatPlatform\Models\WechatAccountMenuDetail;use Modules\WechatPlatform\Models\WechatMenu;use function PHPUnit\Framework\isNan;class WechatMenuService extends BaseService{    /**     *  添加菜单     * name: addMenu     * @param $param     * date 2023/07/11 11:38     */    public static function addMenu($param)    {        $res = WechatMenu::create($param);        if ($res) {            return ['msg' => '操作成功'];        }        self::throwErrMsg('操作失败');    }    /**     *  更新菜单     * name: updateMenu     * @param $id     * @param $param     * @return string[]     * date 2023/07/12 07:17     */    public static function updateMenu($id, $param)    {        $info = WechatMenu::where('id', $id)->where('is_del', 0)->first();        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);        $data = [            'type' => $param['type'],            'content' => $param['content'],            'msg_content' => $param['msg_content']        ];        WechatAccountMenuDetail::where('meun_id', $id)->update($data);        // 更新公众号菜单        self::createWechatMenus($appIds);        return ['msg' => '操作成功'];    }    /**     *  创建微信菜单     * name: createWechatMenus     * @param array $appIds     * @return bool     * date 2023/07/12 07:16     */    private static function createWechatMenus(array $appIds)    {        if (empty($appIds)) {            return true;        }        $info = WechatAccountMenuDetail::query()->whereIn('id', $appIds)->get();        foreach ($info as $val) {        }        return true;    }    /**     * 菜单详情     * name: detail     * @param $id     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null     * date 2023/07/12 07:17     */    public static function detail($id)    {        return WechatMenu::query()->where('id', $id)->first();    }    /**     *  公众号列表     * name: authList     * @param $id     * @param $userId     * @return mixed     * date 2023/07/12 07:16     */    public static function authList($id, $userId)    {        $list = DB::table('wechat_authorization_infos')            ->where('user_id', $userId)            ->select('id', 'nick_name', 'is_enabled')->get();        $authList = WechatAccountMenuDetail::where('menu_id', $id)->get();        if (!$list->isEmpty()) {            foreach ($list as $val) {                $val->is_auth = 0;                $info = $authList->where('wechat_authorization_info_id', $val->id)->first();                if (getProp($info, 'status') == 1) {                    $val->is_auth = 1;                }                if ($val->is_enabled != 1) {                    $val->nick_name .= "(取消授权)";                }                unset($val->is_enabled);            }        }        return $list;    }    /**     *   删除     * name: del     * @param $ids     * @return string[]     * date 2023/07/12 07:14     */    public static function del($ids)    {        WechatMenu::query()->whereIn('id', $ids)->update(['is_del' => 1, 'del_at' => get_date()]);        $applyIds = WechatAccountMenuDetail::query()->whereIn('menu_id', $ids)->where('status', 1)->pluck('id')->toArray();        if ($applyIds) {            WechatAccountMenuDetail::query()->whereIn('menu_id', $ids)->update(['status' => 0]);            // 删除菜单            self::delWechatAccountMenu($applyIds);        }        return ['msg' => "操作成功"];    }    private static function delWechatAccountMenu(array $applyIds)    {        $info = WechatAccountMenuDetail::query()->whereIn('id', $applyIds)->get();        foreach ($info as $val) {        }        return "操作成功";    }    /**     *  分配公众号     * name: allocation     * @param $id     * @param array $wxAuthIds     * date 2023/07/12 07:20     */    public static function allocation($id, $wxAuthIds)    {        $info = WechatMenu::query()->where('id', $id)->where('is_del', 0)->first();        if (is_empty($info)) {            self::throwErrMsg('该菜单不存在或已删除');        }        // 已分配的        $appIds = WechatAccountMenuDetail::where(['status' => 1, 'meun_id' => $info->id])->pluck('id')->toArray();        $list = [];        $delAppId = [];        $createAddIp  = [];        if (empty($wxAuthIds)) {            $data['wechat_accounts'] = [];            $delAppId = $appIds;        } else {            $wechatAccountInfos = DB::table('wechat_authorization_infos')                ->whereIn('id', $wxAuthIds)                ->where('is_enabled', 1)                ->where('user_id', $info->user_id)                ->get();            if ($wechatAccountInfos->isEmpty()) {                self::throwErrMsg("优化师对提交的公众号没有使用权限");            }            $canNotUsed = $wechatAccountInfos->pluck('id')->toArray();            $canNotUsed = array_diff($wxAuthIds, $canNotUsed);            if (count($canNotUsed) > 0) {                self::throwErrMsg("优化师对id:为:" . implode(',', $canNotUsed) . "的公众号没有使用权限");            }            foreach ($appIds as  $val){                if (!in_array($val,$wxAuthIds)){                    $delAppId[] = $val;                }            }            $allSet = WechatAccountMenuDetail::query()->where('user_id', $info->user_id)->get();            foreach ($wechatAccountInfos as $val) {                if (in_array($val->id,$appIds)){                    $createAddIp[] = $val->id;                }                $data['wechat_accounts'][] = [                    'id' => $val->id,                    'appid' => $val->authorizer_appid,                    'nick_name' => $val->nick_name,                    'component_appid' => $val->component_appid                ];                $appIds[] = $val->authorizer_appid;                $list[] = [                    'meun_id' => $info->id,                    'user_id' => $info->user_id,                    'puser_id' => $info->puser_id,                    'miniprogram_id' => $info->miniprogram_id,                    'appid' => $val->authorizer_appid,                    'wechat_authorization_info_id' => $val->id,                    'nick_name' => $val->nick_name,                    'content' => $info->content,                    'msg_content' =>  $info->msg_content,                    'status' => 1,                ];            }            unset($wechatAccountInfos,$appIds,$val,$canNotUsed);        }        DB::beginTransaction();        try {            if (!empty($delAppId)){                WechatAccountMenuDetail::whereIn('id',$delAppId)->update(['status' => 0]);                foreach ($list as $val){                    WechatAccountMenuDetail::updateOrCreate(                        [                            'meun_id' => $val['meun_id'],                            'miniprogram_id' => $val['miniprogram_id'],                            'wechat_authorization_info_id' => $val['wechat_authorization_info_id']                        ],$val);                }            }else{                WechatAccountMenuDetail::where('meun_id',$id)->update(['status' => 0]);            }            WechatMenu::query()->where('id',$id)->update($data);            DB::commit();        }catch (\Exception $exception){            self::throwErrMsg('操作失败');        }        if (!empty($createAddIp)){            self::createWechatMenus($createAddIp);        }        if(!empty($delAppId)){            self::delWechatAccountMenu($delAppId);        }        return ['msg' => "操作成功"];    }}
 |