Synthetic.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. }
  49. $this->success('成功',$list);
  50. }
  51. /**
  52. * @title 藏品详情
  53. * @desc 藏品详情
  54. * @author Gavin
  55. * @url /api/Synthetic/CollectionDetail
  56. * @method POST
  57. * @header name:Authorization require:1 desc:Token
  58. *
  59. * @param name:id type:string require:1 default:-- desc:藏品ID
  60. *
  61. * @return name:name type:string default:-- desc:藏品名称
  62. * @return name:cover type:string default:-- desc:藏品图
  63. * @return name:label type:string default:-- desc:标签
  64. * @return name:price type:DECIMAL default:-- desc:价格
  65. * @return name:inventory type:int default:-- desc:库存
  66. * @return name:now_inventory type:int default:-- desc:剩余库存
  67. * @return name:sy_state type:float default:-- desc:藏品状态(1:进行中2:即将开售3:已结束4:已售罄)
  68. * @return name:sell_time type:string default:-- desc:发行时间
  69. * @return name:end_time type:string default:-- desc:结束时间
  70. * @return name:describe type:string default:-- desc:商品描述
  71. * @return name:instructions type:int default:1 desc:权益说明
  72. * @return name:buy_notice type:int default:1 desc:购买须知
  73. * @return name:warm_prompt type:int default:1 desc:温馨提示
  74. * @return name:auth_img type:int default:1 desc:作者头像
  75. * @return name:auth_name type:int default:1 desc:作者姓名
  76. * @return name:is_exchange type:int default:1 desc:材料是否够(1:够,可以合成0:不够)
  77. * @return name:material@name type:string default:-- desc:材料名称
  78. * @return name:material@cover type:string default:-- desc:材料图片
  79. * @return name:material@num type:string default:-- desc:所需数量
  80. * @return name:material@now_num type:string default:-- desc:当前所有数量
  81. */
  82. public function CollectionDetail(){
  83. $collect_id = input('id');
  84. if (!$collect_id) $this->error('参数错误');
  85. checkSynCollectionState($collect_id);
  86. $info = Db::name('store_collection')
  87. ->where('is_deleted',0)
  88. ->where('status',1)
  89. ->where('type',2)
  90. ->where('id',$collect_id)
  91. ->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')
  92. ->find();
  93. if (!$info) $this->error('藏品不存在');
  94. $info['describe'] = explode('|',$info['describe']);
  95. $info['now_inventory'] = getCollectionInventory($info['id']);
  96. $material = Db::name('store_collection_material')
  97. ->alias('a')
  98. ->join('store_collection b','a.c_id=b.id')
  99. ->where('a.cid',$collect_id)
  100. ->field('a.id,a.c_id,a.num,b.name,b.cover')
  101. ->select();
  102. $info['is_exchange'] = $this->check_exchange($collect_id,$this->uid);
  103. foreach ($material as &$v){
  104. $v['now_num'] = Db::name('store_order_info')
  105. ->where('c_id',$v['c_id'])
  106. ->where('mid',$this->uid)
  107. ->where('status','neq',2)
  108. ->where('is_destruction',1)
  109. ->count();
  110. }
  111. $info['material'] = $material;
  112. $this->success('成功',$info);
  113. }
  114. /**
  115. * 判断材料是否足够可以兑换
  116. */
  117. public function check_exchange($id,$mid){
  118. $list = Db::name('store_collection_material')
  119. ->alias('a')
  120. ->join('store_collection b','a.c_id=b.id')
  121. ->where('a.cid',$id)
  122. ->field('a.id,a.c_id,a.num,b.name,b.cover')
  123. ->select();
  124. $is_exchange = 1;
  125. foreach ($list as &$v){
  126. $user_count = Db::name('store_order_info')
  127. ->where('c_id',$v['c_id'])
  128. ->where('mid',$mid)
  129. ->where('status','neq',2)
  130. ->where('is_destruction',1)
  131. ->count();
  132. if ($user_count<$v['num']){
  133. $is_exchange = 0;
  134. break;
  135. }
  136. }
  137. return $is_exchange;
  138. }
  139. /**
  140. * @title 合成藏品
  141. * @desc 合成藏品
  142. * @author Gavin
  143. * @url /api/Synthetic/synCollection
  144. * @method POST
  145. * @header name:Authorization require:1 desc:Token
  146. *
  147. * @param name:id type:int require:1 default:1 desc:藏品ID
  148. */
  149. public function synCollection(){
  150. $id = input('id');
  151. if (!$id) $this->error('参数错误');
  152. $info = Db::name('store_collection')
  153. ->where('is_deleted',0)
  154. ->where('status',1)
  155. ->where('type',2)
  156. ->where('id',$id)
  157. ->find();
  158. if (!$info) $this->error('藏品不存在');
  159. $now_inventory = getCollectionInventory($id);
  160. if ($now_inventory<=0) $this->error('已售罄');
  161. if (strtotime($info['sell_time'])>time()) $this->error('未开始,无法合成');
  162. if (strtotime($info['end_time'])<time()) $this->error('已结束,无法合成');
  163. $info = Db::name('store_order_info')->where('mid',$this->uid)->where('c_id',$id)->count();
  164. if ($info) $this->error('已合成过,只能合成一次');
  165. //获取是否已经铸造hash
  166. $hashCount = getLenCollection($id);
  167. if (!$hashCount) $this->error('hash未铸造,无法合成');
  168. $is_exchange = $this->check_exchange($id,$this->uid);
  169. if (!$is_exchange) $this->error('材料不足,无法合成');
  170. $com = true;
  171. Db::startTrans();
  172. try {
  173. $material = Db::name('store_collection_material')
  174. ->where('cid',$id)
  175. ->select();
  176. foreach ($material as &$v){
  177. Db::name('store_order_info')
  178. ->where('c_id',$v['c_id'])
  179. ->where('mid',$this->uid)
  180. ->where('status','neq',2)
  181. ->where('is_destruction',1)
  182. ->limit($v['num'])
  183. ->order('id asc')
  184. ->update(['is_destruction'=>0]);
  185. }
  186. //获取排名
  187. $rank = getRanking($id)+1;
  188. $tag = getTag($id,$rank,$info['inventory']);
  189. saveRanking($id);
  190. $company = '南宁中科数艺科技有限公司';
  191. $hash = getCompanyHash($id);
  192. $company_hash = $hash['hash'];
  193. $company_hash_time = $hash['create_at'];
  194. $tokenid = $hash['tokenid'];
  195. Db::name('hash')->where('hash',$hash['hash'])->update(['status'=>1]);
  196. $collectors_hash = '';
  197. $date = [
  198. 'order_id'=>0,
  199. 'order_no'=>get_order_sn(),
  200. 'tag'=>$tag,
  201. 'mid'=>$this->uid,
  202. 'c_id'=>$id,
  203. 'name'=>$info['name'],
  204. 'cover'=>$info['cover'],
  205. 'pro_info'=>json_encode($info,true),
  206. 'company'=>$company,
  207. 'company_hash'=>$company_hash,
  208. 'company_hash_time'=>$company_hash_time,
  209. 'ddcid'=>$tokenid,
  210. 'collectors_hash'=>$collectors_hash,
  211. 'collectors_hash_time'=>'',
  212. 'status'=>5
  213. ];
  214. Db::name('store_order_info')->insert($date);
  215. Db::commit();
  216. }catch (\Exception $e){
  217. $com=false;
  218. Db::rollback();
  219. }
  220. if ($com){
  221. setMemberInfoHash($this->uid);
  222. //减掉库存
  223. loseCollectionInventory($id,1);
  224. $this->success('合成成功');
  225. }
  226. $this->error('合成失败,请稍后重试');
  227. }
  228. }