IAPUserData.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // IAPUserData.cpp
  3. // TileManor
  4. //
  5. // Created by 徐炼新 on 2024/9/30.
  6. //
  7. #include "IAPUserData.hpp"
  8. #include "IAPConf.hpp"
  9. #include <algorithm>
  10. #include <string>
  11. #include <regex>
  12. NS_IAP_BEGIN
  13. IAPUserData* IAPUserData::_instance = nullptr;
  14. IAPUserData* IAPUserData::getInstance(){
  15. if(!_instance){
  16. _instance = new IAPUserData();
  17. }
  18. return _instance;
  19. }
  20. void IAPUserData::init(){
  21. }
  22. void IAPUserData::addBuyInfo(std::string commodityID){
  23. buyInfos.push_back(commodityID);
  24. }
  25. UserBuyType IAPUserData::getUserBuyType(){
  26. // 如果没有购买记录
  27. if(buyInfos.size() == 0) return UserBuyType::NoShopping;
  28. auto conf = IAPConf::getInstance();
  29. vector<GoodsInfo> goodInfos;
  30. conf->getAllGoods(goodInfos);
  31. struct SortVec{
  32. std::string id;
  33. std::string price;
  34. bool isSpecial;
  35. };
  36. vector<SortVec> vec;
  37. std::regex re(R"(\d+(\.\d{1,2})?)"); // 匹配整数或最多两位小数的数字
  38. for(const auto& goodInfo : goodInfos){
  39. bool isSpecial = goodInfo.style == "mostValued" ? true : false;
  40. std::smatch match;
  41. if (std::regex_search(goodInfo.cost, match, re)) {
  42. SortVec sv;
  43. sv.id = goodInfo.id;
  44. sv.price = match[0]; // 提取价格
  45. sv.isSpecial = isSpecial;
  46. vec.push_back(sv);
  47. } else {
  48. log("IAPUserData::getUserBuyType : 转换失败");
  49. }
  50. }
  51. // 按照价格排序 , 价格相同按照是否为special商品排序
  52. sort(vec.begin(), vec.end(), [](SortVec a,SortVec b){
  53. if(a.price != b.price) return stof(a.price) < stof(b.price);
  54. if(a.isSpecial)return true;
  55. if(b.isSpecial)return false;
  56. return true;
  57. });
  58. bool onlySpecial = true;
  59. bool isBuyExpensive = false;
  60. float maxAmount = getMaxAmount();
  61. for(const auto& buyInfo : buyInfos){
  62. for(size_t i = 0; i < vec.size(); i++){
  63. if(buyInfo != vec[i].id)continue;
  64. if(!vec[i].isSpecial) onlySpecial = false;
  65. if(stof(vec[i].price) == maxAmount) isBuyExpensive = true;
  66. }
  67. }
  68. // 购买过最贵商品
  69. if(isBuyExpensive)return UserBuyType::LotShopping;
  70. // 仅购买Special商品
  71. if(onlySpecial)return UserBuyType::LittleShopping;
  72. // 正常购物
  73. return UserBuyType::NormalShopping;
  74. }
  75. void IAPUserData::clearBuyInfo(){
  76. buyInfos.clear();
  77. }
  78. float IAPUserData::getMaxAmount(){
  79. float maxn = 0.0;
  80. auto conf = IAPConf::getInstance();
  81. vector<GoodsInfo> goodInfos;
  82. conf->getAllGoods(goodInfos);
  83. std::regex re(R"(\d+(\.\d{1,2})?)"); // 匹配整数或最多两位小数的数字
  84. for(const auto& goodInfo : goodInfos){
  85. std::smatch match;
  86. if (std::regex_search(goodInfo.cost, match, re)) {
  87. maxn = std::max(maxn, stof(match[0])); // 提取价格
  88. } else {
  89. log("IAPUserData::getUserBuyType : 转换失败");
  90. }
  91. }
  92. return maxn;
  93. }
  94. NS_IAP_END