UserService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. namespace app\common\service;
  3. use fast\Random;
  4. use think\Exception;
  5. class UserService
  6. {
  7. /**
  8. * 获取请求参数
  9. * @return void
  10. */
  11. public function faceAuth($params=[])
  12. {
  13. $result = [
  14. 'status' => 1,
  15. 'msg' => '获取成功',
  16. 'data' => [],
  17. ];
  18. try {
  19. $idCard = isset($params['id_card']) ? $params['id_card'] : '';
  20. $realName = isset($params['real_name']) ? $params['real_name'] : '';
  21. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  22. $tencentConfig = config('tencent_yun');
  23. $sercrtId = isset($tencentConfig['SecretId']) ? $tencentConfig['SecretId'] : '';
  24. $sercrtKey = isset($tencentConfig['SecretKey']) ? $tencentConfig['SecretKey'] : '';
  25. //获取token
  26. $token_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/access_token?app_id='.$sercrtId.'&secret='.$sercrtKey.'&grant_type=client_credential&version=1.0.0';
  27. $token_result = file_get_contents($token_url);
  28. if (!$token_result) {
  29. throw new Exception('您的网络开小差啦1~');
  30. }
  31. $token_result = json_decode($token_result, true);
  32. if ($token_result['code'] != 0) {
  33. throw new Exception('您的网络开小差啦2~');
  34. }
  35. $token = $token_result['access_token'];
  36. //获取签名鉴权参数ticket
  37. $ticket_url = 'https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/api_ticket?app_id='.$sercrtId.'&access_token='.$token.'&type=SIGN&version=1.0.0';
  38. $ticket_result = file_get_contents($ticket_url);
  39. if (!$ticket_result) {
  40. throw new Exception('您的网络开小差啦3~');
  41. }
  42. $ticket_result = json_decode($ticket_result, true);
  43. if ($ticket_result['code'] != 0) {
  44. throw new Exception('您的网络开小差啦4~');
  45. }
  46. $ticket = $ticket_result['tickets'][0]['value'];
  47. //获取签名
  48. $sign_data = [
  49. 'wbappid' => $sercrtId,
  50. 'userId' => (string)$userId,
  51. 'version' => '1.0.0',
  52. 'ticket' => $ticket,
  53. 'nonce' => Random::alnum(32)
  54. ];
  55. asort($sign_data);//排序
  56. $sign_string = join('', $sign_data);
  57. $sign = sha1($sign_string);
  58. //上传身份信息
  59. $orderNo = getMillisecond() . $userId . mt_rand(1, 1000); //商户请求的唯一标识
  60. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/server/getAdvFaceId?orderNo='.$orderNo;
  61. $data = [
  62. 'webankAppId' => $sercrtId,
  63. 'orderNo' => $orderNo,
  64. 'userId' => (string)$userId,
  65. 'name' => $realName,//姓名
  66. 'idNo' => $idCard,//证件号
  67. 'version' => '1.0.0',
  68. 'sign' => $sign,
  69. 'nonce' => $sign_data['nonce']
  70. ];
  71. $res = curl_post($url,json_encode($data, 320), ['Content-Type: application/json']);
  72. $result['data'] = json_decode($res,true);
  73. $result['data']['nonce'] = $sign_data['nonce'];
  74. $result['data']['sign'] = $sign;
  75. } catch (Exception $e) {
  76. $result['status'] = 0;
  77. $result['msg'] = $e->getMessage();
  78. }
  79. return $result;
  80. }
  81. /**
  82. * 阿里实名认证二要素
  83. * https://market.aliyun.com/products/57000002/cmapi026109.html
  84. * @return void
  85. */
  86. public function aliCheck($params=[])
  87. {
  88. $result = [
  89. 'status' => 1,
  90. 'msg' => '',
  91. 'data' => [],
  92. ];
  93. try {
  94. $idCard = isset($params['id_card']) ? $params['id_card'] : '';
  95. $realName = isset($params['real_name']) ? $params['real_name'] : '';
  96. $aliyunConfig = config('ali_yun');
  97. $host = "https://eid.shumaidata.com";
  98. $path = "/eid/check";
  99. $method = "POST";
  100. $appcode = isset($aliyunConfig['app_code']) ? $aliyunConfig['app_code'] : '';
  101. $headers = ['Content-Type: application/json'];
  102. array_push($headers, "Authorization:APPCODE " . $appcode);
  103. $querys = "idcard=".$idCard."&name=".urlencode($realName);
  104. $bodys = "";
  105. $url = $host . $path . "?" . $querys;
  106. $curl = curl_init();
  107. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  108. curl_setopt($curl, CURLOPT_URL, $url);
  109. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  110. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  111. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  112. //设定返回信息中是否包含响应信息头,启用时会将头文件的信息作为数据流输出,true 表示输出信息头, false表示不输出信息头
  113. //如果需要将字符串转成json,请将 CURLOPT_HEADER 设置成 false
  114. curl_setopt($curl, CURLOPT_HEADER, false);
  115. if (1 == strpos("$".$host, "https://"))
  116. {
  117. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  118. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  119. }
  120. $returnRes = curl_exec($curl);
  121. curl_close($curl);
  122. $resultData = json_decode($returnRes,true);
  123. if (isset($resultData['code']) && !empty($resultData['code'])) {
  124. throw new Exception($resultData['message']);
  125. }
  126. if (isset($resultData['result']['res']) && $resultData['result']['res'] != 1) {
  127. throw new Exception($resultData['result']['description']);
  128. }
  129. $result['data'] = $resultData;
  130. } catch (Exception $e) {
  131. $result['status'] = 0;
  132. $result['msg'] = $e->getMessage();
  133. }
  134. return $result;
  135. }
  136. /**
  137. * 阿里银行卡三要素
  138. * https://market.aliyun.com/products/57000002/cmapi00063283.html
  139. * @return void
  140. */
  141. public function bankCheck($params=[])
  142. {
  143. $result = [
  144. 'status' => 1,
  145. 'msg' => '',
  146. 'data' => [],
  147. ];
  148. try {
  149. $bankcard = isset($params['bank_no']) ? $params['bank_no'] : '';//银行卡
  150. $idCard = isset($params['id_card']) ? $params['id_card'] : '';//身份证号
  151. $realName = isset($params['real_name']) ? $params['real_name'] : '';//姓名
  152. $aliyunConfig = config('ali_yun');
  153. $host = "https://sxbank3v2.market.alicloudapi.com";
  154. $path = "/bankcard3/check";
  155. $method = "POST";
  156. $appcode = isset($aliyunConfig['app_code']) ? $aliyunConfig['app_code'] : '';
  157. $headers = [];
  158. array_push($headers, "Authorization:APPCODE " . $appcode);
  159. $querys = "bankcard=".$bankcard."&idCard=".$idCard."&name=".urlencode($realName);
  160. $bodys = "";
  161. $url = $host . $path . "?" . $querys;
  162. $curl = curl_init();
  163. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  164. curl_setopt($curl, CURLOPT_URL, $url);
  165. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  166. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  167. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  168. //设定返回信息中是否包含响应信息头,启用时会将头文件的信息作为数据流输出,true 表示输出信息头, false表示不输出信息头
  169. //如果需要将字符串转成json,请将 CURLOPT_HEADER 设置成 false
  170. curl_setopt($curl, CURLOPT_HEADER, false);
  171. if (1 == strpos("$".$host, "https://"))
  172. {
  173. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  174. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  175. }
  176. $returnRes = curl_exec($curl);
  177. curl_close($curl);
  178. $resultData = json_decode($returnRes,true);
  179. if (isset($resultData['code']) && !empty($resultData['code'])) {
  180. throw new Exception($resultData['msg']);
  181. }
  182. $aliResult = isset($resultData['data']['result']) ? $resultData['data']['result'] : 0;
  183. if ($aliResult != 1) {
  184. //核查结果(1:一致,2:不一致,3:无效卡号或卡状态异常)
  185. $aliMsg = '';
  186. if ($aliResult == 2) {
  187. $aliMsg = '不一致';
  188. } elseif ($aliResult == 3) {
  189. $aliMsg = '无效卡号或卡状态异常';
  190. }
  191. throw new Exception($aliMsg);
  192. }
  193. $result['data'] = $resultData;
  194. } catch (Exception $e) {
  195. $result['status'] = 0;
  196. $result['msg'] = $e->getMessage();
  197. }
  198. return $result;
  199. }
  200. /**
  201. * 邀请赠送金额
  202. * @return void
  203. */
  204. public function inviteMoney($params=[])
  205. {
  206. $result = [
  207. 'status' => 1,
  208. 'msg' => '',
  209. 'data' => [],
  210. ];
  211. try {
  212. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  213. if (!empty($userId)) {
  214. $field = 'id,pre_userid,money';
  215. $where['id'] = $userId;
  216. $user = model('User')->field($field)->where($where)->find();
  217. if (!empty($user)) {
  218. if (!empty($user['pre_userid'])) {
  219. $preWhere['id'] = $user['pre_userid'];
  220. $preUser = model('User')->field($field)->where($preWhere)->find();
  221. if (!empty($preUser)) {
  222. //查看是否赠送过金额
  223. $moneyLogWhere['invite_user_id'] = $userId;
  224. $moneyLogWhere['type'] = 103;
  225. $moneyLog = model('UserMoneyLog')->where($moneyLogWhere)->find();
  226. if (empty($moneyLog)) {
  227. //充值金额满足才能获取充值奖励
  228. $inviteMaxMoney = config('site.invite_max_money');
  229. $recharWhere['user_id'] = $userId;
  230. $recharWhere['status'] = 1;
  231. $userRecharMoney = model('RecharOrder')->where($recharWhere)->sum('money');
  232. if ($userRecharMoney >= $inviteMaxMoney) {
  233. $money = config('site.invite_money');
  234. if ($money > 0) {
  235. $remark = '邀请推广充值';
  236. $res = model('Wallet')->lockChangeAccountRemain($preUser['id'],$money,'+',$preUser['money'],$remark,103,'money',$userId);
  237. if (!$res['status']) {
  238. throw new Exception($res['msg']);
  239. }
  240. }
  241. $inviteGift = config('site.invite_gift');
  242. if ($inviteGift > 0) {
  243. $inviteGiftNum = config('site.invite_gift_num');
  244. $giftWhere['id'] = $inviteGift;
  245. $gift = model('Gift')->where($giftWhere)->find();
  246. if (!empty($gift) && $inviteGiftNum > 0) {
  247. $userGiftBackWhere['user_id'] = $preUser['id'];
  248. $userGiftBackWhere['invite_user_id'] = $userId;
  249. $userGiftBackWhere['get_way'] = 5;
  250. $userGiftBack = model('GiftBack')->where($userGiftBackWhere)->find();
  251. if (empty($userGiftBack)) {
  252. for($i=1;$i<=$inviteGiftNum;$i++) {
  253. $giftBack[] = [
  254. 'user_id' => $preUser['id'],//用户ID
  255. 'gift_id' => $inviteGift,//礼物ID
  256. 'name' => $gift['name'],//礼物名称
  257. 'image' => $gift['image'],//礼物静图
  258. 'gif_image' => $gift['special'],//礼物动图
  259. 'value' => $gift['value'],//购买价值
  260. 'number' => 1,//数量
  261. 'get_way' => 5,//获得途径:1=活动获得,2=充值购买,3=爵位赠送,4=签到赠送,5=邀请获得
  262. 'createtime' => time(),
  263. ];
  264. }
  265. //$giftBackRes = model('GiftBack')->insertGetId($giftBack);
  266. $giftBackRes = model('GiftBack')->insertAll($giftBack);
  267. if (!$giftBackRes) {
  268. throw new Exception('赠送礼物失败');
  269. }
  270. }
  271. }
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. }
  279. } catch (Exception $e) {
  280. $result['status'] = 0;
  281. $result['msg'] = $e->getMessage();
  282. }
  283. return $result;
  284. }
  285. /**
  286. * 获取用户家族角色
  287. * @return void
  288. */
  289. public function getGuildRole($params=[])
  290. {
  291. $result = [
  292. 'status' => 1,
  293. 'msg' => '操作成功',
  294. 'data' => [],
  295. ];
  296. try {
  297. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  298. $guildId = isset($params['guild_id']) ? $params['guild_id'] : 0;
  299. if (empty($userId) || empty($guildId)) {
  300. throw new Exception('参数错误');
  301. }
  302. $guildWhere['id'] = $guildId;
  303. $guild = model('Guild')->where($guildWhere)->find();
  304. if (empty($guild)) {
  305. throw new Exception('未找到家族信息');
  306. }
  307. $guildRole = 4;//家族角色:1组长,2副族长,3成员,4非成员
  308. if ($userId == $guild['user_id']) {
  309. $guildRole = 1;
  310. }
  311. $guildMemberWhere['guild_id'] = $guildId;
  312. $guildMemberWhere['user_id'] = $userId;
  313. $guildMember = model('GuildMember')->field('id,role')->where($guildMemberWhere)->find();
  314. if (!empty($guildMember)) {
  315. if ($guildMember['role'] == 0) {
  316. $guildRole = 3;
  317. } elseif ($guildMember['role'] == 1) {
  318. $guildRole = 2;
  319. }
  320. }
  321. $result['data'] = [
  322. 'guild_role' => $guildRole,
  323. ];
  324. } catch (Exception $e) {
  325. $result['status'] = 0;
  326. $result['msg'] = $e->getMessage();
  327. }
  328. return $result;
  329. }
  330. /**
  331. * 更新用户消费/魅力/礼物数据
  332. * @return void
  333. */
  334. public function updateGuildMember($params=[])
  335. {
  336. $result = [
  337. 'status' => 1,
  338. 'msg' => '操作成功',
  339. 'data' => [],
  340. ];
  341. try {
  342. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  343. if (!empty($userId)) {
  344. $guildMemberWhere['user_id'] = $userId;
  345. $guildMember = model('GuildMember')->where($guildMemberWhere)->count();
  346. if (!empty($guildMember)) {
  347. $wealth = $charm = $gift_num = 0;
  348. $userJewelLogWhere["type"] = ["in",[0,2,3,5,6,13]];//查看wallet.php文件
  349. $userJewelLogWhere["user_id"] = $userId;
  350. $userJewelLogPay = model('UserJewelLog')
  351. ->field("sum(value) as total_price")
  352. ->where($userJewelLogWhere)
  353. ->select();
  354. $wealth = isset($userJewelLogPay[0]['total_price']) ? (int)$userJewelLogPay[0]['total_price'] : 0;
  355. $userGiftWhere = [];
  356. $userGiftWhere["user_to_id"] = $userId;
  357. $userGift = model('GiftUserParty')
  358. ->field("sum(value) as total_price,sum(number)as gift_total_num")
  359. ->where($userGiftWhere)
  360. ->select();
  361. $charm = isset($userGift[0]['total_price']) ? (int)$userGift[0]['total_price'] : 0;
  362. $giftNum = isset($userGift[0]['gift_total_num']) ? (int)$userGift[0]['gift_total_num'] : 0;
  363. $guildMemberData = ['wealth'=>$wealth,'charm'=>$charm,'gift_num'=>$giftNum];
  364. $r = model('GuildMember')->update($guildMemberData,$guildMemberWhere);
  365. if (!$r) {
  366. throw new Exception('更新失败');
  367. }
  368. }
  369. }
  370. } catch (Exception $e) {
  371. $result['status'] = 0;
  372. $result['msg'] = $e->getMessage();
  373. }
  374. return $result;
  375. }
  376. }