Goods2.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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 Goods2 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. checkSynCollectionState();
  31. $this->title = '合成藏品管理';
  32. $query = $this->_query($this->table)->where('is_deleted',0)->like('name');
  33. $query->dateBetween('create_at')->where('type',2)->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'])->count();
  54. $now_inventory = getCollectionInventory($v['id']);
  55. $v['now_inventory'] = $now_inventory<=0 ? 0 : $now_inventory;
  56. $v['shengyu'] = Db::name('hash')->where('goods_id',$v['id'])->where('status',0)->count();
  57. }
  58. }
  59. /**
  60. * 添加商品
  61. * @auth true
  62. * @menu true
  63. * @throws \think\Exception
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * @throws \think\exception\DbException
  67. * @throws \think\exception\PDOException
  68. */
  69. public function add()
  70. {
  71. $this->title = '添加藏品';
  72. $this->_form($this->table, 'form');
  73. }
  74. /**
  75. * 编辑商品
  76. * @auth true
  77. * @menu true
  78. * @throws \think\Exception
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. * @throws \think\exception\DbException
  82. * @throws \think\exception\PDOException
  83. */
  84. function edit()
  85. {
  86. $this->title = '编辑商品';
  87. $id = $this->request->get('id');
  88. $checksell = $this->checkSellTime($id);
  89. // if (!$checksell) $this->error('藏品已经开始抢购或已过期,无法修改');
  90. $this->_form($this->table, 'form');
  91. }
  92. /**
  93. * 表单数据处理
  94. * @auth true
  95. * @menu true
  96. * @param array $data
  97. */
  98. protected function _form_filter(&$data)
  99. {
  100. if($this->request->post()){
  101. // if (isset($data['id']) || !empty($data['id'])){
  102. // $checksell = $this->checkSellTime($data['id']);
  103. // if (!$checksell) $this->error('藏品已经开始抢购或已过期,无法修改');
  104. // }
  105. if ($data['cover'] == '') $this->error('请上传藏品图片');
  106. if ($data['price']<=0) $this->error('藏品价格错误');
  107. if ($data['auth_img'] == '') $this->error('请上传作者头像');
  108. if ($data['describe'] == '') $this->error('请上传商品描述');
  109. // if ($data['buy_count']<1) $this->error('限购数量不能小于1');
  110. //if ($data['share_img'] == '') $this->error('请上传分享二维码');
  111. $data['date'] = date('Y-m-d',strtotime($data['sell_time']));
  112. if (isset($data['id'])){
  113. $buy_count = Db::name('store_order_info')
  114. ->whereIn('status','1,3')
  115. ->where('c_id',$data['id'])
  116. ->count();
  117. }else{
  118. $buy_count = 0;
  119. }
  120. $data['type'] = 2;
  121. $data['now_inventory'] = $data['inventory']-$buy_count;
  122. }else{
  123. if (!empty($data)){
  124. $data['covers'] = $data['cover'];
  125. $data['auth_imgs'] = $data['auth_img'];
  126. $data['describes'] = $data['describe'];
  127. $data['share_imgs'] = $data['share_img'];
  128. }
  129. $list = Db::name('store_collection')->field('id,name,cover')->select();
  130. $this->list = $list;
  131. }
  132. }
  133. /**
  134. * 处理成功回调
  135. */
  136. public function _form_result($result,$data){
  137. if ($result) {
  138. setCollectionInfoHash($result);
  139. $redis = new Redis();
  140. $count = Db::name($this->table)->where('id',$result)->value('now_inventory');
  141. $redis->set('collection_count_'.$result,$count);
  142. $this->success('操作成功',url('/#/store/goods2/index'));
  143. }
  144. }
  145. /**
  146. * @auth true
  147. * @menu true
  148. * 商品上架
  149. */
  150. public function up()
  151. {
  152. $data = $this->request->post();
  153. if (Db::name($this->table)->where('id',$data['id'])->update(['status'=>1])){
  154. setCollectionInfoHash($data['id']);
  155. $this->success('恭喜您,数据更新成功');
  156. }else{
  157. $this->error('数据更新失败');
  158. }
  159. }
  160. /**
  161. * @auth true
  162. * @menu true
  163. * 商品下架
  164. */
  165. public function down()
  166. {
  167. $data = $this->request->post();
  168. if (Db::name($this->table)->where('id',$data['id'])->update(['status'=>0])){
  169. setCollectionInfoHash($data['id']);
  170. $this->success('恭喜您,数据更新成功');
  171. }else{
  172. $this->error('数据更新失败');
  173. }
  174. //$this->_save($this->table, ['status' => '0']);
  175. }
  176. /**
  177. * @auth true
  178. * @menu true
  179. * 商品删除
  180. */
  181. public function del()
  182. {
  183. $data = $this->request->post();
  184. if (Db::name($this->table)->where('id',$data['id'])->update(['is_deleted'=>1])){
  185. setCollectionInfoHash($data['id']);
  186. $this->success('恭喜您,数据更新成功');
  187. }else{
  188. $this->error('数据更新失败');
  189. }
  190. //$this->_save($this->table, ['is_deleted' => '1']);
  191. }
  192. // public function _save_result($result){
  193. // if ($result){
  194. // setCollectionInfoHash($result);
  195. // }
  196. // }
  197. //判断是否已经抢购开始
  198. function checkSellTime($id){
  199. $sell_time = Db::name($this->table)->where('id',$id)->value('sell_time');
  200. $advance_minutes = getAdvanceMinutes();
  201. if ($advance_minutes>0){
  202. $sell_time = strtotime($sell_time)-($advance_minutes*60);
  203. }else{
  204. $sell_time = strtotime($sell_time);
  205. }
  206. if ($sell_time<=time()) return false;
  207. return true;
  208. }
  209. public function send(){
  210. if($this->request->post()){
  211. $data = $this->request->post();
  212. if (!isset($data['mid']) || $data['mid']==''){
  213. $this->error('请选择用户');
  214. }
  215. $inventory = Db::name($this->table)->where('id',$data['id'])->value('inventory');
  216. $info = Db::name($this->table)->where('id',$data['id'])->find();
  217. $redis = new Redis();
  218. $zhuzao_count = $redis->Llen('collectionHash_'.$data['id']);
  219. if ($zhuzao_count<$data['number']){
  220. $this->error('铸造的的数量不足,无法赠送');
  221. }
  222. $com = true;
  223. Db::startTrans();
  224. try {
  225. $array = [];
  226. for ($i=0;$i<$data['number'];$i++){
  227. //获取排名
  228. $rank = getRanking($data['id'])+1;
  229. $tag = getTag($data['id'],$rank,$inventory);
  230. saveRanking($data['id']);
  231. $company = '纪元部落有限公司';
  232. $hash = getCompanyHash($data['id']);
  233. $company_hash = $hash['hash'];
  234. $company_hash_time = $hash['create_at'];
  235. Db::name('hash')->where('hash',$hash['hash'])->update(['status'=>1]);
  236. $collectors_hash = '';
  237. $date = [
  238. 'order_no'=>get_order_sn(),
  239. 'tag'=>$tag,
  240. 'mid'=>$data['mid'],
  241. 'c_id'=>$data['id'],
  242. 'name'=>$info['name'],
  243. 'cover'=>$info['cover'],
  244. 'pro_info'=>json_encode($info,true),
  245. 'company'=>$company,
  246. 'company_hash'=>$company_hash,
  247. 'company_hash_time'=>$company_hash_time,
  248. 'collectors_hash'=>$collectors_hash,
  249. 'collectors_hash_time'=>''
  250. ];
  251. $array[] = $date;
  252. }
  253. Db::name('store_order_info')->insertAll($array);
  254. Db::commit();
  255. } catch (\Exception $e){
  256. Db::rollback();
  257. $com = false;
  258. }
  259. if ($com){
  260. //减掉库存
  261. loseCollectionInventory($data['id'],$data['number']);
  262. $this->success('赠送成功');
  263. }else{
  264. $this->error('赠送失败');
  265. }
  266. }else{
  267. $id=$this->request->get('id');
  268. $this->assign('id',$id);
  269. $user = Db::name('store_member')->where('is_deleted',0)->field('id,name,phone')->select();
  270. $this->assign('user',$user);
  271. $this->fetch();
  272. }
  273. }
  274. public function hash(){
  275. if($this->request->post()){
  276. $id = $this->request->post('id');
  277. $number = $this->request->post('number');
  278. if ($number<=0){
  279. $this->error('数量错误');
  280. }
  281. if (Db::name('store_collection')->where('id',$id)->setInc('casting_num',$number)){
  282. $redis = new Redis();
  283. $redis->Incrby('castingHash_'.$id,$number);
  284. $this->success('增加成功,正在铸造中');
  285. }
  286. $this->error('增加失败,请稍后重试');
  287. }else{
  288. $id=$this->request->get('id');
  289. $this->assign('id',$id);
  290. $this->fetch();
  291. }
  292. }
  293. /**
  294. * 批量赠送
  295. * @auth true
  296. * @throws \think\Exception
  297. * @throws \think\exception\PDOException
  298. */
  299. public function import()
  300. {
  301. list($msec, $sec) = explode(' ', microtime());
  302. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); //毫秒值
  303. $get = $this->request->get();
  304. $import_path = $_SERVER['DOCUMENT_ROOT'].'/'.$get['path'];
  305. $url = $get['url'];
  306. $c_id = $get['c_id'];
  307. if (getConfigValue('storage_type')=='oss'){
  308. $save_dir = "./upload/excle/"; // 服务资源目录
  309. $filename = date('Ymd').time().".xlsx"; // 自定义名称
  310. $res = $this->getFile($url,$save_dir,$filename,1);
  311. $import_path = $_SERVER['DOCUMENT_ROOT'].'/upload/excle/'.$res;
  312. }
  313. $info = getCollectionInfoHash($c_id);
  314. $num = 1;
  315. try {
  316. $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
  317. $objExcel = $objReader->load($import_path);
  318. $list = $objExcel->getActiveSheet()->toArray();
  319. $result = 0;
  320. $success = 0;
  321. $number = 0;
  322. $array = array();
  323. foreach ($list as $k=>$v){
  324. if($k>$num-1){
  325. if(array_filter($v)){
  326. $data=[];
  327. foreach ($v as $kk=>$vv){
  328. $data[IntToChr($kk)]=trim($vv);
  329. }
  330. $a['phone'] = $data['A'];
  331. $a['number'] = $data['B'];
  332. array_push($array,$a);
  333. $number = $number+$data['B'];
  334. }
  335. }
  336. }
  337. $redis = new Redis();
  338. $zhuzao_count = $redis->Llen('collectionHash_'.$c_id);
  339. if ($zhuzao_count<$number){
  340. $this->error('铸造的的数量不足,无法赠送',[],3);
  341. }
  342. $error = 0;
  343. foreach ($array as &$v){
  344. $member = Db::name('store_member')->where('phone',$v['phone'])->find();
  345. if (!$member){
  346. $error = $error+1;
  347. }else{
  348. $array1 = [];
  349. for ($i=0;$i<$v['number'];$i++){
  350. //获取排名
  351. $rank = getRanking($c_id)+1;
  352. $tag = getTag($c_id,$rank,$info['inventory']);
  353. saveRanking($c_id);
  354. $company = '纪元部落有限公司';
  355. $hash = getCompanyHash($c_id);
  356. $company_hash = $hash['hash'];
  357. $company_hash_time = $hash['create_at'];
  358. Db::name('hash')->where('hash',$hash['hash'])->update(['status'=>1]);
  359. $collectors_hash = '';
  360. $date = [
  361. 'order_no'=>get_order_sn(),
  362. 'tag'=>$tag,
  363. 'mid'=>$member['id'],
  364. 'c_id'=>$c_id,
  365. 'name'=>$info['name'],
  366. 'cover'=>$info['cover'],
  367. 'pro_info'=>json_encode($info,true),
  368. 'company'=>$company,
  369. 'company_hash'=>$company_hash,
  370. 'company_hash_time'=>$company_hash_time,
  371. 'collectors_hash'=>$collectors_hash,
  372. 'collectors_hash_time'=>''
  373. ];
  374. $array1[] = $date;
  375. }
  376. if (Db::name('store_order_info')->insertAll($array1)){
  377. loseCollectionInventory($c_id,$v['number']);
  378. $success +=1;
  379. }else{
  380. $error = $error+1;
  381. }
  382. }
  383. }
  384. if ($error<1){
  385. $this->success('成功');
  386. }else{
  387. $this->error('失败',['error'=>$error,'success'=>$success],2);
  388. }
  389. } catch (\think\exception\ValidateException $e) {
  390. $this->error($e->getMessage());
  391. }
  392. }
  393. public function detail(){
  394. $id=$this->request->get('id');
  395. $this->title = Db::name($this->table)->where('id',$id)->value('name').'--铸造明细';
  396. $query = $this->_query('hash')->where('goods_id',$id);
  397. $query->dateBetween('create_at')->order('id desc')->page();
  398. $this->fetch();
  399. }
  400. public function curlRequest($url, $headers = [], $body = [], $method = "GET")
  401. {
  402. $ch = curl_init();
  403. curl_setopt($ch, CURLOPT_URL, $url);
  404. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置请求头
  405. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);//设置请求体
  406. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //定义请求类型
  407. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  408. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  409. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  410. curl_setopt($ch, CURLOPT_HTTP_VERSION, 'CURL_HTTP_VERSION_1_1');
  411. $output = curl_exec($ch);
  412. curl_close($ch);
  413. return $output;
  414. }
  415. /**
  416. * 合成材料
  417. * @auth true
  418. * @throws \think\Exception
  419. * @throws \think\exception\PDOException
  420. */
  421. public function material(){
  422. $id=$this->request->get('id');
  423. $this->assign('cid',$id);
  424. $this->title = Db::name($this->table)->where('id',$id)->value('name').'--合成材料';
  425. $query = $this->_query('store_collection_material')->alias('a')->join('store_collection b','a.c_id=b.id');
  426. $query->where('a.cid',$id) ->field('a.*,b.name,b.cover')->order('id desc')->page();
  427. }
  428. /**
  429. * 添加商品
  430. * @auth true
  431. * @menu true
  432. * @throws \think\Exception
  433. * @throws \think\db\exception\DataNotFoundException
  434. * @throws \think\db\exception\ModelNotFoundException
  435. * @throws \think\exception\DbException
  436. * @throws \think\exception\PDOException
  437. */
  438. public function add_mater()
  439. {
  440. $cid = $this->request->get('cid');
  441. $this->assign('cid',$cid);
  442. $this->title = '添加材料';
  443. $list = Db::name('store_collection')->where('type','neq',2)->field('id,name')->select();
  444. $this->list = $list;
  445. $this->_form($this->table);
  446. }
  447. /**
  448. * 添加商品
  449. * @auth true
  450. * @menu true
  451. * @throws \think\Exception
  452. * @throws \think\db\exception\DataNotFoundException
  453. * @throws \think\db\exception\ModelNotFoundException
  454. * @throws \think\exception\DbException
  455. * @throws \think\exception\PDOException
  456. */
  457. public function edit_mater()
  458. {
  459. $cid = $this->request->get('cid');
  460. $this->assign('cid',$cid);
  461. $id = $this->request->get('id');
  462. $this->title = '编辑材料';
  463. $list = Db::name('store_collection')->where('type','neq',2)->field('id,name')->select();
  464. $this->list = $list;
  465. $vo = Db::name('store_collection_material')->where('id',$id)->find();
  466. $this->vo = $vo;
  467. $this->_form($this->table,'add_mater');
  468. }
  469. /**
  470. * 处理
  471. * @auth true
  472. * @menu true
  473. * @throws \think\Exception
  474. * @throws \think\db\exception\DataNotFoundException
  475. * @throws \think\db\exception\ModelNotFoundException
  476. * @throws \think\exception\DbException
  477. * @throws \think\exception\PDOException
  478. */
  479. public function add_edit_mater(){
  480. $data = $this->request->post();
  481. if ($data['c_id']==''){
  482. $this->error('请选择材料');
  483. }
  484. if (isset($data['id'])){
  485. $info = Db::name('store_collection_material')->where('id','neq',$data['id'])->where('cid',$data['cid'])->where('c_id',$data['c_id'])->find();
  486. if ($info) $this->error('材料已存在');
  487. $data['update_at'] = date('Y-m-d H:i:s');
  488. if (Db::name('store_collection_material')->where('id',$data['id'])->update($data)){
  489. $this->success('编辑成功');
  490. }
  491. $this->error('编辑失败,请稍后重试');
  492. }else{
  493. $info = Db::name('store_collection_material')->where('cid',$data['cid'])->where('c_id',$data['c_id'])->find();
  494. if ($info) $this->error('材料已存在');
  495. if (Db::name('store_collection_material')->insert($data)){
  496. $this->success('添加成功');
  497. }
  498. $this->error('添加失败,请稍后重试');
  499. }
  500. }
  501. /**
  502. * @auth true
  503. * @menu true
  504. * 商品删除
  505. */
  506. public function del_mater()
  507. {
  508. $data = $this->request->post();
  509. if (Db::name('store_collection_material')->where('id',$data['id'])->delete()){
  510. $this->success('恭喜您,数据更新成功');
  511. }else{
  512. $this->error('数据更新失败');
  513. }
  514. }
  515. /**
  516. * 下载文件到服务器
  517. * addtime 2020年8月28日 18:38:43
  518. */
  519. function getFile($url, $save_dir = '', $filename = '', $type = 0)
  520. {
  521. if (trim($url) == '') {
  522. return false;
  523. }
  524. if (trim($save_dir) == '') {
  525. $save_dir = './';
  526. }
  527. if (0 !== strrpos($save_dir, '/')) {
  528. $save_dir.= '/';
  529. }
  530. //创建保存目录
  531. if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {
  532. return false;
  533. }
  534. //获取远程文件所采用的方法
  535. if ($type) {
  536. $ch = curl_init();
  537. $timeout = 5;
  538. curl_setopt($ch, CURLOPT_URL, $url);
  539. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  540. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  541. $content = curl_exec($ch);
  542. curl_close($ch);
  543. } else {
  544. ob_start();
  545. readfile($url);
  546. $content = ob_get_contents();
  547. ob_end_clean();
  548. }
  549. $size = strlen($content);
  550. //文件大小
  551. $fp2 = @fopen($save_dir . $filename, 'a');
  552. fwrite($fp2, $content);
  553. fclose($fp2);
  554. unset($content, $url);
  555. return $filename;
  556. }
  557. }