E企盈营销工具技术服务商 热线:4006-838-530

PHP+微信小程序分享获取群ID

E企盈直播平台营销卖货系统

PHP+微信小程序分享获取群ID前言需求说明代码总结前言时间2018年10月,吐槽下微信小程序,因为一些原因说不能在分享到群的时候获取群id,但是我在开发的时候明显说可以获取的(体验版),但是正式版一跑问题就来了,获取群id时得时不得,不得:不进分享成功后的回调方法0.0。无爱来,所以只能更换下思路,从用户点击群里面的分享地址进来后在进行获取来,浪费俺宝贵的时间。需求说明通过小程序发布文章,要求可以指定用户查看,然后通过分享到群进行扩散。那问题来了,要想指定用户查看你得有用户分组(组对应群),所以就有来当前的问题和解决方案。代码小程序app.js因为onLaunch事件的生命周期问题,所以放onShow中,我这里是把iv和encryptedData缓存起来,因为要跟文章用户关联的话在这里处理会有重复  onShow: function (ops) {    // console.log(ops);    if (ops.scene == 1044) {      wx.getShareInfo({        shareTicket: ops.shareTicket,        success: function (res) {          var params = {            iv: res.iv,            encryptedData: res.encryptedData          };          wx.setStorageSync(‘shareTicket’, params);        }      })    }  },小程序index.jsindex属于入口,我把逻辑放这里来处理,分享得地址进来的话要把对应的群信息拿到,并关联到文章,用户,然后在判断访问的用户是不是有权访问文章内容,符合条件就会在跳转进文章的详情页面,过程中虽然会有点体验上的问题,但是总体来说影响不大。。。  /**   * 生命周期函数–监听页面加载   */  onLoad: function(options) {    var that = this;    var user_id = ”;    //用户缓存信息,如果没有就重新授权,记得把对应参数带过去    user_id = wx.getStorageSync(‘userinfo’);    if (!user_id) {      wx.removeStorageSync(‘userinfo’);      that.setData({        redview: false      });      wx.reLaunch({        url: ‘../auth/auth?soft_wen_id=’ + options.soft_wen_id + ‘&matching=’ + options.matching + ‘&type=’ + options.type,      });      return false;    }    var shareTicket = wx.getStorageSync(‘shareTicket’);    //判断是否上分享进来,判断是否有权限(api)顺便绑定用户的群信息,如果没有权限就跳转到首页    if (typeof(options.matching) != “undefined”) {      var where = {        matching: options.matching,//文章每次分享都会生成的随机码,跟群进行匹配        soft_wen_id: options.soft_wen_id,//文章id        member_id: user_id.member_id,//用户id        session_key: user_id.session_key,//用户授权后拿到的key        iv: shareTicket.iv,        encryptedData: shareTicket.encryptedData      };      //接口请求      http.http_post(‘memberbinding’, where, false, function(binddata) {        if (binddata.data.status == 200) {          wx.removeStorageSync(‘shareTicket’);           //业务逻辑      });    } else {     //业务逻辑    }  },PHP代码接口这里就是走一些业务逻辑了    /**     * 用户通过文章分享地址进入小程序,关联绑定群id     * @param $post     * @return array     */    public static function MemberBinding($post)    {        //不是分享地址进来        if ($post[‘matching’] != ‘9999’) {            //关联群跟用,群跟文章            if ($post[‘encryptedData’] != ‘undefined’) {                $result = WxVisitLogic::getGroup($post);                if (!$result) {                    return [‘features’ => 2, ‘msg’ => ‘软文群信息异常’];                }            }        }        //判断当前文章是否需要权限        //判断当前用户是否少文章制定阅读的人        $SoftMember = SoftMember::find()->where([‘soft_wen_id’ => $post[‘soft_wen_id’], ‘is_appoint’ => 1])->asArray()->all();        $myMember = SoftMember::find()->where([‘soft_wen_id’ => $post[‘soft_wen_id’], ‘member_id’ => $post[‘member_id’], ‘is_release_id’ => 1])->asArray()->one();        if ($myMember) {            return [‘features’ => 1, ‘msg’ => ‘用户发布者,有权限访问’];        }        if (count($SoftMember) > 0) {            $is = false;            foreach ($SoftMember as $item) {                if ($item[‘member_id’] == $post[‘member_id’]) {                    $is = false;                    break;                } else {                    $is = true;                }            }            if ($is) {                return [‘features’ => 2, ‘msg’ => ‘当前用户不是软文制定阅读的,无权限访问’];            } else {                return [‘features’ => 1, ‘msg’ => ‘用户属于制定阅读的人,有权限访问’];            }        } else {            //虽然无权限,还是要帮增加一条关联信息            $SoftMembers = SoftMember::find()->where([‘soft_wen_id’ => $post[‘soft_wen_id’], ‘member_id’ => $post[‘member_id’]])->asArray()->one();            if (!$SoftMembers) {                //新增一条                $SoftMember = new SoftMember();                $SoftMember->member_id = $post[‘member_id’];                $SoftMember->soft_wen_id = $post[‘soft_wen_id’];                $SoftMember->is_release_id = 0;                $SoftMember->created_at = time();                $SoftMember->updated_at = time();                $SoftMember->save(false);            }            return [‘features’ => 1, ‘msg’ => ‘软文无权限限制,随意查看’];        }    }WXBizDataCrypt是解密获取群id的类,基础上通用,这里就不放出来    /**    * 分享到群后获取群id    */    public static function getGroup($post)    {        $weiXinXcx = Yii::$app->params[‘WECHAT’][‘weixinxcx’];        $pc = new WXBizDataCrypt($weiXinXcx[‘appid’], $post[‘session_key’]);        $errCode = $pc->decryptData($post[‘encryptedData’], $post[‘iv’], $result, $weiXinXcx[‘appid’], $post[‘session_key’]);        //解密是否出错        if ($errCode != 0) {            return false;        }        //转换数组        $result = json_decode($result, JSON_UNESCAPED_UNICODE);        $Group = Group::find()->where([‘groupwx_id’ => $result[‘openGId’]])->one();        //不存在就新增        if (!$Group) {            $Group = new Group();            $Group->groupwx_id = $result[‘openGId’];            $Group->save();        }        //如果分享到的群关联当前访问用户(包括发布者)        $MemberGroup = MemberGroup::find()->where([‘member_id’ => $post[‘member_id’], ‘group_id’ => $Group->group_id])->one();        $SSoftWen =  SSoftWen::find()->where([‘soft_wen_id’ => $post[‘soft_wen_id’]])->asArray()->one();        $MemberGroup1 = MemberGroup::find()->where([‘member_id’ => $SSoftWen[‘cuser_id’], ‘group_id’ => $Group->group_id])->one();        if (!$MemberGroup) {            //新增关系            $MemberGroup = new MemberGroup();            $MemberGroup->member_id = $post[‘member_id’];            $MemberGroup->group_id = $Group->group_id;            $MemberGroup->save(false);        }        if(!$MemberGroup1){            //新增关系            $MemberGroup1 = new MemberGroup();            $MemberGroup1->member_id = $SSoftWen[‘cuser_id’];            $MemberGroup1->group_id = $Group->group_id;            $MemberGroup1->save(false);        }        //修改进软文表中        if(!$SSoftWen[‘scenario’]){           SSoftWen::updateAll([‘scenario’ => $result[‘openGId’]] , [‘soft_wen_id’ => $post[‘soft_wen_id’]]);        }        $group = SSoftGroup::find()->where([‘matching’ => $post[‘matching’],’soft_wen_id’=>$post[‘soft_wen_id’],’group_id’=>$Group->group_id])->asArray()->one();        if(!$group){            $SSoftGroup = new SSoftGroup();            $SSoftGroup->group_id = $Group->group_id;            $SSoftGroup->soft_wen_id = $post[‘soft_wen_id’];            $SSoftGroup->matching = $post[‘matching’];            $SSoftGroup->save();        }        return true;    }总结因为场景问题,可能大家的解决方案会不一样,但是分享者的初衷就是为了问题提供多种解决方案,通过度娘总会看到让你灵光一闪的文章。最后小程序的发布用到资质也是坑,大家开发前最好做好调研,不然产品出来了,发现没有资质就凉凉了,审核估计被各种打回。。。最后镇文图

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » PHP+微信小程序分享获取群ID
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏