|
@@ -0,0 +1,232 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by PhpStorm.
|
|
|
+ * User: stanley-king
|
|
|
+ * Date: 2017/6/9
|
|
|
+ * Time: 上午11:57
|
|
|
+ */
|
|
|
+
|
|
|
+class inoherbControl extends mobileControl
|
|
|
+{
|
|
|
+ private $mModInoherb;
|
|
|
+
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ parent::__construct();
|
|
|
+ $this->mModInoherb = Model('inoherb');
|
|
|
+ }
|
|
|
+ public function listOp()
|
|
|
+ {
|
|
|
+ $items = $this->items($this->page_size(),false);
|
|
|
+ $count = $this->mModInoherb->gettotalpage();
|
|
|
+ if(empty($items)) {
|
|
|
+ return self::outsuccess(['articles' => null,
|
|
|
+ 'mobile_page' => mobile_page(0)]);
|
|
|
+ } else {
|
|
|
+ $articles = $this->format($items);
|
|
|
+ return self::outsuccess(['articles' => $articles,
|
|
|
+ 'mobile_page' => mobile_page($count)]);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ public function addOp()
|
|
|
+ {
|
|
|
+ $flower_type = intval($_GET['flower_type']);
|
|
|
+ $content = $_GET['content'];
|
|
|
+ if(empty($content)) {
|
|
|
+ return self::outerr(errcode::ErrParamter,"填写的内容不能为空.");
|
|
|
+ }
|
|
|
+
|
|
|
+ $content = urldecode($content);
|
|
|
+ $trans = new trans_wapper($this->mModInoherb);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if(!$this->exists())
|
|
|
+ {
|
|
|
+ $data['content'] = $content;
|
|
|
+ $data['flower_type'] = $flower_type;
|
|
|
+ $data['unionid'] = session_helper::unionid();
|
|
|
+ $data['content'] = $content;
|
|
|
+ $data['likes'] = 0;
|
|
|
+
|
|
|
+ if(session_helper::logined()) {
|
|
|
+ $data['member_id'] = session_helper::memberid();
|
|
|
+ } else {
|
|
|
+ $data['member_id'] = 0;
|
|
|
+ }
|
|
|
+ $this->mModInoherb->insert($data);
|
|
|
+ }
|
|
|
+
|
|
|
+ $trans->commit();
|
|
|
+ } catch (Exception $ex) {
|
|
|
+ $trans->rollback();
|
|
|
+ }
|
|
|
+
|
|
|
+ return self::outsuccess(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function supportOp()
|
|
|
+ {
|
|
|
+ $inoherb_id = intval($_GET['inoherb_id']);
|
|
|
+ if($inoherb_id < 0) {
|
|
|
+ return self::outerr(errcode::ErrParamter,'错误的文章序列号.');
|
|
|
+ }
|
|
|
+
|
|
|
+ if($this->supported($inoherb_id)) {
|
|
|
+ $supported = false;
|
|
|
+ $supports = $this->unsupport($inoherb_id);
|
|
|
+ } else {
|
|
|
+ $supported = true;
|
|
|
+ $supports = $this->support($inoherb_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ return self::outsuccess(['likes' => $supports,'supported' => $supported,'inoherb_id' => $inoherb_id]);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function supported($inoherb_id)
|
|
|
+ {
|
|
|
+ $time = strtotime(date('Y-m-d',time()));
|
|
|
+ if(!isset($_SESSION['inoherb'])) {
|
|
|
+ $_SESSION['inoherb'] = [];
|
|
|
+ }
|
|
|
+ if(!isset($_SESSION['inoherb'][$time])) {
|
|
|
+ $_SESSION['inoherb'][$time] = [];
|
|
|
+ }
|
|
|
+
|
|
|
+ $inoherb = &$_SESSION['inoherb'][$time];
|
|
|
+ if(algorithm::binary_search($inoherb,$inoherb_id)) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function support($inoherb_id)
|
|
|
+ {
|
|
|
+ $time = strtotime(date('Y-m-d',time()));
|
|
|
+ if(!isset($_SESSION['inoherb'])) {
|
|
|
+ $_SESSION['inoherb'] = [];
|
|
|
+ }
|
|
|
+ if(!isset($_SESSION['inoherb'][$time])) {
|
|
|
+ $_SESSION['inoherb'][$time] = [];
|
|
|
+ }
|
|
|
+
|
|
|
+ $inoherb = &$_SESSION['inoherb'][$time];
|
|
|
+ if(algorithm::binary_search($inoherb,$inoherb_id) == false) {
|
|
|
+ $pos = algorithm::lower_bonud($inoherb,$inoherb_id);
|
|
|
+ algorithm::array_insert($inoherb,$pos,$inoherb_id);
|
|
|
+ $ret = $this->mModInoherb->where(['inoherb_id' => $inoherb_id])->update(['likes' => ['exp', 'likes + 1']]);
|
|
|
+ }
|
|
|
+ return $this->likes($inoherb_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function unsupport($inoherb_id)
|
|
|
+ {
|
|
|
+ $time = strtotime(date('Y-m-d',time()));
|
|
|
+ if(!isset($_SESSION['inoherb'])) {
|
|
|
+ $_SESSION['inoherb'] = [];
|
|
|
+ }
|
|
|
+ if(!isset($_SESSION['inoherb'][$time])) {
|
|
|
+ $_SESSION['inoherb'][$time] = [];
|
|
|
+ }
|
|
|
+
|
|
|
+ $inoherb = &$_SESSION['inoherb'][$time];
|
|
|
+ if(algorithm::binary_search($inoherb,$inoherb_id) == true) {
|
|
|
+ $pos = algorithm::lower_bonud($inoherb,$inoherb_id);
|
|
|
+ algorithm::array_erase($inoherb,$pos);
|
|
|
+ $ret = $this->mModInoherb->where(['inoherb_id' => $inoherb_id])->update(['likes' => ['exp', 'likes - 1']]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->likes($inoherb_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function mineOp()
|
|
|
+ {
|
|
|
+ $inoherb_id = intval($_GET['inoherb_id']);
|
|
|
+ if($inoherb_id < 0) {
|
|
|
+ return self::outerr(errcode::ErrParamter,'错误的文章序列号.');
|
|
|
+ }
|
|
|
+ $item = $this->item(['unionid' => session_helper::unionid()]);
|
|
|
+ if(empty($item)) {
|
|
|
+ return self::outsuccess(['articles' => null,
|
|
|
+ 'mobile_page' => mobile_page(0)]); }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ $articles = $this->format($item);
|
|
|
+ return self::outsuccess(['articles' => $articles,
|
|
|
+ 'mobile_page' => mobile_page(1)]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function infoOp()
|
|
|
+ {
|
|
|
+ $inoherb_id = intval($_GET['inoherb_id']);
|
|
|
+ if($inoherb_id < 0) {
|
|
|
+ return self::outerr(errcode::ErrParamter,'错误的文章序列号.');
|
|
|
+ }
|
|
|
+ $item = $this->item(['inoherb_id' => $inoherb_id]);
|
|
|
+
|
|
|
+ if(empty($item)) {
|
|
|
+ return self::outsuccess(['articles' => null,
|
|
|
+ 'mobile_page' => mobile_page(0)]); }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ $articles = $this->format($item);
|
|
|
+ return self::outsuccess(['articles' => $articles,
|
|
|
+ 'mobile_page' => mobile_page(1)]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function exists()
|
|
|
+ {
|
|
|
+ $unionid = session_helper::unionid();
|
|
|
+ if(empty($unionid)) return true;
|
|
|
+
|
|
|
+ $items = $this->mModInoherb->field('*')->where(['unionid' => $unionid])->select();
|
|
|
+ if(empty($items)) {
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function likes($inoherb_id)
|
|
|
+ {
|
|
|
+ $items = $this->mModInoherb->field('likes')->where(['inoherb_id' => $inoherb_id])->select();
|
|
|
+ if(empty($items)) {
|
|
|
+ return 0;
|
|
|
+ } else {
|
|
|
+ return intval($items[0]['likes']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function item($condition)
|
|
|
+ {
|
|
|
+ return $this->mModInoherb->field('*')->where($condition)->select();
|
|
|
+ }
|
|
|
+
|
|
|
+ private function items($page = 0,$lock = false)
|
|
|
+ {
|
|
|
+ return $this->mModInoherb->field('*')->order('likes desc')->page($page)->lock($lock)->select();
|
|
|
+ }
|
|
|
+
|
|
|
+ private function format($items)
|
|
|
+ {
|
|
|
+ $result = [];
|
|
|
+ foreach ($items as $item)
|
|
|
+ {
|
|
|
+ $val = [];
|
|
|
+ $val['inoherb_id'] = intval($item['inoherb_id']);
|
|
|
+ $val['avatar'] = session_helper::avatar();
|
|
|
+ $val['flower_type'] = intval($item['flower_type']);
|
|
|
+ $val['content'] = $item['content'];
|
|
|
+ $val['likes'] = $item['likes'];
|
|
|
+ $val['supported'] = $this->supported($val['inoherb_id']);
|
|
|
+
|
|
|
+ $result[] = $val;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+}
|