微信小程序 点赞

版权声明:111 https://blog.csdn.net/qq_42897782/article/details/85102528

小程序企业资讯给文章点赞

资讯列表页

资讯详情页

小程序前端代码:

(在前端添加点击事件  bindtap='update')

<view style='padding-top:30rpx;width:93%;margin:0 auto;'>
    <text class='block font15 gray5 tl fl' style='padding-left:20rpx;'>阅读数  {{oldcommon.read_num}} </text>
    <view class='p_tb15 m_t5 fr' style='width:15%;margin-top:-30rpx;margin-right:10rpx;'  bindtap='update_zan'>
      <view  wx:if="{{oldcommon.zanstate == 0}}">
      <image  class='fl zanimagerrzy' src='/images/zan.png' style='width:32rpx;height:32rpx;margin-top:3rpx;'></image></view>
      <view  wx:elif="{{oldcommon.zanstate == 1}}">
      <image src='/images/zanclock.png' class='fl zanimagerrzy' style='width:32rpx;height:32rpx;margin-top:3rpx;'></image></view>
      <text class='fl m_lr5 font15 gray5 zanallrrzy'>{{oldcommon.vote_num}}</text>
      <view class='cl'></view>
    </view>
    <view class='cl'></view>
</view>

小程序JS代码

 //点赞 update_zan
  update_zan: function (e) {
    var that = this;
    var oldcommon = that.data.oldcommon;
    console.log(oldcommon);
    var zanstate = (oldcommon.zanstate == 1) ? 0 : 1;
    var zanall = oldcommon.vote_num;
    // 更换状态并修改数据库
    wx.request({
      url: app.d.hostUrl + 'changezanstate',
      data: {
        id: oldcommon.id,
        openid: wx.getStorageSync('openid'),
        zanstate: zanstate
      },
      method: 'GET',
      // header: {},   
      success: function (res) {
        //console.log(res);
        var data = res.data;
        if(data.errno == 1){
          // 执行失败则提示并终止程序
          return;
        }
        // 成功则刷新摸板
        // 更新图片和更新点赞总数
        var imgsrc = "";
        if (zanstate == 1){
          oldcommon.vote_num++;
        } else{
          oldcommon.vote_num--;
        }
        oldcommon.zanstate = zanstate;
        //console.log(oldcommon);
        that.setData({
          id: oldcommon.id,
          oldcommon: oldcommon
        })       
      }
    })
    return ;
 },

后台接口

//切换文章点赞状态
    public function doPagechangezanstate(){
		global $_W,$_GPC;
		$uniacid=$_W['uniacid'];
		// 接收参数
		$id=$_GPC['id'];
		$openid=$_GPC['openid'];
		$zanstate = $_GPC['zanstate'];

		// 检测文章是否存在
		$common = pdo_fetch("select * from ".tablename('yyf_company_news')." where id='$id'");
		if (!$common) {
			//文章不存在退出
			return $this->result(1,'error','未找到该文章');
		}

		// 检测用户是否存在
		$userinfo = pdo_fetch("select * from ".tablename('yyf_company_userinfo')." where openid='$openid'");
		if (!$userinfo) {
			//用户不存在退出
			return $this->result(1,'error','未找到该用户');
		}

		// 开启事务,因为使用mysiam表,这里暂不使用事务

		// 验证并更新点赞中间表(备注:中间表执行失败继续执行,不影响总数,若要严谨则表修改成innodb引擎并使用事务)
		$zan = pdo_fetch("select * from ".tablename('yyf_company_zan')." where `a_id` = $id and `openid`='$openid'");
		if ($zan) {
			// 不能重复操作
			if ((int)$zan['status'] == $zanstate) {
				// 取消点赞则退出
				if ($zanstate == 0) {
					return $this->result(1,'error','未点赞不能取消');
				}
				// 重复操作退出
				return $this->result(1,'error','不能重复点赞');
			}
			// 更新中间表状态
			pdo_update('yyf_company_zan', array('status =' => $zanstate),array('a_id' => $id, 'openid' => $openid));
		}
		else{
			// 取消点赞则退出
			if ($zanstate == 0) {
				return $this->result(1,'error','未点赞不能取消');
			}
			// 插入中间表
			pdo_insert("yyf_company_zan", array('openid' => $openid, 'a_id' => $id, 'create_at' => time()));

		}
		$num = ($zanstate == 1) ? 1 : -1;
		// 增减点赞总数
		$res = pdo_update('yyf_company_news', array('vote_num +=' => $num),array('id' => $id));
		if (!$res) {
			// 数据库执行失败则退出
			return $this->result(1,'error',"数据库执行失败");
		}
		
		// 成功则提交事务并更新摸板,反之则提示失败不更新摸板

		// 成功返回,并刷新摸板
		// 这里为了执行效率并且对于点赞总数严谨性不高,不从数据库取数据,直接前端更新
        return $this->result(0,'success',$data);
	}
扫描二维码关注公众号,回复: 5285868 查看本文章

对应的数据表

猜你喜欢

转载自blog.csdn.net/qq_42897782/article/details/85102528
今日推荐