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

import CoursePlugin
import android.annotation.SuppressLint
import android.os.Handler
import android.text.TextUtils
7
import com.alibaba.android.arouter.facade.annotation.Route
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
import com.ydl.ydlcommon.bean.StatusBarOptions
严久程 committed
12 13 14
import com.ydl.ydlcommon.data.http.BaseResponse
import com.ydl.ydlcommon.data.http.RxUtils
import com.ydl.ydlcommon.data.http.ThrowableConsumer
konghaorui committed
15
import com.ydl.ydlcommon.modular.ModularServiceManager
严久程 committed
16 17 18 19
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
20 21
import com.yidianling.common.tools.ToastUtil
import com.yidianling.course.bean.CourseCouponBean
严久程 committed
22
import com.yidianling.course.constants.CourseBIConstants
严久程 committed
23
import com.yidianling.course.flutterPlugin.CourseSendPlugin
严久程 committed
24
import com.yidianling.course.net.CourseRetrofitUtils
konghaorui committed
25
import com.yidianling.course.router.CourseIn
严久程 committed
26
import com.yidianling.course.widget.CourseCouponDialog
konghaorui committed
27
import com.yidianling.user.api.service.IAppService
严久程 committed
28 29 30 31 32 33 34
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.*

35

严久程 committed
36 37 38 39 40 41 42
/**
 * @author jiucheng
 * @描述:课程频道页(flutter)
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2019/10/22
 */
43
@Route(path = "/course/course")
严久程 committed
44
class FlutterCourseHomeFragment : BaseFlutterFragment() {
严久程 committed
45

46

严久程 committed
47 48
    private var hasRequestCoupon = false
    private var dialog: CourseCouponDialog? = null
49

严久程 committed
50
    override fun initChannelPlugin(flutterView: FlutterView) {
严久程 committed
51
        CoursePlugin.register(this, flutterView)
严久程 committed
52 53

        CourseSendPlugin.initContext(this, flutterView)
54

严久程 committed
55 56 57
    }

    override fun initialRoute(): String {
严久程 committed
58
        return "course/home"
严久程 committed
59 60 61 62 63 64 65
    }

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

严久程 committed
66
            ActionCountUtils.count(CourseBIConstants.CourseHomeEvent.POSITION_COURSE_HOME_PAGE_VISIT)
严久程 committed
67 68 69
        }
    }

严久程 committed
70 71

    override fun getStatusViewOptions(): StatusBarOptions {
严久程 committed
72
        return StatusBarOptions(false, true)
严久程 committed
73 74
    }

严久程 committed
75 76 77 78 79
    /**
     * 获取优惠券信息
     */
    private fun prepareCoupon() {
        //判断是否登录  以及本次app启动后是否已经请求过
konghaorui committed
80
        if (CourseIn.getUserService()?.isLogin() && !hasRequestCoupon) {
严久程 committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
            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
102
        CourseRetrofitUtils.getCourseCoupons(userId, "3", "1", "3")
严久程 committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
            .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
123 124 125 126 127 128 129
    }

    /**
     * 可领取的优惠券列表
     */
    private fun couponDataResponse(data: List<CourseCouponBean>) {
        if (dialog == null) {
严久程 committed
130 131 132 133 134 135
            dialog = CourseCouponDialog(
                activity,
                data,
                object : CourseCouponDialog.OnClickEnsureListener {
                    override fun clickUse(toLink: String) {
                        YDLRouterManager.router(toLink)
严久程 committed
136
                        dialog!!.dismiss()
严久程 committed
137 138 139 140 141 142 143
                    }

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

严久程 committed
145
                            //查看我的卡券
konghaorui committed
146
                            ModularServiceManager.provide(IAppService::class.java).myRedPockIntent(activity)
严久程 committed
147
                        }
严久程 committed
148
                    }
严久程 committed
149
                })
严久程 committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        }
        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
169
        CourseRetrofitUtils.receiveCoupon(couponIdsStr)
严久程 committed
170 171 172 173 174 175 176 177 178 179
            .compose(RxUtils.netCheck())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(Consumer {
                receiveCouponResponse(it)
            }, object : ThrowableConsumer() {
                override fun accept(msg: String) {
                    ToastUtil.toastShort(msg)
                }
            })
严久程 committed
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 210 211 212 213 214 215 216 217
    }

    /**
     * 优惠券领取成功
     */
    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
    }
}