FlutterCourseHomeFragment.kt 7.11 KB
Newer Older
严久程 committed
1 2 3 4 5 6 7
package com.yidianling.course

import CoursePlugin
import android.annotation.SuppressLint
import android.os.Handler
import android.text.TextUtils
import com.alibaba.android.arouter.launcher.ARouter
8
import com.channel.ydl_flutter_base.base.BaseFlutterFragment
严久程 committed
9 10
import com.google.gson.Gson
import com.ydl.ydl_router.manager.YDLRouterManager
严久程 committed
11 12 13 14 15 16 17
import com.ydl.ydlcommon.data.http.BaseResponse
import com.ydl.ydlcommon.data.http.RxUtils
import com.ydl.ydlcommon.data.http.ThrowableConsumer
import com.ydl.ydlcommon.router.YdlCommonRouterManager
import com.ydl.ydlcommon.utils.YDLCacheUtils
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
import com.ydl.ydlcommon.utils.remind.ToastHelper
严久程 committed
18 19
import com.yidianling.common.tools.ToastUtil
import com.yidianling.course.bean.CourseCouponBean
严久程 committed
20
import com.yidianling.course.constants.CourseBIConstants
严久程 committed
21
import com.yidianling.course.flutterPlugin.CourseSendPlugin
严久程 committed
22
import com.yidianling.course.net.CourseRetrofitUtils
konghaorui committed
23
import com.yidianling.course.router.CourseIn
严久程 committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
import com.yidianling.course.widget.CourseCouponDialog
import io.flutter.view.FlutterView
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author jiucheng
 * @描述:课程频道页(flutter)
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2019/10/22
 */
class FlutterCourseHomeFragment : BaseFlutterFragment() {
严久程 committed
40

41

严久程 committed
42 43
    private var hasRequestCoupon = false
    private var dialog: CourseCouponDialog? = null
44

严久程 committed
45
    override fun initChannelPlugin(flutterView: FlutterView) {
严久程 committed
46
        CoursePlugin.register(this, flutterView)
严久程 committed
47 48

        CourseSendPlugin.initContext(this, flutterView)
49

严久程 committed
50 51 52
    }

    override fun initialRoute(): String {
严久程 committed
53
        return "course/home"
严久程 committed
54 55 56 57 58 59 60
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            prepareCoupon()

严久程 committed
61
            ActionCountUtils.count(CourseBIConstants.CourseHomeEvent.POSITION_COURSE_HOME_PAGE_VISIT)
严久程 committed
62 63 64 65 66 67 68 69
        }
    }

    /**
     * 获取优惠券信息
     */
    private fun prepareCoupon() {
        //判断是否登录  以及本次app启动后是否已经请求过
konghaorui committed
70
        if (CourseIn.getUserService()?.isLogin() && !hasRequestCoupon) {
严久程 committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
            Handler().postDelayed({
                //优惠券逻辑
                if (compareCouponTime()) {
                    getCouponData()
                }
            }, 2000)
        }
    }

    /**
     * 获取可以领取的课程优惠券
     *
     * @param uid         uid
     * @param services    适用服务 1预约咨询 2电话倾诉 3课程(用户课程 专家课程) 5测评
     * @param receiveType 领取条件方式 1前台领取 2链接领取
     * @param applyEnds   适用终端 1PC官网 2wap站点 3用户app 4好杮app 5专家app
     * @return
     */
    @SuppressLint("SimpleDateFormat", "CheckResult")
    private fun getCouponData() {
        val (userId) = YdlCommonRouterManager.getYdlCommonRoute().getUserInfo() ?: return
严久程 committed
92
        CourseRetrofitUtils.getCourseCoupons(userId, "3", "1", "3")
严久程 committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
            .compose(RxUtils.netCheck())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(Consumer {
                hasRequestCoupon = true

                val simpleFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")//如2016-08-10
                val time = System.currentTimeMillis()
                val date = Date()
                date.time = time
                val nowTime = simpleFormat.format(date)

                if (it?.data != null && it.data.isNotEmpty()) {
                    YDLCacheUtils.saveCouponTime(userId, nowTime)
                    couponDataResponse(it.data)
                }
            }, object : ThrowableConsumer() {
                override fun accept(msg: String) {
                }
            })
严久程 committed
113 114 115 116 117 118 119
    }

    /**
     * 可领取的优惠券列表
     */
    private fun couponDataResponse(data: List<CourseCouponBean>) {
        if (dialog == null) {
严久程 committed
120 121 122 123 124 125
            dialog = CourseCouponDialog(
                activity,
                data,
                object : CourseCouponDialog.OnClickEnsureListener {
                    override fun clickUse(toLink: String) {
                        YDLRouterManager.router(toLink)
严久程 committed
126
                        dialog!!.dismiss()
严久程 committed
127 128 129 130 131 132 133
                    }

                    override fun clickEnsure(status: Int) {
                        if (status == CourseCouponDialog.STATUS_RECEIVER) {
                            receiveCoupon(data)
                        } else {
                            dialog!!.dismiss()
严久程 committed
134

严久程 committed
135 136
                            //查看我的卡券
                            ARouter.getInstance().build("/mine/redpacket")
严久程 committed
137 138
                                .withString("is_from_main", "1")
                                .navigation()
严久程 committed
139
                        }
严久程 committed
140
                    }
严久程 committed
141
                })
严久程 committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        }
        dialog!!.show()
    }


    /**
     * 领取优惠券
     */
    @SuppressLint("CheckResult")
    fun receiveCoupon(list: List<CourseCouponBean>) {
        val (userId) = YdlCommonRouterManager.getYdlCommonRoute().getUserInfo() ?: return
        val couponIds = ArrayList<String>()
        for (bean in list) {
            couponIds.add(bean.id)
        }
        val bean = CouponReceiveBean()
        bean.couponIds = couponIds
        bean.uid = userId
        val couponIdsStr = Gson().toJson(bean)
严久程 committed
161
        CourseRetrofitUtils.receiveCoupon(couponIdsStr)
严久程 committed
162 163 164 165 166 167 168 169 170 171
            .compose(RxUtils.netCheck())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(Consumer {
                receiveCouponResponse(it)
            }, object : ThrowableConsumer() {
                override fun accept(msg: String) {
                    ToastUtil.toastShort(msg)
                }
            })
严久程 committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    }

    /**
     * 优惠券领取成功
     */
    private fun receiveCouponResponse(data: BaseResponse<List<String>>) {
        if (data.code == 200) {
            dialog?.updateStatus(CourseCouponDialog.STATUS_GET, data.data)
        } else {
            ToastHelper.show(data.msg)
        }
    }

    /**
     * 对比上次获取优惠券时间,若时间差超过一天,则再次请求,否则不获请求
     */
    @SuppressLint("SimpleDateFormat")
    private fun compareCouponTime(): Boolean {
        val uid = YdlCommonRouterManager.getYdlCommonRoute().getUserInfo()?.userId
        val simpleFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")//如2016-08-10
        val last = YDLCacheUtils.getCouponTime(uid)
        return if (TextUtils.isEmpty(last)) {
            //还没有获取过优惠券,直接请求数据
            true
        } else {
            val time = System.currentTimeMillis()
            val date = Date()
            date.time = time
            val nowTime = simpleFormat.format(date)
            !TextUtils.equals(last.substring(0, 10), nowTime.substring(0, 10))
        }
    }

    inner class CouponReceiveBean {
        var couponIds: List<String>? = null
        var uid: String? = null
    }
}