PHP做app扫码登录的一些步骤和代码片段记录一下

总体的思路步骤大致如下:

  1. 生成一个唯一值,返回给前端页面用来生成唯一值;
  2. 前端轮询接口,直到返回二维码过期或者扫码成功;
  3. app端进行扫码,并对二维码的值进行判断;
  4. 二维码值错误,返回错误信息;
  5. 二维码正确,让用户选择是否登录,请求后端接口,写入数据库相应表(code表),状态标记为已扫码(状态有已扫码,登录,取消);
  6. 当用户选择登录,状态标记为登录,取消则标记为取消
  7. 前端轮询,根据不同的返回状态显示不同的提示信息,和app端同步,当用户选择登录的时候控制页面跳转,取消的时候刷新二维码开始新的轮询

代码片段基于CRM Chat项目。项目地址CRMChat: TP6+swoole4开源客服系统,支持微信网页、H5端、PC端客服接入,商家端有PC端管理、H5端、App端接待,支持用户添加标签、分组等功能,前后端所有代码全部开源

1、从后端获取一个唯一的key值,让前端带着这个key组成链接,生成二维码。

    /**
     * 获取登录唯一code
     * @return mixed
     */
    public function getLoginKey()
    {
        $key  = md5(time() . uniqid());
        $time = time() + 600;
        CacheService::set($key, 1, 600);
        return $this->success(['key' => $key, 'time' => $time]);
    }
 // 获取客服扫码key
    getSanCodeKey() {
      getSanCodeKey().then(res => {
        this.codeKey = res.data.key
        this.creatQrCode()
        this.scanTime = setInterval(() => {
          this.timeNum++
          if(this.timeNum >= 60) {
            this.timeNum = 0
       
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Android扫码登录的示例代码,使用了Google提供的ZXing扫码库: 1. 在项目的build.gradle文件中添加依赖: ``` dependencies { implementation 'com.google.zxing:core:3.3.3' implementation 'com.journeyapps:zxing-android-embedded:3.5.0' } ``` 2. 在需要扫码登录的Activity中添加扫码逻辑: ``` public class LoginActivity extends AppCompatActivity { private Button mScanButton; private TextView mResultTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mScanButton = findViewById(R.id.scan_button); mResultTextView = findViewById(R.id.result_textview); mScanButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 启动扫码界面 IntentIntegrator integrator = new IntentIntegrator(LoginActivity.this); integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE); integrator.setPrompt("请将二维码置于框内"); integrator.setCameraId(0); integrator.setBeepEnabled(false); integrator.setBarcodeImageEnabled(false); integrator.initiateScan(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if (result != null) { if (result.getContents() != null) { // 获取扫码结果 String scanResult = result.getContents(); // 将扫码结果显示在TextView上 mResultTextView.setText(scanResult); } else { Toast.makeText(this, "扫码已取消", Toast.LENGTH_LONG).show(); } } else { super.onActivityResult(requestCode, resultCode, data); } } } ``` 3. 在布局文件中添加Button和TextView: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/scan_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="扫码登录" android:layout_centerInParent="true"/> <TextView android:id="@+id/result_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/scan_button" android:layout_centerHorizontal="true"/> </RelativeLayout> ``` 以上代码仅为示例,实际应用中还需要根据具体业务逻辑进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值