Commit 9cbf0f08 authored by mengcuiguang's avatar mengcuiguang

添加支付宝

parent 5f12285d
......@@ -96,7 +96,12 @@
android:exported="false"
android:launchMode="singleTop"
android:screenOrientation="portrait" />
<activity
android:name=".ui.activitys.AlipayLoginActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:exported="false"
android:launchMode="singleTop"
android:screenOrientation="portrait" />
<activity
android:name=".ui.activitys.MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
......
package com.duben.supertheateru.manager
import android.annotation.SuppressLint
import android.app.Activity
import android.os.Handler
import android.os.Message
import android.text.TextUtils
import com.alipay.sdk.app.AuthTask
import com.duben.supertheateru.MintsApplication
import com.duben.supertheateru.common.AppConfig
import com.duben.supertheateru.mvp.model.AuthResult
import com.duben.supertheateru.mvp.model.BaseResponse
import com.duben.supertheateru.utils.ToastUtil
import com.duben.library.net.neterror.BaseSubscriber
import com.duben.library.net.neterror.Throwable
import com.duben.library.utils.json.JsonUtil
import com.google.gson.JsonObject
import java.lang.ref.WeakReference
/**
* 支付宝授权
*/
object AlipayAuthManager {
private const val SDK_AUTH_FLAG = 2
@SuppressLint("HandlerLeak")
private val mHandler: Handler = object : Handler() {
override fun handleMessage(msg: Message) {
when (msg.what) {
SDK_AUTH_FLAG -> {
val authResult = AuthResult(msg.obj as Map<String, String>, true)
val resultStatus: String = authResult.getResultStatus()
// 判断resultStatus 为“9000”且result_code
// 为“200”则代表授权成功,具体状态码代表含义可参考授权接口文档
if (TextUtils.equals(
resultStatus,
"9000"
) && TextUtils.equals(authResult.getResultCode(), "200")
) {
// 获取alipay_open_id,调支付时作为参数extern_token 的value
// 传入,则支付账户为该授权账户
println("mcg -->>>>> authinfo=" + JsonUtil.toJson(authResult))
commitAlipayAutoInfo(JsonUtil.toJson(authResult))
} else {
// 其他状态值则为授权失败
authListener?.authFail(resultStatus)
}
}
else -> {}
}
}
}
private fun commitAlipayAutoInfo(info: String) {
val vo = HashMap<String, Any>()
vo.put("user_id", UserManager.getInstance().userID)
vo.put("result", info)
val baseApplication = MintsApplication.getContext() as MintsApplication
AppHttpManager.getInstance(baseApplication)
.call(baseApplication.loanService.reportAlipayMsg(vo),
object : BaseSubscriber<BaseResponse<Any>>() {
override fun onError(e: Throwable) {
authListener?.authFail("授权失败!")
}
override fun onNext(t: BaseResponse<Any>) {
if (t.status == 200) {
authListener?.authSuccess()
} else {
authListener?.authFail(t.message)
}
}
})
}
/**
* 支付宝账户授权业务示例
*/
fun authV2(activity: Activity, listener: AuthListener?) {
authListener = listener
val weakReference = WeakReference(activity)
println("mcg -->>>>> 开始授权")
val vo = HashMap<String, Any>()
vo.put("user_id", UserManager.getInstance().userID)
val baseApplication = MintsApplication.getContext() as MintsApplication
AppHttpManager.getInstance(baseApplication)
.call(baseApplication.loanService.getAlipayAuthUrl(vo),
object : BaseSubscriber<BaseResponse<JsonObject>>() {
override fun onError(e: Throwable) {
}
override fun onNext(t: BaseResponse<JsonObject>) {
if (t.status == 200) {
val data = t.data
val authInfo = data["url"].asString
val authRunnable = Runnable { // 构造AuthTask 对象
val authTask = AuthTask(weakReference.get())
// 调用授权接口,获取授权结果
val result = authTask.authV2(authInfo, true)
val msg = Message()
msg.what = SDK_AUTH_FLAG
msg.obj = result
mHandler.sendMessage(msg)
}
// 必须异步调用
val authThread = Thread(authRunnable)
authThread.start()
} else {
ToastUtil.show(MintsApplication.getContext(), t.message)
}
}
})
}
private var authListener: AuthListener? = null
interface AuthListener {
fun authSuccess()
fun authFail(resultStatus: String)
}
}
\ No newline at end of file
......@@ -95,7 +95,7 @@ public class UserManager {
String uid = String.valueOf(user.getPk_id());
String mobile = user.getMobile();
String openid = user.getOpenid();
if (!TextUtils.isEmpty(mobile) || !TextUtils.isEmpty(openid)) {
if (!TextUtils.isEmpty(mobile) || user.isAlipaySet()) {
ps.put(IS_TEMP_USER, uid);
} else {
ps.remove(IS_TEMP_USER);
......
package com.duben.supertheateru.mvp.model; import android.text.TextUtils; import java.io.Serializable;import java.util.Map; public class AuthResult implements Serializable { private String resultStatus; private String result; private String memo; private String resultCode; private String authCode; private String alipayOpenId; public AuthResult(Map<String, String> rawResult, boolean removeBrackets) { if (rawResult == null) { return; } for (String key : rawResult.keySet()) { if (TextUtils.equals(key, "resultStatus")) { resultStatus = rawResult.get(key); } else if (TextUtils.equals(key, "result")) { result = rawResult.get(key); } else if (TextUtils.equals(key, "memo")) { memo = rawResult.get(key); } } String[] resultValue = result.split("&"); for (String value : resultValue) { if (value.startsWith("alipay_open_id")) { alipayOpenId = removeBrackets(getValue("alipay_open_id=", value), removeBrackets); continue; } if (value.startsWith("auth_code")) { authCode = removeBrackets(getValue("auth_code=", value), removeBrackets); continue; } if (value.startsWith("result_code")) { resultCode = removeBrackets(getValue("result_code=", value), removeBrackets); continue; } } } private String removeBrackets(String str, boolean remove) { if (remove) { if (!TextUtils.isEmpty(str)) { if (str.startsWith("\"")) { str = str.replaceFirst("\"", ""); } if (str.endsWith("\"")) { str = str.substring(0, str.length() - 1); } } } return str; } @Override public String toString() { return "authCode={" + authCode + "}; resultStatus={" + resultStatus + "}; memo={" + memo + "}; result={" + result + "}"; } private String getValue(String header, String data) { return data.substring(header.length(), data.length()); } /** * @return the resultStatus */ public String getResultStatus() { return resultStatus; } /** * @return the memo */ public String getMemo() { return memo; } /** * @return the result */ public String getResult() { return result; } /** * @return the resultCode */ public String getResultCode() { return resultCode; } /** * @return the authCode */ public String getAuthCode() { return authCode; } /** * @return the alipayOpenId */ public String getAlipayOpenId() { return alipayOpenId; }}
\ No newline at end of file
......@@ -16,6 +16,7 @@ public class UserBean implements Serializable {
private String idcode;
private String head;
private int activiteFlag=0;// 1-匹配用户
private boolean alipaySet;//支付宝授权
private String openid;
private boolean isForever;//true-永久会员
private long expireTime;// vip到期时间 0-非vip
......@@ -97,4 +98,12 @@ public class UserBean implements Serializable {
public int getActiviteFlag() {
return 1;
}
public boolean isAlipaySet() {
return alipaySet;
}
public void setAlipaySet(boolean alipaySet) {
this.alipaySet = alipaySet;
}
}
......@@ -329,6 +329,18 @@ public interface LoanService {
@POST("api/vedioV1/vedio4List")
Observable<BaseResponse<BannerList>> vedio4List(@Body Map<String, Object> vo);
/**
* 支付宝提交
*/
@POST("roseApi/user/reportAlipayMsg")
Observable<BaseResponse<Object>> reportAlipayMsg(@Body Map<String, Object> vo);
/**
* 获取支付宝授权
*/
@POST("roseApi/user/getAlipayAuthUrl")
Observable<BaseResponse<JsonObject>> getAlipayAuthUrl(@Body Map<String, Object> vo);
/**
* 默认http工厂
*/
......
package com.duben.supertheateru.ui.activitys
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.TextPaint
import android.text.TextUtils
import android.text.style.ClickableSpan
import android.view.KeyEvent
import android.view.View
import androidx.core.content.ContextCompat
import com.daimajia.androidanimations.library.Techniques
import com.daimajia.androidanimations.library.YoYo
import com.duben.library.utils.nodoubleclick.AntiShake
import com.duben.supertheateru.R
import com.duben.supertheateru.common.Constant
import com.duben.supertheateru.manager.AlipayAuthManager
import com.duben.supertheateru.manager.TrackManager
import com.duben.supertheateru.manager.UserManager
import com.duben.supertheateru.ui.activitys.base.BaseActivity
import com.duben.supertheateru.utils.SpanUtils
import com.duben.supertheateru.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_alipay_login.*
/**
* 描述:支付宝登录
* 作者:孟崔广
* 时间:2024/6/25 13:50
*/
class AlipayLoginActivity : BaseActivity(), View.OnClickListener {
private var isCheckAgree = false
override fun getContentViewLayoutID() = R.layout.activity_alipay_login
override fun toggleOverridePendingTransition() = true
override fun getOverridePendingTransitionMode() = TransitionMode.BOTTOM
override fun isApplyKitKatTranslucency() = false
override fun initViewsAndEvents() {
initView()
initListener()
if (TextUtils.isEmpty(UserManager.getInstance().userID)) {
TrackManager.getInstance().visitorlogin()
}
}
override fun onDestroy() {
super.onDestroy()
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
return if (keyCode == KeyEvent.KEYCODE_BACK) {
true
} else super.onKeyDown(keyCode, event)
}
override fun finish() {
super.finish()
//关闭窗体动画显示
overridePendingTransition(0, R.anim.push_bottom_out)
}
override fun onClick(v: View?) {
if (AntiShake.check(v?.id)) return
when (v?.id) {
R.id.rlLoginWx -> {
if (!isCheckAgree) {
ToastUtil.showLong(this, "请勾选同意后再进行登录")
llWxloginCheck.postDelayed({
YoYo.with(Techniques.Shake).duration(500).repeat(2).playOn(llWxloginCheck)
}, 200)
return
}
// 去授权
showToast("支付宝拉起登录")
AlipayAuthManager.authV2(this@AlipayLoginActivity,
object : AlipayAuthManager.AuthListener {
override fun authSuccess() {
if (isFinishing) return
showToast("登录成功")
hideLoading()
Handler(Looper.getMainLooper()).postDelayed({
readyGoThenKill(MainActivity::class.java)
}, 1000)
}
override fun authFail(resultStatus: String) {
showToast("支付宝授权失败 $resultStatus")
}
})
}
}
}
private fun initView() {
SpanUtils.with(tvLoginAgreement)
.append("已阅读并同意")
.append("《用户注册协议》").setClickSpan(object : ClickableSpan() {
override fun onClick(widget: View) {
val bundle = Bundle()
bundle.putString(WebActivity.WEB_TITLE, getString(R.string.register_name))
bundle.putString(WebActivity.WEB_URL, Constant.REGISTER_URL)
readyGo(WebActivity::class.java, bundle)
}
override fun updateDrawState(ds: TextPaint) {
ds.color = ContextCompat.getColor(mContext, R.color.main_mints)
ds.isUnderlineText = false
}
})
.append("、")
.append("《用户隐私协议》").setClickSpan(object : ClickableSpan() {
override fun onClick(widget: View) {
val bundle = Bundle()
bundle.putString(WebActivity.WEB_TITLE, getString(R.string.privacy_name))
bundle.putString(WebActivity.WEB_URL, Constant.PRIVACY_URL)
readyGo(WebActivity::class.java, bundle)
}
override fun updateDrawState(ds: TextPaint) {
ds.color = ContextCompat.getColor(mContext, R.color.main_mints)
ds.isUnderlineText = false
}
})
.append("与您的利益切身相关。请您注册前务必仔细阅读!")
.create()
}
private fun initListener() {
ivLoginBack.setOnClickListener(this)
tvLoginMobile.setOnClickListener(this)
rlLoginWx.setOnClickListener(this)
wxloginCheck.setOnCheckedChangeListener { buttonView, isChecked ->
this.isCheckAgree = isChecked
}
}
}
......@@ -76,6 +76,7 @@ public class ForegroundOrBackground implements Application.ActivityLifecycleCall
if (TextUtils.equals(simpleName, "SplashActivity") ||
TextUtils.equals(simpleName, "SplashAdActivity") ||
TextUtils.equals(simpleName, "SplashForeAdActivity") ||
TextUtils.equals(simpleName, "AlipayLoginActivity") ||
TextUtils.equals(simpleName, "VipActivity")) {
count++;
System.out.println("mcg __>>>>>>>:: onActivityStarted simpleName="+simpleName+" 进入 count=" + count);
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<ImageView
android:id="@+id/ivLoginBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="25dp"
android:paddingTop="40dp"
android:visibility="invisible"
android:paddingRight="25dp"
android:paddingBottom="10dp"
android:scaleType="center"
android:src="@mipmap/ic_activity_quit" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher_main" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rlLoginWx"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="40dp"
android:layout_marginTop="230dp"
android:layout_marginRight="40dp"
android:background="@drawable/shape_green">
<TextView
android:id="@+id/tv_wx_login_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:text="支付宝登录"
android:textColor="@color/white"
android:textSize="18sp" />
</RelativeLayout>
<TextView
android:id="@+id/tvLoginMobile"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="24dp"
android:text="其他登录方式"
android:textColor="@color/gray"
android:textSize="14sp" />
<LinearLayout
android:id="@+id/llWxloginCheck"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/wxloginCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:checked="false"
android:gravity="center" />
<TextView
android:id="@+id/tvLoginAgreement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="2dp"
android:layout_marginRight="30dp"
android:textColor="#707A8D"
android:textSize="12sp"
tools:text="123123123123123123123123123123123123123123123123123123123123123123" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment