Member.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * 会员服务
  4. */
  5. namespace app\weixin\service;
  6. use app\weixin\model\AccountLog;
  7. use app\weixin\model\UserProfile;
  8. use app\weixin\model\Member as MemberModel;
  9. use app\weixin\model\Wechat;
  10. use think\Db;
  11. class Member
  12. {
  13. /**
  14. * 会员信息认证审核
  15. * @param $userId 用户ID
  16. * @param $status 审核状态:2-成功,3-失败
  17. * @param string $scene 审核类型:idcard-身份证,education-学历,position-职业/工作
  18. * @param string $failRemark 审核失败原因
  19. * @return array|int
  20. */
  21. public static function checkAuth($userId, $status, $scene='idcard', $failRemark=''){
  22. try {
  23. // 验证认证信息是否存在
  24. $field = 'id,userid,idcard,idcard_check,education_check,position_check';
  25. $info = UserProfile::getInfo(['userid'=> $userId],$field);
  26. if(empty($info)){
  27. return 2122;
  28. }
  29. if(!in_array($status, [2,3])){
  30. return 2127;
  31. }
  32. //var_dump($info);
  33. // 审核处理
  34. $sceneNames = ['idcard'=>'身份证','education'=> '学历','position'=> '工作'];
  35. switch($scene){
  36. case 'idcard': // 身份认证审核
  37. // 验证状态
  38. $idcardCheck = isset($info['idcard_check'])? intval($info['idcard_check']) : 0;
  39. if($idcardCheck != 0 && $idcardCheck != 1 && $status == 2){
  40. return 2123;
  41. }
  42. $data = [
  43. 'idcard_check'=> $status,
  44. 'idcard_fail'=> $failRemark? trim($failRemark) : '',
  45. ];
  46. break;
  47. case 'education': // 学历认证审核
  48. // 验证状态
  49. $educationCheck = isset($info['education_check'])? intval($info['education_check']) : 0;
  50. if($educationCheck != 0 && $educationCheck != 1 && $status == 2){
  51. return 2123;
  52. }
  53. $data = [
  54. 'education_check'=> $status,
  55. 'education_fail'=> $failRemark? trim($failRemark) : '',
  56. ];
  57. break;
  58. case 'position': // 工作认证审核
  59. // 验证状态
  60. $positionCheck = isset($info['position_check'])? intval($info['position_check']) : 0;
  61. if($positionCheck != 0 && $positionCheck != 1 && $status == 2){
  62. return 2123;
  63. }
  64. $data = [
  65. 'position_check'=> $status,
  66. 'position_fail'=> $failRemark? trim($failRemark) : '',
  67. ];
  68. break;
  69. default:
  70. return 2126;
  71. break;
  72. }
  73. // 更新数据处理
  74. Db::startTrans();
  75. if(!UserProfile::saveData(['userid'=> $userId], $data)){
  76. Db::rollback();
  77. return 2125;
  78. }
  79. // 审核通过奖励处理
  80. $remark = '';
  81. $sceneName = isset($sceneNames[$scene])? $sceneNames[$scene] : '信息';
  82. $field = 'id,openid,user_nickname,real_name,redheart';
  83. $memberInfo = MemberModel::getInfo(['id'=> $userId], $field);
  84. if($status == 2){
  85. // 身份认证更新生日
  86. $idcard = isset($info['idcard'])? trim($info['idcard']) : '';
  87. if($scene == 'idcard' && $idcard){
  88. $birthday = substr($idcard, 6,8);
  89. $birthday = substr($birthday,0,4).'-'.substr($birthday,4,2).'-'.substr($birthday,6,2);
  90. $birthday = $birthday? strtotime($birthday) : 0;
  91. if($birthday){
  92. MemberModel::saveData(['id'=> $userId], ['birthday'=> $birthday]);
  93. }
  94. }
  95. $siteInfo = cmf_get_site_info();
  96. $awardNum = isset($siteInfo[$scene.'_award_redheart'])? intval($siteInfo[$scene.'_award_redheart']) : 0;
  97. if($awardNum){
  98. $redheart = isset($memberInfo['redheart'])? intval($memberInfo['redheart']) : 0;
  99. if(!MemberModel::saveData(['id'=> $userId], ['redheart'=> intval($redheart + $awardNum)])){
  100. Db::rollback();
  101. return 2125;
  102. }
  103. // 账户明细
  104. $remark = "{$sceneName}认证成功奖励{$awardNum}个爱心";
  105. $accountData = [
  106. 'type' => 4,
  107. 'account_type' => 1,
  108. 'change_type' => 1,
  109. 'user_id' => $userId,
  110. 'money' => $awardNum,
  111. 'balance' => $redheart,
  112. 'created_at' => date('Y-m-d H:i:s'),
  113. 'remark' => "{$sceneName}认证成功奖励{$awardNum}个爱心",
  114. ];
  115. AccountLog::insertGetId($accountData);
  116. }
  117. }
  118. Db::commit();
  119. // 推送审核信息
  120. $openid = isset($memberInfo['openid'])? $memberInfo['openid'] : '';
  121. if ($openid) {
  122. $realname = isset($memberInfo['real_name']) ? $memberInfo['real_name'] : '';
  123. $checkTime = date('Y.m.d H:i');
  124. $remark = $status==2? "感谢您的使用,点击进入公众号,遇见一段爱情!" : '请修改后重新提交';
  125. $params = [
  126. 'title' => $status==2? "恭喜!您的{$sceneName}认证已经通过审核!\n\n审核时间:\t{$checkTime}" : $failRemark."\n\n审核时间:\t{$checkTime}",
  127. 'remark' => $remark,
  128. 'type' => 'auth',
  129. 'keywords' => [
  130. /*'keyword1' => [
  131. 'value' => $realname,
  132. 'color' => '#173177',
  133. ],*/
  134. 'keyword1' => [
  135. 'value' => "{$sceneName}认证",
  136. 'color' => '#173177',
  137. ],
  138. 'keyword2' => [
  139. 'value' => $status==2? '审核通过' : '认证失败',
  140. 'color' => '#173177',
  141. ],
  142. ],
  143. 'url' => $status==2? url('/weixin/match/index', '', '', true) : url('/weixin/auth/' . $scene, '', '', true),
  144. ];
  145. PRedis::set('auths:message:u_'.$userId, ['result' => $memberInfo, 'params' => $params], 600);
  146. Wechat::sendTplMsg($openid, $params);
  147. }
  148. return ['id'=> $userId];
  149. } catch (\Exception $exception){
  150. // var_dump($exception);
  151. return 1015;
  152. }
  153. }
  154. }