| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\AdminLog;
- use app\admin\model\QcodeExport;
- use app\admin\model\QcodeGoods;
- use app\admin\model\QcodeLarge;
- use app\common\controller\Backend;
- use think\Config;
- use think\Db;
- use think\Hook;
- use think\Session;
- use think\Validate;
- /**
- * 公众号扫一扫功能
- */
- class Officialaccount extends Backend
- {
- protected $noNeedLogin = ['login'];
- protected $noNeedRight = ['index', 'logout'];
- protected $layout = '';
- public function _initialize()
- {
- parent::_initialize();
- //移除HTML标签
- $this->request->filter('trim,strip_tags,htmlspecialchars');
- }
- public function index()
- {
- $goods = new QcodeGoods();
- $export = new QcodeExport();
- $large = new QcodeLarge();
- $userinfo = Session::get('admin');
- $search = "05 05284607";
- // $search = "05 05296185";
- $good_list = $goods->where('shdh', $search)->find();
- if (strpos($good_list['export_id'], ',') === false) {
- $export_id[0] = $good_list['export_id'];
- } else {
- $export_id = explode(',', $good_list['export_id']);
- }
- $data = [];
- $motter_nos = []; // 用于存储 motter_no 字段
- foreach ($export_id as $k => $v) {
- $data[$k] = $export->where('_id', $v)->find();
- // 确保 $data[$k] 是一个有效的对象
- if ($data[$k] instanceof QcodeExport) {
- if (!empty($data[$k]['order_ddbh'])) {
- $motter_nos[] = $data[$k]['order_ddbh'];
- }
- }
- $large_str = explode(',', $data[$k]['large_str']);
- $large_list = $large->name($userinfo['company'] . '_' . 'qcode_large')->whereIn('_id', $large_str)->select();
- $large_num = $large_weight = 0;
- foreach ($large_list as $value) {
- $large_num += $value['l_num']; // 累加数量
- $large_weight += $value['l_weight']; // 累加重量
- }
- // 更新数据
- $data[$k]['l_num'] = $large_num / 10000; // 转换数量
- $data[$k]['l_weight'] = $large_weight; // 更新重量
- // 添加 motter_no 到数组
- if (!empty($data[$k]['order_ddbh'])) {
- $order_ddbh[] = $data[$k]['order_ddbh'];
- }
- }
- // 拼接 motter_no 字段
- $good_list['order_ddbh'] = implode(',', $order_ddbh);
- // 继续处理其他数据
- $good_list['address'] = $userinfo['company_address'];
- $good_list['data'] = $data;
- $good_list['count'] = count($data);
- $good_list['shrq_date'] = substr($good_list['create_time'], 0, 10);
- if ($good_list['note'] === 'NULL') {
- $good_list['note'] = '';
- }
- // 新增代码
- $good_array = $good_list ? $good_list->toArray() : [];
- $dataList = $good_array['data'] ?? [];
- // 步骤1:按照 create_time 升序排序
- usort($dataList, function ($a, $b) {
- return strtotime($a['create_time']) - strtotime($b['create_time']);
- });
- // 步骤2:添加 large_ber 字段
- $start = 1;
- foreach ($dataList as &$item) {
- $count = intval($item['large_num']);
- $end = $start + $count - 1;
- $item['large_ber'] = ($start === $end) ? (string)$start : "$start-$end";
- $start = $end + 1;
- }
- unset($item);
- // echo "<pre>";
- // print_r($dataList);
- // echo "<pre>";
- // 字段中文命名映射表(仅保留这些字段)
- $fieldMap = [
- 'order_ddbh' => '销售单号',
- 'large_ber' => '托盘序号',
- 'matter_name' => '产品名称',
- 'total_boxes' => '每托箱数',
- 'tray_num' => '每层箱数',
- 'box_num' => '每托层数',
- 'large_num' => '托盘数',
- 'larger_num' => '总箱数',
- ];
- // 最终数据列表,只保留映射字段
- $finalList = [];
- foreach ($dataList as $item) {
- $row = [];
- // 添加映射字段
- foreach ($fieldMap as $fieldKey => $fieldName) {
- if (isset($item[$fieldKey])) {
- $row[$fieldName] = $item[$fieldKey];
- }
- }
- // 拼接托盘规格(格式:长×宽)
- if (isset($item['pallet_length']) && isset($item['pallet_width'])) {
- $row['托盘规格'] = $item['pallet_length'] . '×' . $item['pallet_width'];
- }
- $finalList[] = $row;
- }
- echo "<pre>";
- print_r($finalList);
- echo "</pre>";
- }
- /**
- *1.服务器接口,微信公众平台填写的url
- * http://域名/控制器/link
- */
- public function link(){
- echo '123';die;
- $echostr=$_GET['echostr'];//微信服务器提供的 随机字符串
- if ($this->check()){//验证签名是否正确
- echo $echostr;
- exit;
- }
- }
- /**
- * 2.验证签名
- */
- public function check(){
- $signature=$_GET['signature']; //微信服务器提供的 微信加密签名
- $timestamp=$_GET['timestamp']; //微信服务器提供的 时间戳
- $nonce=$_GET['nonce']; //微信服务器提供的 随机数
- $token='z9EGslrxPpbicuy48mkw'; //自己定义的 Token
- $tmpArr = array($token,$timestamp,$nonce);//数组
- sort($tmpArr); //排序
- $tmpstr=implode($tmpArr); //数据转字符串
- $tmpstr=sha1($tmpstr); //字符串加密
- if ($tmpstr==$signature){
- return true;
- }else{
- return false;
- }
- }
- /**
- * 获取access_token存进数据库
- */
- public function access_token(){
- $token = Db::name("v_access_token")->find(1);
- $date = date('Y-m-d H:i:s');
- if (strtotime($token['addtime']) > strtotime($date)){
- return $token['access_token'];
- }else{
- $appid = "你的appid";
- $appsecret = "你的appsecret ";
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
- $rt = $this->request_get($url);
- $data['access_token']=$rt['access_token'];
- $data['addtime']= date("Y-m-d H:i:s", strtotime("$date +60 min"));
- $rt =DB::name("v_access_token")->where("id='1'")->save($data);
- if ($rt){
- $token = Db::name("v_access_token")->find(1);
- return $token['access_token'];
- }else{
- return "获取access_token错误";
- }
- }
- }
- /**
- * 3.发送http请求,并返回数据
- * @param $url
- * @return mixed
- */
- public function request_get($url){
- $curl = curl_init();// 1. 初始化一个 cURL 对象
- curl_setopt($curl,CURLOPT_URL,$url);// 2.设置你需要抓取的URL
- curl_setopt($curl,CURLOPT_HEADER,0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );// 3.https必须加这个,不加不好使(不多加解释,东西太多了
- curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
- $res = curl_exec($curl);// 5. 运行cURL,请求网页
- curl_close($curl);// 6. 关闭URL请求
- $json_obj = json_decode($res,true);
- return $json_obj;
- }
- public function user(){
- //1.用户点击静默授权链接 获取用户的code
- $code = input("code");
- //2.通过code换取网页授权access_token
- $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->appid&secret=$this->appsecret&code={$code}&grant_type=authorization_code";
- $rt = $this->request_get($url);
- //拿着access_token换取用户信息
- if (!empty($rt['access_token'])) {
- Session('access_token', $rt['access_token'], 7200);
- Session('openid', $rt['openid'], 7200);
- }
- $access_token = Session('access_token');
- $openid = Session('openid');
- //3.获取用户基本信息
- $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
- $user_url = $this->request_get($url);
- $openid = $user_url['openid'];
- if($openid){
- $user = Db::name('v_user')->where("openid='$openid'")->find();
- if (!$user){
- $data['openid'] = $user_url['openid'];//用户openid
- $data['nickname'] = $user_url['nickname'];//用户名字
- $data['headimgurl'] = $user_url['headimgurl'];//用户头像
- $data['sex'] = $user_url['sex'];//用户性别
- $data['addtime'] = date('Y-m-d H:i:s');
- Db::name('v_user')->add($data);
- }//数据库没有用户信息添加到数据库mn_user用户表
- }else{
- $this->error('请使用手机进入',U('index'));
- }
- }
- }
|