123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php namespace Api\Lib;
- use Dever;
- class Util
- {
- # 生成订单号
- public function createNumber($prefix, $table, $where = [], $key = 'order_num')
- {
- $number = \Dever\Helper\Str::order($prefix);
- $where[$key] = $number;
- $state = Dever::db($table)->find($where);
- if (!$state) {
- return $number;
- }
- return $this->createNumber($prefix, $table, $where, $key);
- }
- # 获取某个setting值
- public function setting($key, $account, $project = 'api')
- {
- $setting = Dever::db('platform_setting', 'api')->find(['key' => $key]);
- $setting = Dever::db('account_setting', $project)->find(['account_id' => $account, 'platform_setting_id' => $setting['id']]);
- $value = $setting['value'] ?? 0;
- return $value;
- }
- # 获取openid 仅jspai和小程序需要openid
- public function openid($account, $env, $uid, $result = [], $project = 'api')
- {
- $account = Dever::load('account', 'api')->get($account, $project);
- if ($account) {
- $info = Dever::db('openid', 'api')->find(['account_id' => $account['id'], 'uid' => $uid, 'env' => $env]);
- if ($info) {
- $result['openid'] = $info['openid'];
- } else {
- if ($env == 3) {
- # jsapi 一般需要oauth授权
- if (empty($result['t'])) {
- $result['t'] = \Dever\Helper\Secure::login($uid);
- }
- $result['account'] = $account['key'];
- $result['link'] = Dever::url('api/oauth.code', $result);
- } elseif ($env == 5) {
- # 小程序
- $param['js_code'] = Dever::input('applet_code', 'is_string', '登录信息');
- $data = Dever::load('account', 'api')->run($account, 'applet_login', $param, $env, 'run', $project);
- if (isset($data['openid'])) {
- $result['openid'] = $data['openid'];
- $update['uid'] = $uid;
- $update['account_id'] = $account['id'];
- $update['env'] = $env;
- $update['openid'] = $data['openid'];
- Dever::db('openid', 'api')->insert($update);
- }
- }
- }
- }
- return $result;
- }
- # 获取token
- public function token($account, $env, $project = 'api')
- {
- $account = Dever::load('account', 'api')->get($account, $project);
- $result = '';
- if ($env == 5) {
- $appid = 'applet_appid';
- $secret = 'applet_secret';
- }
- $appid = $this->setting($appid, $account['id'], $project);
- $secret = $this->setting($secret, $account['id'], $project);
- if ($account && $appid && $secret) {
- $param = ['appid' => $appid, 'appsecret' => $secret];
- $info = Dever::db('token', 'api')->find($param);
- if ($info && time() <= $info['edate']) {
- $result = $info['token'];
- } else {
- $data = Dever::load('account', 'api')->run($account, 'access_token', $param, 1, 'run', $project);
- if (isset($data['access_token'])) {
- $result = $data['access_token'];
- $param['token'] = $result;
- $param['edate'] = time() + $data['expires_in'];
- if ($info) {
- Dever::db('token', 'api')->update($info['id'], $param);
- } else {
- Dever::db('token', 'api')->insert($param);
- }
- }
- }
- }
- return $result;
- }
- # 获取参数类型
- public function fieldType($platform_id)
- {
- $data = [];
- $data[] = array
- (
- 'id' => 1,
- 'name' => '格式转换',
- 'children' => Dever::db('format', 'api')->select([]),
- );
- $where = ['platform_id' => $platform_id];
- $data[] = array
- (
- 'id' => 2,
- 'name' => '加密',
- 'children' => Dever::db('platform_ssl', 'api')->select($where),
- );
- $data[] = array
- (
- 'id' => 3,
- 'name' => '签名',
- 'children' => Dever::db('platform_sign', 'api')->select($where),
- );
- return $data;
- }
- # 获取签名列表
- public function getPlatformSign($platform_id)
- {
- return Dever::db('platform_sign', 'api')->select(['platform_id' => $platform_id]);
- }
- # 格式转换
- public function format($id, $value)
- {
- $info = Dever::db('format', 'api')->find($id);
- if ($info) {
- //$info['method'] = str_replace('{value}', "'{value}'", $info['method']);
- $value = \Dever\Helper\Str::val($info['method'], ['value' => $value]);
- }
- return $value;
- }
- }
|