|
@@ -9,10 +9,16 @@
|
|
|
|
|
|
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
|
|
@@ -21,8 +27,226 @@ class WechatMenuService extends BaseService
|
|
|
*/
|
|
|
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' => "操作成功"];
|
|
|
+ }
|
|
|
|
|
|
}
|