lmw
2024-06-17 f571288a24fcf10143dcc8015ffbbf38dbc0c614
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.dollearn.student.ui.home
 
import android.content.Intent
import cn.sinata.xldutils.callPhone
import cn.sinata.xldutils.utils.optString
import com.dollearn.student.R
import com.dollearn.student.WeparkApplication
import com.dollearn.student.dialog.PayDialog
import com.dollearn.student.network.Apis
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.request
import com.dollearn.student.ui.TransparentStatusBarActivity
import com.dollearn.student.utils.Const
import com.dollearn.student.utils.event.EmptyEvent
import com.dollearn.student.utils.extention.loadLongImage
import com.dollearn.student.utils.interfaces.StringCallback
import com.dollearn.student.utils.pay.PayListener
import com.dollearn.student.utils.pay.PayUtil
import kotlinx.android.synthetic.main.activity_join_vip.*
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult
 
class JoinVipActivity : TransparentStatusBarActivity(), PayListener {
    override fun setContentView() = R.layout.activity_join_vip
 
    private val type by lazy { intent.getIntExtra("type", TYPE_VIP) }
    private var phone:String? = null
 
    override fun initClick() {
        bg_action.setOnClickListener {
            if (type == TYPE_FREE)
                callPhone(phone)
            else
                PayDialog.show(supportFragmentManager,true,object :StringCallback{
                    override fun onResult(rst: String) {
                        pay(rst.toInt())
                    }
                })
        }
    }
 
    override fun initView() {
        if (type == TYPE_FREE){
            title = "免费福利"
            tv_action.text = "联系我们"
            tv_action.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.iv_phone,0,0,0)
            getFree()
        }else{
            getImage()
            PayUtil.addPayListener(this)
        }
    }
 
    private fun getFree(){
        HttpManager.queryStoreFreeBenefit(WeparkApplication.storeId).request(this){_,data->
            phone = data?.optString("phone")
            data?.optString("img")?.loadLongImage(this,iv_img)
        }
    }
 
    private fun getImage(){
        val map = hashMapOf<String, Any>()
        map["position"] = 2
        HttpManager.queryString(Apis.querySystemImg,map).request(this){_,data->
            data?.loadLongImage(this,iv_img)
        }
    }
 
    private fun pay(payType:Int){
        HttpManager.addVipPayment(payType).request(this){_,data->
            if (payType == 2){
                PayUtil.aliPay(this,data?.orderInfo?:"")
            }else{
                PayUtil.weChatPay(data!!)
            }
        }
    }
 
    override fun onPaySuccess() {
        EventBus.getDefault().post(EmptyEvent(Const.EventCode.REFRESH_USER)) //成为会员后刷新首页
        startActivity<PayResultActivity>()
        finish()
    }
 
    override fun onPayCancel() {
 
    }
 
    override fun onPayError(msg: String) {
        startActivityForResult<PayResultActivity>(1,"type" to PayResultActivity.TYPE_VIP_FAILED)
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK){
            tv_action.postDelayed({
                tv_action.callOnClick()
            },500)
        }
    }
 
    override fun onDestroy() {
        if (type == TYPE_VIP)
            PayUtil.removePayListener(this)
        super.onDestroy()
    }
 
    companion object{
        const val TYPE_VIP = 0 //VIP
        const val TYPE_FREE = 1 //免费福利
    }
}