PHP code example of ims / common-service

1. Go to this page and download the library: Download ims/common-service library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ims / common-service example snippets


        namespace app\index\model;
		
		use ImsCommonService\BaseModel;

		/**
		 * DemoModel demo 模型
		 * @version 1.0
		 * @author: W_wang
		 * @since: 2019/1/25 9:51
		 */
		class DemoModel extends BaseModel
		{

			public function __construct()
			{
				parent::__construct();
				//初始化表名称
				$this->table = env('DB_DATABASE') . '.wh_demo';
				// 排序字段
				$this->order = 'id desc';
				$this->fields = 'id,name,wh_demo.age';//初始化返回字段
				$this->isShowSql = 1;//初始化返回sql标识0不返回,1返回

				//链表配置(可选)
				$this->joinTable = 'wh_dept';
				$this->joinVal = 'wh_demo.demo_id = wh_dept.id';
				$this->joinType = 'left';
			}
		}
    

        namespace app\index\controller;
		
		use think\Controller;
		use ImsCommonService\TpCacheService;

		/**
		 * DemoController demo 控制器
		 * @version 1.0
		 * @author: W_wang
		 * @since: 2019/1/25 9:51
		 */
		class DemoController extends Controller
		{

			public function cache()
			{
				$this->cache = new TpCacheService();
				//缓存key
				$cacheKey = 'demo';
				//写缓存
				$re = $this->cache->set($cacheKey, time(), 100);
				var_dump($re);
				//读缓存
				$re = $this->cache->get($cacheKey);
				var_dump($re);
				//删除缓存
				// $re = $this->cache->delete($cacheKey);
				var_dump($re);

				缓存组用法
				//缓存组key
				$cacheKeyMain = 'demo_main';
				//写缓存(组)
				$re = $this->cache->saveWithKey($cacheKeyMain, $cacheKey . rand(1, 100), array(
					"do" => 1,
					"data" => time()
				), 100);
				var_dump($re);
				//删除缓存(组)
				// $re = $this->cache->delWithKey($cacheKeyMain);
				var_dump($re);
			}
		}
    

        namespace app\index\controller;
		
		use think\Controller;
		use ImsCommonService\CommonService;

		/**
		 * DemoController demo 控制器
		 * @version 1.0
		 * @author: W_wang
		 * @since: 2019/1/25 9:51
		 */
		class DemoController extends Controller
		{

			public function CommonService()
			{
				//初始化类
				$CommonService = new CommonService();
				//调用方法
				$data = $CommonService->getUser();
				p($data);
			}
		}
    

        namespace app\index\controller;
		
		use think\Controller;
		use ImsCommonService\Code\Code;

		/**
		 * DemoController demo 控制器
		 * @version 1.0
		 * @author: W_wang
		 * @since: 2019/1/25 9:51
		 */
		class DemoController extends Controller
		{

			public function code()
			{
				$code = trim(input('code'));
				//验证code
				if (!empty($code)) {
					$data = Code::checkCode($code);
					echo json_encode($data);
					die;
				}

				//获取验证码
				$width = '350';
				$height = '50';
				$font_size = '20';
				echo Code::getCode($width, $height, $font_size);
				die;
			}
		}