Tripartite.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2015-2025 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. * @author : niuteam
  10. */
  11. namespace app\api\controller;
  12. use app\model\member\Login as LoginModel;
  13. use app\model\message\Message;
  14. use app\model\member\Register as RegisterModel;
  15. use app\model\web\Config as WebConfig;
  16. use think\facade\Cache;
  17. /**
  18. * 第三方自动注册绑定手机
  19. * Class Tripartite
  20. * @package app\api\controller
  21. */
  22. class Tripartite extends BaseApi
  23. {
  24. /**
  25. * 绑定手机号
  26. */
  27. public function mobile()
  28. {
  29. $key = $this->params[ 'key' ];
  30. $verify_data = Cache::get($key);
  31. if ($verify_data[ "mobile" ] == $this->params[ "mobile" ] && $verify_data[ "code" ] == $this->params[ "code" ]) {
  32. $register = new RegisterModel();
  33. $exist = $register->mobileExist($this->params[ "mobile" ], $this->site_id);
  34. if ($exist) {
  35. $login = new LoginModel();
  36. $res = $login->mobileLogin($this->params);
  37. if ($res[ 'code' ] >= 0) {
  38. $token = $this->createToken($res[ 'data' ][ 'member_id' ]);
  39. $res = $this->success([ 'token' => $token ]);
  40. }
  41. } else {
  42. $res = $register->mobileRegister($this->params);
  43. if ($res[ 'code' ] >= 0) {
  44. $token = $this->createToken($res[ 'data' ]);
  45. $res = $this->success([ 'token' => $token, 'is_register' => 1 ]);
  46. }
  47. }
  48. } else {
  49. $res = $this->error("", "手机动态码不正确");
  50. }
  51. return $this->response($res);
  52. }
  53. /**
  54. * 绑定手机验证码
  55. * @throws Exception
  56. */
  57. public function mobileCode()
  58. {
  59. // 校验验证码
  60. $config_model = new WebConfig();
  61. $info = $config_model->getCaptchaConfig();
  62. if ($info[ 'data' ][ 'value' ][ 'shop_reception_login' ] == 1) {
  63. $captcha = new Captcha();
  64. $check_res = $captcha->checkCaptcha(false);
  65. if ($check_res[ 'code' ] < 0) return $this->response($check_res);
  66. }
  67. $mobile = $this->params[ 'mobile' ];
  68. if (empty($mobile)) return $this->response($this->error([], "手机号不可为空!"));
  69. $code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);// 生成4位随机数,左侧补0
  70. $message_model = new Message();
  71. $res = $message_model->sendMessage([ 'type' => 'code', "mobile" => $mobile, "site_id" => $this->site_id, "support_type" => [ 'sms' ], "code" => $code, "keywords" => "MEMBER_BIND" ]);
  72. if ($res[ "code" ] >= 0) {
  73. //将验证码存入缓存
  74. $key = 'bind_mobile_code_' . md5(uniqid(null, true));
  75. Cache::tag("bind_mobile_code_")->set($key, [ 'mobile' => $mobile, 'code' => $code ], 600);
  76. return $this->response($this->success([ "key" => $key ]));
  77. } else {
  78. return $this->response($res);
  79. }
  80. }
  81. /**
  82. * 手机号授权登录
  83. */
  84. public function mobileAuth()
  85. {
  86. $decrypt_data = event('PhoneNumber', $this->params, true);
  87. if ($decrypt_data[ 'code' ] < 0) return $this->response($decrypt_data);
  88. $this->params[ 'mobile' ] = $decrypt_data[ 'data' ];
  89. $register = new RegisterModel();
  90. $exist = $register->mobileExist($this->params[ "mobile" ], $this->site_id);
  91. if ($exist) {
  92. $login = new LoginModel();
  93. $res = $login->mobileLogin($this->params);
  94. if ($res[ 'code' ] >= 0) {
  95. $token = $this->createToken($res[ 'data' ][ 'member_id' ]);
  96. $res = $this->success([ 'token' => $token, 'can_receive_registergift' => $res[ 'data' ][ 'can_receive_registergift' ] ]);
  97. }
  98. } else {
  99. $res = $register->mobileRegister($this->params);
  100. if ($res[ 'code' ] >= 0) {
  101. $token = $this->createToken($res[ 'data' ]);
  102. $res = $this->success([ 'token' => $token, 'is_register' => 1 ]);
  103. }
  104. }
  105. return $this->response($res);
  106. }
  107. /**
  108. * 获取手机号
  109. * @return false|string
  110. */
  111. public function getPhoneNumber()
  112. {
  113. $decrypt_data = event('PhoneNumber', $this->params, true);
  114. if (empty($decrypt_data)) return $this->error('', '没有获取手机号的渠道');
  115. if ($decrypt_data[ 'code' ] < 0) return $this->response($decrypt_data);
  116. return $this->response(success(0, '', [ 'mobile' => $decrypt_data[ 'data' ] ]));
  117. }
  118. }