UserService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. * @return void
  139. */
  140. public function inviteMoney($params=[])
  141. {
  142. $result = [
  143. 'status' => 1,
  144. 'msg' => '',
  145. 'data' => [],
  146. ];
  147. try {
  148. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  149. if (!empty($userId)) {
  150. $field = 'id,pre_userid,money';
  151. $where['id'] = $userId;
  152. $user = model('User')->field($field)->where($where)->find();
  153. if (!empty($user)) {
  154. if (!empty($user['pre_userid'])) {
  155. $preWhere['id'] = $user['pre_userid'];
  156. $preUser = model('User')->field($field)->where($preWhere)->find();
  157. if (!empty($preUser)) {
  158. //查看是否赠送过金额
  159. $moneyLogWhere['invite_user_id'] = $userId;
  160. $moneyLogWhere['type'] = 103;
  161. $moneyLog = model('UserMoneyLog')->where($moneyLogWhere)->find();
  162. if (empty($moneyLog)) {
  163. $money = config('site.invite_money');
  164. $remark = '邀请推广充值';
  165. $res = model('Wallet')->lockChangeAccountRemain($preUser['id'],$money,'+',$preUser['money'],$remark,103,'money',$userId);
  166. if (!$res['status']) {
  167. throw new Exception($res['msg']);
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. } catch (Exception $e) {
  175. $result['status'] = 0;
  176. $result['msg'] = $e->getMessage();
  177. }
  178. return $result;
  179. }
  180. /**
  181. * 获取用户家族角色
  182. * @return void
  183. */
  184. public function getGuildRole($params=[])
  185. {
  186. $result = [
  187. 'status' => 1,
  188. 'msg' => '操作成功',
  189. 'data' => [],
  190. ];
  191. try {
  192. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  193. $guildId = isset($params['guild_id']) ? $params['guild_id'] : 0;
  194. if (empty($userId) || empty($guildId)) {
  195. throw new Exception('参数错误');
  196. }
  197. $guildWhere['id'] = $guildId;
  198. $guild = model('Guild')->where($guildWhere)->find();
  199. if (empty($guild)) {
  200. throw new Exception('未找到家族信息');
  201. }
  202. $guildRole = 4;//家族角色:1组长,2副族长,3成员,4非成员
  203. if ($userId == $guild['user_id']) {
  204. $guildRole = 1;
  205. }
  206. $guildMemberWhere['guild_id'] = $guildId;
  207. $guildMemberWhere['user_id'] = $userId;
  208. $guildMember = model('GuildMember')->field('id,role')->where($guildMemberWhere)->find();
  209. if (!empty($guildMember)) {
  210. if ($guildMember['role'] == 0) {
  211. $guildRole = 3;
  212. } elseif ($guildMember['role'] == 1) {
  213. $guildRole = 2;
  214. }
  215. }
  216. $result['data'] = [
  217. 'guild_role' => $guildRole,
  218. ];
  219. } catch (Exception $e) {
  220. $result['status'] = 0;
  221. $result['msg'] = $e->getMessage();
  222. }
  223. return $result;
  224. }
  225. /**
  226. * 更新用户消费/魅力/礼物数据
  227. * @return void
  228. */
  229. public function updateGuildMember($params=[])
  230. {
  231. $result = [
  232. 'status' => 1,
  233. 'msg' => '操作成功',
  234. 'data' => [],
  235. ];
  236. try {
  237. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  238. if (!empty($userId)) {
  239. $guildMemberWhere['user_id'] = $userId;
  240. $guildMember = model('GuildMember')->where($guildMemberWhere)->count();
  241. if (!empty($guildMember)) {
  242. $wealth = $charm = $gift_num = 0;
  243. $userJewelLogWhere["type"] = ["in",[0,2,3,5,6,13]];//查看wallet.php文件
  244. $userJewelLogWhere["user_id"] = $userId;
  245. $userJewelLogPay = model('UserJewelLog')
  246. ->field("sum(value) as total_price")
  247. ->where($userJewelLogWhere)
  248. ->select();
  249. $wealth = isset($userJewelLogPay[0]['total_price']) ? (int)$userJewelLogPay[0]['total_price'] : 0;
  250. $userGiftWhere = [];
  251. $userGiftWhere["user_to_id"] = $userId;
  252. $userGift = model('GiftUserParty')
  253. ->field("sum(value) as total_price,sum(number)as gift_total_num")
  254. ->where($userGiftWhere)
  255. ->select();
  256. $charm = isset($userGift[0]['total_price']) ? (int)$userGift[0]['total_price'] : 0;
  257. $giftNum = isset($userGift[0]['gift_total_num']) ? (int)$userGift[0]['gift_total_num'] : 0;
  258. $guildMemberData = ['wealth'=>$wealth,'charm'=>$charm,'gift_num'=>$giftNum];
  259. $r = model('GuildMember')->update($guildMemberData,$guildMemberWhere);
  260. }
  261. }
  262. } catch (Exception $e) {
  263. $result['status'] = 0;
  264. $result['msg'] = $e->getMessage();
  265. }
  266. return $result;
  267. }
  268. }