微信小程序仿朋友圈发布动态功能(3)

public function adds() { extract(generateRequestParamVars()); $user = D(self::$WECHAT_USER)->find($user_id); $data = []; $data['user_id'] = $user_id; $data['user_name'] = $user['nickname']; $data['user_img'] = $user['imageurl']; $data['content'] = $content; $data['images'] = $images; $data['create_time'] = time(); if ($this->add($data) === false) { throw new \Exception('OPERATION_FAILED'); } }

保存好了之后,接下来如何在前端中显示图片呢?关键在于保存图片数组到数据库里,如何让它取出来的时候转为数组。代码如下:

控制层:

public function my_moments() { try{ $data = D(self::$MOMENTS_MODEL)->my_moments(); $ajaxReturnData['status'] = 1; $ajaxReturnData['message'] = 'success'; $ajaxReturnData['data'] = $data; }catch (\Exception $e){ $ajaxReturnData['status'] = 0; $ajaxReturnData['message'] = 'fail'; } $this->ajaxReturn($ajaxReturnData); }

模型层:(这里使用了json_decode($array,true)方法,打印出来就是数组了)

public function my_moments() { extract(generateRequestParamVars()); $user = D(self::$WECHAT_USER)->find($user_id); if($user['is_doctor'] == 1){ $conditions = []; $conditions['user_id'] = $user_id; $doctor = D(self::$DOCTOR_MODEL)->where($conditions)->find(); $identity = $doctor['hospital']. "" . $doctor['grade']; }else{ $identity = ''; } $conditions = []; $conditions['user_id'] = $user_id; $moments = $this->where($conditions)->order('create_time desc')->select(); for($i = 0 ; $i < count($moments) ; $i ++){ $moments[$i]['images'] = json_decode($moments[$i]['images'],true); } $data = []; $data[0] = $user; $data[1] = $moments; $data[2] = $identity; return $data; }

最后,动态页面如何显示图片呢?

主要我还在做九宫格图片的适配,就不贴代码了,主要是图片src需要加前缀,也就是你的域名。这样就能显示出来啦~

<image src="https://wechat.homedoctor.com{{image}}"></image>

不相信你能看到最后,哈哈~我写的太多了

总结

以上所述是小编给大家介绍的微信小程序仿朋友圈发布动态界面,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/adf760773e3cf04e71041cbfac723868.html