IssueController.class.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /* 应用 - ICO众筹认购 */
  3. namespace Mobile\Controller;
  4. class IssueController extends MobileController
  5. {
  6. protected function _initialize()
  7. {
  8. parent::_initialize();
  9. $allow_action=array("index","details","issuelog", "upbuynum","sendteamjl");
  10. if(!in_array(ACTION_NAME,$allow_action)){
  11. $this->error(L("非法操作"));
  12. }
  13. }
  14. //认购首页
  15. public function index(){
  16. $fields = "id,name,coinname,buycoin,num,price,imgs,lockday,starttime,finishtime,ysnum,sellnum";
  17. $list = M("issue")->where(array('status'=>1))->field($fields)->order("id desc")->select();
  18. // $this->assign('list',$list);
  19. $congif = M("config")->where(array('id'=>1))->field("webissue")->find();
  20. $webissue = $congif['webissue'];
  21. foreach ($list as &$item){
  22. $item['imgs'] = 'http://'.$_SERVER['HTTP_HOST'].'/Upload/public/'.$item['imgs'];
  23. }
  24. $data = [
  25. 'list' => $list,
  26. 'webissue' => 'http://'.$_SERVER['HTTP_HOST'].'/Upload/public/'.$webissue,
  27. ];
  28. $this->ajaxReturn(['code' => 1, 'data' => $data]);
  29. // $this->assign('webissue',$webissue);
  30. // $this->display();
  31. }
  32. //认购项目详情
  33. public function details($id=NULL){
  34. $info = M("issue")->where(array('id'=>$id))->find();
  35. if(empty($info)){
  36. $this->ajaxReturn(['code' => 3, 'info' => L('未登录')]);
  37. }
  38. $uid = userid();
  39. $buycoin = trim($info['buycoin']);
  40. $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
  41. // $this->assign("uid",$uid);
  42. $money = $minfo[$buycoin];
  43. if($money <= 0){
  44. $money = 0;
  45. }
  46. $info['imgs'] = 'http://'.$_SERVER['HTTP_HOST'].'/Upload/public/'.$info['imgs'];
  47. $data = [
  48. 'uid' => $uid,
  49. 'info' => $info,
  50. 'money' => $money,
  51. ];
  52. $this->ajaxReturn(['code' => 1, 'data' => $data]);
  53. // $this->assign('info',$info);
  54. // $this->assign("money",$money);
  55. // $this->display();
  56. }
  57. public function upbuynum($pid=null,$num=null){
  58. if (checkstr($pid) || checkstr($num)) {
  59. $this->ajaxReturn(['code'=>0,'info'=>L('参数错误')]);
  60. }
  61. $uid = userid();
  62. $uinfo = M("user")->where(array('id'=>$uid))->field("id,rzstatus,username,invit_1,invit_2,invit_3,is_tax,taxstatus")->find();
  63. if(empty($uinfo)){
  64. $this->ajaxReturn(['code' => 3, 'info' => L('未登录')]);
  65. }
  66. if($uinfo['rzstatus'] != 2){
  67. $this->ajaxReturn(['code'=>0,'info'=>L('请先完成实名认证')]);
  68. }
  69. //检查该会员是不是需要缴纳税金,如有需要检查有没有缴纳
  70. if($uinfo['is_tax'] == 1){
  71. if($uinfo['taxstatus'] != 1){
  72. $this->ajaxReturn(['code'=>3]);
  73. }
  74. }
  75. $issue = M("issue")->where(array('id'=>$pid))->find();
  76. if(empty($issue)){
  77. $this->ajaxReturn(['code'=>0,'info'=>L('参数错误')]);
  78. }
  79. if(time() < strtotime($issue['starttime'])){
  80. $this->ajaxReturn(['code'=>0,'info'=>L('认购未开始')]);
  81. }
  82. if(time() > strtotime($issue['finishtime'])){
  83. $this->ajaxReturn(['code'=>0,'info'=>L('认购已结束')]);
  84. }
  85. if($issue['state'] != 1){
  86. $this->ajaxReturn(['code'=>0,'info'=>L('禁止认购')]);
  87. }
  88. if($num <= 0){
  89. $this->ajaxReturn(['code'=>0,'info'=>L('请输入认购数量')]);
  90. }
  91. if($num < $issue['min']){
  92. $this->ajaxReturn(['code'=>0,'info'=>L('不能小于最低认购量')]);
  93. }
  94. if($num > $issue['max']){
  95. $this->ajaxReturn(['code'=>0,'info'=>L('不能高于最高认购量')]);
  96. }
  97. //查已经认购的量
  98. $allnum = M("issue_log")->where(array('uid'=>$uid,'pid'=>$pid))->count("num");
  99. if(($allnum + $num) > $issue['allmax']){
  100. $this->ajaxReturn(['code'=>0,'info'=>L('已超出个人认购上限')]);
  101. }
  102. //计算发行剩余量
  103. $surplus = $issue['num'] - $issue['sellnum'] - $issue['ysnum'];
  104. if($surplus < $num){
  105. $this->ajaxReturn(['code'=>0,'info'=>L('超出发行总量')]);
  106. }
  107. //计算支付额度
  108. $allmoney = $num * $issue['price'];
  109. $buycoin = trim(strtolower($issue['buycoin']));
  110. //查会员余额
  111. $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
  112. if($minfo[$buycoin] < $allmoney){
  113. $this->ajaxReturn(['code'=>0,'info'=>L('账户余额不足')]);
  114. }
  115. //组装认购记录
  116. $log['pid'] = $pid;
  117. $log['uid'] = $uid;
  118. $log['account'] = $uinfo['username'];
  119. $log['name'] = $issue['name'];
  120. $log['coinname'] = $issue['coinname'];
  121. $log['buycoin'] = $issue['buycoin'];
  122. $log['price'] = $issue['price'];
  123. $log['num'] = $num;
  124. $log['mum'] = $allmoney;
  125. $log['lockday'] = $issue['lockday'];
  126. $log['addtime'] = date("Y-m-d H:i:s",time());
  127. $log['endtime'] = date("Y-m-d H:i:s",(time() + 86400 *$issue['lockday']));
  128. $log['endday'] = date("Y-m-d",(time() + 86400 *$issue['lockday']));
  129. $log['status'] = 1;
  130. $logre = M("issue_log")->add($log);
  131. //修改项目已出售量
  132. $upre = M("issue")->where(array('id'=>$pid))->setInc('sellnum',$num);
  133. //扣除会员购买金额并写入日志
  134. $decre = M("user_coin")->where(array('userid'=>$uid))->setDec($buycoin,$allmoney);
  135. $decbill['uid'] = $uid;
  136. $decbill['username'] = $uinfo['username'];
  137. $decbill['num'] = $allmoney;
  138. $decbill['coinname'] = $buycoin;
  139. $decbill['afternum'] = $minfo[$buycoin] - $allmoney;
  140. $decbill['type'] = 11;
  141. $decbill['addtime'] = date("Y-m-d H:i:s",time());
  142. $decbill['st'] = 2;
  143. $decbill['remark'] = $issue['name'].L("认购");
  144. $decbillre = M("bill")->add($decbill);
  145. //增加会员认购币金额(冻结)并写入日志
  146. $coinnamed = $issue['coinname']."d";
  147. $incre = M("user_coin")->where(array('userid'=>$uid))->setInc($coinnamed,$num);
  148. $incbill['uid'] = $uid;
  149. $incbill['username'] = $uinfo['username'];
  150. $incbill['num'] = $num;
  151. $incbill['coinname'] = $issue['coinname'];
  152. $incbill['afternum'] = $minfo[$coinnamed] - $num;
  153. $incbill['type'] = 12;
  154. $incbill['addtime'] = date("Y-m-d H:i:s",time());
  155. $incbill['st'] = 1;
  156. $incbill['remark'] = $issue['name'].L("认购");
  157. $incbillre = M("bill")->add($incbill);
  158. if($logre && $upre && $decre && $decbillre && $incre && $incbillre){
  159. $jlcoin = $issue['jlcoin'];
  160. if($uinfo['invit_1'] > 0){
  161. $onebl = $issue['one_jl'];
  162. $onemoney = $num * $onebl / 100;
  163. if($onemoney > 0){
  164. $this->sendteamjl($uinfo['invit_1'],$onemoney,$jlcoin,1);
  165. }
  166. }
  167. if($uinfo['invit_2'] > 0){
  168. $twobl = $issue['two_jl'];
  169. $twomoney = $num * $twobl / 100;
  170. $this->sendteamjl($uinfo['invit_2'],$twomoney,$jlcoin,2);
  171. }
  172. if($uinfo['invit_3'] > 0){
  173. $threebl = $issue['three_jl'];
  174. $threemoney = $num * $threebl / 100;
  175. $this->sendteamjl($uinfo['invit_3'],$threemoney,$jlcoin,3);
  176. }
  177. $this->ajaxReturn(['code'=>1,'info'=>L('认购成功')]);
  178. }else{
  179. $this->ajaxReturn(['code'=>0,'info'=>L('认购失败')]);
  180. }
  181. }
  182. //派发团队奖励
  183. public function sendteamjl($uid,$money,$coinanme,$invit){
  184. $coinnamed = $coinanme."d";
  185. $uinfo = M("user")->where(array('id'=>$uid))->field("id,username")->find();
  186. $minfo = M("user_coin")->where(array('userid'=>$uid))->find();
  187. M("user_coin")->where(array('userid'=>$uid))->setInc($coinnamed,$money);
  188. if($invit == 1){
  189. $type = 13;
  190. $str = L('一代会员认购奖励');
  191. }elseif($invit == 2){
  192. $type = 14;
  193. $str = L('二代会员认购奖励');
  194. }elseif($invit == 3){
  195. $type = 15;
  196. $str = L('三代会员认购奖励');
  197. }
  198. $incbill['uid'] = $uid;
  199. $incbill['username'] = $uinfo['username'];
  200. $incbill['num'] = $money;
  201. $incbill['coinname'] = $coinanme;
  202. $incbill['afternum'] = $minfo[$coinnamed] + $money;
  203. $incbill['type'] = $type;
  204. $incbill['addtime'] = date("Y-m-d H:i:s",time());
  205. $incbill['st'] = 1;
  206. $incbill['remark'] = $str;
  207. M("bill")->add($incbill);
  208. }
  209. //认购记录
  210. public function issuelog(){
  211. if (!userid()) {
  212. $this->ajaxReturn(['code' => 3, 'info' => L('未登录')]);
  213. }
  214. $uid = userid();
  215. $type = I('get.type');
  216. $page = I('get.page');
  217. $limit = I('get.limit');
  218. if ($type == 1){
  219. //全部认购
  220. $list = M("issue_log")->where(array('uid'=>$uid))->page($page, $limit)->order("id desc")->select();
  221. }elseif ($type == 2){
  222. //冻结中的认购
  223. $list = M("issue_log")->where(array('uid'=>$uid,'status'=>1))->page($page, $limit)->order("id desc")->select();
  224. }elseif ($type == 3){
  225. //已解冻
  226. $list = M("issue_log")->where(array('uid'=>$uid,'status'=>2))->page($page, $limit)->order("id desc")->select();
  227. }
  228. $list = count($list) == 0 ? [] : $list;
  229. $this->ajaxReturn(['code' => 1, 'data' => $list]);
  230. }
  231. }
  232. ?>