Util.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php namespace Api\Lib;
  2. use Dever;
  3. class Util
  4. {
  5. # 生成订单号
  6. public function createNumber($prefix, $table, $where = [], $key = 'order_num')
  7. {
  8. $number = \Dever\Helper\Str::order($prefix);
  9. $where[$key] = $number;
  10. $state = Dever::db($table)->find($where);
  11. if (!$state) {
  12. return $number;
  13. }
  14. return $this->createNumber($prefix, $table, $where, $key);
  15. }
  16. # 获取某个setting值
  17. public function setting($key, $account, $project = 'api')
  18. {
  19. $setting = Dever::db('platform_setting', 'api')->find(['key' => $key]);
  20. $setting = Dever::db('account_setting', $project)->find(['account_id' => $account, 'platform_setting_id' => $setting['id']]);
  21. $value = $setting['value'] ?? 0;
  22. return $value;
  23. }
  24. # 获取openid 仅jspai和小程序需要openid
  25. public function openid($account, $env, $uid, $result = [], $project = 'api')
  26. {
  27. $account = Dever::load('account', 'api')->get($account, $project);
  28. if ($account) {
  29. $info = Dever::db('openid', 'api')->find(['account_id' => $account['id'], 'uid' => $uid, 'env' => $env]);
  30. if ($info) {
  31. $result['openid'] = $info['openid'];
  32. } else {
  33. if ($env == 3) {
  34. # jsapi 一般需要oauth授权
  35. if (empty($result['t'])) {
  36. $result['t'] = \Dever\Helper\Secure::login($uid);
  37. }
  38. $result['account'] = $account['key'];
  39. $result['link'] = Dever::url('api/oauth.code', $result);
  40. } elseif ($env == 5) {
  41. # 小程序
  42. $param['js_code'] = Dever::input('applet_code', 'is_string', '登录信息');
  43. $data = Dever::load('account', 'api')->run($account, 'applet_login', $param, $env, 'run', $project);
  44. if (isset($data['openid'])) {
  45. $result['openid'] = $data['openid'];
  46. $update['uid'] = $uid;
  47. $update['account_id'] = $account['id'];
  48. $update['env'] = $env;
  49. $update['openid'] = $data['openid'];
  50. Dever::db('openid', 'api')->insert($update);
  51. }
  52. }
  53. }
  54. }
  55. return $result;
  56. }
  57. # 获取token
  58. public function token($account, $env, $project = 'api')
  59. {
  60. $account = Dever::load('account', 'api')->get($account, $project);
  61. $result = '';
  62. if ($env == 5) {
  63. $appid = 'applet_appid';
  64. $secret = 'applet_secret';
  65. }
  66. $appid = $this->setting($appid, $account['id'], $project);
  67. $secret = $this->setting($secret, $account['id'], $project);
  68. if ($account && $appid && $secret) {
  69. $param = ['appid' => $appid, 'appsecret' => $secret];
  70. $info = Dever::db('token', 'api')->find($param);
  71. if ($info && time() <= $info['edate']) {
  72. $result = $info['token'];
  73. } else {
  74. $data = Dever::load('account', 'api')->run($account, 'access_token', $param, 1, 'run', $project);
  75. if (isset($data['access_token'])) {
  76. $result = $data['access_token'];
  77. $param['token'] = $result;
  78. $param['edate'] = time() + $data['expires_in'];
  79. if ($info) {
  80. Dever::db('token', 'api')->update($info['id'], $param);
  81. } else {
  82. Dever::db('token', 'api')->insert($param);
  83. }
  84. }
  85. }
  86. }
  87. return $result;
  88. }
  89. # 获取参数类型
  90. public function fieldType($platform_id)
  91. {
  92. $data = [];
  93. $data[] = array
  94. (
  95. 'id' => 1,
  96. 'name' => '格式转换',
  97. 'children' => Dever::db('format', 'api')->select([]),
  98. );
  99. $where = ['platform_id' => $platform_id];
  100. $data[] = array
  101. (
  102. 'id' => 2,
  103. 'name' => '加密',
  104. 'children' => Dever::db('platform_ssl', 'api')->select($where),
  105. );
  106. $data[] = array
  107. (
  108. 'id' => 3,
  109. 'name' => '签名',
  110. 'children' => Dever::db('platform_sign', 'api')->select($where),
  111. );
  112. return $data;
  113. }
  114. # 获取签名列表
  115. public function getPlatformSign($platform_id)
  116. {
  117. return Dever::db('platform_sign', 'api')->select(['platform_id' => $platform_id]);
  118. }
  119. # 格式转换
  120. public function format($id, $value)
  121. {
  122. $info = Dever::db('format', 'api')->find($id);
  123. if ($info) {
  124. //$info['method'] = str_replace('{value}', "'{value}'", $info['method']);
  125. $value = \Dever\Helper\Str::val($info['method'], ['value' => $value]);
  126. }
  127. return $value;
  128. }
  129. }