inoherb.php 9.7 KB

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