jh-admin/app/api/controller/Tripartite.php

149 lines
5.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2023-2033 成都SAAS云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.gobuysaas.com
* =========================================================
* @author : small
*/
namespace app\api\controller;
use app\model\member\Login as LoginModel;
use app\model\message\Message;
use app\model\member\Register as RegisterModel;
use app\model\web\Config as WebConfig;
use think\facade\Cache;
/**
* 第三方自动注册绑定手机
* Class Tripartite
* @package app\api\controller
*/
class Tripartite extends BaseApi
{
/**
* 绑定手机号
*/
public function mobile()
{
$key = $this->params['key'];
$verify_data = Cache::get($key);
if ($verify_data["mobile"] == $this->params["mobile"] && $verify_data["code"] == $this->params["code"]) {
$register = new RegisterModel();
$exist = $register->mobileExist($this->params["mobile"], $this->site_id);
if ($exist) {
$login = new LoginModel();
$res = $login->mobileLogin($this->params);
if ($res['code'] >= 0) {
$token = $this->createToken($res['data']['member_id']);
$res = $this->success(['token' => $token]);
}
} else {
$res = $register->mobileRegister($this->params);
if ($res['code'] >= 0) {
$token = $this->createToken($res['data']);
$res = $this->success(['token' => $token, 'is_register' => 1]);
}
}
} else {
$res = $this->error("", "手机动态码不正确");
}
return $this->response($res);
}
/**
* 绑定手机验证码
* @throws Exception
*/
public function mobileCode()
{
// 校验验证码
$config_model = new WebConfig();
$info = $config_model->getCaptchaConfig();
if ($info['data']['value']['shop_reception_login'] == 1) {
$captcha = new Captcha();
$check_res = $captcha->checkCaptcha(false);
if ($check_res['code'] < 0) return $this->response($check_res);
}
$mobile = $this->params['mobile'];
if (empty($mobile)) return $this->response($this->error([], "手机号不可为空!"));
$code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);// 生成4位随机数左侧补0
$message_model = new Message();
$res = $message_model->sendMessage(['type' => 'code', "mobile" => $mobile, "site_id" => $this->site_id, "support_type" => ['sms'], "code" => $code, "keywords" => "MEMBER_BIND"]);
if ($res["code"] >= 0) {
//将验证码存入缓存
$key = 'bind_mobile_code_' . md5(uniqid(null, true));
Cache::tag("bind_mobile_code_")->set($key, ['mobile' => $mobile, 'code' => $code], 600);
return $this->response($this->success(["key" => $key]));
} else {
return $this->response($res);
}
}
/**
* 手机号授权登录
*/
public function mobileAuth()
{
$decrypt_data = event('DecryptData', $this->params, true);
if ($decrypt_data['code'] < 0) return $this->response($decrypt_data);
$this->params['mobile'] = $decrypt_data['data']['purePhoneNumber'];
$register = new RegisterModel();
$exist = $register->mobileExist($this->params["mobile"], $this->site_id);
if ($exist) {
$login = new LoginModel();
$res = $login->mobileLogin($this->params);
if ($res['code'] >= 0) {
$token = $this->createToken($res['data']['member_id']);
$res = $this->success(['token' => $token, 'can_receive_registergift' => $res['data']['can_receive_registergift']]);
}
} else {
$res = $register->mobileRegister($this->params);
if ($res['code'] >= 0) {
$token = $this->createToken($res['data']);
$res = $this->success(['token' => $token, 'is_register' => 1]);
}
}
return $this->response($res);
}
/**
* 获取手机号
* @return false|string
*/
public function getPhoneNumber()
{
$decrypt_data = event('DecryptData', $this->params, true);
if ($decrypt_data['code'] < 0) return $this->response($decrypt_data);
return $this->response(success(0, '', ['mobile' => $decrypt_data['data']['purePhoneNumber']]));
}
/**
* Common: 微信小程序手机号一键绑定
* Author: wu-hui
* Time: 2024/05/02 15:31
* @return false|string
*/
public function bindMobileMiniWeChat(){
$token = $this->checkToken();
if ($token['code'] < 0) return $this->response($token);
// 获取手机号加密信息 并且解密
$decryptData = event('DecryptData', $this->params, true);
if ($decryptData['code'] < 0) return $this->response($decryptData);
try{
$phoneNumber = $decryptData['data']['purePhoneNumber'] ?? '';
model('member')->update(['mobile'=>$phoneNumber],['member_id'=>$this->member_id]);
return $this->response(success(0, '绑定成功'));
}catch(\Exception $e){
return $this->response(error(-1, $e->getMessage()));
}
}
}