IAPUserData.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. float 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 == "special" ? 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 = std::stof(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(std::fabs(a.price - b.price) < 1e-9) return a.price < 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. for(const auto& buyInfo : buyInfos){
  61. for(size_t i = 0; i < vec.size(); i++){
  62. if(buyInfo != vec[i].id)continue;
  63. if(!vec[i].isSpecial) onlySpecial = false;
  64. if(i == vec.size() - 1) isBuyExpensive = true;
  65. }
  66. }
  67. // 购买过最贵商品
  68. if(isBuyExpensive)return UserBuyType::LotShopping;
  69. // 仅购买Special商品
  70. if(onlySpecial)return UserBuyType::LittleShopping;
  71. // 正常购物
  72. return UserBuyType::NormalShopping;
  73. }
  74. NS_IAP_END