Third.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-05-06
  7. *
  8. *
  9. */
  10. namespace app\controller\api;
  11. use app\common\repositories\merchant\order\OrderGzcRepository;
  12. use app\common\repositories\merchant\order\OrderMerchantRepository;
  13. use app\common\repositories\user\UserRepository;
  14. use app\controller\api\user\Volunteer;
  15. use app\controller\api\util\Signature;
  16. use app\models\routine\RoutineQrcode;
  17. use app\traits\HaikeApiRequest;
  18. use app\traits\HuiPay;
  19. use app\validate\api\PaymentCreate;
  20. use crmeb\basic\BaseController;
  21. use think\exception\ValidateException;
  22. use think\facade\Db;
  23. use think\facade\Log;
  24. use think\facade\Request;
  25. /**
  26. * Class Auth
  27. * @package app\controller\api
  28. * @author xaboy
  29. * @day 2020-05-06
  30. */
  31. class Third extends BaseController
  32. {
  33. use HaikeApiRequest;
  34. use HuiPay;
  35. public function thirdRegister(UserRepository $repository)
  36. {
  37. $data = $this->request->params(['phone', 'spread', 'sign']);
  38. Log::channel("gong")->info("三方注册:" . json_encode($data));
  39. // 校验sign
  40. $checkSign = Signature::thirdCheckSign($data, Request::header());
  41. if (!$checkSign) {
  42. Log::channel("gong")->error('sign校验失败');
  43. return app('json')->fail('sign校验失败');
  44. }
  45. $user = $repository->accountByUser($data['phone']);
  46. if (!$user) {
  47. // 注册
  48. $user = $repository->registr($data['phone'], '', 'THIRD', intval($data['spread']));
  49. if ($data['spread']) {
  50. $repository->bindSpread($user, intval($data['spread']));
  51. }
  52. }
  53. $user = $repository->mainUser($user);
  54. $repository->loginAfter($user);
  55. $data = Db::name('user')->field('uid,phone,create_time')->where('uid', $user['uid'])->find();
  56. $data['certification'] = Db::name("user_certification")->field('user_name,mobile,user_card')->where(["uid" => $user['uid'], 'status' => 1])->find();
  57. $data['unsettled_pension'] = Db::name('gzc_logs')->where('success', 0)->where('uid', $user['uid'])->sum('pension');
  58. $data['have_already'] = Db::name('gzc_logs')->where('success', 1)->where('uid', $user['uid'])->sum('pension');
  59. $birth_data = substr($data['certification']["user_card"], 6, 8);
  60. $birth = strtotime(substr($data['certification']["user_card"], 6, 8));
  61. $old_age = $this->getSex($data['certification']["user_card"]) ? 60 : 55;
  62. $age = $this->getAgeByID($birth);
  63. if ($age < $old_age) {
  64. $yes = $old_age - $age;
  65. $data["birthday60"] = date("Y-" . substr($birth_data, 4, 2) . '-' . substr($birth_data, 6, 2), strtotime("+" . $yes . " year"));
  66. }
  67. if ($age == $old_age) {
  68. $data['can_withdraw'] = 1;
  69. $data["birthday60"] = date("Y-m-d", $birth);
  70. }
  71. if ($age > $old_age) {
  72. $data['can_withdraw'] = 1;
  73. $yes = $age - $old_age;
  74. $data["birthday60"] = date("Y-" . substr($birth_data, 4, 2) . '-' . substr($birth_data, 6, 2), strtotime("-" . $yes . " year"));
  75. }
  76. return app('json')->success($data);
  77. }
  78. public function thirdGzc(UserRepository $repository)
  79. {
  80. $data = $this->request->params(['uid', 'pension', 'out_trade_no','sign']);
  81. Log::channel("gong")->info("三方注册:" . json_encode($data));
  82. // 校验sign
  83. $checkSign = Signature::thirdCheckSign($data, Request::header());
  84. if (!$checkSign) {
  85. Log::channel("gong")->error('sign校验失败');
  86. return app('json')->fail('sign校验失败');
  87. }
  88. $uid = $data['uid'];
  89. $pension = $data['pension'];
  90. $data = Db::name('user')->field('uid,phone,create_time')->where('uid', $uid)->find();
  91. if (!$data)
  92. return app('json')->fail('该用户未注册');
  93. $certification = Db::name("user_certification")->where(["uid" => $uid, 'status' => 1])->find();
  94. if (!$certification)
  95. return app('json')->fail('该用户未实名');
  96. $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  97. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  98. Db::transaction(function () use ($uid, $certification, $pension, $OrderGzcRepository, $OrderMerchantRepository,$data) {
  99. //新增入账明细记录
  100. $orderId = $OrderMerchantRepository->getNewOrderId('IN10');
  101. $create = [
  102. 'uid' => $uid,
  103. 'platform_no' => $orderId,
  104. 'user_name' => $certification['user_name'],
  105. 'mobile' => $certification['mobile'],
  106. 'user_card' => $certification['user_card'],
  107. 'pension' => $pension,
  108. 'order_type' => 10,
  109. 'out_trade_no' => $data['out_trade_no']
  110. ];
  111. $test_user = Db::name('user')->where('uid', $uid)->value('test_user');
  112. if ($test_user == 0) {
  113. $OrderGzcRepository->create($create);
  114. }
  115. });
  116. return app('json')->success("true");
  117. }
  118. public function thirdGzcList(UserRepository $repository)
  119. {
  120. $data = $this->request->params(['uid', 'start_time', 'end_time', 'page', 'limit', 'sign']);
  121. Log::channel("gong")->info("三方注册:" . json_encode($data));
  122. // 校验sign
  123. $checkSign = Signature::thirdCheckSign($data, Request::header());
  124. if (!$checkSign) {
  125. Log::channel("gong")->error('sign校验失败');
  126. return app('json')->fail('sign校验失败');
  127. }
  128. $data = Db::name('gzc_logs')
  129. ->field('id,uid,user_name,mobile,user_card,pension,success,create_time')
  130. ->where('id', '>', 2538)
  131. ->when($data['uid'], function ($query, $data) {
  132. $query->where('uid', '=', $data);
  133. })
  134. ->when($data['start_time'], function ($query, $data) {
  135. $query->where('create_time', '>', $data);
  136. })
  137. ->when($data['end_time'], function ($query, $data) {
  138. $query->where('create_time', '<', $data);
  139. })
  140. ->page($data['page'], $data['limit'])
  141. ->select()
  142. ->toArray();
  143. return app('json')->success($data);
  144. }
  145. public function thirdCertification()
  146. {
  147. $data = $this->request->params(['uid', 'mobile', 'user_name', 'user_card', 'sign']);
  148. Log::channel("gong")->info("三方注册:" . json_encode($data));
  149. // 校验sign
  150. $checkSign = Signature::thirdCheckSign($data, Request::header());
  151. if (!$checkSign) {
  152. Log::channel("gong")->error('sign校验失败');
  153. return app('json')->fail('sign校验失败');
  154. }
  155. unset($data['sign']);
  156. $renzeng = Db::name("user_certification")->where("uid", $data['uid'])->find();
  157. if ($renzeng) {
  158. } else {
  159. $res = Db::name('user_certification')->insert($data);
  160. }
  161. return app('json')->success([
  162. 'user_name' => $data['user_name'],
  163. 'age' => $this->getAgeByID($data['user_card']),
  164. 'user_card' => $data['user_card'],
  165. ]);
  166. }
  167. public function payBalance()
  168. {
  169. $data = $this->request->params(['money', 'sign']);
  170. Log::channel("gong")->info("三方支付:" . json_encode($data));
  171. // 校验sign
  172. $checkSign = Signature::thirdCheckSign($data, Request::header());
  173. if (!$checkSign) {
  174. Log::channel("gong")->error('sign校验失败');
  175. return app('json')->fail('sign校验失败');
  176. }
  177. }
  178. public function payCreate()
  179. {
  180. $data = $this->request->params(['pay_type', 'pay_amt', 'order_no', 'goods_title', 'goods_desc', 'notify_url', 'sign']);
  181. try {
  182. validate(PaymentCreate::class)->check($data);
  183. } catch (ValidateException $exception) {
  184. return app('json')->fail($exception->getMessage());
  185. }
  186. Log::channel("gong")->info("三方注册:" . json_encode($data));
  187. // 校验sign
  188. $checkSign = Signature::thirdCheckSign($data, Request::header());
  189. if (!$checkSign) {
  190. Log::channel("gong")->error('sign校验失败');
  191. return app('json')->fail('sign校验失败');
  192. }
  193. $payType = $data['pay_type'];
  194. $payMoney = $data['pay_amt'];
  195. $pay_user_id = 'oGtg_6KAbu-DKWPIruXccOG5VOmw';
  196. if ($payType == 'WX') {
  197. $pay_wx = systemConfig('pay_wx');
  198. //微信
  199. if ($pay_wx == 3) {
  200. $member_id = Db::name('merchant_member')->where('mer_id', 324)->where("type", 3)->value('member_id');
  201. $key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  202. $app_id = 'app_383a75dd-c03d-44ab-8f58-92c60fe9b9d0';
  203. } else {
  204. $member_id = Db::name('merchant_member')->where('mer_id', 324)->where("type", 2)->value('member_id');
  205. $key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  206. $app_id = 'app_0827b960-b932-45ec-b800-4e850091b419';
  207. }
  208. $type = '2';
  209. $pay_channel = 'wx_pub';
  210. $expend = [
  211. 'open_id' => $pay_user_id
  212. ];
  213. } elseif ($payType == 'ALI') {
  214. $member_id = Db::name('merchant_member')->where('mer_id', 324)->where("type", 1)->value('member_id');
  215. $app_id = 'app_44108546-d27a-48ee-88c1-84b45b75114f';
  216. $type = '1';
  217. $pay_channel = 'alipay_pub';
  218. $expend = [
  219. 'buyer_id'=>$pay_user_id
  220. ];
  221. $key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  222. };
  223. if ($payMoney == '0.00') {
  224. $div_members = [
  225. [
  226. 'member_id' => 0,
  227. 'amount' => sprintf("%01.2f",$payMoney),
  228. 'fee_flag' => 'Y'
  229. ]
  230. ];
  231. } else {
  232. $div_members = [
  233. [
  234. 'member_id' => $member_id,
  235. 'amount' => sprintf("%01.2f",$payMoney* 0.8) ,
  236. 'fee_flag' => 'Y'
  237. ],
  238. [
  239. 'member_id' => 0,
  240. 'amount' => sprintf("%01.2f",$payMoney * 0.2),
  241. 'fee_flag' => 'N'
  242. ]
  243. ];
  244. }
  245. $params = [
  246. 'order_no' => $data['order_no'],
  247. 'app_id' => $app_id,
  248. 'pay_channel' => $pay_channel,
  249. 'pay_amt' => (string)sprintf("%01.2f",$payMoney),
  250. 'goods_title' => $data['goods_title'],
  251. 'goods_desc' => $data['goods_desc'],
  252. 'device_info' => [
  253. 'device_ip' => app('request')->ip()
  254. // 'device_ip'=>'115.171.134.68'
  255. ],
  256. 'expend' => json_encode($expend),
  257. 'div_members' => json_encode($div_members),
  258. 'fee_mode' => 'I',
  259. 'notify_url' => $data['notify_url']
  260. ];
  261. $res = $this->Payment_create_traits($params, $type, $key);
  262. if (!empty($res['error_code'])) {
  263. return app('json')->fail($res['error_msg']);
  264. } else {
  265. return app('json')->success($res);
  266. }
  267. }
  268. public function getSex($cid)
  269. {
  270. //根据身份证号,自动返回性别
  271. $sexint = (int)substr($cid, 16, 1);
  272. return $sexint % 2 === 0 ? false : true;
  273. }
  274. public function getAgeByID($id)
  275. {
  276. //过了这年的生日才算多了1周岁
  277. if (empty($id)) return '';
  278. $date = strtotime(substr($id, 6, 8));
  279. //获得出生年月日的时间戳
  280. $today = strtotime('today');
  281. //获得今日的时间戳
  282. $diff = floor(($today - $date) / 86400 / 365);
  283. //得到两个日期相差的大体年数
  284. //strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比
  285. $age = strtotime(substr($id, 6, 8) . ' +' . $diff . 'years') > $today ? ($diff + 1) : $diff;
  286. return $age;
  287. }
  288. }