festval.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: songlintao
  5. * Date: 2017/9/25
  6. * Time: 下午3:30
  7. */
  8. defined( 'InShopNC' ) or exit( 'Access Invalid!' );
  9. require_once(BASE_ROOT_PATH . '/helper/session_helper.php');
  10. require_once(BASE_ROOT_PATH . '/helper/fcode/operator.php');
  11. use user_session\storage;
  12. class festvalControl extends mobileControl
  13. {
  14. const RELATIVE_UPLOAD = "/upfile";
  15. const UPFILE_PATH = BASE_UPLOAD_PATH . self::RELATIVE_UPLOAD;
  16. private $mod_activity_image;
  17. public function __construct() {
  18. parent::__construct();
  19. $this->mod_activity_image = Model('activity_image');
  20. }
  21. private $qixiConfig = [
  22. // 0 => ["common_id"=>2778,"batch_code"=>"HSDX01","goods_name"=>"唇油"],//131
  23. // 0 => ["common_id"=> 1111,"batch_code"=>"QXHD01","goods_name"=>"卸妆水"],
  24. // 1 => ["common_id"=>3400,"batch_code"=>"HSDX01","goods_name"=>"唇油"],
  25. 0 => ["common_id"=>7120,"batch_code"=>"QXHD03","goods_name"=>"唇油"],//兰芝 水润绚彩染唇油
  26. 1 => ["common_id"=>7848,"batch_code"=>"QXHD01","goods_name"=>"卸妆水"],//美宝莲 净澈多效卸妆水
  27. 2 => ["common_id"=>7844,"batch_code"=>"QXHD04","goods_name"=>"喷雾"],// Fa清香亚麻喷雾
  28. 3 => ["common_id"=>8041,"batch_code"=>"QXHD02","goods_name"=>"香皂"],// 安宝笛 鸢尾花味香皂
  29. ];
  30. public function qixiOp()
  31. {
  32. if(session_helper::need_wechat_author())
  33. {
  34. $author = new thrid_author\wxauthor();
  35. $url = urlencode(BASE_SITE_URL . "/mobile/index.php?act=festval&op=qixi");
  36. $url = $author->enter($url);
  37. return self::outsuccess(['direct_uri' => $url],"redirect");
  38. }
  39. $begin = strtotime("2018-08-14");
  40. $end = strtotime("2018-08-18");
  41. if($this->activity_validate($begin,$end)){
  42. return self::outsuccess(null,'festival/TanabataFestival/index');
  43. }else{
  44. return self::outsuccess(null,'festival/end/index');
  45. }
  46. }
  47. private function activity_validate($begin,$end)
  48. {
  49. $now = time();
  50. return ($now >= $begin && $now < $end);
  51. }
  52. public function getAwardOp()
  53. {
  54. $memberid = session_helper::memberid();
  55. if($memberid<=0) {
  56. return self::outerr(errcode::ErrUnLogin,"用户需要登陆");
  57. };
  58. $minfo = new member_info($memberid);
  59. $moblie = $minfo->mobile();
  60. $sid = session_id();
  61. $get = false;
  62. $grabed = false;
  63. $name = "";
  64. $qixiConf = $this->qixiConfig;
  65. foreach ($qixiConf as $key => $item)
  66. {
  67. $command_id = $item['common_id'];
  68. $batch_code = $item['batch_code'];
  69. $goods_name = $item['goods_name'];
  70. $oper = new fcode\operator($command_id,$batch_code,$moblie,$sid);
  71. $fcode = $oper->grabed();
  72. if($fcode) {
  73. $grabed = true;
  74. $name = $goods_name;
  75. $get = true;
  76. break;
  77. }
  78. $fcode = $oper->grab();
  79. if($fcode)
  80. {
  81. $res = $oper->bind($fcode);
  82. if($res){
  83. $name = $goods_name;
  84. $get = true;
  85. break;
  86. }
  87. }
  88. }
  89. if($get) {
  90. return self::outsuccess(["grabed"=>$grabed,"goods_name"=>$name]);
  91. } else {
  92. return self::outerr(errcode::ErrGoodsOutofStock,"七夕活动的奖品已被抽完咯,关注熊猫不要错过下次哦");
  93. }
  94. }
  95. public function indexOp()
  96. {
  97. $this->topup();
  98. $url = BASE_SITE_URL . "/hfive/festval/index.html";
  99. if(session_helper::need_wechat_author())
  100. {
  101. $author = new thrid_author\wxauthor();
  102. $url = $author->enter($url);
  103. }
  104. return self::outsuccess(['direct_uri' => $url],"redirect");
  105. }
  106. private function from_base64($base_input, &$file_name)
  107. {
  108. if(empty($base_input)) {
  109. return false;
  110. }
  111. $regx = '/^(data:\s*image\/(\w+);base64,)/';
  112. if (preg_match($regx, $base_input, $result))
  113. {
  114. $file_type = $result[2];
  115. $content = str_replace( $result[1], '', $base_input);
  116. $file_name = md5($content) .'.'. $file_type;
  117. return base64_decode($content);
  118. } else {
  119. return false;
  120. }
  121. }
  122. function add_imageOp()
  123. {
  124. $image_content = $this->from_base64($_POST['image_data'],$file_name);
  125. $act_id = intval($_POST['act_id']);
  126. if ($image_content == false || $act_id <= 0) {
  127. return self::outerr( errcode::ErrParamter, "参数错误", "festval/error" );
  128. }
  129. $file_path = self::UPFILE_PATH . "/{$file_name}";
  130. if(file_put_contents($file_path,$image_content) == false) {
  131. return self::outerr( errcode::ErrParamter, "保存文件失败", "festval/error" );
  132. }
  133. $datas = [];
  134. {
  135. if(session_helper::logined()) {
  136. $datas['member_id'] = session_helper::memberid();
  137. } else {
  138. $datas['member_id'] = 0;
  139. }
  140. if(session_helper::unionid() != false) {
  141. $datas['unionid'] = session_helper::unionid();
  142. } else {
  143. $datas['unionid'] = "";
  144. }
  145. $datas['act_id'] = $act_id;
  146. $datas['image_path'] = self::RELATIVE_UPLOAD . "/" . $file_name;
  147. $datas['add_time'] = time();
  148. $datas['likes'] = 0;
  149. }
  150. $ret = $this->mod_activity_image->insert($datas);
  151. if($ret > 0) {
  152. return self::outsuccess(null);
  153. } else {
  154. return self::outerr( errcode::ErrParamter, "保存文件失败", "festval/error" );
  155. }
  156. }
  157. function likeOp()
  158. {
  159. $act_id = $_GET['act_id'];
  160. $image_id = $_GET['image_id'];
  161. if($act_id < 0 || $image_id < 0) {
  162. return self::outerr(errcode::ErrParamter,"参数错误");
  163. }
  164. $supporter = new festval_support($act_id,$image_id);
  165. if($supporter->supported()) {
  166. $supporter->unsupport();
  167. $supported = false;
  168. } else {
  169. $supporter->support();
  170. $supported = true;
  171. }
  172. $ret = $this->mod_activity_image->likes($image_id,true);
  173. return self::outsuccess(['supported' => $supported,'likes' => intval($ret['likes'])]);
  174. }
  175. function listOp()
  176. {
  177. $act_id = intval($_POST['act_id']);
  178. if ($act_id <= 0) {
  179. return self::outerr( errcode::ErrParamter, "参数错误", "festval/error" );
  180. }
  181. $items = $this->mod_activity_image->image_list(['act_id' => $act_id],'*',$this->page_size());
  182. $pages = $this->mod_activity_image->gettotalpage();
  183. $result = [];
  184. foreach ($items as $item) {
  185. $result[] = $this->format($item);
  186. }
  187. return self::outsuccess(['blocks' => $result,'mobile_page' => mobile_page($pages)]);
  188. }
  189. private function format($item)
  190. {
  191. $result = [];
  192. $image_id = intval($item['image_id']);
  193. $act_id = intval($item['act_id']);
  194. $result['image_id'] = $image_id;
  195. $supporter = new festval_support($act_id,$image_id);
  196. $result['supported'] = $supporter->supported();
  197. $result['act_id'] = $item['act_id'];
  198. $result['member_id'] = $item['member_id'];
  199. $result['image'] = $this->img_url($item['image_path']);
  200. $result['likes'] = intval($item['likes']);
  201. $result['add_time'] = intval($item['add_time']);
  202. return $result;
  203. }
  204. private function img_url($path)
  205. {
  206. if(strncasecmp($path,"http://",strlen("http://")) == 0) {
  207. $url = $path;
  208. } else {
  209. $url = UPLOAD_SITE_URL . $path;
  210. }
  211. return $url;
  212. }
  213. private function topup()
  214. {
  215. if(session_helper::logined()) {
  216. $pred = new predeposit_helper($_SESSION['member_id'],true);
  217. $pred->topup_bonus($_SESSION['member_mobile']);
  218. }
  219. }
  220. }
  221. class festval_support extends storage
  222. {
  223. private $act_id;
  224. private $image_id;
  225. public function __construct($act_id, $image_id)
  226. {
  227. $this->act_id = intval($act_id);
  228. $this->image_id = intval($image_id);
  229. parent::__construct();
  230. }
  231. public function is_act() {
  232. return ($this->act_id > 0 && $this->image_id <= 0);
  233. }
  234. public function limit_type()
  235. {
  236. return storage::NORMAL_SUPPORT;
  237. }
  238. public function storage_tag()
  239. {
  240. if($this->is_act()) {
  241. return "festval_support";
  242. } else {
  243. return "festval_image_{$this->act_id}";
  244. }
  245. }
  246. public function supported()
  247. {
  248. if($this->is_act()) {
  249. return parent::base_supported($this->act_id);
  250. } else {
  251. return parent::base_supported($this->image_id);
  252. }
  253. }
  254. public function support()
  255. {
  256. if($this->is_act())
  257. {
  258. $ret = parent::base_support($this->act_id);
  259. if($ret) {
  260. $this->add_act_likes();
  261. }
  262. }
  263. else
  264. {
  265. $ret = parent::base_support($this->image_id);
  266. if($ret) {
  267. $this->add_image_likes();
  268. }
  269. }
  270. return $ret;
  271. }
  272. public function unsupport()
  273. {
  274. if($this->is_act())
  275. {
  276. $ret = parent::base_unsupport($this->act_id);
  277. if($ret) {
  278. $this->sub_act_likes();
  279. }
  280. }
  281. else
  282. {
  283. $ret = parent::base_unsupport($this->image_id);
  284. if($ret) {
  285. $this->sub_image_likes();
  286. }
  287. }
  288. return $ret;
  289. }
  290. private function add_act_likes()
  291. {
  292. }
  293. private function sub_act_likes()
  294. {
  295. }
  296. private function add_image_likes()
  297. {
  298. $mod_image = Model('activity_image');
  299. $mod_image->where(['image_id' => $this->image_id])->update([ 'likes' => ['exp', 'likes + 1'] ]);
  300. }
  301. private function sub_image_likes()
  302. {
  303. $mod_image = Model('activity_image');
  304. $mod_image->where(['image_id' => $this->image_id])->update([ 'likes' => ['exp', 'likes - 1'] ]);
  305. }
  306. }