Officialaccount.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\AdminLog;
  4. use app\admin\model\QcodeExport;
  5. use app\admin\model\QcodeGoods;
  6. use app\admin\model\QcodeLarge;
  7. use app\common\controller\Backend;
  8. use think\Config;
  9. use think\Db;
  10. use think\Hook;
  11. use think\Session;
  12. use think\Validate;
  13. /**
  14. * 公众号扫一扫功能
  15. */
  16. class Officialaccount extends Backend
  17. {
  18. protected $noNeedLogin = ['login'];
  19. protected $noNeedRight = ['index', 'logout'];
  20. protected $layout = '';
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. //移除HTML标签
  25. $this->request->filter('trim,strip_tags,htmlspecialchars');
  26. }
  27. public function index()
  28. {
  29. $goods = new QcodeGoods();
  30. $export = new QcodeExport();
  31. $large = new QcodeLarge();
  32. $userinfo = Session::get('admin');
  33. $search = "05 05284607";
  34. // $search = "05 05296185";
  35. $good_list = $goods->where('shdh', $search)->find();
  36. if (strpos($good_list['export_id'], ',') === false) {
  37. $export_id[0] = $good_list['export_id'];
  38. } else {
  39. $export_id = explode(',', $good_list['export_id']);
  40. }
  41. $data = [];
  42. $motter_nos = []; // 用于存储 motter_no 字段
  43. foreach ($export_id as $k => $v) {
  44. $data[$k] = $export->where('_id', $v)->find();
  45. // 确保 $data[$k] 是一个有效的对象
  46. if ($data[$k] instanceof QcodeExport) {
  47. if (!empty($data[$k]['order_ddbh'])) {
  48. $motter_nos[] = $data[$k]['order_ddbh'];
  49. }
  50. }
  51. $large_str = explode(',', $data[$k]['large_str']);
  52. $large_list = $large->name($userinfo['company'] . '_' . 'qcode_large')->whereIn('_id', $large_str)->select();
  53. $large_num = $large_weight = 0;
  54. foreach ($large_list as $value) {
  55. $large_num += $value['l_num']; // 累加数量
  56. $large_weight += $value['l_weight']; // 累加重量
  57. }
  58. // 更新数据
  59. $data[$k]['l_num'] = $large_num / 10000; // 转换数量
  60. $data[$k]['l_weight'] = $large_weight; // 更新重量
  61. // 添加 motter_no 到数组
  62. if (!empty($data[$k]['order_ddbh'])) {
  63. $order_ddbh[] = $data[$k]['order_ddbh'];
  64. }
  65. }
  66. // 拼接 motter_no 字段
  67. $good_list['order_ddbh'] = implode(',', $order_ddbh);
  68. // 继续处理其他数据
  69. $good_list['address'] = $userinfo['company_address'];
  70. $good_list['data'] = $data;
  71. $good_list['count'] = count($data);
  72. $good_list['shrq_date'] = substr($good_list['create_time'], 0, 10);
  73. if ($good_list['note'] === 'NULL') {
  74. $good_list['note'] = '';
  75. }
  76. // 新增代码
  77. $good_array = $good_list ? $good_list->toArray() : [];
  78. $dataList = $good_array['data'] ?? [];
  79. // 步骤1:按照 create_time 升序排序
  80. usort($dataList, function ($a, $b) {
  81. return strtotime($a['create_time']) - strtotime($b['create_time']);
  82. });
  83. // 步骤2:添加 large_ber 字段
  84. $start = 1;
  85. foreach ($dataList as &$item) {
  86. $count = intval($item['large_num']);
  87. $end = $start + $count - 1;
  88. $item['large_ber'] = ($start === $end) ? (string)$start : "$start-$end";
  89. $start = $end + 1;
  90. }
  91. unset($item);
  92. // echo "<pre>";
  93. // print_r($dataList);
  94. // echo "<pre>";
  95. // 字段中文命名映射表(仅保留这些字段)
  96. $fieldMap = [
  97. 'order_ddbh' => '销售单号',
  98. 'large_ber' => '托盘序号',
  99. 'matter_name' => '产品名称',
  100. 'total_boxes' => '每托箱数',
  101. 'tray_num' => '每层箱数',
  102. 'box_num' => '每托层数',
  103. 'large_num' => '托盘数',
  104. 'larger_num' => '总箱数',
  105. ];
  106. // 最终数据列表,只保留映射字段
  107. $finalList = [];
  108. foreach ($dataList as $item) {
  109. $row = [];
  110. // 添加映射字段
  111. foreach ($fieldMap as $fieldKey => $fieldName) {
  112. if (isset($item[$fieldKey])) {
  113. $row[$fieldName] = $item[$fieldKey];
  114. }
  115. }
  116. // 拼接托盘规格(格式:长×宽)
  117. if (isset($item['pallet_length']) && isset($item['pallet_width'])) {
  118. $row['托盘规格'] = $item['pallet_length'] . '×' . $item['pallet_width'];
  119. }
  120. $finalList[] = $row;
  121. }
  122. echo "<pre>";
  123. print_r($finalList);
  124. echo "</pre>";
  125. }
  126. /**
  127. *1.服务器接口,微信公众平台填写的url
  128. * http://域名/控制器/link
  129. */
  130. public function link(){
  131. echo '123';die;
  132. $echostr=$_GET['echostr'];//微信服务器提供的 随机字符串
  133. if ($this->check()){//验证签名是否正确
  134. echo $echostr;
  135. exit;
  136. }
  137. }
  138. /**
  139. * 2.验证签名
  140. */
  141. public function check(){
  142. $signature=$_GET['signature']; //微信服务器提供的 微信加密签名
  143. $timestamp=$_GET['timestamp']; //微信服务器提供的 时间戳
  144. $nonce=$_GET['nonce']; //微信服务器提供的 随机数
  145. $token='z9EGslrxPpbicuy48mkw'; //自己定义的 Token
  146. $tmpArr = array($token,$timestamp,$nonce);//数组
  147. sort($tmpArr); //排序
  148. $tmpstr=implode($tmpArr); //数据转字符串
  149. $tmpstr=sha1($tmpstr); //字符串加密
  150. if ($tmpstr==$signature){
  151. return true;
  152. }else{
  153. return false;
  154. }
  155. }
  156. /**
  157. * 获取access_token存进数据库
  158. */
  159. public function access_token(){
  160. $token = Db::name("v_access_token")->find(1);
  161. $date = date('Y-m-d H:i:s');
  162. if (strtotime($token['addtime']) > strtotime($date)){
  163. return $token['access_token'];
  164. }else{
  165. $appid = "你的appid";
  166. $appsecret = "你的appsecret ";
  167. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
  168. $rt = $this->request_get($url);
  169. $data['access_token']=$rt['access_token'];
  170. $data['addtime']= date("Y-m-d H:i:s", strtotime("$date +60 min"));
  171. $rt =DB::name("v_access_token")->where("id='1'")->save($data);
  172. if ($rt){
  173. $token = Db::name("v_access_token")->find(1);
  174. return $token['access_token'];
  175. }else{
  176. return "获取access_token错误";
  177. }
  178. }
  179. }
  180. /**
  181. * 3.发送http请求,并返回数据
  182. * @param $url
  183. * @return mixed
  184. */
  185. public function request_get($url){
  186. $curl = curl_init();// 1. 初始化一个 cURL 对象
  187. curl_setopt($curl,CURLOPT_URL,$url);// 2.设置你需要抓取的URL
  188. curl_setopt($curl,CURLOPT_HEADER,0);
  189. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );// 3.https必须加这个,不加不好使(不多加解释,东西太多了
  190. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
  191. $res = curl_exec($curl);// 5. 运行cURL,请求网页
  192. curl_close($curl);// 6. 关闭URL请求
  193. $json_obj = json_decode($res,true);
  194. return $json_obj;
  195. }
  196. public function user(){
  197. //1.用户点击静默授权链接 获取用户的code
  198. $code = input("code");
  199. //2.通过code换取网页授权access_token
  200. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->appid&secret=$this->appsecret&code={$code}&grant_type=authorization_code";
  201. $rt = $this->request_get($url);
  202. //拿着access_token换取用户信息
  203. if (!empty($rt['access_token'])) {
  204. Session('access_token', $rt['access_token'], 7200);
  205. Session('openid', $rt['openid'], 7200);
  206. }
  207. $access_token = Session('access_token');
  208. $openid = Session('openid');
  209. //3.获取用户基本信息
  210. $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
  211. $user_url = $this->request_get($url);
  212. $openid = $user_url['openid'];
  213. if($openid){
  214. $user = Db::name('v_user')->where("openid='$openid'")->find();
  215. if (!$user){
  216. $data['openid'] = $user_url['openid'];//用户openid
  217. $data['nickname'] = $user_url['nickname'];//用户名字
  218. $data['headimgurl'] = $user_url['headimgurl'];//用户头像
  219. $data['sex'] = $user_url['sex'];//用户性别
  220. $data['addtime'] = date('Y-m-d H:i:s');
  221. Db::name('v_user')->add($data);
  222. }//数据库没有用户信息添加到数据库mn_user用户表
  223. }else{
  224. $this->error('请使用手机进入',U('index'));
  225. }
  226. }
  227. }