Goods.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. namespace app\store\controller;
  3. use library\Controller;
  4. use think\cache\driver\Redis;
  5. use think\Db;
  6. /**
  7. * 商品
  8. * Class Goods
  9. * @package app\store\controller
  10. */
  11. class Goods extends Controller
  12. {
  13. /**
  14. * 绑定数据表
  15. * @var string
  16. */
  17. protected $table = 'store_collection';
  18. /**
  19. * 商品列表
  20. * @auth true
  21. * @menu true
  22. * @throws \think\Exception
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. * @throws \think\exception\DbException
  26. * @throws \think\exception\PDOException
  27. */
  28. public function index()
  29. {
  30. checkCollectionState();
  31. $this->title = '藏品管理';
  32. $query = $this->_query($this->table)->where('is_deleted',0)->like('name');
  33. $query->dateBetween('create_at')->order('id desc')->page();
  34. }
  35. /**
  36. * 数据列表处理
  37. * @auth true
  38. * @menu true
  39. * @param array $data
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. * @throws \think\exception\DbException
  43. */
  44. protected function _index_page_filter(&$data)
  45. {
  46. foreach ($data as &$v){
  47. $checksell = $this->checkSellTime($v['id']);
  48. if (!$checksell){
  49. $v['is_save'] = 0;
  50. }else{
  51. $v['is_save'] = 1;
  52. }
  53. $v['zz']=Db::table('hash')->where('goods_id',$v['id'])->where('success',1)->count();
  54. }
  55. }
  56. /**
  57. * 添加商品
  58. * @auth true
  59. * @menu true
  60. * @throws \think\Exception
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. * @throws \think\exception\DbException
  64. * @throws \think\exception\PDOException
  65. */
  66. public function add()
  67. {
  68. $this->title = '添加藏品';
  69. $this->_form($this->table, 'form');
  70. }
  71. /**
  72. * 编辑商品
  73. * @auth true
  74. * @menu true
  75. * @throws \think\Exception
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. * @throws \think\exception\DbException
  79. * @throws \think\exception\PDOException
  80. */
  81. function edit()
  82. {
  83. $this->title = '编辑商品';
  84. $id = $this->request->get('id');
  85. $checksell = $this->checkSellTime($id);
  86. if (!$checksell) $this->error('藏品已经开始抢购或已过期,无法修改');
  87. $this->_form($this->table, 'form');
  88. }
  89. /**
  90. * 表单数据处理
  91. * @auth true
  92. * @menu true
  93. * @param array $data
  94. */
  95. protected function _form_filter(&$data)
  96. {
  97. if($this->request->post()){
  98. if (isset($data['id']) || !empty($data['id'])){
  99. $checksell = $this->checkSellTime($data['id']);
  100. if (!$checksell) $this->error('藏品已经开始抢购或已过期,无法修改');
  101. }
  102. if ($data['cover'] == '') $this->error('请上传藏品图片');
  103. if ($data['auth_img'] == '') $this->error('请上传作者头像');
  104. if ($data['describe'] == '') $this->error('请上传商品描述');
  105. if ($data['buy_count']<1) $this->error('限购数量不能小于1');
  106. //if ($data['share_img'] == '') $this->error('请上传分享二维码');
  107. $data['date'] = date('Y-m-d',strtotime($data['sell_time']));
  108. if (isset($data['id'])){
  109. $buy_count = Db::name('store_order_info')->where('c_id',$data['id'])->count();
  110. }else{
  111. $buy_count = 0;
  112. }
  113. $data['now_inventory'] = $data['inventory']-$buy_count;
  114. }else{
  115. if (!empty($data)){
  116. $data['covers'] = $data['cover'];
  117. $data['auth_imgs'] = $data['auth_img'];
  118. $data['describes'] = $data['describe'];
  119. $data['share_imgs'] = $data['share_img'];
  120. }
  121. }
  122. }
  123. /**
  124. * 处理成功回调
  125. */
  126. public function _form_result($result,$data){
  127. if ($result) {
  128. setCollectionInfoHash($result);
  129. $redis = new Redis();
  130. $count = Db::name($this->table)->where('id',$result)->value('now_inventory');
  131. $redis->set('collection_count_'.$result,$count);
  132. $this->success('操作成功',url('/#/store/goods/index'));
  133. }
  134. }
  135. /**
  136. * @auth true
  137. * @menu true
  138. * 商品上架
  139. */
  140. public function up()
  141. {
  142. $data = $this->request->post();
  143. if (Db::name($this->table)->where('id',$data['id'])->update(['status'=>1])){
  144. setCollectionInfoHash($data['id']);
  145. $this->success('恭喜您,数据更新成功');
  146. }else{
  147. $this->error('数据更新失败');
  148. }
  149. }
  150. /**
  151. * @auth true
  152. * @menu true
  153. * 商品下架
  154. */
  155. public function down()
  156. {
  157. $data = $this->request->post();
  158. if (Db::name($this->table)->where('id',$data['id'])->update(['status'=>0])){
  159. setCollectionInfoHash($data['id']);
  160. $this->success('恭喜您,数据更新成功');
  161. }else{
  162. $this->error('数据更新失败');
  163. }
  164. //$this->_save($this->table, ['status' => '0']);
  165. }
  166. /**
  167. * @auth true
  168. * @menu true
  169. * 商品删除
  170. */
  171. public function del()
  172. {
  173. $data = $this->request->post();
  174. if (Db::name($this->table)->where('id',$data['id'])->update(['is_deleted'=>1])){
  175. setCollectionInfoHash($data['id']);
  176. $this->success('恭喜您,数据更新成功');
  177. }else{
  178. $this->error('数据更新失败');
  179. }
  180. //$this->_save($this->table, ['is_deleted' => '1']);
  181. }
  182. // public function _save_result($result){
  183. // if ($result){
  184. // setCollectionInfoHash($result);
  185. // }
  186. // }
  187. //判断是否已经抢购开始
  188. function checkSellTime($id){
  189. $sell_time = Db::name($this->table)->where('id',$id)->value('sell_time');
  190. $advance_minutes = getAdvanceMinutes();
  191. if ($advance_minutes>0){
  192. $sell_time = strtotime($sell_time)-($advance_minutes*60);
  193. }else{
  194. $sell_time = strtotime($sell_time);
  195. }
  196. if ($sell_time<=time()) return false;
  197. return true;
  198. }
  199. public function send(){
  200. if($this->request->post()){
  201. $data = $this->request->post();
  202. if (!isset($data['mid']) || $data['mid']==''){
  203. $this->error('请选择用户');
  204. }
  205. $inventory = Db::name($this->table)->where('id',$data['id'])->value('inventory');
  206. $info = Db::name($this->table)->where('id',$data['id'])->find();
  207. $zhuzao_count = Db::name('hash')
  208. ->where('goods_id',$data['id'])
  209. ->where('success',1)
  210. ->where('status',0)
  211. ->count();
  212. if ($zhuzao_count<$data['number']){
  213. $this->error('铸造的的数量不足,无法赠送');
  214. }
  215. $com = true;
  216. Db::startTrans();
  217. try {
  218. $array = [];
  219. for ($i=0;$i<$data['number'];$i++){
  220. //获取排名
  221. $rank = getRanking($data['id'])+1;
  222. $tag = getTag($data['id'],$rank,$inventory);
  223. saveRanking($data['id']);
  224. $company = '象寻数字科技(上海)有限公司';
  225. $hash = getCompanyHash($data['id']);
  226. $company_hash = $hash['hash'];
  227. $ddcid = Db::name('hash')->where('hash',$hash['hash'])->value('ddcid');
  228. $company_hash_time = $hash['create_at'] ? $hash['create_at'] : date('Y-m-d H:i:s');
  229. Db::name('hash')->where('hash',$hash['hash'])->update(['status'=>1]);
  230. $collectors_hash = '';
  231. $date = [
  232. 'order_no'=>get_order_sn(),
  233. 'tag'=>$tag,
  234. 'mid'=>$data['mid'],
  235. 'c_id'=>$data['id'],
  236. 'pro_info'=>json_encode($info,true),
  237. 'company'=>$company,
  238. 'company_hash'=>$company_hash,
  239. 'company_hash_time'=>$company_hash_time,
  240. 'ddcid'=>$ddcid,
  241. 'collectors_hash'=>$collectors_hash,
  242. 'collectors_hash_time'=>''
  243. ];
  244. $array[] = $date;
  245. }
  246. Db::name('store_order_info')->insertAll($array);
  247. //减少数据库库存
  248. Db::name($this->table)->where('id',$data['id'])->setDec('now_inventory',$data['number']);
  249. Db::commit();
  250. } catch (\Exception $e){
  251. Db::rollback();
  252. $com = false;
  253. }
  254. if ($com){
  255. //减掉库存
  256. loseCollectionInventory($data['id'],$data['number']);
  257. $this->success('赠送成功');
  258. }else{
  259. $this->error('赠送失败');
  260. }
  261. }else{
  262. $id=$this->request->get('id');
  263. $this->assign('id',$id);
  264. $user = Db::name('store_member')->where('is_deleted',0)->field('id,name,phone')->where('is_auth',1)->select();
  265. $this->assign('user',$user);
  266. $this->fetch();
  267. }
  268. }
  269. public function hash(){
  270. if($this->request->post()){
  271. $id = $this->request->post('id');
  272. $number = $this->request->post('number');
  273. if ($number<=0){
  274. $this->error('数量错误');
  275. }
  276. if (Db::name('store_collection')->where('id',$id)->setInc('casting_num',$number)){
  277. $redis = new Redis();
  278. $redis->Incrby('castingHash_'.$id,$number);
  279. $this->success('增加成功,正在铸造中');
  280. }
  281. $this->error('增加失败,请稍后重试');
  282. Db::name('store_collection')->where('id',$id)->setInc('casting_num',$number);
  283. $goods=Db::table('store_collection')->where('id',$this->request->post('id'))->find();
  284. if($number){
  285. $url2='http://192.144.219.204:8083/ddc/getNonce';
  286. $nonce=$this->curlRequest($url2);
  287. $result=json_decode($nonce,true);
  288. if(isset($result['code'])){
  289. $this->error($result['msg']);
  290. }
  291. for ($i=1;$i<=$number;$i++){
  292. $address='0x8583c53ca3759f0893cb6c156b682e8fef22ed95';
  293. $str=$goods['id'].'-'.rand(1000000,9999999);
  294. // $url='http://192.144.219.204:8083/ddc/createHash?address='.$address.'&ddcURI='.$str;
  295. $nonce = $nonce + 1;
  296. $url='http://192.144.219.204:8083/ddc/createHash?address='.$address.'&ddcURI='.$str.'&nonce='.$nonce;
  297. $res=$this->curlRequest($url);
  298. $result=json_decode($res,true);
  299. if($result['code']){
  300. $this->error($result['msg']);
  301. }
  302. $data['goods_id']=$id;
  303. $data['hash']=$res;
  304. $data['status']=0;
  305. $redis = new Redis();
  306. if (Db::table('hash')->insert($data)){
  307. //存入reids list
  308. $redis_data = ['hash'=>$res,'create_at'=>date('Y-m-d H:i:s')];
  309. $redis->rPush('collectionHash_'.$id,json_encode($redis_data));
  310. }
  311. }
  312. }else{
  313. $this->error('请填写正确的铸造数量');
  314. }
  315. $this->success('铸造成功',url('/#/store/goods/index'));
  316. }else{
  317. $id=$this->request->get('id');
  318. $this->assign('id',$id);
  319. $this->fetch();
  320. }
  321. }
  322. public function detail(){
  323. $id=$this->request->get('id');
  324. $this->title = Db::name($this->table)->where('id',$id)->value('name').'--铸造明细';
  325. $query = $this->_query('hash')->where('goods_id',$id)->where('success',1);
  326. $query->dateBetween('create_at')->order('id desc')->page();
  327. $this->fetch();
  328. }
  329. public function curlRequest($url, $headers = [], $body = [], $method = "GET")
  330. {
  331. $ch = curl_init();
  332. curl_setopt($ch, CURLOPT_URL, $url);
  333. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置请求头
  334. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);//设置请求体
  335. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //定义请求类型
  336. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  337. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  338. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  339. curl_setopt($ch, CURLOPT_HTTP_VERSION, 'CURL_HTTP_VERSION_1_1');
  340. $output = curl_exec($ch);
  341. curl_close($ch);
  342. return $output;
  343. }
  344. }