UserService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. if ($money > 0) {
  229. $remark = '邀请推广充值';
  230. $res = model('Wallet')->lockChangeAccountRemain($preUser['id'],$money,'+',$preUser['money'],$remark,103,'money',$userId);
  231. if (!$res['status']) {
  232. throw new Exception($res['msg']);
  233. }
  234. }
  235. $inviteGift = config('site.invite_gift');
  236. if ($inviteGift > 0) {
  237. $inviteGiftNum = config('site.invite_gift_num');
  238. $giftWhere['id'] = $inviteGift;
  239. $gift = model('Gift')->where($giftWhere)->find();
  240. if (!empty($gift) && $inviteGiftNum > 0) {
  241. $userGiftBackWhere['user_id'] = $preUser['id'];
  242. $userGiftBackWhere['get_way'] = 5;
  243. $userGiftBack = model('GiftBack')->where($userGiftBackWhere)->find();
  244. if (empty($userGiftBack)) {
  245. for($i=1;$i<=$inviteGiftNum;$i++) {
  246. $giftBack[] = [
  247. 'user_id' => $preUser['id'],//用户ID
  248. 'gift_id' => $inviteGift,//礼物ID
  249. 'name' => $gift['name'],//礼物名称
  250. 'image' => $gift['image'],//礼物静图
  251. 'gif_image' => $gift['special'],//礼物动图
  252. 'value' => $gift['value'],//购买价值
  253. 'number' => 1,//数量
  254. 'get_way' => 5,//获得途径:1=活动获得,2=充值购买,3=爵位赠送,4=签到赠送,5=邀请获得
  255. 'createtime' => time(),
  256. ];
  257. }
  258. //$giftBackRes = model('GiftBack')->insertGetId($giftBack);
  259. $giftBackRes = model('GiftBack')->insertAll($giftBack);
  260. if (!$giftBackRes) {
  261. throw new Exception('赠送礼物失败');
  262. }
  263. }
  264. }
  265. }
  266. }
  267. }
  268. }
  269. }
  270. }
  271. } catch (Exception $e) {
  272. $result['status'] = 0;
  273. $result['msg'] = $e->getMessage();
  274. }
  275. return $result;
  276. }
  277. /**
  278. * 获取用户家族角色
  279. * @return void
  280. */
  281. public function getGuildRole($params=[])
  282. {
  283. $result = [
  284. 'status' => 1,
  285. 'msg' => '操作成功',
  286. 'data' => [],
  287. ];
  288. try {
  289. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  290. $guildId = isset($params['guild_id']) ? $params['guild_id'] : 0;
  291. if (empty($userId) || empty($guildId)) {
  292. throw new Exception('参数错误');
  293. }
  294. $guildWhere['id'] = $guildId;
  295. $guild = model('Guild')->where($guildWhere)->find();
  296. if (empty($guild)) {
  297. throw new Exception('未找到家族信息');
  298. }
  299. $guildRole = 4;//家族角色:1组长,2副族长,3成员,4非成员
  300. if ($userId == $guild['user_id']) {
  301. $guildRole = 1;
  302. }
  303. $guildMemberWhere['guild_id'] = $guildId;
  304. $guildMemberWhere['user_id'] = $userId;
  305. $guildMember = model('GuildMember')->field('id,role')->where($guildMemberWhere)->find();
  306. if (!empty($guildMember)) {
  307. if ($guildMember['role'] == 0) {
  308. $guildRole = 3;
  309. } elseif ($guildMember['role'] == 1) {
  310. $guildRole = 2;
  311. }
  312. }
  313. $result['data'] = [
  314. 'guild_role' => $guildRole,
  315. ];
  316. } catch (Exception $e) {
  317. $result['status'] = 0;
  318. $result['msg'] = $e->getMessage();
  319. }
  320. return $result;
  321. }
  322. /**
  323. * 更新用户消费/魅力/礼物数据
  324. * @return void
  325. */
  326. public function updateGuildMember($params=[])
  327. {
  328. $result = [
  329. 'status' => 1,
  330. 'msg' => '操作成功',
  331. 'data' => [],
  332. ];
  333. try {
  334. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  335. if (!empty($userId)) {
  336. $guildMemberWhere['user_id'] = $userId;
  337. $guildMember = model('GuildMember')->where($guildMemberWhere)->count();
  338. if (!empty($guildMember)) {
  339. $wealth = $charm = $gift_num = 0;
  340. $userJewelLogWhere["type"] = ["in",[0,2,3,5,6,13]];//查看wallet.php文件
  341. $userJewelLogWhere["user_id"] = $userId;
  342. $userJewelLogPay = model('UserJewelLog')
  343. ->field("sum(value) as total_price")
  344. ->where($userJewelLogWhere)
  345. ->select();
  346. $wealth = isset($userJewelLogPay[0]['total_price']) ? (int)$userJewelLogPay[0]['total_price'] : 0;
  347. $userGiftWhere = [];
  348. $userGiftWhere["user_to_id"] = $userId;
  349. $userGift = model('GiftUserParty')
  350. ->field("sum(value) as total_price,sum(number)as gift_total_num")
  351. ->where($userGiftWhere)
  352. ->select();
  353. $charm = isset($userGift[0]['total_price']) ? (int)$userGift[0]['total_price'] : 0;
  354. $giftNum = isset($userGift[0]['gift_total_num']) ? (int)$userGift[0]['gift_total_num'] : 0;
  355. $guildMemberData = ['wealth'=>$wealth,'charm'=>$charm,'gift_num'=>$giftNum];
  356. $r = model('GuildMember')->update($guildMemberData,$guildMemberWhere);
  357. if (!$r) {
  358. throw new Exception('更新失败');
  359. }
  360. }
  361. }
  362. } catch (Exception $e) {
  363. $result['status'] = 0;
  364. $result['msg'] = $e->getMessage();
  365. }
  366. return $result;
  367. }
  368. }