UserService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. $money = config('site.invite_money');
  228. $remark = '邀请推广充值';
  229. $res = model('Wallet')->lockChangeAccountRemain($preUser['id'],$money,'+',$preUser['money'],$remark,103,'money',$userId);
  230. if (!$res['status']) {
  231. throw new Exception($res['msg']);
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. } catch (Exception $e) {
  239. $result['status'] = 0;
  240. $result['msg'] = $e->getMessage();
  241. }
  242. return $result;
  243. }
  244. /**
  245. * 获取用户家族角色
  246. * @return void
  247. */
  248. public function getGuildRole($params=[])
  249. {
  250. $result = [
  251. 'status' => 1,
  252. 'msg' => '操作成功',
  253. 'data' => [],
  254. ];
  255. try {
  256. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  257. $guildId = isset($params['guild_id']) ? $params['guild_id'] : 0;
  258. if (empty($userId) || empty($guildId)) {
  259. throw new Exception('参数错误');
  260. }
  261. $guildWhere['id'] = $guildId;
  262. $guild = model('Guild')->where($guildWhere)->find();
  263. if (empty($guild)) {
  264. throw new Exception('未找到家族信息');
  265. }
  266. $guildRole = 4;//家族角色:1组长,2副族长,3成员,4非成员
  267. if ($userId == $guild['user_id']) {
  268. $guildRole = 1;
  269. }
  270. $guildMemberWhere['guild_id'] = $guildId;
  271. $guildMemberWhere['user_id'] = $userId;
  272. $guildMember = model('GuildMember')->field('id,role')->where($guildMemberWhere)->find();
  273. if (!empty($guildMember)) {
  274. if ($guildMember['role'] == 0) {
  275. $guildRole = 3;
  276. } elseif ($guildMember['role'] == 1) {
  277. $guildRole = 2;
  278. }
  279. }
  280. $result['data'] = [
  281. 'guild_role' => $guildRole,
  282. ];
  283. } catch (Exception $e) {
  284. $result['status'] = 0;
  285. $result['msg'] = $e->getMessage();
  286. }
  287. return $result;
  288. }
  289. /**
  290. * 更新用户消费/魅力/礼物数据
  291. * @return void
  292. */
  293. public function updateGuildMember($params=[])
  294. {
  295. $result = [
  296. 'status' => 1,
  297. 'msg' => '操作成功',
  298. 'data' => [],
  299. ];
  300. try {
  301. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  302. if (!empty($userId)) {
  303. $guildMemberWhere['user_id'] = $userId;
  304. $guildMember = model('GuildMember')->where($guildMemberWhere)->count();
  305. if (!empty($guildMember)) {
  306. $wealth = $charm = $gift_num = 0;
  307. $userJewelLogWhere["type"] = ["in",[0,2,3,5,6,13]];//查看wallet.php文件
  308. $userJewelLogWhere["user_id"] = $userId;
  309. $userJewelLogPay = model('UserJewelLog')
  310. ->field("sum(value) as total_price")
  311. ->where($userJewelLogWhere)
  312. ->select();
  313. $wealth = isset($userJewelLogPay[0]['total_price']) ? (int)$userJewelLogPay[0]['total_price'] : 0;
  314. $userGiftWhere = [];
  315. $userGiftWhere["user_to_id"] = $userId;
  316. $userGift = model('GiftUserParty')
  317. ->field("sum(value) as total_price,sum(number)as gift_total_num")
  318. ->where($userGiftWhere)
  319. ->select();
  320. $charm = isset($userGift[0]['total_price']) ? (int)$userGift[0]['total_price'] : 0;
  321. $giftNum = isset($userGift[0]['gift_total_num']) ? (int)$userGift[0]['gift_total_num'] : 0;
  322. $guildMemberData = ['wealth'=>$wealth,'charm'=>$charm,'gift_num'=>$giftNum];
  323. $r = model('GuildMember')->update($guildMemberData,$guildMemberWhere);
  324. }
  325. }
  326. } catch (Exception $e) {
  327. $result['status'] = 0;
  328. $result['msg'] = $e->getMessage();
  329. }
  330. return $result;
  331. }
  332. }