Thinkphp 手机App接口demo分享

<?php
namespace AppServ\Controller;
use Think\Controller;

class LoginController extends Controller
{
    public function index()
    {
        header("Content-type: application/json");
        if ($_POST) {
            $datas = json_decode($_POST['data'], true);
            $user = $datas['content']['user'];
            $pwd = $datas['content']['pwd'];
            //存储手机端发过来的值
            $t = var_export($datas, true);
            file_put_contents(APP_PATH . 'AppServ/Controller/post.txt', '<br><span style="color: #ff0000;font-weight: bold">'.date('H:i:s') . "</span><br>\n", FILE_APPEND);
            file_put_contents(APP_PATH . 'AppServ/Controller/post.txt', '$t :' . $t . "<br>\n", FILE_APPEND);
           
            $content = array();
            $message = jsonData('5', '数据格式错误', $content);
            if (isset($user) && !empty($user) && isset($pwd) && !empty($pwd) && $datas['token'] == "public") {
                //查询是否有此用户
                $m = D('Manager');
                $map['mg_name'] = $user;
                $map['mg_mobile'] = $user;
                $map['_logic'] = 'OR';
                $info=$m->where($map)->find();
                //file_put_contents(APP_PATH.'AppServ/Controller/post.txt','$sql:'.$m->getLastSql()."<br>\n",FILE_APPEND);

                if ($info) {
                    //查出随机字符
                    $pwd_str = $info['mg_rand_str'];
                    $pwd_true = $info['mg_pwd'];
                    //用户输入的密码
                    $pwd_false = md5(md5($pwd) . $pwd_str);

                    if ($pwd_true === $pwd_false) {
                        $data['token'] = md5($info['mg_id'] . time());
                        //设定token有效期一天
                        $data['timer'] = time() + 86400;
                        //用户id
                        $data['user_id'] = $info['mg_id'];
                        //token 存储
                        $t = D("token");
                        $msg = $t->add($data);


                        //存储成功返回
                        if ($msg) {
                            $content = array(
                                "token" => $data['token']
                            );
                            $message = jsonData('1', '登陆成功', $content);
                        } else {
                            $message = jsonData('3', '未知错误!', $content);
                        }
                    } else {
                        $message = jsonData('2', '密码错误!', $content);
                    }
                } else {
                    $message = jsonData('4', '用户名不存在!', $content);
                }
            }
            echo $message;
        } else {
            $this->display();
        }

    }

}

以下是基于ThinkPHP框架整合极光推送的示例代码: 1. 安装JPush SDK 使用composer安装JPush SDK: ``` composer require jpush/jpush ``` 2. 配置JPush 在ThinkPHP框架中,可以将JPush的配置信息写入到config目录下的jpush.php文件中,示例代码如下: ```php return [ 'app_key' => 'YOUR_APP_KEY', 'master_secret' => 'YOUR_MASTER_SECRET', ]; ``` 3. 创建JPushService类 在app/service目录下创建JPushService类,该类用于封装极光推送的相关操作。 ```php <?php namespace app\service; use JPush\Client as JPush; class JPushService { protected $jpush; public function __construct() { $config = config('jpush'); $this->jpush = new JPush($config['app_key'], $config['master_secret']); } // 发送通知 public function sendNotification($title, $content, $extras = [], $audience = 'all') { $notification = [ 'title' => $title, 'alert' => $content, ]; $message = [ 'title' => $title, 'msg_content' => $content, 'extras' => $extras, ]; $options = [ 'apns_production' => false, ]; $response = $this->jpush->push() ->setPlatform(['ios', 'android']) ->setAudience($audience) ->setNotification($notification) ->setMessage($message) ->setOptions($options) ->send(); return $response; } } ``` 4. 使用JPushService类发送推送 在控制器中使用JPushService类发送推送,示例代码如下: ```php <?php namespace app\controller; use app\service\JPushService; class PushController { public function send() { $title = '测试推送'; $content = '这是一条测试推送'; $extras = ['key1' => 'value1', 'key2' => 'value2']; $jpushService = new JPushService(); $response = $jpushService->sendNotification($title, $content, $extras); if ($response['http_code'] === 200) { return json(['code' => 0, 'message' => '推送成功']); } else { return json(['code' => -1, 'message' => '推送失败']); } } } ``` 以上就是基于ThinkPHP框架整合极光推送的示例代码。需要注意的是,示例代码中使用了JPush的免费版服务,如果需要使用更高级别的服务,需要进行相应的付费。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值