inoherb.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/6/9
  6. * Time: 上午11:57
  7. */
  8. require_once (BASE_ROOT_PATH . '/helper/algorithm.php');
  9. require_once (BASE_ROOT_PATH . '/helper/login_helper.php');
  10. require_once (BASE_ROOT_PATH . '/helper/session_helper.php');
  11. require_once (BASE_ROOT_PATH . '/helper/sms_helper.php');
  12. class inoherbControl extends mobileControl
  13. {
  14. private $mModInoherb;
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. $this->mModInoherb = Model('inoherb');
  19. }
  20. public function indexOp()
  21. {
  22. $inoherb_id = intval($_GET['inoherb_id']);
  23. if($inoherb_id > 0) {
  24. $url = "http://p.lrlz.com/hfive/inoherb/index.html?#/share?inoherb_id={$inoherb_id}";
  25. }
  26. else {
  27. $url = "http://p.lrlz.com/hfive/inoherb/index.html?2";
  28. }
  29. if(session_helper::need_wechat_author())
  30. {
  31. $author = new thrid_author\wxauthor();
  32. $author->enter($url);
  33. return;
  34. }
  35. else {
  36. fcgi_header("location:{$url}");
  37. }
  38. }
  39. public function listOp()
  40. {
  41. $items = $this->items($this->page_size(),false);
  42. $count = $this->mModInoherb->gettotalpage();
  43. if(empty($items)) {
  44. return self::outsuccess(['articles' => null,
  45. 'mobile_page' => mobile_page(0)]);
  46. } else {
  47. $articles = $this->format($items);
  48. return self::outsuccess(['articles' => $articles,
  49. 'mobile_page' => mobile_page($count)]);
  50. }
  51. }
  52. public function addOp()
  53. {
  54. $flower_type = intval($_GET['flower_type']);
  55. $content = $_GET['content'];
  56. if(empty($content)) {
  57. return self::outerr(errcode::ErrParamter,"填写的内容不能为空.");
  58. }
  59. $content = urldecode($content);
  60. $trans = new trans_wapper($this->mModInoherb,__METHOD__);
  61. try
  62. {
  63. if(!$this->exists())
  64. {
  65. $data['content'] = $content;
  66. $data['flower_type'] = $flower_type;
  67. $data['unionid'] = session_helper::unionid();
  68. $data['content'] = $content;
  69. $data['likes'] = 0;
  70. $data['avatar'] = session_helper::avatar();
  71. $data['nickname'] = session_helper::nickname();
  72. if(session_helper::logined()) {
  73. $data['member_id'] = session_helper::memberid();
  74. $data['mobile'] = session_helper::cur_mobile();
  75. } else {
  76. $data['member_id'] = 0;
  77. }
  78. $this->mModInoherb->insert($data);
  79. }
  80. $trans->commit();
  81. } catch (Exception $ex) {
  82. $trans->rollback();
  83. }
  84. return self::outsuccess(null);
  85. }
  86. public function supportOp()
  87. {
  88. $inoherb_id = intval($_GET['inoherb_id']);
  89. if($inoherb_id < 0) {
  90. return self::outerr(errcode::ErrParamter,'错误的文章序列号.');
  91. }
  92. if($this->supported($inoherb_id)) {
  93. $supported = false;
  94. $supports = $this->unsupport($inoherb_id);
  95. } else {
  96. $supported = true;
  97. $supports = $this->support($inoherb_id);
  98. }
  99. return self::outsuccess(['likes' => $supports,'supported' => $supported,'inoherb_id' => $inoherb_id]);
  100. }
  101. private function supported($inoherb_id)
  102. {
  103. $time = strtotime(date('Y-m-d',time()));
  104. if(!isset($_SESSION['inoherb'])) {
  105. $_SESSION['inoherb'] = [];
  106. }
  107. if(!isset($_SESSION['inoherb'][$time])) {
  108. $_SESSION['inoherb'][$time] = [];
  109. }
  110. $inoherb = &$_SESSION['inoherb'][$time];
  111. if(algorithm::binary_search($inoherb,$inoherb_id)) {
  112. return true;
  113. } else {
  114. return false;
  115. }
  116. }
  117. private function support($inoherb_id)
  118. {
  119. $time = strtotime(date('Y-m-d',time()));
  120. if(!isset($_SESSION['inoherb'])) {
  121. $_SESSION['inoherb'] = [];
  122. }
  123. if(!isset($_SESSION['inoherb'][$time])) {
  124. $_SESSION['inoherb'][$time] = [];
  125. }
  126. $inoherb = &$_SESSION['inoherb'][$time];
  127. if(algorithm::binary_search($inoherb,$inoherb_id) == false) {
  128. $pos = algorithm::lower_bonud($inoherb,$inoherb_id);
  129. algorithm::array_insert($inoherb,$pos,$inoherb_id);
  130. $ret = $this->mModInoherb->where(['inoherb_id' => $inoherb_id])->update(['likes' => ['exp', 'likes + 1']]);
  131. }
  132. return $this->likes($inoherb_id);
  133. }
  134. private function unsupport($inoherb_id)
  135. {
  136. $time = strtotime(date('Y-m-d',time()));
  137. if(!isset($_SESSION['inoherb'])) {
  138. $_SESSION['inoherb'] = [];
  139. }
  140. if(!isset($_SESSION['inoherb'][$time])) {
  141. $_SESSION['inoherb'][$time] = [];
  142. }
  143. $inoherb = &$_SESSION['inoherb'][$time];
  144. if(algorithm::binary_search($inoherb,$inoherb_id) == true) {
  145. $pos = algorithm::lower_bonud($inoherb,$inoherb_id);
  146. algorithm::array_erase($inoherb,$pos);
  147. $ret = $this->mModInoherb->where(['inoherb_id' => $inoherb_id])->update(['likes' => ['exp', 'likes - 1']]);
  148. }
  149. return $this->likes($inoherb_id);
  150. }
  151. public function mineOp()
  152. {
  153. $inoherb_id = intval($_GET['inoherb_id']);
  154. if($inoherb_id < 0) {
  155. return self::outerr(errcode::ErrParamter,'错误的文章序列号.');
  156. }
  157. $item = $this->item(['unionid' => session_helper::unionid()],true);
  158. if(empty($item)) {
  159. return self::outsuccess(['articles' => null,
  160. 'mobile_page' => mobile_page(0)]); }
  161. else
  162. {
  163. $articles = $this->format($item);
  164. return self::outsuccess(['articles' => $articles,
  165. 'mobile_page' => mobile_page(1)]);
  166. }
  167. }
  168. public function infoOp()
  169. {
  170. $inoherb_id = intval($_GET['inoherb_id']);
  171. if($inoherb_id < 0) {
  172. return self::outerr(errcode::ErrParamter,'错误的文章序列号.');
  173. }
  174. $item = $this->item(['inoherb_id' => $inoherb_id]);
  175. if(empty($item)) {
  176. return self::outsuccess(['articles' => null, 'mobile_page' => mobile_page(0)]);
  177. } else {
  178. $articles = $this->format($item);
  179. return self::outsuccess(['articles' => $articles, 'mobile_page' => mobile_page(1)]);
  180. }
  181. }
  182. public function loginedOp()
  183. {
  184. $logined = session_helper::logined();
  185. return self::outsuccess(['logined' => $logined]);
  186. }
  187. public function bindOp()
  188. {
  189. $mobile = $_GET['mobile'];
  190. $validator = new Validator();
  191. $validator->setValidate(Validator::verify_mobile($mobile));
  192. $err = $validator->validate();
  193. if ($err != '') {
  194. return self::outerr(errcode::ErrParamter, $err);
  195. }
  196. if(!isset($_GET['code']) || empty($_GET['code'])) {
  197. return self::outerr(errcode::ErrParamter, "请输入验证码.");
  198. }
  199. $code = $_GET['code'];
  200. $ret = sms_helper::check_code(Sms::getbonus_code,$code,$mobile);
  201. if(is_array($ret)) {
  202. return self::outerr($ret['code'], $ret['msg']);
  203. } else {
  204. login_helper::onBinded($mobile,$code);
  205. $member_id = session_helper::memberid();
  206. $nickname = session_helper::nickname();
  207. $this->mModInoherb->where(['unionid' => session_helper::unionid()])->update(['mobile' => $mobile,'member_id' => $member_id,'nickname' => $nickname]);
  208. return self::outsuccess(null);
  209. }
  210. }
  211. private function exists()
  212. {
  213. $unionid = session_helper::unionid();
  214. if(empty($unionid)) return true;
  215. $items = $this->mModInoherb->field('*')->where(['unionid' => $unionid])->select();
  216. if(empty($items)) {
  217. return false;
  218. } else {
  219. return true;
  220. }
  221. }
  222. private function likes($inoherb_id)
  223. {
  224. $items = $this->mModInoherb->field('likes')->where(['inoherb_id' => $inoherb_id])->lock(true)->select();
  225. if(empty($items)) {
  226. return 0;
  227. } else {
  228. return intval($items[0]['likes']);
  229. }
  230. }
  231. private function item($condition,$lock = false)
  232. {
  233. return $this->mModInoherb->field('*')->where($condition)->lock($lock)->select();
  234. }
  235. private function items($page = 0,$lock = false)
  236. {
  237. return $this->mModInoherb->field('*')->order('likes desc,inoherb_id desc')->page($page)->lock($lock)->select();
  238. }
  239. private function format($items)
  240. {
  241. $result = [];
  242. foreach ($items as $item)
  243. {
  244. $val = [];
  245. $val['inoherb_id'] = intval($item['inoherb_id']);
  246. $val['avatar'] = $item['avatar'];
  247. $val['flower_type'] = intval($item['flower_type']);
  248. $val['content'] = $item['content'];
  249. $val['likes'] = $item['likes'];
  250. $val['supported'] = $this->supported($val['inoherb_id']);
  251. $val['nickname'] = $item['nickname'];
  252. $result[] = $val;
  253. }
  254. return $result;
  255. }
  256. }