Synthetic.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\library\AliPay;
  4. use EasyWeChat\Factory;
  5. use think\cache\driver\Redis;
  6. use think\Db;
  7. /**
  8. * @title 合成藏品
  9. * @controller secondary
  10. * @package app\api\controller
  11. */
  12. class Synthetic extends Base
  13. {
  14. public function initialize(){
  15. parent::initialize();
  16. parent::check_login();
  17. }
  18. /**
  19. * @title 合成藏品列表
  20. * @desc 合成藏品列表
  21. * @author Gavin
  22. * @url /api/Synthetic/synList
  23. * @method POST
  24. * @header name:Authorization require:1 desc:Token
  25. *
  26. *
  27. * @return name:name type:string default:-- desc:藏品名称
  28. * @return name:cover type:string default:-- desc:藏品图
  29. * @return name:label type:string default:-- desc:标签
  30. * @return name:price type:DECIMAL default:-- desc:价格
  31. * @return name:inventory type:int default:-- desc:库存
  32. * @return name:now_inventory type:int default:-- desc:剩余库存
  33. * @return name:sy_state type:float default:-- desc:藏品状态(1:进行中2:即将开售3:已结束4:已售罄)
  34. * @return name:sell_time type:string default:-- desc:发行时间
  35. * @return name:end_time type:string default:-- desc:结束时间
  36. */
  37. public function synList(){
  38. checkSynCollectionState();
  39. $list = Db::name('store_collection')
  40. ->where('is_deleted',0)
  41. ->where('status',1)
  42. ->where('type',2)
  43. ->field('id,cover,name,label,price,inventory,now_inventory,sy_state,sell_time,end_time')
  44. ->order('sy_state asc,sell_time asc')
  45. ->select();
  46. foreach ($list as &$v) {
  47. $v['now_inventory'] = getCollectionInventory($v['id']);
  48. $v['id'] = (string)$v['id'];
  49. }
  50. $this->success('成功',$list);
  51. }
  52. /**
  53. * @title 藏品详情
  54. * @desc 藏品详情
  55. * @author Gavin
  56. * @url /api/Synthetic/CollectionDetail
  57. * @method POST
  58. * @header name:Authorization require:1 desc:Token
  59. *
  60. * @param name:id type:string require:1 default:-- desc:藏品ID
  61. *
  62. * @return name:name type:string default:-- desc:藏品名称
  63. * @return name:cover type:string default:-- desc:藏品图
  64. * @return name:label type:string default:-- desc:标签
  65. * @return name:price type:DECIMAL default:-- desc:价格
  66. * @return name:inventory type:int default:-- desc:库存
  67. * @return name:now_inventory type:int default:-- desc:剩余库存
  68. * @return name:sy_state type:float default:-- desc:藏品状态(1:进行中2:即将开售3:已结束4:已售罄)
  69. * @return name:sell_time type:string default:-- desc:发行时间
  70. * @return name:end_time type:string default:-- desc:结束时间
  71. * @return name:describe type:string default:-- desc:商品描述
  72. * @return name:instructions type:int default:1 desc:权益说明
  73. * @return name:buy_notice type:int default:1 desc:购买须知
  74. * @return name:warm_prompt type:int default:1 desc:温馨提示
  75. * @return name:auth_img type:int default:1 desc:作者头像
  76. * @return name:auth_name type:int default:1 desc:作者姓名
  77. * @return name:is_exchange type:int default:1 desc:材料是否够(1:够,可以合成0:不够)
  78. * @return name:material@name type:string default:-- desc:材料名称
  79. * @return name:material@cover type:string default:-- desc:材料图片
  80. * @return name:material@num type:string default:-- desc:所需数量
  81. * @return name:material@now_num type:string default:-- desc:当前所有数量
  82. */
  83. public function CollectionDetail(){
  84. $collect_id = input('id');
  85. if (!$collect_id) $this->error('参数错误');
  86. checkSynCollectionState($collect_id);
  87. $info = Db::name('store_collection')
  88. ->where('is_deleted',0)
  89. ->where('status',1)
  90. ->where('type',2)
  91. ->where('id',$collect_id)
  92. ->field('id,cover,name,label,price,inventory,now_inventory,sy_state,sell_time,end_time,describe,buy_count,instructions,buy_notice,warm_prompt,auth_img,auth_name,share_img,intro')
  93. ->find();
  94. if (!$info) $this->error('藏品不存在');
  95. $info['describe'] = explode('|',$info['describe']);
  96. $info['now_inventory'] = getCollectionInventory($info['id']);
  97. $material = Db::name('store_collection_material')
  98. ->alias('a')
  99. ->join('store_collection b','a.c_id=b.id')
  100. ->where('a.cid',$collect_id)
  101. ->field('a.id,a.c_id,a.num,b.name,b.cover')
  102. ->select();
  103. $info['is_exchange'] = $this->check_exchange($collect_id,$this->uid);
  104. foreach ($material as &$v){
  105. $v['now_num'] = Db::name('store_order_info')
  106. ->where('c_id',$v['c_id'])
  107. ->where('mid',$this->uid)
  108. ->where('status','neq',2)
  109. ->where('is_destruction',1)
  110. ->count();
  111. }
  112. $info['material'] = $material;
  113. $info['id'] = (string)$info['id'];
  114. $this->success('成功',$info);
  115. }
  116. /**
  117. * 判断材料是否足够可以兑换
  118. */
  119. public function check_exchange($id,$mid){
  120. $list = Db::name('store_collection_material')
  121. ->alias('a')
  122. ->join('store_collection b','a.c_id=b.id')
  123. ->where('a.cid',$id)
  124. ->field('a.id,a.c_id,a.num,b.name,b.cover')
  125. ->select();
  126. $is_exchange = 1;
  127. foreach ($list as &$v){
  128. $user_count = Db::name('store_order_info')
  129. ->where('c_id',$v['c_id'])
  130. ->where('mid',$mid)
  131. ->where('status','neq',2)
  132. ->where('is_destruction',1)
  133. ->count();
  134. if ($user_count<$v['num']){
  135. $is_exchange = 0;
  136. break;
  137. }
  138. }
  139. return $is_exchange;
  140. }
  141. /**
  142. * @title 合成藏品
  143. * @desc 合成藏品
  144. * @author Gavin
  145. * @url /api/Synthetic/synCollection
  146. * @method POST
  147. * @header name:Authorization require:1 desc:Token
  148. *
  149. * @param name:id type:int require:1 default:1 desc:藏品ID
  150. */
  151. public function synCollection(){
  152. $id = input('id');
  153. if (!$id) $this->error('参数错误');
  154. $info = Db::name('store_collection')
  155. ->where('is_deleted',0)
  156. ->where('status',1)
  157. ->where('type',2)
  158. ->where('id',$id)
  159. ->find();
  160. if (!$info) $this->error('藏品不存在');
  161. $now_inventory = getCollectionInventory($id);
  162. if ($now_inventory<=0) $this->error('已售罄');
  163. if (strtotime($info['sell_time'])>time()) $this->error('未开始,无法合成');
  164. if (strtotime($info['end_time'])<time()) $this->error('已结束,无法合成');
  165. // $info_count = Db::name('store_order_info')->where('mid',$this->uid)->where('c_id',$id)->count();
  166. // if ($info_count) $this->error('已合成过,只能合成一次');
  167. //获取是否已经铸造hash
  168. $is_nft = Db::name('hash2')->where('goods_id',$id)->where('success',1)->find();
  169. if (!$is_nft) $this->error('nft未上架,无法合成');
  170. $is_exchange = $this->check_exchange($id,$this->uid);
  171. if (!$is_exchange) $this->error('材料不足,无法合成');
  172. $com = true;
  173. Db::startTrans();
  174. try {
  175. $material = Db::name('store_collection_material')
  176. ->where('cid',$id)
  177. ->select();
  178. foreach ($material as &$v){
  179. Db::name('store_order_info')
  180. ->where('c_id',$v['c_id'])
  181. ->where('mid',$this->uid)
  182. ->where('status','neq',2)
  183. ->where('is_destruction',1)
  184. ->limit($v['num'])
  185. ->order('id asc')
  186. ->update(['is_destruction'=>0]);
  187. }
  188. //获取排名
  189. $rank = getRanking($id)+1;
  190. $tag = getTag($id,$rank,$info['inventory']);
  191. saveRanking($id);
  192. $collectors_hash = '';
  193. $date = [
  194. 'order_id'=>0,
  195. 'order_no'=>get_order_sn(),
  196. 'tag'=>$tag,
  197. 'mid'=>$this->uid,
  198. 'c_id'=>$id,
  199. 'name'=>$info['name'],
  200. 'cover'=>$info['cover'],
  201. 'pro_info'=>json_encode($info,true),
  202. 'type'=>$info['type'],
  203. 'tokenid'=>$is_nft['class_id'],
  204. 'nfttype'=>$is_nft['operationId'],
  205. 'collectors_hash'=>$collectors_hash,
  206. 'collectors_hash_time'=>'',
  207. 'status'=>5
  208. ];
  209. Db::name('store_order_info')->insert($date);
  210. Db::commit();
  211. }catch (\Exception $e){
  212. $com=false;
  213. Db::rollback();
  214. }
  215. if ($com){
  216. setMemberInfoHash($this->uid);
  217. //减掉库存
  218. loseCollectionInventory($id,1);
  219. $this->success('合成成功');
  220. }
  221. $this->error('合成失败,请稍后重试');
  222. }
  223. }