zhujie
2025-03-26 57d1ff885c981283758d355856012b16b4c7bc5e
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package com.example.firstapp.ui.notifications
 
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.example.firstapp.R
import com.example.firstapp.activity.ContentDetailActivity
import com.example.firstapp.database.service.RetrofitClient
import com.example.firstapp.databinding.FragmentNotificationsBinding
import com.example.firstapp.pay.PayAbility
import com.example.firstapp.ui.reminderOther.ReminderOtherAddActivity2
import com.example.firstapp.ui.reminderOther.ReminderSettingsFragmentOther
import com.example.firstapp.ui.profile.EditProfileActivity
import com.example.firstapp.utils.Log
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.launch
import com.bumptech.glide.Glide
import com.example.firstapp.database.response.UserInfo
 
class NotificationsFragment : Fragment() {
 
    private var _binding: FragmentNotificationsBinding? = null
 
    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!
 
    // 默认值
    private val DEFAULT_XIAOHONGSHU_URL = "https://www.xiaohongshu.com/user/profile/64978d5c000000001001ee26"
    private val DEFAULT_EMAIL = "support@example.com"
    private val DEFAULT_SHARE_TEXT = "推荐一个很棒的应用给你!\n下载地址:https://oia.xiaohongshu.com/oia"
 
    // 存储从接口获取的值
    private var xiaohongshuUrl = ""
    private var contactEmail = ""
    private var shareText = ""
    private var currentUserInfo: UserInfo? = null // 确保使用你的实际数据类
 
 
    private val editProfileLauncher = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { result ->
        if (result.resultCode == AppCompatActivity.RESULT_OK) {
            result.data?.let { data ->
                // 更新昵称
                val newNickname = data.getStringExtra("nickname")
                newNickname?.let {
                    binding.tvNickname.text = it
                }
 
                // 更新头像
                val avatarUri = data.getStringExtra("avatar_uri")
                avatarUri?.let {
                    Glide.with(requireContext())
                        .load(Uri.parse(it))
                        .circleCrop()
                        .into(binding.ivAvatar)
                }
 
                // TODO: 将更新后的信息保存到服务器
            }
        }
    }
 
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentNotificationsBinding.inflate(inflater, container, false)
 
        // 先加载配置,再设置点击事件
        lifecycleScope.launch {
            loadConfigurations()
            setupClickListeners()
        }
 
        return binding.root
    }
 
    private suspend fun loadConfigurations() {
        try {
            // 获取小红书链接
            val xhsResponse = RetrofitClient.apiService.getDictValue("setting", "xiaohongshu")
            if (xhsResponse.code == 200 && !xhsResponse.data?.itemValue.isNullOrEmpty()) {
                xiaohongshuUrl = xhsResponse.data?.itemValue ?: DEFAULT_XIAOHONGSHU_URL
            } else {
                xiaohongshuUrl = DEFAULT_XIAOHONGSHU_URL
            }
 
            // 获取邮箱
            val emailResponse = RetrofitClient.apiService.getDictValue("setting", "email")
            if (emailResponse.code == 200 && !emailResponse.data?.itemValue.isNullOrEmpty()) {
                contactEmail = emailResponse.data?.itemValue ?: DEFAULT_EMAIL
            } else {
                contactEmail = DEFAULT_EMAIL
            }
 
            // 获取分享文本
            val shareResponse = RetrofitClient.apiService.getDictValue("setting", "share_link")
            if (shareResponse.code == 200 && !shareResponse.data?.itemValue.isNullOrEmpty()) {
                shareText = shareResponse.data?.itemValue ?: DEFAULT_SHARE_TEXT
            } else {
                shareText = DEFAULT_SHARE_TEXT
            }
        } catch (e: Exception) {
            // 如果发生错误,使用默认值
            xiaohongshuUrl = DEFAULT_XIAOHONGSHU_URL
            contactEmail = DEFAULT_EMAIL
            shareText = DEFAULT_SHARE_TEXT
        }
    }
 
    private fun setupClickListeners() {
 
        // 支付插件
        binding.payPlugin.setOnClickListener {
            // 跳转到支付插件页面
            lifecycleScope.launch {
                try {
                    val response = RetrofitClient.apiService.getPayOrderInfo()
                    var orderInfo=response.data
                    Log.d("AliPayHelper","获取订单信息时: ${response}")
                    // 这里调用支付宝
                    PayAbility.aliPay(requireActivity(), orderInfo, Observer {
                        when (it.resultStatus) {
                            "9000" -> {
//                                Snackbar.make(binding.root, "支付成功", Snackbar.LENGTH_LONG).show()
                                requireActivity().runOnUiThread {
                                    Toast.makeText(requireContext(), "支付成功", Toast.LENGTH_LONG).show()
                                }
                            }
                            else -> {
//                                Snackbar.make(binding.root, "支付失败", Snackbar.LENGTH_LONG).show()
                                requireActivity().runOnUiThread {
                                    Toast.makeText(requireContext(), "支付失败", Toast.LENGTH_LONG).show()
                                }
 
                            }
                        }
                    })
 
                } catch (e: Exception) {
                    Log.d("AliPayHelper","获取订单信息时发生错误: ${e.message}")
                }
            }
        }
 
 
        // 设置提醒
        binding.layoutReminder.setOnClickListener {
            findNavController().navigate(R.id.action_navigation_notifications_to_reminderSettingsFragment)
        }
 
        // 待办
        binding.layoutTodo.setOnClickListener {
            // TODO: 实现待办功能
            Toast.makeText(context, "待办功能开发中", Toast.LENGTH_SHORT).show()
        }
 
        // 好友邀请
        binding.layoutInvite.setOnClickListener {
            shareToWechat()
        }
 
        // AI助手
        binding.layoutAi.setOnClickListener {
            // TODO: 实现AI助手功能
            Toast.makeText(context, "AI助手功能开发中", Toast.LENGTH_SHORT).show()
        }
 
        // 离线模式
        binding.layoutOffline.setOnClickListener {
            // TODO: 实现离线模式功能
            Toast.makeText(context, "离线模式功能开发中", Toast.LENGTH_SHORT).show()
        }
 
        // 关于小红书
        binding.layoutAbout.setOnClickListener {
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(xiaohongshuUrl))
            startActivity(intent)
        }
 
        // 邮件联系
        binding.layoutEmail.setOnClickListener {
            showEmailDialog()
        }
 
        // 意见与反馈
        binding.layoutFeedback.setOnClickListener {
            showFeedbackDialog()
        }
 
        // 隐私协议
        binding.layoutPrivacy.setOnClickListener {
            startContentActivity("privacy_policy", "隐私协议")
            startActivity(intent)
        }
 
        // 使用教程
        binding.layoutTutorial.setOnClickListener {
            startContentActivity("user_guide", "使用教程")
        }
 
        // 头像点击老的处理逻辑
        binding.layoutUserInfo.setOnClickListener {
            currentUserInfo?.let { user ->
                val intent = Intent(requireContext(), EditProfileActivity::class.java).apply {
                    putExtra("nickname", user.name) // 使用数据模型中的字段
                    putExtra("avatar_url", user.cover) // 使用正确的URL字段
                }
                editProfileLauncher.launch(intent)
            } ?: run {
                Toast.makeText(context, "用户信息未加载完成", Toast.LENGTH_SHORT).show()
            }
        }
 
        // VIP续费
        binding.btnRenew.setOnClickListener {
            Toast.makeText(context, "VIP续费功能开发中", Toast.LENGTH_SHORT).show()
        }
    }
 
    private fun showEmailDialog() {
        MaterialAlertDialogBuilder(requireContext())
            .setTitle("联系邮箱")
            .setMessage(contactEmail)
            .setPositiveButton("复制") { _, _ ->
                val clipboard = requireContext().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                val clip = ClipData.newPlainText("email", contactEmail)
                clipboard.setPrimaryClip(clip)
                Toast.makeText(context, "邮箱已复制", Toast.LENGTH_SHORT).show()
            }
            .setNegativeButton("取消", null)
            .show()
    }
 
    private fun showFeedbackDialog() {
        val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_feedback, null)
        val editText = dialogView.findViewById<EditText>(R.id.feedback_edit_text)
 
        MaterialAlertDialogBuilder(requireContext())
            .setTitle("意见反馈")
            .setView(dialogView)
            .setPositiveButton("提交") { _, _ ->
                val feedback = editText.text.toString()
                if (feedback.isNotEmpty()) {
                    // TODO: 提交反馈到服务器
                    Toast.makeText(context, "感谢您的反馈", Toast.LENGTH_SHORT).show()
                }
            }
            .setNegativeButton("取消", null)
            .show()
    }
 
    private fun shareToWechat() {
        try {
            // 获取应用程序的包名
            //val packageName = requireContext().packageName
 
            // 创建分享意图
            val intent = Intent().apply {
                action = Intent.ACTION_SEND
                type = "text/plain"
                putExtra(Intent.EXTRA_TEXT, shareText)
 
                // 指定分享到微信
                setPackage("com.tencent.mm")
            }
            startActivity(Intent.createChooser(intent, "分享到微信"))
        } catch (e: Exception) {
            Toast.makeText(context, "请先安装微信", Toast.LENGTH_SHORT).show()
        }
    }
 
    private fun startContentActivity(type: String, title: String) {
        val intent = Intent(requireContext(), ContentDetailActivity::class.java).apply {
            putExtra(ContentDetailActivity.EXTRA_CONTENT_TYPE, type)
            putExtra(ContentDetailActivity.EXTRA_TITLE, title)
        }
        startActivity(intent)
    }
 
    private suspend fun loadUserInfo() {
        try {
            val response = RetrofitClient.apiService.getUserInfo("17586582287")
            if (response.code == "0" && response.data != null) {
                // 保存用户信息
                currentUserInfo = response.data
                val userInfo = response.data
 
                // 设置头像
                Glide.with(this)
                    .load(userInfo.cover)
                    .placeholder(R.drawable.default_avatar)
                    .into(binding.ivAvatar)
 
                // 设置昵称和账号
                binding.tvNickname.text = userInfo.name
                binding.tvUserId.text = "个人账号:${userInfo.contactTel}"
 
                // 设置VIP信息
                if (userInfo.showed) {
                    binding.ivVip.visibility = View.VISIBLE
                    binding.cardVip.visibility = View.VISIBLE
                    binding.tvVipExpire.text = "${userInfo.overTime} 到期"
                } else {
                    //非会员信息
                    binding.btnRenew.text = "立即开通"
                    binding.linearVipContainer.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.gray))
                    binding.cardVip.visibility = View.GONE
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
            Toast.makeText(context, "获取用户信息失败", Toast.LENGTH_SHORT).show()
        }
    }
 
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
 
        // 加载用户信息
        lifecycleScope.launch {
            loadUserInfo()
        }
    }
 
    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}