Bind.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\model\ActivityConfigModel;
  4. use app\common\model\CronTableModel;
  5. use app\common\model\GoodsOrderModel;
  6. use app\common\model\PackageMarketingModel;
  7. use app\common\model\PackageModel;
  8. use app\common\model\PackagePurchasedModel;
  9. use app\common\model\PayCodeModel;
  10. use app\common\model\PointsUpdateModel;
  11. use app\common\model\UserBodyDataModel;
  12. use app\common\model\UserCarDataModel;
  13. use app\common\model\UserCyclingDataModel;
  14. use app\common\model\UserCyclingDataStatistics;
  15. use app\common\model\UserFriendsModel;
  16. use app\common\model\UserModel;
  17. use app\common\service\helperService;
  18. use app\common\service\serviceSms;
  19. use think\Cache;
  20. use think\Config;
  21. use think\Controller;
  22. use think\Cookie;
  23. use think\image\Exception;
  24. use think\Session;
  25. use think\Validate;
  26. /**
  27. * 用于用户绑定基本信息
  28. * Class Bind
  29. * @package app\index\controller
  30. */
  31. class Bind extends Controller{
  32. public $user_no = null;
  33. protected function _initialize(){
  34. //user_id作为session的
  35. $this->user_no = Cookie::get('user_no');
  36. $action_name = $this->request->action();
  37. $allows = ['bindEmail','sendMobile','saveMobile','seeCode'];
  38. if(empty($this->user_no) && in_array($action_name,$allows)){
  39. $this->redirect('index/index/start');
  40. exit;
  41. }
  42. }
  43. /**
  44. * 商品兑换支付的回掉
  45. * @param $order_no
  46. * @param $out_trade_no
  47. * @param $total_price
  48. * @param $params
  49. * @return bool
  50. */
  51. private function wxGoodsOrder($order_no,$out_trade_no,$total_price,$params){
  52. $payCodeModel = new PayCodeModel();
  53. $goodsOrderModel = new GoodsOrderModel();
  54. $goodsOrder = $goodsOrderModel->getOne(['order_no'=>$order_no]);
  55. try{
  56. $res = $payCodeModel->saveData([
  57. 'order_no'=>$order_no,
  58. 'pay_type'=>1,
  59. 'pay_no'=>$out_trade_no,
  60. 'price'=>$total_price,
  61. 'add_ts'=>time(),
  62. 'status'=>1,
  63. 'note_json'=>json_encode($params),
  64. 'pathway'=>2,
  65. 'user_no'=>isset($goodsOrder['user_no'])?$goodsOrder['user_no']:'',
  66. ]);
  67. }catch (\Exception $ex){
  68. file_put_contents('wxNotifyPay.mp',"[1]",FILE_APPEND);
  69. file_put_contents('wxNotifyPay.mp',json_encode($params),FILE_APPEND);
  70. file_put_contents('wxNotifyPay.mp',$ex->getMessage(),FILE_APPEND);
  71. file_put_contents('wxNotifyPay.mp',"\n",FILE_APPEND);
  72. return false;
  73. }
  74. if($res === false){
  75. file_put_contents('wxNotifyPay.mp',"[2]",FILE_APPEND);
  76. file_put_contents('wxNotifyPay.mp',json_encode($params),FILE_APPEND);
  77. file_put_contents('wxNotifyPay.mp',"\n",FILE_APPEND);
  78. return false;
  79. }
  80. $goodsOrderModel = new GoodsOrderModel();
  81. $result = $goodsOrderModel->saveData(['status'=>2],['order_no'=>"$order_no"]);
  82. $user_no = $goodsOrder['user_no'];
  83. $userModel = new UserModel();
  84. $user = $userModel->getOne(['user_no'=>"$user_no"]);
  85. serviceSms::exchangeSuccess($user['mobile']);
  86. if($result === false){
  87. file_put_contents('wxNotifyPay.mp',"[3]",FILE_APPEND);
  88. file_put_contents('wxNotifyPay.mp',json_encode($params),FILE_APPEND);
  89. file_put_contents('wxNotifyPay.mp',"\n",FILE_APPEND);
  90. return false;
  91. }
  92. return true;
  93. }
  94. /**
  95. * 套餐支付的回掉
  96. * @param $order_no
  97. * @param $out_trade_no
  98. * @param $total_price
  99. * @param $params
  100. * @return bool
  101. */
  102. private function wxPackagePurchased($order_no,$out_trade_no,$total_price,$params){
  103. $payCodeModel = new PayCodeModel();
  104. $packagePurchasedModel = new PackagePurchasedModel();
  105. $packagePurchased = $packagePurchasedModel->getOne(['package_purchased_no'=>"$order_no"]);
  106. try{
  107. $res = $payCodeModel->saveData([
  108. 'order_no'=>$order_no,
  109. 'pay_type'=>1,
  110. 'pay_no'=>$out_trade_no,
  111. 'price'=>$total_price,
  112. 'add_ts'=>time(),
  113. 'status'=>1,
  114. 'note_json'=>json_encode($params),
  115. 'pathway'=>1,
  116. 'user_no'=>isset($packagePurchased['user_no'])?$packagePurchased['user_no']:'',
  117. ]);
  118. }catch (\Exception $ex){
  119. file_put_contents('wxNotifyPay.mp',"[4]",FILE_APPEND);
  120. file_put_contents('wxNotifyPay.mp',json_encode($params),FILE_APPEND);
  121. file_put_contents('wxNotifyPay.mp',$ex->getMessage(),FILE_APPEND);
  122. file_put_contents('wxNotifyPay.mp',"\n",FILE_APPEND);
  123. return false;
  124. }
  125. if($res === false){
  126. file_put_contents('wxNotifyPay.mp',"[5]",FILE_APPEND);
  127. file_put_contents('wxNotifyPay.mp',json_encode($params),FILE_APPEND);
  128. file_put_contents('wxNotifyPay.mp',"\n",FILE_APPEND);
  129. return false;
  130. }
  131. try {
  132. $packagePurchasedModel = new PackagePurchasedModel();
  133. $result = $packagePurchasedModel->saveData(['real_price' => $total_price, 'status' => 3], ['package_purchased_no' => $order_no, 'status' => 1]);
  134. if ($result === false) {
  135. file_put_contents('wxNotifyPay.mp', "[6]", FILE_APPEND);
  136. file_put_contents('wxNotifyPay.mp', json_encode($params), FILE_APPEND);
  137. file_put_contents('wxNotifyPay.mp', "\n", FILE_APPEND);
  138. return false;
  139. }
  140. //给自己添加优惠条件
  141. $num = $packagePurchased['num'];
  142. $activityConfigModel = new ActivityConfigModel();
  143. $activityConfigList = $activityConfigModel->getMulti(['buy_courses'=>['elt',$num]],"code <> 'NEW'");
  144. //获取优惠信息
  145. $this->getDiscountInformation($activityConfigList,$num,$packagePurchased['user_no']);
  146. $userModel = new UserModel();
  147. $friend_user_no = $packagePurchased['user_no'];
  148. $friend = $userModel->getOne(['user_no'=>"$friend_user_no"]);
  149. serviceSms::buyPackageSuccess($num,$friend['mobile']);
  150. //接下来给邀请码的人,进行一次加入次数
  151. $invite_code = $packagePurchased['invite_code'];
  152. if (empty($invite_code)) {
  153. return false;
  154. }
  155. $user = $userModel->getOne(['invite_code' => $invite_code]);
  156. if (empty($user)) {
  157. return false;
  158. }
  159. if(time()>strtotime('2018-01-01')){
  160. Config::set('converted_radix',Config::get('up2018_converted_radix'));
  161. }
  162. $give_times = floor($num / Config::get('converted_radix'));
  163. if($user['user_no'] != $friend['user_no']){
  164. $day = Config::get('invite')['day'];
  165. //赠送给邀请者的
  166. $packagePurchasedModel->saveData([
  167. 'package_purchased_no' => helperService::createOrderNum(),
  168. 'invite_code' => '',
  169. 'user_no' => "{$user['user_no']}",
  170. 'package_id' => 0,
  171. 'store_id' => Config::get('invite')['store_id'],
  172. 'num' => $give_times,
  173. 'total_price' => 0,
  174. 'status' => 3,
  175. 'day' => $day,
  176. 'expire_ts' => time() + 86400 * $day,
  177. 'real_price' => 0,
  178. 'add_ts' => time(),
  179. 'type' => 4
  180. ]);
  181. serviceSms::getGive($user['mobile'],$user['user_name'],$friend['user_name'],$num,$give_times);
  182. }
  183. //赠送给本人的
  184. $packagePurchasedModel->saveData([
  185. 'package_purchased_no' => helperService::createOrderNum(),
  186. 'invite_code' => '',
  187. 'user_no' => "{$friend_user_no}",
  188. 'package_id' => 0,
  189. 'store_id' => Config::get('invite')['store_id'],
  190. 'num' => $give_times,
  191. 'total_price' => 0,
  192. 'status' => 3,
  193. 'day' => $day,
  194. 'expire_ts' => time() + 86400 * $day,
  195. 'real_price' => 0,
  196. 'add_ts' => time(),
  197. 'type' => 4
  198. ]);
  199. serviceSms::myGive($friend['mobile'],$friend['user_name'],$num,$give_times);
  200. }catch (Exception $ex){
  201. file_put_contents('wxNotifyPay.mp',"[4]",FILE_APPEND);
  202. file_put_contents('wxNotifyPay.mp',json_encode($params),FILE_APPEND);
  203. file_put_contents('wxNotifyPay.mp',$ex->getMessage(),FILE_APPEND);
  204. file_put_contents('wxNotifyPay.mp',"\n",FILE_APPEND);
  205. return false;
  206. }
  207. return true;
  208. }
  209. /**
  210. * 授权信息获取
  211. */
  212. public function setCookie(){
  213. header("Content-Type: text/html; charset=utf-8");
  214. $params = $this->request->param();
  215. $openId = isset($params['openid'])?$params['openid']:'';
  216. $header_img = isset($params['headimgurl'])?$params['headimgurl']:'';
  217. $nick_name = isset($params['nickname'])?$params['nickname']:'';
  218. $userModel = new UserModel();
  219. $user = $userModel->getOne(['open_id'=>"$openId"]);
  220. if($user){
  221. Cookie::set('user_no',$user['user_no']);
  222. }else{
  223. $userNo = HelperService::createOrderNum();
  224. $data=[
  225. 'open_id'=>"$openId",
  226. 'user_no'=>"$userNo",
  227. 'head_img'=>"$header_img",
  228. 'user_name'=>$nick_name
  229. ];
  230. try {
  231. $userModel->saveData($data);
  232. }catch (\Exception $ex){
  233. file_put_contents('userInfo.wx',json_encode($data),FILE_APPEND);
  234. file_put_contents('userInfo.wx',"\n",FILE_APPEND);
  235. $data['user_name'] = 'xinyi'.rand(100,999);
  236. $userModel->saveData($data);
  237. }
  238. Cookie::set('user_no',$userNo);
  239. }
  240. $IndexHtml = 'http://'.$_SERVER['SERVER_NAME']."/index/index/index";
  241. if(Session::has('old_url')){
  242. $IndexHtml = Session::get('old_url');
  243. }
  244. header("HTTP/1.1 301 Moved Permanently");
  245. header("Location:$IndexHtml");
  246. exit;
  247. }
  248. /**
  249. * 解密参数
  250. * [加密规则,请求端 1、json_encode 2、openssl_public_encrypt 3、base64_encode]
  251. * [解密规则,服务端 1、base64_decode 2、openssl_private_decrypt 3、json_decode]
  252. * @param bool $is_get
  253. * @return array
  254. */
  255. private function decryptOpenssl($is_get=false){
  256. if($is_get == false){
  257. $data = file_get_contents("php://input");
  258. }else{
  259. $params = $this->request->param();
  260. if(!isset($params['encrypted'])){
  261. helperService::returnJson([],400,'encrypted is empty');
  262. }
  263. $data = $params['encrypted'];
  264. }
  265. $base64data = base64_decode($data);
  266. //私钥
  267. $privateKey = openssl_pkey_get_private(helperService::$private_key);
  268. $decrypted = '';
  269. foreach (str_split($base64data, 128) as $chunk) {
  270. openssl_private_decrypt($chunk, $decryptData, $privateKey); //私钥解密
  271. $decrypted .= $decryptData;
  272. }
  273. return json_decode($decrypted,true);
  274. }
  275. }