Active.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace Active\Lib;
  3. use Dever;
  4. class Active
  5. {
  6. public function action($uid, $active_id, $num, $path, $remark = false, $idcard = false)
  7. {
  8. $user = Dever::db('active/user')->find($uid);
  9. // print_R(json_decode('[{"id":"1","value":"文本"},{"id":"2","value":"文本"},{"id":"3","value":"1"}]'));die;
  10. $data = Dever::db('active/info')->find($active_id);
  11. if ($data['col'] && $data['col'] == 'idcard' && (!$idcard || !preg_match(Dever::rule('idcard'), $idcard))){
  12. Dever::alert('身份证号不能为空或身份证号不正确');
  13. }
  14. // if ($data['act_status'] == 1 || (isset($data['act_start']) && $data['act_start'] && time() < $data['sign_start'])) {
  15. // Dever::alert('活动还未开始报名');
  16. // }
  17. if ($data['act_status'] >=3 || (isset($data['sign_end']) && $data['sign_end'] && time()>$data['sign_end'])){
  18. Dever::alert('活动报名已结束');
  19. }
  20. $insert['active_id'] = $active_id;
  21. $insert['num'] = $num;
  22. $insert['price'] = $num * $data['price'];
  23. if ($remark) {
  24. $insert['remark'] = $remark;
  25. }
  26. if ($path) {
  27. $insert['path'] = $path;
  28. }
  29. $insert['uid'] = $user['id'];
  30. $title = array();
  31. if ($data['attr']) {
  32. if ($data['attr_input']) {
  33. $input['num'] = explode(',',$data['attr_input']);
  34. }
  35. $data['attr'] = explode(',',$data['attr']);
  36. foreach ($data['attr'] as $k => $v) {
  37. foreach ($input['num'] as $k1 => $v1) {
  38. if($k == $k1){
  39. $title[$k]['id'] = $v;
  40. $title[$k]['num'] = $v1;
  41. }
  42. }
  43. }
  44. }
  45. $order = Dever::db('active/order')->getNum(array('uid'=>$uid,'active_id'=>$active_id,'status' => '2,4,5'));
  46. $member = Dever::load('active/lib/user')->agent($user);
  47. if ($data['status'] == 2) {
  48. if (!$member) {
  49. Dever::alert('本活动只能是代理商报名');
  50. }
  51. }
  52. if ($title && $member && $member['title_id'] && $member['title_id'] > 1) {
  53. foreach ($title as $k => $v) {
  54. $title_id = explode(',',$member['title_id']);
  55. if($title_id && in_array($v['id'],$title_id)) {
  56. if ($v['num'] > 0 && $num > $v['num']) {
  57. Dever::alert('您已超过限购数量');
  58. }
  59. if ($order) {
  60. if ($order['total'] && $v['num'] > 0 && ($order['total']+$num) > $v['num']) {
  61. Dever::alert('您已超过限购数量');
  62. }
  63. }
  64. }
  65. }
  66. } else {
  67. if ($order) {
  68. if ($data['num'] > 0 && $order['total'] && ($order['total'] + $num) > $data['num']) {
  69. Dever::alert('您已超过限购数量');
  70. }
  71. } elseif ($data['num']>0 && $num > $data['num'] ) {
  72. Dever::alert('您已超过限购数量');
  73. }
  74. }
  75. $test = Dever::input('test', 1);
  76. $info = false;
  77. if ($test == 1) {
  78. $time = time();
  79. $order_data_where = $insert;
  80. $order_data_where['start'] = $time - 20;
  81. $order_data_where['end'] = $time;
  82. $order_data_where['option']['start'] = array('yes-cdate', '>=');
  83. $order_data_where['option']['end'] = array('yes-cdate', '<=');
  84. $info = Dever::db('active/order')->find($order_data_where);
  85. }
  86. $insert['order_num'] = $this->getOrderId();
  87. if (!$info) {
  88. $ids = Dever::db('active/order')->insert($insert);
  89. } else {
  90. Dever::alert('请不要重复提交');
  91. }
  92. $url = Dever::url();
  93. $refer = 'https://www.churenyiliao.com/';
  94. if (strstr($url, 'uat.')) {
  95. $refer = 'http://guanli.uat.churenyiliao.com/';
  96. }
  97. $refer .= 'data/payback/active.html?order_id=' . $ids;
  98. if ($ids) {
  99. $info = Dever::db('active/order')->find($ids);
  100. $active = Dever::db('active/info')->find($info['active_id']);
  101. $param = array
  102. (
  103. 'project_id' => 6,
  104. 'channel_id' => 1,
  105. 'system_source' => 1,
  106. 'account_id' => 8,
  107. 'uid' => $uid,
  108. 'name' => $active['name'],
  109. //'openid' => $data['openid'],
  110. 'cash' => $info['price'],
  111. 'product_id' => $ids,
  112. 'order_id' => $info['order_num'],
  113. 'h5' => 1,
  114. 'refer' => $refer,
  115. );
  116. $result = Dever::load('pay/api.pay', $param);
  117. if ($result['order']) {
  118. Dever::db('active/order')->update(array('where_id'=>$ids,'url'=>$result['order']));
  119. }
  120. $result['order_num'] = $info['order_num'];
  121. $result['order_id'] = $info['id'];
  122. $jump = Dever::input('jump');
  123. if ($jump == 1) {
  124. Dever::location($result['order']);
  125. }
  126. return $result;
  127. } else {
  128. Dever::alert('提交失败');
  129. }
  130. }
  131. # 支付成功回调 安全加密 设置token
  132. public function success_secure_api_token()
  133. {
  134. $project_id = Dever::input('pay_project_id');
  135. $info = Dever::db('pay/project')->one($project_id);
  136. if ($info) {
  137. return $info['key'];
  138. }
  139. return 'agent_buy_dever_2020';
  140. }
  141. # 支付成功回调 安全加密
  142. public function success_secure_api($param = array())
  143. {
  144. $this->success($param);
  145. }
  146. # 支付成功回调
  147. public function success($param = array())
  148. {
  149. $send = $param ? $param : Dever::preInput('pay_');
  150. $product_id = $send['pay_product_id'];
  151. $order_id = $send['pay_order_id'];
  152. $status = $send['pay_status'];
  153. $cash = $send['pay_cash'];
  154. $msg = $send['pay_msg'];
  155. $order = Dever::db('active/order')->one(array('id' => $product_id, 'clear' => true));
  156. if ($order && $status == 2) {
  157. $code = Dever::db('active/code')->find(array('join_uid'=>$order['uid'],'active_id'=>$order['active_id']));
  158. if (!$code) {
  159. $active = Dever::db('active/info')->find($order['active_id']);
  160. $insert['join_uid'] = $order['uid'];
  161. $insert['active_id'] = $order['active_id'];
  162. $insert['buy_uid'] = $order['uid'];
  163. $insert['order_id'] = $order['id'];
  164. $insert['write_code'] = $this->getCode();
  165. $insert['act_status'] = $active['act_status'];
  166. $ids = Dever::db('active/code')->insert($insert);
  167. if ($ids) {
  168. $update['code_num'] = 1;
  169. }
  170. }
  171. $update['status'] = 2;
  172. $update['where_id'] = $order['id'];
  173. Dever::db('active/order')->update($update);
  174. }
  175. return 'ok';
  176. }
  177. #活动核销码
  178. public function getCode()
  179. {
  180. $where['write_code'] = Dever::rand(6, 0);
  181. $state = Dever::db('active/code')->one($where);
  182. if (!$state) {
  183. return $where['write_code'];
  184. } else {
  185. return $this->getCode();
  186. }
  187. }
  188. #生成订单号
  189. public function getOrderId()
  190. {
  191. $where['order_num'] = Dever::order('D');
  192. $where['clear'] = true;
  193. $state = Dever::db('active/order')->one($where);
  194. if (!$state) {
  195. return $where['order_num'];
  196. } else {
  197. return $this->getOrderId();
  198. }
  199. }
  200. }