zoukankan      html  css  js  c++  java
  • 项目:IT资源共享资源(登录后端)<2>

    公众号技术标签 小程序 PHP 源码 项目

    ThinkPHP5.0目录结构,新建一个模块api,该模块下面有controller、model 目录,前者放控制器,写API接口(这里是简单的api写法)。后者是模型,进行数据库处理。我们先说一下数据库的搭建。

    • 数据库名称:ziliao

    • 表前缀:ziliao_

    • 需要在ThinkPHP数据库配置里面填写相应的数据库信息

    • 下面是目录结构图

     
    image

    SQL(表ziliao_user)

    CREATE TABLE `ziliao_user` (`id` int(6) NOT NULL AUTO_INCREMENT COMMENT 'id',`openid` varchar(100) DEFAULT NULL COMMENT '小程序openid',`nickname` varchar(50) DEFAULT NULL COMMENT '微信名称',`sex` tinyint(1) DEFAULT NULL COMMENT '性别:1男0女',`image` varchar(255) DEFAULT NULL COMMENT '头像',`jifen` int(6) DEFAULT NULL COMMENT '积分',`create_time` int(11) DEFAULT NULL COMMENT '加入时间',PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

    Common控制器(获取openid和access_token等公共类)

    <?php namespace appapicontroller;use thinkController;/** * 用户类 */class Common extends Controller{private $appid = '你小程序的appid';private $secret = 'xxxxxx';//获取openidpublic function getOpenid()    {$code = input('code');//小程序传来的code值  $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$this->appid.'&secret='.$this->secret.'&js_code='.$code.'&grant_type=authorization_code';  //yourAppid为开发者appid.appSecret为开发者的appsecret,都可以从微信公众平台获取;  $info = file_get_contents($url);//发送HTTPs请求并获取返回的数据,推荐使用curl  return json(['code'=>200,'msg'=>'获取openid成功','data'=>$info]);}//获取// getAccessToken.phppublic function getAccessToken () {     $url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->$this->secret.'&secret='.$appsecret;$html = file_get_contents($url);$output = json_decode($html, true);$access_token = $output['access_token'];return $access_token;}}?>

    User控制器(进行有关于用户的接口)

    <?php namespace appapicontroller;use thinkController;/** * 用户类 */class User extends Controller{private $resp = ['code'=>-100,'msg'=>''];//首次授权登录插入用户数据public function insert_user( )    {$data = input();$model = model('User');if (empty($model->user_is_exit($data['openid']))) {$data['jifen'] = 20;//新用户积分20$data['create_time'] = time();$result = $model->frist_insert_user($data);if ($result) {$this->resp['code'] = 200;$this->resp['msg'] = '新用户插入成功';$this->resp['status'] = true;}else{$this->resp['msg'] = '新用户插入失败';$this->resp['status'] = false;}}else{$this->resp['code'] = 200;$this->resp['msg'] = '该用户已存在';$this->resp['status'] = 'exit';}return json($this->resp);}}?>

    User模型

    <?php namespace appapimodel;use thinkModel;/** * User */class User extends Model{//判断用户是否存在public function user_is_exit($openid)    {$is_exit = db('user')->where('openid',$openid)->find();return $is_exit;}//插入数据库public function frist_insert_user($where)    {$res = db('user')->data($where)->insert();return $res;}}?>
    • 如果推送的文章中,大家认可的,别忘了转发分享;

    • 为了大家更好的长久查看源码及讲解,阅读后可以收藏

    • 长按下图二维码,与小编携手编程之美

     
    image
  • 相关阅读:
    L208
    L207
    L206
    L205 EE
    L204
    监控glusterfs
    监控elssticSearch健康状态
    防火墙
    创建逻辑卷
    编译安装nginx,并使用systemd管理nginx
  • 原文地址:https://www.cnblogs.com/fengyongxian/p/10834888.html
Copyright © 2011-2022 走看看