Traceability.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\model\Chanpin;
  4. use app\api\model\Chandi;
  5. use app\api\model\Qy_basic;
  6. use app\api\model\Zd_cpmc;
  7. use app\api\model\Sym;
  8. use app\api\model\Nszy;
  9. use app\api\model\Trpsy;
  10. use OpenApi\Annotations as OA;
  11. use app\BaseController;
  12. use thans\jwt\facade\JWTAuth;
  13. //use think\Db;
  14. use think\facade\Request as re;
  15. use think\Request;
  16. use thans\jwt\JWT;
  17. use app\validate\Chandi as ChandiValidate;
  18. use think\exception\ValidateException;
  19. use rsa\Rsa;
  20. //use think\Facade\Db;
  21. class Traceability extends BaseController
  22. {
  23. use ResponseJson;
  24. protected $request;
  25. protected $middleware = [
  26. 'jwt' => ['except' => []],
  27. // 'SelectProductionInfo','TraceabilityPrint','TraceabilityPrint2','ShowInformation'
  28. ];
  29. public function __construct(Request $request)
  30. {
  31. $this->request = $request;
  32. }
  33. //溯源码打印界面,选择产品批次号接口
  34. public function SelectProductionInfo(){
  35. $data = $this->request->post();
  36. $query=$data['query'];
  37. $qydm=$data['qydm'];
  38. if($query){
  39. $result['rows']=Chanpin::
  40. where([
  41. ['t_chanpin.qydm', '=', $qydm],
  42. ['t_zd_cpmc.cpmc', 'like', '%'.$query .'%']
  43. ])
  44. ->field("t_chanpin.*,t_qy_basic.qymc,t_zd_cpmc.cpgg,t_zd_cpmc.cpmc")
  45. ->leftJoin('t_qy_basic','t_chanpin.qydm=t_qy_basic.qydm')
  46. ->leftJoin('t_zd_cpmc','t_chanpin.cpbh=t_zd_cpmc.cpbh and t_chanpin.qydm=t_zd_cpmc.qydm')
  47. ->select();
  48. $result['total']=Chanpin::
  49. where([
  50. ['t_chanpin.qydm', '=', $qydm],
  51. ['t_zd_cpmc.cpmc', 'like', '%'.$query .'%']
  52. ])
  53. ->field("t_chanpin.*,t_qy_basic.qymc,t_zd_cpmc.cpgg,t_zd_cpmc.cpmc")
  54. ->leftJoin('t_qy_basic','t_chanpin.qydm=t_qy_basic.qydm')
  55. ->leftJoin('t_zd_cpmc','t_chanpin.cpbh=t_zd_cpmc.cpbh and t_chanpin.qydm=t_zd_cpmc.qydm')
  56. ->count();
  57. }
  58. else{
  59. $result['rows']=Db::name('chanpin')
  60. ->where('t_chanpin.qydm', '=', $qydm)
  61. ->field("t_chanpin.*,t_qy_basic.qymc,t_zd_cpmc.cpgg,t_zd_cpmc.cpmc")
  62. ->leftJoin('t_zd_cpmc','t_chanpin.cpbh=t_zd_cpmc.cpbh and t_chanpin.qydm=t_zd_cpmc.qydm')
  63. ->leftJoin('t_qy_basic','t_chanpin.qydm=t_qy_basic.qydm')
  64. ->select();
  65. $result['total']=Db::name('chanpin')->where('t_chanpin.qydm', '=', $qydm)->count();
  66. }
  67. // $publicEncrypt = $rsa->publicEncrypt(json_encode($result)); //公钥加密
  68. if($result){
  69. return $this->jsonSuccessData($result);
  70. }
  71. else{
  72. return $this->jsonData(-1,"无查询结果");
  73. }
  74. }
  75. /*溯源码打印功能,先输入信息,后打码*/
  76. public function TraceabilityPrint(){
  77. //加密
  78. // require_once(dirname(__FILE__)."/Rsa.php");
  79. // $rsa = new Rsa();
  80. $data = $this->request->post();
  81. $nums = $data['nums'];
  82. $cpmc = $data['cpmc'];
  83. $pch = $data['pch'];
  84. $qydm= $data['qydm'];
  85. $bh = Qy_basic::where('t_qy_basic.qydm','=',$qydm)->field('t_qy_basic.bh,t_qy_basic.qymc,t_qy_basic.fzrtel')->find();
  86. $result = Chanpin::where([
  87. ['t_chanpin.qydm', '=', $qydm],
  88. ['t_chanpin.pch', '=',$pch]
  89. ])
  90. ->field('t_chanpin.chandi,t_chanpin.rq')
  91. ->find();
  92. $chandi = $result['chandi'];
  93. $ret = Chandi::where([
  94. ['t_chandi.qydm', '=', $qydm],
  95. ['t_chandi.chandi', '=',$chandi]
  96. ])
  97. ->field('t_chandi.*')
  98. ->find();
  99. $rets = Zd_cpmc::where([
  100. ['t_zd_cpmc.qydm', '=', $qydm],
  101. ['t_zd_cpmc.cpmc', '=',$cpmc]
  102. ])
  103. ->find();
  104. for($i=0;$i<$nums;$i++){
  105. $numbers=rand(10000,19999);
  106. $symMes['qydm'] = $qydm;
  107. $symMes['cpbh'] = $rets['cpbh'];
  108. $symMes['cdbh'] = $ret['cdbh'];
  109. $symMes['pch'] = $pch;
  110. $symMes['date'] = date("Y-m-d");
  111. $symMes['sym']=$bh['bh'].reter(substr($data['pch'],0,2)).
  112. reter(substr($data['pch'],2,2)).
  113. reter(substr($data['pch'],4,2)).
  114. substr($data['pch'],6,2).substr($numbers,0,5);
  115. $Infomation[$i]['sym'] = $symMes['sym'];
  116. $symres = Sym::create($symMes);
  117. }
  118. // dump($symres);die();
  119. /*以上部分完成溯源码的生成*/
  120. /*下面开始完成根据$symres['bh']查找该条溯源码的信息*/
  121. $infomations = array();
  122. for($i=0;$i<$nums;$i++) {
  123. $infomations[$i]['qymc'] = $bh['qymc'];
  124. $infomations[$i]['fzrtel'] = $bh['fzrtel'];
  125. $infomations[$i]['sym'] = $Infomation[$i]['sym'];
  126. $infomations[$i]['date'] = $result['rq'];
  127. $infomations[$i]['cpmc'] = $cpmc;
  128. $infomations[$i]['chandi'] = $ret['chandi'];
  129. $infomations[$i]['cpgg'] = $rets['cpgg'];
  130. $infomations[$i]['url'] = 'http://www.aielab.net/ah/S?'.$Infomation[$i]['sym'];
  131. }
  132. // $infomations['qymc'] = $bh['qymc'];
  133. // $infomations['fzrtel'] = $bh['fzrtel'];
  134. // $infomations['sym'] = $symres['sym'];
  135. // $infomations['date'] = $result['rq'];
  136. // $infomations['cpmc'] = $cpmc;
  137. // $publicEncrypt = $rsa->publicEncrypt(json_encode($infomations)); //公钥加密
  138. if($infomations){
  139. return $this->jsonSuccessData($infomations);
  140. }else{
  141. return $this->jsonData(-1,'无查询结果');
  142. }
  143. }
  144. /*溯源码打印功能,先生成溯源码,后补充信息*/
  145. public function TraceabilityPrint2(){
  146. $data = $this->request->post();
  147. $qydm = $data['qydm'];
  148. $bh = Qy_basic::where('qydm','=',$qydm)->field('bh')->find();
  149. $randomnums = generate_word();
  150. $numbers=rand(10000,19999);
  151. $symMes['sym']=$bh['bh'].$randomnums.substr($numbers,0,5);
  152. $symMes['date'] = date("Y-m-d");
  153. $symMes['qydm'] = $qydm;
  154. $symres = Sym::create($symMes);
  155. // $publicEncrypt = $rsa->publicEncrypt(json_encode($symres)); //公钥加密
  156. if($symres){
  157. return $this->jsonSuccessData($symres);
  158. }else{
  159. return $this->jsonData(-1,'无查询结果');
  160. }
  161. }
  162. /*扫码显示界面的信息*/
  163. public function ShowInformation(){
  164. //加密
  165. // require_once(dirname(__FILE__)."/Rsa.php");
  166. // $rsa = new Rsa();
  167. $data = $this->request->post();
  168. $qydm = $data['qydm'];
  169. $symresult = Sym::where('t_sym.sym','=',$data['sym'])->field("t_sym.pch,t_sym.cdbh,t_sym.cpbh,t_sym.qydm")->find();
  170. // dump($symresult);die();
  171. $information = Qy_basic::where('t_qy_basic.qydm','=',$qydm)->field("t_qy_basic.dwjj,t_qy_basic.qymc,t_qy_basic.fzrtel")->find();
  172. $productresult = Zd_cpmc::where([
  173. ['t_zd_cpmc.cpbh','=',$symresult['cpbh']],
  174. ['t_zd_cpmc.qydm','=',$qydm]
  175. ])
  176. ->field("t_zd_cpmc.cpmc,t_zd_cpmc.picname,t_zd_cpmc.bzq,t_zd_cpmc.ms,t_zd_cpmc.cpgg")
  177. ->find();
  178. $chanpinresult = Chanpin::where([
  179. ['t_chanpin.pch','=',$symresult['pch']],
  180. ['t_chanpin.qydm','=',$qydm]
  181. ])
  182. ->field("t_chanpin.rq,t_chanpin.chandi")
  183. ->find();
  184. $nszyresult = Nszy::where([
  185. ['t_nszy.cdbh','=',$symresult['cdbh']],
  186. ['t_nszy.qydm','=',$qydm]
  187. ])
  188. ->field("t_nszy.nszy,t_nszy.trpmc,t_nszy.zyl,t_nszy.zyldw,t_nszy.rq,t_nszy.zyr")
  189. ->select();
  190. $trpresult = Trpsy::where([
  191. ['t_trpsy.cdbh','=',$symresult['cdbh']],
  192. ['t_trpsy.qydm','=',$qydm]
  193. ])
  194. ->field("t_trpsy.trpm,t_trpsy.trpgg,t_trpsy.cgrq,t_trpsy.cgr")
  195. ->select();
  196. $information['cpmc'] = $productresult['cpmc'];
  197. $information['bzq'] = $productresult['bzq'];
  198. $information['ms'] = $productresult['ms'];
  199. $information['cpgg'] = $productresult['cpgg'];
  200. $information['picname'] = "http://qklapi.ahaia.net/Uploads/".$qydm."/".$productresult['picname'];
  201. $information['rq'] = $chanpinresult['rq'];
  202. $information['chandi'] = $chanpinresult['chandi'];
  203. $information['nszy'] = $nszyresult;
  204. $information['trpsy'] = $trpresult;
  205. // $publicEncrypt = $rsa->publicEncrypt(json_encode($information)); //公钥加密
  206. if($symresult['pch']&&$symresult['cpbh']!=null) {
  207. return $this->jsonSuccessData($information);
  208. }else if($symresult==null) {
  209. return $this->jsonData(-1, "该溯源码不存在");
  210. }
  211. }
  212. }