Userverify.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\api\controller;
  13. use app\api\model\user\CommissionsDetail;
  14. use app\api\model\UserCoupon;
  15. use app\common\enum\Setting as SettingEnum;
  16. use app\common\exception\BaseException;
  17. use app\api\model\UserCoupon as UserCouponModel;
  18. use app\api\service\User as UserService;
  19. use app\common\library\helper;
  20. use app\common\model\Shops;
  21. use app\api\model\user\BonusHistory;
  22. use app\api\model\user\CommissionSteps;
  23. use app\api\model\Setting as SettingModel;
  24. use app\common\model\UserVerify as UserVerifyModel;
  25. use app\common\model\SmsCode;
  26. /**
  27. * 用户实名验证管理
  28. * Class User
  29. * @package app\api
  30. */
  31. class Userverify extends Controller
  32. {
  33. public function add(){
  34. $param = $this->request->param();
  35. $userinfo = UserService::getCurrentLoginUser(true);
  36. $id_card = $param['id_card']??'';
  37. if(!$this->isValid($id_card)){
  38. return $this->renderError("请输入正确的身份证号");
  39. }
  40. $user_name = $param['user_name']??'';
  41. $mobile = $param['mobile']??'';
  42. $sms_code = $param['sms_code']??'';
  43. $user_id = $userinfo->user_id;
  44. $sms = SmsCode::where("user_id",$user_id)->where("mobile",$mobile)->where("create_time",'>',time()-300)->order(['id'=>'desc'])->find();
  45. if(empty($sms)){
  46. return $this->renderError("请获取手机验证码");
  47. }
  48. if($sms->code!=$sms_code){
  49. return $this->renderError("验证码不正确");
  50. }
  51. UserVerifyModel::where('user_id',$user_id)->delete();
  52. $data['user_id'] = $user_id;
  53. $data['id_card'] = $id_card;
  54. $data['user_name'] = $user_name;
  55. $data['mobile'] = $mobile;
  56. $data['create_time'] = time();
  57. $data['update_time'] = time();
  58. $userverify = new UserVerifyModel;
  59. $userverify->save($data);
  60. $sms->is_use = 1;
  61. $sms->save();
  62. return $this->renderSuccess();
  63. }
  64. /**
  65. * 校验身份证号是否合法
  66. * @param string $num 待校验的身份证号
  67. * @return bool
  68. */
  69. public function isValid(string $num)
  70. {
  71. //老身份证长度15位,新身份证长度18位
  72. $length = strlen($num);
  73. if ($length == 15) { //如果是15位身份证
  74. //15位身份证没有字母
  75. if (!is_numeric($num)) {
  76. return false;
  77. }
  78. // 省市县(6位)
  79. $areaNum = substr($num, 0, 6);
  80. // 出生年月(6位)
  81. $dateNum = substr($num, 6, 6);
  82. } else if ($length == 18) { //如果是18位身份证
  83. //基本格式校验
  84. if (!preg_match('/^\d{17}[0-9xX]$/', $num)) {
  85. return false;
  86. }
  87. // 省市县(6位)
  88. $areaNum = substr($num, 0, 6);
  89. // 出生年月日(8位)
  90. $dateNum = substr($num, 6, 8);
  91. } else { //假身份证
  92. return false;
  93. }
  94. //验证地区
  95. if (!$this->isAreaCodeValid($areaNum)) {
  96. return false;
  97. }
  98. //验证日期
  99. if (!$this->isDateValid($dateNum)) {
  100. return false;
  101. }
  102. //验证最后一位
  103. if (!$this->isVerifyCodeValid($num)) {
  104. return false;
  105. }
  106. return true;
  107. }
  108. /**
  109. * 省市自治区校验
  110. * @param string $area 省、直辖市代码
  111. * @return bool
  112. */
  113. private static function isAreaCodeValid(string $area) {
  114. $provinceCode = substr($area, 0, 2);
  115. // 根据GB/T2260—999,省市代码11到65
  116. if (11 <= $provinceCode && $provinceCode <= 65) {
  117. return true;
  118. } else {
  119. return false;
  120. }
  121. }
  122. /**
  123. * 验证出生日期合法性
  124. * @param string $date 日期
  125. * @return bool
  126. */
  127. private function isDateValid(string $date) {
  128. if (strlen($date) == 6) { //15位身份证号没有年份,这里拼上年份
  129. $date = '19'.$date;
  130. }
  131. $year = intval(substr($date, 0, 4));
  132. $month = intval(substr($date, 4, 2));
  133. $day = intval(substr($date, 6, 2));
  134. //日期基本格式校验
  135. if (!checkdate($month, $day, $year)) {
  136. return false;
  137. }
  138. //日期格式正确,但是逻辑存在问题(如:年份大于当前年)
  139. $currYear = date('Y');
  140. if ($year > $currYear) {
  141. return false;
  142. }
  143. return true;
  144. }
  145. /**
  146. * 验证18位身份证最后一位
  147. * @param string $num 待校验的身份证号
  148. * @return bool
  149. */
  150. public function isVerifyCodeValid(string $num)
  151. {
  152. if (strlen($num) == 18) {
  153. $factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  154. $tokens = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
  155. $checkSum = 0;
  156. for ($i = 0; $i < 17; $i++) {
  157. $checkSum += intval($num[$i]) * $factor[$i];
  158. }
  159. $mod = $checkSum % 11;
  160. $token = $tokens[$mod];
  161. $lastChar = strtoupper($num[17]);
  162. if ($lastChar != $token) {
  163. return false;
  164. }
  165. }
  166. return true;
  167. }
  168. // /**
  169. // * 当前用户详情
  170. // * @return array|\think\response\Json
  171. // * @throws BaseException
  172. // */
  173. // public function info()
  174. // {
  175. // // 当前用户信息
  176. // $userInfo = UserService::getCurrentLoginUser(true);
  177. // // 获取用户头像
  178. // $userInfo['avatar'] = $userInfo['avatar'] ?? ['preview_url' => config('chef.user_default_avatar'), 'external_url' => config('chef.user_default_avatar')];
  179. // // 获取会员等级
  180. // $userInfo['grade'];
  181. // // 今日收益
  182. // $userInfo['today_profits'] = helper::bcadd(CommissionsDetail::getUserTodayProfits($userInfo->user_id), 0, 2);
  183. // $userInfo['shop_name'] = '';
  184. // $userInfo['is_pickup'] = 0;
  185. // if($userInfo['shop_id'] && $userInfo['role']==4){
  186. // $shops = Shops::find($userInfo['shop_id']);
  187. // $userInfo['shop_name'] = $shops['shop_name'];
  188. // if($shops['is_pickup']){
  189. // $userInfo['is_pickup'] = $shops['is_pickup'];//开启门店自提
  190. // }
  191. // }
  192. // $userInfo['bind_shop_name'] = Shops::find($userInfo['bind_shop_id'])['shop_name'] ?? '';
  193. // //待结算金额
  194. // $djs_amount = CommissionsDetail::where('user_id', $userInfo->user_id)->where('is_shop_commission',0)->where('clearing_status', 0)->sum('clearing_money') ?? 0;
  195. // $userInfo['djs_amount'] = helper::bcadd($djs_amount, 0, 2);
  196. // $userInfo['show_cash_box'] = false;
  197. // $wait_clearing = CommissionsDetail::getUserWaitCommission($userInfo['user_id']);
  198. // if ($wait_clearing >0 || $userInfo['can_withdraw_money'] >0){
  199. // $userInfo['show_cash_box'] = true;
  200. // }
  201. // $userInfo['can_withdraw_money'] = helper::bcsub($userInfo['can_withdraw_money'],0,2); // 佣金已结算总金额
  202. // $userInfo['have_withdrew_money'] = helper::bcsub($userInfo['have_withdrew_money'],0,2); // 已提现金额
  203. // $userInfo['ktxyj_amount'] = helper::bcsub($userInfo['ktxyj_amount'],0,2); // 可提现金额
  204. // // 上月达量奖励金
  205. // $userInfo['last_month_bonus'] = BonusHistory::lastMonthBonus($userInfo['user_id']);
  206. // // 阶梯奖励金计算
  207. // $userInfo['calc_bonus_steps'] = CommissionSteps::calcBonusSteps($userInfo);
  208. // // 阶梯奖励金计算
  209. // $userInfo['sales_bonus_steps'] = $list['distributor_step'] = SettingModel::getItem(SettingEnum::DISTRIBUTOR_STEP)['distributor'];
  210. // // 我的优惠券可用数量
  211. // $userInfo['user_coupon_num'] = (new UserCoupon)->getCount($userInfo['user_id']);
  212. // return $this->renderSuccess(compact('userInfo'));
  213. // }
  214. // /**
  215. // * 账户资产
  216. // * @return array|\think\response\Json
  217. // * @throws BaseException
  218. // */
  219. // public function assets()
  220. // {
  221. // // 当前用户信息
  222. // $userInfo = UserService::getCurrentLoginUser(true);
  223. // // 用户优惠券模型
  224. // $model = new UserCouponModel;
  225. // // 返回数据
  226. // return $this->renderSuccess([
  227. // 'assets' => [
  228. // 'balance' => $userInfo['balance'], // 账户余额
  229. // 'points' => $userInfo['points'], // 会员积分
  230. // 'coupon' => $model->getCount($userInfo['user_id']), // 优惠券数量(可用)
  231. // ]
  232. // ]);
  233. // }
  234. }